Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow :host and :host-context to take selectors… #58

Merged
merged 2 commits into from
Nov 11, 2024

Conversation

savetheclocktower
Copy link
Contributor

@savetheclocktower savetheclocktower commented Nov 3, 2024

…as well as :nth-child and :nth-last-child.

Fixes #57.

This was prompted by #57 and I wanted to fix it while it was still in my head. I got bitten by the other thing that this PR fixes…

:nth-last-child(-n + 3 of li.important) {}

…where li.important is parsed as a plain_value instead of as a selector.

This is another quick fix that avoids a larger reckoning with pseudoclass arguments. It's hard to say whether the CSS spec has any sort of predictable limitation in mind for what can be a valid argument to a pseudoclass; the ones we've whitelisted are those that take CSS selectors, but among the others it's generally a one-off set of valid values that varies per pseudoclass. For example:

  • :state()’s argument is a custom state defined by a custom element — something we can't know, but which is probably constrained similarly to a JS identifier (begins with ASCII, no spaces, etc.)
  • :lang()’s argument is a language or locale code (en, en-US)
  • :dir()’s is either rtl or ltr
  • :nth-of-type() has similar syntax to :nth-child, but without the optional filtering selector that the latter has — even, odd, 4, -n, 2n+1 are all valid values

In theory, we could get away with a minimal approach:

  • Pseudoclass arguments that are obviously selectors get parsed as such
  • All other text inside a pseudoclass’s parentheses is parsed as plain_value

In practice, we go a bit further than this; for instance, I think the 4 in :nth-child(4) would get parsed as an integer_value, which is fine.

As I describe in #57, the holy grail here would be to prevent plain_value from precluding selector detection while lexing, but I haven't figured that out yet. Until we do, the best approach is to manually handle each pseudoclass that can take a selector to prevent scenarios where both selector and plain_value are valid lexing options.


I forget what the right hygiene is these days around committing generated files, so let me know if I've done this wrong and I'll amend it.

…as well as `:nth-child` and `:nth-last-child`.
@mohamedmansour
Copy link

@amaanq is it possible you can review this please! This unblocks Web Components support for tree-sitter-css since they depend on :host

@amaanq amaanq merged commit eca27e1 into tree-sitter:master Nov 11, 2024
4 checks passed
@savetheclocktower savetheclocktower deleted the fix-nth-child-of branch November 11, 2024 07:17
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.

bug: pseudo class selectors fails to parse class_names on attribute selectors
3 participants