From 698f8c2f01ea549d77d7dc3338a12e04c11057b9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:02:58 +0200 Subject: Adding upstream version 1.64.0+dfsg1. Signed-off-by: Daniel Baumann --- vendor/minifier/.cargo-checksum.json | 1 + vendor/minifier/Cargo.lock | 42 + vendor/minifier/Cargo.toml | 60 + vendor/minifier/LICENSE | 21 + vendor/minifier/README.md | 30 + vendor/minifier/src/css/mod.rs | 40 + vendor/minifier/src/css/tests.rs | 286 +++ vendor/minifier/src/css/token.rs | 875 ++++++++ vendor/minifier/src/html.rs | 233 ++ vendor/minifier/src/js/mod.rs | 17 + vendor/minifier/src/js/token.rs | 1431 ++++++++++++ vendor/minifier/src/js/tools.rs | 1156 ++++++++++ vendor/minifier/src/js/utils.rs | 575 +++++ vendor/minifier/src/json/json_minifier.rs | 50 + vendor/minifier/src/json/mod.rs | 114 + vendor/minifier/src/json/read/byte_to_char.rs | 132 ++ vendor/minifier/src/json/read/internal_buffer.rs | 44 + vendor/minifier/src/json/read/internal_reader.rs | 63 + vendor/minifier/src/json/read/json_read.rs | 106 + vendor/minifier/src/json/string.rs | 100 + vendor/minifier/src/lib.rs | 10 + vendor/minifier/src/main.rs | 94 + vendor/minifier/tests/files/main.js | 2542 ++++++++++++++++++++++ vendor/minifier/tests/files/minified_main.js | 1 + vendor/minifier/tests/files/test.json | 5 + vendor/minifier/tests/js_minify.rs | 61 + 26 files changed, 8089 insertions(+) create mode 100644 vendor/minifier/.cargo-checksum.json create mode 100644 vendor/minifier/Cargo.lock create mode 100644 vendor/minifier/Cargo.toml create mode 100644 vendor/minifier/LICENSE create mode 100644 vendor/minifier/README.md create mode 100644 vendor/minifier/src/css/mod.rs create mode 100644 vendor/minifier/src/css/tests.rs create mode 100644 vendor/minifier/src/css/token.rs create mode 100644 vendor/minifier/src/html.rs create mode 100644 vendor/minifier/src/js/mod.rs create mode 100644 vendor/minifier/src/js/token.rs create mode 100644 vendor/minifier/src/js/tools.rs create mode 100644 vendor/minifier/src/js/utils.rs create mode 100644 vendor/minifier/src/json/json_minifier.rs create mode 100644 vendor/minifier/src/json/mod.rs create mode 100644 vendor/minifier/src/json/read/byte_to_char.rs create mode 100644 vendor/minifier/src/json/read/internal_buffer.rs create mode 100644 vendor/minifier/src/json/read/internal_reader.rs create mode 100644 vendor/minifier/src/json/read/json_read.rs create mode 100644 vendor/minifier/src/json/string.rs create mode 100644 vendor/minifier/src/lib.rs create mode 100644 vendor/minifier/src/main.rs create mode 100644 vendor/minifier/tests/files/main.js create mode 100644 vendor/minifier/tests/files/minified_main.js create mode 100644 vendor/minifier/tests/files/test.json create mode 100644 vendor/minifier/tests/js_minify.rs (limited to 'vendor/minifier') diff --git a/vendor/minifier/.cargo-checksum.json b/vendor/minifier/.cargo-checksum.json new file mode 100644 index 000000000..4760bcb0d --- /dev/null +++ b/vendor/minifier/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"Cargo.lock":"adb64b1e8820e83bebfc867d6e5846154743bc324754d50a2bb13742f682f1b9","Cargo.toml":"d5c1e43fffcf2a4a384ea0adfacac7c08b41bd3b05937e7e2ad1aeb0ea2aad62","LICENSE":"d64fe3199be0c90d1f88b363e6b567d5812f64c01accc8957e71381598a3d670","README.md":"7ce7cc43ebca972096db97b9f8bd211e5ac0c688f01b1732d5e2243e8c32d02d","src/css/mod.rs":"afbab7f8a657871fb567b25c5c87cb3e1c9e88d486335e154873b8c818370506","src/css/tests.rs":"056d1d3d927be4c7cece4033b48b28fe3a142939ac2f71cb10ca93f2a28a4477","src/css/token.rs":"b9fff49884a74423e11a913f49dd923b2f8c28e5a29894b08c9d1b2fbc06b85f","src/html.rs":"7b78e2c7d1b0d85065de7e6ab7e0407a74590e23ad1d20fe106c2361f5b09447","src/js/mod.rs":"b63e9cac9cc2cf7063f1eb598892fde3aa55081d0bf926df85c008e35d8e5a30","src/js/token.rs":"81918762068fba64a53dc0328222e74e8c922e657be46bb431ae4a3ac0e9fce9","src/js/tools.rs":"50e9581d6cfa2aebf2928f3df63bbb8a72149c7a40a687f248c108cf154a74ab","src/js/utils.rs":"54d97e766cb38ca1ac7c661a376f8e01c735486f2b78ae149794bb735e8f787b","src/json/json_minifier.rs":"83b5868bff0692aa655973596ad6ff34d215001be47c5c9adad8049faeec87bb","src/json/mod.rs":"e4ff2108ba01d5e937e22108c467c1c191ffc55fffedebd9c86d2a9789abb5e4","src/json/read/byte_to_char.rs":"633cdff14eb99fe7e2ba72594444b2c681a7ec13f557fecfc791b11ce8b9f130","src/json/read/internal_buffer.rs":"459e17fcc86039dd5000dbf9e8c834bfaa02cba2a2afbd19c067c9d621ba7d0b","src/json/read/internal_reader.rs":"37d9f8a31fd6477bbed676ce923a85568e9a96ef5d46db02e92e65ba13c65dea","src/json/read/json_read.rs":"dbc2c729c0c07109860e5a7c69f4fea2c4721fc17f764622392c54bd2afb1b55","src/json/string.rs":"9b9b3a40bfdb6fcbe2f3215a0506b8edfab577dd79e4a404c492fbc486896567","src/lib.rs":"ec53713f3903f80439c83a835d46b562c3cae119f2354d7a5558c0603c86c1a7","src/main.rs":"10c62d1d5544896506962d569d1507d3396bf894f501c26586a561b904956c47","tests/files/main.js":"18f43c7672eb75f532bc7f99dd2b6d644b3a11e19662774e93405928fde4d4d4","tests/files/minified_main.js":"0952e8cff3fae6c91b13a15f2923d922ccd9bce339419cc4bd52b5314180dabb","tests/files/test.json":"e07a148ef24385057cb7420209a630758c6ce80970bd54c8ffee5be207205608","tests/js_minify.rs":"66de1b596668a5f8d11e0b65d42ace3da6bc0b2b997b2d9e2fc09a488624c5af"},"package":"ac96d1e7a65f206443f95afff6de8f1690c77c97d6fc9c9bb2d2cd0662e9ff9f"} \ No newline at end of file diff --git a/vendor/minifier/Cargo.lock b/vendor/minifier/Cargo.lock new file mode 100644 index 000000000..9f5453ee1 --- /dev/null +++ b/vendor/minifier/Cargo.lock @@ -0,0 +1,42 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "0.7.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f" +dependencies = [ + "memchr", +] + +[[package]] +name = "memchr" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" + +[[package]] +name = "minifier" +version = "0.2.1" +dependencies = [ + "regex", +] + +[[package]] +name = "regex" +version = "1.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d83f127d94bdbcda4c8cc2e50f6f84f4b611f69c902699ca385a39c3a75f9ff1" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.6.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49b3de9ec5dc0a3417da371aab17d729997c15010e7fd24ff707773a33bddb64" diff --git a/vendor/minifier/Cargo.toml b/vendor/minifier/Cargo.toml new file mode 100644 index 000000000..be981e4d0 --- /dev/null +++ b/vendor/minifier/Cargo.toml @@ -0,0 +1,60 @@ +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies. +# +# If you are reading this file be aware that the original Cargo.toml +# will likely look very different (and much more reasonable). +# See Cargo.toml.orig for the original contents. + +[package] +edition = "2021" +name = "minifier" +version = "0.2.1" +authors = ["Guillaume Gomez "] +description = "Minifier tool/lib for JS/CSS/JSON files" +documentation = "https://docs.rs/minifier-rs" +readme = "README.md" +keywords = [ + "minify", + "minifier", + "JS", + "HTML", + "CSS", +] +license = "MIT" +repository = "https://github.com/GuillaumeGomez/minifier-rs" +resolver = "2" + +[profile.release] +opt-level = 3 +lto = true +codegen-units = 1 +strip = true + +[profile.release.package."*"] +opt-level = 3 +codegen-units = 1 + +[lib] +name = "minifier" + +[[bin]] +name = "minifier" +doc = false + +[dependencies.regex] +version = "1.5.5" +optional = true + +[features] +html = ["regex"] + +[badges.appveyor] +repository = "GuillaumeGomez/minifier-rs" +service = "github" + +[badges.travis-ci] +repository = "GuillaumeGomez/minifier-rs" diff --git a/vendor/minifier/LICENSE b/vendor/minifier/LICENSE new file mode 100644 index 000000000..8bd1bcc08 --- /dev/null +++ b/vendor/minifier/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2017 Guillaume Gomez + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/vendor/minifier/README.md b/vendor/minifier/README.md new file mode 100644 index 000000000..e9aa5bba6 --- /dev/null +++ b/vendor/minifier/README.md @@ -0,0 +1,30 @@ +# minifier-rs + +Minifier tool/lib for JS/CSS/JSON files. + +This crate provides both a library and binary, depending on your needs. + +## Usage + +To use the binary, just run like this: + +``` +> cargo run test.js +``` + +To use the library, add it into your `Cargo.toml` file like this: + +```toml +[dependencies] +minifier = "^0.0.1" +``` + +Then import it into your code like this: + +```rust +extern crate minifier; +``` + +## WARNING!! + +Please be aware that this is still at a very early stage of development so you shouldn't rely on it too much! diff --git a/vendor/minifier/src/css/mod.rs b/vendor/minifier/src/css/mod.rs new file mode 100644 index 000000000..224ad8126 --- /dev/null +++ b/vendor/minifier/src/css/mod.rs @@ -0,0 +1,40 @@ +// Take a look at the license at the top of the repository in the LICENSE file. + +use std::{fmt, io}; + +mod token; + +/// Minifies a given CSS source code. +/// +/// # Example +/// +/// ```rust +/// use minifier::css::minify; +/// +/// let css = r#" +/// .foo > p { +/// color: red; +/// }"#.into(); +/// let css_minified = minify(css).expect("minification failed"); +/// assert_eq!(&css_minified.to_string(), ".foo>p{color:red;}"); +/// ``` +pub fn minify<'a>(content: &'a str) -> Result, &'static str> { + token::tokenize(content).map(Minified) +} + +pub struct Minified<'a>(token::Tokens<'a>); + +impl<'a> Minified<'a> { + pub fn write(self, w: W) -> io::Result<()> { + self.0.write(w) + } +} + +impl<'a> fmt::Display for Minified<'a> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.fmt(f) + } +} + +#[cfg(test)] +mod tests; diff --git a/vendor/minifier/src/css/tests.rs b/vendor/minifier/src/css/tests.rs new file mode 100644 index 000000000..dd696afde --- /dev/null +++ b/vendor/minifier/src/css/tests.rs @@ -0,0 +1,286 @@ +// Take a look at the license at the top of the repository in the LICENSE file. + +use crate::css::minify; + +/*enum Element { + /// Rule starting with `@`: + /// + /// * charset + /// * font-face + /// * import + /// * keyframes + /// * media + AtRule(AtRule<'a>), + /// Any "normal" CSS rule block. + /// + /// Contains the selector(s) and its content. + ElementRule(Vec<&'a str>, Vec>), +} + +fn get_property<'a>(source: &'a str, iterator: &mut Peekable, + start_pos: &mut usize) -> Option> { + let mut end_pos = None; + // First we get the property name. + while let Some((pos, c)) = iterator.next() { + if let Ok(c) = ReservedChar::try_from(c) { + if c.is_useless() { + continue + } else if c == ReservedChar::OpenCurlyBrace { + return None + } else if c == ReservedChar::Colon { + end_pos = Some(pos); + break + } else { // Invalid character. + return None; + } + } else if (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '-' { + // everything's fine for now... + } else { + return None; // invalid character + } + } + if end_pos.is_none() || end_pos == Some(*start_pos + 1) { + return None; + } + while let Some((pos, c)) = iterator.next() { + if let Ok(c) = ReservedChar::try_from(c) { + if c == ReservedChar::DoubleQuote || c == ReservedChar::Quote { + get_string(source, iterator, &mut 0, c) + } else if c == ReservedChar::SemiColon { + // we reached the end! + let end_pos = end_pos.unwrap(); + *start_pos = pos; + return Property { + name: &source[start_pos..end_pos], + value: &source[end_pos..pos], + } + } + } + } + None +} + +enum Selector<'a> { + Tag(&'a str), + /// '.' + Class(&'a str), + /// '#' + Id(&'a str), + /// '<', '>', '(', ')', '+', ' ', '[', ']' + Operator(char), +} + +struct ElementRule<'a> { + selectors: Vec>, + properties: Vec>, +} + +fn get_element_rule<'a>(source: &'a str, iterator: &mut Peekable, + c: char) -> Option> { + let mut selectors = Vec::with_capacity(2); + + while let Some(s) = get_next_selector(source, iterator, c) { + if !selectors.is_empty() || !s.empty_operator() { + } + selectors.push(s); + } +} + +fn get_media_query<'a>(source: &'a str, iterator: &mut Peekable, + start_pos: &mut usize) -> Option> { + while let Some((pos, c)) = iterator.next() { + if c == '{' { + ; + } + } + None // An error occurred, sad life... +} + + +fn get_properties<'a>(source: &'a str, iterator: &mut Peekable, + start_pos: &mut usize) -> Vec { + let mut ret = Vec::with_capacity(2); + while let Some(property) = get_property(source, iterator, start_pos) { + ret.push(property); + } + ret +} + +pub struct Property<'a> { + name: &'a str, + value: &'a str, +} + +pub enum AtRule<'a> { + /// Contains the charset. Supposed to be the first rule in the style sheet and be present + /// only once. + Charset(&'a str), + /// font-face rule. + FontFace(Vec>), + /// Contains the import. + Import(&'a str), + /// Contains the rule and the block. + Keyframes(&'a str, Tokens<'a>), + /// Contains the rules and the block. + Media(Vec<&'a str>, Tokens<'a>), +} + +impl fmt::Display for AtRule { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "@{}", &match *self { + AtRule::Charset(c) => format!("charset {};", c), + AtRule::FontFace(t) => format!("font-face {{{}}};", t), + AtRule::Import(i) => format!("import {};", i), + AtRule::Keyframes(r, t) => format!("keyframes {} {{{}}}", r, t), + AtRule::Media(r, t) => format!("media {} {{{}}}", r.join(" ").collect::(), t), + }) + } +}*/ + +#[test] +fn check_minification() { + let s = r#" +/** Baguette! */ +.b > p + div:hover { + background: #fff; +} + +a[target = "_blank"] { + /* I like weird tests. */ + border: 1px solid yellow ; +} +"#; + let expected = r#"/*! Baguette! */ +.b>p+div:hover{background:#fff;}a[target="_blank"]{border:1px solid yellow;}"#; + assert_eq!(minify(s).expect("minify failed").to_string(), expected); +} + +#[test] +fn check_minification2() { + let s = r#" +h2, h3:not(.impl):not(.method):not(.type) { + background-color: #0a042f !important; +} + +:target { background: #494a3d; } + +.table-display tr td:first-child { + float: right; +} + +/* just some + * long + * + * very + * long + * comment :) + */ +@media (max-width: 700px) { + .theme-picker { + left: 10px; + top: 54px; + z-index: 1; + background-color: rgba(0, 0 , 0 , 0); + font: 15px "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace; + } +}"#; + let expected = "h2,h3:not(.impl):not(.method):not(.type){background-color:#0a042f !important;}\ + :target{background:#494a3d;}.table-display tr td:first-child{float:right;}\ + @media (max-width:700px){.theme-picker{left:10px;top:54px;z-index:1;\ + background-color:rgba(0,0,0,0);font:15px \"SFMono-Regular\",Consolas,\ + \"Liberation Mono\",Menlo,Courier,monospace;}}"; + assert_eq!(minify(s).expect("minify failed").to_string(), expected); +} + +#[test] +fn check_calc() { + let s = ".foo { width: calc(100% - 34px); }"; + let expected = ".foo{width:calc(100% - 34px);}"; + assert_eq!(minify(s).expect("minify failed").to_string(), expected); +} + +#[test] +fn check_spaces() { + let s = ".line-numbers .line-highlighted { color: #0a042f !important; }"; + let expected = ".line-numbers .line-highlighted{color:#0a042f !important;}"; + assert_eq!(minify(s).expect("minify failed").to_string(), expected); +} + +#[test] +fn check_space_after_paren() { + let s = ".docblock:not(.type-decl) a:not(.srclink) {}"; + let expected = ".docblock:not(.type-decl) a:not(.srclink){}"; + assert_eq!(minify(s).expect("minify failed").to_string(), expected); +} + +#[test] +fn check_space_after_and() { + let s = "@media only screen and (max-width : 600px) {}"; + let expected = "@media only screen and (max-width:600px){}"; + assert_eq!(minify(s).expect("minify failed").to_string(), expected); +} + +#[test] +fn check_space_after_or_not() { + let s = "@supports not ((text-align-last: justify) or (-moz-text-align-last: justify)) {}"; + let expected = "@supports not ((text-align-last:justify) or (-moz-text-align-last:justify)){}"; + assert_eq!(minify(s).expect("minify failed").to_string(), expected); +} + +#[test] +fn check_space_after_brackets() { + let s = "#main[data-behavior = \"1\"] {}"; + let expected = "#main[data-behavior=\"1\"]{}"; + assert_eq!(minify(s).expect("minify failed").to_string(), expected); + + let s = "#main[data-behavior = \"1\"] .aclass"; + let expected = "#main[data-behavior=\"1\"] .aclass"; + assert_eq!(minify(s).expect("minify failed").to_string(), expected); + + let s = "#main[data-behavior = \"1\"] ul.aclass"; + let expected = "#main[data-behavior=\"1\"] ul.aclass"; + assert_eq!(minify(s).expect("minify failed").to_string(), expected); +} + +#[test] +fn check_whitespaces_in_calc() { + let s = ".foo { width: calc(130px + 10%); }"; + let expected = ".foo{width:calc(130px + 10%);}"; + assert_eq!(minify(s).expect("minify failed").to_string(), expected); + + let s = ".foo { width: calc(130px + (45% - 10% + (12 * 2px))); }"; + let expected = ".foo{width:calc(130px + (45% - 10% + (12 * 2px)));}"; + assert_eq!(minify(s).expect("minify failed").to_string(), expected); +} + +#[test] +fn check_weird_comments() { + let s = ".test1 { + font-weight: 30em; +}/**/ +.test2 { + font-weight: 30em; +}/**/ +.test3 { + font-weight: 30em; +}/**/"; + let expected = ".test1{font-weight:30em;}.test2{font-weight:30em;}.test3{font-weight:30em;}"; + assert_eq!(minify(s).expect("minify failed").to_string(), expected); +} + +#[test] +fn check_slash_slash() { + let s = "body { + background-image: url(data:image/webp;base64,c//S4KP//ZZ/19Uj/UA==); +}"; + let expected = "body{background-image:url(data:image/webp;base64,c//S4KP//ZZ/19Uj/UA==);}"; + assert_eq!(minify(s).expect("minify failed").to_string(), expected); +} + +#[test] +fn issue_80() { + assert_eq!( + minify("@import 'i';t{x: #fff;}").unwrap().to_string(), + "@import 'i';t{x:#fff;}", + ); +} diff --git a/vendor/minifier/src/css/token.rs b/vendor/minifier/src/css/token.rs new file mode 100644 index 000000000..d2d738840 --- /dev/null +++ b/vendor/minifier/src/css/token.rs @@ -0,0 +1,875 @@ +// Take a look at the license at the top of the repository in the LICENSE file. + +use std::convert::TryFrom; +use std::fmt; +use std::iter::Peekable; +use std::str::CharIndices; + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum ReservedChar { + Comma, + SuperiorThan, + OpenParenthese, + CloseParenthese, + OpenCurlyBrace, + CloseCurlyBrace, + OpenBracket, + CloseBracket, + Colon, + SemiColon, + Slash, + Plus, + EqualSign, + Space, + Tab, + Backline, + Star, + Quote, + DoubleQuote, + Pipe, + Tilde, + Dollar, + Circumflex, +} + +impl fmt::Display for ReservedChar { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "{}", + match *self { + ReservedChar::Comma => ',', + ReservedChar::OpenParenthese => '(', + ReservedChar::CloseParenthese => ')', + ReservedChar::OpenCurlyBrace => '{', + ReservedChar::CloseCurlyBrace => '}', + ReservedChar::OpenBracket => '[', + ReservedChar::CloseBracket => ']', + ReservedChar::Colon => ':', + ReservedChar::SemiColon => ';', + ReservedChar::Slash => '/', + ReservedChar::Star => '*', + ReservedChar::Plus => '+', + ReservedChar::EqualSign => '=', + ReservedChar::Space => ' ', + ReservedChar::Tab => '\t', + ReservedChar::Backline => '\n', + ReservedChar::SuperiorThan => '>', + ReservedChar::Quote => '\'', + ReservedChar::DoubleQuote => '"', + ReservedChar::Pipe => '|', + ReservedChar::Tilde => '~', + ReservedChar::Dollar => '$', + ReservedChar::Circumflex => '^', + } + ) + } +} + +impl TryFrom for ReservedChar { + type Error = &'static str; + + fn try_from(value: char) -> Result { + match value { + '\'' => Ok(ReservedChar::Quote), + '"' => Ok(ReservedChar::DoubleQuote), + ',' => Ok(ReservedChar::Comma), + '(' => Ok(ReservedChar::OpenParenthese), + ')' => Ok(ReservedChar::CloseParenthese), + '{' => Ok(ReservedChar::OpenCurlyBrace), + '}' => Ok(ReservedChar::CloseCurlyBrace), + '[' => Ok(ReservedChar::OpenBracket), + ']' => Ok(ReservedChar::CloseBracket), + ':' => Ok(ReservedChar::Colon), + ';' => Ok(ReservedChar::SemiColon), + '/' => Ok(ReservedChar::Slash), + '*' => Ok(ReservedChar::Star), + '+' => Ok(ReservedChar::Plus), + '=' => Ok(ReservedChar::EqualSign), + ' ' => Ok(ReservedChar::Space), + '\t' => Ok(ReservedChar::Tab), + '\n' | '\r' => Ok(ReservedChar::Backline), + '>' => Ok(ReservedChar::SuperiorThan), + '|' => Ok(ReservedChar::Pipe), + '~' => Ok(ReservedChar::Tilde), + '$' => Ok(ReservedChar::Dollar), + '^' => Ok(ReservedChar::Circumflex), + _ => Err("Unknown reserved char"), + } + } +} + +impl ReservedChar { + fn is_useless(&self) -> bool { + *self == ReservedChar::Space + || *self == ReservedChar::Tab + || *self == ReservedChar::Backline + } + + fn is_operator(&self) -> bool { + Operator::try_from(*self).is_ok() + } +} + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum Operator { + Plus, + Multiply, + Minus, + Modulo, + Divide, +} + +impl fmt::Display for Operator { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "{}", + match *self { + Operator::Plus => '+', + Operator::Multiply => '*', + Operator::Minus => '-', + Operator::Modulo => '%', + Operator::Divide => '/', + } + ) + } +} + +impl TryFrom for Operator { + type Error = &'static str; + + fn try_from(value: char) -> Result { + match value { + '+' => Ok(Operator::Plus), + '*' => Ok(Operator::Multiply), + '-' => Ok(Operator::Minus), + '%' => Ok(Operator::Modulo), + '/' => Ok(Operator::Divide), + _ => Err("Unknown operator"), + } + } +} + +impl TryFrom for Operator { + type Error = &'static str; + + fn try_from(value: ReservedChar) -> Result { + match value { + ReservedChar::Slash => Ok(Operator::Divide), + ReservedChar::Star => Ok(Operator::Multiply), + ReservedChar::Plus => Ok(Operator::Plus), + _ => Err("Unknown operator"), + } + } +} + +#[derive(Eq, PartialEq, Clone, Debug)] +pub enum SelectorElement<'a> { + PseudoClass(&'a str), + Class(&'a str), + Id(&'a str), + Tag(&'a str), + Media(&'a str), +} + +impl<'a> TryFrom<&'a str> for SelectorElement<'a> { + type Error = &'static str; + + fn try_from(value: &'a str) -> Result, Self::Error> { + if let Some(value) = value.strip_prefix('.') { + if value.is_empty() { + Err("cannot determine selector") + } else { + Ok(SelectorElement::Class(value)) + } + } else if let Some(value) = value.strip_prefix('#') { + if value.is_empty() { + Err("cannot determine selector") + } else { + Ok(SelectorElement::Id(value)) + } + } else if let Some(value) = value.strip_prefix('@') { + if value.is_empty() { + Err("cannot determine selector") + } else { + Ok(SelectorElement::Media(value)) + } + } else if let Some(value) = value.strip_prefix(':') { + if value.is_empty() { + Err("cannot determine selector") + } else { + Ok(SelectorElement::PseudoClass(value)) + } + } else if value.chars().next().unwrap_or(' ').is_alphabetic() { + Ok(SelectorElement::Tag(value)) + } else { + Err("unknown selector") + } + } +} + +impl<'a> fmt::Display for SelectorElement<'a> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match *self { + SelectorElement::Class(c) => write!(f, ".{}", c), + SelectorElement::Id(i) => write!(f, "#{}", i), + SelectorElement::Tag(t) => write!(f, "{}", t), + SelectorElement::Media(m) => write!(f, "@{} ", m), + SelectorElement::PseudoClass(pc) => write!(f, ":{}", pc), + } + } +} + +#[derive(Eq, PartialEq, Clone, Debug, Copy)] +pub enum SelectorOperator { + /// `~=` + OneAttributeEquals, + /// `|=` + EqualsOrStartsWithFollowedByDash, + /// `$=` + EndsWith, + /// `^=` + FirstStartsWith, + /// `*=` + Contains, +} + +impl fmt::Display for SelectorOperator { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match *self { + SelectorOperator::OneAttributeEquals => write!(f, "~="), + SelectorOperator::EqualsOrStartsWithFollowedByDash => write!(f, "|="), + SelectorOperator::EndsWith => write!(f, "$="), + SelectorOperator::FirstStartsWith => write!(f, "^="), + SelectorOperator::Contains => write!(f, "*="), + } + } +} + +#[derive(Eq, PartialEq, Clone, Debug)] +pub enum Token<'a> { + /// Comment. + Comment(&'a str), + /// Comment starting with `/**`. + License(&'a str), + Char(ReservedChar), + Other(&'a str), + SelectorElement(SelectorElement<'a>), + String(&'a str), + SelectorOperator(SelectorOperator), + Operator(Operator), +} + +impl<'a> fmt::Display for Token<'a> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match *self { + // Token::AtRule(at_rule) => write!(f, "{}", at_rule, content), + // Token::ElementRule(selectors) => write!(f, "{}", x), + Token::Comment(c) => write!(f, "{}", c), + Token::License(l) => writeln!(f, "/*!{}*/", l), + Token::Char(c) => write!(f, "{}", c), + Token::Other(s) => write!(f, "{}", s), + Token::SelectorElement(ref se) => write!(f, "{}", se), + Token::String(s) => write!(f, "{}", s), + Token::SelectorOperator(so) => write!(f, "{}", so), + Token::Operator(op) => write!(f, "{}", op), + } + } +} + +impl<'a> Token<'a> { + fn is_comment(&self) -> bool { + matches!(*self, Token::Comment(_)) + } + + fn is_char(&self) -> bool { + matches!(*self, Token::Char(_)) + } + + fn get_char(&self) -> Option { + match *self { + Token::Char(c) => Some(c), + _ => None, + } + } + + fn is_useless(&self) -> bool { + match *self { + Token::Char(c) => c.is_useless(), + _ => false, + } + } + + fn is_a_media(&self) -> bool { + matches!(*self, Token::SelectorElement(SelectorElement::Media(_))) + } + + fn is_a_license(&self) -> bool { + matches!(*self, Token::License(_)) + } + + fn is_operator(&self) -> bool { + match *self { + Token::Operator(_) => true, + Token::Char(c) => c.is_operator(), + _ => false, + } + } +} + +impl<'a> PartialEq for Token<'a> { + fn eq(&self, other: &ReservedChar) -> bool { + match *self { + Token::Char(c) => c == *other, + _ => false, + } + } +} + +fn get_comment<'a>( + source: &'a str, + iterator: &mut Peekable>, + start_pos: &mut usize, +) -> Option> { + let mut prev = ReservedChar::Quote; + *start_pos += 1; + let builder = if let Some((_, c)) = iterator.next() { + if c == '!' || (c == '*' && iterator.peek().map(|(_, c)| c) != Some(&'/')) { + *start_pos += 1; + Token::License + } else { + if let Ok(c) = ReservedChar::try_from(c) { + prev = c; + } + Token::Comment + } + } else { + Token::Comment + }; + + for (pos, c) in iterator { + if let Ok(c) = ReservedChar::try_from(c) { + if c == ReservedChar::Slash && prev == ReservedChar::Star { + let ret = Some(builder(&source[*start_pos..pos - 1])); + *start_pos = pos; + return ret; + } + prev = c; + } else { + prev = ReservedChar::Space; + } + } + None +} + +fn get_string<'a>( + source: &'a str, + iterator: &mut Peekable>, + start_pos: &mut usize, + start: ReservedChar, +) -> Option> { + while let Some((pos, c)) = iterator.next() { + if c == '\\' { + // we skip next character + iterator.next(); + continue; + } + if let Ok(c) = ReservedChar::try_from(c) { + if c == start { + let ret = Some(Token::String(&source[*start_pos..pos + 1])); + *start_pos = pos; + return ret; + } + } + } + None +} + +fn fill_other<'a>( + source: &'a str, + v: &mut Vec>, + start: usize, + pos: usize, + is_in_block: isize, + is_in_media: bool, + is_in_attribute_selector: bool, +) { + if start < pos { + if !is_in_attribute_selector + && ((is_in_block == 0 && !is_in_media) || (is_in_media && is_in_block == 1)) + { + let mut is_pseudo_class = false; + let mut add = 0; + if let Some(&Token::Char(ReservedChar::Colon)) = v.last() { + is_pseudo_class = true; + add = 1; + } + if let Ok(s) = SelectorElement::try_from(&source[start - add..pos]) { + if is_pseudo_class { + v.pop(); + } + v.push(Token::SelectorElement(s)); + } else { + let s = &source[start..pos]; + if !s.starts_with(':') + && !s.starts_with('.') + && !s.starts_with('#') + && !s.starts_with('@') + { + v.push(Token::Other(s)); + } + } + } else { + v.push(Token::Other(&source[start..pos])); + } + } +} + +#[allow(clippy::comparison_chain)] +pub(super) fn tokenize<'a>(source: &'a str) -> Result, &'static str> { + let mut v = Vec::with_capacity(1000); + let mut iterator = source.char_indices().peekable(); + let mut start = 0; + let mut is_in_block: isize = 0; + let mut is_in_media = false; + let mut is_in_attribute_selector = false; + + loop { + let (mut pos, c) = match iterator.next() { + Some(x) => x, + None => { + fill_other( + source, + &mut v, + start, + source.len(), + is_in_block, + is_in_media, + is_in_attribute_selector, + ); + break; + } + }; + if let Ok(c) = ReservedChar::try_from(c) { + fill_other( + source, + &mut v, + start, + pos, + is_in_block, + is_in_media, + is_in_attribute_selector, + ); + is_in_media = is_in_media + || v.last() + .unwrap_or(&Token::Char(ReservedChar::Space)) + .is_a_media(); + match c { + ReservedChar::Quote | ReservedChar::DoubleQuote => { + if let Some(s) = get_string(source, &mut iterator, &mut pos, c) { + v.push(s); + } + } + ReservedChar::Star + if *v.last().unwrap_or(&Token::Char(ReservedChar::Space)) + == ReservedChar::Slash => + { + v.pop(); + if let Some(s) = get_comment(source, &mut iterator, &mut pos) { + v.push(s); + } + } + ReservedChar::OpenBracket => { + if is_in_attribute_selector { + return Err("Already in attribute selector"); + } + is_in_attribute_selector = true; + v.push(Token::Char(c)); + } + ReservedChar::CloseBracket => { + if !is_in_attribute_selector { + return Err("Unexpected ']'"); + } + is_in_attribute_selector = false; + v.push(Token::Char(c)); + } + ReservedChar::OpenCurlyBrace => { + is_in_block += 1; + v.push(Token::Char(c)); + } + ReservedChar::CloseCurlyBrace => { + is_in_block -= 1; + if is_in_block < 0 { + return Err("Too much '}'"); + } else if is_in_block == 0 { + is_in_media = false; + } + v.push(Token::Char(c)); + } + ReservedChar::SemiColon if is_in_block == 0 => { + is_in_media = false; + v.push(Token::Char(c)); + } + ReservedChar::EqualSign => { + match match v + .last() + .unwrap_or(&Token::Char(ReservedChar::Space)) + .get_char() + .unwrap_or(ReservedChar::Space) + { + ReservedChar::Tilde => Some(SelectorOperator::OneAttributeEquals), + ReservedChar::Pipe => { + Some(SelectorOperator::EqualsOrStartsWithFollowedByDash) + } + ReservedChar::Dollar => Some(SelectorOperator::EndsWith), + ReservedChar::Circumflex => Some(SelectorOperator::FirstStartsWith), + ReservedChar::Star => Some(SelectorOperator::Contains), + _ => None, + } { + Some(r) => { + v.pop(); + v.push(Token::SelectorOperator(r)); + } + None => v.push(Token::Char(c)), + } + } + c if !c.is_useless() => { + v.push(Token::Char(c)); + } + c => { + if !v + .last() + .unwrap_or(&Token::Char(ReservedChar::Space)) + .is_useless() + && (!v + .last() + .unwrap_or(&Token::Char(ReservedChar::OpenCurlyBrace)) + .is_char() + || v.last() + .unwrap_or(&Token::Char(ReservedChar::OpenCurlyBrace)) + .is_operator() + || v.last() + .unwrap_or(&Token::Char(ReservedChar::OpenCurlyBrace)) + .get_char() + == Some(ReservedChar::CloseParenthese) + || v.last() + .unwrap_or(&Token::Char(ReservedChar::OpenCurlyBrace)) + .get_char() + == Some(ReservedChar::CloseBracket)) + { + v.push(Token::Char(ReservedChar::Space)); + } else if let Ok(op) = Operator::try_from(c) { + v.push(Token::Operator(op)); + } + } + } + start = pos + 1; + } + } + Ok(Tokens(clean_tokens(v))) +} + +fn clean_tokens(mut v: Vec>) -> Vec> { + let mut i = 0; + let mut is_in_calc = false; + let mut paren = 0; + + while i < v.len() { + if v[i] == Token::Other("calc") { + is_in_calc = true; + } else if is_in_calc { + if v[i] == Token::Char(ReservedChar::CloseParenthese) { + paren -= 1; + is_in_calc = paren != 0; + } else if v[i] == Token::Char(ReservedChar::OpenParenthese) { + paren += 1; + } + } + + if v[i].is_useless() { + if i > 0 && v[i - 1] == Token::Char(ReservedChar::CloseBracket) { + if i + 1 < v.len() + && (v[i + 1].is_useless() + || v[i + 1] == Token::Char(ReservedChar::OpenCurlyBrace)) + { + v.remove(i); + continue; + } + } else if i > 0 + && (v[i - 1] == Token::Other("and") + || v[i - 1] == Token::Other("or") + || v[i - 1] == Token::Other("not")) + { + // retain the space after "and", "or" or "not" + } else if (is_in_calc && v[i - 1].is_useless()) + || !is_in_calc + && ((i > 0 + && ((v[i - 1].is_char() + && v[i - 1] != Token::Char(ReservedChar::CloseParenthese)) + || v[i - 1].is_a_media() + || v[i - 1].is_a_license())) + || (i < v.len() - 1 && v[i + 1].is_char())) + { + v.remove(i); + continue; + } + } else if v[i].is_comment() { + v.remove(i); + continue; + } + i += 1; + } + v +} + +#[derive(Debug, PartialEq, Eq, Clone)] +pub(super) struct Tokens<'a>(Vec>); + +impl<'a> Tokens<'a> { + pub(super) fn write(self, mut w: W) -> std::io::Result<()> { + for token in self.0.iter() { + write!(w, "{}", token)?; + } + Ok(()) + } +} + +impl<'a> fmt::Display for Tokens<'a> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + for token in self.0.iter() { + write!(f, "{}", token)?; + } + Ok(()) + } +} + +#[test] +fn css_basic() { + let s = r#" +/*! just some license */ +.foo > #bar p:hover { + color: blue; + background: "blue"; +} + +/* a comment! */ +@media screen and (max-width: 640px) { + .block:hover { + display: block; + } +}"#; + let expected = vec![ + Token::License(" just some license "), + Token::SelectorElement(SelectorElement::Class("foo")), + Token::Char(ReservedChar::SuperiorThan), + Token::SelectorElement(SelectorElement::Id("bar")), + Token::Char(ReservedChar::Space), + Token::SelectorElement(SelectorElement::Tag("p")), + Token::SelectorElement(SelectorElement::PseudoClass("hover")), + Token::Char(ReservedChar::OpenCurlyBrace), + Token::Other("color"), + Token::Char(ReservedChar::Colon), + Token::Other("blue"), + Token::Char(ReservedChar::SemiColon), + Token::Other("background"), + Token::Char(ReservedChar::Colon), + Token::String("\"blue\""), + Token::Char(ReservedChar::SemiColon), + Token::Char(ReservedChar::CloseCurlyBrace), + Token::SelectorElement(SelectorElement::Media("media")), + Token::Other("screen"), + Token::Char(ReservedChar::Space), + Token::Other("and"), + Token::Char(ReservedChar::Space), + Token::Char(ReservedChar::OpenParenthese), + Token::Other("max-width"), + Token::Char(ReservedChar::Colon), + Token::Other("640px"), + Token::Char(ReservedChar::CloseParenthese), + Token::Char(ReservedChar::OpenCurlyBrace), + Token::SelectorElement(SelectorElement::Class("block")), + Token::SelectorElement(SelectorElement::PseudoClass("hover")), + Token::Char(ReservedChar::OpenCurlyBrace), + Token::Other("display"), + Token::Char(ReservedChar::Colon), + Token::Other("block"), + Token::Char(ReservedChar::SemiColon), + Token::Char(ReservedChar::CloseCurlyBrace), + Token::Char(ReservedChar::CloseCurlyBrace), + ]; + assert_eq!(tokenize(s), Ok(Tokens(expected))); +} + +#[test] +fn elem_selector() { + let s = r#" +/** just some license */ +a[href*="example"] { + background: yellow; +} +a[href$=".org"] { + font-style: italic; +} +span[lang|="zh"] { + color: red; +} +a[href^="/"] { + background-color: gold; +} +div[value~="test"] { + border-width: 1px; +} +span[lang="pt"] { + font-size: 12em; /* I love big fonts */ +} +"#; + let expected = vec![ + Token::License(" just some license "), + Token::SelectorElement(SelectorElement::Tag("a")), + Token::Char(ReservedChar::OpenBracket), + Token::Other("href"), + Token::SelectorOperator(SelectorOperator::Contains), + Token::String("\"example\""), + Token::Char(ReservedChar::CloseBracket), + Token::Char(ReservedChar::OpenCurlyBrace), + Token::Other("background"), + Token::Char(ReservedChar::Colon), + Token::Other("yellow"), + Token::Char(ReservedChar::SemiColon), + Token::Char(ReservedChar::CloseCurlyBrace), + Token::SelectorElement(SelectorElement::Tag("a")), + Token::Char(ReservedChar::OpenBracket), + Token::Other("href"), + Token::SelectorOperator(SelectorOperator::EndsWith), + Token::String("\".org\""), + Token::Char(ReservedChar::CloseBracket), + Token::Char(ReservedChar::OpenCurlyBrace), + Token::Other("font-style"), + Token::Char(ReservedChar::Colon), + Token::Other("italic"), + Token::Char(ReservedChar::SemiColon), + Token::Char(ReservedChar::CloseCurlyBrace), + Token::SelectorElement(SelectorElement::Tag("span")), + Token::Char(ReservedChar::OpenBracket), + Token::Other("lang"), + Token::SelectorOperator(SelectorOperator::EqualsOrStartsWithFollowedByDash), + Token::String("\"zh\""), + Token::Char(ReservedChar::CloseBracket), + Token::Char(ReservedChar::OpenCurlyBrace), + Token::Other("color"), + Token::Char(ReservedChar::Colon), + Token::Other("red"), + Token::Char(ReservedChar::SemiColon), + Token::Char(ReservedChar::CloseCurlyBrace), + Token::SelectorElement(SelectorElement::Tag("a")), + Token::Char(ReservedChar::OpenBracket), + Token::Other("href"), + Token::SelectorOperator(SelectorOperator::FirstStartsWith), + Token::String("\"/\""), + Token::Char(ReservedChar::CloseBracket), + Token::Char(ReservedChar::OpenCurlyBrace), + Token::Other("background-color"), + Token::Char(ReservedChar::Colon), + Token::Other("gold"), + Token::Char(ReservedChar::SemiColon), + Token::Char(ReservedChar::CloseCurlyBrace), + Token::SelectorElement(SelectorElement::Tag("div")), + Token::Char(ReservedChar::OpenBracket), + Token::Other("value"), + Token::SelectorOperator(SelectorOperator::OneAttributeEquals), + Token::String("\"test\""), + Token::Char(ReservedChar::CloseBracket), + Token::Char(ReservedChar::OpenCurlyBrace), + Token::Other("border-width"), + Token::Char(ReservedChar::Colon), + Token::Other("1px"), + Token::Char(ReservedChar::SemiColon), + Token::Char(ReservedChar::CloseCurlyBrace), + Token::SelectorElement(SelectorElement::Tag("span")), + Token::Char(ReservedChar::OpenBracket), + Token::Other("lang"), + Token::Char(ReservedChar::EqualSign), + Token::String("\"pt\""), + Token::Char(ReservedChar::CloseBracket), + Token::Char(ReservedChar::OpenCurlyBrace), + Token::Other("font-size"), + Token::Char(ReservedChar::Colon), + Token::Other("12em"), + Token::Char(ReservedChar::SemiColon), + Token::Char(ReservedChar::CloseCurlyBrace), + ]; + assert_eq!(tokenize(s), Ok(Tokens(expected))); +} + +#[test] +fn check_media() { + let s = "@media (max-width: 700px) { color: red; }"; + + let expected = vec![ + Token::SelectorElement(SelectorElement::Media("media")), + Token::Char(ReservedChar::OpenParenthese), + Token::Other("max-width"), + Token::Char(ReservedChar::Colon), + Token::Other("700px"), + Token::Char(ReservedChar::CloseParenthese), + Token::Char(ReservedChar::OpenCurlyBrace), + Token::SelectorElement(SelectorElement::Tag("color")), + Token::Char(ReservedChar::Colon), + Token::Other("red"), + Token::Char(ReservedChar::SemiColon), + Token::Char(ReservedChar::CloseCurlyBrace), + ]; + + assert_eq!(tokenize(s), Ok(Tokens(expected))); +} + +#[test] +fn check_supports() { + let s = "@supports not (display: grid) { div { float: right; } }"; + + let expected = vec![ + Token::SelectorElement(SelectorElement::Media("supports")), + Token::Other("not"), + Token::Char(ReservedChar::Space), + Token::Char(ReservedChar::OpenParenthese), + Token::Other("display"), + Token::Char(ReservedChar::Colon), + Token::Other("grid"), + Token::Char(ReservedChar::CloseParenthese), + Token::Char(ReservedChar::OpenCurlyBrace), + Token::SelectorElement(SelectorElement::Tag("div")), + Token::Char(ReservedChar::OpenCurlyBrace), + Token::Other("float"), + Token::Char(ReservedChar::Colon), + Token::Other("right"), + Token::Char(ReservedChar::SemiColon), + Token::Char(ReservedChar::CloseCurlyBrace), + Token::Char(ReservedChar::CloseCurlyBrace), + ]; + + assert_eq!(tokenize(s), Ok(Tokens(expected))); +} + +#[test] +fn check_calc() { + let s = ".foo { width: calc(100% - 34px); }"; + + let expected = vec![ + Token::SelectorElement(SelectorElement::Class("foo")), + Token::Char(ReservedChar::OpenCurlyBrace), + Token::Other("width"), + Token::Char(ReservedChar::Colon), + Token::Other("calc"), + Token::Char(ReservedChar::OpenParenthese), + Token::Other("100%"), + Token::Char(ReservedChar::Space), + Token::Other("-"), + Token::Char(ReservedChar::Space), + Token::Other("34px"), + Token::Char(ReservedChar::CloseParenthese), + Token::Char(ReservedChar::SemiColon), + Token::Char(ReservedChar::CloseCurlyBrace), + ]; + assert_eq!(tokenize(s), Ok(Tokens(expected))); +} diff --git a/vendor/minifier/src/html.rs b/vendor/minifier/src/html.rs new file mode 100644 index 000000000..b6fa99362 --- /dev/null +++ b/vendor/minifier/src/html.rs @@ -0,0 +1,233 @@ +// Take a look at the license at the top of the repository in the LICENSE file. + +use regex::{Captures, Regex}; + +fn condense_whitespace(source: &str) -> String { + let lower_source = source.to_lowercase(); + if lower_source.find("\s+<").unwrap(); + let source = re.replace_all(source, "> <").into_owned(); + let re = Regex::new(r"\s{2,}|[\r\n]").unwrap(); + re.replace_all(&source, " ").into_owned() + } else { + source.trim().to_owned() + } +} + +fn condense(source: &str) -> String { + let re = Regex::new(r"<(style|script)[\w|\s].*?>").unwrap(); + let type_reg = Regex::new(r#"\s*?type="[\w|\s].*?""#).unwrap(); + re.replace_all(source, |caps: &Captures| { + type_reg.replace_all(&caps[0], "").into_owned() + }) + .into_owned() +} + +fn clean_unneeded_tags(source: &str) -> String { + let useless_tags = [ + "", + "", + "", + "", + "
", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + ]; + let mut res = source.to_owned(); + for useless_tag in &useless_tags { + res = res.replace(useless_tag, ""); + } + res +} + +fn remove_comments(source: &str) -> String { + // "build" and "endbuild" should be matched case insensitively. + let re = Regex::new("").unwrap(); + re.replace_all(source, |caps: &Captures| { + if caps[0].replace(" + +
+ +
+

A little sub title

+
    +
  • A list!
  • +
  • Who doesn't like lists?
  • +
  • Well, who cares...
  • +
+
+
+ +
Narnia \ +

Big header

\ +

A little sub \ + title

  • A list!
  • Who doesn't like lists? \ +
  • Well, who cares...
\ +