Skip to content

[lexical-html] Bug Fix: reject an empty selector group in parseSelector - #9083

Merged
etrepum merged 2 commits into
meta:mainfrom
luantaraschi:fix/parse-selector-empty-group
Aug 20, 2026
Merged

etrepum merged 2 commits into
meta:mainfrom
luantaraschi:fix/parse-selector-empty-group

Conversation

@luantaraschi

Copy link
Copy Markdown
Contributor

Description

parseSelector builds one ParsedSimpleSelector per comma separated group, and a group with no tag and no refinement is treated as a wildcard. That is right for *, which is exactly that shape, but the parser cannot tell * from a group that is simply empty, so a hole in a selector list, a stray comma, or an empty string all come back as a selector that matches every element.

Measured through getSelectorImpl, on main, asking whether the compiled selector matches a <div>:

source matches <div>
h1,,h2 yes
h1, yes
,h1 yes
h1, ,h2 yes
"" yes
" " yes
h1 no

parseSelector is exported from @lexical/html and is what sel.css() calls, so an import rule written with any of those sources quietly captures every element instead of the handful the author meant, and the rule that should have won is shadowed. The file already says what it wants to do with input outside its subset:

 * Anything outside the subset (regex attribute, inline-style match,
 * combinators, pseudo-classes) is intentionally rejected — chain combinator
 * methods off the returned builder instead.

and it has the position annotated Cursor.assert for exactly that. The empty group was the one case that slipped through, and the old code says so in a comment on an empty if.

So parseSimpleSelector now reports whether it consumed a *, and parseSelector asserts that every group has a tag, a refinement, or that star. Nothing else changes: *, h1, * and *.foo still parse, and the tag dispatch and the OR predicate for lists are untouched.

Test plan

Before

CSS parser rejects an empty selector or a hole in a selector list fails on main: none of the six malformed sources throws.

After

That test passes, and the rest of the file is unaffected.

npx vitest run packages/lexical-html
 Test Files  11 passed (11)
      Tests  92 passed (92)

Full npx vitest run for the repo is green as well, with the same result as on a clean checkout here.

A comma separated group with no tag and no refinement is the shape a lone
`*` produces, and the parser could not tell that apart from a group that
is simply empty. So `h1,,h2`, `h1,`, `,h1`, `""` and a whitespace only
source all compiled into a selector that matches every element, and an
import rule written that way captured the whole document instead of the
handful of elements the author meant.

parseSimpleSelector now reports whether it consumed a `*`, and
parseSelector asserts each group has a tag, a refinement, or that star,
using the position annotated Cursor.assert the file already uses for
input outside its subset.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 20, 2026
@etrepum etrepum added the extended-tests Run extended e2e tests on a PR label Aug 20, 2026
}

return {predicates, tags};
return {isUniversal, predicates, tags};

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the representation should change to support this, you should simply throw in the parser here when this condition is detected

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, done. The check moved into parseSimpleSelector and the extra field is gone, so ParsedSimpleSelector is back to what it was. Same message and column as before, so the test did not change.

@vercel

vercel Bot commented Aug 20, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
lexical Ready Ready Preview Aug 20, 2026 8:36am
lexical-playground Ready Ready Preview Aug 20, 2026 8:36am

Request Review

@etrepum
etrepum added this pull request to the merge queue Aug 20, 2026
Merged via the queue into meta:main with commit ff2f693 Aug 20, 2026
48 checks passed
@etrepum etrepum mentioned this pull request Aug 31, 2026

This branch was successfully deployed

2 active deployments
Preview – lexical — 3e00b32c Deployed Aug 20, 2026 by vercel[bot]
Preview – lexical-playground — 3e00b32c Deployed Aug 20, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. extended-tests Run extended e2e tests on a PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants