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

Use nth-of-type instead of nth-child when appropriated #3

Open
antonmedv opened this issue Feb 24, 2018 · 9 comments
Open

Use nth-of-type instead of nth-child when appropriated #3

antonmedv opened this issue Feb 24, 2018 · 9 comments
Labels
enhancement New feature or request

Comments

@antonmedv
Copy link
Owner

No description provided.

@antonmedv antonmedv added the enhancement New feature or request label Feb 24, 2018
@antonmedv
Copy link
Owner Author

After thinking a little bit more, nth-child as good as nth-of-type

@kfrederix
Copy link

@antonmedv I'm curious what are your thoughts about this now?
Personally, I would prefer nth-of-type just because it's so much more intuitive (for humans reading the selector) and also it might be a tad more robust in some cases.

I wouldn't mind creating a PR, if you would be open to that?

@antonmedv antonmedv reopened this Apr 6, 2022
@antonmedv
Copy link
Owner Author

Probably. Is nth-of-type available everywhere?

@kfrederix
Copy link

Browser support for this looks good to me at this point, yes.

@antonmedv
Copy link
Owner Author

Sounds good.

@eoghanmurray
Copy link

eoghanmurray commented Apr 25, 2023

I'm wondering why #some_id div:nth-child(2) a is preferred over #some_id :nth-child(2) a in the existing code? That div appears to be redundant in the examples I've checked.

:nth-of-type does require the tagName as part of the selector

@eoghanmurray
Copy link

One idea (for the readability the OP mentioned) is that the lib could favour div:nth-of-type(2) for cases where there are mixed siblings and a plain :nth-child for cases where siblings are all of the same type.

@antonmedv
Copy link
Owner Author

It’s an optimisation problem.
if we can check all selectors, we can select the best. But it’s too slow, so Finder uses heuristics to “guess” best selector. Finder uses concept of penalties.

@Snooz82
Copy link

Snooz82 commented Jul 31, 2023

I would second that.

input:nth-of-type(1) shouldnbe more stable and less affected by other siblings in the parent that are not of type input. So if some div or span changes, that selector could still work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants