diff options
author | Michał Gołębiowski-Owczarek <m.goleb@gmail.com> | 2022-12-12 22:27:59 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-12 22:27:59 +0100 |
commit | 024d87195ac46690023e2b0b308d4406a8a5a27e (patch) | |
tree | 7238b95beb38397175388af04006c08d1a1edf78 | |
parent | c909d6b1ff444e68618b6da13d9c21714f681925 (diff) | |
download | jquery-024d87195ac46690023e2b0b308d4406a8a5a27e.tar.gz jquery-024d87195ac46690023e2b0b308d4406a8a5a27e.zip |
Core:Selector: Move jQuery.contains from the selector to the core module
The `jQuery.contains` method is quite simple in jQuery 4+. On the other side,
it's a dependency of the core `isAttached` util which is not ideal; moving
it from the `selector` the `core` module resolves the issue.
Closes gh-5167
-rw-r--r-- | src/core.js | 14 | ||||
-rw-r--r-- | src/core/isAttached.js | 2 | ||||
-rw-r--r-- | src/selector-native.js | 1 | ||||
-rw-r--r-- | src/selector.js | 1 | ||||
-rw-r--r-- | src/selector/contains.js | 15 | ||||
-rw-r--r-- | test/unit/core.js | 56 | ||||
-rw-r--r-- | test/unit/selector.js | 55 |
7 files changed, 70 insertions, 74 deletions
diff --git a/src/core.js b/src/core.js index 4f5731ae1..16cd9a7c7 100644 --- a/src/core.js +++ b/src/core.js @@ -314,6 +314,20 @@ jQuery.extend( { return !rhtmlSuffix.test( namespace || docElem && docElem.nodeName || "HTML" ); }, + // Note: an element does not contain itself + contains: function( a, b ) { + var bup = b && b.parentNode; + + return a === bup || !!( bup && bup.nodeType === 1 && ( + + // Support: IE 9 - 11+ + // IE doesn't have `contains` on SVG. + a.contains ? + a.contains( bup ) : + a.compareDocumentPosition && a.compareDocumentPosition( bup ) & 16 + ) ); + }, + merge: function( first, second ) { var len = +second.length, j = 0, diff --git a/src/core/isAttached.js b/src/core/isAttached.js index 9c57a4741..72c7bbda4 100644 --- a/src/core/isAttached.js +++ b/src/core/isAttached.js @@ -1,8 +1,6 @@ import jQuery from "../core.js"; import documentElement from "../var/documentElement.js"; -import "../selector/contains.js"; // jQuery.contains - var isAttached = function( elem ) { return jQuery.contains( elem.ownerDocument, elem ) || elem.getRootNode( composed ) === elem.ownerDocument; diff --git a/src/selector-native.js b/src/selector-native.js index 0e2e48d81..7ca500515 100644 --- a/src/selector-native.js +++ b/src/selector-native.js @@ -30,7 +30,6 @@ 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"; diff --git a/src/selector.js b/src/selector.js index a69e16556..e6ec6a326 100644 --- a/src/selector.js +++ b/src/selector.js @@ -12,7 +12,6 @@ import isIE from "./var/isIE.js"; import support from "./selector/support.js"; // The following utils are attached directly to the jQuery object. -import "./selector/contains.js"; import "./selector/escapeSelector.js"; import "./selector/uniqueSort.js"; diff --git a/src/selector/contains.js b/src/selector/contains.js deleted file mode 100644 index 137b54388..000000000 --- a/src/selector/contains.js +++ /dev/null @@ -1,15 +0,0 @@ -import jQuery from "../core.js"; - -// Note: an element does not contain itself -jQuery.contains = function( a, b ) { - var bup = b && b.parentNode; - - return a === bup || !!( bup && bup.nodeType === 1 && ( - - // Support: IE 9 - 11+ - // IE doesn't have `contains` on SVG. - a.contains ? - a.contains( bup ) : - a.compareDocumentPosition && a.compareDocumentPosition( bup ) & 16 - ) ); -}; diff --git a/test/unit/core.js b/test/unit/core.js index b4bd5838c..49b7aa607 100644 --- a/test/unit/core.js +++ b/test/unit/core.js @@ -1557,3 +1557,59 @@ QUnit[ includesModule( "deferred" ) ? "test" : "skip" ]( "jQuery.readyException throw new Error( "Error in jQuery ready" ); } ); } ); + +QUnit.test( "jQuery.contains", function( assert ) { + assert.expect( 16 ); + + var container = document.getElementById( "nonnodes" ), + element = container.firstChild, + text = element.nextSibling, + nonContained = container.nextSibling, + detached = document.createElement( "a" ); + assert.ok( element && element.nodeType === 1, "preliminary: found element" ); + assert.ok( text && text.nodeType === 3, "preliminary: found text" ); + assert.ok( nonContained, "preliminary: found non-descendant" ); + assert.ok( jQuery.contains( container, element ), "child" ); + assert.ok( jQuery.contains( container.parentNode, element ), "grandchild" ); + assert.ok( jQuery.contains( container, text ), "text child" ); + assert.ok( jQuery.contains( container.parentNode, text ), "text grandchild" ); + assert.ok( !jQuery.contains( container, container ), "self" ); + assert.ok( !jQuery.contains( element, container ), "parent" ); + assert.ok( !jQuery.contains( container, nonContained ), "non-descendant" ); + assert.ok( !jQuery.contains( container, document ), "document" ); + assert.ok( !jQuery.contains( container, document.documentElement ), "documentElement (negative)" ); + assert.ok( !jQuery.contains( container, null ), "Passing null does not throw an error" ); + assert.ok( jQuery.contains( document, document.documentElement ), "documentElement (positive)" ); + assert.ok( jQuery.contains( document, element ), "document container (positive)" ); + assert.ok( !jQuery.contains( document, detached ), "document container (negative)" ); +} ); + +QUnit.test( "jQuery.contains in SVG (jQuery trac-10832)", function( assert ) { + assert.expect( 4 ); + + var svg = jQuery( + "<svg xmlns='http://www.w3.org/2000/svg' version='1.1' height='1' width='1'>" + + "<g><circle cx='1' cy='1' r='1' /></g>" + + "</svg>" + ).appendTo( "#qunit-fixture" )[ 0 ]; + + assert.ok( jQuery.contains( svg, svg.firstChild ), "root child" ); + assert.ok( jQuery.contains( svg.firstChild, svg.firstChild.firstChild ), "element child" ); + assert.ok( jQuery.contains( svg, svg.firstChild.firstChild ), "root granchild" ); + assert.ok( !jQuery.contains( svg.firstChild.firstChild, svg.firstChild ), + "parent (negative)" ); +} ); + +QUnit.testUnlessIE( "jQuery.contains within <template/> doesn't throw (gh-5147)", function( assert ) { + assert.expect( 1 ); + + var template = jQuery( "<template><div><div class='a'></div></div></template>" ), + a = jQuery( template[ 0 ].content ).find( ".a" ); + + template.appendTo( "#qunit-fixture" ); + + jQuery.contains( a[ 0 ].ownerDocument, a[ 0 ] ); + + assert.ok( true, "Didn't throw" ); +} ); + diff --git a/test/unit/selector.js b/test/unit/selector.js index 720d2ddf5..0057fe57a 100644 --- a/test/unit/selector.js +++ b/test/unit/selector.js @@ -1853,61 +1853,6 @@ testIframe( } ); -QUnit.test( "jQuery.contains", function( assert ) { - assert.expect( 16 ); - - var container = document.getElementById( "nonnodes" ), - element = container.firstChild, - text = element.nextSibling, - nonContained = container.nextSibling, - detached = document.createElement( "a" ); - assert.ok( element && element.nodeType === 1, "preliminary: found element" ); - assert.ok( text && text.nodeType === 3, "preliminary: found text" ); - assert.ok( nonContained, "preliminary: found non-descendant" ); - assert.ok( jQuery.contains( container, element ), "child" ); - assert.ok( jQuery.contains( container.parentNode, element ), "grandchild" ); - assert.ok( jQuery.contains( container, text ), "text child" ); - assert.ok( jQuery.contains( container.parentNode, text ), "text grandchild" ); - assert.ok( !jQuery.contains( container, container ), "self" ); - assert.ok( !jQuery.contains( element, container ), "parent" ); - assert.ok( !jQuery.contains( container, nonContained ), "non-descendant" ); - assert.ok( !jQuery.contains( container, document ), "document" ); - assert.ok( !jQuery.contains( container, document.documentElement ), "documentElement (negative)" ); - assert.ok( !jQuery.contains( container, null ), "Passing null does not throw an error" ); - assert.ok( jQuery.contains( document, document.documentElement ), "documentElement (positive)" ); - assert.ok( jQuery.contains( document, element ), "document container (positive)" ); - assert.ok( !jQuery.contains( document, detached ), "document container (negative)" ); -} ); - -QUnit.test( "jQuery.contains in SVG (jQuery trac-10832)", function( assert ) { - assert.expect( 4 ); - - var svg = jQuery( - "<svg xmlns='http://www.w3.org/2000/svg' version='1.1' height='1' width='1'>" + - "<g><circle cx='1' cy='1' r='1' /></g>" + - "</svg>" - ).appendTo( "#qunit-fixture" )[ 0 ]; - - assert.ok( jQuery.contains( svg, svg.firstChild ), "root child" ); - assert.ok( jQuery.contains( svg.firstChild, svg.firstChild.firstChild ), "element child" ); - assert.ok( jQuery.contains( svg, svg.firstChild.firstChild ), "root granchild" ); - assert.ok( !jQuery.contains( svg.firstChild.firstChild, svg.firstChild ), - "parent (negative)" ); -} ); - -QUnit.testUnlessIE( "jQuery.contains within <template/> doesn't throw (gh-5147)", function( assert ) { - assert.expect( 1 ); - - var template = jQuery( "<template><div><div class='a'></div></div></template>" ), - a = jQuery( template[ 0 ].content ).find( ".a" ); - - template.appendTo( "#qunit-fixture" ); - - jQuery.contains( a[ 0 ].ownerDocument, a[ 0 ] ); - - assert.ok( true, "Didn't throw" ); -} ); - QUnit.test( "find in document fragments", function( assert ) { assert.expect( 1 ); |