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

Fix nth of type #52

Closed
wants to merge 4 commits into from
Closed

Conversation

kilobyte2007
Copy link
Contributor

@kilobyte2007 kilobyte2007 commented Feb 24, 2021

The current implementation of nth of type is not working correctly when there are more similar tags deeper down. Example:

<ul>
<li><a>Link 1</a></li>
<li>
    <a>Submenu</a>
    <ul><li><a>Sublink</a></li></ul>
</li>
<li><a>Link 2</a></li>
</ul>

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).

@fczbkk
Copy link
Owner

fczbkk commented Feb 27, 2021

@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 result will be what you expect it to be.

@kilobyte2007
Copy link
Contributor Author

kilobyte2007 commented Feb 27, 2021

The case you provided will indeed fallback to nth-child because div:nth-of-type(1) > p:nth-of-type(1) is valid for both p tags so that's not the case I was looking to fix, as this is already correct.

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.

@kilobyte2007
Copy link
Contributor Author

Hey @fczbkk, sorry to bump this. Any updates here?

@fczbkk
Copy link
Owner

fczbkk commented Mar 15, 2021

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.

@kilobyte2007
Copy link
Contributor Author

Great, thanks!

@fczbkk
Copy link
Owner

fczbkk commented Mar 20, 2021

Hey @kilobyte2007, I just published v3.0.0. It tries to prefer child selectors over descendant selectors, which means that in many cases, the selectors will be shorter and simpler. Please, give it a go.

@kilobyte2007
Copy link
Contributor Author

kilobyte2007 commented Mar 22, 2021

Hey, @fczbkk great news! Will give it a go later this week. Thanks a lot.
But I still think this PR needs to be merged because the nth-selector still won't work correctly in some situations. Example:

var div = document.createElement('ul')
div.innerHTML = '<li class="a1"></li><li class="a2"><ul><li class="b1"></li></ul></li><li class="a3"></li>'
div.querySelectorAll('li').forEach((el, i) => console.log(el, i))

In this case, the .a3 element will have a :nth-of-type(4) selector with the current implementation while it should be :nth-of-type(3) because the nested .b1 should be ignored. Or am I missing something?

fczbkk added a commit that referenced this pull request Sep 8, 2021
@fczbkk
Copy link
Owner

fczbkk commented Sep 8, 2021

@kilobyte2007 I finally had some time to play with this. You were right. This problem should be fixed in v3.4.4.

@fczbkk fczbkk closed this Sep 8, 2021
@kilobyte2007
Copy link
Contributor Author

Thanks a lot I'll try to use the v3.4.4 version.

@kilobyte2007 kilobyte2007 deleted the fix-nth-of-type branch January 10, 2022 14:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants