finder icon indicating copy to clipboard operation
finder copied to clipboard

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

Open antonmedv opened this issue 7 years ago • 9 comments

antonmedv avatar Feb 24 '18 11:02 antonmedv

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

antonmedv avatar Feb 27 '18 15:02 antonmedv

@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?

kfrederix avatar Apr 06 '22 16:04 kfrederix

Probably. Is nth-of-type available everywhere?

antonmedv avatar Apr 06 '22 17:04 antonmedv

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

kfrederix avatar Apr 06 '22 19:04 kfrederix

Sounds good.

antonmedv avatar Apr 06 '22 22:04 antonmedv

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 avatar Apr 25 '23 12:04 eoghanmurray

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.

eoghanmurray avatar Apr 25 '23 12:04 eoghanmurray

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.

antonmedv avatar Apr 25 '23 17:04 antonmedv

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.

Snooz82 avatar Jul 31 '23 20:07 Snooz82