From afa04b81ee1cc33398ad558c08c169e3a0876148 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Go=C5=82e=CC=A8biowski-Owczarek?= Date: Sun, 25 May 2025 14:43:10 +0200 Subject: [PATCH 1/3] jQuery.find: Backport Sizzle wiki to an API page This change migrates most of https://github.com/jquery/sizzle/wiki to an API page. Contrary to most other pages, each API didn't get its own XML `` with a full signature to save time. This can be further improved in the future. A Note section has been added, explaining the qSA to custom traversal fallback mechanism of jQuery. The browser support section has not been migrated as jQuery has different browser support than Sizzle and it'd be irrelevant here. Sizzle mentions have been replaced with `jQuery.find` or jQuery. Fixes gh-1224 --- entries/jQuery.find.xml | 280 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 280 insertions(+) create mode 100644 entries/jQuery.find.xml diff --git a/entries/jQuery.find.xml b/entries/jQuery.find.xml new file mode 100644 index 00000000..a3f013df --- /dev/null +++ b/entries/jQuery.find.xml @@ -0,0 +1,280 @@ + + + jQuery.find() + The jQuery selector engine, formerly known as Sizzle, is exposed under jQuery.find. This page describes all the APIs under jQuery.find. + + +

Note

+ +

jQuery selector engine first tries to run the passed selector — with some modifications — through native querySelectorAll, so selectors natively supported by the current browsers generally work out of the box. However, if the browser does not recognize the selector, jQuery does the whole selection on its own. Some newer selectors may not work with such a jQuery selection.

+ +

Selectors

+ +

CSS 3

+ +

jQuery supports virtually all CSS 3 Selectors, including escaped selectors (.foo\+bar), Unicode selectors, and results returned in document order. The only exceptions are those that would require additional DOM event listeners to keep track of the state of elements.

+ +

As such, the following pseudo-selectors are not supported:

+
    +
  • :hover
  • +
  • :active
  • +
  • :visited, :link
  • +
+ +

Note: These CSS3 pseudo-selectors were unsupported prior to version 1.9:

+
    +
  • :target
  • +
  • :root
  • +
  • :nth-last-child
  • +
  • :nth-of-type, :nth-last-of-type, :first-of-type, :last-of-type, :only-of-type
  • +
  • :lang()
  • +
+ +

Other selectors and conventions

+ +

Changes

+ +
    +
  • Full selector lists in :not(); e.g. :not(a.b), :not(div > p), :not(div, p)
  • +
  • Nested pseudo-selectors; e.g. :not(:has(div:first-child))
  • +
+ +

Additions

+ +
    +
  • [NAME!=VALUE]: Elements whose NAME attribute doesn't match the specified value. Equivalent to :not([NAME=VALUE]).
  • +
  • :contains(TEXT): Elements with textContent containing the word 'TEXT'. Case-sensitive.
  • +
  • :header: Header elements (h1, h2, h3, h4, h5, h6).
  • +
  • :parent: Elements with at least one child node (either text or an element).
  • +
  • :selected: (option) elements that are currently selected
  • +
+ +

Form Selector Additions

+ +

Note: In this context, input, button, select, and textarea are all considered to be input elements.

+ +
    +
  • :input: Input elements
  • +
  • :button: Input elements that are buttons or have type "button"
  • +
  • :checkbox, :file, :image, :password, :radio, :reset, :submit, :text: Input elements with the specified type
  • +
+ +

Positional Selector Additions

+ +

In this context, "positional" refers to an element's placement in the collection after a selection, based on document order. For example, div:first would return an array containing the first div on the page, while div:first em would target the first div on the page and select all em elements within.

+ +

Note: Positional indexes begin at zero.

+ +
    +
  • :first/:last: The first/last matching element
  • +
  • :even/:odd: Even/odd-numbered elements
  • +
  • :eq/:nth: The nth element; e.g. :eq(5) finds the 6th element
  • +
  • :lt/:gt: Elements at positions above/below the specified position
  • +
+ +

API

+ +

The jQuery Selection API consists of 3 parts:

+ + + +

Public API

+ +

jQuery.find( String selector[, DOMNode context[, Array results]] )

+ +

The main function for finding elements. Uses querySelectorAll if available.

+ +

returns (Array): All elements matching the selector

+ +

Parameters

+ +

selector: A CSS selector

+ +

context: An element, document, or document fragment to use as the context for finding elements. Defaults to document. + Note: Prior to version 2.1, document fragments were not valid here.

+ +

results: An array or an array-like object, to which jQuery will append results. For example, jQuery passes a jQuery collection. An "array-like object" is an object with a nonnegative numeric length property and a push method.

+ +

jQuery.find.matchesSelector( DOMElement element, String selector )

+ +

Uses native matchesSelector if available

+ +

returns(Boolean): Whether the given element matches the selector

+ +

Parameters

+ +

element: A DOMElement against which jQuery will test the selector

+ +

selector: A CSS selector

+ +

jQuery.find.matches( String selector, Array<DOMElement> elements )

+ +

returns(Array): Elements in the array that match the given selector

+ +

Parameters

+ +

selector: A CSS selector

+ +

elements: An array of DOMElements to filter against the specified selector

+ +

Extension API

+ +

jQuery.find.selectors.match.NAME = RegExp

+ +

This contains the regular expressions used to parse a selector into different parts, to be used for finding and filtering. The name of each of the regular expressions should correspond to the names specified in the jQuery.find.selectors.find and jQuery.find.selectors.filter objects.

+ +

Finding

+ +

In order to add a new find function:

+
    +
  • A regular expression must be added to the match object.
  • +
  • A function to find must be defined.
  • +
  • "|" + NAME must be appended to the jQuery.find.selectors.order regex.
  • +
+ +
jQuery.find.selectors.find.NAME = function( match, context, isXML ) {}
+ +

A method for finding some elements on a page. The specified function will be called no more than once per selector.

+
    +
  • match is the array of results returned from matching the specified regex against the selector.
  • +
  • context is the DOMElement or DOMDocument from which selection will occur.
  • +
  • isXML is a boolean value indicating whether the function is currently operating within an XML document.
  • +
+ +

Filtering

+ +

In order to add a new filtering statement:

+
    +
  • A regular expression must be added to the match object.
  • +
  • A function must be added to the filter object.
  • +
  • A function may optionally be added to the preFilter object.
  • +
+ +
jQuery.find.selectors.preFilter.NAME = function( match ) {}
+ +

An optional pre-filter function which allows filtering of the matched array against the corresponding regular expression, which will return a new matched array. This matched array will eventually be passed and flattened as arguments against the corresponding filter function. This is intended to clean up some of the repetitive processing that occurs in a filter function.

+ +
jQuery.find.selectors.filter.NAME: function( element, match[1][, match[2], match[3], ...] ) {}
+ +

Note: match[0] will be deleted prior to being passed to a filter, and must not be used.

+ +

The arguments for a filter method are the element and the captures from the regex corresponding to this filter (indicated above by what is in the match, starting at index 1). The return result must be boolean: true if the element matches the selector, false if not.

+ +

Attributes

+ +
jQuery.find.selectors.attrHandle.LOWERCASE_NAME = function( elem, casePreservedName, isXML ) {}
+ +

Handle an attribute which requires specialized processing (such as href, which has cross-browser issues). The return result must be the actual string value of that attribute.

+ +

Pseudo-selectors (pseudos)

+ +
jQuery.find.selectors.pseudos.NAME = function( elem ) {}
+ +

The most common extension to a selector engine: adding a new pseudo. The return result from this function must be boolean: true if the element matches the selector, false if not.

+ +

For example, this defines a simple :fixed pseudo:

+

+var $test = jQuery( document );
+jQuery.find.selectors.pseudos.fixed = function( elem ) {
+  $test[ 0 ] = elem;
+  return $test.css( "position" ) === "fixed";
+};
+    
+ +
jQuery.find.selectors.createPseudo( function )
+ +

createPseudo is only required if the custom pseudo-selector accepts an argument.

+ +

Note: In jQuery 1.8 and earlier, the API for creating custom pseudos with arguments was broken. In jQuery 1.8.1+, the API is backwards-compatible. Regardless, the use of createPseudo is greatly encouraged.

+ +

Now that the parser compiles a single function containing other functions, custom pseudo-selectors with arguments are much cleaner.

+ +

For example, within jQuery, the implementation of the :not( <sub-selector> ) pseudo is very similar to:

+ +

+jQuery.find.selectors.pseudos.not =
+  jQuery.find.selectors.createPseudo( function( subSelector ) {
+    var matcher = jQuery.find.compile( subSelector );
+    return function( elem ) {
+      return !matcher( elem );
+    };
+  } );
+    
+ +
Backwards-compatible plugins for pseudos with arguments
+ +

In order to write a custom selector with arguments that can take advantage of the new API, yet still support all versions of jQuery, check for the createPseudo method.

+ +

The following example uses jQuery syntax. Live example

+

+// An implementation of a case-insensitive contains pseudo
+// made for all versions of jQuery
+( function( $ ) {
+
+function icontains( elem, text ) {
+  return (
+    elem.textContent ||
+    elem.innerText ||
+    $( elem ).text() ||
+    ""
+  ).toLowerCase().indexOf( ( text || "" ).toLowerCase() ) > -1;
+}
+
+$.expr.pseudos.icontains = $.expr.createPseudo ?
+  $.expr.createPseudo( function( text ) {
+    return function( elem ) {
+      return icontains( elem, text );
+    };
+  } ) :
+  function( elem, i, match ) {
+    return icontains( elem, match[ 3 ] );
+  };
+
+} )( jQuery );
+    
+ +
jQuery.find.selectors.setFilters.LOWERCASE_NAME = function( elements, argument, not ) {}
+ +

These filters are run after a previous part of a selector has already returned results. setFilters are found from matching jQuery.find.selectors.match.POS. When applicable, argument is expected to be an integer. The not argument is a boolean indicating whether the result should be inverted (as in div:not(:first)).

+ +

For example, the code for the :first setFilter is similar to:

+

+var first = function( elements, argument, not ) {
+  // No argument for first
+  return not ? elements.slice( 1 ) : [ elements[ 0 ] ];
+};
+jQuery.find.selectors.setFilters.first = first;
+    
+ +

It is easy to extend jQuery selection engine — even jQuery's POS selectors. For example, to rename :first as :uno:

+
 [ 
] + ]]>
+ +

Internal API

+ +

Note: Functionality should be accessed via the Public and Extension APIs. While the Internal API is intended specifically for internal use, it has been exposed for edge cases.

+ +

jQuery.find.selectors.cacheLength

+ +

jQuery internally caches compiled selector functions and tokenization objects. The length of these caches defaults to 50, but can be set to any positive integer by assigning to this property.

+ +

jQuery.find.compile( selector )

+ +

This method compiles a selector function and caches it for later use. For example, calling jQuery.find.compile( ".myWidget:myPseudo" ) during initialization of a plugin will speed up the first selection of matching elements.

+ +

returns(Function): The compiled function to be used when filtering the set of possibly matching elements

+ +

Parameters

+ +

selector: A CSS selector

+ +
+
From b0b30589df9eeaf3d93ceb5c54460c86759566f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Go=C5=82e=CC=A8biowski-Owczarek?= Date: Sun, 25 May 2025 14:49:13 +0200 Subject: [PATCH 2/3] jQuery.find: Rename jQuery.find.selectors to jQuery.expr --- entries/jQuery.find.xml | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/entries/jQuery.find.xml b/entries/jQuery.find.xml index a3f013df..78a345f9 100644 --- a/entries/jQuery.find.xml +++ b/entries/jQuery.find.xml @@ -123,9 +123,9 @@

Extension API

-

jQuery.find.selectors.match.NAME = RegExp

+

jQuery.expr.match.NAME = RegExp

-

This contains the regular expressions used to parse a selector into different parts, to be used for finding and filtering. The name of each of the regular expressions should correspond to the names specified in the jQuery.find.selectors.find and jQuery.find.selectors.filter objects.

+

This contains the regular expressions used to parse a selector into different parts, to be used for finding and filtering. The name of each of the regular expressions should correspond to the names specified in the jQuery.expr.find and jQuery.expr.filter objects.

Finding

@@ -133,10 +133,10 @@
  • A regular expression must be added to the match object.
  • A function to find must be defined.
  • -
  • "|" + NAME must be appended to the jQuery.find.selectors.order regex.
  • +
  • "|" + NAME must be appended to the jQuery.expr.order regex.
-
jQuery.find.selectors.find.NAME = function( match, context, isXML ) {}
+
jQuery.expr.find.NAME = function( match, context, isXML ) {}

A method for finding some elements on a page. The specified function will be called no more than once per selector.

    @@ -154,11 +154,11 @@
  • A function may optionally be added to the preFilter object.
-
jQuery.find.selectors.preFilter.NAME = function( match ) {}
+
jQuery.expr.preFilter.NAME = function( match ) {}

An optional pre-filter function which allows filtering of the matched array against the corresponding regular expression, which will return a new matched array. This matched array will eventually be passed and flattened as arguments against the corresponding filter function. This is intended to clean up some of the repetitive processing that occurs in a filter function.

-
jQuery.find.selectors.filter.NAME: function( element, match[1][, match[2], match[3], ...] ) {}
+
jQuery.expr.filter.NAME: function( element, match[1][, match[2], match[3], ...] ) {}

Note: match[0] will be deleted prior to being passed to a filter, and must not be used.

@@ -166,26 +166,26 @@

Attributes

-
jQuery.find.selectors.attrHandle.LOWERCASE_NAME = function( elem, casePreservedName, isXML ) {}
+
jQuery.expr.attrHandle.LOWERCASE_NAME = function( elem, casePreservedName, isXML ) {}

Handle an attribute which requires specialized processing (such as href, which has cross-browser issues). The return result must be the actual string value of that attribute.

Pseudo-selectors (pseudos)

-
jQuery.find.selectors.pseudos.NAME = function( elem ) {}
+
jQuery.expr.pseudos.NAME = function( elem ) {}

The most common extension to a selector engine: adding a new pseudo. The return result from this function must be boolean: true if the element matches the selector, false if not.

For example, this defines a simple :fixed pseudo:


 var $test = jQuery( document );
-jQuery.find.selectors.pseudos.fixed = function( elem ) {
+jQuery.expr.pseudos.fixed = function( elem ) {
   $test[ 0 ] = elem;
   return $test.css( "position" ) === "fixed";
 };
     
-
jQuery.find.selectors.createPseudo( function )
+
jQuery.expr.createPseudo( function )

createPseudo is only required if the custom pseudo-selector accepts an argument.

@@ -196,8 +196,8 @@ jQuery.find.selectors.pseudos.fixed = function( elem ) {

For example, within jQuery, the implementation of the :not( <sub-selector> ) pseudo is very similar to:


-jQuery.find.selectors.pseudos.not =
-  jQuery.find.selectors.createPseudo( function( subSelector ) {
+jQuery.expr.pseudos.not =
+  jQuery.expr.createPseudo( function( subSelector ) {
     var matcher = jQuery.find.compile( subSelector );
     return function( elem ) {
       return !matcher( elem );
@@ -237,9 +237,9 @@ $.expr.pseudos.icontains = $.expr.createPseudo ?
 } )( jQuery );
     
-
jQuery.find.selectors.setFilters.LOWERCASE_NAME = function( elements, argument, not ) {}
+
jQuery.expr.setFilters.LOWERCASE_NAME = function( elements, argument, not ) {}
-

These filters are run after a previous part of a selector has already returned results. setFilters are found from matching jQuery.find.selectors.match.POS. When applicable, argument is expected to be an integer. The not argument is a boolean indicating whether the result should be inverted (as in div:not(:first)).

+

These filters are run after a previous part of a selector has already returned results. setFilters are found from matching jQuery.expr.match.POS. When applicable, argument is expected to be an integer. The not argument is a boolean indicating whether the result should be inverted (as in div:not(:first)).

For example, the code for the :first setFilter is similar to:


@@ -247,14 +247,14 @@ var first = function( elements, argument, not ) {
   // No argument for first
   return not ? elements.slice( 1 ) : [ elements[ 0 ] ];
 };
-jQuery.find.selectors.setFilters.first = first;
+jQuery.expr.setFilters.first = first;
     

It is easy to extend jQuery selection engine — even jQuery's POS selectors. For example, to rename :first as :uno:

 [ 
] ]]>
@@ -262,7 +262,7 @@ jQuery.find( "div:uno" ); // ==> [
]

Note: Functionality should be accessed via the Public and Extension APIs. While the Internal API is intended specifically for internal use, it has been exposed for edge cases.

-

jQuery.find.selectors.cacheLength

+

jQuery.expr.cacheLength

jQuery internally caches compiled selector functions and tokenization objects. The length of these caches defaults to 50, but can be set to any positive integer by assigning to this property.

From 0104bbac8b6e81b201b90e65f5f100fbceae53da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Go=C5=82e=CC=A8biowski-Owczarek?= Date: Sun, 25 May 2025 14:49:23 +0200 Subject: [PATCH 3/3] jQuery.find: Remove "Backwards-compatible plugins" Remove the "Backwards-compatible plugins for pseudos with arguments" section. It's no longer supported in jQuery 4.0 and it's pretty low-level anwyay. --- entries/jQuery.find.xml | 32 -------------------------------- 1 file changed, 32 deletions(-) diff --git a/entries/jQuery.find.xml b/entries/jQuery.find.xml index 78a345f9..f7e3de43 100644 --- a/entries/jQuery.find.xml +++ b/entries/jQuery.find.xml @@ -205,38 +205,6 @@ jQuery.expr.pseudos.not = } ); -
Backwards-compatible plugins for pseudos with arguments
- -

In order to write a custom selector with arguments that can take advantage of the new API, yet still support all versions of jQuery, check for the createPseudo method.

- -

The following example uses jQuery syntax. Live example

-

-// An implementation of a case-insensitive contains pseudo
-// made for all versions of jQuery
-( function( $ ) {
-
-function icontains( elem, text ) {
-  return (
-    elem.textContent ||
-    elem.innerText ||
-    $( elem ).text() ||
-    ""
-  ).toLowerCase().indexOf( ( text || "" ).toLowerCase() ) > -1;
-}
-
-$.expr.pseudos.icontains = $.expr.createPseudo ?
-  $.expr.createPseudo( function( text ) {
-    return function( elem ) {
-      return icontains( elem, text );
-    };
-  } ) :
-  function( elem, i, match ) {
-    return icontains( elem, match[ 3 ] );
-  };
-
-} )( jQuery );
-    
-
jQuery.expr.setFilters.LOWERCASE_NAME = function( elements, argument, not ) {}

These filters are run after a previous part of a selector has already returned results. setFilters are found from matching jQuery.expr.match.POS. When applicable, argument is expected to be an integer. The not argument is a boolean indicating whether the result should be inverted (as in div:not(:first)).