Repository navigation
Conversation
merge_style_property() split the style attribute on every semicolon
and colon, so a declaration value containing either character was
cut. A value with a colon lost everything after it:
background-image:url(https://example.com/a.png)
=> background-image:url(https;color:green;
A fragment without a colon raised "Undefined array key 1".
Rewrite the function on WP_HTML_Style_Attribute_Processor: remove
every declaration of the property with next_declaration( $name ) and
remove_declaration(), then append_declaration() the new value. The
other declarations keep their original text, and the appended
declaration is written as "name: value;".
Property names match as the processor defines: ASCII-case-insensitive
for standard properties, exact for custom properties. A value that is
not one valid declaration value is rejected by append_declaration(),
so the property is removed and nothing is added.
Update the expected strings in the tests for the new output format.
Port the tests from WordPress#12733 with expected strings in the processor's output format, and add tests for property name matching, for replacing a property whose value has a quoted semicolon, and for rejected values.
Owner
Author
|
Superseded by #111, which rebuilds the whole data-wp-style directive on one style processor per element. |
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.
Caution
This is a work in progress draft and is not ready for any serious review.
WP_Interactivity_API::merge_style_property(), which appliesdata-wp-style--*directives on the server, splits the existingstyleattribute on every;and:. A declaration whose value contains either character is cut. This rebuilds the function onWP_HTML_Style_Attribute_Processor. WordPress#12733 fixes the same bug (https://core.trac.wordpress.org/ticket/63899) with string splitting.style, directive setscolor: greenbackground-image:url(a.png)background-image:url(a.png);color:green;background-image:url(a.png); color: green;background-image:url(https://a.test/a.png)background-image:url(https;color:green;background-image:url(https://a.test/a.png); color: green;The function removes every declaration of the property in one
next_declaration( $name )loop withremove_declaration(), then callsappend_declaration(), as the processor's class docblock documents for setting a property. It keeps its contract: afalse,nullor empty value removes the property, a value is trimmed and trailing semicolons are removed, and the new declaration goes at the end.Behavior differences from trunk:
name:value;.name: value;, with a space before an earlier declaration.COLOR:redis replaced when the directive setscolor. Custom properties match exactly: setting--MyVarleaves--myvar. Directive suffixes come from lowercased attribute names, so only direct callers can pass an uppercase name.append_declaration()rejects a value that is not one valid declaration value: an unclosed block or function, a top-level semicolon, or a trailing!important. The property is then removed and nothing is added. Trunk appended any value as text. On the client,style.setProperty()also ignores such values.Changed expected strings
Every changed assertion differs only in format, except the two noted.
test_merge_style_property_sets_properties:''+color: greencolor:green;color: green;color:red;+color: greencolor:green;color: green;color:red;+background: bluecolor:red;background:blue;color:red; background: blue;color:red;margin:5px;+color: greenmargin:5px;color:green;margin:5px; color: green;color:red;margin:5px;+padding: 10pxcolor:red;margin:5px;padding:10px;color:red;margin:5px; padding: 10px;color : red; margin : 5px;+padding: " 10px "color:red;margin:5px;padding:10px;color : red; margin : 5px; padding: 10px;(whitespace kept; two spaces, see below)color:red;margin:5px;+margin: 15pxcolor:red;margin:15px;color:red; margin: 15px;color:red;margin:5px+padding: 10pxcolor:red;margin:5px;padding:10px;color:red;margin:5px; padding: 10px;color:red;margin:5px;+padding: "10px;"color:red;margin:5px;padding:10px;color:red;margin:5px; padding: 10px;color:red;;margin:5px;;+padding: 10pxcolor:red;margin:5px;padding:10px;color:red;;margin:5px;; padding: 10px;(empty declarations kept)border-style: dashed; border: 3px solid red;+border-style: insetborder:3px solid red;border-style:inset;border: 3px solid red; border-style: inset;test_wp_style_sets_style_attributecolor:green;color: green;test_wp_style_sets_multiple_style_propertiesbackground:green;color:green;background: green; color: green;test_wp_style_sets_multiple_style_properties_with_different_values(2 assertions)color:green;color: green;test_wp_style_sets_style_property_when_style_attribute_existspadding:10px;color:green;padding:10px; color: green;test_wp_style_overwrites_style_property_when_style_property_existscolor:green;color: green;test_wp_style_keeps_style_property_when_style_property_existscolor:green;color: green;test_wp_style_keeps_style_property_when_style_property_exists_and_is_not_the_only_onepadding:10px;color:green;padding:10px; color: green;test_wp_style_doesnt_change_style_attribute_with_empty_value:padding:10pxpadding:10px;padding:10px(unchanged input)test_wp_style_doesnt_change_style_attribute_without_value:padding: 10px;padding:10px;padding: 10px;(unchanged input)test_wp_style_works_with_multiple_directivescolor:green;color: green;test_wp_style_can_use_CSS_variables--text-color:green;--text-color: green;test_process_directives_process_the_directives_in_the_correct_orderdisplay:none;display: none;Comparison with WordPress#12733
Its tests are ported with expected strings in the format above, and pass. Its implementation fails 18 of the 28 tests in
Tests_WP_Interactivity_API_WP_Styleon this branch; 16 differ only in format. The other two show behavior differences:stylefont-family:"Foo;Bar",serif;color:redfont-family: serifBar",serif;color:red;font-family:serif;(CSS reads one string to the end)color:red; font-family: serif;COLOR:red;margin:5px;color: greenCOLOR:red;margin:5px;color:green;margin:5px; color: green;color:red;margin:5px;color: rgb(0 0 0margin:5px;color:rgb(0 0 0;margin:5px;color:red;margin:5px;color: green !importantmargin:5px;color:green !important;margin:5px;Cost
Measured per call on PHP 8.5 CLI, opcache off, one process per variant, best of 5 runs of 20,000 calls (opcache on gives the same within 10%):
'', addcolorTokenizing the 61-byte, 5-declaration style once costs about 10 µs.
remove_declaration()andappend_declaration()each parse the whole style again to verify the result, andappend_declaration()tokenizes the name and value several times.Known limitations
append_declaration()keeps the whitespace and adds a separator space, which gives two spaces (margin : 5px; padding: 10px;).!importantis rejected. The function could detect it and pass$importanttoappend_declaration().Depends on WordPress#14782.
Trac ticket: TBD
Use of AI Tools
AI assistance: Yes
Tools: Claude Code
Models: Claude Opus 5.5
Used for: implementation, tests and this description.
This pull request is for code review only. Keep other discussion in the Trac ticket. Do not merge. See GitHub Pull Requests for Code Review in the Core Handbook.