Update minify and respect HTML spec

This commit is contained in:
Vincent Prouillet 2021-08-09 20:28:14 +02:00
parent 17f3fe2bda
commit 85f68f87cc
3 changed files with 12 additions and 19 deletions

4
Cargo.lock generated
View file

@ -1404,9 +1404,9 @@ dependencies = [
[[package]]
name = "minify-html"
version = "0.4.11"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee59033f9253bab9ec605e58b6a9f9ac22548346cbc88927194a7f56527b541b"
checksum = "4724a59fe4c7e7a6a0389cf151027b11a29ee0a4f838e7871533896d68910907"
dependencies = [
"aho-corasick",
"lazy_static",

View file

@ -14,7 +14,7 @@ serde = { version = "1.0", features = ["derive"] }
slug = "0.1"
percent-encoding = "2"
filetime = "0.2.12"
minify-html = "0.4.2"
minify-html = "0.5"
errors = { path = "../errors" }

View file

@ -1,23 +1,16 @@
use errors::{bail, Result};
use minify_html::{with_friendly_error, Cfg};
use minify_html::{minify, Cfg};
pub fn html(html: String) -> Result<String> {
let cfg = &Cfg { minify_js: false, minify_css: false };
let mut input_bytes = html.as_bytes().to_vec();
let mut cfg = Cfg::new();
cfg.ensure_spec_compliant_unquoted_attribute_values = true;
cfg.keep_spaces_between_attributes = true;
cfg.keep_html_and_head_opening_tags = true;
match with_friendly_error(&mut input_bytes, cfg) {
Ok(len) => match std::str::from_utf8(&input_bytes[..len]) {
let minified = minify(html.as_bytes(), &cfg);
match std::str::from_utf8(&minified) {
Ok(result) => Ok(result.to_string()),
Err(err) => bail!("Failed to convert bytes to string : {}", err),
},
Err(minify_error) => {
bail!(
"Failed to truncate html at character {}: {} \n {}",
minify_error.position,
minify_error.message,
minify_error.code_context
);
}
}
}