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
74 changes: 74 additions & 0 deletions src/wp-includes/css-api/class-wp-css-token-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,80 @@ public function get_token_length(): ?int {
return $this->token_length;
}

/**
* Splits CSS into the groups of tokens between its top-level commas.
*
* A comma inside a function, `()`, `[]` or `{}` block belongs to the
* group that holds the block. Each group holds every token of its
* part of the input, including whitespace and comments, and no comma.
*
* Example:
*
* ```php interactive
* $groups = WP_CSS_Token_Processor::parse_comma_separated_list( '"A, B", var(--a, b) c' );
* foreach ( $groups as $group ) {
* $text = '';
* foreach ( $group as $token ) {
* $text .= substr( '"A, B", var(--a, b) c', $token->get_start(), $token->get_length() );
* }
* echo "[{$text}]\n";
* }
* // Outputs:
* // ["A, B"]
* // [ var(--a, b) c]
* ```
*
* Unlike the CSS Syntax 3 algorithm, a comma at the end of the input
* produces a final empty group, so a caller whose grammar forbids a
* trailing comma can reject it. Empty input produces no groups.
* Blocks and functions are not nested in the result: their tokens
* appear in order in the group that holds them.
*
* @since 7.3.0
*
* @link https://www.w3.org/TR/css-syntax-3/#parse-comma-separated-list-of-component-values
*
* @param string $css CSS source to split.
* @return WP_CSS_Token[][] Groups of tokens, in source order.
*/
public static function parse_comma_separated_list( string $css ): array {
$processor = new self( $css );
$groups = array();
$group = array();

// Closing token types of the open blocks and functions, innermost last.
$closers = array();

while ( $processor->next_token() ) {
$type = $processor->token_type;

if ( self::TOKEN_COMMA === $type && array() === $closers ) {
$groups[] = $group;
$group = array();
continue;
}

// A closing token ends a block only when it matches the innermost open block.
if ( array() !== $closers && end( $closers ) === $type ) {
array_pop( $closers );
} elseif ( self::TOKEN_FUNCTION === $type || self::TOKEN_LEFT_PAREN === $type ) {
$closers[] = self::TOKEN_RIGHT_PAREN;
} elseif ( self::TOKEN_LEFT_BRACKET === $type ) {
$closers[] = self::TOKEN_RIGHT_BRACKET;
} elseif ( self::TOKEN_LEFT_BRACE === $type ) {
$closers[] = self::TOKEN_RIGHT_BRACE;
}

$group[] = $processor->get_token();
}

if ( array() !== $group || array() !== $groups ) {
$groups[] = $group;
}

return $groups;
}

/**
* Clears token state between tokens.
*
Expand Down
32 changes: 32 additions & 0 deletions src/wp-includes/css-api/class-wp-css-token.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,38 @@ public function is_terminated(): bool {
return $this->is_terminated;
}

/**
* Reports whether the token is whitespace or a comment.
*
* CSS Syntax 3 discards comments while tokenizing, and most grammars
* allow whitespace between component values, so a consumer that reads
* a value usually skips both.
*
* Example:
*
* ```php interactive
* $processor = new WP_CSS_Token_Processor( "a /* b *\x2F c" );
* while ( $processor->next_token() ) {
* $token = $processor->get_token();
* if ( ! $token->is_whitespace_or_comment() ) {
* echo $token->get_value() . "\n";
* }
* }
* // Outputs:
* // a
* // c
* ```
*
* @since 7.3.0
*
* @link https://www.w3.org/TR/css-syntax-3/#consume-comments
*
* @return bool Whether the token is a whitespace token or a comment.
*/
public function is_whitespace_or_comment(): bool {
return WP_CSS_Token_Processor::TOKEN_WHITESPACE === $this->type || WP_CSS_Token_Processor::TOKEN_COMMENT === $this->type;
}

/**
* Decodes the value from the CSS source.
*
Expand Down
33 changes: 33 additions & 0 deletions tests/phpunit/tests/css-api/wpCssToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,39 @@ public static function data_is_terminated(): array {
);
}

/**
* Tests that whitespace and comment tokens are reported as such.
*
* @ticket 65738
* @dataProvider data_is_whitespace_or_comment
*
* @covers ::is_whitespace_or_comment
*/
public function test_is_whitespace_or_comment( string $css, bool $expected ): void {
$processor = new WP_CSS_Token_Processor( $css );
$this->assertTrue( $processor->next_token() );

$this->assertSame( $expected, $processor->get_token()->is_whitespace_or_comment() );
}

/**
* Data provider for test_is_whitespace_or_comment().
*
* @return array[]
*/
public static function data_is_whitespace_or_comment(): array {
return array(
'space' => array( ' a', true ),
'newline and tab' => array( "\n\ta", true ),
'comment' => array( '/* a */', true ),
'unterminated comment' => array( '/* a', true ),
'ident' => array( 'a', false ),
'string with a space' => array( '" "', false ),
'escaped space' => array( '\\20', false ),
'delim slash' => array( '/ *', false ),
);
}

/**
* Tests that the value of a comment token is the text between its delimiters.
*
Expand Down
56 changes: 56 additions & 0 deletions tests/phpunit/tests/css-api/wpCssTokenProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2619,4 +2619,60 @@ public function test_ident_start_codepoint_bounds_check(): void {
);
$this->assertSame( $expected_tokens, $actual_tokens );
}

/**
* Tests that a comma-separated list splits at its top-level commas only.
*
* @ticket 65738
* @dataProvider data_parse_comma_separated_list
*
* @covers ::parse_comma_separated_list
*
* @param string $css CSS to split.
* @param string[] $expected Source text of each group.
*/
public function test_parse_comma_separated_list( string $css, array $expected ): void {
$groups = WP_CSS_Token_Processor::parse_comma_separated_list( $css );

$actual = array();
foreach ( $groups as $group ) {
$text = '';
foreach ( $group as $token ) {
$text .= substr( $css, $token->get_start(), $token->get_length() );
}
$actual[] = $text;
}

$this->assertSame( $expected, $actual );
}

/**
* Data provider for test_parse_comma_separated_list().
*
* @return array[]
*/
public static function data_parse_comma_separated_list(): array {
return array(
'empty input' => array( '', array() ),
'whitespace only' => array( ' ', array( ' ' ) ),
'one item' => array( 'a', array( 'a' ) ),
'items keep their whitespace' => array( 'a , b', array( 'a ', ' b' ) ),
'trailing comma' => array( 'a,', array( 'a', '' ) ),
'leading comma' => array( ',a', array( '', 'a' ) ),
'empty item' => array( 'a,,b', array( 'a', '', 'b' ) ),
'comma in a string' => array( '"a,b",c', array( '"a,b"', 'c' ) ),
'escaped comma' => array( 'a\,b,c', array( 'a\,b', 'c' ) ),
'comma in a comment' => array( 'a/*,*/,b', array( 'a/*,*/', 'b' ) ),
'comma in a function' => array( 'var(--a, b), c', array( 'var(--a, b)', ' c' ) ),
'comma in a url token' => array( 'url(a,b),c', array( 'url(a,b)', 'c' ) ),
'comma in parentheses' => array( ':is(a, b), c', array( ':is(a, b)', ' c' ) ),
'comma in brackets' => array( '[a, b], c', array( '[a, b]', ' c' ) ),
'comma in braces' => array( '{a, b}, c', array( '{a, b}', ' c' ) ),
'nested blocks' => array( 'f(a,[b,{c,d}],e),g', array( 'f(a,[b,{c,d}],e)', 'g' ) ),
'stray closer at the top level' => array( 'a),b', array( 'a)', 'b' ) ),
'mismatched closer in a block' => array( '(a]b,c),d', array( '(a]b,c)', 'd' ) ),
'unclosed block holds the rest' => array( 'f(a,b', array( 'f(a,b' ) ),
'unterminated string holds comma' => array( '"a,b', array( '"a,b' ) ),
);
}
}
Loading