Merge pull request #1305 from idolactivities/fix/1304-utf8-minification

Ensure minified HTML is truncated before converting to String
This commit is contained in:
Vincent Prouillet 2021-01-13 09:53:33 +01:00 committed by GitHub
commit ca1a6dd69d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -6,8 +6,8 @@ pub fn html(html: String) -> Result<String> {
let mut input_bytes = html.as_bytes().to_vec(); let mut input_bytes = html.as_bytes().to_vec();
match with_friendly_error(&mut input_bytes, cfg) { match with_friendly_error(&mut input_bytes, cfg) {
Ok(len) => match std::str::from_utf8(&input_bytes) { Ok(len) => match std::str::from_utf8(&input_bytes[..len]) {
Ok(result) => Ok(result[..len].to_string()), Ok(result) => Ok(result.to_string()),
Err(err) => bail!("Failed to convert bytes to string : {}", err), Err(err) => bail!("Failed to convert bytes to string : {}", err),
}, },
Err(minify_error) => { Err(minify_error) => {
@ -47,4 +47,16 @@ mod tests {
let res = html(input.to_owned()).unwrap(); let res = html(input.to_owned()).unwrap();
assert_eq!(res, expected); assert_eq!(res, expected);
} }
// https://github.com/getzola/zola/issues/1304
#[test]
fn can_minify_multibyte_characters() {
let input = r#"
"#;
let expected = r#"俺が好きなのはキツネの…ケツねw ー丁寧なインタネット生活の人より"#;
let res = html(input.to_owned()).unwrap();
assert_eq!(res, expected);
}
} }