Skip to content

Commit 3f456ac

Browse files
committed
Block Style Variations: Simplify block style variation selector regex
The existing regex's first capture group is effectively dead. It only matched a literal `:(...)`, which isn't valid selector syntax (functional pseudo-classes like `:is()` and `:where()` always have a name between the colon and the parenthesis), so `$matches[1]` is always empty. The second group did all the work, so dropping the dead group gives identical output and is much easier to understand. Props aaronrobertshaw, andrewserong, ramonopoly, wildworks. Fixes #65588. git-svn-id: https://develop.svn.wordpress.org/trunk@62700 602fd350-edb4-49c9-b593-d223f7449a82
1 parent eb88149 commit 3f456ac

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

src/wp-includes/class-wp-theme-json.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5662,11 +5662,22 @@ protected static function get_block_style_variation_selector( $variation_name, $
56625662
$selector_parts = static::split_selector_list( $block_selector );
56635663
$result = array();
56645664

5665+
/*
5666+
* Append the variation class to each selector's ancestor: the first
5667+
* run of characters before any combinator (whitespace) or pseudo-class
5668+
* (`:`). Only the first match is replaced.
5669+
*
5670+
* Examples ("custom" variation):
5671+
* - `.wp-block` => `.wp-block.is-style-custom`
5672+
* - `.wp-block .inner` => `.wp-block.is-style-custom .inner`
5673+
* - `.wp-block:where(.a .b)` => `.wp-block.is-style-custom:where(.a .b)`
5674+
* - `:where(.outer .inner)` => `:where(.outer.is-style-custom .inner)`
5675+
*/
56655676
foreach ( $selector_parts as $part ) {
56665677
$result[] = preg_replace_callback(
5667-
'/((?::\([^)]+\))?\s*)([^\s:]+)/',
5678+
'/[^\s:]+/',
56685679
function ( $matches ) use ( $variation_class ) {
5669-
return $matches[1] . $matches[2] . $variation_class;
5680+
return $matches[0] . $variation_class;
56705681
},
56715682
$part,
56725683
$limit

0 commit comments

Comments
 (0)