Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit024d871

Browse files
authored
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; movingit from the `selector` the `core` module resolves the issue.Closesgh-5167
1 parentc909d6b commit024d871

File tree

7 files changed

+70
-74
lines changed

7 files changed

+70
-74
lines changed

‎src/core.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,20 @@ jQuery.extend( {
314314
return!rhtmlSuffix.test(namespace||docElem&&docElem.nodeName||"HTML");
315315
},
316316

317+
// Note: an element does not contain itself
318+
contains:function(a,b){
319+
varbup=b&&b.parentNode;
320+
321+
returna===bup||!!(bup&&bup.nodeType===1&&(
322+
323+
// Support: IE 9 - 11+
324+
// IE doesn't have `contains` on SVG.
325+
a.contains ?
326+
a.contains(bup) :
327+
a.compareDocumentPosition&&a.compareDocumentPosition(bup)&16
328+
));
329+
},
330+
317331
merge:function(first,second){
318332
varlen=+second.length,
319333
j=0,

‎src/core/isAttached.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
importjQueryfrom"../core.js";
22
importdocumentElementfrom"../var/documentElement.js";
33

4-
import"../selector/contains.js";// jQuery.contains
5-
64
varisAttached=function(elem){
75
returnjQuery.contains(elem.ownerDocument,elem)||
86
elem.getRootNode(composed)===elem.ownerDocument;

‎src/selector-native.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import documentElement from "./var/documentElement.js";
3030
importwhitespacefrom"./var/whitespace.js";
3131

3232
// The following utils are attached directly to the jQuery object.
33-
import"./selector/contains.js";
3433
import"./selector/escapeSelector.js";
3534
import"./selector/uniqueSort.js";
3635

‎src/selector.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import isIE from "./var/isIE.js";
1212
importsupportfrom"./selector/support.js";
1313

1414
// The following utils are attached directly to the jQuery object.
15-
import"./selector/contains.js";
1615
import"./selector/escapeSelector.js";
1716
import"./selector/uniqueSort.js";
1817

‎src/selector/contains.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

‎test/unit/core.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,3 +1557,59 @@ QUnit[ includesModule( "deferred" ) ? "test" : "skip" ]( "jQuery.readyException
15571557
thrownewError("Error in jQuery ready");
15581558
});
15591559
});
1560+
1561+
QUnit.test("jQuery.contains",function(assert){
1562+
assert.expect(16);
1563+
1564+
varcontainer=document.getElementById("nonnodes"),
1565+
element=container.firstChild,
1566+
text=element.nextSibling,
1567+
nonContained=container.nextSibling,
1568+
detached=document.createElement("a");
1569+
assert.ok(element&&element.nodeType===1,"preliminary: found element");
1570+
assert.ok(text&&text.nodeType===3,"preliminary: found text");
1571+
assert.ok(nonContained,"preliminary: found non-descendant");
1572+
assert.ok(jQuery.contains(container,element),"child");
1573+
assert.ok(jQuery.contains(container.parentNode,element),"grandchild");
1574+
assert.ok(jQuery.contains(container,text),"text child");
1575+
assert.ok(jQuery.contains(container.parentNode,text),"text grandchild");
1576+
assert.ok(!jQuery.contains(container,container),"self");
1577+
assert.ok(!jQuery.contains(element,container),"parent");
1578+
assert.ok(!jQuery.contains(container,nonContained),"non-descendant");
1579+
assert.ok(!jQuery.contains(container,document),"document");
1580+
assert.ok(!jQuery.contains(container,document.documentElement),"documentElement (negative)");
1581+
assert.ok(!jQuery.contains(container,null),"Passing null does not throw an error");
1582+
assert.ok(jQuery.contains(document,document.documentElement),"documentElement (positive)");
1583+
assert.ok(jQuery.contains(document,element),"document container (positive)");
1584+
assert.ok(!jQuery.contains(document,detached),"document container (negative)");
1585+
});
1586+
1587+
QUnit.test("jQuery.contains in SVG (jQuery trac-10832)",function(assert){
1588+
assert.expect(4);
1589+
1590+
varsvg=jQuery(
1591+
"<svg xmlns='http://www.w3.org/2000/svg' version='1.1' height='1' width='1'>"+
1592+
"<g><circle cx='1' cy='1' r='1' /></g>"+
1593+
"</svg>"
1594+
).appendTo("#qunit-fixture")[0];
1595+
1596+
assert.ok(jQuery.contains(svg,svg.firstChild),"root child");
1597+
assert.ok(jQuery.contains(svg.firstChild,svg.firstChild.firstChild),"element child");
1598+
assert.ok(jQuery.contains(svg,svg.firstChild.firstChild),"root granchild");
1599+
assert.ok(!jQuery.contains(svg.firstChild.firstChild,svg.firstChild),
1600+
"parent (negative)");
1601+
});
1602+
1603+
QUnit.testUnlessIE("jQuery.contains within <template/> doesn't throw (gh-5147)",function(assert){
1604+
assert.expect(1);
1605+
1606+
vartemplate=jQuery("<template><div><div class='a'></div></div></template>"),
1607+
a=jQuery(template[0].content).find(".a");
1608+
1609+
template.appendTo("#qunit-fixture");
1610+
1611+
jQuery.contains(a[0].ownerDocument,a[0]);
1612+
1613+
assert.ok(true,"Didn't throw");
1614+
});
1615+

‎test/unit/selector.js

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1853,61 +1853,6 @@ testIframe(
18531853
}
18541854
);
18551855

1856-
QUnit.test("jQuery.contains",function(assert){
1857-
assert.expect(16);
1858-
1859-
varcontainer=document.getElementById("nonnodes"),
1860-
element=container.firstChild,
1861-
text=element.nextSibling,
1862-
nonContained=container.nextSibling,
1863-
detached=document.createElement("a");
1864-
assert.ok(element&&element.nodeType===1,"preliminary: found element");
1865-
assert.ok(text&&text.nodeType===3,"preliminary: found text");
1866-
assert.ok(nonContained,"preliminary: found non-descendant");
1867-
assert.ok(jQuery.contains(container,element),"child");
1868-
assert.ok(jQuery.contains(container.parentNode,element),"grandchild");
1869-
assert.ok(jQuery.contains(container,text),"text child");
1870-
assert.ok(jQuery.contains(container.parentNode,text),"text grandchild");
1871-
assert.ok(!jQuery.contains(container,container),"self");
1872-
assert.ok(!jQuery.contains(element,container),"parent");
1873-
assert.ok(!jQuery.contains(container,nonContained),"non-descendant");
1874-
assert.ok(!jQuery.contains(container,document),"document");
1875-
assert.ok(!jQuery.contains(container,document.documentElement),"documentElement (negative)");
1876-
assert.ok(!jQuery.contains(container,null),"Passing null does not throw an error");
1877-
assert.ok(jQuery.contains(document,document.documentElement),"documentElement (positive)");
1878-
assert.ok(jQuery.contains(document,element),"document container (positive)");
1879-
assert.ok(!jQuery.contains(document,detached),"document container (negative)");
1880-
});
1881-
1882-
QUnit.test("jQuery.contains in SVG (jQuery trac-10832)",function(assert){
1883-
assert.expect(4);
1884-
1885-
varsvg=jQuery(
1886-
"<svg xmlns='http://www.w3.org/2000/svg' version='1.1' height='1' width='1'>"+
1887-
"<g><circle cx='1' cy='1' r='1' /></g>"+
1888-
"</svg>"
1889-
).appendTo("#qunit-fixture")[0];
1890-
1891-
assert.ok(jQuery.contains(svg,svg.firstChild),"root child");
1892-
assert.ok(jQuery.contains(svg.firstChild,svg.firstChild.firstChild),"element child");
1893-
assert.ok(jQuery.contains(svg,svg.firstChild.firstChild),"root granchild");
1894-
assert.ok(!jQuery.contains(svg.firstChild.firstChild,svg.firstChild),
1895-
"parent (negative)");
1896-
});
1897-
1898-
QUnit.testUnlessIE("jQuery.contains within <template/> doesn't throw (gh-5147)",function(assert){
1899-
assert.expect(1);
1900-
1901-
vartemplate=jQuery("<template><div><div class='a'></div></div></template>"),
1902-
a=jQuery(template[0].content).find(".a");
1903-
1904-
template.appendTo("#qunit-fixture");
1905-
1906-
jQuery.contains(a[0].ownerDocument,a[0]);
1907-
1908-
assert.ok(true,"Didn't throw");
1909-
});
1910-
19111856
QUnit.test("find in document fragments",function(assert){
19121857
assert.expect(1);
19131858

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp