Test attr minification
This commit is contained in:
parent
74b4ab689e
commit
29d1b72230
|
@ -148,49 +148,68 @@ impl AttrValMinified {
|
|||
out.extend_from_slice(self.suffix);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub fn str(&self) -> String {
|
||||
let mut out = Vec::with_capacity(self.len());
|
||||
self.out(&mut out);
|
||||
String::from_utf8(out).unwrap()
|
||||
}
|
||||
|
||||
pub fn typ(&self) -> AttrType {
|
||||
self.typ
|
||||
}
|
||||
}
|
||||
|
||||
pub fn minify_attr_val(val: &[u8]) -> AttrValMinified {
|
||||
let double_quoted = AttrValMinified {
|
||||
pub fn encode_using_double_quotes(val: &[u8]) -> AttrValMinified {
|
||||
AttrValMinified {
|
||||
typ: AttrType::Quoted,
|
||||
prefix: b"\"",
|
||||
data: DOUBLE_QUOTED_REPLACER.replace_all(val),
|
||||
start: 0,
|
||||
suffix: b"\"",
|
||||
};
|
||||
let single_quoted = AttrValMinified {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn encode_using_single_quotes(val: &[u8]) -> AttrValMinified {
|
||||
AttrValMinified {
|
||||
typ: AttrType::Quoted,
|
||||
prefix: b"'",
|
||||
data: SINGLE_QUOTED_REPLACER.replace_all(val),
|
||||
start: 0,
|
||||
suffix: b"'",
|
||||
};
|
||||
let unquoted = {
|
||||
let data = UNQUOTED_QUOTED_REPLACER.replace_all(val);
|
||||
let first_char_encoded: &'static [u8] = match data.get(0) {
|
||||
Some(b'"') => match data.get(1) {
|
||||
Some(&s) if DIGIT[s] || s == b';' => b""",
|
||||
_ => b""",
|
||||
},
|
||||
Some(b'\'') => match data.get(1) {
|
||||
Some(&s) if DIGIT[s] || s == b';' => b"'",
|
||||
_ => b"'",
|
||||
},
|
||||
_ => b"",
|
||||
};
|
||||
let start = if !first_char_encoded.is_empty() { 1 } else { 0 };
|
||||
AttrValMinified {
|
||||
typ: AttrType::Unquoted,
|
||||
prefix: b"",
|
||||
data,
|
||||
start,
|
||||
suffix: b"",
|
||||
}
|
||||
};
|
||||
|
||||
// When lengths are equal, prefer double quotes to all and single quotes to unquoted.
|
||||
min(min(double_quoted, single_quoted), unquoted)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn encode_unquoted(val: &[u8]) -> AttrValMinified {
|
||||
let data = UNQUOTED_QUOTED_REPLACER.replace_all(val);
|
||||
let prefix: &'static [u8] = match data.get(0) {
|
||||
Some(b'"') => match data.get(1) {
|
||||
Some(&s) if DIGIT[s] || s == b';' => b""",
|
||||
_ => b""",
|
||||
},
|
||||
Some(b'\'') => match data.get(1) {
|
||||
Some(&s) if DIGIT[s] || s == b';' => b"'",
|
||||
_ => b"'",
|
||||
},
|
||||
_ => b"",
|
||||
};
|
||||
let start = if !prefix.is_empty() { 1 } else { 0 };
|
||||
AttrValMinified {
|
||||
typ: AttrType::Unquoted,
|
||||
prefix,
|
||||
data,
|
||||
start,
|
||||
suffix: b"",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn minify_attr_val(val: &[u8]) -> AttrValMinified {
|
||||
// When lengths are equal, prefer double quotes to all and single quotes to unquoted.
|
||||
min(
|
||||
min(
|
||||
encode_using_double_quotes(val),
|
||||
encode_using_single_quotes(val),
|
||||
),
|
||||
encode_unquoted(val),
|
||||
)
|
||||
}
|
||||
|
|
|
@ -6,3 +6,5 @@ pub mod css;
|
|||
pub mod element;
|
||||
pub mod instruction;
|
||||
pub mod js;
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
use crate::minify::attr::{
|
||||
encode_unquoted, encode_using_double_quotes, encode_using_single_quotes,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn test_encode_using_double_quotes() {
|
||||
let min = encode_using_double_quotes(br#"abr"aca"dab ""10";""8"$4 a""#);
|
||||
assert_eq!(
|
||||
min.str(),
|
||||
r#""abr"aca"dab ""10";""8"$4 a"""#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_encode_using_single_quotes() {
|
||||
let min = encode_using_single_quotes(br#"'abr'aca'dab '10';'8'$4 a'"#);
|
||||
assert_eq!(
|
||||
min.str(),
|
||||
r#"''abr'aca'dab ''10';''8'$4 a''"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_encode_unquoted() {
|
||||
let min = encode_unquoted(br#""123' 'h 0 ;abbibi "' \ >& 3>;"#);
|
||||
assert_eq!(
|
||||
min.str(),
|
||||
r#""123' 'h   0 ;abbibi "' \ >& 3>;"#,
|
||||
);
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
mod attr;
|
|
@ -13,7 +13,7 @@ fn test_parse_tag() {
|
|||
|
||||
|
||||
=
|
||||
"password" "a" = "b" :cd /e /=fg = /\h /i/ /j/k/l m=n=o q==\r/s/ / t] = /u / w=//>"###,
|
||||
"password" "a" = " b " :cd /e /=fg = /\h /i/ /j/k/l m=n=o q==\r/s/ / t] = /u / w=//>"###,
|
||||
);
|
||||
let tag = parse_tag(&mut code);
|
||||
assert_eq!(
|
||||
|
@ -22,7 +22,7 @@ fn test_parse_tag() {
|
|||
attributes: {
|
||||
let mut map = HashMap::<Vec<u8>, Vec<u8>>::new();
|
||||
map.insert(b"type".to_vec(), b"password".to_vec());
|
||||
map.insert(b"\"a\"".to_vec(), b"b".to_vec());
|
||||
map.insert(b"\"a\"".to_vec(), b" b ".to_vec());
|
||||
map.insert(b":cd".to_vec(), b"".to_vec());
|
||||
map.insert(b"e".to_vec(), b"".to_vec());
|
||||
map.insert(b"=fg".to_vec(), b"/\\h".to_vec());
|
||||
|
|
|
@ -2,8 +2,8 @@ fn _eval(src: &'static [u8], expected: &'static [u8], cfg: &super::Cfg) -> () {
|
|||
let mut code = src.to_vec();
|
||||
let min = super::minify(&mut code, cfg);
|
||||
assert_eq!(
|
||||
std::str::from_utf8(&min).unwrap(),
|
||||
std::str::from_utf8(expected).unwrap(),
|
||||
std::str::from_utf8(&min).unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue