From 85f68f87cc06e7fe1b59ff98bb8415769dc72722 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Mon, 9 Aug 2021 20:28:14 +0200 Subject: [PATCH] Update minify and respect HTML spec --- Cargo.lock | 4 ++-- components/utils/Cargo.toml | 2 +- components/utils/src/minify.rs | 25 +++++++++---------------- 3 files changed, 12 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index edbca47c..0d5cd36d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", diff --git a/components/utils/Cargo.toml b/components/utils/Cargo.toml index d12e0f1a..0fc2ae7e 100644 --- a/components/utils/Cargo.toml +++ b/components/utils/Cargo.toml @@ -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" } diff --git a/components/utils/src/minify.rs b/components/utils/src/minify.rs index faab7812..3ad91e7d 100644 --- a/components/utils/src/minify.rs +++ b/components/utils/src/minify.rs @@ -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 { - 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]) { - 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 - ); - } + 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), } }