From dce14853f6e6dee35ff35687e59038200942fc27 Mon Sep 17 00:00:00 2001 From: leaysgur <6259812+leaysgur@users.noreply.github.com> Date: Mon, 2 Nov 2026 05:03:28 +0000 Subject: [PATCH] perf(formatter_htmlish): skip escaping attr values without the chosen quote (#28700) Skip useless attr escape if possible. --- crates/oxc_formatter_htmlish/src/print/attribute.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/crates/oxc_formatter_htmlish/src/print/attribute.rs b/crates/oxc_formatter_htmlish/src/print/attribute.rs index 02e6f86983..85ffecaef3 100644 --- a/crates/oxc_formatter_htmlish/src/print/attribute.rs +++ b/crates/oxc_formatter_htmlish/src/print/attribute.rs @@ -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)]); }