Test attr minification
This commit is contained in:
parent
74b4ab689e
commit
29d1b72230
|
@ -148,29 +148,41 @@ 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 = {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn encode_unquoted(val: &[u8]) -> AttrValMinified {
|
||||
let data = UNQUOTED_QUOTED_REPLACER.replace_all(val);
|
||||
let first_char_encoded: &'static [u8] = match data.get(0) {
|
||||
let prefix: &'static [u8] = match data.get(0) {
|
||||
Some(b'"') => match data.get(1) {
|
||||
Some(&s) if DIGIT[s] || s == b';' => b""",
|
||||
_ => b""",
|
||||
|
@ -181,16 +193,23 @@ pub fn minify_attr_val(val: &[u8]) -> AttrValMinified {
|
|||
},
|
||||
_ => b"",
|
||||
};
|
||||
let start = if !first_char_encoded.is_empty() { 1 } else { 0 };
|
||||
let start = if !prefix.is_empty() { 1 } else { 0 };
|
||||
AttrValMinified {
|
||||
typ: AttrType::Unquoted,
|
||||
prefix: b"",
|
||||
prefix,
|
||||
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 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;
|
|
@ -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