Skip to content

Commit 63c3af4

Browse files
authored
Selector: Stop relying on CSS.supports( "selector(...)" )
`CSS.supports( "selector(...)" )` has different semantics than selectors passed to `querySelectorAll`. Apart from the fact that the former returns `false` for unrecognized selectors and the latter throws, `qSA` is more forgiving and accepts some invalid selectors, auto-correcting them where needed - for example, mismatched brackers are auto-closed. This behavior difference is breaking for many users. To add to that, a recent CSSWG resolution made `:is()` & `:where()` the only pseudos with forgiving parsing; browsers are in the process of making `:has()` parsing unforgiving. Taking all that into account, we go back to our previous try-catch approach without relying on `CSS.supports( "selector(...)" )`. The only difference is we detect forgiving parsing in `:has()` and mark the selector as buggy. The PR also updates `playwright-webkit` so that we test against a version of WebKit that already has non-forgiving `:has()`. Fixes gh-5194 Closes gh-5207 Ref gh-5206 Ref gh-5098 Ref gh-5107 Ref w3c/csswg-drafts#7676
1 parent ac1c59a commit 63c3af4

File tree

3 files changed

+81
-116
lines changed

3 files changed

+81
-116
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
"karma-webkit-launcher": "2.1.0",
5656
"load-grunt-tasks": "5.1.0",
5757
"native-promise-only": "0.8.1",
58-
"playwright-webkit": "1.29.2",
58+
"playwright-webkit": "1.30.0",
5959
"promises-aplus-tests": "2.1.2",
6060
"q": "1.5.1",
6161
"qunit": "2.9.2",

src/selector.js

+23-60
Original file line numberDiff line numberDiff line change
@@ -303,32 +303,6 @@ function find( selector, context, results, seed ) {
303303
}
304304

305305
try {
306-
307-
// `qSA` may not throw for unrecognized parts using forgiving parsing:
308-
// https://drafts.csswg.org/selectors/#forgiving-selector
309-
// like the `:is()` pseudo-class:
310-
// https://drafts.csswg.org/selectors/#matches
311-
// `CSS.supports` is still expected to return `false` then:
312-
// https://drafts.csswg.org/css-conditional-4/#typedef-supports-selector-fn
313-
// https://drafts.csswg.org/css-conditional-4/#dfn-support-selector
314-
if ( support.cssSupportsSelector &&
315-
316-
// `CSS.supports( "selector(...)" )` requires the argument to the
317-
// `selector` function to be a `<complex-selector>`, not
318-
// a `<complex-selector-list>` which our selector may be. Wrapping with
319-
// `:is` works around the issue and is supported by all browsers
320-
// we support except for IE which will fail the support test anyway.
321-
// eslint-disable-next-line no-undef
322-
!CSS.supports( "selector(:is(" + newSelector + "))" ) ) {
323-
324-
// Support: IE 9 - 11+
325-
// Throw to get to the same code path as an error directly in qSA.
326-
// Note: once we only support browser supporting
327-
// `CSS.supports('selector(...)')`, we can most likely drop
328-
// the `try-catch`. IE doesn't implement the API.
329-
throw new Error();
330-
}
331-
332306
push.apply( results,
333307
newContext.querySelectorAll( newSelector )
334308
);
@@ -575,33 +549,22 @@ function setDocument( node ) {
575549
return document.querySelectorAll( ":scope" );
576550
} );
577551

578-
// Support: IE 9 - 11+
579-
// IE doesn't support `CSS.supports( "selector(...)" )`; it will throw
580-
// in this support test.
581-
//
582-
// Support: Chrome 105+, Firefox <106, Safari 15.4+
583-
// Make sure forgiving mode is not used in `CSS.supports( "selector(...)" )`.
584-
//
585-
// `:is()` uses a forgiving selector list as an argument and is widely
586-
// implemented, so it's a good one to test against.
587-
support.cssSupportsSelector = assert( function() {
588-
/* eslint-disable no-undef */
589-
590-
return CSS.supports( "selector(*)" ) &&
591-
592-
// Support: Firefox 78-81 only
593-
// In old Firefox, `:is()` didn't use forgiving parsing. In that case,
594-
// fail this test as there's no selector to test against that.
595-
// `CSS.supports` uses unforgiving parsing
596-
document.querySelectorAll( ":is(:jqfake)" ) &&
597-
598-
// `*` is needed as Safari & newer Chrome implemented something in between
599-
// for `:has()` - it throws in `qSA` if it only contains an unsupported
600-
// argument but multiple ones, one of which is supported, are fine.
601-
// We want to play safe in case `:is()` gets the same treatment.
602-
!CSS.supports( "selector(:is(*,:jqfake))" );
603-
604-
/* eslint-enable */
552+
// Support: Chrome 105 - 110+, Safari 15.4 - 16.3+
553+
// Make sure the the `:has()` argument is parsed unforgivingly.
554+
// We include `*` in the test to detect buggy implementations that are
555+
// _selectively_ forgiving (specifically when the list includes at least
556+
// one valid selector).
557+
// Note that we treat complete lack of support for `:has()` as if it were
558+
// spec-compliant support, which is fine because use of `:has()` in such
559+
// environments will fail in the qSA path and fall back to jQuery traversal
560+
// anyway.
561+
support.cssHas = assert( function() {
562+
try {
563+
document.querySelector( ":has(*,:jqfake)" );
564+
return false;
565+
} catch ( e ) {
566+
return true;
567+
}
605568
} );
606569

607570
// ID filter and find
@@ -752,14 +715,14 @@ function setDocument( node ) {
752715
}
753716
} );
754717

755-
if ( !support.cssSupportsSelector ) {
718+
if ( !support.cssHas ) {
756719

757-
// Support: Chrome 105+, Safari 15.4+
758-
// `:has()` uses a forgiving selector list as an argument so our regular
759-
// `try-catch` mechanism fails to catch `:has()` with arguments not supported
760-
// natively like `:has(:contains("Foo"))`. Where supported & spec-compliant,
761-
// we now use `CSS.supports("selector(:is(SELECTOR_TO_BE_TESTED))")`, but
762-
// outside that we mark `:has` as buggy.
720+
// Support: Chrome 105 - 110+, Safari 15.4 - 16.3+
721+
// Our regular `try-catch` mechanism fails to detect natively-unsupported
722+
// pseudo-classes inside `:has()` (such as `:has(:contains("Foo"))`)
723+
// in browsers that parse the `:has()` argument as a forgiving selector list.
724+
// https://drafts.csswg.org/selectors/#relational now requires the argument
725+
// to be parsed unforgivingly, but browsers have not yet fully adjusted.
763726
rbuggyQSA.push( ":has" );
764727
}
765728

0 commit comments

Comments
 (0)