Changeset 58896
- Timestamp:
- 08/14/2024 06:55:26 PM (2 months ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-theme-json.php
r58890 r58896 1449 1449 $processed_css = ''; 1450 1450 1451 if ( empty( $css ) ) { 1452 return $processed_css; 1453 } 1454 1451 1455 // Split CSS nested rules. 1452 1456 $parts = explode( '&', $css ); 1453 1457 foreach ( $parts as $part ) { 1458 if ( empty( $part ) ) { 1459 continue; 1460 } 1454 1461 $is_root_css = ( ! str_contains( $part, '{' ) ); 1455 1462 if ( $is_root_css ) { … … 1464 1471 $nested_selector = $part[0]; 1465 1472 $css_value = $part[1]; 1466 $part_selector = str_starts_with( $nested_selector, ' ' ) 1473 1474 /* 1475 * Handle pseudo elements such as ::before, ::after etc. Regex will also 1476 * capture any leading combinator such as >, +, or ~, as well as spaces. 1477 * This allows pseudo elements as descendants e.g. `.parent ::before`. 1478 */ 1479 $matches = array(); 1480 $has_pseudo_element = preg_match( '/([>+~\s]*::[a-zA-Z-]+)/', $nested_selector, $matches ); 1481 $pseudo_part = $has_pseudo_element ? $matches[1] : ''; 1482 $nested_selector = $has_pseudo_element ? str_replace( $pseudo_part, '', $nested_selector ) : $nested_selector; 1483 1484 // Finalize selector and re-append pseudo element if required. 1485 $part_selector = str_starts_with( $nested_selector, ' ' ) 1467 1486 ? static::scope_selector( $selector, $nested_selector ) 1468 1487 : static::append_to_selector( $selector, $nested_selector ); 1469 $final_selector = ":root :where($part_selector)"; 1470 $processed_css .= $final_selector . '{' . trim( $css_value ) . '}';} 1488 $final_selector = ":root :where($part_selector)$pseudo_part"; 1489 1490 $processed_css .= $final_selector . '{' . trim( $css_value ) . '}'; 1491 } 1471 1492 } 1472 1493 return $processed_css; -
trunk/tests/phpunit/tests/theme/wpThemeJson.php
r58890 r58896 5244 5244 /** 5245 5245 * @ticket 61165 5246 * @ticket 61769 5246 5247 * 5247 5248 * @dataProvider data_process_blocks_custom_css … … 5271 5272 return array( 5272 5273 // Simple CSS without any nested selectors. 5274 'empty css' => array( 5275 'input' => array( 5276 'selector' => '.foo', 5277 'css' => '', 5278 ), 5279 'expected' => '', 5280 ), 5273 5281 'no nested selectors' => array( 5274 5282 'input' => array( … … 5286 5294 'expected' => ':root :where(.foo){color: red; margin: auto;}:root :where(.foo.one){color: blue;}:root :where(.foo .two){color: green;}', 5287 5295 ), 5296 'no root styles' => array( 5297 'input' => array( 5298 'selector' => '.foo', 5299 'css' => '&::before{color: red;}', 5300 ), 5301 'expected' => ':root :where(.foo)::before{color: red;}', 5302 ), 5288 5303 // CSS with pseudo elements. 5289 5304 'with pseudo elements' => array( … … 5292 5307 'css' => 'color: red; margin: auto; &::before{color: blue;} & ::before{color: green;} &.one::before{color: yellow;} & .two::before{color: purple;}', 5293 5308 ), 5294 '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;}',5309 '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;}', 5295 5310 ), 5296 5311 // CSS with multiple root selectors. … … 5300 5315 '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;}', 5301 5316 ), 5302 '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 ::before, .bar::before){color: yellow;}:root :where(.foo ::before, .bar ::before){color: purple;}:root :where(.foo.three::before, .bar.three::before){color: orange;}:root :where(.foo .four::before, .bar .four::before){color: skyblue;}',5317 '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;}', 5303 5318 ), 5304 5319 );
Note: See TracChangeset
for help on using the changeset viewer.