WP_Theme_JSON::split_selector_list( string $selector ): string[]
- Since
- 7.1.0
- Source
wp-includes/class-wp-theme-json.php:1612
Description
While selectors are joined by commas, not all commas separate top-level selectors.
This method only separates top-level selectors, so some commas may appear inside strings, nested selectors, and comments. Leading and trailing CSS whitespace is trimmed from the returned list items.
Non-selector content, such as comments, are retained in the list in the same item as the selector content they follow.
Example:
array( '.wp-block' ) === self::split_selector_list( '.wp-block' );
array( '.one', '.two' ) === self::split_selector_list( '.one, .two' );
// Nested selector lists are retained within their containing selector.
array( ':is(.a, .b)', 'c' ) === self::split_selector_list( ':is(.a, .b), .c' );
// Commas within strings do not separate selectors.
$selectors = self::split_selector_list( '[data-label="Save, continue"],.fallback' );
$selectors === array( '[data-label="Save, continue"]', '.fallback' )
array( 'lang(zh, "*-hant")', '.foo' ) === self::split_selector_list( 'lang(zh, "*-hant"), .foo' );
// Identifiers may contain escaped commas.
array( '.foo\,bar', '.baz' ) === self::split_selector_list( '.foo\,bar,.baz' );
// Comments stay with the selector they follow.
array( '.a /* a, the first *\/', '.b' ) === self::split_selector_list( '.a /* a, the first *\/,.b' );Compatibility
- WordPress
- since 7.1.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in 1 of the 5 tracked releases, added in 7.1.0, and compiles on PHP 7.4 through 8.6-dev.
Parameters
$selectorstring- CSS selector list as a string, e.g. '.wp-block .wp-block-paragraph'.
Return value
string[]- List of trimmed selectors parsed from input list.
Performance profile
How much work a call to WP_Theme_JSON::split_selector_list() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Light
- Scaling
- Scales with input
- Instructions
- 6–26
- Plugin surface
- None
- Called by
- 0
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 144.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What one call costs · 2 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Theme_JSON::split_selector_list() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 6–18 | none |
$selector && $at && $selector_length | 22–26 | strcspn() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 144 | 6–26 | 25 | |
| 8.5 | 144 | 6–26 | 25 | |
| 8.4 | 144 | 6–26 | 25 | 24 fewer instructions than PHP 8.3 |
| 8.3 | 168 | 12–35 | 25 | |
| 8.2 | 168 | 12–35 | 25 | |
| 8.1 | 168 | 12–35 | 25 | 2 fewer instructions than PHP 7.4 |
| 7.4 | 170 | 12–35 | 25 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 1
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
Source code
protected static function split_selector_list( $selector ): array { if ( ! str_contains( $selector, ',' ) ) { // See note on trimming CSS whitespace in main loop. return array( trim( $selector, " \t\n" ) ); } $selectors = array(); $selector_length = strlen( $selector ); $parentheses_depth = 0; $at = 0; $was_at = 0; while ( $at < $selector_length ) { $next_at = $at + strcspn( $selector, '/,\'"()<-\\', $at ); if ( $next_at >= $selector_length ) { break; } $next_cp = $selector[ $next_at ]; // Escaped syntax characters do not act as delimiters. if ( '\\' === $next_cp ) { $at = min( $next_at + 2, $selector_length ); continue; } /* * Start of a parenthesized expression, which maintains a stack of parentheses. * For the sake of this function, no selector list will be split inside parentheses. * Therefore it’s possible to jump ahead until this list completes. */ if ( '(' === $next_cp || ')' === $next_cp ) { $parentheses_depth += '(' === $next_cp ? 1 : -1; $at = $next_at + 1; continue; } // Start of a string, which will be incorporated into the selector in which it’s found. if ( "'" === $next_cp || '"' === $next_cp ) { $end_of_string = $next_at + 1; while ( $end_of_string < $selector_length ) { $end_of_string += strcspn( $selector, "{$next_cp}\\", $end_of_string ); if ( $end_of_string >= $selector_length ) { break; } $end_cp = $selector[ $end_of_string ]; // Skip escaped characters. if ( '\\' === $end_cp ) { $end_of_string = $end_of_string + 2; continue; } if ( $next_cp === $end_cp ) { ++$end_of_string; break; } ++$end_of_string; } $at = $end_of_string; continue; } // Start of a comment, which will be incorporated into the selector in which it’s found. if ( '/' === $next_cp && ( $next_at + 1 ) < $selector_length && '*' === $selector[ $next_at + 1 ] ) { $comment_end_at = strpos( $selector, '*/', $next_at + 1 ); $is_terminated = false !== $comment_end_at; $after_comment = $is_terminated ? $comment_end_at + 2 : strlen( $selector ); $at = $after_comment; continue; } // Start of a CDO or CDC, which will be incorporated into the selector in which it’s found. if ( ( '<' === $next_cp && 0 === substr_compare( $selector, '<!--', $next_at, 4 ) ) || ( '-' === $next_cp && 0 === substr_compare( $selector, '-->', $next_at, 3 ) ) ) {Changelog
Introduced in 7.1.0.
Signature, return type and hooks compared across 1 parsed release.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/class-wp-theme-json.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.