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] - generation of 'alternate' selector #65

Open
eoghanmurray opened this issue Apr 21, 2023 · 3 comments
Open

[feature request] - generation of 'alternate' selector #65

eoghanmurray opened this issue Apr 21, 2023 · 3 comments

Comments

@eoghanmurray
Copy link

So let's say I have generated a lovely selector like

.opened .single-link > span

My problem is that while the opened class is unique on the page at the moment, with my next click, I will open another 'section' of the page, and then there will be two opened classes present, and my original selector is no longer unique.

I guess I could use a MutationObserver to watch for class changes and build up a list of 'volatile' classnames, however this will not work if the HTML has exactly one .opened class at the start.

So I was thinking that it would be nice to have the ability to generate a 'backup' selector, which would be the equivalent of the following

finder(target,{
    className: (e) => e !== 'opened' && e !== 'single-link',
})

Is that something that is worthwhile to elevate to a single config option, which takes into account what would normally be generated, but then operates on alternative basis that whatever is currently unique, may not be in future?

@antonmedv
Copy link
Owner

What about adding api like finderN to generate N selectors.

@eoghanmurray
Copy link
Author

That'd be fine for me for n=2 ... my main concern is that the selectors are orthogonal

@eoghanmurray
Copy link
Author

I have achieved what I want for now with the following:

    const targetSelector = finder(target);
    const firstRoundClasses = targetSelector.match(/\.-?[_a-zA-Z]+[_a-zA-Z0-9-]*/g);
    const altTargetSelector = finder(target, {
        className: (cn) => firstRoundClasses === null || firstRoundClasses.indexOf('.' + cn) < 0
    });

using a className regexp. So thank you very much for the className option!

If there is a place to add that as a recipe, I can do so.

Of course this could be extended to exclude tagNames that have already been used, but that wasn't too interesting for me.

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

2 participants