From 53cf7244da2a2040333335c36e435b1c12efdff9 Mon Sep 17 00:00:00 2001 From: Michał Gołębiowski-Owczarek Date: Wed, 20 Sep 2023 02:31:35 +0200 Subject: CSS:Selector: Align with 3.x, remove the outer `selector.js` wrapper Bring some changes from `3.x-stable`: * rename `rtrim` to `rtrimCSS` to distinguish from the previous `rtrim` regex used for `jQuery.trim` * backport one `id` selector test that avoids the selector engine path Other changes: * remove the inner function wrapper from `selector.js` by renaming the imported `document.js` value * use `jQuery.error` in `selectorError` * make Selector tests pass in all-modules runs by fixing a sinon mistake in Core tests - Core tests had a spy set up for `jQuery.error` that wasn't cleaned up, influencing Selector tests when all were run together Closes gh-5295 --- src/css/curCSS.js | 6 +++--- src/selector.js | 29 ++++++++++++++++------------- src/selector/rbuggyQSA.js | 2 ++ src/selector/selectorError.js | 4 +++- src/selector/tokenize.js | 4 ++-- src/var/rtrim.js | 6 ------ src/var/rtrimCSS.js | 6 ++++++ 7 files changed, 32 insertions(+), 25 deletions(-) delete mode 100644 src/var/rtrim.js create mode 100644 src/var/rtrimCSS.js (limited to 'src') diff --git a/src/css/curCSS.js b/src/css/curCSS.js index 16104d893..b0f0a7555 100644 --- a/src/css/curCSS.js +++ b/src/css/curCSS.js @@ -2,7 +2,7 @@ import { jQuery } from "../core.js"; import { isAttached } from "../core/isAttached.js"; import { getStyles } from "./var/getStyles.js"; import { rcustomProp } from "./var/rcustomProp.js"; -import { rtrim } from "../var/rtrim.js"; +import { rtrimCSS } from "../var/rtrimCSS.js"; export function curCSS( elem, name, computed ) { var ret, @@ -41,12 +41,12 @@ export function curCSS( elem, name, computed ) { // allowing us to differentiate them without a performance penalty // and returning `undefined` aligns with older jQuery. // - // rtrim treats U+000D CARRIAGE RETURN and U+000C FORM FEED + // rtrimCSS treats U+000D CARRIAGE RETURN and U+000C FORM FEED // as whitespace while CSS does not, but this is not a problem // because CSS preprocessing replaces them with U+000A LINE FEED // (which *is* CSS whitespace) // https://www.w3.org/TR/css-syntax-3/#input-preprocessing - ret = ret.replace( rtrim, "$1" ) || undefined; + ret = ret.replace( rtrimCSS, "$1" ) || undefined; } if ( ret === "" && !isAttached( elem ) ) { diff --git a/src/selector.js b/src/selector.js index 92bfcfed3..d71e65ad4 100644 --- a/src/selector.js +++ b/src/selector.js @@ -1,12 +1,12 @@ import { jQuery } from "./core.js"; import { nodeName } from "./core/nodeName.js"; -import { document } from "./var/document.js"; +import { document as preferredDoc } from "./var/document.js"; import { indexOf } from "./var/indexOf.js"; import { pop } from "./var/pop.js"; import { push } from "./var/push.js"; import { whitespace } from "./var/whitespace.js"; import { rbuggyQSA } from "./selector/rbuggyQSA.js"; -import { rtrim } from "./var/rtrim.js"; +import { rtrimCSS } from "./var/rtrimCSS.js"; import { isIE } from "./var/isIE.js"; import { identifier } from "./selector/var/identifier.js"; import { booleans } from "./selector/var/booleans.js"; @@ -27,10 +27,6 @@ import { toSelector } from "./selector/toSelector.js"; import "./selector/escapeSelector.js"; import "./selector/uniqueSort.js"; -var preferredDoc = document; - -( function() { - var i, outermostContext, @@ -167,7 +163,11 @@ function find( selector, context, results, seed ) { // Outside of IE, if we're not changing the context we can // use :scope instead of an ID. - if ( newContext !== context || isIE ) { + // Support: IE 11+ + // IE sometimes throws a "Permission denied" error when strict-comparing + // two documents; shallow comparisons work. + // eslint-disable-next-line eqeqeq + if ( newContext != context || isIE ) { // Capture the context ID, setting it first if necessary if ( ( nid = context.getAttribute( "id" ) ) ) { @@ -204,7 +204,7 @@ function find( selector, context, results, seed ) { } // All others - return select( selector.replace( rtrim, "$1" ), context, results, seed ); + return select( selector.replace( rtrimCSS, "$1" ), context, results, seed ); } /** @@ -632,7 +632,7 @@ jQuery.expr = { // spaces as combinators var input = [], results = [], - matcher = compile( selector.replace( rtrim, "$1" ) ); + matcher = compile( selector.replace( rtrimCSS, "$1" ) ); return matcher[ jQuery.expando ] ? markFunction( function( seed, matches, _context, xml ) { @@ -1070,7 +1070,12 @@ function matcherFromTokens( tokens ) { return indexOf.call( checkContext, elem ) > -1; }, implicitRelative, true ), matchers = [ function( elem, context, xml ) { - var ret = ( !leadingRelative && ( xml || context !== outermostContext ) ) || ( + + // Support: IE 11+ + // IE sometimes throws a "Permission denied" error when strict-comparing + // two documents; shallow comparisons work. + // eslint-disable-next-line eqeqeq + var ret = ( !leadingRelative && ( xml || context != outermostContext ) ) || ( ( checkContext = context ).nodeType ? matchContext( elem, context, xml ) : matchAnyContext( elem, context, xml ) ); @@ -1104,7 +1109,7 @@ function matcherFromTokens( tokens ) { // If the preceding token was a descendant combinator, insert an implicit any-element `*` tokens.slice( 0, i - 1 ) .concat( { value: tokens[ i - 2 ].type === " " ? "*" : "" } ) - ).replace( rtrim, "$1" ), + ).replace( rtrimCSS, "$1" ), matcher, i < j && matcherFromTokens( tokens.slice( i, j ) ), j < len && matcherFromTokens( ( tokens = tokens.slice( j ) ) ), @@ -1369,6 +1374,4 @@ find.select = select; find.setDocument = setDocument; find.tokenize = tokenize; -} )(); - export { jQuery, jQuery as $ }; diff --git a/src/selector/rbuggyQSA.js b/src/selector/rbuggyQSA.js index 7e4632c7f..b056ffe75 100644 --- a/src/selector/rbuggyQSA.js +++ b/src/selector/rbuggyQSA.js @@ -2,6 +2,8 @@ import { isIE } from "../var/isIE.js"; import { whitespace } from "../var/whitespace.js"; import { support } from "./support.js"; +// Build QSA regex. +// Regex strategy adopted from Diego Perini. export var rbuggyQSA = []; if ( isIE ) { diff --git a/src/selector/selectorError.js b/src/selector/selectorError.js index 69770ec6a..da2b239f9 100644 --- a/src/selector/selectorError.js +++ b/src/selector/selectorError.js @@ -1,3 +1,5 @@ +import { jQuery } from "../core.js"; + export function selectorError( msg ) { - throw new Error( "Syntax error, unrecognized expression: " + msg ); + jQuery.error( "Syntax error, unrecognized expression: " + msg ); } diff --git a/src/selector/tokenize.js b/src/selector/tokenize.js index 3c1e06c46..fb25cb207 100644 --- a/src/selector/tokenize.js +++ b/src/selector/tokenize.js @@ -1,7 +1,7 @@ import { jQuery } from "../core.js"; import { rcomma } from "./var/rcomma.js"; import { rleadingCombinator } from "./var/rleadingCombinator.js"; -import { rtrim } from "../var/rtrim.js"; +import { rtrimCSS } from "../var/rtrimCSS.js"; import { createCache } from "./createCache.js"; import { selectorError } from "./selectorError.js"; import { filterMatchExpr } from "./filterMatchExpr.js"; @@ -42,7 +42,7 @@ export function tokenize( selector, parseOnly ) { value: matched, // Cast descendant combinators to space - type: match[ 0 ].replace( rtrim, " " ) + type: match[ 0 ].replace( rtrimCSS, " " ) } ); soFar = soFar.slice( matched.length ); } diff --git a/src/var/rtrim.js b/src/var/rtrim.js deleted file mode 100644 index aa20c961c..000000000 --- a/src/var/rtrim.js +++ /dev/null @@ -1,6 +0,0 @@ -import { whitespace } from "./whitespace.js"; - -export var rtrim = new RegExp( - "^" + whitespace + "+|((?:^|[^\\\\])(?:\\\\.)*)" + whitespace + "+$", - "g" -); diff --git a/src/var/rtrimCSS.js b/src/var/rtrimCSS.js new file mode 100644 index 000000000..f35b8a1e9 --- /dev/null +++ b/src/var/rtrimCSS.js @@ -0,0 +1,6 @@ +import { whitespace } from "./whitespace.js"; + +export var rtrimCSS = new RegExp( + "^" + whitespace + "+|((?:^|[^\\\\])(?:\\\\.)*)" + whitespace + "+$", + "g" +); -- cgit v1.2.3