Repository navigation
Conversation
consume_ident_sequence() scans ASCII name bytes with strspn() and then asks consume_ident_start_codepoint() about the byte that stopped the scan. For an ASCII byte such as `,` `:` or a space, that method ran _wp_scan_utf8() and utf8_ord() before returning 0. An ASCII byte other than a letter, `_` or NUL never starts an identifier, so the method returns 0 for it before decoding. Tokenizing a 70 KB stylesheet (a generated global stylesheet with the admin-bar and buttons styles, 15,979 tokens) took 6.2 ms before and 4.9 ms after, best of 10 runs on PHP 8.5.
consume_ident_like() decoded every identifier to compare it with "url", and used the result only when the next byte is `(`. Checking that byte first skips the decoding for every identifier that is not followed by `(`, which is most of them. Tokenizing the 70 KB stylesheet from the previous commit took 4.9 ms before and 4.5 ms after, best of 10 runs on PHP 8.5.
Selector lists in theme.json, block supports, duotone and the style engine are split with explode( ',' ) or with scanners that track some nesting. Their callers need the source text of each selector, as written, to prepend or append to it; parse_comma_separated_list() returns tokens, and a caller that only needs the text pays for one WP_CSS_Token per token. The method returns the groups of parse_comma_separated_list() as substrings of the input. When the input has no string, comment, escape or url token, every comma and bracket byte is a token of its own, so a strcspn() scan over those bytes finds the groups without tokenizing. Theme selector lists rarely hold any of those, so the scan handles most calls. On the 348 selector lists that WP_Theme_JSON::split_selector_list() receives while building the twentytwentyfour and twentytwentyfive stylesheets, the scan takes 0.7 us per call; tokenizing every input takes 11 us. 600,000 random inputs over CSS syntax characters gave the same groups as parse_comma_separated_list().
WP_Theme_JSON has a hand-written selector list scanner in
split_selector_list(), a regular expression path in append_to_selector()
and prepend_to_selector(), and explode( ',' ) in
get_block_element_selectors(). The scanner tracks only parentheses, and
explode() splits inside functions and strings:
get_block_element_selectors( '.wp-block-a,.wp-block-b' )['h1']
'.wp-block-a h1,.wp-block-b h1'
get_block_element_selectors( ':is(.wp-block-a,.wp-block-b)' )['h1']
':is(.wp-block-a h1,.wp-block-b) h1'
All three use WP_CSS_Token_Processor::split_comma_separated_list(),
which splits where the CSS tokenizer finds top-level commas.
split_selector_list() keeps its trimming and drops the empty selector
after a trailing comma, as before. The regular expression path in
append_to_selector() and prepend_to_selector() is gone: for the inputs
it handled, the split gives the same result.
Measured on the 348 split_selector_list() and 3,248
prepend_to_selector() calls made while building the twentytwentyfour
and twentytwentyfive stylesheets and block metadata, one PHP 8.5
process per variant, best of 15:
split_selector_list() 0.49 us -> 0.73 us per call
prepend_to_selector() 0.17 us -> 0.27 us per call
Both functions return the same strings as before on all of these
inputs.
wp_split_selector_list() tracks parentheses only, so it splits at
commas inside attribute strings, escapes, comments and brackets:
wp_split_selector_list( 'a[title=x]' ) array( 'a[title=x]' )
wp_split_selector_list( 'a[title="x,y"]' ) array( 'a[title="x', 'y"]' )
It uses WP_CSS_Token_Processor::split_comma_separated_list(), which
splits where the CSS tokenizer finds top-level commas. Each selector
keeps its surrounding whitespace, and a trailing comma still gives a
final empty selector, so callers see the same strings for every input
the parenthesis count split correctly.
wp_get_block_state_element_selectors() checked for a comma before
splitting an element selector. wp_split_selector_list() returns the
selector unchanged when it has no comma, so the check is gone.
WordPress#12610 fixes the
same defect by calling WP_Theme_JSON::split_selector_list() instead
and removing wp_split_selector_list(), which shipped in 7.1. Its test
is included here unchanged.
wp_get_state_selector_ancestor() reads a selector byte by byte and
treats any space as a combinator, including a space inside a comment
or after a backslash. For a comment before the combinator, the
instance class lands inside the comment and the state rule matches
every list item:
.wp-block-list> li .wp-block-list> .wp-states-test:hover
.wp-block-list/* x */> li .wp-block-list/* .wp-states-test */> li:hover
The function walks CSS tokens instead. Whitespace tokens and the >, +
and ~ delimiters at the top level are combinators, comments next to a
combinator join the ancestor, and an escaped space stays in its
identifier. Functions and brackets are skipped as before.
For the selectors in the existing tests and the core block selectors,
the result is unchanged.
WP_Duotone::enqueue_block_css() splits a block's duotone selector with
explode( ',' ) and adds the filter class to each part. A comma inside
an attribute string or an escape puts the class inside the selector:
[data-x="a"] img .wp-duotone-unset-1[data-x="a"] img
[data-x="a,b"] img .wp-duotone-unset-1[data-x="a, .wp-duotone-unset-1b"] img
It uses WP_CSS_Token_Processor::split_comma_separated_list(), which
splits where the CSS tokenizer finds top-level commas. A selector with
no comma is used as is, as before.
WP_Style_Engine_CSS_Rule::get_css() prettifies a selector list with
explode( ',' ) and str_replace( ',', ",\n" ), so it breaks lines
inside functions and attribute strings. A line break inside a string
ends the string, and the browser drops the rule:
[title="x"] [title="x"] {
[title="x, y"] [title="x,
y"] {
It splits the list with WP_CSS_Token_Processor::split_comma_separated_list()
and joins the trimmed selectors with ",\n". Selector lists without
commas inside functions, strings or escapes give the same output.
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
Selector lists in theme.json, block supports, duotone and the style engine are split with explode( ',' ) or with scanners that track some nesting, which split inside strings, escapes, comments or blocks. Their callers need the source text of each selector, as written, to prepend or append to it. The method returns the source text between top-level commas, found by tokenizing the input. Joining the items with commas reproduces the input. A comma at the end of the input produces a final empty item, as the selector grammar rejects a trailing comma and the CSS Syntax 3 algorithm does not show one.
…r-lists split_comma_separated_list() moves to its own branch on the tokenizer branch and tokenizes every input there. The merge takes that version, which has no byte-scan fast path, and its tests, which check the items without parse_comma_separated_list().
…omma-separated-list-text
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Caution
This is a work in progress draft and is not ready for any serious review.
Core splits CSS selector lists in five places, with three different rules:
explode( ',' ), a parenthesis counter, and a hand-written scanner. The first two split inside attribute strings, escapes and comments;explode()also splits inside functions. This branch replaces all of them with one split based on the CSS tokenizer,WP_CSS_Token_Processor::split_comma_separated_list().wp_split_selector_list()a[title="x,y"]a[title="xandy"]WP_Theme_JSON::get_block_element_selectors(),h1:is(.wp-block-a,.wp-block-b):is(.wp-block-a h1,.wp-block-b) h1:is(.wp-block-a,.wp-block-b) h1WP_Duotone::enqueue_block_css()[data-x="a,b"] img.wp-duotone-unset-1[data-x="a, .wp-duotone-unset-1b"] img.wp-duotone-unset-1[data-x="a,b"] imgWP_Style_Engine_CSS_Rule::get_css( true )[title="x, y"]wp_get_state_selector_ancestor().wp-block-list/* x */> li.wp-block-list/* .wp-states-test */> li:hover.wp-block-list/* x */> .wp-states-test:hoverWordPress#12610 (https://core.trac.wordpress.org/ticket/65669) fixes the first row by calling
WP_Theme_JSON::split_selector_list()from the states support and deletingwp_split_selector_list(). That function shipped in 7.1, so this branch keeps it and changes only how it splits. Its test is included unchanged and passes.Design
The splitter is a CSS API method, not a private core function. Theme JSON, block supports, duotone and the style engine all need it, and they share no private home:
WP_Theme_JSONis@access private, andwp_split_selector_list()lives in the states block support. The method returns the source text of the groups thatparse_comma_separated_list()returns, so the grouping rule is defined once, by the tokenizer. Joining the result with commas reproduces the input. It does not trim:wp_split_selector_list()andget_block_element_selectors()keep the whitespace around each selector, and keeping it is what makes their output byte-identical to trunk.Every caller keeps its trunk output for the inputs trunk split correctly:
WP_Theme_JSON::split_selector_list()still trims\t\nand drops the empty selector after a trailing comma.wp_split_selector_list()still returns untrimmed selectors and a final empty selector after a trailing comma.append_to_selector()andprepend_to_selector()lose their regular expression path; the split gives the same strings for the inputs it handled.wp_get_block_state_element_selectors()loses a comma check that duplicated the one inwp_split_selector_list().wp_get_state_selector_ancestor()(r64591) walks tokens instead of bytes. The tokenizer handles it with no API changes: whitespace tokens and>+~delimiters at the top level are combinators, comments next to a combinator join the ancestor, and an escaped space stays in its identifier. It does need its own depth counter for functions and brackets, whichsplit_comma_separated_list()also keeps.Performance
Every selector list is tokenized, which is about 20 times slower than trunk's scanner. Performance work comes after the API is established.
Inputs: the 348
split_selector_list()and 3,248prepend_to_selector()calls made while building the twentytwentyfour and twentytwentyfive stylesheets and block metadata. One PHP 8.5 process per variant, best of 15, opcache off:split_selector_list()prepend_to_selector()For those calls that is about 16.5 ms against 0.7 ms on trunk. Both functions return the same strings as trunk on all of these inputs. The numbers were measured with the tokenizer path forced, before the byte-scan fast path was removed from this branch.
CSS API additions
WP_CSS_Token_Processor::split_comma_separated_list()is proposed in CSS API: Add a text form of the comma-separated list split #109 and merged into this branch: callers need the source text of each selector to prepend or append to it, andparse_comma_separated_list()returns tokens.Limitations
WP_Block::process_block_bindings()splits attribute source selectors withexplode( ',' ), but treats each part as a tag name, so it is left alone.str_contains( $s, ',' ) ? split_comma_separated_list( $s ) : array( $s )to keepexplode()'s single empty item for empty input. The method returns an empty array for empty input, asparse_comma_separated_list()does.Depends on WordPress#14781, #98 and #109.
Trac ticket: TBD
Use of AI Tools
AI assistance: Yes
Tools: Claude Code
Models: Claude Opus 5.5
Used for: implementation, tests and this description.
This pull request is for code review only. Keep other discussion in the Trac ticket. Do not merge. See GitHub Pull Requests for Code Review in the Core Handbook.