Improve attr min performance

This commit is contained in:
Wilson Lin 2021-08-08 11:23:24 +10:00
parent 3831a2605e
commit 472c4d8fac
1 changed files with 29 additions and 23 deletions

View File

@ -108,7 +108,7 @@ fn build_unquoted_replacer() -> Replacer {
} }
// If spec compliance is required, these characters must also be encoded in an unquoted attr value, // If spec compliance is required, these characters must also be encoded in an unquoted attr value,
// as well as `<` and `>`. // as well as whitespace, `<`, and `>`.
static WHATWG_UNQUOTED: &[(u8, &[u8])] = &[ static WHATWG_UNQUOTED: &[(u8, &[u8])] = &[
(b'"', b"&#34"), (b'"', b"&#34"),
(b'\'', b"&#39"), (b'\'', b"&#39"),
@ -240,29 +240,35 @@ pub fn encode_using_single_quotes(val: &[u8]) -> AttrMinifiedValue {
} }
pub fn encode_unquoted(val: &[u8], whatwg: bool) -> AttrMinifiedValue { pub fn encode_unquoted(val: &[u8], whatwg: bool) -> AttrMinifiedValue {
let data = if whatwg { if whatwg {
WHATWG_UNQUOTED_QUOTED_REPLACER.replace_all(val) AttrMinifiedValue {
quoted: false,
prefix: b"",
data: WHATWG_UNQUOTED_QUOTED_REPLACER.replace_all(val),
start: 0,
suffix: b"",
}
} else { } else {
UNQUOTED_QUOTED_REPLACER.replace_all(val) let data = UNQUOTED_QUOTED_REPLACER.replace_all(val);
}; let prefix: &'static [u8] = match data.get(0) {
let prefix: &'static [u8] = match data.get(0) { Some(b'"') => match data.get(1) {
Some(b'"') => match data.get(1) { Some(&c2) if DIGIT[c2] || c2 == b';' => b"&#34;",
Some(&c2) if DIGIT[c2] || c2 == b';' => b"&#34;", _ => b"&#34",
_ => b"&#34", },
}, Some(b'\'') => match data.get(1) {
Some(b'\'') => match data.get(1) { Some(&c2) if DIGIT[c2] || c2 == b';' => b"&#39;",
Some(&c2) if DIGIT[c2] || c2 == b';' => b"&#39;", _ => b"&#39",
_ => b"&#39", },
}, _ => b"",
_ => b"", };
}; let start = if !prefix.is_empty() { 1 } else { 0 };
let start = if !prefix.is_empty() { 1 } else { 0 }; AttrMinifiedValue {
AttrMinifiedValue { quoted: false,
quoted: false, prefix,
prefix, data,
data, start,
start, suffix: b"",
suffix: b"", }
} }
} }