From a54fccb5fbee510bb1eb996a4287326df91c1140 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Tue, 17 Nov 2026 16:55:05 +0400 Subject: [PATCH 1/3] Interactivity API: Apply data-wp-style with one style processor 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. --- .../class-wp-interactivity-api.php | 121 +++--- .../wpInteractivityAPI-wp-style.php | 361 +++++++++++++----- .../interactivity-api/wpInteractivityAPI.php | 2 +- 3 files changed, 329 insertions(+), 155 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..3c79c6bcbae36 100644 --- a/src/wp-includes/interactivity-api/class-wp-interactivity-api.php +++ b/src/wp-includes/interactivity-api/class-wp-interactivity-api.php @@ -1474,87 +1474,78 @@ private function data_wp_class_processor( WP_Interactivity_API_Directives_Proces * It updates the style attribute value of the current HTML element based on * the evaluation of its associated references. * + * Each `data-wp-style--` entry removes every declaration of the + * property, then appends a declaration with the entry's value when the + * value is set. Other declarations keep their text. + * + * The value is converted to a string: + * + * - A string is used without surrounding whitespace and trailing semicolons. + * - `true` is `true`, and a number other than `0` is its string. + * - An empty string, `0` and any other value remove the property. + * + * 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 appends nothing. + * * @since 6.5.0 + * @since 7.3.0 Parses the style attribute value as CSS. * * @param WP_Interactivity_API_Directives_Processor $p The directives processor instance. * @param string $mode Whether the processing is entering or exiting the tag. */ private function data_wp_style_processor( WP_Interactivity_API_Directives_Processor $p, string $mode ) { - if ( 'enter' === $mode ) { - $entries = $this->get_directive_entries( $p, 'style' ); - foreach ( $entries as $entry ) { - $style_property = $entry['suffix']; - if ( empty( $style_property ) || null !== $entry['unique_id'] ) { - continue; - } + if ( 'enter' !== $mode ) { + return; + } - $style_property_value = $this->evaluate( $entry ); - $style_attribute_value = $p->get_attribute( 'style' ); - $style_attribute_value = ( $style_attribute_value && ! is_bool( $style_attribute_value ) ) ? $style_attribute_value : ''; + // Maps each property name to its value, or to '' to remove the property. + $values = array(); + foreach ( $this->get_directive_entries( $p, 'style' ) as $entry ) { + if ( empty( $entry['suffix'] ) || null !== $entry['unique_id'] ) { + continue; + } - /* - * Checks first if the style property is not falsy and the style - * attribute value is not empty because if it is, it doesn't need to - * update the attribute value. - */ - if ( $style_property_value || $style_attribute_value ) { - $style_attribute_value = $this->merge_style_property( $style_attribute_value, $style_property, $style_property_value ); - /* - * If the style attribute value is not empty, it sets it. Otherwise, - * it removes it. - */ - if ( ! empty( $style_attribute_value ) ) { - $p->set_attribute( 'style', $style_attribute_value ); - } else { - $p->remove_attribute( 'style' ); - } - } + $value = $this->evaluate( $entry ); + if ( true === $value ) { + $value = 'true'; + } elseif ( is_int( $value ) || is_float( $value ) ) { + $value = $value ? (string) $value : ''; } + + $values[ $entry['suffix'] ] = is_string( $value ) ? rtrim( trim( $value ), ';' ) : ''; } - } - /** - * 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. - * - * @since 6.5.0 - * - * 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', null ) => '' - * - * @param string $style_attribute_value The current style attribute value. - * @param string $style_property_name The style property name to set. - * @param string|false|null $style_property_value The value to set for the style property. With false, null or an - * empty string, it removes the style property. - * @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 . ';' : ''; - - // Generates an array with all the properties but the modified one. - foreach ( $style_assignments as $style_assignment ) { - if ( empty( trim( $style_assignment ) ) ) { - continue; + if ( empty( $values ) ) { + return; + } + + $style = $p->get_attribute( 'style' ); + $styles = new WP_HTML_Style_Attribute_Processor( $style ); + + while ( $styles->next_declaration() ) { + if ( isset( $values[ $styles->get_property_name() ] ) ) { + $styles->remove_declaration(); } - list( $name, $value ) = explode( ':', $style_assignment ); - if ( trim( $name ) !== $style_property_name ) { - $result[] = trim( $name ) . ':' . trim( $value ) . ';'; + } + + foreach ( $values as $name => $value ) { + if ( '' !== $value ) { + $styles->append_declaration( (string) $name, $value ); } } - // Adds the new/modified property at the end of the list. - $result[] = $new_style_property; + $original_style = is_string( $style ) ? $style : ''; + $updated_style = $styles->get_updated_style(); + if ( $original_style === $updated_style ) { + return; + } - return implode( '', $result ); + if ( '' === $updated_style ) { + $p->remove_attribute( 'style' ); + } else { + $p->set_attribute( 'style', $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..eed1f870d43a7 100644 --- a/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-style.php +++ b/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-style.php @@ -36,129 +36,312 @@ public function set_up() { } /** - * Invokes the private `merge_style_property` method of WP_Interactivity_API - * class. - * - * @param string $style_attribute_value The current style attribute value. - * @param string $style_property_name The style property name to set. - * @param string|false|null $style_property_value The value to set for the style property. With false, null or an - * empty string, it removes the style property. - * @return string The new style attribute value after the specified property has been added, updated or removed. - */ - private function merge_style_property( $style_attribute_value, $style_property_name, $style_property_value ) { - $evaluate = new ReflectionMethod( $this->interactivity, 'merge_style_property' ); - if ( PHP_VERSION_ID < 80100 ) { - $evaluate->setAccessible( true ); + * Invokes the `process_directives` method of WP_Interactivity_API class. + * + * @param string $html The HTML that needs to be processed. + * @return array An array containing an instance of the WP_HTML_Tag_Processor and the processed HTML. + */ + private function process_directives( $html ) { + $new_html = $this->interactivity->process_directives( $html ); + $p = new WP_HTML_Tag_Processor( $new_html ); + $p->next_tag(); + return array( $p, $new_html ); + } + + /** + * Processes a `div` element with one `data-wp-style` entry per property. + * + * @param string|null $style Style attribute value, or null for no style attribute. + * @param array $properties Values to evaluate, keyed by property name. + * @return string|true|null Style attribute value after processing. + */ + private function style_after_directives( ?string $style, array $properties ) { + $interactivity = new WP_Interactivity_API(); + $state = array(); + $html = 'invokeArgs( $this->interactivity, array( $style_attribute_value, $style_property_name, $style_property_value ) ); + foreach ( array_keys( $properties ) as $i => $name ) { + $state[ "p{$i}" ] = $properties[ $name ]; + $html .= " data-wp-style--{$name}=\"styleTest::state.p{$i}\""; + } + $html .= '>Text'; + + $interactivity->state( 'styleTest', $state ); + $p = new WP_HTML_Tag_Processor( $interactivity->process_directives( $html ) ); + $p->next_tag(); + return $p->get_attribute( 'style' ); } /** - * Tests that `merge_style_property` correctly sets style properties. + * Tests that the `data-wp-style` directive sets style properties. * * @ticket 60356 * - * @covers ::merge_style_property + * @covers ::process_directives */ - public function test_merge_style_property_sets_properties() { + public function test_wp_style_sets_properties() { // Adds property on empty style attribute. - $result = $this->merge_style_property( '', 'color', 'green' ); - $this->assertSame( 'color:green;', $result ); + $this->assertSame( 'color: green;', $this->style_after_directives( '', array( 'color' => 'green' ) ) ); // 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;', $this->style_after_directives( 'color:red;', array( 'color' => 'green' ) ) ); // 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;', $this->style_after_directives( 'color:red;', array( 'background' => 'blue' ) ) ); // 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;', $this->style_after_directives( 'color:red;margin:5px;', array( 'color' => 'green' ) ) ); // 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;', $this->style_after_directives( 'color:red;margin:5px;', array( 'padding' => '10px' ) ) ); - // Removes whitespaces in all properties. - $result = $this->merge_style_property( ' color : red; margin : 5px; ', 'padding', ' 10px ' ); - $this->assertSame( 'color:red;margin:5px;padding:10px;', $result ); + // Keeps whitespace in existing declarations and trims the new value. + $this->assertSame( ' color : red; margin : 5px; padding: 10px;', $this->style_after_directives( ' color : red; margin : 5px; ', array( 'padding' => ' 10px ' ) ) ); // 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;', $this->style_after_directives( 'color:red;margin:5px;', array( 'margin' => '15px' ) ) ); // 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;', $this->style_after_directives( 'color:red;margin:5px', array( 'padding' => '10px' ) ) ); // 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;', $this->style_after_directives( 'color:red;margin:5px;', array( 'padding' => '10px;' ) ) ); - // Handles empty properties in the input. - $result = $this->merge_style_property( 'color:red;;margin:5px;;', 'padding', '10px' ); - $this->assertSame( 'color:red;margin:5px;padding:10px;', $result ); + // Keeps empty declarations in the input. + $this->assertSame( 'color:red;;margin:5px;; padding: 10px;', $this->style_after_directives( 'color:red;;margin:5px;;', array( 'padding' => '10px' ) ) ); // 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;', $this->style_after_directives( 'border-style: dashed; border: 3px solid red;', array( 'border-style' => 'inset' ) ) ); } /** - * Tests that `merge_style_property` works correctly with falsy values, - * removing or ignoring them as appropriate. + * Tests that the `data-wp-style` directive removes a property, or adds + * nothing, for falsy values. * * @ticket 60356 * - * @covers ::merge_style_property + * @covers ::process_directives */ - public function test_merge_style_property_with_falsy_values() { - // Removes a property with an empty string. - $result = $this->merge_style_property( 'color:red;margin:5px;', 'color', '' ); - $this->assertSame( 'margin:5px;', $result ); + public function test_wp_style_with_falsy_values() { + foreach ( array( '', null, false, 0, 0.0, array() ) as $value ) { + $this->assertSame( 'margin:5px;', $this->style_after_directives( 'color:red;margin:5px;', array( 'color' => $value ) ), 'Removes the property with ' . var_export( $value, true ) ); + $this->assertSame( 'color:red;', $this->style_after_directives( 'color:red;', array( 'padding' => $value ) ), 'Adds nothing with ' . var_export( $value, true ) ); + } + } - // Removes a property with null. - $result = $this->merge_style_property( 'color:red;margin:5px;', 'color', null ); - $this->assertSame( 'margin:5px;', $result ); + /** + * Tests that the `data-wp-style` directive writes non-falsy scalar values + * as strings. + * + * The string `'0'` sets the property, `true` is written as `true`, and a + * number is written as its string. + * + * @covers ::process_directives + */ + public function test_wp_style_writes_scalar_values_as_strings() { + $this->assertSame( 'opacity: 0;', $this->style_after_directives( 'opacity:1', array( 'opacity' => '0' ) ) ); + $this->assertSame( '--flag: true;', $this->style_after_directives( null, array( '--flag' => true ) ) ); + $this->assertSame( 'z-index: 5;', $this->style_after_directives( null, array( 'z-index' => 5 ) ) ); + $this->assertSame( 'opacity: 0.5;', $this->style_after_directives( null, array( 'opacity' => 0.5 ) ) ); + } + + /** + * Tests that the `data-wp-style` directive removes the property for a value + * that is not a string, number or boolean. + * + * @covers ::process_directives + */ + public function test_wp_style_removes_property_for_non_scalar_values() { + $this->assertSame( 'margin:5px;', $this->style_after_directives( 'color:red;margin:5px;', array( 'color' => array( 'green' ) ) ) ); + $this->assertSame( 'margin:5px;', $this->style_after_directives( 'color:red;margin:5px;', array( 'color' => (object) array( 'a' => 'green' ) ) ) ); + } - // Removes a property with false. - $result = $this->merge_style_property( 'color:red;margin:5px;', 'color', false ); - $this->assertSame( 'margin:5px;', $result ); + /** + * Tests that the `data-wp-style` directive 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 ::process_directives + */ + public function test_wp_style_preserves_values_containing_colons() { + // Keeps an untouched property whose value contains a URL. + $this->assertSame( + 'background-image:url(https://example.com/a.png); color: green;', + $this->style_after_directives( 'background-image:url(https://example.com/a.png)', array( 'color' => 'green' ) ) + ); - // Removes a property with 0. - $result = $this->merge_style_property( 'color:red;margin:5px;', 'color', 0 ); - $this->assertSame( 'margin:5px;', $result ); + // Replaces a property whose previous value contained a URL. + $this->assertSame( + 'background-image: none;', + $this->style_after_directives( 'background-image:url(https://example.com/a.png)', array( 'background-image' => 'none' ) ) + ); - // It doesn't add a new property with an empty string. - $result = $this->merge_style_property( 'color:red;', 'padding', '' ); - $this->assertSame( 'color:red;', $result ); + // Keeps every colon after the first, not only the second. + $this->assertSame( + '--fallback:url(https://a.test/x.png), url(https://b.test/y.png); color: green;', + $this->style_after_directives( '--fallback:url(https://a.test/x.png), url(https://b.test/y.png);', array( 'color' => 'green' ) ) + ); + } - // It doesn't add a new property with null. - $result = $this->merge_style_property( 'color:red;', 'padding', null ); - $this->assertSame( 'color:red;', $result ); + /** + * Tests that the `data-wp-style` directive preserves values containing a + * semicolon. + * + * A semicolon inside a quoted string or a `url()` does not end a declaration. + * + * @ticket 63899 + * + * @covers ::process_directives + */ + public function test_wp_style_preserves_values_containing_semicolons() { + // Keeps a quoted value containing a semicolon. + $this->assertSame( + 'font-family:"Foo;Bar",serif; color: green;', + $this->style_after_directives( 'font-family:"Foo;Bar",serif', array( 'color' => 'green' ) ) + ); - // It doesn't add a new property with false. - $result = $this->merge_style_property( 'color:red;', 'padding', false ); - $this->assertSame( 'color:red;', $result ); + // Keeps a data URI, which contains both a colon and a semicolon. + $this->assertSame( + 'background-image:url(data:image/gif;base64,R0lGODlhAQ); color: green;', + $this->style_after_directives( 'background-image:url(data:image/gif;base64,R0lGODlhAQ)', array( 'color' => 'green' ) ) + ); - // It doesn't add a new property with 0. - $result = $this->merge_style_property( 'color:red;', 'padding', 0 ); - $this->assertSame( 'color:red;', $result ); + // Replaces a property whose value contains a quoted semicolon. + $this->assertSame( + 'color:red; font-family: serif;', + $this->style_after_directives( 'font-family:"Foo;Bar",serif;color:red', array( 'font-family' => 'serif' ) ) + ); } /** - * Invokes the `process_directives` method of WP_Interactivity_API class. + * Tests that the `data-wp-style` directive sets a value containing a + * semicolon inside a string or a `url()`. * - * @param string $html The HTML that needs to be processed. - * @return array An array containing an instance of the WP_HTML_Tag_Processor and the processed HTML. + * The appended value is written from its tokens, which escapes a semicolon + * in a string and a semicolon or comma in a `url()`. + * + * @ticket 63899 + * + * @covers ::process_directives */ - private function process_directives( $html ) { - $new_html = $this->interactivity->process_directives( $html ); - $p = new WP_HTML_Tag_Processor( $new_html ); - $p->next_tag(); - return array( $p, $new_html ); + public function test_wp_style_sets_values_containing_semicolons() { + $this->assertSame( + 'color:red; font-family: "Foo\\3b Bar", serif;', + $this->style_after_directives( 'color:red', array( 'font-family' => '"Foo;Bar", serif' ) ) + ); + + $this->assertSame( + 'background-image: url(data:image/gif\\3b base64\\2c R0lGODlhAQ);', + $this->style_after_directives( null, array( 'background-image' => 'url(data:image/gif;base64,R0lGODlhAQ)' ) ) + ); + } + + /** + * Tests that the `data-wp-style` directive keeps segments that contain no + * colon. + * + * @ticket 63899 + * + * @covers ::process_directives + */ + public function test_wp_style_handles_segments_without_a_colon() { + $this->assertSame( + 'translate:none;translate3d(0px, 0px, 0px); color: green;', + $this->style_after_directives( 'translate:none;translate3d(0px, 0px, 0px);', array( 'color' => 'green' ) ) + ); + } + + /** + * Tests that the `data-wp-style` directive matches property names as CSS + * does. + * + * @ticket 63899 + * + * @covers ::process_directives + */ + public function test_wp_style_matches_property_names() { + // Replaces a standard property written in any case. + $this->assertSame( 'margin:5px; color: green;', $this->style_after_directives( 'COLOR:red;margin:5px;', array( 'color' => 'green' ) ) ); + + // Replaces only the custom property with the same case. + $this->assertSame( '--MyVar:1; --myvar: 3;', $this->style_after_directives( '--MyVar:1;--myvar:2;', array( '--myvar' => '3' ) ) ); + + // Removes every declaration of the property. + $this->assertSame( 'margin:5px; color: green;', $this->style_after_directives( 'color:red;margin:5px;color:blue;', array( 'color' => 'green' ) ) ); + } + + /** + * Tests that the `data-wp-style` directive replaces a declaration with an + * `!important` priority. + * + * @ticket 63899 + * + * @covers ::process_directives + */ + public function test_wp_style_replaces_important_declaration() { + $this->assertSame( 'margin:5px; color: blue;', $this->style_after_directives( 'color: red !important; margin:5px;', array( 'color' => 'blue' ) ) ); + $this->assertSame( 'margin:5px;', $this->style_after_directives( 'color: red !important; margin:5px;', array( 'color' => false ) ) ); + } + + /** + * Tests that the `data-wp-style` directive applies several entries on one + * element: it replaces, removes and adds properties in one style attribute. + * + * @ticket 63899 + * + * @covers ::process_directives + */ + public function test_wp_style_applies_several_entries() { + $this->assertSame( + 'margin: 0 auto; padding: 1em; width: 50%; background: url(https://example.com/b.png); color: blue;', + $this->style_after_directives( + 'color: red; margin: 0 auto; background: url(https://example.com/a.png); padding: 1em; height: 10px; width: 50%', + array( + 'color' => 'blue', + 'height' => false, + 'background' => 'url(https://example.com/b.png)', + ) + ) + ); + } + + /** + * Tests that the `data-wp-style` directive removes the property and adds + * nothing when the new value is not a valid declaration value. + * + * @ticket 63899 + * + * @covers ::process_directives + */ + public function test_wp_style_rejects_invalid_values() { + // A value with a semicolon outside a string or block. + $this->assertSame( 'margin:5px;', $this->style_after_directives( 'color:red;margin:5px;', array( 'color' => 'green; background: blue' ) ) ); + + // A value with an unclosed function. + $this->assertSame( 'margin:5px;', $this->style_after_directives( 'color:red;margin:5px;', array( 'color' => 'rgb(0 0 0' ) ) ); + + // A value with an !important priority. + $this->assertSame( 'margin:5px;', $this->style_after_directives( 'color:red;margin:5px;', array( 'color' => 'green !important' ) ) ); + + // A value with a closing brace. + $this->assertSame( 'margin:5px;', $this->style_after_directives( 'color:red;margin:5px;', array( 'color' => 'green}' ) ) ); + } + + /** + * Tests that the `data-wp-style` directive keeps the style attribute text + * when no declaration changes. + * + * @ticket 63899 + * + * @covers ::process_directives + */ + public function test_wp_style_keeps_style_attribute_text_when_unchanged() { + $this->assertSame( ' padding : 10px ;; ', $this->style_after_directives( ' padding : 10px ;; ', array( 'color' => false ) ) ); } /** @@ -172,7 +355,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 +373,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 +391,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 +414,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 +428,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 +470,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 +484,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 +555,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 +570,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 +584,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 +642,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 26ccd5e2118e3a8a669de57340ea56de7575c09c Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Tue, 17 Nov 2026 16:58:18 +0400 Subject: [PATCH 2/3] Tests: Limit invalid data-wp-style value cases to their classes 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(). --- .../tests/interactivity-api/wpInteractivityAPI-wp-style.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-style.php b/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-style.php index eed1f870d43a7..067bac0e855b4 100644 --- a/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-style.php +++ b/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-style.php @@ -319,9 +319,6 @@ public function test_wp_style_applies_several_entries() { * @covers ::process_directives */ public function test_wp_style_rejects_invalid_values() { - // A value with a semicolon outside a string or block. - $this->assertSame( 'margin:5px;', $this->style_after_directives( 'color:red;margin:5px;', array( 'color' => 'green; background: blue' ) ) ); - // A value with an unclosed function. $this->assertSame( 'margin:5px;', $this->style_after_directives( 'color:red;margin:5px;', array( 'color' => 'rgb(0 0 0' ) ) ); From 6de3e18a45d3138c7090ed29e2983b9d236559d0 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Tue, 17 Nov 2026 17:21:00 +0400 Subject: [PATCH 3/3] Ignore a data-wp-style value that is not one declaration value 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. --- .../class-wp-interactivity-api.php | 18 ++++++++---- .../wpInteractivityAPI-wp-style.php | 28 +++++++++++++------ 2 files changed, 33 insertions(+), 13 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 3c79c6bcbae36..da8a75d6e1e58 100644 --- a/src/wp-includes/interactivity-api/class-wp-interactivity-api.php +++ b/src/wp-includes/interactivity-api/class-wp-interactivity-api.php @@ -1480,13 +1480,14 @@ private function data_wp_class_processor( WP_Interactivity_API_Directives_Proces * * The value is converted to a string: * - * - A string is used without surrounding whitespace and trailing semicolons. + * - A string is used without surrounding whitespace. * - `true` is `true`, and a number other than `0` is its string. * - An empty string, `0` and any other value remove the property. * - * 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 appends nothing. + * A string that is not one valid CSS declaration value, such as one with a + * semicolon outside a string or block, or one ending in `!important`, is + * ignored and the property keeps its declarations, as `style.setProperty()` + * ignores it in the browser. * * @since 6.5.0 * @since 7.3.0 Parses the style attribute value as CSS. @@ -1513,7 +1514,14 @@ private function data_wp_style_processor( WP_Interactivity_API_Directives_Proces $value = $value ? (string) $value : ''; } - $values[ $entry['suffix'] ] = is_string( $value ) ? rtrim( trim( $value ), ';' ) : ''; + $value = is_string( $value ) ? trim( $value ) : ''; + + // Only a value that can be written as one declaration replaces the property. + if ( '' !== $value && ! ( new WP_HTML_Style_Attribute_Processor( '' ) )->append_declaration( $entry['suffix'], $value ) ) { + continue; + } + + $values[ $entry['suffix'] ] = $value; } if ( empty( $values ) ) { diff --git a/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-style.php b/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-style.php index 067bac0e855b4..6d1eb12221792 100644 --- a/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-style.php +++ b/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-style.php @@ -106,8 +106,8 @@ public function test_wp_style_sets_properties() { // Adds missing trailing semicolon. $this->assertSame( 'color:red;margin:5px; padding: 10px;', $this->style_after_directives( 'color:red;margin:5px', array( 'padding' => '10px' ) ) ); - // Doesn't add double semicolons. - $this->assertSame( 'color:red;margin:5px; padding: 10px;', $this->style_after_directives( 'color:red;margin:5px;', array( 'padding' => '10px;' ) ) ); + // Ignores a value with a trailing semicolon, which is not one declaration value. + $this->assertSame( 'color:red;margin:5px;', $this->style_after_directives( 'color:red;margin:5px;', array( 'padding' => '10px;' ) ) ); // Keeps empty declarations in the input. $this->assertSame( 'color:red;;margin:5px;; padding: 10px;', $this->style_after_directives( 'color:red;;margin:5px;;', array( 'padding' => '10px' ) ) ); @@ -311,22 +311,34 @@ public function test_wp_style_applies_several_entries() { } /** - * Tests that the `data-wp-style` directive removes the property and adds - * nothing when the new value is not a valid declaration value. + * Tests that the `data-wp-style` directive ignores a value that is not one + * valid declaration value and keeps the property's declarations. * * @ticket 63899 * * @covers ::process_directives */ - public function test_wp_style_rejects_invalid_values() { + public function test_wp_style_ignores_invalid_values() { // A value with an unclosed function. - $this->assertSame( 'margin:5px;', $this->style_after_directives( 'color:red;margin:5px;', array( 'color' => 'rgb(0 0 0' ) ) ); + $this->assertSame( 'color:red;margin:5px;', $this->style_after_directives( 'color:red;margin:5px;', array( 'color' => 'rgb(0 0 0' ) ) ); // A value with an !important priority. - $this->assertSame( 'margin:5px;', $this->style_after_directives( 'color:red;margin:5px;', array( 'color' => 'green !important' ) ) ); + $this->assertSame( 'color:red;margin:5px;', $this->style_after_directives( 'color:red;margin:5px;', array( 'color' => 'green !important' ) ) ); // A value with a closing brace. - $this->assertSame( 'margin:5px;', $this->style_after_directives( 'color:red;margin:5px;', array( 'color' => 'green}' ) ) ); + $this->assertSame( 'color:red;margin:5px;', $this->style_after_directives( 'color:red;margin:5px;', array( 'color' => 'green}' ) ) ); + + // An invalid value leaves the other entries on the element in effect. + $this->assertSame( + 'margin:5px; color: blue;', + $this->style_after_directives( + 'color:red;margin:5px;', + array( + 'color' => 'blue', + 'padding' => '1px}', + ) + ) + ); } /**