Make WordPress Core


Ignore:
Timestamp:
07/13/2026 01:41:59 AM (7 weeks ago)
Author:
ramonopoly
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-theme-json.php

    r62671 r62700  
    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,
Note: See TracChangeset for help on using the changeset viewer.