-
Notifications
You must be signed in to change notification settings - Fork 20.5k
Selector: Re-introduce selector-native.js #5085
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
Changes from all commits
35ea38c
3d02f15
d7fa796
0aaa97d
d903a4d
f7a8a15
0e65138
3e54970
8d02def
5167381
733f55f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -128,11 +128,11 @@ module.exports = function( grunt ) { | |
| * Adds the specified module to the excluded or included list, depending on the flag | ||
| * @param {String} flag A module path relative to | ||
| * the src directory starting with + or - to indicate | ||
| * whether it should included or excluded | ||
| * whether it should be included or excluded | ||
| */ | ||
| const excluder = flag => { | ||
| let additional; | ||
| const m = /^(\+|\-|)([\w\/-]+)$/.exec( flag ); | ||
| const m = /^(\+|-|)([\w\/-]+)$/.exec( flag ); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was just WebStorm complaining that the escape was not necessary. |
||
| const exclude = m[ 1 ] === "-"; | ||
| const module = m[ 2 ]; | ||
|
|
||
|
|
@@ -150,10 +150,16 @@ module.exports = function( grunt ) { | |
| // These are the removable dependencies | ||
| // It's fine if the directory is not there | ||
| try { | ||
| excludeList( | ||
| fs.readdirSync( `${ srcFolder }/${ module }` ), | ||
| module | ||
| ); | ||
|
|
||
| // `selector` is a special case as we don't just remove | ||
| // the module, but we replace it with `selector-native` | ||
| // which re-uses parts of the `src/selector` folder. | ||
| if ( module !== "selector" ) { | ||
| excludeList( | ||
| fs.readdirSync( `${ srcFolder }/${ module }` ), | ||
| module | ||
| ); | ||
| } | ||
| } catch ( e ) { | ||
| grunt.verbose.writeln( e ); | ||
| } | ||
|
|
@@ -232,14 +238,14 @@ module.exports = function( grunt ) { | |
| // Remove the comma for anonymous defines | ||
| setOverride( `${ srcFolder }/exports/amd.js`, | ||
| read( "exports/amd.js" ) | ||
| .replace( /(\s*)"jquery"(\,\s*)/, | ||
| .replace( /(\s*)"jquery"(,\s*)/, | ||
| amdName ? "$1\"" + amdName + "\"$2" : "" ) ); | ||
| } | ||
|
|
||
| grunt.verbose.writeflags( excluded, "Excluded" ); | ||
| grunt.verbose.writeflags( included, "Included" ); | ||
|
|
||
| // Indicate a Slim build without listing all of the exclusions | ||
| // Indicate a Slim build without listing all the exclusions | ||
| // to save space. | ||
| if ( isPureSlim ) { | ||
| version += " slim"; | ||
|
|
@@ -260,7 +266,13 @@ module.exports = function( grunt ) { | |
|
|
||
| // Replace excluded modules with empty sources. | ||
| for ( const module of excluded ) { | ||
| setOverride( `${ srcFolder }/${ module }.js`, "" ); | ||
| setOverride( | ||
| `${ srcFolder }/${ module }.js`, | ||
|
|
||
| // The `selector` module is not removed, but replaced | ||
| // with `selector-native`. | ||
| module === "selector" ? read( "selector-native.js" ) : "" | ||
| ); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| /* | ||
| * Optional limited selector module for custom builds. | ||
| * | ||
| * Note that this DOES NOT SUPPORT many documented jQuery | ||
| * features in exchange for its smaller size: | ||
| * | ||
| * * Attribute not equal selector (!=) | ||
| * * Positional selectors (:first; :eq(n); :odd; etc.) | ||
| * * Type selectors (:input; :checkbox; :button; etc.) | ||
| * * State-based selectors (:animated; :visible; :hidden; etc.) | ||
| * * :has(selector) | ||
| * * :not(complex selector) | ||
| * * custom selectors via jQuery extensions | ||
| * * Leading combinators (e.g., $collection.find("> *")) | ||
| * * Reliable functionality on XML fragments | ||
| * * Requiring all parts of a selector to match elements under context | ||
| * (e.g., $div.find("div > *") now matches children of $div) | ||
| * * Matching against non-elements | ||
| * * Reliable sorting of disconnected nodes | ||
| * * querySelectorAll bug fixes (e.g., unreliable :focus on WebKit) | ||
| * | ||
| * If any of these are unacceptable tradeoffs, either use the full | ||
| * selector engine or customize this stub for the project's specific | ||
| * needs. | ||
| */ | ||
|
|
||
| import jQuery from "./core.js"; | ||
| import document from "./var/document.js"; | ||
| import documentElement from "./var/documentElement.js"; | ||
| import whitespace from "./var/whitespace.js"; | ||
|
|
||
| // The following utils are attached directly to the jQuery object. | ||
| import "./selector/contains.js"; | ||
| import "./selector/escapeSelector.js"; | ||
| import "./selector/uniqueSort.js"; | ||
|
|
||
| // Support: IE 9 - 11+ | ||
| // IE requires a prefix. | ||
| var matches = documentElement.matches || documentElement.msMatchesSelector; | ||
|
|
||
| jQuery.extend( { | ||
| find: function( selector, context, results, seed ) { | ||
| var elem, nodeType, | ||
| i = 0; | ||
|
|
||
| results = results || []; | ||
| context = context || document; | ||
|
|
||
| // Same basic safeguard as in the full selector module | ||
| if ( !selector || typeof selector !== "string" ) { | ||
| return results; | ||
| } | ||
|
|
||
| // Early return if context is not an element, document or document fragment | ||
| if ( ( nodeType = context.nodeType ) !== 1 && nodeType !== 9 && nodeType !== 11 ) { | ||
| return []; | ||
| } | ||
|
|
||
| if ( seed ) { | ||
| while ( ( elem = seed[ i++ ] ) ) { | ||
| if ( jQuery.find.matchesSelector( elem, selector ) ) { | ||
| results.push( elem ); | ||
| } | ||
| } | ||
| } else { | ||
| jQuery.merge( results, context.querySelectorAll( selector ) ); | ||
| } | ||
|
|
||
| return results; | ||
| }, | ||
| expr: { | ||
| attrHandle: {}, | ||
| match: { | ||
| bool: new RegExp( "^(?:checked|selected|async|autofocus|autoplay|controls|defer" + | ||
| "|disabled|hidden|ismap|loop|multiple|open|readonly|required|scoped)$", "i" ), | ||
| needsContext: new RegExp( "^" + whitespace + "*[>+~]" ) | ||
| } | ||
| } | ||
| } ); | ||
|
|
||
| jQuery.extend( jQuery.find, { | ||
| matches: function( expr, elements ) { | ||
| return jQuery.find( expr, null, null, elements ); | ||
| }, | ||
| matchesSelector: function( elem, expr ) { | ||
| return matches.call( elem, expr ); | ||
| } | ||
| } ); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| QUnit.module( "selector", { | ||
| beforeEach: function() { | ||
| this.safari = /\bsafari\b/i.test( navigator.userAgent ) && | ||
| !/\bchrome\b/i.test( navigator.userAgent ); | ||
| !/\b(?:headless)?chrome\b/i.test( navigator.userAgent ); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tests in CI are running on Chrome Headless which has a different user agent, making |
||
| }, | ||
| afterEach: moduleTeardown | ||
| } ); | ||
|
|
@@ -1908,6 +1908,21 @@ QUnit.testUnlessIE( "jQuery.contains within <template/> doesn't throw (gh-5147)" | |
| assert.ok( true, "Didn't throw" ); | ||
| } ); | ||
|
|
||
| QUnit.test( "find in document fragments", function( assert ) { | ||
| assert.expect( 1 ); | ||
|
|
||
| var elem, | ||
| nonnodes = jQuery( "#nonnodes" ).contents(), | ||
| fragment = document.createDocumentFragment(); | ||
|
|
||
| nonnodes.each( function() { | ||
| fragment.appendChild( this ); | ||
| } ); | ||
|
|
||
| elem = jQuery( fragment ).find( "#nonnodesElement" ); | ||
| assert.strictEqual( elem.length, 1, "Selection works" ); | ||
| } ); | ||
|
|
||
| QUnit.test( "jQuery.uniqueSort", function( assert ) { | ||
| assert.expect( 14 ); | ||
|
|
||
|
|
@@ -2156,7 +2171,7 @@ QUnit.test( "jQuery.escapeSelector", function( assert ) { | |
| assert.equal( jQuery.escapeSelector( "\uD834" ), "\uD834", "Doesn't escape lone high surrogate" ); | ||
| } ); | ||
|
|
||
| QUnit.test( "custom pseudos", function( assert ) { | ||
| QUnit[ QUnit.jQuerySelectors ? "test" : "skip" ]( "custom pseudos", function( assert ) { | ||
| assert.expect( 6 ); | ||
|
|
||
| try { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.