diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index af00baee62d72..d80baf80f6800 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -2073,62 +2073,489 @@ public function get_stylesheet( $types = array( 'variables', 'styles', 'presets' /** * Processes the CSS, to apply nesting. * + * The CSS is the contents of a style rule for the block, as in CSS + * Nesting: declarations apply to the block, and nested rules apply + * to elements related to it. Every rule is flattened into a + * `:root :where()` selector, so it has 0-1-0 specificity. + * + * - Declarations at the top level apply to `$selector`. + * - `&` in a nested selector stands for the selectors of the rule + * around it. A nested selector without `&` selects descendants, + * as if it started with `& `. + * - The first rule keeps its legacy meaning: when it starts with a + * class, ID, attribute, pseudo-class or pseudo-element, the block + * selector is prepended without a space, so `.is-style-x { … }` + * selects `.block.is-style-x`. + * - In a selector list whose first selector is joined to the block + * selector without a space, as in `&.a, .rtl .b`, the selectors + * without `&` stay as written. + * - A pseudo-element moves after `:where()`, which cannot hold one. + * - `@media`, `@supports`, `@container` and `@layer` blocks wrap the + * rules made from their contents. Other at-rules are dropped. + * - Text without a property name and a colon is not a declaration, and + * is dropped. So is a rule or declaration still open at the end of + * the CSS. + * + * Example: + * + * color: red; & > a { color: blue; &:hover { color: green; } } + * + * with `$selector` `.foo` produces + * + * :root :where(.foo){color: red;} + * :root :where(.foo > a){color: blue;} + * :root :where(.foo > a:hover){color: green;} + * * @since 6.2.0 * @since 6.6.0 Enforced 0-1-0 specificity for block custom CSS selectors. * @since 7.0.0 Made public for use in custom-css block support. + * @since 7.3.0 Supports nesting at any depth, selector lists, nested selectors + * without `&`, and conditional group rules such as `@media`. + * + * @link https://www.w3.org/TR/css-nesting-1/ * * @param string $css The CSS to process. * @param string $selector The selector to nest. * @return string The processed CSS. */ public static function process_blocks_custom_css( $css, $selector ) { - $processed_css = ''; - if ( empty( $css ) || ! is_string( $css ) ) { - return $processed_css; + return ''; + } + + $tokens = static::get_custom_css_tokens( $css ); + + return static::process_custom_css_contents( + $css, + $tokens, + 0, + count( $tokens['type'] ), + static::split_selector_list( $selector ), + ':root :where(' . trim( $selector ) . ')', + true + ); + } + + /** + * Tokenizes custom CSS and pairs each block opener with its closer. + * + * A token that the end of the input cuts off, such as an unclosed + * string or comment, is removed and `truncated` is set. + * + * @since 7.3.0 + * @ignore + * + * @param string $css Custom CSS. + * @return array { + * Token data, indexed by token position. + * + * @type string[] $type Token types. + * @type int[] $start Byte offsets at which the tokens start. + * @type int[] $end Byte offsets at which the tokens end. + * @type int[] $match For a function, `(`, `[` or `{` token, the position + * of its closer, or -1 if the input ends first. + * @type bool $truncated Whether a token was cut off by the end of the input. + * } + */ + protected static function get_custom_css_tokens( $css ) { + $tokens = array( + 'type' => array(), + 'start' => array(), + 'end' => array(), + 'match' => array(), + 'truncated' => false, + ); + + // Positions of the open blocks, innermost last, and the closer each expects. + $open = array(); + $closers = array(); + + $processor = new WP_CSS_Token_Processor( $css ); + for ( $i = 0; $processor->next_token(); $i++ ) { + $type = $processor->get_token_type(); + $start = $processor->get_token_start(); + + $tokens['type'][ $i ] = $type; + $tokens['start'][ $i ] = $start; + $tokens['end'][ $i ] = $start + $processor->get_token_length(); + + switch ( $type ) { + case WP_CSS_Token_Processor::TOKEN_FUNCTION: + case WP_CSS_Token_Processor::TOKEN_LEFT_PAREN: + $open[] = $i; + $closers[] = WP_CSS_Token_Processor::TOKEN_RIGHT_PAREN; + break; + + case WP_CSS_Token_Processor::TOKEN_LEFT_BRACKET: + $open[] = $i; + $closers[] = WP_CSS_Token_Processor::TOKEN_RIGHT_BRACKET; + break; + + case WP_CSS_Token_Processor::TOKEN_LEFT_BRACE: + $open[] = $i; + $closers[] = WP_CSS_Token_Processor::TOKEN_RIGHT_BRACE; + break; + + case WP_CSS_Token_Processor::TOKEN_COMMENT: + case WP_CSS_Token_Processor::TOKEN_STRING: + case WP_CSS_Token_Processor::TOKEN_URL: + case WP_CSS_Token_Processor::TOKEN_BAD_URL: + if ( ! $processor->get_token()->is_terminated() ) { + array_pop( $tokens['type'] ); + array_pop( $tokens['start'] ); + array_pop( $tokens['end'] ); + $tokens['truncated'] = true; + } + break; + + default: + // A closer ends a block only when it matches the innermost open block. + if ( array() !== $closers && end( $closers ) === $type ) { + $tokens['match'][ array_pop( $open ) ] = $i; + array_pop( $closers ); + } + } } - // Split CSS nested rules. - $parts = explode( '&', $css ); - foreach ( $parts as $part ) { - if ( empty( $part ) ) { + foreach ( $open as $i ) { + $tokens['match'][ $i ] = -1; + } + + return $tokens; + } + + /** + * Processes the declarations and rules in a range of custom CSS tokens. + * + * @since 7.3.0 + * @ignore + * + * @param string $css Custom CSS. + * @param array $tokens Token data from get_custom_css_tokens(). + * @param int $at Position of the first token. + * @param int $end Position after the last token. + * @param string[] $parents Selectors that `&` stands for. + * @param string $declarations_selector Selector of the rule that holds the declarations. + * @param bool $is_top_level Whether the range is the whole CSS. + * @return string The processed CSS. + */ + protected static function process_custom_css_contents( $css, $tokens, $at, $end, $parents, $declarations_selector, $is_top_level ) { + $processed_css = ''; + $is_first_rule = $is_top_level; + + // Byte range of consecutive declarations, which share one rule. + $declarations_start = null; + $declarations_end = null; + + while ( $at < $end ) { + $type = $tokens['type'][ $at ]; + if ( + WP_CSS_Token_Processor::TOKEN_WHITESPACE === $type || + WP_CSS_Token_Processor::TOKEN_COMMENT === $type || + WP_CSS_Token_Processor::TOKEN_SEMICOLON === $type || + WP_CSS_Token_Processor::TOKEN_CDO === $type || + WP_CSS_Token_Processor::TOKEN_CDC === $type + ) { + ++$at; continue; } - $is_root_css = ( ! str_contains( $part, '{' ) ); - if ( $is_root_css ) { - // If the part doesn't contain braces, it applies to the root level. - $processed_css .= ':root :where(' . trim( $selector ) . '){' . trim( $part ) . '}'; - } else { - // If the part contains braces, it's a nested CSS rule. - $part = explode( '{', str_replace( '}', '', $part ) ); - if ( count( $part ) !== 2 ) { - continue; + + $item_start = $at; + $is_at_rule = WP_CSS_Token_Processor::TOKEN_AT_KEYWORD === $type; + $is_custom_property = ( + WP_CSS_Token_Processor::TOKEN_IDENT === $type && + 0 === substr_compare( $css, '--', $tokens['start'][ $at ], 2 ) + ); + + /* + * An item ends at a `;` or, unless it is a custom property, at the `{` of its block. + * A closer outside the item's own blocks makes the item invalid, and a `}` also ends it. + */ + $brace = null; + $is_closed = true; + $is_valid = true; + while ( $at < $end ) { + $type = $tokens['type'][ $at ]; + if ( WP_CSS_Token_Processor::TOKEN_SEMICOLON === $type ) { + break; + } + if ( WP_CSS_Token_Processor::TOKEN_RIGHT_BRACE === $type ) { + $is_valid = false; + break; + } + if ( WP_CSS_Token_Processor::TOKEN_RIGHT_PAREN === $type || WP_CSS_Token_Processor::TOKEN_RIGHT_BRACKET === $type ) { + $is_valid = false; + } + if ( WP_CSS_Token_Processor::TOKEN_LEFT_BRACE === $type && ! $is_custom_property ) { + $brace = $at; + break; + } + if ( isset( $tokens['match'][ $at ] ) ) { + if ( -1 === $tokens['match'][ $at ] ) { + $is_closed = false; + $at = $end; + break; + } + $at = $tokens['match'][ $at ]; } - $nested_selector = $part[0]; - $css_value = $part[1]; + ++$at; + } - /* - * Handle pseudo elements such as ::before, ::after etc. Regex will also - * capture any leading combinator such as >, +, or ~, as well as spaces. - * This allows pseudo elements as descendants e.g. `.parent ::before`. - */ - $matches = array(); - $has_pseudo_element = preg_match( '/([>+~\s]*::[a-zA-Z-]+)/', $nested_selector, $matches ); - $pseudo_part = $has_pseudo_element ? $matches[1] : ''; - $nested_selector = $has_pseudo_element ? str_replace( $pseudo_part, '', $nested_selector ) : $nested_selector; + if ( ! $is_closed || ( $tokens['truncated'] && $at === $end && count( $tokens['type'] ) === $end ) ) { + break; + } - // Finalize selector and re-append pseudo element if required. - $part_selector = str_starts_with( $nested_selector, ' ' ) - ? static::scope_selector( $selector, $nested_selector ) - : static::append_to_selector( $selector, $nested_selector ); - $final_selector = ":root :where($part_selector)$pseudo_part"; + if ( null === $brace ) { + // A declaration starts with a name and a colon. + $colon = $item_start + 1; + while ( $colon < $at && ( WP_CSS_Token_Processor::TOKEN_WHITESPACE === $tokens['type'][ $colon ] || WP_CSS_Token_Processor::TOKEN_COMMENT === $tokens['type'][ $colon ] ) ) { + ++$colon; + } + $is_declaration = ( + $is_valid && + WP_CSS_Token_Processor::TOKEN_IDENT === $tokens['type'][ $item_start ] && + $colon < $at && + WP_CSS_Token_Processor::TOKEN_COLON === $tokens['type'][ $colon ] + ); - $processed_css .= $final_selector . '{' . trim( $css_value ) . '}'; + if ( $is_declaration ) { + $last = $at - 1; + while ( WP_CSS_Token_Processor::TOKEN_WHITESPACE === $tokens['type'][ $last ] || WP_CSS_Token_Processor::TOKEN_COMMENT === $tokens['type'][ $last ] ) { + --$last; + } + $declarations_start = $declarations_start ?? $tokens['start'][ $item_start ]; + $declarations_end = $at < $end ? $tokens['end'][ $at ] : $tokens['end'][ $last ]; + } elseif ( null !== $declarations_start ) { + $processed_css .= $declarations_selector . '{' . substr( $css, $declarations_start, $declarations_end - $declarations_start ) . '}'; + $declarations_start = null; + } + ++$at; + continue; } + + $close = $tokens['match'][ $brace ]; + if ( -1 === $close ) { + break; + } + + if ( null !== $declarations_start ) { + $processed_css .= $declarations_selector . '{' . substr( $css, $declarations_start, $declarations_end - $declarations_start ) . '}'; + $declarations_start = null; + } + + if ( ! $is_valid ) { + $is_first_rule = false; + $at = $close + 1; + continue; + } + + if ( $is_at_rule ) { + $name = strtolower( WP_CSS_Token_Processor::decode_range( $css, $tokens['start'][ $item_start ] + 1, $tokens['end'][ $item_start ] - $tokens['start'][ $item_start ] - 1 ) ); + if ( in_array( $name, array( 'media', 'supports', 'container', 'layer' ), true ) ) { + $inner = static::process_custom_css_contents( $css, $tokens, $brace + 1, $close, $parents, $declarations_selector, false ); + if ( '' !== $inner ) { + $processed_css .= trim( substr( $css, $tokens['start'][ $item_start ], $tokens['start'][ $brace ] - $tokens['start'][ $item_start ] ) ) . '{' . $inner . '}'; + } + } + } else { + list( $rule_selector, $rule_parents ) = static::resolve_custom_css_selector( $css, $tokens, $item_start, $brace, $parents, $is_first_rule ); + + $processed_css .= static::process_custom_css_contents( $css, $tokens, $brace + 1, $close, $rule_parents, $rule_selector, false ); + } + + $is_first_rule = false; + $at = $close + 1; } + + if ( null !== $declarations_start ) { + $processed_css .= $declarations_selector . '{' . substr( $css, $declarations_start, $declarations_end - $declarations_start ) . '}'; + } + return $processed_css; } + /** + * Resolves a nested selector list against the selectors around it. + * + * @since 7.3.0 + * @ignore + * + * @param string $css Custom CSS. + * @param array $tokens Token data from get_custom_css_tokens(). + * @param int $at Position of the first token of the selector list. + * @param int $end Position of the `{` after it. + * @param string[] $parents Selectors that `&` stands for. + * @param bool $is_first_rule Whether the rule is the first one in the CSS. + * @return array { + * @type string $0 Selector for the rule, in `:root :where()`. + * @type string[] $1 Resolved selectors, for the rules nested in this one. + * } + */ + protected static function resolve_custom_css_selector( $css, $tokens, $at, $end, $parents, $is_first_rule ) { + $selectors = array(); + $resolved = array(); + $is_appended = false; + + // A space before the first rule selects descendants. + $has_leading_space = $at > 0 && WP_CSS_Token_Processor::TOKEN_WHITESPACE === $tokens['type'][ $at - 1 ]; + + while ( $at < $end ) { + // Collect one selector, up to a top-level comma. + $selector_tokens = array(); + $nested_closers = array(); + for ( ; $at < $end; $at++ ) { + if ( array() === $nested_closers && WP_CSS_Token_Processor::TOKEN_COMMA === $tokens['type'][ $at ] ) { + break; + } + if ( isset( $tokens['match'][ $at ] ) ) { + $nested_closers[] = $tokens['match'][ $at ]; + } elseif ( array() !== $nested_closers && end( $nested_closers ) === $at ) { + array_pop( $nested_closers ); + } + $selector_tokens[] = $at; + } + ++$at; + + while ( array() !== $selector_tokens && WP_CSS_Token_Processor::TOKEN_WHITESPACE === $tokens['type'][ end( $selector_tokens ) ] ) { + array_pop( $selector_tokens ); + } + while ( array() !== $selector_tokens && WP_CSS_Token_Processor::TOKEN_WHITESPACE === $tokens['type'][ $selector_tokens[0] ] ) { + array_shift( $selector_tokens ); + } + if ( array() === $selector_tokens ) { + continue; + } + + // Split the selector at each `&`. Whitespace becomes one space. + $pieces = array( '' ); + foreach ( $selector_tokens as $i ) { + $type = $tokens['type'][ $i ]; + $text = substr( $css, $tokens['start'][ $i ], $tokens['end'][ $i ] - $tokens['start'][ $i ] ); + if ( WP_CSS_Token_Processor::TOKEN_DELIM === $type && '&' === $text ) { + $pieces[] = ''; + } else { + $pieces[ count( $pieces ) - 1 ] .= WP_CSS_Token_Processor::TOKEN_WHITESPACE === $type ? ' ' : $text; + } + } + + $is_first_selector = array() === $selectors; + if ( 1 === count( $pieces ) ) { + $first = $selector_tokens[0]; + $first_text = substr( $css, $tokens['start'][ $first ], $tokens['end'][ $first ] - $tokens['start'][ $first ] ); + $is_type_or_combinator_first = ( + WP_CSS_Token_Processor::TOKEN_IDENT === $tokens['type'][ $first ] || + ( WP_CSS_Token_Processor::TOKEN_DELIM === $tokens['type'][ $first ] && str_contains( '*>+~', $first_text ) ) + ); + + if ( ! $is_first_selector && $is_appended ) { + // After an appended first selector, a selector without `&` stays as written. + $pieces = array( $pieces[0] ); + } elseif ( $is_first_rule && $is_first_selector && ! $has_leading_space && ! $is_type_or_combinator_first ) { + $pieces = array( '', $pieces[0] ); + } else { + $pieces = array( '', ' ' . $pieces[0] ); + } + } + + if ( $is_first_selector ) { + $is_appended = '' === $pieces[0] && '' !== $pieces[1] && ' ' !== $pieces[1][0]; + } + + $selectors[] = $pieces; + } + + $bases = array(); + $pseudos = array(); + foreach ( $parents as $parent ) { + foreach ( $selectors as $pieces ) { + $selector = implode( $parent, $pieces ); + $resolved[] = $selector; + + list( $base, $pseudo ) = static::split_custom_css_pseudo_element( $selector ); + + $bases[] = $base; + $pseudos[] = $pseudo; + } + } + + // A pseudo-element cannot be in `:where()`, so each different one needs its own. + if ( count( array_unique( $pseudos ) ) <= 1 ) { + return array( ':root :where(' . implode( ', ', $bases ) . ')' . ( $pseudos[0] ?? '' ), $resolved ); + } + + $rule_selectors = array(); + foreach ( $bases as $i => $base ) { + $rule_selectors[] = ":root :where({$base}){$pseudos[ $i ]}"; + } + + return array( implode( ', ', $rule_selectors ), $resolved ); + } + + /** + * Splits a selector before its pseudo-element. + * + * The pseudo-element part starts at the first top-level `::` and + * includes the whitespace and combinators before it. + * + * Example: + * + * array( '.a', ' > ::before:hover' ) === split_custom_css_pseudo_element( '.a > ::before:hover' ); + * + * @since 7.3.0 + * @ignore + * + * @param string $selector Complex selector. + * @return string[] The selector before the pseudo-element, and the pseudo-element part. + */ + protected static function split_custom_css_pseudo_element( $selector ) { + if ( ! str_contains( $selector, '::' ) ) { + return array( $selector, '' ); + } + + // Byte offsets of the top-level tokens, and whether each is a colon or a combinator. + $starts = array(); + $is_colon = array(); + $is_combinator = array(); + + $processor = new WP_CSS_Token_Processor( $selector ); + $depth = 0; + $split_at = null; + while ( null === $split_at && $processor->next_token() ) { + $type = $processor->get_token_type(); + + if ( 0 === $depth ) { + $start = $processor->get_token_start(); + $starts[] = $start; + $is_colon[] = WP_CSS_Token_Processor::TOKEN_COLON === $type; + $is_combinator[] = ( + WP_CSS_Token_Processor::TOKEN_WHITESPACE === $type || + ( WP_CSS_Token_Processor::TOKEN_DELIM === $type && str_contains( '>+~', $selector[ $start ] ) ) + ); + + $i = count( $starts ) - 1; + if ( $i > 0 && $is_colon[ $i ] && $is_colon[ $i - 1 ] ) { + $split_at = $i - 1; + while ( $split_at > 0 && $is_combinator[ $split_at - 1 ] ) { + --$split_at; + } + } + } + + 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; + } elseif ( $depth > 0 && ( WP_CSS_Token_Processor::TOKEN_RIGHT_PAREN === $type || WP_CSS_Token_Processor::TOKEN_RIGHT_BRACKET === $type ) ) { + --$depth; + } + } + + if ( null === $split_at ) { + return array( $selector, '' ); + } + + $split_at = $starts[ $split_at ]; + return array( substr( $selector, 0, $split_at ), substr( $selector, $split_at ) ); + } + /** * Returns the global styles custom CSS. * diff --git a/tests/phpunit/tests/theme/wpThemeJson.php b/tests/phpunit/tests/theme/wpThemeJson.php index 12feba3b3ab4f..03cb9093567d7 100644 --- a/tests/phpunit/tests/theme/wpThemeJson.php +++ b/tests/phpunit/tests/theme/wpThemeJson.php @@ -8592,28 +8592,28 @@ public function test_process_blocks_custom_css( $input, $expected ) { public static function data_process_blocks_custom_css() { return array( // Simple CSS without any nested selectors. - 'empty css' => array( + 'empty css' => array( 'input' => array( 'selector' => '.foo', 'css' => '', ), 'expected' => '', ), - 'css as an object' => array( + 'css as an object' => array( 'input' => array( 'selector' => '.foo', 'css' => array( 'color' => 'red' ), ), 'expected' => '', ), - 'css as a list of strings' => array( + 'css as a list of strings' => array( 'input' => array( 'selector' => '.foo', 'css' => array( 'color: red;' ), ), 'expected' => '', ), - 'no nested selectors' => array( + 'no nested selectors' => array( 'input' => array( 'selector' => '.foo', 'css' => 'color: red; margin: auto;', @@ -8621,14 +8621,14 @@ public static function data_process_blocks_custom_css() { 'expected' => ':root :where(.foo){color: red; margin: auto;}', ), // CSS with nested selectors. - 'with nested selector' => array( + 'with nested selector' => array( 'input' => array( 'selector' => '.foo', 'css' => 'color: red; margin: auto; &.one{color: blue;} & .two{color: green;}', ), 'expected' => ':root :where(.foo){color: red; margin: auto;}:root :where(.foo.one){color: blue;}:root :where(.foo .two){color: green;}', ), - 'no root styles' => array( + 'no root styles' => array( 'input' => array( 'selector' => '.foo', 'css' => '&::before{color: red;}', @@ -8636,7 +8636,7 @@ public static function data_process_blocks_custom_css() { 'expected' => ':root :where(.foo)::before{color: red;}', ), // CSS with pseudo elements. - 'with pseudo elements' => array( + 'with pseudo elements' => array( 'input' => array( 'selector' => '.foo', 'css' => 'color: red; margin: auto; &::before{color: blue;} & ::before{color: green;} &.one::before{color: yellow;} & .two::before{color: purple;}', @@ -8644,13 +8644,229 @@ public static function data_process_blocks_custom_css() { 'expected' => ':root :where(.foo){color: red; margin: auto;}:root :where(.foo)::before{color: blue;}:root :where(.foo) ::before{color: green;}:root :where(.foo.one)::before{color: yellow;}:root :where(.foo .two)::before{color: purple;}', ), // CSS with multiple root selectors. - 'with multiple root selectors' => array( + 'with multiple root selectors' => array( 'input' => array( 'selector' => '.foo, .bar', 'css' => 'color: red; margin: auto; &.one{color: blue;} & .two{color: green;} &::before{color: yellow;} & ::before{color: purple;} &.three::before{color: orange;} & .four::before{color: skyblue;}', ), 'expected' => ':root :where(.foo, .bar){color: red; margin: auto;}:root :where(.foo.one, .bar.one){color: blue;}:root :where(.foo .two, .bar .two){color: green;}:root :where(.foo, .bar)::before{color: yellow;}:root :where(.foo, .bar) ::before{color: purple;}:root :where(.foo.three, .bar.three)::before{color: orange;}:root :where(.foo .four, .bar .four)::before{color: skyblue;}', ), + // Selector lists in nested rules. + 'with nested selector list' => array( + 'input' => array( + 'selector' => '.foo', + 'css' => '&.one, & .two {color: red;}', + ), + 'expected' => ':root :where(.foo.one, .foo .two){color: red;}', + ), + // CSS with nested selectors without `&`. + 'nested without &' => array( + 'input' => array( + 'selector' => '.foo', + 'css' => 'color: red; img{color: blue;}', + ), + 'expected' => ':root :where(.foo){color: red;}:root :where(.foo img){color: blue;}', + ), + 'only nested without &' => array( + 'input' => array( + 'selector' => '.foo', + 'css' => 'img{color: blue;}', + ), + 'expected' => ':root :where(.foo img){color: blue;}', + ), + 'mixed nested selectors' => array( + 'input' => array( + 'selector' => '.foo', + 'css' => 'color: red; &.one{color: blue;} .two{color: green;} > img{color: yellow;} ::before{color: purple;}', + ), + 'expected' => ':root :where(.foo){color: red;}:root :where(.foo.one){color: blue;}:root :where(.foo .two){color: green;}:root :where(.foo > img){color: yellow;}:root :where(.foo) ::before{color: purple;}', + ), + 'nested without & inside &' => array( + 'input' => array( + 'selector' => '.foo', + 'css' => '& { color: red; img { color: blue; } }', + ), + 'expected' => ':root :where(.foo){color: red;}:root :where(.foo img){color: blue;}', + ), + 'nested without & multi root' => array( + 'input' => array( + 'selector' => '.foo, .bar', + 'css' => 'color: red; img{color: blue;}', + ), + 'expected' => ':root :where(.foo, .bar){color: red;}:root :where(.foo img, .bar img){color: blue;}', + ), + // A leading rule without `&` keeps appending to the block selector. + 'leading pseudo-class' => array( + 'input' => array( + 'selector' => '.foo', + 'css' => ':hover{color: red;}', + ), + 'expected' => ':root :where(.foo:hover){color: red;}', + ), + 'leading pseudo-element' => array( + 'input' => array( + 'selector' => '.foo', + 'css' => '::before{color: red;}', + ), + 'expected' => ':root :where(.foo)::before{color: red;}', + ), + 'leading class' => array( + 'input' => array( + 'selector' => '.foo', + 'css' => '.is-style-x{color: red;}', + ), + 'expected' => ':root :where(.foo.is-style-x){color: red;}', + ), + 'leading id' => array( + 'input' => array( + 'selector' => '.foo', + 'css' => '#hero{color: red;}', + ), + 'expected' => ':root :where(.foo#hero){color: red;}', + ), + 'leading :is()' => array( + 'input' => array( + 'selector' => '.foo', + 'css' => ':is(.a, .b){color: red;}', + ), + 'expected' => ':root :where(.foo:is(.a, .b)){color: red;}', + ), + 'twenty twenty-four calendar' => array( + 'input' => array( + 'selector' => '.wp-block-calendar', + 'css' => '.wp-block-calendar table:where(:not(.has-text-color)) th{background-color:var(--wp--preset--color--contrast-2);color:var(--wp--preset--color--base);border-color:var(--wp--preset--color--contrast-2)} & table:where(:not(.has-text-color)) td{border-color:var(--wp--preset--color--contrast-2)}', + ), + 'expected' => ':root :where(.wp-block-calendar.wp-block-calendar table:where(:not(.has-text-color)) th){background-color:var(--wp--preset--color--contrast-2);color:var(--wp--preset--color--base);border-color:var(--wp--preset--color--contrast-2)}:root :where(.wp-block-calendar table:where(:not(.has-text-color)) td){border-color:var(--wp--preset--color--contrast-2)}', + ), + 'leading selector list' => array( + 'input' => array( + 'selector' => '.foo', + 'css' => '.a, .b{color: red;}', + ), + 'expected' => ':root :where(.foo.a, .b){color: red;}', + ), + 'selector after an appended selector stays as written' => array( + 'input' => array( + 'selector' => '.foo', + 'css' => '&.a, .rtl .b{color: red;}', + ), + 'expected' => ':root :where(.foo.a, .rtl .b){color: red;}', + ), + 'selector after a descendant selector is nested' => array( + 'input' => array( + 'selector' => '.foo', + 'css' => '& .a, .b{color: red;}', + ), + 'expected' => ':root :where(.foo .a, .foo .b){color: red;}', + ), + 'different pseudo-elements in a list' => array( + 'input' => array( + 'selector' => '.foo', + 'css' => '&::before, &::after{content: "";}', + ), + 'expected' => ':root :where(.foo)::before, :root :where(.foo)::after{content: "";}', + ), + // Nesting deeper than one level. + 'nested rule in a nested rule' => array( + 'input' => array( + 'selector' => '.foo', + 'css' => "&.is-layout-flex > *:has(+ *) {\n\tdisplay: flex;\n\tcolumn-gap: 5px;\n\n\t/* Second level */\n\t&::after {\n\t\tcontent: '•'\n\t}\n}", + ), + 'expected' => ":root :where(.foo.is-layout-flex > *:has(+ *)){display: flex;\n\tcolumn-gap: 5px;}:root :where(.foo.is-layout-flex > *:has(+ *))::after{content: '•'}", + ), + '& after a compound' => array( + 'input' => array( + 'selector' => '.foo', + 'css' => '.dark &{color: red;}', + ), + 'expected' => ':root :where(.dark .foo){color: red;}', + ), + 'declarations after a nested rule' => array( + 'input' => array( + 'selector' => '.foo', + 'css' => 'color: red; & a{color: blue;} margin: 0;', + ), + 'expected' => ':root :where(.foo){color: red;}:root :where(.foo a){color: blue;}:root :where(.foo){margin: 0;}', + ), + // Conditional group rules. + 'media query' => array( + 'input' => array( + 'selector' => '.foo', + 'css' => '@media (min-width: 1024px) { & { grid-template-columns: repeat(2, 1fr); } }', + ), + 'expected' => '@media (min-width: 1024px){:root :where(.foo){grid-template-columns: repeat(2, 1fr);}}', + ), + 'declarations in a media query' => array( + 'input' => array( + 'selector' => '.foo', + 'css' => 'color: red; @media (min-width: 600px) { color: blue; & a { color: green; } }', + ), + 'expected' => ':root :where(.foo){color: red;}@media (min-width: 600px){:root :where(.foo){color: blue;}:root :where(.foo a){color: green;}}', + ), + 'media query in a nested rule' => array( + 'input' => array( + 'selector' => '.foo', + 'css' => '& a { color: red; @supports (display: grid) { @media print { color: blue; } } }', + ), + 'expected' => ':root :where(.foo a){color: red;}@supports (display: grid){@media print{:root :where(.foo a){color: blue;}}}', + ), + 'other at-rules are dropped' => array( + 'input' => array( + 'selector' => '.foo', + 'css' => '@import url(a.css); @font-face { font-family: a; } color: red;', + ), + 'expected' => ':root :where(.foo){color: red;}', + ), + // Syntax that holds `&`, `{` or `}` without nesting. + '& and braces in strings' => array( + 'input' => array( + 'selector' => '.foo', + 'css' => 'content: "&{}"; & [title="a&b"]{color: red;}', + ), + 'expected' => ':root :where(.foo){content: "&{}";}:root :where(.foo [title="a&b"]){color: red;}', + ), + '& in a comment' => array( + 'input' => array( + 'selector' => '.foo', + 'css' => 'color: red; /* & { } */ margin: 0;', + ), + 'expected' => ':root :where(.foo){color: red; /* & { } */ margin: 0;}', + ), + 'braces in a custom property' => array( + 'input' => array( + 'selector' => '.foo', + 'css' => '--x: { a: b }; color: red;', + ), + 'expected' => ':root :where(.foo){--x: { a: b }; color: red;}', + ), + 'text that is not a declaration is dropped' => array( + 'input' => array( + 'selector' => '.foo', + 'css' => 'color red; margin: 0;', + ), + 'expected' => ':root :where(.foo){margin: 0;}', + ), + 'stray closing brace ends a declaration' => array( + 'input' => array( + 'selector' => '.foo', + 'css' => 'color: red }; margin: 0; } .a { color: blue; }', + ), + 'expected' => ':root :where(.foo){margin: 0;}:root :where(.foo .a){color: blue;}', + ), + 'unclosed rule is dropped' => array( + 'input' => array( + 'selector' => '.foo', + 'css' => 'color: red; & a { color: blue;', + ), + 'expected' => ':root :where(.foo){color: red;}', + ), + 'unclosed string is dropped' => array( + 'input' => array( + 'selector' => '.foo', + 'css' => 'color: red; content: "a', + ), + 'expected' => ':root :where(.foo){color: red;}', + ), ); }