Skip to content

Commit 71b865f

Browse files
committed
Pin style processor mutation sequencing
1 parent 4d49c90 commit 71b865f

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

‎src/wp-includes/html-api/class-wp-html-style-attribute-processor.php‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ class WP_HTML_Style_Attribute_Processor {
9898
* Do not instantiate directly. Use
9999
* {@see WP_HTML_Style_Attribute_Processor::create()} instead.
100100
*
101+
* @since {WP_VERSION}
102+
*
101103
* @param string $style Decoded CSS text value from a style attribute.
102104
*/
103105
private function __construct( string $style ) {
@@ -107,6 +109,8 @@ private function __construct( string $style ) {
107109
/**
108110
* Creates a processor for decoded CSS text from a style attribute.
109111
*
112+
* @since {WP_VERSION}
113+
*
110114
* @param string $decoded_css_text Decoded CSS text value from a style attribute.
111115
* @return static Created processor.
112116
*/
@@ -124,6 +128,8 @@ public static function create( $decoded_css_text ) {
124128
* When a property name is provided, normal CSS properties are matched
125129
* ASCII-case-insensitively while custom properties are matched exactly.
126130
*
131+
* @since {WP_VERSION}
132+
*
127133
* @param string|null $property_name Optional declaration property name to match.
128134
* @return bool Whether a matching declaration was found.
129135
*/
@@ -149,6 +155,8 @@ public function next_declaration( ?string $property_name = null ): bool {
149155
/**
150156
* Gets the current declaration's property name.
151157
*
158+
* @since {WP_VERSION}
159+
*
152160
* @return string|null Property name, or null when not on a declaration.
153161
*/
154162
public function get_property_name(): ?string {
@@ -159,6 +167,8 @@ public function get_property_name(): ?string {
159167
/**
160168
* Indicates whether the current declaration has an !important priority.
161169
*
170+
* @since {WP_VERSION}
171+
*
162172
* @return bool|null Whether the current declaration has an !important priority,
163173
* or null when not on a declaration.
164174
*/
@@ -170,6 +180,8 @@ public function is_important(): ?bool {
170180
/**
171181
* Sets the value of the current declaration.
172182
*
183+
* @since {WP_VERSION}
184+
*
173185
* @param string $value CSS declaration value, without the property name.
174186
* @param bool|null $important Optional. Whether the declaration should be important.
175187
* Defaults to preserving the current priority.
@@ -224,6 +236,8 @@ public function set_value( string $value, ?bool $important = null ): bool {
224236
/**
225237
* Sets the !important priority of the current declaration.
226238
*
239+
* @since {WP_VERSION}
240+
*
227241
* @param bool $important Whether the declaration should be important.
228242
* @return bool Whether the current declaration priority is set as requested.
229243
*/
@@ -319,6 +333,8 @@ public function set_important( bool $important ): bool {
319333
* Only the current declaration is removed. Other declarations with the same
320334
* property name remain in place.
321335
*
336+
* @since {WP_VERSION}
337+
*
322338
* @return bool Whether the current declaration was removed.
323339
*/
324340
public function remove_declaration(): bool {
@@ -353,6 +369,8 @@ public function remove_declaration(): bool {
353369
* Appending does not remove or replace existing declarations with the same
354370
* property name.
355371
*
372+
* @since {WP_VERSION}
373+
*
356374
* @param string $property_name CSS property name.
357375
* @param string $value CSS declaration value, without the property name.
358376
* @param bool $important Optional. Whether the declaration should be important. Default false.
@@ -413,6 +431,8 @@ public function append_declaration( string $property_name, string $value, bool $
413431
/**
414432
* Returns the updated decoded CSS text value for the style attribute.
415433
*
434+
* @since {WP_VERSION}
435+
*
416436
* @return string Updated decoded CSS text value for the style attribute.
417437
*/
418438
public function get_updated_style(): string {

‎tests/phpunit/tests/html-api/wpHtmlStyleAttributeProcessor.php‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,39 @@ public function test_append_declaration_preserves_multiple_appends_in_order() {
414414
$this->assertSame( 'color: red; background: white;', $processor->get_updated_style() );
415415
}
416416

417+
/**
418+
* @covers ::append_declaration
419+
* @covers ::get_property_name
420+
* @covers ::get_updated_style
421+
* @covers ::is_important
422+
* @covers ::next_declaration
423+
* @covers ::remove_declaration
424+
* @covers ::set_important
425+
* @covers ::set_value
426+
*/
427+
public function test_mixed_mutations_preserve_the_logical_cursor_and_declaration_order() {
428+
$processor = WP_HTML_Style_Attribute_Processor::create( 'color: red; background: white;' );
429+
430+
$this->assertTrue( $processor->next_declaration( 'color' ) );
431+
$this->assertTrue( $processor->set_value( 'green', true ) );
432+
$this->assertTrue( $processor->set_important( false ) );
433+
$this->assertTrue( $processor->append_declaration( 'border', '1px', true ) );
434+
435+
$this->assertSame( 'color', $processor->get_property_name() );
436+
$this->assertFalse( $processor->is_important() );
437+
$this->assertTrue( $processor->set_value( 'blue' ) );
438+
$this->assertSame( 'color: blue; background: white; border: 1px !important;', $processor->get_updated_style() );
439+
440+
$this->assertTrue( $processor->remove_declaration() );
441+
$this->assertNull( $processor->get_property_name() );
442+
$this->assertTrue( $processor->next_declaration() );
443+
$this->assertSame( 'background', $processor->get_property_name() );
444+
$this->assertTrue( $processor->next_declaration() );
445+
$this->assertSame( 'border', $processor->get_property_name() );
446+
$this->assertTrue( $processor->is_important() );
447+
$this->assertSame( 'background: white; border: 1px !important;', $processor->get_updated_style() );
448+
}
449+
417450
/**
418451
* @covers ::append_declaration
419452
* @covers ::get_updated_style

0 commit comments

Comments
 (0)