diff --git a/src/wp-includes/block-supports/states.php b/src/wp-includes/block-supports/states.php index 809072fd5612e..873cf90257281 100644 --- a/src/wp-includes/block-supports/states.php +++ b/src/wp-includes/block-supports/states.php @@ -431,12 +431,7 @@ function wp_get_block_state_element_selectors( $root_selector ) { break; } - $selector_prefix = "$block_selector "; - if ( ! str_contains( $element_selector, ',' ) ) { - $selectors[] = $selector_prefix . $element_selector; - continue; - } - + $selector_prefix = "$block_selector "; $prepended_selectors = array(); foreach ( wp_split_selector_list( $element_selector ) as $selector ) { $prepended_selectors[] = $selector_prefix . $selector; @@ -577,7 +572,11 @@ function wp_get_block_state_unique_class( $block_name, $css_rules ) { /** * Splits a selector list by top-level commas. * + * Commas inside strings, comments, escapes, functions and blocks do not + * split the list. Each selector keeps its surrounding whitespace. + * * @since 7.1.0 + * @since 7.3.0 Ignores commas inside strings, comments, escapes, brackets and braces. * * @param string $selector CSS selector list. * @return string[] Selectors. @@ -587,30 +586,7 @@ function wp_split_selector_list( $selector ) { return array( $selector ); } - $selectors = array(); - $current_selector = ''; - $parentheses_depth = 0; - $selector_length = strlen( $selector ); - - for ( $i = 0; $i < $selector_length; $i++ ) { - $char = $selector[ $i ]; - - if ( '(' === $char ) { - ++$parentheses_depth; - } elseif ( ')' === $char && $parentheses_depth > 0 ) { - --$parentheses_depth; - } elseif ( ',' === $char && 0 === $parentheses_depth ) { - $selectors[] = $current_selector; - $current_selector = ''; - continue; - } - - $current_selector .= $char; - } - - $selectors[] = $current_selector; - - return $selectors; + return WP_CSS_Token_Processor::split_comma_separated_list( $selector ); } /** @@ -638,26 +614,57 @@ function wp_get_state_selector_ancestor( $selector, $block_name ) { return ''; } + /* + * - `$ancestor_length`: bytes up to the end of the first top-level combinator, + * including the whitespace and comments around it. + * - `$next_compound`: source text of the compound after that combinator, + * without the contents of its functions and brackets. + * + * .wp-block-list > li:not(.x) a + * ╰──── ancestor ─╯╰─ next ──╯ + */ + $processor = new WP_CSS_Token_Processor( $selector ); $ancestor_length = 0; $next_compound = ''; $depth = 0; - $selector_length = strlen( $selector ); - for ( $i = 0; $i < $selector_length; $i++ ) { - $char = $selector[ $i ]; + while ( $processor->next_token() ) { + $type = $processor->get_token_type(); + $token_end = $processor->get_token_start() + $processor->get_token_length(); - if ( '(' === $char || '[' === $char ) { - ++$depth; - } elseif ( ( ')' === $char || ']' === $char ) && $depth > 0 ) { - --$depth; - } elseif ( 0 === $depth && str_contains( " \t\n\r\f>+~", $char ) ) { - if ( $ancestor_length ) { - break; + if ( $depth > 0 ) { + if ( WP_CSS_Token_Processor::TOKEN_RIGHT_PAREN === $type || WP_CSS_Token_Processor::TOKEN_RIGHT_BRACKET === $type ) { + --$depth; + } elseif ( WP_CSS_Token_Processor::TOKEN_FUNCTION === $type || WP_CSS_Token_Processor::TOKEN_LEFT_PAREN === $type || WP_CSS_Token_Processor::TOKEN_LEFT_BRACKET === $type ) { + ++$depth; + } + continue; + } + + $is_combinator = ( + WP_CSS_Token_Processor::TOKEN_WHITESPACE === $type || + ( WP_CSS_Token_Processor::TOKEN_DELIM === $type && str_contains( '>+~', $selector[ $processor->get_token_start() ] ) ) + ); + + if ( $is_combinator || WP_CSS_Token_Processor::TOKEN_COMMENT === $type ) { + if ( '' !== $next_compound ) { + if ( $is_combinator ) { + break; + } + continue; + } + if ( $is_combinator || $ancestor_length > 0 ) { + $ancestor_length = $token_end; } - $ancestor_length = $i + strspn( $selector, " \t\n\r\f>+~", $i ); - $i = $ancestor_length - 1; - } elseif ( 0 === $depth && $ancestor_length ) { - $next_compound .= $char; + continue; + } + + if ( $ancestor_length > 0 ) { + $next_compound .= substr( $selector, $processor->get_token_start(), $processor->get_token_length() ); + } + + if ( WP_CSS_Token_Processor::TOKEN_FUNCTION === $type || WP_CSS_Token_Processor::TOKEN_LEFT_PAREN === $type || WP_CSS_Token_Processor::TOKEN_LEFT_BRACKET === $type ) { + ++$depth; } } diff --git a/src/wp-includes/class-wp-duotone.php b/src/wp-includes/class-wp-duotone.php index 49cf221c11af6..fd0cd4c223c17 100644 --- a/src/wp-includes/class-wp-duotone.php +++ b/src/wp-includes/class-wp-duotone.php @@ -846,7 +846,9 @@ private static function get_global_styles_presets( $sources ) { */ private static function enqueue_block_css( $filter_id, $duotone_selector, $filter_value ) { // Build the CSS selectors to which the filter will be applied. - $selectors = explode( ',', $duotone_selector ); + $selectors = str_contains( $duotone_selector, ',' ) + ? WP_CSS_Token_Processor::split_comma_separated_list( $duotone_selector ) + : array( $duotone_selector ); $selectors_scoped = array(); foreach ( $selectors as $selector_part ) { diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 15d08fba457a9..af00baee62d72 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -1660,29 +1660,6 @@ protected static function append_to_selector( $selector, $to_append ) { return trim( $selector, " \t\n" ) . $to_append; } - /** - * Check for an opportunity to skip the more-costly selector splitting. - * This should be possible if there are no comments, strings, functions, - * URLs, escapes, or comment declaration openers (CDOs). - * - * Note that this means the fast-path will not apply for selectors like - * the following incomplete list: - * - * - `[class ~= "wide"]` - * - `.wp-block:is(.is-style-a, .is-style-b)` - * - `:nth-child(1)` - * - * These syntax forms all present opportunities where a comma may not - * separate selectors. If none of the start characters are present, - * there should be no way for a comma to mean anything other than a - * comma token. The exception are syntax errors, which are not handled here. - * - * @link https://www.w3.org/TR/css-syntax-3/#parse-comma-separated-list-of-component-values - */ - if ( strlen( $selector ) === strcspn( $selector, '/\'"(<\\' ) ) { - return preg_replace( '~[ \t\n]*,[ \t\n]*~', "{$to_append}, ", trim( $selector, " \t\n" ) ) . $to_append; - } - $new_selectors = array(); $selectors = static::split_selector_list( $selector ); foreach ( $selectors as $sel ) { @@ -1709,29 +1686,6 @@ protected static function prepend_to_selector( $selector, $to_prepend ) { return $to_prepend . trim( $selector, " \t\n" ); } - /** - * Check for an opportunity to skip the more-costly selector splitting. - * This should be possible if there are no comments, strings, functions, - * URLs, escapes, or comment declaration openers (CDOs). - * - * Note that this means the fast-path will not apply for selectors like - * the following incomplete list: - * - * - `[class ~= "wide"]` - * - `.wp-block:is(.is-style-a, .is-style-b)` - * - `:nth-child(1)` - * - * These syntax forms all present opportunities where a comma may not - * separate selectors. If none of the start characters are present, - * there should be no way for a comma to mean anything other than a - * comma token. The exception are syntax errors, which are not handled here. - * - * @link https://www.w3.org/TR/css-syntax-3/#parse-comma-separated-list-of-component-values - */ - if ( strlen( $selector ) === strcspn( $selector, '/\'"(<\\' ) ) { - return $to_prepend . preg_replace( '~[ \t\n]*,[ \t\n]*~', ", {$to_prepend}", trim( $selector, " \t\n" ) ); - } - $new_selectors = array(); $selectors = static::split_selector_list( $selector ); foreach ( $selectors as $sel ) { @@ -1781,119 +1735,30 @@ protected static function prepend_to_selector( $selector, $to_prepend ) { */ protected static function split_selector_list( $selector ): array { if ( ! str_contains( $selector, ',' ) ) { - // See note on trimming CSS whitespace in main loop. return array( trim( $selector, " \t\n" ) ); } - $selectors = array(); - $selector_length = strlen( $selector ); - $parentheses_depth = 0; - $at = 0; - $was_at = 0; - - while ( $at < $selector_length ) { - $next_at = $at + strcspn( $selector, '/,\'"()<-\\', $at ); - if ( $next_at >= $selector_length ) { - break; - } - - $next_cp = $selector[ $next_at ]; - - // Escaped syntax characters do not act as delimiters. - if ( '\\' === $next_cp ) { - $at = min( $next_at + 2, $selector_length ); - continue; - } - - /* - * Start of a parenthesized expression, which maintains a stack of parentheses. - * For the sake of this function, no selector list will be split inside parentheses. - * Therefore it’s possible to jump ahead until this list completes. - */ - if ( '(' === $next_cp || ')' === $next_cp ) { - $parentheses_depth += '(' === $next_cp ? 1 : -1; - $at = $next_at + 1; - continue; - } + $selectors = WP_CSS_Token_Processor::split_comma_separated_list( $selector ); - // Start of a string, which will be incorporated into the selector in which it’s found. - if ( "'" === $next_cp || '"' === $next_cp ) { - $end_of_string = $next_at + 1; - while ( $end_of_string < $selector_length ) { - $end_of_string += strcspn( $selector, "{$next_cp}\\", $end_of_string ); - if ( $end_of_string >= $selector_length ) { - break; - } - - $end_cp = $selector[ $end_of_string ]; - - // Skip escaped characters. - if ( '\\' === $end_cp ) { - $end_of_string = $end_of_string + 2; - continue; - } - - if ( $next_cp === $end_cp ) { - ++$end_of_string; - break; - } - - ++$end_of_string; - } - - $at = $end_of_string; - continue; - } - - // Start of a comment, which will be incorporated into the selector in which it’s found. - if ( '/' === $next_cp && ( $next_at + 1 ) < $selector_length && '*' === $selector[ $next_at + 1 ] ) { - $comment_end_at = strpos( $selector, '*/', $next_at + 1 ); - $is_terminated = false !== $comment_end_at; - $after_comment = $is_terminated ? $comment_end_at + 2 : strlen( $selector ); - $at = $after_comment; - continue; - } - - // Start of a CDO or CDC, which will be incorporated into the selector in which it’s found. - if ( - ( '<' === $next_cp && 0 === substr_compare( $selector, '', $next_at, 3 ) ) - ) { - $at = $next_at + ( '<' === $next_cp ? 4 : 3 ); - continue; - } - - // Everything else is either a comma token or part of a selector. - if ( ',' === $next_cp && 0 === $parentheses_depth ) { - /** - * Trim each selector so that downstream code doesn’t see whitespace - * as the first character in a selector and get confused. - * - * There is inconsistency in this because comments and other syntax - * are included which are also not part of the selector itself, but - * a tradeoff is made between removing common syntax which carries - * no meaning and rarer syntax which leaves auxiliary information. - * - * > A newline, U+0009 CHARACTER TABULATION, or U+0020 SPACE. - * > Note that U+000D CARRIAGE RETURN and U+000C FORM FEED are - * > not included in this definition, as they are converted - * > to U+000A LINE FEED during preprocessing. - * - * @link https://www.w3.org/TR/css-syntax/#whitespace - * @link https://www.w3.org/TR/css-syntax/#newline - */ - $selectors[] = trim( substr( $selector, $was_at, $next_at - $was_at ), " \t\n" ); - $at = $next_at + 1; - $was_at = $at; - continue; - } - - $at = $next_at + 1; + // A comma at the end of the list does not start another selector. + if ( '' === end( $selectors ) ) { + array_pop( $selectors ); } - if ( $was_at < $selector_length ) { - // See note on trimming CSS whitespace in main loop. - $selectors[] = trim( substr( $selector, $was_at ), " \t\n" ); + /* + * Trim CSS whitespace so that callers do not see it as the first + * character of a selector. Comments stay in the selector they follow. + * + * > A newline, U+0009 CHARACTER TABULATION, or U+0020 SPACE. + * > Note that U+000D CARRIAGE RETURN and U+000C FORM FEED are + * > not included in this definition, as they are converted + * > to U+000A LINE FEED during preprocessing. + * + * @link https://www.w3.org/TR/css-syntax/#whitespace + * @link https://www.w3.org/TR/css-syntax/#newline + */ + foreach ( $selectors as $i => $item ) { + $selectors[ $i ] = trim( $item, " \t\n" ); } return $selectors; @@ -6031,7 +5896,9 @@ protected static function get_block_element_selectors( $root_selector ) { * If the block selector is compounded, will append the element to each * individual block selector. */ - $block_selectors = explode( ',', $root_selector ); + $block_selectors = str_contains( $root_selector, ',' ) + ? WP_CSS_Token_Processor::split_comma_separated_list( $root_selector ) + : array( $root_selector ); $element_selectors = array(); foreach ( static::ELEMENTS as $el_name => $el_selector ) { $element_selector = array(); 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 4463542c984b1..2e7ae2dfd9622 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 @@ -860,6 +860,75 @@ public static function parse_comma_separated_list( string $css ): array { return $groups; } + /** + * 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. * @@ -1131,11 +1200,14 @@ private function consume_ident_like(): bool { // Consume an ident sequence, and let string be the result. $ident_start = $this->at; $this->consume_ident_sequence(); - $string = self::decode_range( $this->css, $ident_start, $this->at - $ident_start ); // If string's value is an ASCII case-insensitive match for "url", // and the next input code point is U+0028 LEFT PARENTHESIS ((). - if ( 0 === strcasecmp( $string, 'url' ) && $this->at < $this->length && '(' === $this->css[ $this->at ] ) { + if ( + $this->at < $this->length && + '(' === $this->css[ $this->at ] && + 0 === strcasecmp( self::decode_range( $this->css, $ident_start, $this->at - $ident_start ), 'url' ) + ) { // Consume it. ++$this->at; @@ -1461,6 +1533,11 @@ private function consume_ident_start_codepoint( $at ): int { return 1; } + // Other ASCII code points do not start an identifier. + if ( $this->css[ $at ] < "\x80" ) { + return 0; + } + $new_at = $at; $invalid_length = 0; if ( 1 !== _wp_scan_utf8( $this->css, $new_at, $invalid_length, null, 1 ) ) { diff --git a/src/wp-includes/style-engine/class-wp-style-engine-css-rule.php b/src/wp-includes/style-engine/class-wp-style-engine-css-rule.php index a483911335ad9..0b9dd4f017e98 100644 --- a/src/wp-includes/style-engine/class-wp-style-engine-css-rule.php +++ b/src/wp-includes/style-engine/class-wp-style-engine-css-rule.php @@ -177,9 +177,10 @@ public function get_css( $should_prettify = false, $indent_count = 0 ) { $nested_declarations_indent = $should_prettify ? $indent_count + 2 : 0; $suffix = $should_prettify ? "\n" : ''; $spacer = $should_prettify ? ' ' : ''; - // Trims any multiple selectors strings. - $selector = $should_prettify ? implode( ',', array_map( 'trim', explode( ',', $this->get_selector() ) ) ) : $this->get_selector(); - $selector = $should_prettify ? str_replace( array( ',' ), ",\n", $selector ) : $selector; + // Puts each selector of a selector list on its own line. + $selector = $should_prettify + ? implode( ",\n", array_map( 'trim', WP_CSS_Token_Processor::split_comma_separated_list( $this->get_selector() ) ) ) + : $this->get_selector(); $rules_group = $this->get_rules_group(); $has_rules_group = ! empty( $rules_group ); $css_declarations = $this->declarations->get_declarations_string( $should_prettify, $has_rules_group ? $nested_declarations_indent : $declarations_indent ); diff --git a/tests/phpunit/tests/block-supports/duotone.php b/tests/phpunit/tests/block-supports/duotone.php index 0aed3b7e3dcff..dc6e5d6162768 100644 --- a/tests/phpunit/tests/block-supports/duotone.php +++ b/tests/phpunit/tests/block-supports/duotone.php @@ -188,6 +188,58 @@ public function test_css_declarations_are_generated_even_with_empty_block_conten $this->assertNotEmpty( $actual ); } + /** + * Tests that the filter class is added to each selector in a duotone + * selector list, and that commas inside strings and escapes do not + * split the list. + * + * @ticket 65669 + * + * @covers ::render_duotone_support + * + * @dataProvider data_duotone_selector_list + * + * @param string $duotone_selector Duotone selector of the block type. + * @param string $expected Expected selector, with `ID` in place of the filter class. + */ + public function test_duotone_selector_list( $duotone_selector, $expected ) { + register_block_type( + 'test/duotone-selector-list', + array( + 'api_version' => 3, + 'supports' => array( 'filter' => array( 'duotone' => true ) ), + 'selectors' => array( 'filter' => array( 'duotone' => $duotone_selector ) ), + ) + ); + + $block = array( + 'blockName' => 'test/duotone-selector-list', + 'attrs' => array( 'style' => array( 'color' => array( 'duotone' => 'unset' ) ) ), + ); + $wp_block = new WP_Block( $block ); + WP_Duotone::render_duotone_support( '
', $block, $wp_block ); + WP_Duotone::output_block_styles(); + $css = wp_style_engine_get_stylesheet_from_context( 'block-supports', array( 'prettify' => false ) ); + + unregister_block_type( 'test/duotone-selector-list' ); + + $rule = str_replace( 'ID', 'wp-duotone-unset-[0-9]+', preg_quote( $expected . '{filter:unset;}', '/' ) ); + $this->assertMatchesRegularExpression( "/(?:^|}){$rule}/", $css ); + } + + /** + * Data provider for test_duotone_selector_list(). + * + * @return array