module("traversing", { teardown: moduleTeardown }); test( "find(String)", function() { expect( 1 ); equal( jQuery("#foo").find(".blogTest").text(), "Yahoo", "Basic selector" ); }); test( "find(String) under non-elements", function() { expect( 2 ); var j = jQuery("#nonnodes").contents(); equal( j.find("div").length, 0, "Check node,textnode,comment to find zero divs" ); equal( j.find("div").addBack().length, 3, "Check node,textnode,comment to find zero divs, but preserves pushStack" ); }); test( "find(leading combinator)", function() { expect( 4 ); deepEqual( jQuery("#qunit-fixture").find("> div").get(), q( "foo", "nothiddendiv", "moretests", "tabindex-tests", "liveHandlerOrder", "siblingTest", "fx-test-group" ), "find child elements" ); deepEqual( jQuery("#qunit-fixture").find("> #foo, > #moretests").get(), q( "foo", "moretests" ), "find child elements" ); deepEqual( jQuery("#qunit-fixture").find("> #foo > p").get(), q( "sndp", "en", "sap" ), "find child elements" ); deepEqual( jQuery("#siblingTest, #siblingfirst").find("+ *").get(), q( "siblingnext", "fx-test-group" ), "ensure document order" ); }); test( "find(node|jQuery object)", function() { expect( 13 ); var $foo = jQuery("#foo"), $blog = jQuery(".blogTest"), $first = jQuery("#first"), $two = $blog.add( $first ), $twoMore = jQuery("#ap").add( $blog ), $fooTwo = $foo.add( $blog ); equal( $foo.find( $blog ).text(), "Yahoo", "Find with blog jQuery object" ); equal( $foo.find( $blog[ 0 ] ).text(), "Yahoo", "Find with blog node" ); equal( $foo.find( $first ).length, 0, "#first is not in #foo" ); equal( $foo.find( $first[ 0 ]).length, 0, "#first not in #foo (node)" ); deepEqual( $foo.find( $two ).get(), $blog.get(), "Find returns only nodes within #foo" ); deepEqual( $foo.find( $twoMore ).get(), $blog.get(), "...regardless of order" ); ok( $fooTwo.find( $blog ).is(".blogTest"), "Blog is part of the collection, but also within foo" ); ok( $fooTwo.find( $blog[ 0 ] ).is(".blogTest"), "Blog is part of the collection, but also within foo(node)" ); equal( $two.find( $foo ).length, 0, "Foo is not in two elements" ); equal( $two.find( $foo[ 0 ] ).length, 0, "Foo is not in two elements(node)" ); equal( $two.find( $first ).length, 0, "first is in the collection and not within two" ); equal( $two.find( $first ).length, 0, "first is in the collection and not within two(node)" ); equal( $two.find( $foo[ 0 ] ).addBack().length, 2, "find preserves the pushStack, see #12009" ); }); test("is(String|undefined)", function() { expect(23); ok( jQuery("#form").is("form"), "Check for element: A form must be a form" ); ok( !jQuery("#form").is("div"), "Check for element: A form is not a div" ); ok( jQuery("#mark").is(".blog"), "Check for class: Expected class 'blog'" ); ok( !jQuery("#mark").is(".link"), "Check for class: Did not expect class 'link'" ); ok( jQuery("#simon").is(".blog.link"), "Check for multiple classes: Expected classes 'blog' and 'link'" ); ok( !jQuery("#simon").is(".blogTest"), "Check for multiple classes: Expected classes 'blog' and 'link', but not 'blogTest'" ); ok( jQuery("#en").is("[lang=\"en\"]"), "Check for attribute: Expected attribute lang to be 'en'" ); ok( !jQuery("#en").is("[lang=\"de\"]"), "Check for attribute: Expected attribute lang to be 'en', not 'de'" ); ok( jQuery("#text1").is("[type=\"text\"]"), "Check for attribute: Expected attribute type to be 'text'" ); ok( !jQuery("#text1").is("[type=\"radio\"]"), "Check for attribute: Expected attribute type to be 'text', not 'radio'" ); ok( jQuery("#text2").is(":disabled"), "Check for pseudoclass: Expected to be disabled" ); ok( !jQuery("#text1").is(":disabled"), "Check for pseudoclass: Expected not disabled" ); ok( jQuery("#radio2").is(":checked"), "Check for pseudoclass: Expected to be checked" ); ok( !jQuery("#radio1").is(":checked"), "Check for pseudoclass: Expected not checked" ); ok( !jQuery("#foo").is(0), "Expected false for an invalid expression - 0" ); ok( !jQuery("#foo").is(null), "Expected false for an invalid expression - null" ); ok( !jQuery("#foo").is(""), "Expected false for an invalid expression - \"\"" ); ok( !jQuery("#foo").is(undefined), "Expected false for an invalid expression - undefined" ); ok( !jQuery("#foo").is({ plain: "object" }), "Check passing invalid object" ); // test is() with comma-separated expressions ok( jQuery("#en").is("[lang=\"en\"],[lang=\"de\"]"), "Comma-separated; Check for lang attribute: Expect en or de" ); ok( jQuery("#en").is("[lang=\"de\"],[lang=\"en\"]"), "Comma-separated; Check for lang attribute: Expect en or de" ); ok( jQuery("#en").is("[lang=\"en\"] , [lang=\"de\"]"), "Comma-separated; Check for lang attribute: Expect en or de" ); ok( jQuery("#en").is("[lang=\"de\"] , [lang=\"en\"]"), "Comma-separated; Check for lang attribute: Expect en or de" ); }); test("is() against non-elements (#10178)", function() { expect(14); var label, i, test, collection = jQuery( document ), tests = [ "a", "*" ], nonelements = { text: document.createTextNode(""), comment: document.createComment(""), document: document, window: window, array: [], "plain object": {}, "function": function() {} }; for ( label in nonelements ) { collection[ 0 ] = nonelements[ label ]; for ( i = 0; i < tests.length; i++ ) { test = tests[ i ]; ok( !collection.is( test ), label + " does not match \"" + test + "\"" ); } } }); test("is(jQuery)", function() { expect(19); ok( jQuery("#form").is( jQuery("form") ), "Check for element: A form is a form" ); ok( !jQuery("#form").is( jQuery("div") ), "Check for element: A form is not a div" ); ok( jQuery("#mark").is( jQuery(".blog") ), "Check for class: Expected class 'blog'" ); ok( !jQuery("#mark").is( jQuery(".link") ), "Check for class: Did not expect class 'link'" ); ok( jQuery("#simon").is( jQuery(".blog.link") ), "Check for multiple classes: Expected classes 'blog' and 'link'" ); ok( !jQuery("#simon").is( jQuery(".blogTest") ), "Check for multiple classes: Expected classes 'blog' and 'link', but not 'blogTest'" ); ok( jQuery("#en").is( jQuery("[lang=\"en\"]") ), "Check for attribute: Expected attribute lang to be 'en'" ); ok( !jQuery("#en").is( jQuery("[lang=\"de\"]") ), "Check for attribute: Expected attribute lang to be 'en', not 'de'" ); ok( jQuery("#text1").is( jQuery("[type=\"text\"]") ), "Check for attribute: Expected attribute type to be 'text'" ); ok( !jQuery("#text1").is( jQuery("[type=\"radio\"]") ), "Check for attribute: Expected attribute type to be 'text', not 'radio'" ); ok( !jQuery("#text1").is( jQuery("input:disabled") ), "Check for pseudoclass: Expected not disabled" ); ok( jQuery("#radio2").is( jQuery("input:checked") ), "Check for pseudoclass: Expected to be checked" ); ok( !jQuery("#radio1").is( jQuery("input:checked") ), "Check for pseudoclass: Expected not checked" ); // Some raw elements ok( jQuery("#form").is( jQuery("#qunit-fixture form")[0] ), "Check for element: A form is a form" ); ok( !jQuery("#form").is( jQuery("div")[0] ), "Check for element: A form is not a div" ); ok( jQuery("#mark").is( jQuery(".blog")[0] ), "Check for class: Expected class 'blog'" ); ok( !jQuery("#mark").is( jQuery(".link")[0] ), "Check for class: Did not expect class 'link'" ); ok( jQuery("#simon").is( jQuery(".blog.link")[0] ), "Check for multiple classes: Expected classes 'blog' and 'link'" ); ok( !jQuery("#simon").is( jQuery(".blogTest")[0] ), "Check for multiple classes: Expected classes 'blog' and 'link', but not 'blogTest'" ); }); test("is() with :has() selectors", function() { expect(6); ok( jQuery("#foo").is(":has(p)"), "Check for child: Expected a child 'p' element" ); ok( !jQuery("#foo").is(":has(ul)"), "Check for child: Did not expect 'ul' element" ); ok( jQuery("#foo").is(":has(p):has(a):has(code)"), "Check for childs: Expected 'p', 'a' and 'code' child elements" ); 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'" ); ok( jQuery("#foo").is( jQuery("div:has(p)") ), "Check for child: Expected a child 'p' element" ); ok( !jQuery("#foo").is( jQuery("div:has(ul)") ), "Check for child: Did not expect 'ul' element" ); }); test("is() with positional selectors", function() { expect(27); var posp = jQuery( "
" ).appendTo( "#qunit-fixture" ), isit = function( sel, match, expect ) { equal( jQuery( sel ).is( match ), expect, "jQuery('" + sel + "').is('" + match + "')" ); }; isit( "#posp", "p:last", true ); isit( "#posp", "#posp:first", true ); isit( "#posp", "#posp:eq(2)", false ); isit( "#posp", "#posp a:first", false ); isit( "#posp .firsta", "#posp a:first", true ); isit( "#posp .firsta", "#posp a:last", false ); isit( "#posp .firsta", "#posp a:even", true ); isit( "#posp .firsta", "#posp a:odd", false ); isit( "#posp .firsta", "#posp a:eq(0)", true ); isit( "#posp .firsta", "#posp a:eq(9)", false ); isit( "#posp .firsta", "#posp em:eq(0)", false ); isit( "#posp .firsta", "#posp em:first", false ); isit( "#posp .firsta", "#posp:first", false ); isit( "#posp .seconda", "#posp a:first", false ); isit( "#posp .seconda", "#posp a:last", true ); isit( "#posp .seconda", "#posp a:gt(0)", true ); isit( "#posp .seconda", "#posp a:lt(5)", true ); isit( "#posp .seconda", "#posp a:lt(1)", false ); isit( "#posp em", "#posp a:eq(0) em", true ); isit( "#posp em", "#posp a:lt(1) em", true ); isit( "#posp em", "#posp a:gt(1) em", false ); isit( "#posp em", "#posp a:first em", true ); isit( "#posp em", "#posp a em:last", true ); isit( "#posp em", "#posp a em:eq(2)", false ); ok( jQuery("#option1b").is("#select1 option:not(:first)"), "POS inside of :not() (#10970)" ); ok( jQuery( posp[0] ).is("p:last"), "context constructed from a single node (#13797)" ); ok( !jQuery( posp[0] ).find("#firsta").is("a:first"), "context derived from a single node (#13797)" ); }); test("index()", function() { expect( 2 ); equal( jQuery("#text2").index(), 2, "Returns the index of a child amongst its siblings" ); equal( jQuery("").index(), -1, "Node without parent returns -1" ); }); test("index(Object|String|undefined)", function() { expect(16); var elements = jQuery([window, document]), inputElements = jQuery("#radio1,#radio2,#check1,#check2"); // Passing a node equal( elements.index(window), 0, "Check for index of elements" ); equal( elements.index(document), 1, "Check for index of elements" ); equal( inputElements.index(document.getElementById("radio1")), 0, "Check for index of elements" ); equal( inputElements.index(document.getElementById("radio2")), 1, "Check for index of elements" ); equal( inputElements.index(document.getElementById("check1")), 2, "Check for index of elements" ); equal( inputElements.index(document.getElementById("check2")), 3, "Check for index of elements" ); equal( inputElements.index(window), -1, "Check for not found index" ); equal( inputElements.index(document), -1, "Check for not found index" ); // Passing a jQuery object // enabled since [5500] equal( elements.index( elements ), 0, "Pass in a jQuery object" ); equal( elements.index( elements.eq(1) ), 1, "Pass in a jQuery object" ); equal( jQuery("#form input[type='radio']").index( jQuery("#radio2") ), 1, "Pass in a jQuery object" ); // Passing a selector or nothing // enabled since [6330] equal( jQuery("#text2").index(), 2, "Check for index amongst siblings" ); equal( jQuery("#form").children().eq(4).index(), 4, "Check for index amongst siblings" ); equal( jQuery("#radio2").index("#form input[type='radio']") , 1, "Check for index within a selector" ); equal( jQuery("#form input[type='radio']").index( jQuery("#radio2") ), 1, "Check for index within a selector" ); equal( jQuery("#radio2").index("#form input[type='text']") , -1, "Check for index not found within a selector" ); }); test("filter(Selector|undefined)", function() { expect(9); deepEqual( jQuery("#form input").filter(":checked").get(), q("radio2", "check1"), "filter(String)" ); deepEqual( jQuery("p").filter("#ap, #sndp").get(), q("ap", "sndp"), "filter('String, String')" ); deepEqual( jQuery("p").filter("#ap,#sndp").get(), q("ap", "sndp"), "filter('String,String')" ); deepEqual( jQuery("p").filter(null).get(), [], "filter(null) should return an empty jQuery object"); deepEqual( jQuery("p").filter(undefined).get(), [], "filter(undefined) should return an empty jQuery object"); deepEqual( jQuery("p").filter(0).get(), [], "filter(0) should return an empty jQuery object"); deepEqual( jQuery("p").filter("").get(), [], "filter('') should return an empty jQuery object"); // using contents will get comments regular, text, and comment nodes var j = jQuery("#nonnodes").contents(); equal( j.filter("span").length, 1, "Check node,textnode,comment to filter the one span" ); equal( j.filter("[name]").length, 0, "Check node,textnode,comment to filter the one span" ); }); test("filter(Function)", function() { expect(2); deepEqual( jQuery("#qunit-fixture p").filter(function() { return !jQuery("a", this).length; }).get(), q("sndp", "first"), "filter(Function)" ); deepEqual( jQuery("#qunit-fixture p").filter(function(i, elem) { return !jQuery("a", elem).length; }).get(), q("sndp", "first"), "filter(Function) using arg" ); }); test("filter(Element)", function() { expect(1); var element = document.getElementById("text1"); deepEqual( jQuery("#form input").filter(element).get(), q("text1"), "filter(Element)" ); }); test("filter(Array)", function() { expect(1); var elements = [ document.getElementById("text1") ]; deepEqual( jQuery("#form input").filter(elements).get(), q("text1"), "filter(Element)" ); }); test("filter(jQuery)", function() { expect(1); var elements = jQuery("#text1"); deepEqual( jQuery("#form input").filter(elements).get(), q("text1"), "filter(Element)" ); }); test("filter() with positional selectors", function() { expect(19); var filterit = function(sel, filter, length) { equal( jQuery( sel ).filter( filter ).length, length, "jQuery( " + sel + " ).filter( " + filter + " )" ); }; jQuery( "" + "" + "" + "first" + "" + "" + "test" + "" + "" + "
" ).appendTo( "#qunit-fixture" ); filterit( "#posp", "#posp:first", 1); filterit( "#posp", "#posp:eq(2)", 0 ); filterit( "#posp", "#posp a:first", 0 ); // Keep in mind this is within the selection and // not in relation to other elements (.is() is a different story) filterit( "#posp .firsta", "#posp a:first", 1 ); filterit( "#posp .firsta", "#posp a:last", 1 ); filterit( "#posp .firsta", "#posp a:last-child", 0 ); filterit( "#posp .firsta", "#posp a:even", 1 ); filterit( "#posp .firsta", "#posp a:odd", 0 ); filterit( "#posp .firsta", "#posp a:eq(0)", 1 ); filterit( "#posp .firsta", "#posp a:eq(9)", 0 ); filterit( "#posp .firsta", "#posp em:eq(0)", 0 ); filterit( "#posp .firsta", "#posp em:first", 0 ); filterit( "#posp .firsta", "#posp:first", 0 ); filterit( "#posp .seconda", "#posp a:first", 1 ); filterit( "#posp .seconda", "#posp em:first", 0 ); filterit( "#posp .seconda", "#posp a:last", 1 ); filterit( "#posp .seconda", "#posp a:gt(0)", 0 ); filterit( "#posp .seconda", "#posp a:lt(5)", 1 ); filterit( "#posp .seconda", "#posp a:lt(1)", 1 ); }); test("closest()", function() { expect( 13 ); var jq; deepEqual( jQuery("body").closest("body").get(), q("body"), "closest(body)" ); deepEqual( jQuery("body").closest("html").get(), q("html"), "closest(html)" ); deepEqual( jQuery("body").closest("div").get(), [], "closest(div)" ); deepEqual( jQuery("#qunit-fixture").closest("span,#html").get(), q("html"), "closest(span,#html)" ); // Test .closest() limited by the context jq = jQuery("#nothiddendivchild"); deepEqual( jq.closest("html", document.body).get(), [], "Context limited." ); deepEqual( jq.closest("body", document.body).get(), [], "Context limited." ); deepEqual( jq.closest("#nothiddendiv", document.body).get(), q("nothiddendiv"), "Context not reached." ); //Test that .closest() returns unique'd set equal( jQuery("#qunit-fixture p").closest("#qunit-fixture").length, 1, "Closest should return a unique set" ); // Test on disconnected node equal( jQuery("xxx
").add("xxx
"); equal( x[0].id, "x1", "Check detached element1" ); equal( x[1].id, "x2", "Check detached element2" ); }); test("add(jQuery)", function() { expect( 4 ); var x, tmp = jQuery(""); x = jQuery([]) .add( jQuery("xxx
").appendTo(tmp) ) .add( jQuery("xxx
").appendTo(tmp) ); equal( x[0].id, "x1", "Check element1 in detached parent" ); equal( x[1].id, "x2", "Check element2 in detached parent" ); x = jQuery([]) .add( jQuery("xxx
") ) .add( jQuery("xxx
") ); equal( x[0].id, "x1", "Check detached element1" ); equal( x[1].id, "x2", "Check detached element2" ); }); test("add(Element)", function() { expect( 2 ); var x, tmp = jQuery(""); x = jQuery([]).add(jQuery("xxx
").appendTo(tmp)[0]).add(jQuery("xxx
").appendTo(tmp)[0]); equal( x[0].id, "x1", "Check on-the-fly element1" ); equal( x[1].id, "x2", "Check on-the-fly element2" ); }); test("add(Array elements)", function() { expect( 1 ); deepEqual( jQuery("#sndp").add( jQuery("#en")[0] ).add( jQuery("#sap") ).toArray(), q("sndp", "en", "sap"), "Check elements from document" ); }); test("add(Window)", function() { expect( 1 ); var frame1 = document.createElement( "iframe" ), frame2 = document.createElement( "iframe" ); // This increases window.length and sets window[i] available document.body.appendChild( frame1 ); document.body.appendChild( frame2 ); // Window is tricky because it is a lot like an array, even Array#slice will // turn it into a multi-item array. equal( jQuery([]).add( window ).length, 1, "Add a window" ); document.body.removeChild( frame1 ); document.body.removeChild( frame2 ); }); test("add(NodeList|undefined|HTMLFormElement|HTMLSelectElement)", function() { expect( 4 ); var ps, notDefined; ps = document.getElementsByTagName("p"); equal( jQuery([]).add(ps).length, ps.length, "Add a NodeList" ); equal( jQuery([]).add(notDefined).length, 0, "Adding undefined adds nothing" ); equal( jQuery([]).add( document.getElementById("form") ).length, 1, "Add a form" ); equal( jQuery([]).add( document.getElementById("select1") ).length, 1, "Add a select" ); // We no longer support .add(form.elements), unfortunately. // There is no way, in browsers, to reliably determine the difference // between form.elements and form - and doing .add(form) and having it // add the form elements is way to unexpected, so this gets the boot. //ok( jQuery([]).add(jQuery("#form")[0].elements).length >= 13, "Check elements from array" ); // For the time being, we're discontinuing support for jQuery(form.elements) since it's ambiguous in IE // use jQuery([]).add(form.elements) instead. //equal( jQuery([]).add(jQuery("#form")[0].elements).length, jQuery(jQuery("#form")[0].elements).length, "Array in constructor must equals array in add()" ); }); test("add(String, Context)", function() { expect(6); deepEqual( jQuery( "#firstp" ).add( "#ap" ).get(), q( "firstp", "ap" ), "Add selector to selector " ); deepEqual( jQuery( document.getElementById("firstp") ).add( "#ap" ).get(), q( "firstp", "ap" ), "Add gEBId to selector" ); deepEqual( jQuery( document.getElementById("firstp") ).add( document.getElementById("ap") ).get(), q( "firstp", "ap" ), "Add gEBId to gEBId" ); var ctx = document.getElementById("firstp"); deepEqual( jQuery( "#firstp" ).add( "#ap", ctx ).get(), q( "firstp" ), "Add selector to selector " ); deepEqual( jQuery( document.getElementById("firstp") ).add( "#ap", ctx ).get(), q( "firstp" ), "Add gEBId to selector, not in context" ); deepEqual( jQuery( document.getElementById("firstp") ).add( "#ap", document.getElementsByTagName("body")[0] ).get(), q( "firstp", "ap" ), "Add gEBId to selector, in context" ); }); test("eq('-1') #10616", function() { expect(3); var $divs = jQuery( "div" ); equal( $divs.eq( -1 ).length, 1, "The number -1 returns a selection that has length 1" ); equal( $divs.eq( "-1" ).length, 1, "The string '-1' returns a selection that has length 1" ); deepEqual( $divs.eq( "-1" ), $divs.eq( -1 ), "String and number -1 match" ); }); test("index(no arg) #10977", function() { expect(2); var $list, fragment, div; $list = jQuery("