Test attr minification
This commit is contained in:
parent
74b4ab689e
commit
29d1b72230
|
@ -148,49 +148,68 @@ impl AttrValMinified {
|
||||||
out.extend_from_slice(self.suffix);
|
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 {
|
pub fn typ(&self) -> AttrType {
|
||||||
self.typ
|
self.typ
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn minify_attr_val(val: &[u8]) -> AttrValMinified {
|
pub fn encode_using_double_quotes(val: &[u8]) -> AttrValMinified {
|
||||||
let double_quoted = AttrValMinified {
|
AttrValMinified {
|
||||||
typ: AttrType::Quoted,
|
typ: AttrType::Quoted,
|
||||||
prefix: b"\"",
|
prefix: b"\"",
|
||||||
data: DOUBLE_QUOTED_REPLACER.replace_all(val),
|
data: DOUBLE_QUOTED_REPLACER.replace_all(val),
|
||||||
start: 0,
|
start: 0,
|
||||||
suffix: b"\"",
|
suffix: b"\"",
|
||||||
};
|
}
|
||||||
let single_quoted = AttrValMinified {
|
}
|
||||||
|
|
||||||
|
pub fn encode_using_single_quotes(val: &[u8]) -> AttrValMinified {
|
||||||
|
AttrValMinified {
|
||||||
typ: AttrType::Quoted,
|
typ: AttrType::Quoted,
|
||||||
prefix: b"'",
|
prefix: b"'",
|
||||||
data: SINGLE_QUOTED_REPLACER.replace_all(val),
|
data: SINGLE_QUOTED_REPLACER.replace_all(val),
|
||||||
start: 0,
|
start: 0,
|
||||||
suffix: b"'",
|
suffix: b"'",
|
||||||
};
|
}
|
||||||
let unquoted = {
|
}
|
||||||
let data = UNQUOTED_QUOTED_REPLACER.replace_all(val);
|
|
||||||
let first_char_encoded: &'static [u8] = match data.get(0) {
|
pub fn encode_unquoted(val: &[u8]) -> AttrValMinified {
|
||||||
Some(b'"') => match data.get(1) {
|
let data = UNQUOTED_QUOTED_REPLACER.replace_all(val);
|
||||||
Some(&s) if DIGIT[s] || s == b';' => b""",
|
let prefix: &'static [u8] = match data.get(0) {
|
||||||
_ => b""",
|
Some(b'"') => match data.get(1) {
|
||||||
},
|
Some(&s) if DIGIT[s] || s == b';' => b""",
|
||||||
Some(b'\'') => match data.get(1) {
|
_ => b""",
|
||||||
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 };
|
_ => b"",
|
||||||
AttrValMinified {
|
};
|
||||||
typ: AttrType::Unquoted,
|
let start = if !prefix.is_empty() { 1 } else { 0 };
|
||||||
prefix: b"",
|
AttrValMinified {
|
||||||
data,
|
typ: AttrType::Unquoted,
|
||||||
start,
|
prefix,
|
||||||
suffix: 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 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 element;
|
||||||
pub mod instruction;
|
pub mod instruction;
|
||||||
pub mod js;
|
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);
|
let tag = parse_tag(&mut code);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
@ -22,7 +22,7 @@ fn test_parse_tag() {
|
||||||
attributes: {
|
attributes: {
|
||||||
let mut map = HashMap::<Vec<u8>, Vec<u8>>::new();
|
let mut map = HashMap::<Vec<u8>, Vec<u8>>::new();
|
||||||
map.insert(b"type".to_vec(), b"password".to_vec());
|
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":cd".to_vec(), b"".to_vec());
|
||||||
map.insert(b"e".to_vec(), b"".to_vec());
|
map.insert(b"e".to_vec(), b"".to_vec());
|
||||||
map.insert(b"=fg".to_vec(), b"/\\h".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 mut code = src.to_vec();
|
||||||
let min = super::minify(&mut code, cfg);
|
let min = super::minify(&mut code, cfg);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
std::str::from_utf8(&min).unwrap(),
|
||||||
std::str::from_utf8(expected).unwrap(),
|
std::str::from_utf8(expected).unwrap(),
|
||||||
std::str::from_utf8(&min).unwrap()
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue