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
69 changes: 69 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,75 @@ public function get_token_length(): ?int {
return $this->token_length;
}

/**
* Splits CSS into the source text between its top-level commas.
*
* A comma inside a string, comment, escape, url token, function, `()`,
* `[]` or `{}` block belongs to the item that holds it. Each item is
* the bytes of the input between two top-level commas, with whitespace,
* comments and escapes as written, so joining the items with commas
* reproduces the input.
*
* Example:
*
* ```php interactive
* $items = WP_CSS_Token_Processor::split_comma_separated_list( '[title="a,b"], :is(c, d) e,f\\,g' );
* foreach ( $items as $item ) {
* echo "[{$item}]\n";
* }
* // Outputs:
* // [[title="a,b"]]
* // [ :is(c, d) e]
* // [f\,g]
* ```
*
* Unlike the CSS Syntax 3 algorithm, a comma at the end of the input
* produces a final empty item, so a caller whose grammar forbids a
* trailing comma can reject it. Empty input produces no items.
*
* @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 string[] Source text of each item, in source order.
*/
public static function split_comma_separated_list( string $css ): array {
$processor = new self( $css );
$items = array();
$item_at = 0;

// 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 ) {
$items[] = substr( $css, $item_at, $processor->token_starts_at - $item_at );
$item_at = $processor->at;
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;
}
}

if ( $item_at < strlen( $css ) || array() !== $items ) {
$items[] = substr( $css, $item_at );
}

return $items;
}

/**
* Clears token state between tokens.
*
Expand Down
57 changes: 57 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,61 @@ 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_split_comma_separated_list
*
* @covers ::split_comma_separated_list
*
* @param string $css CSS to split.
* @param string[] $expected Source text of each item.
*/
public function test_split_comma_separated_list( string $css, array $expected ): void {
$items = WP_CSS_Token_Processor::split_comma_separated_list( $css );

$this->assertSame( $expected, $items, 'Wrong items.' );
$this->assertSame( $css, implode( ',', $items ), 'Joining the items with commas must reproduce the input.' );
}

/**
* Data provider for test_split_comma_separated_list().
*
* @return array[]
*/
public static function data_split_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' ) ),
'whitespace around commas' => array( ' , ', array( ' ', ' ' ) ),
'selector list' => array( 'h1, h2,h3', array( 'h1', ' h2', 'h3' ) ),
'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' ) ),
'comma in an attribute string' => array( '[title="a,b"],c', array( '[title="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 an uppercase url token' => array( 'URL(a,b),c', array( 'URL(a,b)', 'c' ) ),
'comma in a quoted url argument' => array( 'url("a,b"),c', array( 'url("a,b")', 'c' ) ),
'bad url ends at its first closer' => array( 'url(a(b),c', array( 'url(a(b)', 'c' ) ),
'escaped url name' => array( 'u\\rl(a(b),c', array( 'u\\rl(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' ) ),
'attribute selectors in a function' => array( 'input:where([type=a],[type=b]), textarea', array( 'input:where([type=a],[type=b])', ' textarea' ) ),
'nested blocks' => array( 'f(a,[b,{c,d}],e),g', array( 'f(a,[b,{c,d}],e)', 'g' ) ),
'string holds a closer' => array( ':is("(",a),b', array( ':is("(",a)', 'b' ) ),
'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