nwsapi: Some uses of `:has` throw "not a valid selector"
I found two particular use cases of the :has selector, which work fine in modern browsers but nwsapi throws an error:
div:has(>div)results in'>div' is not a valid selector(so having>at the start of the subselector string is treated as invalid):has(div)results in'' is not a valid selector(so having:hasat the start of the selector string seems to result in it being parsed as empty)
I believe the selectors above are valid and should be supported, am I wrong?
I should say I’m using version 2.2.12
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 18
- Comments: 22 (13 by maintainers)
@bmm6o So there were two bugs with logical selectors
@dperini Thanks for the response. I’ve pushed changes to my fork that get me the following test results. has-basic 18/18 has-argument-with-explicit-scope 12/13 has-relative-argument.html 20/35
I took a look at your shortcut matcher, but the problem with it is that it changes the DOM by adding an
idto the starting element. It did work though (at least for the+next sibling combinator, I’d assume the others would too).The only correct solution that I can think of at the moment would be to use something like
querySelectorAllwith an adjusted relative selector either on the current element (for>and) or on it’s parent (for~and+) and then walking up the parent tree until we find an element with the same identity as our current element. I think that would be a slow approach, but:hasis known to be slow.@KindaOK
here is the state of my tests proceedings using your suggestions:
@KindaOK thank you so much for the contribution. I confirm that your code is almost complete regarding the minimal :has() support (for WPT basic and invalidation tests).
I am trying to add a local WPT testing to make it easy for other developer to test and suggest changes for the :has() pseudo-class. Originally the :has() pseudo-class was not supported since this selectors implementation was written in between 2014 and 2016 and at that time neither :scope nor :has() was part of the w3 specifications.
Actually the previous selector engine implementation was named “nwmatcher” and included a similar partial implementation of mangled selectors resolution see here by adding left/right context to mangled selector strings:
https://github.com/dperini/nwmatcher/blob/master/src/modules/nwmatcher-shortcuts.js
this “shortcut” module was added about 12 years ago to the “nwmatcher” selector engine project but might still be useful in the current context to make the implementation compatible with the new specifications.
I will try to keep this conversation updated until we can ship these new resolver, maybe not yet fully completed but still very useful looking to other developers interest.
Again, your work and help is much appreciated.
i think
*won’t work for more complex trees inside the element the mangled selector is scoped to, hence:scopeas we’re looking only for a top level element.indeed,
:scopemay not always be the right solution for:has(), i just wanted to add a data point to the cause of the issue. the idea of adding it conditionally sounds more appropriate depending on the mangling logic@cpmotion thank you for reviewing ongoing changes … The commit reference in my previous message above is wrong, also the description in commit 0094625 is correct. It should have referenced @gmarinov’s suggested change, that I have delayed/postponed due to doubts I have.
While testing I realized that maybe ":scope " should only be added in case of mangled selectors arguments, not unconditionally. I may be wrong so your comments and those of other developers are welcome.
Also adding "* " instead of ":scope ", when the argument to :has() is a mangled selector, will yield the he same results.
@gmarinov thank you for your help and suggestions. Your comments are welcome too and much appreciated.
Hi @dperini. Thanks for looking into this. We tested your change and @gmarinov’s together and separately. For our use case, which is essentially the same as the OP’s(
:has(> img)), @gmarinov’s:scopechange alone solved the failures:Yours alone in 0094625 did not, we still had the same invalid selector errors. Not sure if that addresses other issues though. Thanks!
Commit 1a3a5c5989dc0e7943b68333c539d57e3454ab14 (0094625e362d39d261e0a2abac3a3111d370df9c was a wrong commit reference, sorry) is an attempt at fixing the handling of mangled selectors as argument to the :has() pseudo-class.
Please test & report possible issues. Thank you.
@cee-chen The previous sibling selectors work with the version
2.2.7of nswapi.+1ing this, the following selectors also throw “not a valid selector” errors for us in Jest (works fine in browsers):
Previous sibling selectors:
Nested
:has/:is: