Repository: jquery/jquery
File: src/selector-native.js (JavaScript)

Code before:
/*
 * 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 ) {

Review comment (security):
I wonder if this a step too far... would it be worthwhile to preserve that jQuery behavior by filtering `querySelectorAll` results like e.g. `elem => jQuery.find.matchesSelector( elem, "#" + ephemeralId + " *" )` or `elem => jQuery.contains( context, elem )`?

There's also a larger but more performant version of the former using rudimentary selector transformation, e.g.
```js
// Prefix each item in the `selector` list with a scope prefix.
var match,
	extracts = [],
	// Helper here for clarity, inlining at its two call sites probably compresses better.
	restore = s => s.replace( /'(\d+)'/g, (_, i) => extracts[ i - 1 ] );

// Temporarily replace backslash escapes and strings,
// which could otherwise disrupt identification of the list's constituent selectors
// (e.g., `a\\,[attr*='foo,bar']`)
selector = selector.replace(
	/\\[^]|(['"])(?:\\[^]|(?!\1)[^\\])*\1/g,
	s => "'" + extracts.push( s ) + "'"
);

// Likewise for functional pseudo-classes, but recursively
// (e.g., `:not(:is(h1,h2), article header)`).
while ( (match = /([^]*?)(\([^()]*,[^()]*\))([^]*)/.exec(selector)) ) {
	selector = match[ 1 ] +
		"'" + extracts.push( restore( match[ 2 ] ) ) + "'" +
		match[ 3 ];
}

// Prefix and restore replacements in each selector of the list.
selector = selector.replace(
	/[^,]+/g,
	// ...but don't prefix selectors that already start with a :scope pseudo.
	sel => restore( sel )
		.replace( /^(?![ \t\r\n\f]*:scope(?![\\\w-]|[^\0-\x7f]))/, scopePrefix )
);
```
(where `scopePrefix` is either `":scope "` or `"#" + ephemeralId + " "`)

Code after:
/*
 * 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 ) {