Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 18 additions & 19 deletions src/wp-includes/interactivity-api/class-wp-interactivity-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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();
}

/**
Expand Down
162 changes: 137 additions & 25 deletions tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-style.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,47 +63,145 @@ 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 );
}

/**
* 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 );
}

/**
Expand Down Expand Up @@ -172,7 +270,7 @@ private function process_directives( $html ) {
public function test_wp_style_sets_style_attribute() {
$html = '<div data-wp-style--color="myPlugin::state.green">Text</div>';
list($p) = $this->process_directives( $html );
$this->assertSame( 'color:green;', $p->get_attribute( 'style' ) );
$this->assertSame( 'color: green;', $p->get_attribute( 'style' ) );
}

/**
Expand All @@ -190,7 +288,7 @@ public function test_wp_style_sets_multiple_style_properties() {
data-wp-style--background="myPlugin::state.green"
>Text</div>';
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' ) );
}

/**
Expand All @@ -208,7 +306,7 @@ public function test_wp_style_sets_multiple_style_properties_with_different_valu
data-wp-style--background="myPlugin::state.false"
>Text</div>';
list($p) = $this->process_directives( $html );
$this->assertSame( 'color:green;', $p->get_attribute( 'style' ) );
$this->assertSame( 'color: green;', $p->get_attribute( 'style' ) );

$html = '
<div
Expand All @@ -217,7 +315,7 @@ public function test_wp_style_sets_multiple_style_properties_with_different_valu
data-wp-style--background="myPlugin::state.false"
>Text</div>';
list($p) = $this->process_directives( $html );
$this->assertSame( 'color:green;', $p->get_attribute( 'style' ) );
$this->assertSame( 'color: green;', $p->get_attribute( 'style' ) );
}

/**
Expand All @@ -231,7 +329,21 @@ 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 = '<div style="padding:10px;" data-wp-style--color="myPlugin::state.green">Text</div>';
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' ) );
}

/**
* 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 = '<div style="background-image:url(https://example.com/a.png)" data-wp-style--color="myPlugin::state.green">Text</div>';
list($p) = $this->process_directives( $html );
$this->assertSame( 'background-image:url(https://example.com/a.png); color: green;', $p->get_attribute( 'style' ) );
}

/**
Expand All @@ -245,7 +357,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 = '<div style="color:red;" data-wp-style--color="myPlugin::state.green">Text</div>';
list($p) = $this->process_directives( $html );
$this->assertSame( 'color:green;', $p->get_attribute( 'style' ) );
$this->assertSame( 'color: green;', $p->get_attribute( 'style' ) );
}

/**
Expand Down Expand Up @@ -287,7 +399,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 = '<div style="color:green;" data-wp-style--color="myPlugin::state.green">Text</div>';
list($p) = $this->process_directives( $html );
$this->assertSame( 'color:green;', $p->get_attribute( 'style' ) );
$this->assertSame( 'color: green;', $p->get_attribute( 'style' ) );
}

/**
Expand All @@ -301,7 +413,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 = '<div style="padding:10px;color:green;" data-wp-style--color="myPlugin::state.green">Text</div>';
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' ) );
}

/**
Expand Down Expand Up @@ -372,7 +484,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 = '<div style="padding:10px" data-wp-style--color="">Text</div>';
list($p) = $this->process_directives( $html );
$this->assertSame( 'padding:10px;', $p->get_attribute( 'style' ) );
$this->assertSame( 'padding:10px', $p->get_attribute( 'style' ) );
}

/**
Expand All @@ -387,7 +499,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 = '<div style="padding: 10px;" data-wp-style--color>Text</div>';
list($p) = $this->process_directives( $html );
$this->assertSame( 'padding:10px;', $p->get_attribute( 'style' ) );
$this->assertSame( 'padding: 10px;', $p->get_attribute( 'style' ) );
}

/**
Expand All @@ -401,7 +513,7 @@ public function test_wp_style_doesnt_change_style_attribute_without_value() {
public function test_wp_style_works_with_multiple_directives() {
$html = '<div data-wp-style--color="myPlugin::state.green" data-wp-style--color="myPlugin::state.green">Text</div>';
list($p) = $this->process_directives( $html );
$this->assertSame( 'color:green;', $p->get_attribute( 'style' ) );
$this->assertSame( 'color: green;', $p->get_attribute( 'style' ) );
}

/**
Expand Down Expand Up @@ -459,7 +571,7 @@ public function test_wp_style_doesnt_add_style_property_on_falsy_values() {
public function test_wp_style_can_use_CSS_variables() {
$html = '<div data-wp-style----text-color="myPlugin::state.green">Text</div>';
list($p) = $this->process_directives( $html );
$this->assertSame( '--text-color:green;', $p->get_attribute( 'style' ) );
$this->assertSame( '--text-color: green;', $p->get_attribute( 'style' ) );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() );
}
Expand Down