-
Notifications
You must be signed in to change notification settings - Fork 91
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
Fix nth of type #52
Fix nth of type #52
Conversation
@kilobyte2007 I'm not sure if I understand correctly, but I don't think this is going to work. Can you please add a test case for this? Something like this: <div id="root">
<div>
<p id="grandChild"></p>
</div>
<p id="directChild"></p>
</div> const root = document.querySelector('#root');
const directChild = root.querySelector('#directChild');
const result = getCssSelector(directChild, {
selectors: ['nthoftype'],
root,
}); I don't think |
b4c3a8d
to
8554d5d
Compare
The case you provided will indeed fallback to nth-child because I have actually made a mistake in the original comment, should've been "it would return :nth-of-type(4) instead of :nth-of-type(3)". I have added a test, but it won't show up the incorrect selector for the current implementation as it falls back to nth-of-child cause the nth-of-type selector generated is invalid (hence the fallback) but you will see it generates a better selector in the my version. |
Hey @fczbkk, sorry to bump this. Any updates here? |
Hey @kilobyte2007, sorry about the delay. I went through your issue and I finally understood why it did not make sense to me. Your update will work fine with the current version. But I am thinking about a tweak to the algorithm that will potentially create shorter selectors by first considering general child selectors and only then try the direct descendant selector (that's what current algorithm uses). In that case, your change would be counter-productive. I'll try to implement the new algorithm sometime this week. If it won't work, I'll merge your change. |
Great, thanks! |
Hey @kilobyte2007, I just published |
Hey, @fczbkk great news! Will give it a go later this week. Thanks a lot.
In this case, the |
@kilobyte2007 I finally had some time to play with this. You were right. This problem should be fixed in |
Thanks a lot I'll try to use the v3.4.4 version. |
The current implementation of nth of type is not working correctly when there are more similar tags deeper down. Example:
In this case, the correct :nth-of-type of the
<li>
of the Link 2, would be 2 because only siblings are considered (as per https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-of-type), but because in the previous implementation querySelectorAll(tag) was used, it was also considering the deeper nested elements so it would return :nth-of-type(3) instead of :nth-of-type(2).