Skip to content

Commit

Permalink
Update minify and respect HTML spec
Browse files Browse the repository at this point in the history
  • Loading branch information
Keats committed Aug 9, 2021
1 parent 17f3fe2 commit 85f68f8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 19 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion components/utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }

Expand Down
25 changes: 9 additions & 16 deletions components/utils/src/minify.rs
Original file line number Diff line number Diff line change
@@ -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]) {
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),
}
}

Expand Down

0 comments on commit 85f68f8

Please sign in to comment.