diff options
author | Michał Gołębiowski-Owczarek <m.goleb@gmail.com> | 2022-09-19 21:56:02 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-19 20:56:02 +0200 |
commit | d153c375e67f2c2dba82c2fb079c36b8d795e66a (patch) | |
tree | 68849b687ce4dd7ab9a9bf8db648e06d3eac3259 /src/selector.js | |
parent | 78321f078ce04ce78aeade8e2860ac41d05fae54 (diff) | |
download | jquery-d153c375e67f2c2dba82c2fb079c36b8d795e66a.tar.gz jquery-d153c375e67f2c2dba82c2fb079c36b8d795e66a.zip |
Selector: Use jQuery `:has` if `CSS.supports(selector(...))` non-compliant
jQuery has followed the following logic for selector handling for ages:
1. Modify the selector to adhere to scoping rules jQuery mandates.
2. Try `qSA` on the modified selector. If it succeeds, use the results.
3. If `qSA` threw an error, run the jQuery custom traversal instead.
It worked fine so far but now CSS has a concept of forgiving selector lists that
some selectors like `:is()` & `:has()` use. That means providing unrecognized
selectors as parameters to `:is()` & `:has()` no longer throws an error, it will
just return no results. That made browsers with native `:has()` support break
selectors using jQuery extensions inside, e.g. `:has(:contains("Item"))`.
Detecting support for selectors can also be done via:
```js
CSS.supports( "selector(SELECTOR_TO_BE_TESTED)" )
```
which returns a boolean. There was a recent spec change requiring this API to
always use non-forgiving parsing:
https://github.com/w3c/csswg-drafts/issues/7280#issuecomment-1143852187
However, no browsers have implemented this change so far.
To solve this, two changes are being made:
1. In browsers supports the new spec change to `CSS.supports( "selector()" )`,
use it before trying `qSA`.
2. Otherwise, add `:has` to the buggy selectors list.
Fixes gh-5098
Closes gh-5107
Ref w3c/csswg-drafts#7676
Diffstat (limited to 'src/selector.js')
-rw-r--r-- | src/selector.js | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/selector.js b/src/selector.js index bc60e61e4..871cf8682 100644 --- a/src/selector.js +++ b/src/selector.js @@ -9,6 +9,7 @@ import whitespace from "./var/whitespace.js"; import rbuggyQSA from "./selector/rbuggyQSA.js"; import rtrim from "./var/rtrim.js"; import isIE from "./var/isIE.js"; +import support from "./selector/support.js"; // The following utils are attached directly to the jQuery object. import "./selector/contains.js"; @@ -252,6 +253,27 @@ function find( selector, context, results, seed ) { } try { + + // `qSA` may not throw for unrecognized parts using forgiving parsing: + // https://drafts.csswg.org/selectors/#forgiving-selector + // like the `:has()` pseudo-class: + // https://drafts.csswg.org/selectors/#relational + // `CSS.supports` is still expected to return `false` then: + // https://drafts.csswg.org/css-conditional-4/#typedef-supports-selector-fn + // https://drafts.csswg.org/css-conditional-4/#dfn-support-selector + if ( support.cssSupportsSelector && + + // eslint-disable-next-line no-undef + !CSS.supports( "selector(" + newSelector + ")" ) ) { + + // Support: IE 11+ + // Throw to get to the same code path as an error directly in qSA. + // Note: once we only support browser supporting + // `CSS.supports('selector(...)')`, we can most likely drop + // the `try-catch`. IE doesn't implement the API. + throw new Error(); + } + push.apply( results, newContext.querySelectorAll( newSelector ) ); |