diff --git a/docs/wp-html-style-attribute-processor-spec.md b/docs/wp-html-style-attribute-processor-spec.md index 9d24f841676c2..e0fdeaf0e77ce 100644 --- a/docs/wp-html-style-attribute-processor-spec.md +++ b/docs/wp-html-style-attribute-processor-spec.md @@ -319,6 +319,12 @@ Repair should insert only the specific missing delimiters discovered from parser or tokenizer state, plus the boundary needed before the appended declaration. If the processor cannot determine a precise repair, append returns `false`. +A whitespace token at the end of the style stays at the end: the separator and +the appended declaration go before it. `margin: 5px ` becomes +`margin: 5px; padding: 10px; `. Trailing whitespace inside another token, such as +an unterminated string or an escaped space, belongs to that token and stays before +the appended declaration. + EOF repair is essential for append. `set_value()` replacement values must be self-contained and do not receive EOF repair. diff --git a/src/wp-includes/html-api/class-wp-html-style-attribute-processor.php b/src/wp-includes/html-api/class-wp-html-style-attribute-processor.php index 04c8486f3eef2..441548917e10a 100644 --- a/src/wp-includes/html-api/class-wp-html-style-attribute-processor.php +++ b/src/wp-includes/html-api/class-wp-html-style-attribute-processor.php @@ -484,10 +484,8 @@ public function append_declaration( string $property_name, string $value, bool $ $this->ensure_parsed(); $old_declaration_count = count( $this->declarations ); - $trimmed_style = rtrim( $this->style, self::WHITESPACE ); - $insert_at = strlen( $trimmed_style ); - $repair_at = strlen( $this->style ); - $eof_repair = $this->get_eof_repair( $repair_at ); + $insert_at = $this->get_append_offset(); + $eof_repair = $this->get_eof_repair( $insert_at ); if ( null === $eof_repair ) { return false; } @@ -500,7 +498,7 @@ public function append_declaration( string $property_name, string $value, bool $ return false; } - $updated = $this->verify_append( $repair_at, $eof_repair, $separator, $declaration_text, $old_declaration_count, $name, $parsed_value, $important ); + $updated = $this->verify_append( $insert_at, $eof_repair, $separator, $declaration_text, $old_declaration_count, $name, $parsed_value, $important ); if ( null === $updated ) { return false; } @@ -1146,6 +1144,29 @@ private function apply_update( self $updated, int $current_declaration, bool $cu } } + /** + * Gets the byte offset at which to append a declaration. + * + * A whitespace token at the end of the style stays after the appended + * declaration, so the separator follows the text before it. Trailing + * whitespace inside another token, such as an unterminated string or + * an escaped space, is part of that token and stays before the + * appended declaration. + * + * @since 7.3.0 + * @ignore + * + * @return int Byte offset. + */ + private function get_append_offset(): int { + $last_token = end( $this->tokens ); + if ( false !== $last_token && WP_CSS_Token_Processor::TOKEN_WHITESPACE === $last_token->get_type() ) { + return $last_token->get_start(); + } + + return strlen( $this->style ); + } + /** * Gets the separator to use before an appended declaration. * diff --git a/tests/phpunit/tests/html-api/wpHtmlStyleAttributeProcessor.php b/tests/phpunit/tests/html-api/wpHtmlStyleAttributeProcessor.php index 8eec264f5ff5c..eec42a2c1d99b 100644 --- a/tests/phpunit/tests/html-api/wpHtmlStyleAttributeProcessor.php +++ b/tests/phpunit/tests/html-api/wpHtmlStyleAttributeProcessor.php @@ -1043,6 +1043,43 @@ static function ( WP_CSS_Token $token ): array { return $declarations; } + /** + * @ticket 65738 + * + * @covers ::append_declaration + * @covers ::get_updated_style + * + * @dataProvider data_styles_ending_in_whitespace + * + * @param string $style Style text. + * @param string $expected Updated style text. + */ + public function test_append_declaration_before_trailing_whitespace( string $style, string $expected ) { + $processor = new WP_HTML_Style_Attribute_Processor( $style ); + + $this->assertTrue( $processor->append_declaration( 'padding', '10px' ) ); + $this->assertSame( $expected, $processor->get_updated_style() ); + } + + /** + * Data provider. + * + * @return array + */ + public static function data_styles_ending_in_whitespace(): array { + return array( + 'space after a semicolon' => array( 'margin: 5px; ', 'margin: 5px; padding: 10px; ' ), + 'space after a value' => array( 'margin: 5px ', 'margin: 5px; padding: 10px; ' ), + 'newline and tab after a value' => array( "margin: 5px\n\t", "margin: 5px; padding: 10px;\n\t" ), + 'space after a comment' => array( 'margin: 5px /* a */ ', 'margin: 5px /* a */; padding: 10px; ' ), + 'whitespace only' => array( ' ', 'padding: 10px; ' ), + 'space in an open function' => array( 'width: var(--x ', 'width: var(--x); padding: 10px; ' ), + 'space in an unterminated string' => array( 'font-family: "a ', 'font-family: "a "; padding: 10px;' ), + 'escaped space at the end' => array( 'font-family: a\\ ', 'font-family: a\\ ; padding: 10px;' ), + 'space in an unterminated url' => array( 'background: url(a ', 'background: url(a ); padding: 10px;' ), + ); + } + /** * @covers ::append_declaration * @covers ::get_updated_style