Skip to content
Open
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
6 changes: 6 additions & 0 deletions docs/wp-html-style-attribute-processor-spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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.
*
Expand Down
37 changes: 37 additions & 0 deletions tests/phpunit/tests/html-api/wpHtmlStyleAttributeProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, array{string, string}>
*/
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
Expand Down
Loading