Fix html_attr dropping style declarations whose value is zero - #4916
Merged
Conversation
InlineStyle::getValue() skipped a declaration when empty($value) was true,
which also matches 0, 0.0 and '0'. Those are ordinary CSS values (opacity: 0,
z-index: 0, margin: 0, flex-grow: 0), so they were silently dropped, and a
style map containing only such declarations omitted the attribute entirely.
The sibling SeparatedTokenList::getValue() already uses an explicit
null/false test, so class token lists keep a 0 while style declarations did
not. The numeric-key branch of InlineStyle itself never consulted empty(),
so {style: ['opacity: 0']} printed while {style: {opacity: 0}} did not.
smnandre
reviewed
Sep 3, 2026
Contributor
|
Thank you @dylanpulver. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
InlineStyle::getValue()skips a declaration whenempty($value)is true. That also matches0,0.0and'0', which are ordinary CSS values.{{ html_attr({style: {opacity: 0}}) }} {# "" #} {{ html_attr({style: {'flex-grow': 0, color: 'red'}}) }} {# style="color: red;" #} {{ html_attr({style: ['opacity: 0']}) }} {# style="opacity: 0;" #} {{ html_attr({class: [0, 'a']}) }} {# class="0 a" #}Same declaration, opposite result. The numeric-key branch never consults
empty(), and the siblingSeparatedTokenList::getValue()already uses an explicitnull !== $v && false !== $vtest, so token lists keep a0. When every declaration is dropped the attribute is omitted entirely, so{style: {opacity: 0}}renders nothing at all, andHtmlExtension::htmlAttrValue('style', ['opacity' => 0])returnsnull.Silently affects
opacity,z-index,margin,padding,border,flex-growand custom properties.The guard now lists the values that carry no declaration.
null,false,''andtruestill skip, and[]is kept in that list so empty arrays behave exactly as before.html_attr.rstdocuments thenull/false/trueomission rules and says nothing about zero, so no doc change is needed.HtmlAttrTest.phpalready has a case named "zero is not treated as falsy", but only for a plain attribute value.3 tests added. Reverting the fix fails the first; a naive
null/false-only guard fails the other two.