From f023af77b856f4c61c0cb26932135089f4292a4a Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Tue, 17 Nov 2026 11:11:37 +0400 Subject: [PATCH 1/2] Interactivity API: Merge data-wp-style values with the CSS parser 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. --- .../class-wp-interactivity-api.php | 37 +++++++------- .../wpInteractivityAPI-wp-style.php | 50 +++++++++---------- .../interactivity-api/wpInteractivityAPI.php | 2 +- 3 files changed, 44 insertions(+), 45 deletions(-) diff --git a/src/wp-includes/interactivity-api/class-wp-interactivity-api.php b/src/wp-includes/interactivity-api/class-wp-interactivity-api.php index b4f9ed1afcb98..d27c52930441d 100644 --- a/src/wp-includes/interactivity-api/class-wp-interactivity-api.php +++ b/src/wp-includes/interactivity-api/class-wp-interactivity-api.php @@ -1517,15 +1517,22 @@ private function data_wp_style_processor( WP_Interactivity_API_Directives_Proces * Merges an individual style property in the `style` attribute of an HTML * element, updating or removing the property when necessary. * - * If a property is modified, the old one is removed and the new one is added - * at the end of the list. + * Every declaration of the property is removed, and the new declaration is + * appended at the end of the list. Other declarations keep their text. + * Property names match as in {@see WP_HTML_Style_Attribute_Processor::next_declaration()}: + * ASCII-case-insensitively, except custom properties, which match exactly. + * + * A value that is not one valid CSS declaration value, such as one with a + * semicolon outside a string or block, or one ending in `!important`, removes + * the property and adds nothing. * * @since 6.5.0 + * @since 7.3.0 Parses the style attribute value as CSS. * * Example: * - * merge_style_property( 'color:green;', 'color', 'red' ) => 'color:red;' - * merge_style_property( 'background:green;', 'color', 'red' ) => 'background:green;color:red;' + * merge_style_property( 'color:green;', 'color', 'red' ) => 'color: red;' + * merge_style_property( 'background:green;', 'color', 'red' ) => 'background:green; color: red;' * merge_style_property( 'color:green;', 'color', null ) => '' * * @param string $style_attribute_value The current style attribute value. @@ -1535,26 +1542,18 @@ private function data_wp_style_processor( WP_Interactivity_API_Directives_Proces * @return string The new style attribute value after the specified property has been added, updated or removed. */ private function merge_style_property( string $style_attribute_value, string $style_property_name, $style_property_value ): string { - $style_assignments = explode( ';', $style_attribute_value ); - $result = array(); $style_property_value = ! empty( $style_property_value ) ? rtrim( trim( $style_property_value ), ';' ) : null; - $new_style_property = $style_property_value ? $style_property_name . ':' . $style_property_value . ';' : ''; + $styles = new WP_HTML_Style_Attribute_Processor( $style_attribute_value ); - // Generates an array with all the properties but the modified one. - foreach ( $style_assignments as $style_assignment ) { - if ( empty( trim( $style_assignment ) ) ) { - continue; - } - list( $name, $value ) = explode( ':', $style_assignment ); - if ( trim( $name ) !== $style_property_name ) { - $result[] = trim( $name ) . ':' . trim( $value ) . ';'; - } + while ( $styles->next_declaration( $style_property_name ) ) { + $styles->remove_declaration(); } - // Adds the new/modified property at the end of the list. - $result[] = $new_style_property; + if ( $style_property_value ) { + $styles->append_declaration( $style_property_name, $style_property_value ); + } - return implode( '', $result ); + return $styles->get_updated_style(); } /** diff --git a/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-style.php b/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-style.php index 1a5884f34b8e6..88b957f23aca9 100644 --- a/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-style.php +++ b/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-style.php @@ -63,47 +63,47 @@ private function merge_style_property( $style_attribute_value, $style_property_n public function test_merge_style_property_sets_properties() { // Adds property on empty style attribute. $result = $this->merge_style_property( '', 'color', 'green' ); - $this->assertSame( 'color:green;', $result ); + $this->assertSame( 'color: green;', $result ); // Changes style property when there is an existing property. $result = $this->merge_style_property( 'color:red;', 'color', 'green' ); - $this->assertSame( 'color:green;', $result ); + $this->assertSame( 'color: green;', $result ); // Adds a new property when the existing one does not match. $result = $this->merge_style_property( 'color:red;', 'background', 'blue' ); - $this->assertSame( 'color:red;background:blue;', $result ); + $this->assertSame( 'color:red; background: blue;', $result ); // Handles multiple existing properties. $result = $this->merge_style_property( 'color:red;margin:5px;', 'color', 'green' ); - $this->assertSame( 'margin:5px;color:green;', $result ); + $this->assertSame( 'margin:5px; color: green;', $result ); // Adds a new property when multiple existing properties do not match. $result = $this->merge_style_property( 'color:red;margin:5px;', 'padding', '10px' ); - $this->assertSame( 'color:red;margin:5px;padding:10px;', $result ); + $this->assertSame( 'color:red;margin:5px; padding: 10px;', $result ); - // Removes whitespaces in all properties. + // Keeps whitespace in existing declarations and trims the new value. $result = $this->merge_style_property( ' color : red; margin : 5px; ', 'padding', ' 10px ' ); - $this->assertSame( 'color:red;margin:5px;padding:10px;', $result ); + $this->assertSame( ' color : red; margin : 5px; padding: 10px;', $result ); // Updates a property when it's not the first one in the value. $result = $this->merge_style_property( 'color:red;margin:5px;', 'margin', '15px' ); - $this->assertSame( 'color:red;margin:15px;', $result ); + $this->assertSame( 'color:red; margin: 15px;', $result ); // Adds missing trailing semicolon. $result = $this->merge_style_property( 'color:red;margin:5px', 'padding', '10px' ); - $this->assertSame( 'color:red;margin:5px;padding:10px;', $result ); + $this->assertSame( 'color:red;margin:5px; padding: 10px;', $result ); // Doesn't add double semicolons. $result = $this->merge_style_property( 'color:red;margin:5px;', 'padding', '10px;' ); - $this->assertSame( 'color:red;margin:5px;padding:10px;', $result ); + $this->assertSame( 'color:red;margin:5px; padding: 10px;', $result ); - // Handles empty properties in the input. + // Keeps empty declarations in the input. $result = $this->merge_style_property( 'color:red;;margin:5px;;', 'padding', '10px' ); - $this->assertSame( 'color:red;margin:5px;padding:10px;', $result ); + $this->assertSame( 'color:red;;margin:5px;; padding: 10px;', $result ); // Moves the modified property to the end. $result = $this->merge_style_property( 'border-style: dashed; border: 3px solid red;', 'border-style', 'inset' ); - $this->assertSame( 'border:3px solid red;border-style:inset;', $result ); + $this->assertSame( 'border: 3px solid red; border-style: inset;', $result ); } /** @@ -172,7 +172,7 @@ private function process_directives( $html ) { public function test_wp_style_sets_style_attribute() { $html = '
Text
'; list($p) = $this->process_directives( $html ); - $this->assertSame( 'color:green;', $p->get_attribute( 'style' ) ); + $this->assertSame( 'color: green;', $p->get_attribute( 'style' ) ); } /** @@ -190,7 +190,7 @@ public function test_wp_style_sets_multiple_style_properties() { data-wp-style--background="myPlugin::state.green" >Text'; list($p) = $this->process_directives( $html ); - $this->assertSame( 'background:green;color:green;', $p->get_attribute( 'style' ) ); + $this->assertSame( 'background: green; color: green;', $p->get_attribute( 'style' ) ); } /** @@ -208,7 +208,7 @@ public function test_wp_style_sets_multiple_style_properties_with_different_valu data-wp-style--background="myPlugin::state.false" >Text'; list($p) = $this->process_directives( $html ); - $this->assertSame( 'color:green;', $p->get_attribute( 'style' ) ); + $this->assertSame( 'color: green;', $p->get_attribute( 'style' ) ); $html = '
Text
'; list($p) = $this->process_directives( $html ); - $this->assertSame( 'color:green;', $p->get_attribute( 'style' ) ); + $this->assertSame( 'color: green;', $p->get_attribute( 'style' ) ); } /** @@ -231,7 +231,7 @@ public function test_wp_style_sets_multiple_style_properties_with_different_valu public function test_wp_style_sets_style_property_when_style_attribute_exists() { $html = '
Text
'; list($p) = $this->process_directives( $html ); - $this->assertSame( 'padding:10px;color:green;', $p->get_attribute( 'style' ) ); + $this->assertSame( 'padding:10px; color: green;', $p->get_attribute( 'style' ) ); } /** @@ -245,7 +245,7 @@ public function test_wp_style_sets_style_property_when_style_attribute_exists() public function test_wp_style_overwrites_style_property_when_style_property_exists() { $html = '
Text
'; list($p) = $this->process_directives( $html ); - $this->assertSame( 'color:green;', $p->get_attribute( 'style' ) ); + $this->assertSame( 'color: green;', $p->get_attribute( 'style' ) ); } /** @@ -287,7 +287,7 @@ public function test_wp_style_doesnt_add_style_property_on_false() { public function test_wp_style_keeps_style_property_when_style_property_exists() { $html = '
Text
'; list($p) = $this->process_directives( $html ); - $this->assertSame( 'color:green;', $p->get_attribute( 'style' ) ); + $this->assertSame( 'color: green;', $p->get_attribute( 'style' ) ); } /** @@ -301,7 +301,7 @@ public function test_wp_style_keeps_style_property_when_style_property_exists() public function test_wp_style_keeps_style_property_when_style_property_exists_and_is_not_the_only_one() { $html = '
Text
'; list($p) = $this->process_directives( $html ); - $this->assertSame( 'padding:10px;color:green;', $p->get_attribute( 'style' ) ); + $this->assertSame( 'padding:10px; color: green;', $p->get_attribute( 'style' ) ); } /** @@ -372,7 +372,7 @@ public function test_wp_style_doesnt_change_style_attribute_with_empty_directive public function test_wp_style_doesnt_change_style_attribute_with_empty_value() { $html = '
Text
'; list($p) = $this->process_directives( $html ); - $this->assertSame( 'padding:10px;', $p->get_attribute( 'style' ) ); + $this->assertSame( 'padding:10px', $p->get_attribute( 'style' ) ); } /** @@ -387,7 +387,7 @@ public function test_wp_style_doesnt_change_style_attribute_with_empty_value() { public function test_wp_style_doesnt_change_style_attribute_without_value() { $html = '
Text
'; list($p) = $this->process_directives( $html ); - $this->assertSame( 'padding:10px;', $p->get_attribute( 'style' ) ); + $this->assertSame( 'padding: 10px;', $p->get_attribute( 'style' ) ); } /** @@ -401,7 +401,7 @@ public function test_wp_style_doesnt_change_style_attribute_without_value() { public function test_wp_style_works_with_multiple_directives() { $html = '
Text
'; list($p) = $this->process_directives( $html ); - $this->assertSame( 'color:green;', $p->get_attribute( 'style' ) ); + $this->assertSame( 'color: green;', $p->get_attribute( 'style' ) ); } /** @@ -459,7 +459,7 @@ public function test_wp_style_doesnt_add_style_property_on_falsy_values() { public function test_wp_style_can_use_CSS_variables() { $html = '
Text
'; list($p) = $this->process_directives( $html ); - $this->assertSame( '--text-color:green;', $p->get_attribute( 'style' ) ); + $this->assertSame( '--text-color: green;', $p->get_attribute( 'style' ) ); } /** diff --git a/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php b/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php index 561c1c4eca93e..32e9afba95240 100644 --- a/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php +++ b/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php @@ -1356,7 +1356,7 @@ public function test_process_directives_process_the_directives_in_the_correct_or $p->next_tag(); $this->assertSame( 'some-id', $p->get_attribute( 'id' ) ); $this->assertSame( 'some-class', $p->get_attribute( 'class' ) ); - $this->assertSame( 'display:none;', $p->get_attribute( 'style' ) ); + $this->assertSame( 'display: none;', $p->get_attribute( 'style' ) ); $this->assertStringContainsString( 'Updated', $p->get_updated_html() ); $this->assertStringNotContainsString( 'Text', $p->get_updated_html() ); } From 9815a11ee0c1c5113409f62304c6e9ec4f987f8e Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Tue, 17 Nov 2026 11:12:52 +0400 Subject: [PATCH 2/2] Interactivity API: Test data-wp-style values with colons and semicolons Port the tests from https://github.com/WordPress/wordpress-develop/pull/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. --- .../wpInteractivityAPI-wp-style.php | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) diff --git a/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-style.php b/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-style.php index 88b957f23aca9..a9dc89a5db75a 100644 --- a/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-style.php +++ b/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-style.php @@ -106,6 +106,104 @@ public function test_merge_style_property_sets_properties() { $this->assertSame( 'border: 3px solid red; border-style: inset;', $result ); } + /** + * Tests that `merge_style_property` preserves values containing a colon. + * + * Only the first colon in a declaration separates the property name from its + * value, so a value may contain more of them. + * + * @ticket 63899 + * + * @covers ::merge_style_property + */ + public function test_merge_style_property_preserves_values_containing_colons() { + // Keeps an untouched property whose value contains a URL. + $result = $this->merge_style_property( 'background-image:url(https://example.com/a.png)', 'color', 'green' ); + $this->assertSame( 'background-image:url(https://example.com/a.png); color: green;', $result ); + + // Replaces a property whose previous value contained a URL. + $result = $this->merge_style_property( 'background-image:url(https://example.com/a.png)', 'background-image', 'none' ); + $this->assertSame( 'background-image: none;', $result ); + + // Keeps every colon after the first, not only the second. + $result = $this->merge_style_property( '--fallback:url(https://a.test/x.png), url(https://b.test/y.png);', 'color', 'green' ); + $this->assertSame( '--fallback:url(https://a.test/x.png), url(https://b.test/y.png); color: green;', $result ); + } + + /** + * Tests that `merge_style_property` preserves values containing a semicolon. + * + * A semicolon inside a quoted string or a `url()` does not end a declaration. + * + * @ticket 63899 + * + * @covers ::merge_style_property + */ + public function test_merge_style_property_preserves_values_containing_semicolons() { + // Keeps a quoted value containing a semicolon. + $result = $this->merge_style_property( 'font-family:"Foo;Bar",serif', 'color', 'green' ); + $this->assertSame( 'font-family:"Foo;Bar",serif; color: green;', $result ); + + // Keeps a data URI, which contains both a colon and a semicolon. + $result = $this->merge_style_property( 'background-image:url(data:image/gif;base64,R0lGODlhAQ)', 'color', 'green' ); + $this->assertSame( 'background-image:url(data:image/gif;base64,R0lGODlhAQ); color: green;', $result ); + + // Replaces a property whose value contains a quoted semicolon. + $result = $this->merge_style_property( 'font-family:"Foo;Bar",serif;color:red', 'font-family', 'serif' ); + $this->assertSame( 'color:red; font-family: serif;', $result ); + } + + /** + * Tests that `merge_style_property` keeps segments that contain no colon. + * + * @ticket 63899 + * + * @covers ::merge_style_property + */ + public function test_merge_style_property_handles_segments_without_a_colon() { + $result = $this->merge_style_property( 'translate:none;translate3d(0px, 0px, 0px);', 'color', 'green' ); + $this->assertSame( 'translate:none;translate3d(0px, 0px, 0px); color: green;', $result ); + } + + /** + * Tests that `merge_style_property` matches property names as CSS does. + * + * @ticket 63899 + * + * @covers ::merge_style_property + */ + public function test_merge_style_property_matches_property_names() { + // Replaces a standard property written in any case. + $result = $this->merge_style_property( 'COLOR:red;margin:5px;', 'color', 'green' ); + $this->assertSame( 'margin:5px; color: green;', $result ); + + // Replaces only the custom property with the same case. + $result = $this->merge_style_property( '--MyVar:1;--myvar:2;', '--MyVar', '3' ); + $this->assertSame( '--myvar:2; --MyVar: 3;', $result ); + + // Removes every declaration of the property. + $result = $this->merge_style_property( 'color:red;margin:5px;color:blue;', 'color', 'green' ); + $this->assertSame( 'margin:5px; color: green;', $result ); + } + + /** + * Tests that `merge_style_property` removes the property and adds nothing + * when the new value is not a valid declaration value. + * + * @ticket 63899 + * + * @covers ::merge_style_property + */ + public function test_merge_style_property_rejects_invalid_values() { + // A value with an unclosed function. + $result = $this->merge_style_property( 'color:red;margin:5px;', 'color', 'rgb(0 0 0' ); + $this->assertSame( 'margin:5px;', $result ); + + // A value with an !important priority. + $result = $this->merge_style_property( 'color:red;margin:5px;', 'color', 'green !important' ); + $this->assertSame( 'margin:5px;', $result ); + } + /** * Tests that `merge_style_property` works correctly with falsy values, * removing or ignoring them as appropriate. @@ -234,6 +332,20 @@ public function test_wp_style_sets_style_property_when_style_attribute_exists() $this->assertSame( 'padding:10px; color: green;', $p->get_attribute( 'style' ) ); } + /** + * Tests that the `data-wp-style` directive leaves an existing style attribute + * intact when one of its values contains a colon. + * + * @ticket 63899 + * + * @covers ::process_directives + */ + public function test_wp_style_preserves_existing_style_attribute_containing_a_url() { + $html = '
Text
'; + list($p) = $this->process_directives( $html ); + $this->assertSame( 'background-image:url(https://example.com/a.png); color: green;', $p->get_attribute( 'style' ) ); + } + /** * Tests that the `data-wp-style` directive overwrites an existing style * property with a new value.