Repository navigation
Conversation
process_blocks_custom_css() splits the CSS at every `&` and at `{`, so
it handles one level of `&` rules and nothing else. Selector lists,
nesting deeper than one level, rules without `&`, @media, and `&`,
`{` or `}` inside strings, comments and values produce broken rules
or none:
& a { color: red; } :root :where(.foo a){color: red;}
& a { & b { color: red; } } (no rule)
It walks CSS tokens instead, as the contents of a style rule for the
block, following CSS Nesting:
- Declarations apply to the selector around them, at any depth, in
source order.
- `&` stands for the selectors around it, anywhere in a selector. A
nested selector without `&` selects descendants.
- Selector lists resolve each selector; a pseudo-element moves after
`:where()`, and different pseudo-elements get one `:where()` each.
- @media, @supports, @container and @layer blocks wrap the rules made
from their contents. Other at-rules are dropped.
- Text that is not a declaration, a declaration or prelude holding a
stray closer, and anything still open at the end are dropped.
Two trunk behaviors stay because bundled themes use them. The first
rule, when it starts with a class, ID, attribute or pseudo selector, is
joined to the block selector without a space (Twenty Twenty-Four's
Calendar CSS). In a list whose first selector is joined that way, as
in `&.a, .rtl .b`, selectors without `&` stay as written (the Quote
CSS of Ipsum and Twenty Twenty-Four).
Every custom CSS string in the bundled themes' theme.json and style
variations, with a one- and a two-selector block selector, gives the
same rules as before. The output differs only in two ways: no empty
`:root :where(...){}` rule for whitespace before the first `&`, and no
space before the `)` of `:where()` when the CSS has one before `{`.
Building the styles of Ipsum takes 2.2 ms instead of 1.4 ms, and of
Twenty Twenty-Four 1.3 ms instead of 1.1 ms (best of 15, PHP 8.5).
Tokenizing is 15 of the 25 us per custom CSS string.
|
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:
|
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.
WP_Theme_JSON::process_blocks_custom_css()splits block custom CSS at every&and{, so it handles one level of&rules and little else. This branch rebuilds it on the CSS tokenizer and treats the CSS as the contents of a style rule for the block, following CSS Nesting..foo& a { color: red; }:root :where(.foo a){color: red;}& a { & b { color: red; } }:root :where(.foo a){}:root :where(.foo b){color: red;}:root :where(.foo a b){color: red;}color: red; img { color: blue; }:root :where(.foocolor: red; img ){color: blue;}:root :where(.foo){color: red;}:root :where(.foo img){color: blue;}&.a, & .b { color: red; }:root :where(.foo){.a,}:root :where(.foo .b){color: red;}:root :where(.foo.a, .foo .b){color: red;}@media (min-width: 600px) { & { color: red; } }:root :where(.foo@media (min-width: 600px) ){}:root :where(.foo){color: red;}@media (min-width: 600px){:root :where(.foo){color: red;}}Competing work, all in Gutenberg: WordPress/gutenberg#78437 (
@media), WordPress/gutenberg#77602 (deeper nesting), WordPress/gutenberg#86353 (rules without&) and WordPress/gutenberg#70964 (selector lists). Each adds a character scanner or regular expression to the existing split. Issues: WordPress/gutenberg#48989, WordPress/gutenberg#76728, WordPress/gutenberg#81223, WordPress/gutenberg#73217.Semantics
&stands for the selectors around it, anywhere in a selector (.dark &works). A nested selector without&selects descendants, as Global Styles: Support nested custom CSS rules without & WordPress/gutenberg#86353 proposes and Allow CSS nesting in "Additional CSS" WordPress/gutenberg#81223 agreed.:root :where(). A pseudo-element moves after:where(), as on trunk; a list with different pseudo-elements gets one:where()per selector.@media,@supports,@containerand@layerblocks wrap the rules made from their contents, at any depth. Declarations directly in@mediaapply to the selector around it. Other at-rules are dropped.),]or}, and anything still open at the end of the CSS are dropped, so no input produces a}that closes the generated rule early.https://core.trac.wordpress.org/ticket/59158 calls
&the way to target elements inside the block. Two trunk behaviors outside that model stay, because bundled themes depend on them:.is-style-x { … }selects.foo.is-style-x. Twenty Twenty-Four's Calendar CSS starts this way.&stay as written:&.a, .rtl .bgives:root :where(.foo.a, .rtl .b). The Quote CSS in Ipsum and Twenty Twenty-Four uses this to style.rtlpages. Under CSS Nesting,.rtl .bwould select descendants of.fooand those rules would stop matching.Comparison
Every custom CSS string in the bundled themes'
theme.jsonand style variations (49), processed with a one- and a two-selector block selector, gives the same rules as trunk. The output differs only in whitespace inside:where()(trunk keeps the space before{, as in.foo.taxonomy-post_tag )) and in one empty rule trunk emits for whitespace before the first&.The PHP tests of WordPress/gutenberg#86353 (11 cases) are ported unchanged and pass. The test of WordPress/gutenberg#70964 passes with a different expected string: that PR emits
:root :where(.foo.one),:root :where(.foo .two); this branch emits one:where(),:root :where(.foo.one, .foo .two), which matches what trunk emits for the lists it handles. WordPress/gutenberg#78437 and WordPress/gutenberg#77602 have no PHP tests; their examples are tests here. 21 of the 38 test cases fail on trunk.Performance
Building the styles of Ipsum takes 2.2 ms instead of 1.4 ms; Twenty Twenty-Four 1.3 ms instead of 1.1 ms (
get_stylesheet( array( 'styles' ) ), best of 15, PHP 8.5). Per custom CSS string (204 bytes on average) the cost is 25 µs instead of 1.2 µs, of which 15 µs is tokenizing.CSS API
There are no CSS API changes. The walker stays private in
WP_Theme_JSONbecause its rules (legacy first rule, kept selectors, the flattening into:root :where()) are specific to block custom CSS. A general stylesheet API would need, in this order:(,[,{and function token, and whether the input ended first.split_comma_separated_list(),wp_get_state_selector_ancestor()and this walker each keep their own stack of closers.{}; a declaration needs a name and a colon).split_comma_separated_list()takes a string, so the walker splits selector lists itself.get_token().Depends on #102.
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.