Repository navigation
Conversation
data_wp_style_processor() read, split and rewrote the style attribute
once per data-wp-style entry with merge_style_property(), which split
the style on every `;` and `:`. A declaration whose value contained
either character was cut:
style="background-image:url(https://a.test/a.png)"
trunk: background-image:url(https;color:green;
The directive now collects the evaluated value of every entry, builds
one WP_HTML_Style_Attribute_Processor for the element, removes every
declaration of each named property in one next_declaration() loop,
appends a declaration for each set value, and writes the attribute
once. merge_style_property() is removed. Removing before appending
makes a value replace an existing `!important` declaration, as the
client's style.setProperty() does.
Values follow the client where CSS can write them: the string '0'
sets the property (it is truthy on the client), `true` is written as
`true`, and a non-scalar value removes the property instead of
raising a TypeError in trim().
Other declarations keep their text. The appended declaration is
written as `name: value;`, so expected strings in tests change format.
A value that is not one valid declaration value (a top-level `;`, an
unclosed function, a trailing `!important`) removes the property and
appends nothing.
Measured per element, PHP 8.5 CLI, one process per variant, best of
5 rounds, on a 5-declaration style: see the pull request.
See #63899.
Keeps the cases for an unclosed function, a trailing `!important` and a closing brace. The behavior for other invalid values is documented on data_wp_style_processor().
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
The client applies each entry with style.setProperty(), which ignores a value the browser cannot parse as the property's value and leaves the existing declaration in place. The server removed the property's declarations and then failed to append, so the two disagreed: the server output lost the declaration that the client kept. An entry is now checked by appending its declaration to an empty style before anything is removed. A value that fails, such as one with a trailing `!important` or an unbalanced closing bracket, is skipped, and the property keeps its declarations. A trailing `;` is no longer removed from the value, since the client ignores such a value too.
…ity-style-directive
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.
data_wp_style_processor()reads and rewrites thestyleattribute once perdata-wp-style--*entry withmerge_style_property(), which splits the style on every;and:. A declaration whose value contains either character is cut (https://core.trac.wordpress.org/ticket/63899):style, directive setscolortogreenbackground-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;This rewrites the directive on
WP_HTML_Style_Attribute_Processorand removesmerge_style_property(). It supersedes #99, which rebuilt onlymerge_style_property()and built one processor per entry. WordPress#12733 fixes the same bug with string splitting.Design
For each element the directive:
styleattribute and removes every declaration of each named property in onenext_declaration()loop that comparesget_property_name()with the map. The cursor only moves forward, so anext_declaration( $name )loop per property finds nothing after the first loop; the class docblock documents this one-loop form for setting several properties.append_declaration().Removing before appending is required for two reasons: a falsy value must remove the property, and an existing
!importantdeclaration would otherwise win over an appended normal one.style="color: red !important"withcolorset tobluegivescolor: blue;, as on trunk and on the client.Client semantics
The client directive (
packages/interactivity/src/directives/style.tsin Gutenberg) removes the property for a falsy result (!result) and otherwise callsstyle.setProperty( name, result ), which converts the result to a string. Checked in Chromium:stylesetProperty()valuecolor: red !importantbluecolor: blue;color: redgreen !importantcolor: red(value ignored)color: redgreen;color: red(value ignored)color: redrgb(0 0 0color: rgb(0, 0, 0);trueon--flag--flag: true;opacity: 1'0'opacity: 0;The server now follows the client where it can:
The string
'0'sets the property. It is truthy on the client; trunk removed the property.trueis written astrue. Trunk wrote1.0,0.0,'',null,falseand any non-scalar value remove the property. Trunk raised aTypeErrorintrim()for a non-empty array.A string that is not one valid declaration value, such as one with a trailing
!important, a trailing;, a semicolon outside a string or block, or an unbalanced closing bracket, is ignored, and the property keeps its declarations. Each entry is checked by appending its declaration to an empty style before anything is removed, which costs about 14 µs per entry and adds no processor method.It cannot follow the client in two cases:
color: 5pxis invalid.Output format changes
Other declarations keep their text. The appended declaration is written as
name: value;from its tokens.stylecolor=greencolor:green;color: green;padding:10px;color=greenpadding:10px;color:green;padding:10px; color: green;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=10pxcolor:red;margin:5px;padding:10px;color:red;;margin:5px;; padding: 10px;padding:10pxcolor=falsepadding:10px;padding:10px(unchanged)font-family="Foo;Bar", seriffont-family:"Foo;Bar", serif;font-family: "Foo\3b Bar", serif;The serializer escapes a
;in a string and a;or,in an unquotedurl(). CSS reads the escaped and unescaped forms as the same value.Behavior changes
stylefont-family:"Foo;Bar",serif;color:redfont-family=serifBar",serif:;color:red;font-family:serif;and a PHP warningcolor:red; font-family: serif;COLOR:red;margin:5pxcolor=greenCOLOR:red;margin:5px;color:green;margin:5px; color: green;opacity:1opacity='0'opacity: 0;--flag=true--flag:1;--flag: true;color:red;margin:5pxcolor=array( 'green' )TypeErrormargin:5pxcolor:red;margin:5pxcolor=green !importantmargin:5px;color:green !important;color:red;margin:5px(unchanged)color:red;margin:5pxcolor=rgb(0 0 0margin:5px;color:rgb(0 0 0;color:red;margin:5px(unchanged)color:red;margin:5pxpadding=10px;color:red;margin:5px;padding:10px;color:red;margin:5px(unchanged)Tests
merge_style_property()tests are ported to the directive level throughprocess_directives(), as are the tests from #99 and WordPress#12733, with expected strings in the format above. New cases cover several entries on one element, an existing!importantdeclaration, falsy and scalar values, values with;inside a string orurl(), invalid values, and an unchanged attribute.--group interactivity-api,html-api,css-apipasses; PHPCS and PHPStan report no errors.Cost
µs per element for
data_wp_style_processor(), excluding tag processor setup. PHP 8.5 CLI, opcache off, one process per variant, best of 5 rounds of 5,000 elements. The style has 5 declarations in 96 bytes; entries replacecolor, thenpadding, then addheight.styleattributeFor this style, parsing costs 23 µs (13 µs of it tokenizing), each
remove_declaration()18 µs and eachappend_declaration()42 µs, because each mutation parses the whole style again to verify the result andappend_declaration()also tokenizes the name and value several times. One processor per element saves one parse per entry after the first. Joiningserialize_declaration()of the kept declarations with each value appended to an empty processor measures 44, 59 and 75 µs, but it rewrites every kept declaration and the processor does not verify the joined result. These numbers were measured before the value check above; it adds about 14 µs per entry with a value.Known limitations
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.