1 module("traversing", { teardown: moduleTeardown });
3 test( "find(String)", function() {
5 equal( jQuery("#foo").find(".blogTest").text(), "Yahoo", "Basic selector" );
8 test( "find(String) under non-elements", function() {
11 var j = jQuery("#nonnodes").contents();
12 equal( j.find("div").length, 0, "Check node,textnode,comment to find zero divs" );
13 equal( j.find("div").addBack().length, 3, "Check node,textnode,comment to find zero divs, but preserves pushStack" );
16 test( "find(leading combinator)", function() {
19 deepEqual( jQuery("#qunit-fixture").find("> div").get(), q( "foo", "nothiddendiv", "moretests", "tabindex-tests", "liveHandlerOrder", "siblingTest", "fx-test-group" ), "find child elements" );
20 deepEqual( jQuery("#qunit-fixture").find("> #foo, > #moretests").get(), q( "foo", "moretests" ), "find child elements" );
21 deepEqual( jQuery("#qunit-fixture").find("> #foo > p").get(), q( "sndp", "en", "sap" ), "find child elements" );
23 deepEqual( jQuery("#siblingTest, #siblingfirst").find("+ *").get(), q( "siblingnext", "fx-test-group" ), "ensure document order" );
26 test( "find(node|jQuery object)", function() {
29 var $foo = jQuery("#foo"),
30 $blog = jQuery(".blogTest"),
31 $first = jQuery("#first"),
32 $two = $blog.add( $first ),
33 $twoMore = jQuery("#ap").add( $blog ),
34 $fooTwo = $foo.add( $blog );
36 equal( $foo.find( $blog ).text(), "Yahoo", "Find with blog jQuery object" );
37 equal( $foo.find( $blog[ 0 ] ).text(), "Yahoo", "Find with blog node" );
38 equal( $foo.find( $first ).length, 0, "#first is not in #foo" );
39 equal( $foo.find( $first[ 0 ]).length, 0, "#first not in #foo (node)" );
40 deepEqual( $foo.find( $two ).get(), $blog.get(), "Find returns only nodes within #foo" );
41 deepEqual( $foo.find( $twoMore ).get(), $blog.get(), "...regardless of order" );
42 ok( $fooTwo.find( $blog ).is(".blogTest"), "Blog is part of the collection, but also within foo" );
43 ok( $fooTwo.find( $blog[ 0 ] ).is(".blogTest"), "Blog is part of the collection, but also within foo(node)" );
45 equal( $two.find( $foo ).length, 0, "Foo is not in two elements" );
46 equal( $two.find( $foo[ 0 ] ).length, 0, "Foo is not in two elements(node)" );
47 equal( $two.find( $first ).length, 0, "first is in the collection and not within two" );
48 equal( $two.find( $first ).length, 0, "first is in the collection and not within two(node)" );
50 equal( $two.find( $foo[ 0 ] ).addBack().length, 2, "find preserves the pushStack, see #12009" );
53 test("is(String|undefined)", function() {
55 ok( jQuery("#form").is("form"), "Check for element: A form must be a form" );
56 ok( !jQuery("#form").is("div"), "Check for element: A form is not a div" );
57 ok( jQuery("#mark").is(".blog"), "Check for class: Expected class 'blog'" );
58 ok( !jQuery("#mark").is(".link"), "Check for class: Did not expect class 'link'" );
59 ok( jQuery("#simon").is(".blog.link"), "Check for multiple classes: Expected classes 'blog' and 'link'" );
60 ok( !jQuery("#simon").is(".blogTest"), "Check for multiple classes: Expected classes 'blog' and 'link', but not 'blogTest'" );
61 ok( jQuery("#en").is("[lang=\"en\"]"), "Check for attribute: Expected attribute lang to be 'en'" );
62 ok( !jQuery("#en").is("[lang=\"de\"]"), "Check for attribute: Expected attribute lang to be 'en', not 'de'" );
63 ok( jQuery("#text1").is("[type=\"text\"]"), "Check for attribute: Expected attribute type to be 'text'" );
64 ok( !jQuery("#text1").is("[type=\"radio\"]"), "Check for attribute: Expected attribute type to be 'text', not 'radio'" );
65 ok( jQuery("#text2").is(":disabled"), "Check for pseudoclass: Expected to be disabled" );
66 ok( !jQuery("#text1").is(":disabled"), "Check for pseudoclass: Expected not disabled" );
67 ok( jQuery("#radio2").is(":checked"), "Check for pseudoclass: Expected to be checked" );
68 ok( !jQuery("#radio1").is(":checked"), "Check for pseudoclass: Expected not checked" );
70 ok( !jQuery("#foo").is(0), "Expected false for an invalid expression - 0" );
71 ok( !jQuery("#foo").is(null), "Expected false for an invalid expression - null" );
72 ok( !jQuery("#foo").is(""), "Expected false for an invalid expression - \"\"" );
73 ok( !jQuery("#foo").is(undefined), "Expected false for an invalid expression - undefined" );
74 ok( !jQuery("#foo").is({ plain: "object" }), "Check passing invalid object" );
76 // test is() with comma-separated expressions
77 ok( jQuery("#en").is("[lang=\"en\"],[lang=\"de\"]"), "Comma-separated; Check for lang attribute: Expect en or de" );
78 ok( jQuery("#en").is("[lang=\"de\"],[lang=\"en\"]"), "Comma-separated; Check for lang attribute: Expect en or de" );
79 ok( jQuery("#en").is("[lang=\"en\"] , [lang=\"de\"]"), "Comma-separated; Check for lang attribute: Expect en or de" );
80 ok( jQuery("#en").is("[lang=\"de\"] , [lang=\"en\"]"), "Comma-separated; Check for lang attribute: Expect en or de" );
83 test("is() against non-elements (#10178)", function() {
87 collection = jQuery( document ),
90 text: document.createTextNode(""),
91 comment: document.createComment(""),
96 "function": function() {}
99 for ( label in nonelements ) {
100 collection[ 0 ] = nonelements[ label ];
101 for ( i = 0; i < tests.length; i++ ) {
103 ok( !collection.is( test ), label + " does not match \"" + test + "\"" );
108 test("is(jQuery)", function() {
110 ok( jQuery("#form").is( jQuery("form") ), "Check for element: A form is a form" );
111 ok( !jQuery("#form").is( jQuery("div") ), "Check for element: A form is not a div" );
112 ok( jQuery("#mark").is( jQuery(".blog") ), "Check for class: Expected class 'blog'" );
113 ok( !jQuery("#mark").is( jQuery(".link") ), "Check for class: Did not expect class 'link'" );
114 ok( jQuery("#simon").is( jQuery(".blog.link") ), "Check for multiple classes: Expected classes 'blog' and 'link'" );
115 ok( !jQuery("#simon").is( jQuery(".blogTest") ), "Check for multiple classes: Expected classes 'blog' and 'link', but not 'blogTest'" );
116 ok( jQuery("#en").is( jQuery("[lang=\"en\"]") ), "Check for attribute: Expected attribute lang to be 'en'" );
117 ok( !jQuery("#en").is( jQuery("[lang=\"de\"]") ), "Check for attribute: Expected attribute lang to be 'en', not 'de'" );
118 ok( jQuery("#text1").is( jQuery("[type=\"text\"]") ), "Check for attribute: Expected attribute type to be 'text'" );
119 ok( !jQuery("#text1").is( jQuery("[type=\"radio\"]") ), "Check for attribute: Expected attribute type to be 'text', not 'radio'" );
120 ok( !jQuery("#text1").is( jQuery("input:disabled") ), "Check for pseudoclass: Expected not disabled" );
121 ok( jQuery("#radio2").is( jQuery("input:checked") ), "Check for pseudoclass: Expected to be checked" );
122 ok( !jQuery("#radio1").is( jQuery("input:checked") ), "Check for pseudoclass: Expected not checked" );
125 ok( jQuery("#form").is( jQuery("#qunit-fixture form")[0] ), "Check for element: A form is a form" );
126 ok( !jQuery("#form").is( jQuery("div")[0] ), "Check for element: A form is not a div" );
127 ok( jQuery("#mark").is( jQuery(".blog")[0] ), "Check for class: Expected class 'blog'" );
128 ok( !jQuery("#mark").is( jQuery(".link")[0] ), "Check for class: Did not expect class 'link'" );
129 ok( jQuery("#simon").is( jQuery(".blog.link")[0] ), "Check for multiple classes: Expected classes 'blog' and 'link'" );
130 ok( !jQuery("#simon").is( jQuery(".blogTest")[0] ), "Check for multiple classes: Expected classes 'blog' and 'link', but not 'blogTest'" );
133 test("is() with :has() selectors", function() {
136 ok( jQuery("#foo").is(":has(p)"), "Check for child: Expected a child 'p' element" );
137 ok( !jQuery("#foo").is(":has(ul)"), "Check for child: Did not expect 'ul' element" );
138 ok( jQuery("#foo").is(":has(p):has(a):has(code)"), "Check for childs: Expected 'p', 'a' and 'code' child elements" );
139 ok( !jQuery("#foo").is(":has(p):has(a):has(code):has(ol)"), "Check for childs: Expected 'p', 'a' and 'code' child elements, but no 'ol'" );
141 ok( jQuery("#foo").is( jQuery("div:has(p)") ), "Check for child: Expected a child 'p' element" );
142 ok( !jQuery("#foo").is( jQuery("div:has(ul)") ), "Check for child: Did not expect 'ul' element" );
145 test("is() with positional selectors", function() {
150 "<p id='posp'><a class='firsta' href='#'><em>first</em></a>" +
151 "<a class='seconda' href='#'><b>test</b></a><em></em></p>"
152 ).appendTo( "#qunit-fixture" ),
153 isit = function( sel, match, expect ) {
155 jQuery( sel ).is( match ),
157 "jQuery('" + sel + "').is('" + match + "')"
161 isit( "#posp", "p:last", true );
162 isit( "#posp", "#posp:first", true );
163 isit( "#posp", "#posp:eq(2)", false );
164 isit( "#posp", "#posp a:first", false );
166 isit( "#posp .firsta", "#posp a:first", true );
167 isit( "#posp .firsta", "#posp a:last", false );
168 isit( "#posp .firsta", "#posp a:even", true );
169 isit( "#posp .firsta", "#posp a:odd", false );
170 isit( "#posp .firsta", "#posp a:eq(0)", true );
171 isit( "#posp .firsta", "#posp a:eq(9)", false );
172 isit( "#posp .firsta", "#posp em:eq(0)", false );
173 isit( "#posp .firsta", "#posp em:first", false );
174 isit( "#posp .firsta", "#posp:first", false );
176 isit( "#posp .seconda", "#posp a:first", false );
177 isit( "#posp .seconda", "#posp a:last", true );
178 isit( "#posp .seconda", "#posp a:gt(0)", true );
179 isit( "#posp .seconda", "#posp a:lt(5)", true );
180 isit( "#posp .seconda", "#posp a:lt(1)", false );
182 isit( "#posp em", "#posp a:eq(0) em", true );
183 isit( "#posp em", "#posp a:lt(1) em", true );
184 isit( "#posp em", "#posp a:gt(1) em", false );
185 isit( "#posp em", "#posp a:first em", true );
186 isit( "#posp em", "#posp a em:last", true );
187 isit( "#posp em", "#posp a em:eq(2)", false );
189 ok( jQuery("#option1b").is("#select1 option:not(:first)"), "POS inside of :not() (#10970)" );
191 ok( jQuery( posp[0] ).is("p:last"), "context constructed from a single node (#13797)" );
192 ok( !jQuery( posp[0] ).find("#firsta").is("a:first"), "context derived from a single node (#13797)" );
195 test("index()", function() {
198 equal( jQuery("#text2").index(), 2, "Returns the index of a child amongst its siblings" );
200 equal( jQuery("<div/>").index(), -1, "Node without parent returns -1" );
203 test("index(Object|String|undefined)", function() {
206 var elements = jQuery([window, document]),
207 inputElements = jQuery("#radio1,#radio2,#check1,#check2");
210 equal( elements.index(window), 0, "Check for index of elements" );
211 equal( elements.index(document), 1, "Check for index of elements" );
212 equal( inputElements.index(document.getElementById("radio1")), 0, "Check for index of elements" );
213 equal( inputElements.index(document.getElementById("radio2")), 1, "Check for index of elements" );
214 equal( inputElements.index(document.getElementById("check1")), 2, "Check for index of elements" );
215 equal( inputElements.index(document.getElementById("check2")), 3, "Check for index of elements" );
216 equal( inputElements.index(window), -1, "Check for not found index" );
217 equal( inputElements.index(document), -1, "Check for not found index" );
219 // Passing a jQuery object
220 // enabled since [5500]
221 equal( elements.index( elements ), 0, "Pass in a jQuery object" );
222 equal( elements.index( elements.eq(1) ), 1, "Pass in a jQuery object" );
223 equal( jQuery("#form input[type='radio']").index( jQuery("#radio2") ), 1, "Pass in a jQuery object" );
225 // Passing a selector or nothing
226 // enabled since [6330]
227 equal( jQuery("#text2").index(), 2, "Check for index amongst siblings" );
228 equal( jQuery("#form").children().eq(4).index(), 4, "Check for index amongst siblings" );
229 equal( jQuery("#radio2").index("#form input[type='radio']") , 1, "Check for index within a selector" );
230 equal( jQuery("#form input[type='radio']").index( jQuery("#radio2") ), 1, "Check for index within a selector" );
231 equal( jQuery("#radio2").index("#form input[type='text']") , -1, "Check for index not found within a selector" );
234 test("filter(Selector|undefined)", function() {
236 deepEqual( jQuery("#form input").filter(":checked").get(), q("radio2", "check1"), "filter(String)" );
237 deepEqual( jQuery("p").filter("#ap, #sndp").get(), q("ap", "sndp"), "filter('String, String')" );
238 deepEqual( jQuery("p").filter("#ap,#sndp").get(), q("ap", "sndp"), "filter('String,String')" );
240 deepEqual( jQuery("p").filter(null).get(), [], "filter(null) should return an empty jQuery object");
241 deepEqual( jQuery("p").filter(undefined).get(), [], "filter(undefined) should return an empty jQuery object");
242 deepEqual( jQuery("p").filter(0).get(), [], "filter(0) should return an empty jQuery object");
243 deepEqual( jQuery("p").filter("").get(), [], "filter('') should return an empty jQuery object");
245 // using contents will get comments regular, text, and comment nodes
246 var j = jQuery("#nonnodes").contents();
247 equal( j.filter("span").length, 1, "Check node,textnode,comment to filter the one span" );
248 equal( j.filter("[name]").length, 0, "Check node,textnode,comment to filter the one span" );
251 test("filter(Function)", function() {
254 deepEqual( jQuery("#qunit-fixture p").filter(function() {
255 return !jQuery("a", this).length;
256 }).get(), q("sndp", "first"), "filter(Function)" );
258 deepEqual( jQuery("#qunit-fixture p").filter(function(i, elem) { return !jQuery("a", elem).length; }).get(), q("sndp", "first"), "filter(Function) using arg" );
261 test("filter(Element)", function() {
264 var element = document.getElementById("text1");
265 deepEqual( jQuery("#form input").filter(element).get(), q("text1"), "filter(Element)" );
268 test("filter(Array)", function() {
271 var elements = [ document.getElementById("text1") ];
272 deepEqual( jQuery("#form input").filter(elements).get(), q("text1"), "filter(Element)" );
275 test("filter(jQuery)", function() {
278 var elements = jQuery("#text1");
279 deepEqual( jQuery("#form input").filter(elements).get(), q("text1"), "filter(Element)" );
283 test("filter() with positional selectors", function() {
286 var filterit = function(sel, filter, length) {
287 equal( jQuery( sel ).filter( filter ).length, length, "jQuery( " + sel + " ).filter( " + filter + " )" );
292 "<a class='firsta' href='#'>" +
295 "<a class='seconda' href='#'>" +
299 "</p>" ).appendTo( "#qunit-fixture" );
301 filterit( "#posp", "#posp:first", 1);
302 filterit( "#posp", "#posp:eq(2)", 0 );
303 filterit( "#posp", "#posp a:first", 0 );
305 // Keep in mind this is within the selection and
306 // not in relation to other elements (.is() is a different story)
307 filterit( "#posp .firsta", "#posp a:first", 1 );
308 filterit( "#posp .firsta", "#posp a:last", 1 );
309 filterit( "#posp .firsta", "#posp a:last-child", 0 );
310 filterit( "#posp .firsta", "#posp a:even", 1 );
311 filterit( "#posp .firsta", "#posp a:odd", 0 );
312 filterit( "#posp .firsta", "#posp a:eq(0)", 1 );
313 filterit( "#posp .firsta", "#posp a:eq(9)", 0 );
314 filterit( "#posp .firsta", "#posp em:eq(0)", 0 );
315 filterit( "#posp .firsta", "#posp em:first", 0 );
316 filterit( "#posp .firsta", "#posp:first", 0 );
318 filterit( "#posp .seconda", "#posp a:first", 1 );
319 filterit( "#posp .seconda", "#posp em:first", 0 );
320 filterit( "#posp .seconda", "#posp a:last", 1 );
321 filterit( "#posp .seconda", "#posp a:gt(0)", 0 );
322 filterit( "#posp .seconda", "#posp a:lt(5)", 1 );
323 filterit( "#posp .seconda", "#posp a:lt(1)", 1 );
326 test("closest()", function() {
331 deepEqual( jQuery("body").closest("body").get(), q("body"), "closest(body)" );
332 deepEqual( jQuery("body").closest("html").get(), q("html"), "closest(html)" );
333 deepEqual( jQuery("body").closest("div").get(), [], "closest(div)" );
334 deepEqual( jQuery("#qunit-fixture").closest("span,#html").get(), q("html"), "closest(span,#html)" );
336 // Test .closest() limited by the context
337 jq = jQuery("#nothiddendivchild");
338 deepEqual( jq.closest("html", document.body).get(), [], "Context limited." );
339 deepEqual( jq.closest("body", document.body).get(), [], "Context limited." );
340 deepEqual( jq.closest("#nothiddendiv", document.body).get(), q("nothiddendiv"), "Context not reached." );
342 //Test that .closest() returns unique'd set
343 equal( jQuery("#qunit-fixture p").closest("#qunit-fixture").length, 1, "Closest should return a unique set" );
345 // Test on disconnected node
346 equal( jQuery("<div><p></p></div>").find("p").closest("table").length, 0, "Make sure disconnected closest work." );
349 equal( jQuery("<div foo='bar'></div>").closest("[foo]").length, 1, "Disconnected nodes with attribute selector" );
350 equal( jQuery("<div>text</div>").closest("[lang]").length, 0, "Disconnected nodes with text and non-existent attribute selector" );
352 ok( !jQuery(document).closest("#foo").length, "Calling closest on a document fails silently" );
354 jq = jQuery("<div>text</div>");
355 deepEqual( jq.contents().closest("*").get(), jq.get(), "Text node input (#13332)" );
358 test("closest() with positional selectors", function() {
361 deepEqual( jQuery("#qunit-fixture").closest("div:first").get(), [], "closest(div:first)" );
362 deepEqual( jQuery("#qunit-fixture div").closest("body:first div:last").get(), q("fx-tests"), "closest(body:first div:last)" );
365 test("closest(jQuery)", function() {
367 var $child = jQuery("#nothiddendivchild"),
368 $parent = jQuery("#nothiddendiv"),
369 $sibling = jQuery("#foo"),
370 $body = jQuery("body");
371 ok( $child.closest( $parent ).is("#nothiddendiv"), "closest( jQuery('#nothiddendiv') )" );
372 ok( $child.closest( $parent[0] ).is("#nothiddendiv"), "closest( jQuery('#nothiddendiv') ) :: node" );
373 ok( $child.closest( $child ).is("#nothiddendivchild"), "child is included" );
374 ok( $child.closest( $child[0] ).is("#nothiddendivchild"), "child is included :: node" );
375 equal( $child.closest( document.createElement("div") ).length, 0, "created element is not related" );
376 equal( $child.closest( $sibling ).length, 0, "Sibling not a parent of child" );
377 equal( $child.closest( $sibling[0] ).length, 0, "Sibling not a parent of child :: node" );
378 ok( $child.closest( $body.add($parent) ).is("#nothiddendiv"), "Closest ancestor retrieved." );
381 test("not(Selector|undefined)", function() {
383 equal( jQuery("#qunit-fixture > p#ap > a").not("#google").length, 2, "not('selector')" );
384 deepEqual( jQuery("p").not(".result").get(), q("firstp", "ap", "sndp", "en", "sap", "first"), "not('.class')" );
385 deepEqual( jQuery("p").not("#ap, #sndp, .result").get(), q("firstp", "en", "sap", "first"), "not('selector, selector')" );
387 deepEqual( jQuery("#ap *").not("code").get(), q("google", "groups", "anchor1", "mark"), "not('tag selector')" );
388 deepEqual( jQuery("#ap *").not("code, #mark").get(), q("google", "groups", "anchor1"), "not('tag, ID selector')" );
389 deepEqual( jQuery("#ap *").not("#mark, code").get(), q("google", "groups", "anchor1"), "not('ID, tag selector')");
391 var all = jQuery("p").get();
392 deepEqual( jQuery("p").not(null).get(), all, "not(null) should have no effect");
393 deepEqual( jQuery("p").not(undefined).get(), all, "not(undefined) should have no effect");
394 deepEqual( jQuery("p").not(0).get(), all, "not(0) should have no effect");
395 deepEqual( jQuery("p").not("").get(), all, "not('') should have no effect");
398 jQuery("#form option").not("option.emptyopt:contains('Nothing'),optgroup *,[value='1']").get(),
399 q("option1c", "option1d", "option2c", "option2d", "option3c", "option3d", "option3e", "option4d", "option4e", "option5a", "option5b"),
400 "not('complex selector')"
404 test("not(Element)", function() {
407 var selects = jQuery("#form select");
408 deepEqual( selects.not( selects[1] ).get(), q("select1", "select3", "select4", "select5"), "filter out DOM element");
411 test("not(Function)", function() {
414 deepEqual( jQuery("#qunit-fixture p").not(function() { return jQuery("a", this).length; }).get(), q("sndp", "first"), "not(Function)" );
417 test("not(Array)", function() {
420 equal( jQuery("#qunit-fixture > p#ap > a").not(document.getElementById("google")).length, 2, "not(DOMElement)" );
421 equal( jQuery("p").not(document.getElementsByTagName("p")).length, 0, "not(Array-like DOM collection)" );
424 test("not(jQuery)", function() {
427 deepEqual( jQuery("p").not(jQuery("#ap, #sndp, .result")).get(), q("firstp", "en", "sap", "first"), "not(jQuery)" );
430 test("has(Element)", function() {
432 var obj, detached, multipleParent;
434 obj = jQuery("#qunit-fixture").has(jQuery("#sndp")[0]);
435 deepEqual( obj.get(), q("qunit-fixture"), "Keeps elements that have the element as a descendant" );
437 detached = jQuery("<a><b><i/></b></a>");
438 deepEqual( detached.has( detached.find("i")[0] ).get(), detached.get(), "...Even when detached" );
440 multipleParent = jQuery("#qunit-fixture, #header").has(jQuery("#sndp")[0]);
441 deepEqual( multipleParent.get(), q("qunit-fixture"), "Does not include elements that do not have the element as a descendant" );
444 test("has(Selector)", function() {
447 var obj, detached, multipleParent, multipleHas;
449 obj = jQuery("#qunit-fixture").has("#sndp");
450 deepEqual( obj.get(), q("qunit-fixture"), "Keeps elements that have any element matching the selector as a descendant" );
452 detached = jQuery("<a><b><i/></b></a>");
453 deepEqual( detached.has("i").get(), detached.get(), "...Even when detached" );
455 multipleParent = jQuery("#qunit-fixture, #header").has("#sndp");
456 deepEqual( multipleParent.get(), q("qunit-fixture"), "Does not include elements that do not have the element as a descendant" );
458 multipleParent = jQuery("#select1, #select2, #select3").has("#option1a, #option3a");
459 deepEqual( multipleParent.get(), q("select1", "select3"), "Multiple contexts are checks correctly" );
461 multipleHas = jQuery("#qunit-fixture").has("#sndp, #first");
462 deepEqual( multipleHas.get(), q("qunit-fixture"), "Only adds elements once" );
465 test("has(Arrayish)", function() {
468 var simple, detached, multipleParent, multipleHas;
470 simple = jQuery("#qunit-fixture").has(jQuery("#sndp"));
471 deepEqual( simple.get(), q("qunit-fixture"), "Keeps elements that have any element in the jQuery list as a descendant" );
473 detached = jQuery("<a><b><i/></b></a>");
474 deepEqual( detached.has( detached.find("i") ).get(), detached.get(), "...Even when detached" );
476 multipleParent = jQuery("#qunit-fixture, #header").has(jQuery("#sndp"));
477 deepEqual( multipleParent.get(), q("qunit-fixture"), "Does not include elements that do not have an element in the jQuery list as a descendant" );
479 multipleHas = jQuery("#qunit-fixture").has(jQuery("#sndp, #first"));
480 deepEqual( multipleHas.get(), q("qunit-fixture"), "Only adds elements once" );
483 test("addBack()", function() {
485 deepEqual( jQuery("#en").siblings().addBack().get(), q("sndp", "en", "sap"), "Check for siblings and self" );
486 deepEqual( jQuery("#foo").children().addBack().get(), q("foo", "sndp", "en", "sap"), "Check for children and self" );
487 deepEqual( jQuery("#sndp, #en").parent().addBack().get(), q("foo","sndp","en"), "Check for parent and self" );
488 deepEqual( jQuery("#groups").parents("p, div").addBack().get(), q("qunit-fixture", "ap", "groups"), "Check for parents and self" );
489 deepEqual( jQuery("#select1 > option").filter(":first-child").addBack(":last-child").get(), q("option1a", "option1d"), "Should contain the last elems plus the *filtered* prior set elements" );
492 test("siblings([String])", function() {
494 deepEqual( jQuery("#en").siblings().get(), q("sndp", "sap"), "Check for siblings" );
495 deepEqual( jQuery("#nonnodes").contents().eq(1).siblings().get(), q("nonnodesElement"), "Check for text node siblings" );
496 deepEqual( jQuery("#foo").siblings("form, b").get(), q("form", "floatTest", "lengthtest", "name-tests", "testForm"), "Check for multiple filters" );
498 var set = q("sndp", "en", "sap");
499 deepEqual( jQuery("#en, #sndp").siblings().get(), set, "Check for unique results from siblings" );
500 deepEqual( jQuery("#option5a").siblings("option[data-attr]").get(), q("option5c"), "Has attribute selector in siblings (#9261)" );
501 equal( jQuery("<a/>").siblings().length, 0, "Detached elements have no siblings (#11370)" );
504 test("siblings([String]) - jQuery only", function() {
506 deepEqual( jQuery("#sndp").siblings(":has(code)").get(), q("sap"), "Check for filtered siblings (has code child element)" );
507 deepEqual( jQuery("#sndp").siblings(":has(a)").get(), q("en", "sap"), "Check for filtered siblings (has anchor child element)" );
510 test("children([String])", function() {
512 deepEqual( jQuery("#foo").children().get(), q("sndp", "en", "sap"), "Check for children" );
513 deepEqual( jQuery("#foo").children("#en, #sap").get(), q("en", "sap"), "Check for multiple filters" );
516 test("children([String]) - jQuery only", function() {
518 deepEqual( jQuery("#foo").children(":has(code)").get(), q("sndp", "sap"), "Check for filtered children" );
521 test("parent([String])", function() {
526 equal( jQuery("#groups").parent()[0].id, "ap", "Simple parent check" );
527 equal( jQuery("#groups").parent("p")[0].id, "ap", "Filtered parent check" );
528 equal( jQuery("#groups").parent("div").length, 0, "Filtered parent check, no match" );
529 equal( jQuery("#groups").parent("div, p")[0].id, "ap", "Check for multiple filters" );
530 deepEqual( jQuery("#en, #sndp").parent().get(), q("foo"), "Check for unique results from parent" );
532 $el = jQuery("<div>text</div>");
533 deepEqual( $el.contents().parent().get(), $el.get(), "Check for parent of text node (#13265)" );
536 test("parents([String])", function() {
538 equal( jQuery("#groups").parents()[0].id, "ap", "Simple parents check" );
539 deepEqual( jQuery("#nonnodes").contents().eq(1).parents().eq(0).get(), q("nonnodes"), "Text node parents check" );
540 equal( jQuery("#groups").parents("p")[0].id, "ap", "Filtered parents check" );
541 equal( jQuery("#groups").parents("div")[0].id, "qunit-fixture", "Filtered parents check2" );
542 deepEqual( jQuery("#groups").parents("p, div").get(), q("ap", "qunit-fixture"), "Check for multiple filters" );
543 deepEqual( jQuery("#en, #sndp").parents().get(), q("foo", "qunit-fixture", "dl", "body", "html"), "Check for unique results from parents" );
546 test("parentsUntil([String])", function() {
549 var parents = jQuery("#groups").parents();
551 deepEqual( jQuery("#groups").parentsUntil().get(), parents.get(), "parentsUntil with no selector (nextAll)" );
552 deepEqual( jQuery("#groups").parentsUntil(".foo").get(), parents.get(), "parentsUntil with invalid selector (nextAll)" );
553 deepEqual( jQuery("#groups").parentsUntil("#html").get(), parents.slice(0, -1).get(), "Simple parentsUntil check" );
554 equal( jQuery("#groups").parentsUntil("#ap").length, 0, "Simple parentsUntil check" );
555 deepEqual( jQuery("#nonnodes").contents().eq(1).parentsUntil("#html").eq(0).get(), q("nonnodes"), "Text node parentsUntil check" );
556 deepEqual( jQuery("#groups").parentsUntil("#html, #body").get(), parents.slice( 0, 3 ).get(), "Less simple parentsUntil check" );
557 deepEqual( jQuery("#groups").parentsUntil("#html", "div").get(), jQuery("#qunit-fixture").get(), "Filtered parentsUntil check" );
558 deepEqual( jQuery("#groups").parentsUntil("#html", "p,div,dl").get(), parents.slice( 0, 3 ).get(), "Multiple-filtered parentsUntil check" );
559 equal( jQuery("#groups").parentsUntil("#html", "span").length, 0, "Filtered parentsUntil check, no match" );
560 deepEqual( jQuery("#groups, #ap").parentsUntil("#html", "p,div,dl").get(), parents.slice( 0, 3 ).get(), "Multi-source, multiple-filtered parentsUntil check" );
563 test("next([String])", function() {
565 equal( jQuery("#ap").next()[0].id, "foo", "Simple next check" );
566 equal( jQuery("<div>text<a id='element'></a></div>").contents().eq(0).next().attr("id"), "element", "Text node next check" );
567 equal( jQuery("#ap").next("div")[0].id, "foo", "Filtered next check" );
568 equal( jQuery("#ap").next("p").length, 0, "Filtered next check, no match" );
569 equal( jQuery("#ap").next("div, p")[0].id, "foo", "Multiple filters" );
570 equal( jQuery("body").next().length, 0, "Simple next check, no match" );
573 test("prev([String])", function() {
575 equal( jQuery("#foo").prev()[0].id, "ap", "Simple prev check" );
576 deepEqual( jQuery("#nonnodes").contents().eq(1).prev().get(), q("nonnodesElement"), "Text node prev check" );
577 equal( jQuery("#foo").prev("p")[0].id, "ap", "Filtered prev check" );
578 equal( jQuery("#foo").prev("div").length, 0, "Filtered prev check, no match" );
579 equal( jQuery("#foo").prev("p, div")[0].id, "ap", "Multiple filters" );
582 test("nextAll([String])", function() {
585 var elems = jQuery("#form").children();
587 deepEqual( jQuery("#label-for").nextAll().get(), elems.slice(1).get(), "Simple nextAll check" );
588 equal( jQuery("<div>text<a id='element'></a></div>").contents().eq(0).nextAll().attr("id"), "element", "Text node nextAll check" );
589 deepEqual( jQuery("#label-for").nextAll("input").get(), elems.slice(1).filter("input").get(), "Filtered nextAll check" );
590 deepEqual( jQuery("#label-for").nextAll("input,select").get(), elems.slice(1).filter("input,select").get(), "Multiple-filtered nextAll check" );
591 deepEqual( jQuery("#label-for, #hidden1").nextAll("input,select").get(), elems.slice(1).filter("input,select").get(), "Multi-source, multiple-filtered nextAll check" );
594 test("prevAll([String])", function() {
597 var elems = jQuery( jQuery("#form").children().slice(0, 12).get().reverse() );
599 deepEqual( jQuery("#area1").prevAll().get(), elems.get(), "Simple prevAll check" );
600 deepEqual( jQuery("#nonnodes").contents().eq(1).prevAll().get(), q("nonnodesElement"), "Text node prevAll check" );
601 deepEqual( jQuery("#area1").prevAll("input").get(), elems.filter("input").get(), "Filtered prevAll check" );
602 deepEqual( jQuery("#area1").prevAll("input,select").get(), elems.filter("input,select").get(), "Multiple-filtered prevAll check" );
603 deepEqual( jQuery("#area1, #hidden1").prevAll("input,select").get(), elems.filter("input,select").get(), "Multi-source, multiple-filtered prevAll check" );
606 test("nextUntil([String])", function() {
609 var elems = jQuery("#form").children().slice( 2, 12 );
611 deepEqual( jQuery("#text1").nextUntil().get(), jQuery("#text1").nextAll().get(), "nextUntil with no selector (nextAll)" );
612 equal( jQuery("<div>text<a id='element'></a></div>").contents().eq(0).nextUntil().attr("id"), "element", "Text node nextUntil with no selector (nextAll)" );
613 deepEqual( jQuery("#text1").nextUntil(".foo").get(), jQuery("#text1").nextAll().get(), "nextUntil with invalid selector (nextAll)" );
614 deepEqual( jQuery("#text1").nextUntil("#area1").get(), elems.get(), "Simple nextUntil check" );
615 equal( jQuery("#text1").nextUntil("#text2").length, 0, "Simple nextUntil check" );
616 deepEqual( jQuery("#text1").nextUntil("#area1, #radio1").get(), jQuery("#text1").next().get(), "Less simple nextUntil check" );
617 deepEqual( jQuery("#text1").nextUntil("#area1", "input").get(), elems.not("button").get(), "Filtered nextUntil check" );
618 deepEqual( jQuery("#text1").nextUntil("#area1", "button").get(), elems.not("input").get(), "Filtered nextUntil check" );
619 deepEqual( jQuery("#text1").nextUntil("#area1", "button,input").get(), elems.get(), "Multiple-filtered nextUntil check" );
620 equal( jQuery("#text1").nextUntil("#area1", "div").length, 0, "Filtered nextUntil check, no match" );
621 deepEqual( jQuery("#text1, #hidden1").nextUntil("#area1", "button,input").get(), elems.get(), "Multi-source, multiple-filtered nextUntil check" );
623 deepEqual( jQuery("#text1").nextUntil("[class=foo]").get(), jQuery("#text1").nextAll().get(), "Non-element nodes must be skipped, since they have no attributes" );
626 test("prevUntil([String])", function() {
629 var elems = jQuery("#area1").prevAll();
631 deepEqual( jQuery("#area1").prevUntil().get(), elems.get(), "prevUntil with no selector (prevAll)" );
632 deepEqual( jQuery("#nonnodes").contents().eq(1).prevUntil().get(), q("nonnodesElement"), "Text node prevUntil with no selector (prevAll)" );
633 deepEqual( jQuery("#area1").prevUntil(".foo").get(), elems.get(), "prevUntil with invalid selector (prevAll)" );
634 deepEqual( jQuery("#area1").prevUntil("label").get(), elems.slice(0, -1).get(), "Simple prevUntil check" );
635 equal( jQuery("#area1").prevUntil("#button").length, 0, "Simple prevUntil check" );
636 deepEqual( jQuery("#area1").prevUntil("label, #search").get(), jQuery("#area1").prev().get(), "Less simple prevUntil check" );
637 deepEqual( jQuery("#area1").prevUntil("label", "input").get(), elems.slice(0, -1).not("button").get(), "Filtered prevUntil check" );
638 deepEqual( jQuery("#area1").prevUntil("label", "button").get(), elems.slice(0, -1).not("input").get(), "Filtered prevUntil check" );
639 deepEqual( jQuery("#area1").prevUntil("label", "button,input").get(), elems.slice(0, -1).get(), "Multiple-filtered prevUntil check" );
640 equal( jQuery("#area1").prevUntil("label", "div").length, 0, "Filtered prevUntil check, no match" );
641 deepEqual( jQuery("#area1, #hidden1").prevUntil("label", "button,input").get(), elems.slice(0, -1).get(), "Multi-source, multiple-filtered prevUntil check" );
644 test("contents()", function() {
648 equal( jQuery("#ap").contents().length, 9, "Check element contents" );
649 ok( jQuery("#iframe").contents()[0], "Check existence of IFrame document" );
650 ibody = jQuery("#loadediframe").contents()[0].body;
651 ok( ibody, "Check existence of IFrame body" );
653 equal( jQuery("span", ibody).text(), "span text", "Find span in IFrame and check its text" );
655 jQuery(ibody).append("<div>init text</div>");
656 equal( jQuery("div", ibody).length, 2, "Check the original div and the new div are in IFrame" );
658 equal( jQuery("div", ibody).last().text(), "init text", "Add text to div in IFrame" );
660 jQuery("div", ibody).last().text("div text");
661 equal( jQuery("div", ibody).last().text(), "div text", "Add text to div in IFrame" );
663 jQuery("div", ibody).last().remove();
664 equal( jQuery("div", ibody).length, 1, "Delete the div and check only one div left in IFrame" );
666 equal( jQuery("div", ibody).text(), "span text", "Make sure the correct div is still left after deletion in IFrame" );
668 jQuery("<table/>", ibody).append("<tr><td>cell</td></tr>").appendTo(ibody);
669 jQuery("table", ibody).remove();
670 equal( jQuery("div", ibody).length, 1, "Check for JS error on add and delete of a table in IFrame" );
672 // using contents will get comments regular, text, and comment nodes
673 c = jQuery("#nonnodes").contents().contents();
674 equal( c.length, 1, "Check node,textnode,comment contents is just one" );
675 equal( c[0].nodeValue, "hi", "Check node,textnode,comment contents is just the one from span" );
678 test("sort direction", function() {
681 var elems = jQuery("#ap, #select1 > *, #moretests > form"),
697 jQuery.each( methodDirections, function( method, reversed ) {
698 var actual = elems[ method ]().get(),
699 forward = jQuery.unique( [].concat( actual ) );
700 deepEqual( actual, reversed ? forward.reverse() : forward, "Correct sort direction for " + method );
704 test("add(String selector)", function() {
710 jQuery("#sndp").add("#en").add("#sap").toArray(),
711 q("sndp", "en", "sap"),
712 "Check elements from document"
715 divs = jQuery("<div/>").add("#sndp");
716 ok( divs[0].parentNode, "Sort with the disconnected node last (started with disconnected first)." );
719 test("add(String selector, String context)", function() {
723 jQuery([]).add("div", "#nothiddendiv").toArray(),
724 q("nothiddendivchild"),
725 "Check elements from document"
729 test("add(String html)", function() {
733 divs = jQuery("#sndp").add("<div/>");
735 ok( !divs[1].parentNode, "Sort with the disconnected node last." );
738 x = jQuery([]).add("<p id='x1'>xxx</p>").add("<p id='x2'>xxx</p>");
739 equal( x[0].id, "x1", "Check detached element1" );
740 equal( x[1].id, "x2", "Check detached element2" );
743 test("add(jQuery)", function() {
747 tmp = jQuery("<div/>");
751 jQuery("<p id='x1'>xxx</p>").appendTo(tmp)
754 jQuery("<p id='x2'>xxx</p>").appendTo(tmp)
757 equal( x[0].id, "x1", "Check element1 in detached parent" );
758 equal( x[1].id, "x2", "Check element2 in detached parent" );
762 jQuery("<p id='x1'>xxx</p>")
765 jQuery("<p id='x2'>xxx</p>")
768 equal( x[0].id, "x1", "Check detached element1" );
769 equal( x[1].id, "x2", "Check detached element2" );
772 test("add(Element)", function() {
776 tmp = jQuery("<div/>");
778 x = jQuery([]).add(jQuery("<p id='x1'>xxx</p>").appendTo(tmp)[0]).add(jQuery("<p id='x2'>xxx</p>").appendTo(tmp)[0]);
779 equal( x[0].id, "x1", "Check on-the-fly element1" );
780 equal( x[1].id, "x2", "Check on-the-fly element2" );
783 test("add(Array elements)", function() {
787 jQuery("#sndp").add( jQuery("#en")[0] ).add( jQuery("#sap") ).toArray(),
788 q("sndp", "en", "sap"),
789 "Check elements from document"
793 test("add(Window)", function() {
796 var frame1 = document.createElement( "iframe" ),
797 frame2 = document.createElement( "iframe" );
799 // This increases window.length and sets window[i] available
800 document.body.appendChild( frame1 );
801 document.body.appendChild( frame2 );
803 // Window is tricky because it is a lot like an array, even Array#slice will
804 // turn it into a multi-item array.
805 equal( jQuery([]).add( window ).length, 1, "Add a window" );
807 document.body.removeChild( frame1 );
808 document.body.removeChild( frame2 );
811 test("add(NodeList|undefined|HTMLFormElement|HTMLSelectElement)", function() {
816 ps = document.getElementsByTagName("p");
818 equal( jQuery([]).add(ps).length, ps.length, "Add a NodeList" );
820 equal( jQuery([]).add(notDefined).length, 0, "Adding undefined adds nothing" );
822 equal( jQuery([]).add( document.getElementById("form") ).length, 1, "Add a form" );
823 equal( jQuery([]).add( document.getElementById("select1") ).length, 1, "Add a select" );
825 // We no longer support .add(form.elements), unfortunately.
826 // There is no way, in browsers, to reliably determine the difference
827 // between form.elements and form - and doing .add(form) and having it
828 // add the form elements is way to unexpected, so this gets the boot.
829 //ok( jQuery([]).add(jQuery("#form")[0].elements).length >= 13, "Check elements from array" );
831 // For the time being, we're discontinuing support for jQuery(form.elements) since it's ambiguous in IE
832 // use jQuery([]).add(form.elements) instead.
833 //equal( jQuery([]).add(jQuery("#form")[0].elements).length, jQuery(jQuery("#form")[0].elements).length, "Array in constructor must equals array in add()" );
836 test("add(String, Context)", function() {
839 deepEqual( jQuery( "#firstp" ).add( "#ap" ).get(), q( "firstp", "ap" ), "Add selector to selector " );
840 deepEqual( jQuery( document.getElementById("firstp") ).add( "#ap" ).get(), q( "firstp", "ap" ), "Add gEBId to selector" );
841 deepEqual( jQuery( document.getElementById("firstp") ).add( document.getElementById("ap") ).get(), q( "firstp", "ap" ), "Add gEBId to gEBId" );
843 var ctx = document.getElementById("firstp");
844 deepEqual( jQuery( "#firstp" ).add( "#ap", ctx ).get(), q( "firstp" ), "Add selector to selector " );
845 deepEqual( jQuery( document.getElementById("firstp") ).add( "#ap", ctx ).get(), q( "firstp" ), "Add gEBId to selector, not in context" );
846 deepEqual( jQuery( document.getElementById("firstp") ).add( "#ap", document.getElementsByTagName("body")[0] ).get(), q( "firstp", "ap" ), "Add gEBId to selector, in context" );
849 test("eq('-1') #10616", function() {
851 var $divs = jQuery( "div" );
853 equal( $divs.eq( -1 ).length, 1, "The number -1 returns a selection that has length 1" );
854 equal( $divs.eq( "-1" ).length, 1, "The string '-1' returns a selection that has length 1" );
855 deepEqual( $divs.eq( "-1" ), $divs.eq( -1 ), "String and number -1 match" );
858 test("index(no arg) #10977", function() {
860 var $list, fragment, div;
862 $list = jQuery("<ul id='indextest'><li class='zero'>THIS ONE</li><li class='one'>a</li><li class='two'>b</li><li class='three'>c</li></ul>");
863 jQuery("#qunit-fixture").append( $list );
864 strictEqual ( jQuery( "#indextest li.zero" ).first().index() , 0, "No Argument Index Check" );
867 fragment = document.createDocumentFragment();
868 div = fragment.appendChild( document.createElement("div") );
870 equal( jQuery( div ).index(), 0, "If jQuery#index called on element whose parent is fragment, it still should work correctly" );
873 test("traversing non-elements with attribute filters (#12523)", function() {
876 var nonnodes = jQuery("#nonnodes").contents();
878 equal( nonnodes.filter("[id]").length, 1, ".filter" );
879 equal( nonnodes.find("[id]").length, 0, ".find" );
880 strictEqual( nonnodes.is("[id]"), true, ".is" );
881 deepEqual( nonnodes.closest("[id='nonnodes']").get(), q("nonnodes"), ".closest" );
882 deepEqual( nonnodes.parents("[id='nonnodes']").get(), q("nonnodes"), ".parents" );