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

[Feature Request] find path for multiple elements and find closest common parent #82

Open
pascalwacker opened this issue Apr 3, 2024 · 2 comments

Comments

@pascalwacker
Copy link

Basically a way to find "all" of a certain element.

Feature one: Find the closest common parent. If we take a news website often times an article will involve a title, an image as well as a description. This could be something like

<article>
  <h3>Title</h3>
  <span class="tag">Some Tag</span>
  <div>
    <img src="...">
    <p class="description">Some Description</p>
  </div>
</article>

If I want to get a selector for the article, my idea would be to click ex. the title as well as the image it would return the <article> path, as it's the closest parent of the given elements. In practice this would mean, you pass an array of DOM elements into a parentFinder function (or add an optional "mode" parameter to the finder function). who would then first traverse the DOM towards the root and then once a common ancestor is found simply call finder on that one

Feature two: Find multiple similar events. Again let's take the news website from Feature one. Currently finder would only return a path to the exact DOM element. This might look something like #articles > article:nth-child(3) for the 3rd article. I'd want a way for it to find similar elements, so ex. all <article> elements within #articles.
I think one possible implementation would be to use Feature one, to find the wrapping container and once that is found, have an array where you need to supply one DOM element of each item you're looking for in said container, meaning it looks again for a parent path that would match each of the supplied selectors individually. Though I'm sure there has to be a better way, or even a way only requiring you to supply 2-3 examples.

@antonmedv
Copy link
Owner

First one is easy. And can be implemented as a separate function. I guess on npm such packages exist.

For the second one: it is interesting 🤨

PS it is better to separate feature requests in different issues.

@allenchuang
Copy link

allenchuang commented May 21, 2024

I'm interested in the feature too. Are there other libraries that does this? Would be nice to support this natively in this package

For feature 2 wouldn't the selector you're looking for be:

'#articles > article'?

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

No branches or pull requests

3 participants