From 3ae9bb278657732278e74d8d601670880d59d6a6 Mon Sep 17 00:00:00 2001 From: Hugo Alliaume Date: Sat, 14 Nov 2026 22:05:13 +0100 Subject: [PATCH] Speed up `html_attr()` for string values and repeated attribute names For each attribute, `html_attr()` passed the value through `htmlAttrValue()`, which returns a string unchanged, and escaped the attribute name again although names repeat (`class`, `id`). String values now skip `htmlAttrValue()`, and escaped names are remembered per charset (at most 1,000 per charset). 4 string attributes go from 3521-3772 -> 1609-1947 ns, and 6 mixed attributes from 6223-8056 -> 4104-6152 ns. Caveat: the memo uses `$env->getCharset()`, so calling `setCharset()` after the escaper runtime was created could make them differ. --- CHANGELOG | 1 + extra/html-extra/HtmlExtension.php | 15 +++++++++++++-- extra/html-extra/Tests/HtmlAttrTest.php | 13 +++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index a3653427c97..ab855f2dc86 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -21,6 +21,7 @@ * Add a way to mark a `Twig\Markup` instance as safe for some escaping strategies only * Make `Environment::getExtension()` return the runtime of an extension class registered with `AttributeExtension` * Make `Environment::getRuntime()` fall back to the registered extension of the same class + * Speed up `html_attr()` for string values and repeated attribute names # 3.30.0 (2026-09-25) diff --git a/extra/html-extra/HtmlExtension.php b/extra/html-extra/HtmlExtension.php index 407f23fcfb3..e07a6176a9e 100644 --- a/extra/html-extra/HtmlExtension.php +++ b/extra/html-extra/HtmlExtension.php @@ -198,13 +198,24 @@ public static function htmlAttr(Environment $env, iterable|string|false|null ... $result = ''; $runtime = $env->getRuntime(EscaperRuntime::class); + static $escapedNames = []; + $charset = $env->getCharset(); foreach ($attr as $name => $value) { - if (null === $value = self::htmlAttrValue($name, $value)) { + // a string value is already its own rendering, htmlAttrValue() would return it unchanged + if (!\is_string($value) && null === $value = self::htmlAttrValue($name, $value)) { continue; } - $result .= $runtime->escape($name, 'html_attr_relaxed').'="'.$runtime->escape($value).'" '; + if (null === $escapedName = $escapedNames[$charset][$name] ?? null) { + if (\count($escapedNames[$charset] ?? []) >= 1000) { + $escapedNames[$charset] = []; + } + + $escapedName = $escapedNames[$charset][$name] = $runtime->escape($name, 'html_attr_relaxed'); + } + + $result .= $escapedName.'="'.$runtime->escape($value).'" '; } return trim($result); diff --git a/extra/html-extra/Tests/HtmlAttrTest.php b/extra/html-extra/Tests/HtmlAttrTest.php index b1318d70cff..5f6209074d1 100644 --- a/extra/html-extra/Tests/HtmlAttrTest.php +++ b/extra/html-extra/Tests/HtmlAttrTest.php @@ -45,6 +45,19 @@ public static function htmlAttrProvider(): \Generator ], ]; + yield 'string values render as themselves whatever the attribute name prefix' => [ + 'title="a string" style="color: red;" aria-hidden="true" data-json="{"a":1}" data-empty=""', + [ + [ + 'title' => 'a string', + 'style' => 'color: red;', + 'aria-hidden' => 'true', + 'data-json' => '{"a":1}', + 'data-empty' => '', + ], + ], + ]; + // Boolean attribute handling yield 'boolean true renders as empty string, except for aria-* and data-* it uses "true"' => [ 'required="" aria-disabled="true" data-yes="true"',