Skip to content

Theme JSON: Process block custom CSS with the CSS tokenizer - #105

Open
sirreal wants to merge 3 commits into
css-api/selector-listsfrom
css-api/block-custom-css
Open

sirreal wants to merge 3 commits into
css-api/selector-listsfrom
css-api/block-custom-css

Conversation

@sirreal

@sirreal sirreal commented Nov 17, 2026 •

Copy link
Copy Markdown
Owner

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.

Custom CSS on .foo Trunk This branch
& a { color: red; } :root :where(.foo a){color: red;} same
& 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

  • Declarations apply to the selector around them, at any depth. Declarations after a nested rule get their own rule after it, so source order holds.
  • & 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.
  • Every rule stays at 0-1-0 specificity in :root :where(). A pseudo-element moves after :where(), as on trunk; a list with different pseudo-elements gets one :where() per selector.
  • @media, @supports, @container and @layer blocks wrap the rules made from their contents, at any depth. Declarations directly in @media apply to the selector around it. Other at-rules are dropped.
  • Text that is not a declaration, a declaration or selector holding a stray ), ] 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:

  • The first rule, when it starts with a class, ID, attribute or pseudo selector, is joined to the block selector without a space: .is-style-x { … } selects .foo.is-style-x. Twenty Twenty-Four's Calendar CSS starts this way.
  • In a list whose first selector is joined without a space, selectors without & stay as written: &.a, .rtl .b gives :root :where(.foo.a, .rtl .b). The Quote CSS in Ipsum and Twenty Twenty-Four uses this to style .rtl pages. Under CSS Nesting, .rtl .b would select descendants of .foo and those rules would stop matching.

Comparison

Every custom CSS string in the bundled themes' theme.json and 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_JSON because 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:

  1. Block matching: the position of the closer for each (, [, { 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.
  2. A parser for a block's contents following CSS Syntax 3: declarations, qualified rules and at-rules as token ranges, with the spec's error recovery. This walker implements a subset (custom properties may hold {}; a declaration needs a name and a colon).
  3. Comma splitting over a token range. split_comma_separated_list() takes a string, so the walker splits selector lists itself.
  4. A cursor method for whether the current token is terminated. The processor has the flag but exposes it only through get_token().
  5. A faster tokenizer: at 0.25 µs per token it is the larger part of the cost.

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.

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.
@github-actions

Copy link
Copy Markdown

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:

Props jonsurrell.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub or WordPress.org user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant