perf(formatter_htmlish): skip escaping attr values without the chosen quote (#28700)

Skip useless attr escape if possible.
This commit is contained in:
leaysgur committed 2026-11-02 05:03:28 +00:00
1 parent 73a368faee
commit dce14853f6
1 file changed
+7 -2
@@ -198,10 +198,15 @@ fn write_tailwind_classes<'a>(
fn write_quoted_value<'a>(raw: &'a str, f: &mut HtmlishFormatter<'_, 'a>) {
let doubles = raw.bytes().filter(|&b| b == b'"').count();
let singles = raw.bytes().filter(|&b| b == b'\'').count();
// A `char` pattern: a 1-char `&str` would build a substring searcher for every value
// A `char` pattern: a 1-char `&str` would build a substring searcher per call
let (quote, pattern, escaped) =
if doubles > singles { ("'", '\'', "&apos;") } else { ("\"", '"', "&quot;") };
let printed = arena_cow_str(&raw.cow_replace(pattern, escaped), f);
// The chosen quote is the rarer one, so most values have none to escape
let printed = if doubles.min(singles) == 0 {
raw
} else {
arena_cow_str(&raw.cow_replace(pattern, escaped), f)
};
write!(f, [text(quote), text(printed), text(quote)]);
}