antonmedv/finder

No selector found with root node passed in

thisispaul opened this issue · 4 comments

I have come across an issue with my specific use case and the "unique" functionality.

I am using this library to find a path to the element from a root node (its an SVG element). This SVG element happens to be duplicated on the page so when the library runs the unique check the selectors it generates from the root passed in is not unique (to the page) so i always get no path.

It doesn't look like there is a way to specify that I only need the path to be unique from the root element passed in as that is what I will do the resulting query in. I don't know if this is too much of an edge case to implement into the library but I don't know any other way of doing it (without writing my own).

I will open a pull request to see if I can make this work but any pointers are appreciated.
I am not familiar with TS and couldn't get it to compile due to some errors already present it seems. Would it be simple to add a way to use the root node as the "rootDocument" for unique checking?

Can you create PR with falling tests? I'll look into it and will try to fix the bug.

Also can you provide an example for better understanding of the problem.

@antonmedv I have added a pull request which fixes the issue. I have not added tests to it as not sure how to pass Elements in through in that test suite.

I will create an example page now showing what the issue is.

Here is a codepen where you can see the issue: https://codepen.io/thisispaul/project/editor/DQNxyy

The first Hello There span has this code:

$('div:nth-child(1) .test span').click(function () {
  const selector = finder(this, {
    root: $('.test:nth-child(1)')[0]
  });
  
  console.debug(selector);
});

And if you look in the console you can see it does not find a selector. This is because the HTML it is looking through is duplicated exactly so the selector it generates (from the root passed in) is not unique to the document. The second Hello There works fine as I do not pass the root in.

This can be solved by using the root passed in as the rootDocument variable. In my pull request I passed in a separate property to specify the rootDocument which fixes this issue (if the rootDocument is passed in) but maybe it should always use the root as the rootDocument if it is present so this exrta property is not needed?

Closing as this is fixed now due to e072a45