mirror of
https://github.com/oxc-project/oxc.git
synced 2026-11-04 10:29:58 +00:00
perf(formatter_htmlish): skip escaping attr values without the chosen quote (#28700)
Skip useless attr escape if possible.
This commit is contained in:
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 { ("'", '\'', "'") } else { ("\"", '"', """) };
|
||||
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)]);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user