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

[selectors-4] Clarity about whitespace between :nth-child()'s "of" and "S" #8285

Closed
zrhoffman opened this issue Jan 6, 2023 · 5 comments
Closed
Labels
Closed as Question Answered Used when the issue is more of a question than a problem, and it's been answered. selectors-4 Current Work

Comments

@zrhoffman
Copy link

Should things like :nth-child(1 of/* my comment */body) or :nth-child(1 of.c) parse?

From CSS Syntax 3:

<<whitespace-token>> is never indicated in the grammar;
<<whitespace-token>>s are allowed before, after, and between any two tokens,
unless explicitly specified otherwise in prose definitions.
(For example, if the prelude of a rule is a selector,
whitespace is significant.)

I was unable to find any mention of whitespace being required for of S in the CSS Selectors 4 :nth-child() pseudo-class or :nth-last-child() pseudo-class sections.

So should <ident-token "of"> <complex-real-selector-list> parse?

  • If not, the CSS Selectors 4 :nth-child() and :nth-last-child() prose would probably benefit from clarification.

  • If so, some examples would be nice.

@fantasai
Copy link
Collaborator

fantasai commented Jan 6, 2023

https://www.w3.org/TR/selectors-4/#grammar defines white space requirements for selectors, but you're right, it doesn't seem to indicate whether whitespace is required around of. Probably it should be.

@tabatkins
Copy link
Member

The parsing is clear - :nth-child(1 of.c) is perfectly valid and equivalent to :nth-child(1 of .c). Its terrible and you shouldn't write it, but the parsing and behavior is well-defined.

@tabatkins tabatkins added Closed as Question Answered Used when the issue is more of a question than a problem, and it's been answered. and removed Needs Edits labels Jan 7, 2023
@zrhoffman
Copy link
Author

Tests for this case added in web-platform-tests/wpt#37797

@nt1m
Copy link
Member

nt1m commented Jan 8, 2023

@fantasai @tabatkins Is there a reason to not require one? WebKit requires one, and although it'd be easy to change that, I think that'd be unfortunate.

The fact that :nth-child(1 ofdiv) is invalid, but :nth-child(1 of.div) is valid, feels a bit off to me. Though looking at @zrhoffman's testcase, I guess one use-case is adding a comment between the of and the selector.

@tabatkins
Copy link
Member

There's no particular reason to not require one, but the general policy across all of CSS is that whitespace is always optional, and I'm mildly opposed to adding one-off requirements for it unless there's a good reason; it just makes the language slightly less consistent. (And we probably can't add a general requirement for whitespace between tokens, because I suspect minifiers exist that take advantage of the current rules.)

There's not really a "use-case" either way, it's just that comments are a guaranteed way to force a tokenization boundary without whitespace, so we usually reach for them when showing off the parsing. ^_^

webkit-early-warning-system pushed a commit to nt1m/WebKit that referenced this issue Jan 10, 2023
…ld`/`:nth-last-child`

https://bugs.webkit.org/show_bug.cgi?id=250353
rdar://104058693

Reviewed by Simon Fraser.

As clarified by CSSWG editors in: w3c/csswg-drafts#8285

Tests:
- LayoutTests/imported/w3c/web-platform-tests/css/selectors/nth-child-of-no-space-after-of.html
- LayoutTests/imported/w3c/web-platform-tests/css/selectors/nth-last-child-of-no-space-after-of.html

* LayoutTests/TestExpectations:
* LayoutTests/fast/css/parsing-css-nth-child-of-3-expected.txt:
* LayoutTests/fast/css/parsing-css-nth-child-of-3.html:
* LayoutTests/fast/css/parsing-css-nth-last-child-of-3-expected.txt:
* LayoutTests/fast/css/parsing-css-nth-last-child-of-3.html:
* Source/WebCore/css/parser/CSSSelectorParser.cpp:
(WebCore::CSSSelectorParser::consumePseudo):

Canonical link: https://commits.webkit.org/258703@main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Closed as Question Answered Used when the issue is more of a question than a problem, and it's been answered. selectors-4 Current Work
Projects
None yet
Development

No branches or pull requests

4 participants