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 $fooTwo = $foo.add( $blog );
35 equal( $foo.find( $blog ).text(), "Yahoo", "Find with blog jQuery object" );
36 equal( $foo.find( $blog[ 0 ] ).text(), "Yahoo", "Find with blog node" );
37 equal( $foo.find( $first ).length, 0, "#first is not in #foo" );
38 equal( $foo.find( $first[ 0 ]).length, 0, "#first not in #foo (node)" );
39 ok( $foo.find( $two ).is(".blogTest"), "Find returns only nodes within #foo" );
40 ok( $fooTwo.find( $blog ).is(".blogTest"), "Blog is part of the collection, but also within foo" );
41 ok( $fooTwo.find( $blog[ 0 ] ).is(".blogTest"), "Blog is part of the collection, but also within foo(node)" );
43 equal( $two.find( $foo ).length, 0, "Foo is not in two elements" );
44 equal( $two.find( $foo[ 0 ] ).length, 0, "Foo is not in two elements(node)" );
45 equal( $two.find( $first ).length, 0, "first is in the collection and not within two" );
46 equal( $two.find( $first ).length, 0, "first is in the collection and not within two(node)" );
48 equal( $two.find( $foo[ 0 ] ).addBack().length, 2, "find preserves the pushStack, see #12009" );
51 test("is(String|undefined)", function() {
53 ok( jQuery("#form").is("form"), "Check for element: A form must be a form" );
54 ok( !jQuery("#form").is("div"), "Check for element: A form is not a div" );
55 ok( jQuery("#mark").is(".blog"), "Check for class: Expected class 'blog'" );
56 ok( !jQuery("#mark").is(".link"), "Check for class: Did not expect class 'link'" );
57 ok( jQuery("#simon").is(".blog.link"), "Check for multiple classes: Expected classes 'blog' and 'link'" );
58 ok( !jQuery("#simon").is(".blogTest"), "Check for multiple classes: Expected classes 'blog' and 'link', but not 'blogTest'" );
59 ok( jQuery("#en").is("[lang=\"en\"]"), "Check for attribute: Expected attribute lang to be 'en'" );
60 ok( !jQuery("#en").is("[lang=\"de\"]"), "Check for attribute: Expected attribute lang to be 'en', not 'de'" );
61 ok( jQuery("#text1").is("[type=\"text\"]"), "Check for attribute: Expected attribute type to be 'text'" );
62 ok( !jQuery("#text1").is("[type=\"radio\"]"), "Check for attribute: Expected attribute type to be 'text', not 'radio'" );
63 ok( jQuery("#text2").is(":disabled"), "Check for pseudoclass: Expected to be disabled" );
64 ok( !jQuery("#text1").is(":disabled"), "Check for pseudoclass: Expected not disabled" );
65 ok( jQuery("#radio2").is(":checked"), "Check for pseudoclass: Expected to be checked" );
66 ok( !jQuery("#radio1").is(":checked"), "Check for pseudoclass: Expected not checked" );
68 ok( !jQuery("#foo").is(0), "Expected false for an invalid expression - 0" );
69 ok( !jQuery("#foo").is(null), "Expected false for an invalid expression - null" );
70 ok( !jQuery("#foo").is(""), "Expected false for an invalid expression - \"\"" );
71 ok( !jQuery("#foo").is(undefined), "Expected false for an invalid expression - undefined" );
72 ok( !jQuery("#foo").is({ plain: "object" }), "Check passing invalid object" );
74 // test is() with comma-separated expressions
75 ok( jQuery("#en").is("[lang=\"en\"],[lang=\"de\"]"), "Comma-separated; Check for lang attribute: Expect en or de" );
76 ok( jQuery("#en").is("[lang=\"de\"],[lang=\"en\"]"), "Comma-separated; Check for lang attribute: Expect en or de" );
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" );
81 test("is() against non-elements (#10178)", function() {
85 collection = jQuery( document ),
88 text: document.createTextNode(""),
89 comment: document.createComment(""),
94 "function": function() {}
97 for ( label in nonelements ) {
98 collection[ 0 ] = nonelements[ label ];
99 for ( i = 0; i < tests.length; i++ ) {
101 ok( !collection.is( test ), label + " does not match \"" + test + "\"" );
106 test("is(jQuery)", function() {
108 ok( jQuery("#form").is( jQuery("form") ), "Check for element: A form is a form" );
109 ok( !jQuery("#form").is( jQuery("div") ), "Check for element: A form is not a div" );
110 ok( jQuery("#mark").is( jQuery(".blog") ), "Check for class: Expected class 'blog'" );
111 ok( !jQuery("#mark").is( jQuery(".link") ), "Check for class: Did not expect class 'link'" );
112 ok( jQuery("#simon").is( jQuery(".blog.link") ), "Check for multiple classes: Expected classes 'blog' and 'link'" );
113 ok( !jQuery("#simon").is( jQuery(".blogTest") ), "Check for multiple classes: Expected classes 'blog' and 'link', but not 'blogTest'" );
114 ok( jQuery("#en").is( jQuery("[lang=\"en\"]") ), "Check for attribute: Expected attribute lang to be 'en'" );
115 ok( !jQuery("#en").is( jQuery("[lang=\"de\"]") ), "Check for attribute: Expected attribute lang to be 'en', not 'de'" );
116 ok( jQuery("#text1").is( jQuery("[type=\"text\"]") ), "Check for attribute: Expected attribute type to be 'text'" );
117 ok( !jQuery("#text1").is( jQuery("[type=\"radio\"]") ), "Check for attribute: Expected attribute type to be 'text', not 'radio'" );
118 ok( !jQuery("#text1").is( jQuery("input:disabled") ), "Check for pseudoclass: Expected not disabled" );
119 ok( jQuery("#radio2").is( jQuery("input:checked") ), "Check for pseudoclass: Expected to be checked" );
120 ok( !jQuery("#radio1").is( jQuery("input:checked") ), "Check for pseudoclass: Expected not checked" );
123 ok( jQuery("#form").is( jQuery("form")[0] ), "Check for element: A form is a form" );
124 ok( !jQuery("#form").is( jQuery("div")[0] ), "Check for element: A form is not a div" );
125 ok( jQuery("#mark").is( jQuery(".blog")[0] ), "Check for class: Expected class 'blog'" );
126 ok( !jQuery("#mark").is( jQuery(".link")[0] ), "Check for class: Did not expect class 'link'" );
127 ok( jQuery("#simon").is( jQuery(".blog.link")[0] ), "Check for multiple classes: Expected classes 'blog' and 'link'" );
128 ok( !jQuery("#simon").is( jQuery(".blogTest")[0] ), "Check for multiple classes: Expected classes 'blog' and 'link', but not 'blogTest'" );
131 test("is() with :has() selectors", function() {
134 ok( jQuery("#foo").is(":has(p)"), "Check for child: Expected a child 'p' element" );
135 ok( !jQuery("#foo").is(":has(ul)"), "Check for child: Did not expect 'ul' element" );
136 ok( jQuery("#foo").is(":has(p):has(a):has(code)"), "Check for childs: Expected 'p', 'a' and 'code' child elements" );
137 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'" );
139 ok( jQuery("#foo").is( jQuery("div:has(p)") ), "Check for child: Expected a child 'p' element" );
140 ok( !jQuery("#foo").is( jQuery("div:has(ul)") ), "Check for child: Did not expect 'ul' element" );
143 test("is() with positional selectors", function() {
148 "<p id='posp'><a class='firsta' href='#'><em>first</em></a>" +
149 "<a class='seconda' href='#'><b>test</b></a><em></em></p>"
150 ).appendTo( "#qunit-fixture" ),
151 isit = function( sel, match, expect ) {
153 jQuery( sel ).is( match ),
155 "jQuery('" + sel + "').is('" + match + "')"
159 isit( "#posp", "p:last", true );
160 isit( "#posp", "#posp:first", true );
161 isit( "#posp", "#posp:eq(2)", false );
162 isit( "#posp", "#posp a:first", false );
164 isit( "#posp .firsta", "#posp a:first", true );
165 isit( "#posp .firsta", "#posp a:last", false );
166 isit( "#posp .firsta", "#posp a:even", true );
167 isit( "#posp .firsta", "#posp a:odd", false );
168 isit( "#posp .firsta", "#posp a:eq(0)", true );
169 isit( "#posp .firsta", "#posp a:eq(9)", false );
170 isit( "#posp .firsta", "#posp em:eq(0)", false );
171 isit( "#posp .firsta", "#posp em:first", false );
172 isit( "#posp .firsta", "#posp:first", false );
174 isit( "#posp .seconda", "#posp a:first", false );
175 isit( "#posp .seconda", "#posp a:last", true );
176 isit( "#posp .seconda", "#posp a:gt(0)", true );
177 isit( "#posp .seconda", "#posp a:lt(5)", true );
178 isit( "#posp .seconda", "#posp a:lt(1)", false );
180 isit( "#posp em", "#posp a:eq(0) em", true );
181 isit( "#posp em", "#posp a:lt(1) em", true );
182 isit( "#posp em", "#posp a:gt(1) em", false );
183 isit( "#posp em", "#posp a:first em", true );
184 isit( "#posp em", "#posp a em:last", true );
185 isit( "#posp em", "#posp a em:eq(2)", false );
187 ok( jQuery("#option1b").is("#select1 option:not(:first)"), "POS inside of :not() (#10970)" );
189 ok( jQuery( posp[0] ).is("p:last"), "context constructed from a single node (#13797)" );
190 ok( !jQuery( posp[0] ).find("#firsta").is("a:first"), "context derived from a single node (#13797)" );
193 test("index()", function() {
196 equal( jQuery("#text2").index(), 2, "Returns the index of a child amongst its siblings" );
198 equal( jQuery("<div/>").index(), -1, "Node without parent returns -1" );
201 test("index(Object|String|undefined)", function() {
204 var elements = jQuery([window, document]),
205 inputElements = jQuery("#radio1,#radio2,#check1,#check2");
208 equal( elements.index(window), 0, "Check for index of elements" );
209 equal( elements.index(document), 1, "Check for index of elements" );
210 equal( inputElements.index(document.getElementById("radio1")), 0, "Check for index of elements" );
211 equal( inputElements.index(document.getElementById("radio2")), 1, "Check for index of elements" );
212 equal( inputElements.index(document.getElementById("check1")), 2, "Check for index of elements" );
213 equal( inputElements.index(document.getElementById("check2")), 3, "Check for index of elements" );
214 equal( inputElements.index(window), -1, "Check for not found index" );
215 equal( inputElements.index(document), -1, "Check for not found index" );
217 // Passing a jQuery object
218 // enabled since [5500]
219 equal( elements.index( elements ), 0, "Pass in a jQuery object" );
220 equal( elements.index( elements.eq(1) ), 1, "Pass in a jQuery object" );
221 equal( jQuery("#form input[type='radio']").index( jQuery("#radio2") ), 1, "Pass in a jQuery object" );
223 // Passing a selector or nothing
224 // enabled since [6330]
225 equal( jQuery("#text2").index(), 2, "Check for index amongst siblings" );
226 equal( jQuery("#form").children().eq(4).index(), 4, "Check for index amongst siblings" );
227 equal( jQuery("#radio2").index("#form input[type='radio']") , 1, "Check for index within a selector" );
228 equal( jQuery("#form input[type='radio']").index( jQuery("#radio2") ), 1, "Check for index within a selector" );
229 equal( jQuery("#radio2").index("#form input[type='text']") , -1, "Check for index not found within a selector" );
232 test("filter(Selector|undefined)", function() {
234 deepEqual( jQuery("#form input").filter(":checked").get(), q("radio2", "check1"), "filter(String)" );
235 deepEqual( jQuery("p").filter("#ap, #sndp").get(), q("ap", "sndp"), "filter('String, String')" );
236 deepEqual( jQuery("p").filter("#ap,#sndp").get(), q("ap", "sndp"), "filter('String,String')" );
238 deepEqual( jQuery("p").filter(null).get(), [], "filter(null) should return an empty jQuery object");
239 deepEqual( jQuery("p").filter(undefined).get(), [], "filter(undefined) should return an empty jQuery object");
240 deepEqual( jQuery("p").filter(0).get(), [], "filter(0) should return an empty jQuery object");
241 deepEqual( jQuery("p").filter("").get(), [], "filter('') should return an empty jQuery object");
243 // using contents will get comments regular, text, and comment nodes
244 var j = jQuery("#nonnodes").contents();
245 equal( j.filter("span").length, 1, "Check node,textnode,comment to filter the one span" );
246 equal( j.filter("[name]").length, 0, "Check node,textnode,comment to filter the one span" );
249 test("filter(Function)", function() {
252 deepEqual( jQuery("#qunit-fixture p").filter(function() {
253 return !jQuery("a", this).length;
254 }).get(), q("sndp", "first"), "filter(Function)" );
256 deepEqual( jQuery("#qunit-fixture p").filter(function(i, elem) { return !jQuery("a", elem).length; }).get(), q("sndp", "first"), "filter(Function) using arg" );
259 test("filter(Element)", function() {
262 var element = document.getElementById("text1");
263 deepEqual( jQuery("#form input").filter(element).get(), q("text1"), "filter(Element)" );
266 test("filter(Array)", function() {
269 var elements = [ document.getElementById("text1") ];
270 deepEqual( jQuery("#form input").filter(elements).get(), q("text1"), "filter(Element)" );
273 test("filter(jQuery)", function() {
276 var elements = jQuery("#text1");
277 deepEqual( jQuery("#form input").filter(elements).get(), q("text1"), "filter(Element)" );
281 test("filter() with positional selectors", function() {
284 var filterit = function(sel, filter, length) {
285 equal( jQuery( sel ).filter( filter ).length, length, "jQuery( " + sel + " ).filter( " + filter + " )" );
290 "<a class='firsta' href='#'>" +
293 "<a class='seconda' href='#'>" +
297 "</p>" ).appendTo( "#qunit-fixture" );
299 filterit( "#posp", "#posp:first", 1);
300 filterit( "#posp", "#posp:eq(2)", 0 );
301 filterit( "#posp", "#posp a:first", 0 );
303 // Keep in mind this is within the selection and
304 // not in relation to other elements (.is() is a different story)
305 filterit( "#posp .firsta", "#posp a:first", 1 );
306 filterit( "#posp .firsta", "#posp a:last", 1 );
307 filterit( "#posp .firsta", "#posp a:last-child", 0 );
308 filterit( "#posp .firsta", "#posp a:even", 1 );
309 filterit( "#posp .firsta", "#posp a:odd", 0 );
310 filterit( "#posp .firsta", "#posp a:eq(0)", 1 );
311 filterit( "#posp .firsta", "#posp a:eq(9)", 0 );
312 filterit( "#posp .firsta", "#posp em:eq(0)", 0 );
313 filterit( "#posp .firsta", "#posp em:first", 0 );
314 filterit( "#posp .firsta", "#posp:first", 0 );
316 filterit( "#posp .seconda", "#posp a:first", 1 );
317 filterit( "#posp .seconda", "#posp em:first", 0 );
318 filterit( "#posp .seconda", "#posp a:last", 1 );
319 filterit( "#posp .seconda", "#posp a:gt(0)", 0 );
320 filterit( "#posp .seconda", "#posp a:lt(5)", 1 );
321 filterit( "#posp .seconda", "#posp a:lt(1)", 1 );
324 test("closest()", function() {
329 deepEqual( jQuery("body").closest("body").get(), q("body"), "closest(body)" );
330 deepEqual( jQuery("body").closest("html").get(), q("html"), "closest(html)" );
331 deepEqual( jQuery("body").closest("div").get(), [], "closest(div)" );
332 deepEqual( jQuery("#qunit-fixture").closest("span,#html").get(), q("html"), "closest(span,#html)" );
334 // Test .closest() limited by the context
335 jq = jQuery("#nothiddendivchild");
336 deepEqual( jq.closest("html", document.body).get(), [], "Context limited." );
337 deepEqual( jq.closest("body", document.body).get(), [], "Context limited." );
338 deepEqual( jq.closest("#nothiddendiv", document.body).get(), q("nothiddendiv"), "Context not reached." );
340 //Test that .closest() returns unique'd set
341 equal( jQuery("#qunit-fixture p").closest("#qunit-fixture").length, 1, "Closest should return a unique set" );
343 // Test on disconnected node
344 equal( jQuery("<div><p></p></div>").find("p").closest("table").length, 0, "Make sure disconnected closest work." );
347 equal( jQuery("<div foo='bar'></div>").closest("[foo]").length, 1, "Disconnected nodes with attribute selector" );
348 equal( jQuery("<div>text</div>").closest("[lang]").length, 0, "Disconnected nodes with text and non-existent attribute selector" );
350 ok( !jQuery(document).closest("#foo").length, "Calling closest on a document fails silently" );
352 jq = jQuery("<div>text</div>");
353 deepEqual( jq.contents().closest("*").get(), jq.get(), "Text node input (#13332)" );
356 test("closest() with positional selectors", function() {
359 deepEqual( jQuery("#qunit-fixture").closest("div:first").get(), [], "closest(div:first)" );
360 deepEqual( jQuery("#qunit-fixture div").closest("body:first div:last").get(), q("fx-tests"), "closest(body:first div:last)" );
363 test("closest(jQuery)", function() {
365 var $child = jQuery("#nothiddendivchild"),
366 $parent = jQuery("#nothiddendiv"),
367 $sibling = jQuery("#foo"),
368 $body = jQuery("body");
369 ok( $child.closest( $parent ).is("#nothiddendiv"), "closest( jQuery('#nothiddendiv') )" );
370 ok( $child.closest( $parent[0] ).is("#nothiddendiv"), "closest( jQuery('#nothiddendiv') ) :: node" );
371 ok( $child.closest( $child ).is("#nothiddendivchild"), "child is included" );
372 ok( $child.closest( $child[0] ).is("#nothiddendivchild"), "child is included :: node" );
373 equal( $child.closest( document.createElement("div") ).length, 0, "created element is not related" );
374 equal( $child.closest( $sibling ).length, 0, "Sibling not a parent of child" );
375 equal( $child.closest( $sibling[0] ).length, 0, "Sibling not a parent of child :: node" );
376 ok( $child.closest( $body.add($parent) ).is("#nothiddendiv"), "Closest ancestor retrieved." );
379 test("not(Selector|undefined)", function() {
381 equal( jQuery("#qunit-fixture > p#ap > a").not("#google").length, 2, "not('selector')" );
382 deepEqual( jQuery("p").not(".result").get(), q("firstp", "ap", "sndp", "en", "sap", "first"), "not('.class')" );
383 deepEqual( jQuery("p").not("#ap, #sndp, .result").get(), q("firstp", "en", "sap", "first"), "not('selector, selector')" );
385 deepEqual( jQuery("#ap *").not("code").get(), q("google", "groups", "anchor1", "mark"), "not('tag selector')" );
386 deepEqual( jQuery("#ap *").not("code, #mark").get(), q("google", "groups", "anchor1"), "not('tag, ID selector')" );
387 deepEqual( jQuery("#ap *").not("#mark, code").get(), q("google", "groups", "anchor1"), "not('ID, tag selector')");
389 var all = jQuery("p").get();
390 deepEqual( jQuery("p").not(null).get(), all, "not(null) should have no effect");
391 deepEqual( jQuery("p").not(undefined).get(), all, "not(undefined) should have no effect");
392 deepEqual( jQuery("p").not(0).get(), all, "not(0) should have no effect");
393 deepEqual( jQuery("p").not("").get(), all, "not('') should have no effect");
396 jQuery("#form option").not("option.emptyopt:contains('Nothing'),optgroup *,[value='1']").get(),
397 q("option1c", "option1d", "option2c", "option2d", "option3c", "option3d", "option3e", "option4d", "option4e", "option5a", "option5b"),
398 "not('complex selector')"
402 test("not(Element)", function() {
405 var selects = jQuery("#form select");
406 deepEqual( selects.not( selects[1] ).get(), q("select1", "select3", "select4", "select5"), "filter out DOM element");
409 test("not(Function)", function() {
412 deepEqual( jQuery("#qunit-fixture p").not(function() { return jQuery("a", this).length; }).get(), q("sndp", "first"), "not(Function)" );
415 test("not(Array)", function() {
418 equal( jQuery("#qunit-fixture > p#ap > a").not(document.getElementById("google")).length, 2, "not(DOMElement)" );
419 equal( jQuery("p").not(document.getElementsByTagName("p")).length, 0, "not(Array-like DOM collection)" );
422 test("not(jQuery)", function() {
425 deepEqual( jQuery("p").not(jQuery("#ap, #sndp, .result")).get(), q("firstp", "en", "sap", "first"), "not(jQuery)" );
428 test("has(Element)", function() {
430 var obj, detached, multipleParent;
432 obj = jQuery("#qunit-fixture").has(jQuery("#sndp")[0]);
433 deepEqual( obj.get(), q("qunit-fixture"), "Keeps elements that have the element as a descendant" );
435 detached = jQuery("<a><b><i/></b></a>");
436 deepEqual( detached.has( detached.find("i")[0] ).get(), detached.get(), "...Even when detached" );
438 multipleParent = jQuery("#qunit-fixture, #header").has(jQuery("#sndp")[0]);
439 deepEqual( multipleParent.get(), q("qunit-fixture"), "Does not include elements that do not have the element as a descendant" );
442 test("has(Selector)", function() {
445 var obj, detached, multipleParent, multipleHas;
447 obj = jQuery("#qunit-fixture").has("#sndp");
448 deepEqual( obj.get(), q("qunit-fixture"), "Keeps elements that have any element matching the selector as a descendant" );
450 detached = jQuery("<a><b><i/></b></a>");
451 deepEqual( detached.has("i").get(), detached.get(), "...Even when detached" );
453 multipleParent = jQuery("#qunit-fixture, #header").has("#sndp");
454 deepEqual( multipleParent.get(), q("qunit-fixture"), "Does not include elements that do not have the element as a descendant" );
456 multipleParent = jQuery("#select1, #select2, #select3").has("#option1a, #option3a");
457 deepEqual( multipleParent.get(), q("select1", "select3"), "Multiple contexts are checks correctly" );
459 multipleHas = jQuery("#qunit-fixture").has("#sndp, #first");
460 deepEqual( multipleHas.get(), q("qunit-fixture"), "Only adds elements once" );
463 test("has(Arrayish)", function() {
466 var simple, detached, multipleParent, multipleHas;
468 simple = jQuery("#qunit-fixture").has(jQuery("#sndp"));
469 deepEqual( simple.get(), q("qunit-fixture"), "Keeps elements that have any element in the jQuery list as a descendant" );
471 detached = jQuery("<a><b><i/></b></a>");
472 deepEqual( detached.has( detached.find("i") ).get(), detached.get(), "...Even when detached" );
474 multipleParent = jQuery("#qunit-fixture, #header").has(jQuery("#sndp"));
475 deepEqual( multipleParent.get(), q("qunit-fixture"), "Does not include elements that do not have an element in the jQuery list as a descendant" );
477 multipleHas = jQuery("#qunit-fixture").has(jQuery("#sndp, #first"));
478 deepEqual( multipleHas.get(), q("qunit-fixture"), "Only adds elements once" );
481 test("addBack()", function() {
483 deepEqual( jQuery("#en").siblings().addBack().get(), q("sndp", "en", "sap"), "Check for siblings and self" );
484 deepEqual( jQuery("#foo").children().addBack().get(), q("foo", "sndp", "en", "sap"), "Check for children and self" );
485 deepEqual( jQuery("#sndp, #en").parent().addBack().get(), q("foo","sndp","en"), "Check for parent and self" );
486 deepEqual( jQuery("#groups").parents("p, div").addBack().get(), q("qunit-fixture", "ap", "groups"), "Check for parents and self" );
487 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" );
490 test("siblings([String])", function() {
492 deepEqual( jQuery("#en").siblings().get(), q("sndp", "sap"), "Check for siblings" );
493 deepEqual( jQuery("#nonnodes").contents().eq(1).siblings().get(), q("nonnodesElement"), "Check for text node siblings" );
494 deepEqual( jQuery("#foo").siblings("form, b").get(), q("form", "floatTest", "lengthtest", "name-tests", "testForm"), "Check for multiple filters" );
496 var set = q("sndp", "en", "sap");
497 deepEqual( jQuery("#en, #sndp").siblings().get(), set, "Check for unique results from siblings" );
498 deepEqual( jQuery("#option5a").siblings("option[data-attr]").get(), q("option5c"), "Has attribute selector in siblings (#9261)" );
499 equal( jQuery("<a/>").siblings().length, 0, "Detached elements have no siblings (#11370)" );
502 test("siblings([String]) - jQuery only", function() {
504 deepEqual( jQuery("#sndp").siblings(":has(code)").get(), q("sap"), "Check for filtered siblings (has code child element)" );
505 deepEqual( jQuery("#sndp").siblings(":has(a)").get(), q("en", "sap"), "Check for filtered siblings (has anchor child element)" );
508 test("children([String])", function() {
510 deepEqual( jQuery("#foo").children().get(), q("sndp", "en", "sap"), "Check for children" );
511 deepEqual( jQuery("#foo").children("#en, #sap").get(), q("en", "sap"), "Check for multiple filters" );
514 test("children([String]) - jQuery only", function() {
516 deepEqual( jQuery("#foo").children(":has(code)").get(), q("sndp", "sap"), "Check for filtered children" );
519 test("parent([String])", function() {
524 equal( jQuery("#groups").parent()[0].id, "ap", "Simple parent check" );
525 equal( jQuery("#groups").parent("p")[0].id, "ap", "Filtered parent check" );
526 equal( jQuery("#groups").parent("div").length, 0, "Filtered parent check, no match" );
527 equal( jQuery("#groups").parent("div, p")[0].id, "ap", "Check for multiple filters" );
528 deepEqual( jQuery("#en, #sndp").parent().get(), q("foo"), "Check for unique results from parent" );
530 $el = jQuery("<div>text</div>");
531 deepEqual( $el.contents().parent().get(), $el.get(), "Check for parent of text node (#13265)" );
534 test("parents([String])", function() {
536 equal( jQuery("#groups").parents()[0].id, "ap", "Simple parents check" );
537 deepEqual( jQuery("#nonnodes").contents().eq(1).parents().eq(0).get(), q("nonnodes"), "Text node parents check" );
538 equal( jQuery("#groups").parents("p")[0].id, "ap", "Filtered parents check" );
539 equal( jQuery("#groups").parents("div")[0].id, "qunit-fixture", "Filtered parents check2" );
540 deepEqual( jQuery("#groups").parents("p, div").get(), q("ap", "qunit-fixture"), "Check for multiple filters" );
541 deepEqual( jQuery("#en, #sndp").parents().get(), q("foo", "qunit-fixture", "dl", "body", "html"), "Check for unique results from parents" );
544 test("parentsUntil([String])", function() {
547 var parents = jQuery("#groups").parents();
549 deepEqual( jQuery("#groups").parentsUntil().get(), parents.get(), "parentsUntil with no selector (nextAll)" );
550 deepEqual( jQuery("#groups").parentsUntil(".foo").get(), parents.get(), "parentsUntil with invalid selector (nextAll)" );
551 deepEqual( jQuery("#groups").parentsUntil("#html").get(), parents.slice(0, -1).get(), "Simple parentsUntil check" );
552 equal( jQuery("#groups").parentsUntil("#ap").length, 0, "Simple parentsUntil check" );
553 deepEqual( jQuery("#nonnodes").contents().eq(1).parentsUntil("#html").eq(0).get(), q("nonnodes"), "Text node parentsUntil check" );
554 deepEqual( jQuery("#groups").parentsUntil("#html, #body").get(), parents.slice( 0, 3 ).get(), "Less simple parentsUntil check" );
555 deepEqual( jQuery("#groups").parentsUntil("#html", "div").get(), jQuery("#qunit-fixture").get(), "Filtered parentsUntil check" );
556 deepEqual( jQuery("#groups").parentsUntil("#html", "p,div,dl").get(), parents.slice( 0, 3 ).get(), "Multiple-filtered parentsUntil check" );
557 equal( jQuery("#groups").parentsUntil("#html", "span").length, 0, "Filtered parentsUntil check, no match" );
558 deepEqual( jQuery("#groups, #ap").parentsUntil("#html", "p,div,dl").get(), parents.slice( 0, 3 ).get(), "Multi-source, multiple-filtered parentsUntil check" );
561 test("next([String])", function() {
563 equal( jQuery("#ap").next()[0].id, "foo", "Simple next check" );
564 equal( jQuery("<div>text<a id='element'></a></div>").contents().eq(0).next().attr("id"), "element", "Text node next check" );
565 equal( jQuery("#ap").next("div")[0].id, "foo", "Filtered next check" );
566 equal( jQuery("#ap").next("p").length, 0, "Filtered next check, no match" );
567 equal( jQuery("#ap").next("div, p")[0].id, "foo", "Multiple filters" );
568 equal( jQuery("body").next().length, 0, "Simple next check, no match" );
571 test("prev([String])", function() {
573 equal( jQuery("#foo").prev()[0].id, "ap", "Simple prev check" );
574 deepEqual( jQuery("#nonnodes").contents().eq(1).prev().get(), q("nonnodesElement"), "Text node prev check" );
575 equal( jQuery("#foo").prev("p")[0].id, "ap", "Filtered prev check" );
576 equal( jQuery("#foo").prev("div").length, 0, "Filtered prev check, no match" );
577 equal( jQuery("#foo").prev("p, div")[0].id, "ap", "Multiple filters" );
580 test("nextAll([String])", function() {
583 var elems = jQuery("#form").children();
585 deepEqual( jQuery("#label-for").nextAll().get(), elems.slice(1).get(), "Simple nextAll check" );
586 equal( jQuery("<div>text<a id='element'></a></div>").contents().eq(0).nextAll().attr("id"), "element", "Text node nextAll check" );
587 deepEqual( jQuery("#label-for").nextAll("input").get(), elems.slice(1).filter("input").get(), "Filtered nextAll check" );
588 deepEqual( jQuery("#label-for").nextAll("input,select").get(), elems.slice(1).filter("input,select").get(), "Multiple-filtered nextAll check" );
589 deepEqual( jQuery("#label-for, #hidden1").nextAll("input,select").get(), elems.slice(1).filter("input,select").get(), "Multi-source, multiple-filtered nextAll check" );
592 test("prevAll([String])", function() {
595 var elems = jQuery( jQuery("#form").children().slice(0, 12).get().reverse() );
597 deepEqual( jQuery("#area1").prevAll().get(), elems.get(), "Simple prevAll check" );
598 deepEqual( jQuery("#nonnodes").contents().eq(1).prevAll().get(), q("nonnodesElement"), "Text node prevAll check" );
599 deepEqual( jQuery("#area1").prevAll("input").get(), elems.filter("input").get(), "Filtered prevAll check" );
600 deepEqual( jQuery("#area1").prevAll("input,select").get(), elems.filter("input,select").get(), "Multiple-filtered prevAll check" );
601 deepEqual( jQuery("#area1, #hidden1").prevAll("input,select").get(), elems.filter("input,select").get(), "Multi-source, multiple-filtered prevAll check" );
604 test("nextUntil([String])", function() {
607 var elems = jQuery("#form").children().slice( 2, 12 );
609 deepEqual( jQuery("#text1").nextUntil().get(), jQuery("#text1").nextAll().get(), "nextUntil with no selector (nextAll)" );
610 equal( jQuery("<div>text<a id='element'></a></div>").contents().eq(0).nextUntil().attr("id"), "element", "Text node nextUntil with no selector (nextAll)" );
611 deepEqual( jQuery("#text1").nextUntil(".foo").get(), jQuery("#text1").nextAll().get(), "nextUntil with invalid selector (nextAll)" );
612 deepEqual( jQuery("#text1").nextUntil("#area1").get(), elems.get(), "Simple nextUntil check" );
613 equal( jQuery("#text1").nextUntil("#text2").length, 0, "Simple nextUntil check" );
614 deepEqual( jQuery("#text1").nextUntil("#area1, #radio1").get(), jQuery("#text1").next().get(), "Less simple nextUntil check" );
615 deepEqual( jQuery("#text1").nextUntil("#area1", "input").get(), elems.not("button").get(), "Filtered nextUntil check" );
616 deepEqual( jQuery("#text1").nextUntil("#area1", "button").get(), elems.not("input").get(), "Filtered nextUntil check" );
617 deepEqual( jQuery("#text1").nextUntil("#area1", "button,input").get(), elems.get(), "Multiple-filtered nextUntil check" );
618 equal( jQuery("#text1").nextUntil("#area1", "div").length, 0, "Filtered nextUntil check, no match" );
619 deepEqual( jQuery("#text1, #hidden1").nextUntil("#area1", "button,input").get(), elems.get(), "Multi-source, multiple-filtered nextUntil check" );
621 deepEqual( jQuery("#text1").nextUntil("[class=foo]").get(), jQuery("#text1").nextAll().get(), "Non-element nodes must be skipped, since they have no attributes" );
624 test("prevUntil([String])", function() {
627 var elems = jQuery("#area1").prevAll();
629 deepEqual( jQuery("#area1").prevUntil().get(), elems.get(), "prevUntil with no selector (prevAll)" );
630 deepEqual( jQuery("#nonnodes").contents().eq(1).prevUntil().get(), q("nonnodesElement"), "Text node prevUntil with no selector (prevAll)" );
631 deepEqual( jQuery("#area1").prevUntil(".foo").get(), elems.get(), "prevUntil with invalid selector (prevAll)" );
632 deepEqual( jQuery("#area1").prevUntil("label").get(), elems.slice(0, -1).get(), "Simple prevUntil check" );
633 equal( jQuery("#area1").prevUntil("#button").length, 0, "Simple prevUntil check" );
634 deepEqual( jQuery("#area1").prevUntil("label, #search").get(), jQuery("#area1").prev().get(), "Less simple prevUntil check" );
635 deepEqual( jQuery("#area1").prevUntil("label", "input").get(), elems.slice(0, -1).not("button").get(), "Filtered prevUntil check" );
636 deepEqual( jQuery("#area1").prevUntil("label", "button").get(), elems.slice(0, -1).not("input").get(), "Filtered prevUntil check" );
637 deepEqual( jQuery("#area1").prevUntil("label", "button,input").get(), elems.slice(0, -1).get(), "Multiple-filtered prevUntil check" );
638 equal( jQuery("#area1").prevUntil("label", "div").length, 0, "Filtered prevUntil check, no match" );
639 deepEqual( jQuery("#area1, #hidden1").prevUntil("label", "button,input").get(), elems.slice(0, -1).get(), "Multi-source, multiple-filtered prevUntil check" );
642 test("contents()", function() {
646 equal( jQuery("#ap").contents().length, 9, "Check element contents" );
647 ok( jQuery("#iframe").contents()[0], "Check existence of IFrame document" );
648 ibody = jQuery("#loadediframe").contents()[0].body;
649 ok( ibody, "Check existence of IFrame body" );
651 equal( jQuery("span", ibody).text(), "span text", "Find span in IFrame and check its text" );
653 jQuery(ibody).append("<div>init text</div>");
654 equal( jQuery("div", ibody).length, 2, "Check the original div and the new div are in IFrame" );
656 equal( jQuery("div", ibody).last().text(), "init text", "Add text to div in IFrame" );
658 jQuery("div", ibody).last().text("div text");
659 equal( jQuery("div", ibody).last().text(), "div text", "Add text to div in IFrame" );
661 jQuery("div", ibody).last().remove();
662 equal( jQuery("div", ibody).length, 1, "Delete the div and check only one div left in IFrame" );
664 equal( jQuery("div", ibody).text(), "span text", "Make sure the correct div is still left after deletion in IFrame" );
666 jQuery("<table/>", ibody).append("<tr><td>cell</td></tr>").appendTo(ibody);
667 jQuery("table", ibody).remove();
668 equal( jQuery("div", ibody).length, 1, "Check for JS error on add and delete of a table in IFrame" );
670 // using contents will get comments regular, text, and comment nodes
671 c = jQuery("#nonnodes").contents().contents();
672 equal( c.length, 1, "Check node,textnode,comment contents is just one" );
673 equal( c[0].nodeValue, "hi", "Check node,textnode,comment contents is just the one from span" );
676 test("sort direction", function() {
679 var elems = jQuery("#ap, #select1 > *, #moretests > form"),
695 jQuery.each( methodDirections, function( method, reversed ) {
696 var actual = elems[ method ]().get(),
697 forward = jQuery.unique( [].concat( actual ) );
698 deepEqual( actual, reversed ? forward.reverse() : forward, "Correct sort direction for " + method );
702 test("add(String selector)", function() {
708 jQuery("#sndp").add("#en").add("#sap").toArray(),
709 q("sndp", "en", "sap"),
710 "Check elements from document"
713 divs = jQuery("<div/>").add("#sndp");
714 ok( divs[0].parentNode, "Sort with the disconnected node last (started with disconnected first)." );
717 test("add(String selector, String context)", function() {
721 jQuery([]).add("div", "#nothiddendiv").toArray(),
722 q("nothiddendivchild"),
723 "Check elements from document"
727 test("add(String html)", function() {
731 divs = jQuery("#sndp").add("<div/>");
733 ok( !divs[1].parentNode, "Sort with the disconnected node last." );
736 x = jQuery([]).add("<p id='x1'>xxx</p>").add("<p id='x2'>xxx</p>");
737 equal( x[0].id, "x1", "Check detached element1" );
738 equal( x[1].id, "x2", "Check detached element2" );
741 test("add(jQuery)", function() {
745 tmp = jQuery("<div/>");
749 jQuery("<p id='x1'>xxx</p>").appendTo(tmp)
752 jQuery("<p id='x2'>xxx</p>").appendTo(tmp)
755 equal( x[0].id, "x1", "Check element1 in detached parent" );
756 equal( x[1].id, "x2", "Check element2 in detached parent" );
760 jQuery("<p id='x1'>xxx</p>")
763 jQuery("<p id='x2'>xxx</p>")
766 equal( x[0].id, "x1", "Check detached element1" );
767 equal( x[1].id, "x2", "Check detached element2" );
770 test("add(Element)", function() {
774 tmp = jQuery("<div/>");
776 x = jQuery([]).add(jQuery("<p id='x1'>xxx</p>").appendTo(tmp)[0]).add(jQuery("<p id='x2'>xxx</p>").appendTo(tmp)[0]);
777 equal( x[0].id, "x1", "Check on-the-fly element1" );
778 equal( x[1].id, "x2", "Check on-the-fly element2" );
781 test("add(Array elements)", function() {
785 jQuery("#sndp").add( jQuery("#en")[0] ).add( jQuery("#sap") ).toArray(),
786 q("sndp", "en", "sap"),
787 "Check elements from document"
791 test("add(Window)", function() {
794 var frame1 = document.createElement( "iframe" ),
795 frame2 = document.createElement( "iframe" );
797 // This increases window.length and sets window[i] available
798 document.body.appendChild( frame1 );
799 document.body.appendChild( frame2 );
801 // Window is tricky because it is a lot like an array, even Array#slice will
802 // turn it into a multi-item array.
803 equal( jQuery([]).add( window ).length, 1, "Add a window" );
805 document.body.removeChild( frame1 );
806 document.body.removeChild( frame2 );
809 test("add(NodeList|undefined|HTMLFormElement|HTMLSelectElement)", function() {
814 ps = document.getElementsByTagName("p");
816 equal( jQuery([]).add(ps).length, ps.length, "Add a NodeList" );
818 equal( jQuery([]).add(notDefined).length, 0, "Adding undefined adds nothing" );
820 equal( jQuery([]).add( document.getElementById("form") ).length, 1, "Add a form" );
821 equal( jQuery([]).add( document.getElementById("select1") ).length, 1, "Add a select" );
823 // We no longer support .add(form.elements), unfortunately.
824 // There is no way, in browsers, to reliably determine the difference
825 // between form.elements and form - and doing .add(form) and having it
826 // add the form elements is way to unexpected, so this gets the boot.
827 //ok( jQuery([]).add(jQuery("#form")[0].elements).length >= 13, "Check elements from array" );
829 // For the time being, we're discontinuing support for jQuery(form.elements) since it's ambiguous in IE
830 // use jQuery([]).add(form.elements) instead.
831 //equal( jQuery([]).add(jQuery("#form")[0].elements).length, jQuery(jQuery("#form")[0].elements).length, "Array in constructor must equals array in add()" );
834 test("add(String, Context)", function() {
837 deepEqual( jQuery( "#firstp" ).add( "#ap" ).get(), q( "firstp", "ap" ), "Add selector to selector " );
838 deepEqual( jQuery( document.getElementById("firstp") ).add( "#ap" ).get(), q( "firstp", "ap" ), "Add gEBId to selector" );
839 deepEqual( jQuery( document.getElementById("firstp") ).add( document.getElementById("ap") ).get(), q( "firstp", "ap" ), "Add gEBId to gEBId" );
841 var ctx = document.getElementById("firstp");
842 deepEqual( jQuery( "#firstp" ).add( "#ap", ctx ).get(), q( "firstp" ), "Add selector to selector " );
843 deepEqual( jQuery( document.getElementById("firstp") ).add( "#ap", ctx ).get(), q( "firstp" ), "Add gEBId to selector, not in context" );
844 deepEqual( jQuery( document.getElementById("firstp") ).add( "#ap", document.getElementsByTagName("body")[0] ).get(), q( "firstp", "ap" ), "Add gEBId to selector, in context" );
847 test("eq('-1') #10616", function() {
849 var $divs = jQuery( "div" );
851 equal( $divs.eq( -1 ).length, 1, "The number -1 returns a selection that has length 1" );
852 equal( $divs.eq( "-1" ).length, 1, "The string '-1' returns a selection that has length 1" );
853 deepEqual( $divs.eq( "-1" ), $divs.eq( -1 ), "String and number -1 match" );
856 test("index(no arg) #10977", function() {
858 var $list, fragment, div;
860 $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>");
861 jQuery("#qunit-fixture").append( $list );
862 strictEqual ( jQuery( "#indextest li.zero" ).first().index() , 0, "No Argument Index Check" );
865 fragment = document.createDocumentFragment();
866 div = fragment.appendChild( document.createElement("div") );
868 equal( jQuery( div ).index(), 0, "If jQuery#index called on element whose parent is fragment, it still should work correctly" );
871 test("traversing non-elements with attribute filters (#12523)", function() {
874 var nonnodes = jQuery("#nonnodes").contents();
876 equal( nonnodes.filter("[id]").length, 1, ".filter" );
877 equal( nonnodes.find("[id]").length, 0, ".find" );
878 strictEqual( nonnodes.is("[id]"), true, ".is" );
879 deepEqual( nonnodes.closest("[id='nonnodes']").get(), q("nonnodes"), ".closest" );
880 deepEqual( nonnodes.parents("[id='nonnodes']").get(), q("nonnodes"), ".parents" );