From 5d49c115c290bcf3f6d516d15b1f4a2f13ab890c Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Tue, 17 Nov 2026 11:03:35 +0400 Subject: [PATCH 1/2] Add WP_CSS_Token::is_whitespace_or_comment() Most CSS grammars allow whitespace and comments between component values, so every consumer that reads a value skips both token types. The parser-based safecss_filter_attr() defines its own helper for this, and the font-family and selector-list consumers need the same test. --- .../css-api/class-wp-css-token.php | 32 ++++++++++++++++++ tests/phpunit/tests/css-api/wpCssToken.php | 33 +++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/src/wp-includes/css-api/class-wp-css-token.php b/src/wp-includes/css-api/class-wp-css-token.php index 55af708415637..b95b39315daec 100644 --- a/src/wp-includes/css-api/class-wp-css-token.php +++ b/src/wp-includes/css-api/class-wp-css-token.php @@ -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. * diff --git a/tests/phpunit/tests/css-api/wpCssToken.php b/tests/phpunit/tests/css-api/wpCssToken.php index 10fa39d42fb23..0494be2962bef 100644 --- a/tests/phpunit/tests/css-api/wpCssToken.php +++ b/tests/phpunit/tests/css-api/wpCssToken.php @@ -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. * From f607ae13a9b4a040f8c8562b044485f251ad3650 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Tue, 17 Nov 2026 11:04:35 +0400 Subject: [PATCH 2/2] Add WP_CSS_Token_Processor::parse_comma_separated_list() Font family lists, selector lists and several property values are comma-separated lists of component values. Core splits them with explode( ',' ) or with scanners that track some nesting, which split inside strings, escapes, comments or blocks. The method follows the CSS Syntax 3 algorithm and returns the tokens of each group. A comma at the end of the input produces a final empty group, which the algorithm omits, because the font-family and selector grammars reject a trailing comma and a caller cannot otherwise detect one. Blocks are not nested in the result; a caller that needs them reads the function and bracket tokens in order. --- .../css-api/class-wp-css-token-processor.php | 74 +++++++++++++++++++ .../tests/css-api/wpCssTokenProcessor.php | 56 ++++++++++++++ 2 files changed, 130 insertions(+) diff --git a/src/wp-includes/css-api/class-wp-css-token-processor.php b/src/wp-includes/css-api/class-wp-css-token-processor.php index e9b0e27e4bd7e..4463542c984b1 100644 --- a/src/wp-includes/css-api/class-wp-css-token-processor.php +++ b/src/wp-includes/css-api/class-wp-css-token-processor.php @@ -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. * diff --git a/tests/phpunit/tests/css-api/wpCssTokenProcessor.php b/tests/phpunit/tests/css-api/wpCssTokenProcessor.php index c4c190e87cec9..cfb303cebc9f4 100644 --- a/tests/phpunit/tests/css-api/wpCssTokenProcessor.php +++ b/tests/phpunit/tests/css-api/wpCssTokenProcessor.php @@ -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' ) ), + ); + } }