diff --git a/CHANGELOG b/CHANGELOG index 47e3a7240e4..85043d29f2f 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ # 3.29.0 (2026-XX-XX) + * Fix `html_attr` dropping `style` declarations whose value is `0`, `0.0` or `'0'` * Fix the `default` filter fallback emitting an undefined variable warning when it uses the null-safe operator * Fix the `matches` operator silently treating PCRE execution errors as non-matches * Add the `HtmlExtension::htmlAttrValue()` method to resolve a single HTML attribute value the way the `html_attr` function renders it diff --git a/extra/html-extra/HtmlAttr/InlineStyle.php b/extra/html-extra/HtmlAttr/InlineStyle.php index 8aa36b6462f..8f16967bd66 100644 --- a/extra/html-extra/HtmlAttr/InlineStyle.php +++ b/extra/html-extra/HtmlAttr/InlineStyle.php @@ -55,7 +55,9 @@ public function getValue(): ?string { $style = ''; foreach ($this->value as $name => $value) { - if (empty($value) || true === $value) { + // `0`, `0.0` and `'0'` are valid CSS values, so only the values that carry + // no declaration at all are skipped here + if (null === $value || false === $value || true === $value || '' === $value || [] === $value) { continue; } if (is_numeric($name)) { diff --git a/extra/html-extra/Tests/HtmlAttrTest.php b/extra/html-extra/Tests/HtmlAttrTest.php index 38898b86f98..b1318d70cff 100644 --- a/extra/html-extra/Tests/HtmlAttrTest.php +++ b/extra/html-extra/Tests/HtmlAttrTest.php @@ -157,6 +157,27 @@ public static function htmlAttrProvider(): \Generator ], ]; + yield 'zero style declaration values are printed' => [ + 'style="opacity: 0; z-index: 0; margin: 0;"', + [ + ['style' => ['opacity' => 0, 'z-index' => '0', 'margin' => 0.0]], + ], + ]; + + yield 'null, false and empty string style declaration values are omitted' => [ + 'style="color: red;"', + [ + ['style' => ['a' => null, 'b' => false, 'c' => '', 'd' => true, 'color' => 'red']], + ], + ]; + + yield 'style attribute is omitted when every declaration is omitted' => [ + '', + [ + ['style' => ['a' => null, 'b' => false, 'c' => '']], + ], + ]; + yield 'merging style attributes overrides by key' => [ 'style="color: blue; font-size: 14px;"', [