Skip to content

Interactivity API: Merge data-wp-style values with the CSS style attribute processor - #99

Closed
sirreal wants to merge 2 commits into
css-token-style-attr-processorfrom
css-api/interactivity-style-merge
Closed

sirreal wants to merge 2 commits into
css-token-style-attr-processorfrom
css-api/interactivity-style-merge

Conversation

@sirreal

@sirreal sirreal commented Nov 17, 2026

Copy link
Copy Markdown
Owner

Caution

This is a work in progress draft and is not ready for any serious review.

WP_Interactivity_API::merge_style_property(), which applies data-wp-style--* directives on the server, splits the existing style attribute on every ; and :. A declaration whose value contains either character is cut. This rebuilds the function on WP_HTML_Style_Attribute_Processor. WordPress#12733 fixes the same bug (https://core.trac.wordpress.org/ticket/63899) with string splitting.

Existing style, directive sets color: green Trunk This PR
background-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 with remove_declaration(), then calls append_declaration(), as the processor's class docblock documents for setting a property. It keeps its contract: a false, null or 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:

  • Other declarations keep their original text. Trunk rewrote each one as name:value;.
  • The appended declaration is written as name: value;, with a space before an earlier declaration.
  • Standard property names match ASCII-case-insensitively, so COLOR:red is replaced when the directive sets color. Custom properties match exactly: setting --MyVar leaves --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 Trunk expected This PR expected
test_merge_style_property_sets_properties: '' + color: green color:green; color: green;
same: color:red; + color: green color:green; color: green;
same: color:red; + background: blue color:red;background:blue; color:red; background: blue;
same: color:red;margin:5px; + color: green margin:5px;color:green; margin:5px; color: green;
same: color:red;margin:5px; + padding: 10px color:red;margin:5px;padding:10px; color:red;margin:5px; padding: 10px;
same: color : red; margin : 5px; + padding: " 10px " color:red;margin:5px;padding:10px; color : red; margin : 5px; padding: 10px; (whitespace kept; two spaces, see below)
same: color:red;margin:5px; + margin: 15px color:red;margin:15px; color:red; margin: 15px;
same: color:red;margin:5px + padding: 10px color:red;margin:5px;padding:10px; color:red;margin:5px; padding: 10px;
same: color:red;margin:5px; + padding: "10px;" color:red;margin:5px;padding:10px; color:red;margin:5px; padding: 10px;
same: color:red;;margin:5px;; + padding: 10px color:red;margin:5px;padding:10px; color:red;;margin:5px;; padding: 10px; (empty declarations kept)
same: border-style: dashed; border: 3px solid red; + border-style: inset border:3px solid red;border-style:inset; border: 3px solid red; border-style: inset;
test_wp_style_sets_style_attribute color:green; color: green;
test_wp_style_sets_multiple_style_properties background: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_exists padding:10px;color:green; padding:10px; color: green;
test_wp_style_overwrites_style_property_when_style_property_exists color:green; color: green;
test_wp_style_keeps_style_property_when_style_property_exists color:green; color: green;
test_wp_style_keeps_style_property_when_style_property_exists_and_is_not_the_only_one padding:10px;color:green; padding:10px; color: green;
test_wp_style_doesnt_change_style_attribute_with_empty_value: padding:10px padding: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_directives color: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_order display: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_Style on this branch; 16 differ only in format. The other two show behavior differences:

Existing style Directive WordPress#12733 This PR
font-family:"Foo;Bar",serif;color:red font-family: serif Bar",serif;color:red;font-family:serif; (CSS reads one string to the end) color:red; font-family: serif;
COLOR:red;margin:5px; color: green COLOR:red;margin:5px;color:green; margin:5px; color: green;
color:red;margin:5px; color: rgb(0 0 0 margin:5px;color:rgb(0 0 0; margin:5px;
color:red;margin:5px; color: green !important margin: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%):

Input Trunk µs This PR µs
'', add color 0.10 15
1 declaration, replace it 0.16 20
1 declaration, remove it 0.13 4.8
3 declarations, add a fourth 0.40 46
5 declarations, replace the third 0.55 86

Tokenizing the 61-byte, 5-declaration style once costs about 10 µs. remove_declaration() and append_declaration() each parse the whole style again to verify the result, and append_declaration() tokenizes the name and value several times.

Known limitations

  • After a style that ends in whitespace, append_declaration() keeps the whitespace and adds a separator space, which gives two spaces (margin : 5px; padding: 10px;).
  • A value ending in !important is rejected. The function could detect it and pass $important to append_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.

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.
@sirreal

sirreal commented Nov 17, 2026

Copy link
Copy Markdown
Owner Author

Superseded by #111, which rebuilds the whole data-wp-style directive on one style processor per element.

@sirreal sirreal closed this Nov 17, 2026
@sirreal
sirreal deleted the css-api/interactivity-style-merge branch November 17, 2026 13:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant