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").andSelf().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 ] ).andSelf().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-seperated expressions
75 ok( jQuery("#en").is("[lang=\"en\"],[lang=\"de\"]"), "Comma-seperated; Check for lang attribute: Expect en or de" );
76 ok( jQuery("#en").is("[lang=\"de\"],[lang=\"en\"]"), "Comma-seperated; Check for lang attribute: Expect en or de" );
77 ok( jQuery("#en").is("[lang=\"en\"] , [lang=\"de\"]"), "Comma-seperated; Check for lang attribute: Expect en or de" );
78 ok( jQuery("#en").is("[lang=\"de\"] , [lang=\"en\"]"), "Comma-seperated; Check for lang attribute: Expect en or de" );
81 test("is() against window|document (#10178)", function() {
83 ok( !jQuery(window).is("a"), "Checking is on a window does not throw an exception" );
84 ok( !jQuery(document).is("a"), "Checking is on a document does not throw an exception" );
87 test("is(jQuery)", function() {
89 ok( jQuery("#form").is( jQuery("form") ), "Check for element: A form is a form" );
90 ok( !jQuery("#form").is( jQuery("div") ), "Check for element: A form is not a div" );
91 ok( jQuery("#mark").is( jQuery(".blog") ), "Check for class: Expected class 'blog'" );
92 ok( !jQuery("#mark").is( jQuery(".link") ), "Check for class: Did not expect class 'link'" );
93 ok( jQuery("#simon").is( jQuery(".blog.link") ), "Check for multiple classes: Expected classes 'blog' and 'link'" );
94 ok( !jQuery("#simon").is( jQuery(".blogTest") ), "Check for multiple classes: Expected classes 'blog' and 'link', but not 'blogTest'" );
95 ok( jQuery("#en").is( jQuery("[lang=\"en\"]") ), "Check for attribute: Expected attribute lang to be 'en'" );
96 ok( !jQuery("#en").is( jQuery("[lang=\"de\"]") ), "Check for attribute: Expected attribute lang to be 'en', not 'de'" );
97 ok( jQuery("#text1").is( jQuery("[type=\"text\"]") ), "Check for attribute: Expected attribute type to be 'text'" );
98 ok( !jQuery("#text1").is( jQuery("[type=\"radio\"]") ), "Check for attribute: Expected attribute type to be 'text', not 'radio'" );
99 ok( !jQuery("#text1").is( jQuery("input:disabled") ), "Check for pseudoclass: Expected not disabled" );
100 ok( jQuery("#radio2").is( jQuery("input:checked") ), "Check for pseudoclass: Expected to be checked" );
101 ok( !jQuery("#radio1").is( jQuery("input:checked") ), "Check for pseudoclass: Expected not checked" );
104 ok( jQuery("#form").is( jQuery("form")[0] ), "Check for element: A form is a form" );
105 ok( !jQuery("#form").is( jQuery("div")[0] ), "Check for element: A form is not a div" );
106 ok( jQuery("#mark").is( jQuery(".blog")[0] ), "Check for class: Expected class 'blog'" );
107 ok( !jQuery("#mark").is( jQuery(".link")[0] ), "Check for class: Did not expect class 'link'" );
108 ok( jQuery("#simon").is( jQuery(".blog.link")[0] ), "Check for multiple classes: Expected classes 'blog' and 'link'" );
109 ok( !jQuery("#simon").is( jQuery(".blogTest")[0] ), "Check for multiple classes: Expected classes 'blog' and 'link', but not 'blogTest'" );
112 test("is() with :has() selectors", function() {
115 ok( jQuery("#foo").is(":has(p)"), "Check for child: Expected a child 'p' element" );
116 ok( !jQuery("#foo").is(":has(ul)"), "Check for child: Did not expect 'ul' element" );
117 ok( jQuery("#foo").is(":has(p):has(a):has(code)"), "Check for childs: Expected 'p', 'a' and 'code' child elements" );
118 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'" );
120 ok( jQuery("#foo").is( jQuery("div:has(p)") ), "Check for child: Expected a child 'p' element" );
121 ok( !jQuery("#foo").is( jQuery("div:has(ul)") ), "Check for child: Did not expect 'ul' element" );
124 test("is() with positional selectors", function() {
128 "<p id='posp'><a class='firsta' href='#'><em>first</em></a><a class='seconda' href='#'><b>test</b></a><em></em></p>"
129 ).appendTo( "#qunit-fixture" ),
130 isit = function(sel, match, expect) {
131 equal( jQuery( sel ).is( match ), expect, "jQuery('" + sel + "').is('" + match + "')" );
134 isit( "#posp", "#posp:first", true );
135 isit( "#posp", "#posp:eq(2)", false );
136 isit( "#posp", "#posp a:first", false );
138 isit( "#posp .firsta", "#posp a:first", true );
139 isit( "#posp .firsta", "#posp a:last", false );
140 isit( "#posp .firsta", "#posp a:even", true );
141 isit( "#posp .firsta", "#posp a:odd", false );
142 isit( "#posp .firsta", "#posp a:eq(0)", true );
143 isit( "#posp .firsta", "#posp a:eq(9)", false );
144 isit( "#posp .firsta", "#posp em:eq(0)", false );
145 isit( "#posp .firsta", "#posp em:first", false );
146 isit( "#posp .firsta", "#posp:first", false );
148 isit( "#posp .seconda", "#posp a:first", false );
149 isit( "#posp .seconda", "#posp a:last", true );
150 isit( "#posp .seconda", "#posp a:gt(0)", true );
151 isit( "#posp .seconda", "#posp a:lt(5)", true );
152 isit( "#posp .seconda", "#posp a:lt(1)", false );
154 isit( "#posp em", "#posp a:eq(0) em", true );
155 isit( "#posp em", "#posp a:lt(1) em", true );
156 isit( "#posp em", "#posp a:gt(1) em", false );
157 isit( "#posp em", "#posp a:first em", true );
158 isit( "#posp em", "#posp a em:last", true );
159 isit( "#posp em", "#posp a em:eq(2)", false );
161 ok( jQuery("#option1b").is("#select1 option:not(:first)"), "POS inside of :not() (#10970)" );
164 test("index()", function() {
167 equal( jQuery("#text2").index(), 2, "Returns the index of a child amongst its siblings" );
169 equal( jQuery("<div/>").index(), -1, "Node without parent returns -1" );
172 test("index(Object|String|undefined)", function() {
175 var elements = jQuery([window, document]),
176 inputElements = jQuery("#radio1,#radio2,#check1,#check2");
179 equal( elements.index(window), 0, "Check for index of elements" );
180 equal( elements.index(document), 1, "Check for index of elements" );
181 equal( inputElements.index(document.getElementById("radio1")), 0, "Check for index of elements" );
182 equal( inputElements.index(document.getElementById("radio2")), 1, "Check for index of elements" );
183 equal( inputElements.index(document.getElementById("check1")), 2, "Check for index of elements" );
184 equal( inputElements.index(document.getElementById("check2")), 3, "Check for index of elements" );
185 equal( inputElements.index(window), -1, "Check for not found index" );
186 equal( inputElements.index(document), -1, "Check for not found index" );
188 // Passing a jQuery object
189 // enabled since [5500]
190 equal( elements.index( elements ), 0, "Pass in a jQuery object" );
191 equal( elements.index( elements.eq(1) ), 1, "Pass in a jQuery object" );
192 equal( jQuery("#form input[type='radio']").index( jQuery("#radio2") ), 1, "Pass in a jQuery object" );
194 // Passing a selector or nothing
195 // enabled since [6330]
196 equal( jQuery("#text2").index(), 2, "Check for index amongst siblings" );
197 equal( jQuery("#form").children().eq(4).index(), 4, "Check for index amongst siblings" );
198 equal( jQuery("#radio2").index("#form input[type='radio']") , 1, "Check for index within a selector" );
199 equal( jQuery("#form input[type='radio']").index( jQuery("#radio2") ), 1, "Check for index within a selector" );
200 equal( jQuery("#radio2").index("#form input[type='text']") , -1, "Check for index not found within a selector" );
203 test("filter(Selector|undefined)", function() {
205 deepEqual( jQuery("#form input").filter(":checked").get(), q("radio2", "check1"), "filter(String)" );
206 deepEqual( jQuery("p").filter("#ap, #sndp").get(), q("ap", "sndp"), "filter('String, String')" );
207 deepEqual( jQuery("p").filter("#ap,#sndp").get(), q("ap", "sndp"), "filter('String,String')" );
209 deepEqual( jQuery("p").filter(null).get(), [], "filter(null) should return an empty jQuery object");
210 deepEqual( jQuery("p").filter(undefined).get(), [], "filter(undefined) should return an empty jQuery object");
211 deepEqual( jQuery("p").filter(0).get(), [], "filter(0) should return an empty jQuery object");
212 deepEqual( jQuery("p").filter("").get(), [], "filter('') should return an empty jQuery object");
214 // using contents will get comments regular, text, and comment nodes
215 var j = jQuery("#nonnodes").contents();
216 equal( j.filter("span").length, 1, "Check node,textnode,comment to filter the one span" );
217 equal( j.filter("[name]").length, 0, "Check node,textnode,comment to filter the one span" );
220 test("filter(Function)", function() {
223 deepEqual( jQuery("#qunit-fixture p").filter(function() {
224 return !jQuery("a", this).length;
225 }).get(), q("sndp", "first"), "filter(Function)" );
227 deepEqual( jQuery("#qunit-fixture p").filter(function(i, elem) { return !jQuery("a", elem).length; }).get(), q("sndp", "first"), "filter(Function) using arg" );
230 test("filter(Element)", function() {
233 var element = document.getElementById("text1");
234 deepEqual( jQuery("#form input").filter(element).get(), q("text1"), "filter(Element)" );
237 test("filter(Array)", function() {
240 var elements = [ document.getElementById("text1") ];
241 deepEqual( jQuery("#form input").filter(elements).get(), q("text1"), "filter(Element)" );
244 test("filter(jQuery)", function() {
247 var elements = jQuery("#text1");
248 deepEqual( jQuery("#form input").filter(elements).get(), q("text1"), "filter(Element)" );
252 test("filter() with positional selectors", function() {
255 var html = jQuery( "" +
257 "<a class='firsta' href='#'>" +
260 "<a class='seconda' href='#'>" +
264 "</p>" ).appendTo( "#qunit-fixture" ),
265 filterit = function(sel, filter, length) {
266 equal( jQuery( sel ).filter( filter ).length, length, "jQuery( " + sel + " ).filter( " + filter + " )" );
269 filterit( "#posp", "#posp:first", 1);
270 filterit( "#posp", "#posp:eq(2)", 0 );
271 filterit( "#posp", "#posp a:first", 0 );
273 // Keep in mind this is within the selection and
274 // not in relation to other elements (.is() is a different story)
275 filterit( "#posp .firsta", "#posp a:first", 1 );
276 filterit( "#posp .firsta", "#posp a:last", 1 );
277 filterit( "#posp .firsta", "#posp a:last-child", 0 );
278 filterit( "#posp .firsta", "#posp a:even", 1 );
279 filterit( "#posp .firsta", "#posp a:odd", 0 );
280 filterit( "#posp .firsta", "#posp a:eq(0)", 1 );
281 filterit( "#posp .firsta", "#posp a:eq(9)", 0 );
282 filterit( "#posp .firsta", "#posp em:eq(0)", 0 );
283 filterit( "#posp .firsta", "#posp em:first", 0 );
284 filterit( "#posp .firsta", "#posp:first", 0 );
286 filterit( "#posp .seconda", "#posp a:first", 1 );
287 filterit( "#posp .seconda", "#posp em:first", 0 );
288 filterit( "#posp .seconda", "#posp a:last", 1 );
289 filterit( "#posp .seconda", "#posp a:gt(0)", 0 );
290 filterit( "#posp .seconda", "#posp a:lt(5)", 1 );
291 filterit( "#posp .seconda", "#posp a:lt(1)", 1 );
294 test("closest()", function() {
299 deepEqual( jQuery("body").closest("body").get(), q("body"), "closest(body)" );
300 deepEqual( jQuery("body").closest("html").get(), q("html"), "closest(html)" );
301 deepEqual( jQuery("body").closest("div").get(), [], "closest(div)" );
302 deepEqual( jQuery("#qunit-fixture").closest("span,#html").get(), q("html"), "closest(span,#html)" );
304 // Test .closest() limited by the context
305 jq = jQuery("#nothiddendivchild");
306 deepEqual( jq.closest("html", document.body).get(), [], "Context limited." );
307 deepEqual( jq.closest("body", document.body).get(), [], "Context limited." );
308 deepEqual( jq.closest("#nothiddendiv", document.body).get(), q("nothiddendiv"), "Context not reached." );
310 //Test that .closest() returns unique'd set
311 equal( jQuery("#qunit-fixture p").closest("#qunit-fixture").length, 1, "Closest should return a unique set" );
313 // Test on disconnected node
314 equal( jQuery("<div><p></p></div>").find("p").closest("table").length, 0, "Make sure disconnected closest work." );
317 equal( jQuery("<div foo='bar'></div>").closest("[foo]").length, 1, "Disconnected nodes with attribute selector" );
318 equal( jQuery("<div>text</div>").closest("[lang]").length, 0, "Disconnected nodes with text and non-existent attribute selector" );
320 ok( !jQuery(document).closest("#foo").length, "Calling closest on a document fails silently" );
322 jq = jQuery("<div>text</div>");
323 deepEqual( jq.contents().closest("*").get(), jq.get(), "Text node input (#13332)" );
326 test("closest() with positional selectors", function() {
329 deepEqual( jQuery("#qunit-fixture").closest("div:first").get(), [], "closest(div:first)" );
330 deepEqual( jQuery("#qunit-fixture div").closest("body:first div:last").get(), q("fx-tests"), "closest(body:first div:last)" );
333 test("closest(jQuery)", function() {
335 var $child = jQuery("#nothiddendivchild"),
336 $parent = jQuery("#nothiddendiv"),
337 $sibling = jQuery("#foo"),
338 $body = jQuery("body");
339 ok( $child.closest( $parent ).is("#nothiddendiv"), "closest( jQuery('#nothiddendiv') )" );
340 ok( $child.closest( $parent[0] ).is("#nothiddendiv"), "closest( jQuery('#nothiddendiv') ) :: node" );
341 ok( $child.closest( $child ).is("#nothiddendivchild"), "child is included" );
342 ok( $child.closest( $child[0] ).is("#nothiddendivchild"), "child is included :: node" );
343 equal( $child.closest( document.createElement("div") ).length, 0, "created element is not related" );
344 equal( $child.closest( $sibling ).length, 0, "Sibling not a parent of child" );
345 equal( $child.closest( $sibling[0] ).length, 0, "Sibling not a parent of child :: node" );
346 ok( $child.closest( $body.add($parent) ).is("#nothiddendiv"), "Closest ancestor retrieved." );
349 test("not(Selector|undefined)", function() {
351 equal( jQuery("#qunit-fixture > p#ap > a").not("#google").length, 2, "not('selector')" );
352 deepEqual( jQuery("p").not(".result").get(), q("firstp", "ap", "sndp", "en", "sap", "first"), "not('.class')" );
353 deepEqual( jQuery("p").not("#ap, #sndp, .result").get(), q("firstp", "en", "sap", "first"), "not('selector, selector')" );
355 deepEqual( jQuery("#ap *").not("code").get(), q("google", "groups", "anchor1", "mark"), "not('tag selector')" );
356 deepEqual( jQuery("#ap *").not("code, #mark").get(), q("google", "groups", "anchor1"), "not('tag, ID selector')" );
357 deepEqual( jQuery("#ap *").not("#mark, code").get(), q("google", "groups", "anchor1"), "not('ID, tag selector')");
359 var all = jQuery("p").get();
360 deepEqual( jQuery("p").not(null).get(), all, "not(null) should have no effect");
361 deepEqual( jQuery("p").not(undefined).get(), all, "not(undefined) should have no effect");
362 deepEqual( jQuery("p").not(0).get(), all, "not(0) should have no effect");
363 deepEqual( jQuery("p").not("").get(), all, "not('') should have no effect");
366 jQuery("#form option").not("option.emptyopt:contains('Nothing'),optgroup *,[value='1']").get(),
367 q("option1c", "option1d", "option2c", "option2d", "option3c", "option3d", "option3e", "option4d", "option4e", "option5a", "option5b"),
368 "not('complex selector')"
372 test("not(Element)", function() {
375 var selects = jQuery("#form select");
376 deepEqual( selects.not( selects[1] ).get(), q("select1", "select3", "select4", "select5"), "filter out DOM element");
379 test("not(Function)", function() {
382 deepEqual( jQuery("#qunit-fixture p").not(function() { return jQuery("a", this).length; }).get(), q("sndp", "first"), "not(Function)" );
385 test("not(Array)", function() {
388 equal( jQuery("#qunit-fixture > p#ap > a").not(document.getElementById("google")).length, 2, "not(DOMElement)" );
389 equal( jQuery("p").not(document.getElementsByTagName("p")).length, 0, "not(Array-like DOM collection)" );
392 test("not(jQuery)", function() {
395 deepEqual( jQuery("p").not(jQuery("#ap, #sndp, .result")).get(), q("firstp", "en", "sap", "first"), "not(jQuery)" );
398 test("has(Element)", function() {
401 var obj = jQuery("#qunit-fixture").has(jQuery("#sndp")[0]);
402 deepEqual( obj.get(), q("qunit-fixture"), "Keeps elements that have the element as a descendant" );
404 var detached = jQuery("<a><b><i/></b></a>");
405 deepEqual( detached.has( detached.find("i")[0] ).get(), detached.get(), "...Even when detached" );
407 var multipleParent = jQuery("#qunit-fixture, #header").has(jQuery("#sndp")[0]);
408 deepEqual( obj.get(), q("qunit-fixture"), "Does not include elements that do not have the element as a descendant" );
411 test("has(Selector)", function() {
414 var obj = jQuery("#qunit-fixture").has("#sndp");
415 deepEqual( obj.get(), q("qunit-fixture"), "Keeps elements that have any element matching the selector as a descendant" );
417 var detached = jQuery("<a><b><i/></b></a>");
418 deepEqual( detached.has("i").get(), detached.get(), "...Even when detached" );
420 var multipleParent = jQuery("#qunit-fixture, #header").has("#sndp");
421 deepEqual( multipleParent.get(), q("qunit-fixture"), "Does not include elements that do not have the element as a descendant" );
423 multipleParent = jQuery("#select1, #select2, #select3").has("#option1a, #option3a");
424 deepEqual( multipleParent.get(), q("select1", "select3"), "Multiple contexts are checks correctly" );
426 var multipleHas = jQuery("#qunit-fixture").has("#sndp, #first");
427 deepEqual( multipleHas.get(), q("qunit-fixture"), "Only adds elements once" );
430 test("has(Arrayish)", function() {
433 var simple = jQuery("#qunit-fixture").has(jQuery("#sndp"));
434 deepEqual( simple.get(), q("qunit-fixture"), "Keeps elements that have any element in the jQuery list as a descendant" );
436 var detached = jQuery("<a><b><i/></b></a>");
437 deepEqual( detached.has( detached.find("i") ).get(), detached.get(), "...Even when detached" );
439 var multipleParent = jQuery("#qunit-fixture, #header").has(jQuery("#sndp"));
440 deepEqual( multipleParent.get(), q("qunit-fixture"), "Does not include elements that do not have an element in the jQuery list as a descendant" );
442 var multipleHas = jQuery("#qunit-fixture").has(jQuery("#sndp, #first"));
443 deepEqual( simple.get(), q("qunit-fixture"), "Only adds elements once" );
446 test("addBack()", function() {
448 deepEqual( jQuery("#en").siblings().addBack().get(), q("sndp", "en", "sap"), "Check for siblings and self" );
449 deepEqual( jQuery("#foo").children().addBack().get(), q("foo", "sndp", "en", "sap"), "Check for children and self" );
450 deepEqual( jQuery("#sndp, #en").parent().addBack().get(), q("foo","sndp","en"), "Check for parent and self" );
451 deepEqual( jQuery("#groups").parents("p, div").addBack().get(), q("qunit-fixture", "ap", "groups"), "Check for parents and self" );
452 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" );
455 test("siblings([String])", function() {
457 deepEqual( jQuery("#en").siblings().get(), q("sndp", "sap"), "Check for siblings" );
458 deepEqual( jQuery("#nonnodes").contents().eq(1).siblings().get(), q("nonnodesElement"), "Check for text node siblings" );
459 deepEqual( jQuery("#foo").siblings("form, b").get(), q("form", "floatTest", "lengthtest", "name-tests", "testForm"), "Check for multiple filters" );
460 var set = q("sndp", "en", "sap");
461 deepEqual( jQuery("#en, #sndp").siblings().get(), set, "Check for unique results from siblings" );
462 deepEqual( jQuery("#option5a").siblings("option[data-attr]").get(), q("option5c"), "Has attribute selector in siblings (#9261)" );
463 equal( jQuery("<a/>").siblings().length, 0, "Detached elements have no siblings (#11370)" );
466 test("siblings([String]) - jQuery only", function() {
468 deepEqual( jQuery("#sndp").siblings(":has(code)").get(), q("sap"), "Check for filtered siblings (has code child element)" );
469 deepEqual( jQuery("#sndp").siblings(":has(a)").get(), q("en", "sap"), "Check for filtered siblings (has anchor child element)" );
472 test("children([String])", function() {
474 deepEqual( jQuery("#foo").children().get(), q("sndp", "en", "sap"), "Check for children" );
475 deepEqual( jQuery("#foo").children("#en, #sap").get(), q("en", "sap"), "Check for multiple filters" );
478 test("children([String]) - jQuery only", function() {
480 deepEqual( jQuery("#foo").children(":has(code)").get(), q("sndp", "sap"), "Check for filtered children" );
483 test("parent([String])", function() {
488 equal( jQuery("#groups").parent()[0].id, "ap", "Simple parent check" );
489 equal( jQuery("#groups").parent("p")[0].id, "ap", "Filtered parent check" );
490 equal( jQuery("#groups").parent("div").length, 0, "Filtered parent check, no match" );
491 equal( jQuery("#groups").parent("div, p")[0].id, "ap", "Check for multiple filters" );
492 deepEqual( jQuery("#en, #sndp").parent().get(), q("foo"), "Check for unique results from parent" );
494 $el = jQuery("<div>text</div>");
495 deepEqual( $el.contents().parent().get(), $el.get(), "Check for parent of text node (#13265)" );
498 test("parents([String])", function() {
500 equal( jQuery("#groups").parents()[0].id, "ap", "Simple parents check" );
501 deepEqual( jQuery("#nonnodes").contents().eq(1).parents().eq(0).get(), q("nonnodes"), "Text node parents check" );
502 equal( jQuery("#groups").parents("p")[0].id, "ap", "Filtered parents check" );
503 equal( jQuery("#groups").parents("div")[0].id, "qunit-fixture", "Filtered parents check2" );
504 deepEqual( jQuery("#groups").parents("p, div").get(), q("ap", "qunit-fixture"), "Check for multiple filters" );
505 deepEqual( jQuery("#en, #sndp").parents().get(), q("foo", "qunit-fixture", "dl", "body", "html"), "Check for unique results from parents" );
508 test("parentsUntil([String])", function() {
511 var parents = jQuery("#groups").parents();
513 deepEqual( jQuery("#groups").parentsUntil().get(), parents.get(), "parentsUntil with no selector (nextAll)" );
514 deepEqual( jQuery("#groups").parentsUntil(".foo").get(), parents.get(), "parentsUntil with invalid selector (nextAll)" );
515 deepEqual( jQuery("#groups").parentsUntil("#html").get(), parents.slice(0, -1).get(), "Simple parentsUntil check" );
516 equal( jQuery("#groups").parentsUntil("#ap").length, 0, "Simple parentsUntil check" );
517 deepEqual( jQuery("#nonnodes").contents().eq(1).parentsUntil("#html").eq(0).get(), q("nonnodes"), "Text node parentsUntil check" );
518 deepEqual( jQuery("#groups").parentsUntil("#html, #body").get(), parents.slice( 0, 3 ).get(), "Less simple parentsUntil check" );
519 deepEqual( jQuery("#groups").parentsUntil("#html", "div").get(), jQuery("#qunit-fixture").get(), "Filtered parentsUntil check" );
520 deepEqual( jQuery("#groups").parentsUntil("#html", "p,div,dl").get(), parents.slice( 0, 3 ).get(), "Multiple-filtered parentsUntil check" );
521 equal( jQuery("#groups").parentsUntil("#html", "span").length, 0, "Filtered parentsUntil check, no match" );
522 deepEqual( jQuery("#groups, #ap").parentsUntil("#html", "p,div,dl").get(), parents.slice( 0, 3 ).get(), "Multi-source, multiple-filtered parentsUntil check" );
525 test("next([String])", function() {
527 equal( jQuery("#ap").next()[0].id, "foo", "Simple next check" );
528 equal( jQuery("<div>text<a id='element'></a></div>").contents().eq(0).next().attr("id"), "element", "Text node next check" );
529 equal( jQuery("#ap").next("div")[0].id, "foo", "Filtered next check" );
530 equal( jQuery("#ap").next("p").length, 0, "Filtered next check, no match" );
531 equal( jQuery("#ap").next("div, p")[0].id, "foo", "Multiple filters" );
532 equal( jQuery("body").next().length, 0, "Simple next check, no match" );
535 test("prev([String])", function() {
537 equal( jQuery("#foo").prev()[0].id, "ap", "Simple prev check" );
538 deepEqual( jQuery("#nonnodes").contents().eq(1).prev().get(), q("nonnodesElement"), "Text node prev check" );
539 equal( jQuery("#foo").prev("p")[0].id, "ap", "Filtered prev check" );
540 equal( jQuery("#foo").prev("div").length, 0, "Filtered prev check, no match" );
541 equal( jQuery("#foo").prev("p, div")[0].id, "ap", "Multiple filters" );
544 test("nextAll([String])", function() {
547 var elems = jQuery("#form").children();
549 deepEqual( jQuery("#label-for").nextAll().get(), elems.slice(1).get(), "Simple nextAll check" );
550 equal( jQuery("<div>text<a id='element'></a></div>").contents().eq(0).nextAll().attr("id"), "element", "Text node nextAll check" );
551 deepEqual( jQuery("#label-for").nextAll("input").get(), elems.slice(1).filter("input").get(), "Filtered nextAll check" );
552 deepEqual( jQuery("#label-for").nextAll("input,select").get(), elems.slice(1).filter("input,select").get(), "Multiple-filtered nextAll check" );
553 deepEqual( jQuery("#label-for, #hidden1").nextAll("input,select").get(), elems.slice(1).filter("input,select").get(), "Multi-source, multiple-filtered nextAll check" );
556 test("prevAll([String])", function() {
559 var elems = jQuery( jQuery("#form").children().slice(0, 12).get().reverse() );
561 deepEqual( jQuery("#area1").prevAll().get(), elems.get(), "Simple prevAll check" );
562 deepEqual( jQuery("#nonnodes").contents().eq(1).prevAll().get(), q("nonnodesElement"), "Text node prevAll check" );
563 deepEqual( jQuery("#area1").prevAll("input").get(), elems.filter("input").get(), "Filtered prevAll check" );
564 deepEqual( jQuery("#area1").prevAll("input,select").get(), elems.filter("input,select").get(), "Multiple-filtered prevAll check" );
565 deepEqual( jQuery("#area1, #hidden1").prevAll("input,select").get(), elems.filter("input,select").get(), "Multi-source, multiple-filtered prevAll check" );
568 test("nextUntil([String])", function() {
571 var elems = jQuery("#form").children().slice( 2, 12 );
573 deepEqual( jQuery("#text1").nextUntil().get(), jQuery("#text1").nextAll().get(), "nextUntil with no selector (nextAll)" );
574 equal( jQuery("<div>text<a id='element'></a></div>").contents().eq(0).nextUntil().attr("id"), "element", "Text node nextUntil with no selector (nextAll)" );
575 deepEqual( jQuery("#text1").nextUntil(".foo").get(), jQuery("#text1").nextAll().get(), "nextUntil with invalid selector (nextAll)" );
576 deepEqual( jQuery("#text1").nextUntil("#area1").get(), elems.get(), "Simple nextUntil check" );
577 equal( jQuery("#text1").nextUntil("#text2").length, 0, "Simple nextUntil check" );
578 deepEqual( jQuery("#text1").nextUntil("#area1, #radio1").get(), jQuery("#text1").next().get(), "Less simple nextUntil check" );
579 deepEqual( jQuery("#text1").nextUntil("#area1", "input").get(), elems.not("button").get(), "Filtered nextUntil check" );
580 deepEqual( jQuery("#text1").nextUntil("#area1", "button").get(), elems.not("input").get(), "Filtered nextUntil check" );
581 deepEqual( jQuery("#text1").nextUntil("#area1", "button,input").get(), elems.get(), "Multiple-filtered nextUntil check" );
582 equal( jQuery("#text1").nextUntil("#area1", "div").length, 0, "Filtered nextUntil check, no match" );
583 deepEqual( jQuery("#text1, #hidden1").nextUntil("#area1", "button,input").get(), elems.get(), "Multi-source, multiple-filtered nextUntil check" );
585 deepEqual( jQuery("#text1").nextUntil("[class=foo]").get(), jQuery("#text1").nextAll().get(), "Non-element nodes must be skipped, since they have no attributes" );
588 test("prevUntil([String])", function() {
591 var elems = jQuery("#area1").prevAll();
593 deepEqual( jQuery("#area1").prevUntil().get(), elems.get(), "prevUntil with no selector (prevAll)" );
594 deepEqual( jQuery("#nonnodes").contents().eq(1).prevUntil().get(), q("nonnodesElement"), "Text node prevUntil with no selector (prevAll)" );
595 deepEqual( jQuery("#area1").prevUntil(".foo").get(), elems.get(), "prevUntil with invalid selector (prevAll)" );
596 deepEqual( jQuery("#area1").prevUntil("label").get(), elems.slice(0, -1).get(), "Simple prevUntil check" );
597 equal( jQuery("#area1").prevUntil("#button").length, 0, "Simple prevUntil check" );
598 deepEqual( jQuery("#area1").prevUntil("label, #search").get(), jQuery("#area1").prev().get(), "Less simple prevUntil check" );
599 deepEqual( jQuery("#area1").prevUntil("label", "input").get(), elems.slice(0, -1).not("button").get(), "Filtered prevUntil check" );
600 deepEqual( jQuery("#area1").prevUntil("label", "button").get(), elems.slice(0, -1).not("input").get(), "Filtered prevUntil check" );
601 deepEqual( jQuery("#area1").prevUntil("label", "button,input").get(), elems.slice(0, -1).get(), "Multiple-filtered prevUntil check" );
602 equal( jQuery("#area1").prevUntil("label", "div").length, 0, "Filtered prevUntil check, no match" );
603 deepEqual( jQuery("#area1, #hidden1").prevUntil("label", "button,input").get(), elems.slice(0, -1).get(), "Multi-source, multiple-filtered prevUntil check" );
606 test("contents()", function() {
608 equal( jQuery("#ap").contents().length, 9, "Check element contents" );
609 ok( jQuery("#iframe").contents()[0], "Check existance of IFrame document" );
610 var ibody = jQuery("#loadediframe").contents()[0].body;
611 ok( ibody, "Check existance of IFrame body" );
613 equal( jQuery("span", ibody).text(), "span text", "Find span in IFrame and check its text" );
615 jQuery(ibody).append("<div>init text</div>");
616 equal( jQuery("div", ibody).length, 2, "Check the original div and the new div are in IFrame" );
618 equal( jQuery("div", ibody).last().text(), "init text", "Add text to div in IFrame" );
620 jQuery("div", ibody).last().text("div text");
621 equal( jQuery("div", ibody).last().text(), "div text", "Add text to div in IFrame" );
623 jQuery("div", ibody).last().remove();
624 equal( jQuery("div", ibody).length, 1, "Delete the div and check only one div left in IFrame" );
626 equal( jQuery("div", ibody).text(), "span text", "Make sure the correct div is still left after deletion in IFrame" );
628 jQuery("<table/>", ibody).append("<tr><td>cell</td></tr>").appendTo(ibody);
629 jQuery("table", ibody).remove();
630 equal( jQuery("div", ibody).length, 1, "Check for JS error on add and delete of a table in IFrame" );
632 // using contents will get comments regular, text, and comment nodes
633 var c = jQuery("#nonnodes").contents().contents();
634 equal( c.length, 1, "Check node,textnode,comment contents is just one" );
635 equal( c[0].nodeValue, "hi", "Check node,textnode,comment contents is just the one from span" );
638 test("add(String|Element|Array|undefined)", function() {
640 deepEqual( jQuery("#sndp").add("#en").add("#sap").get(), q("sndp", "en", "sap"), "Check elements from document" );
641 deepEqual( jQuery("#sndp").add( jQuery("#en")[0] ).add( jQuery("#sap") ).get(), q("sndp", "en", "sap"), "Check elements from document" );
643 // We no longer support .add(form.elements), unfortunately.
644 // There is no way, in browsers, to reliably determine the difference
645 // between form.elements and form - and doing .add(form) and having it
646 // add the form elements is way to unexpected, so this gets the boot.
647 // ok( jQuery([]).add(jQuery("#form")[0].elements).length >= 13, "Check elements from array" );
649 // For the time being, we're discontinuing support for jQuery(form.elements) since it's ambiguous in IE
650 // use jQuery([]).add(form.elements) instead.
651 //equal( jQuery([]).add(jQuery("#form")[0].elements).length, jQuery(jQuery("#form")[0].elements).length, "Array in constructor must equals array in add()" );
653 var divs = jQuery("<div/>").add("#sndp");
654 ok( divs[0].parentNode, "Sort with the disconnected node last (started with disconnected first)." );
656 divs = jQuery("#sndp").add("<div/>");
657 ok( !divs[1].parentNode, "Sort with the disconnected node last." );
659 var tmp = jQuery("<div/>");
661 var x = jQuery([]).add(jQuery("<p id='x1'>xxx</p>").appendTo(tmp)).add(jQuery("<p id='x2'>xxx</p>").appendTo(tmp));
662 equal( x[0].id, "x1", "Check on-the-fly element1" );
663 equal( x[1].id, "x2", "Check on-the-fly element2" );
665 x = jQuery([]).add(jQuery("<p id='x1'>xxx</p>").appendTo(tmp)[0]).add(jQuery("<p id='x2'>xxx</p>").appendTo(tmp)[0]);
666 equal( x[0].id, "x1", "Check on-the-fly element1" );
667 equal( x[1].id, "x2", "Check on-the-fly element2" );
669 x = jQuery([]).add(jQuery("<p id='x1'>xxx</p>")).add(jQuery("<p id='x2'>xxx</p>"));
670 equal( x[0].id, "x1", "Check on-the-fly element1" );
671 equal( x[1].id, "x2", "Check on-the-fly element2" );
673 x = jQuery([]).add("<p id='x1'>xxx</p>").add("<p id='x2'>xxx</p>");
674 equal( x[0].id, "x1", "Check on-the-fly element1" );
675 equal( x[1].id, "x2", "Check on-the-fly element2" );
678 equal( jQuery([]).add(notDefined).length, 0, "Check that undefined adds nothing" );
680 equal( jQuery([]).add( document.getElementById("form") ).length, 1, "Add a form" );
681 equal( jQuery([]).add( document.getElementById("select1") ).length, 1, "Add a select" );
684 test("add(String, Context)", function() {
687 deepEqual( jQuery( "#firstp" ).add( "#ap" ).get(), q( "firstp", "ap" ), "Add selector to selector " );
688 deepEqual( jQuery( document.getElementById("firstp") ).add( "#ap" ).get(), q( "firstp", "ap" ), "Add gEBId to selector" );
689 deepEqual( jQuery( document.getElementById("firstp") ).add( document.getElementById("ap") ).get(), q( "firstp", "ap" ), "Add gEBId to gEBId" );
691 var ctx = document.getElementById("firstp");
692 deepEqual( jQuery( "#firstp" ).add( "#ap", ctx ).get(), q( "firstp" ), "Add selector to selector " );
693 deepEqual( jQuery( document.getElementById("firstp") ).add( "#ap", ctx ).get(), q( "firstp" ), "Add gEBId to selector, not in context" );
694 deepEqual( jQuery( document.getElementById("firstp") ).add( "#ap", document.getElementsByTagName("body")[0] ).get(), q( "firstp", "ap" ), "Add gEBId to selector, in context" );
697 test("eq('-1') #10616", function() {
699 var $divs = jQuery( "div" );
701 equal( $divs.eq( -1 ).length, 1, "The number -1 returns a selection that has length 1" );
702 equal( $divs.eq( "-1" ).length, 1, "The string '-1' returns a selection that has length 1" );
703 deepEqual( $divs.eq( "-1" ), $divs.eq( -1 ), "String and number -1 match" );
706 test("index(no arg) #10977", function() {
709 var $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>");
710 jQuery("#qunit-fixture").append( $list );
711 strictEqual ( jQuery( "#indextest li.zero" ).first().index() , 0, "No Argument Index Check" );
714 var fragment = document.createDocumentFragment(),
715 div = fragment.appendChild( document.createElement("div") );
717 equal( jQuery( div ).index(), 0, "If jQuery#index called on element whos parent is fragment, it still should work correctly" );