Skip to content

Add support for CSS nesting - #522

Merged
ylafon merged 2 commits into
w3c:mainfrom
j9t:css-nesting
Oct 1, 2026
Merged

ylafon merged 2 commits into
w3c:mainfrom
j9t:css-nesting

Conversation

@j9t

@j9t j9t commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Resolves #431 and #481. The CSS from #521 validates as well (the issue refers to a commit in validator/validator).

Disclosure: I felt free to tackle this knowing this is a much-requested feature; as I’m at the deeper end here, I used Claude Code to help with some of the work. That said, I take responsibility for the code presented with this PR.

What’s supported (CSS3 profiles and up; CSS 2.1 and earlier still report nesting as an error):

  • Nested style rules, including & anywhere in a compound, relative selectors (> a, + p, ~ p), selector lists, and deep nesting
  • Declarations after or between nested rules
  • @media, @supports, and @layer nested in style rules, with declarations directly inside
  • & in pseudo-class arguments (:is(), :not(), :where(), :has(), :nth-*(… of S))

Rules and declarations are told apart as in CSS Syntax: a { before the next ; or } means a nested rule. Nested rules are resolved against their parents and emitted as flat rules, so the cascade, conflict checks, and the “Valid CSS information” output work as before (ul, ol { li { … } } becomes ul li, ol li). Where substituting the parent would change the meaning, & becomes :is(parent).

@container and @scope aren’t supported by the validator in general; nested ones are reported as “Unrecognized at-rule,” as at the top level.

Also fixed along the way:

  • Compounds without a combinator between them were accepted ([x]div { … } was valid; this also affected &div)
  • @layer never closed its at-rule in the output model, so every following rule was printed inside the layer; @layer a, b; statements now print like @import

Notes:

  • CssParser.java was regenerated with JavaCC 7.0.13, because the javacc submodule (7.0.4) can’t parse the current grammar (diamond operator). 7.0.13 reproduces the current CssParser.java byte for byte, so most of that diff is generated.
  • Under CSS3 profiles, bugs/3633*.css now report 1 error instead of a duplicated 2, which is what the test notes call ideal. Their XML entries use the css2 profile, which is unchanged.
  • Testing: all test CSS files (1,090) were validated before and after in the css3svg and css21 profiles. The only change is the one above. The new nesting tests all pass with ant autotest. The 3 general.xml tests that fail (loops/loop1.css, loops/loop2.css, at-rule/valid.css) fail on main as well.

Tests: w3c/css-validator-testsuite#4 (the submodule pointer is left to update).

j9t added 2 commits September 29, 2026 15:09
Signed-off-by: Jens Oliver Meiert <jens@meiert.com>
Signed-off-by: Jens Oliver Meiert <jens@meiert.com>
@vadimkantorov

Copy link
Copy Markdown

Is this codebase different from validator/validator@c5abf58 ?

@j9t

j9t commented Sep 29, 2026

Copy link
Copy Markdown
Contributor Author

Is this codebase different from validator/validator@c5abf58 ?

validator/validator is an HTML validator. This repo covers the W3C CSS validator.

@vadimkantorov

vadimkantorov commented Sep 29, 2026 •

Copy link
Copy Markdown

It appears that CSS validator also exists in HTML validator:
validator/validator@c5abf58

I wanted originally to create an issue against whatever is running at https://validator.w3.org/, but when creating a "CSS issue" at https://github.com/validator/validator/, it redirects into this repo

Where do I need to create an issue to fix CSS-in-HTML validation at https://validator.w3.org/ ?

@ylafon

ylafon commented Oct 1, 2026

Copy link
Copy Markdown
Member

Thanks a lot for this PR, I will merge this then add more patches to support things that were in what I was working on.
Thanks for disclosing the use of Claude and for reviewing it before doing the PR

@ylafon
ylafon merged commit 25ff487 into w3c:main Oct 1, 2026
1 check passed
@vadimkantorov

vadimkantorov commented Oct 1, 2026 •

Copy link
Copy Markdown

Nice :) What is the process for this fix to land in downstream https://github.com/validator/validator and go live at https://validator.w3.org ?

@j9t

j9t commented Oct 1, 2026

Copy link
Copy Markdown
Contributor Author

Thanks a lot for this PR, I will merge this then add more patches to support things that were in what I was working on. Thanks for disclosing the use of Claude and for reviewing it before doing the PR

Absolutely!—glad this helped!

@j9t

j9t commented Oct 1, 2026

Copy link
Copy Markdown
Contributor Author

Nice :) What is the process for this fix to land in downstream https://github.com/validator/validator and go live at https://validator.w3.org ?

CC @sideshowbarker 🙂

sideshowbarker added a commit to validator/validator that referenced this pull request Oct 2, 2026
Revert the css-validator parts of c5abf58 and cb96dc6, so that
css-validator/ matches upstream’s validator-nu branch again. Upstream
now has its own support for both — w3c/css-validator#522 adds CSS
nesting, and later upstream commits add @scope — and the commits that
follow bring that in.
sideshowbarker pushed a commit to validator/validator that referenced this pull request Oct 2, 2026
Add support for CSS nesting to the CSS parser.

From w3c/css-validator#522.

Signed-off-by: Jens Oliver Meiert <jens@meiert.com>
sideshowbarker pushed a commit to validator/validator that referenced this pull request Oct 2, 2026
Add block detection and nesting resolution to the CSS parser.

From w3c/css-validator#522.

Signed-off-by: Jens Oliver Meiert <jens@meiert.com>
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.

Nested Style Rule Parse Error

3 participants