Make WordPress Core


Ignore:
Timestamp:
09/04/2024 02:22:16 PM (5 months ago)
Author:
hellofromTonya
Message:

Editor: Fix block custom CSS pseudo element selectors in global styles.

Fixes a regression introduced in [58241] where selectors with pseudo elements are wrapped within :where() causing malformed CSS and the CSS rule(s) not being applied.

When processing custom CSS for blocks, this changeset:

  • Strips the pseudo-elements from the original nested selector, performs the required wrapping in :root :where, then re-appends the pseudo-element selector with its leading combinators if present.
  • Removes empty CSS rules.

It includes the PHP changes.

Reference:

Follow-up to [58241], [56812], [55216].

Reviewed by andrewserong.
Merges [58896] to the 6.6 branch.

Props aaronrobertshaw, wongjn, harlet7, dballari, ramonopoly, andrewserong, aristath, hellofromTonya.
Fixes #61769.

Location:
branches/6.6
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/6.6

  • branches/6.6/src/wp-includes/class-wp-theme-json.php

    r58986 r58987  
    14401440        $processed_css = '';
    14411441
     1442        if ( empty( $css ) ) {
     1443            return $processed_css;
     1444        }
     1445
    14421446        // Split CSS nested rules.
    14431447        $parts = explode( '&', $css );
    14441448        foreach ( $parts as $part ) {
     1449            if ( empty( $part ) ) {
     1450                continue;
     1451            }
    14451452            $is_root_css = ( ! str_contains( $part, '{' ) );
    14461453            if ( $is_root_css ) {
     
    14551462                $nested_selector = $part[0];
    14561463                $css_value       = $part[1];
    1457                 $part_selector   = str_starts_with( $nested_selector, ' ' )
     1464
     1465                /*
     1466                 * Handle pseudo elements such as ::before, ::after etc. Regex will also
     1467                 * capture any leading combinator such as >, +, or ~, as well as spaces.
     1468                 * This allows pseudo elements as descendants e.g. `.parent ::before`.
     1469                 */
     1470                $matches            = array();
     1471                $has_pseudo_element = preg_match( '/([>+~\s]*::[a-zA-Z-]+)/', $nested_selector, $matches );
     1472                $pseudo_part        = $has_pseudo_element ? $matches[1] : '';
     1473                $nested_selector    = $has_pseudo_element ? str_replace( $pseudo_part, '', $nested_selector ) : $nested_selector;
     1474
     1475                // Finalize selector and re-append pseudo element if required.
     1476                $part_selector  = str_starts_with( $nested_selector, ' ' )
    14581477                    ? static::scope_selector( $selector, $nested_selector )
    14591478                    : static::append_to_selector( $selector, $nested_selector );
    1460                 $final_selector  = ":root :where($part_selector)";
    1461                 $processed_css  .= $final_selector . '{' . trim( $css_value ) . '}';}
     1479                $final_selector = ":root :where($part_selector)$pseudo_part";
     1480
     1481                $processed_css .= $final_selector . '{' . trim( $css_value ) . '}';
     1482            }
    14621483        }
    14631484        return $processed_css;
Note: See TracChangeset for help on using the changeset viewer.