module("traversing", { teardown: moduleTeardown }); test( "find(String)", function() { expect( 7 ); equal( "Yahoo", jQuery("#foo").find(".blogTest").text(), "Check for find" ); // using contents will get comments regular, text, and comment nodes var j = jQuery("#nonnodes").contents(); equal( j.find("div").length, 0, "Check node,textnode,comment to find zero divs" ); equal( j.find("div").andSelf().length, 3, "Check node,textnode,comment to find zero divs, but preserves pushStack" ); deepEqual( jQuery("#qunit-fixture").find("> div").get(), q( "foo", "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( 12 ); var $foo = jQuery("#foo"), $blog = jQuery(".blogTest"), $first = jQuery("#first"), $two = $blog.add( $first ), $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)" ); ok( $foo.find( $two ).is(".blogTest"), "Find returns only nodes within #foo" ); 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 ] ).andSelf().length, 2, "find preserves the pushStack, see #12009" ); }); test("is(String|undefined)", function() { expect(30); 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(":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(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-seperated expressions ok( jQuery("#en").is("[lang=\"en\"],[lang=\"de\"]"), "Comma-seperated; Check for lang attribute: Expect en or de" ); ok( jQuery("#en").is("[lang=\"de\"],[lang=\"en\"]"), "Comma-seperated; Check for lang attribute: Expect en or de" ); ok( jQuery("#en").is("[lang=\"en\"] , [lang=\"de\"]"), "Comma-seperated; Check for lang attribute: Expect en or de" ); ok( jQuery("#en").is("[lang=\"de\"] , [lang=\"en\"]"), "Comma-seperated; Check for lang attribute: Expect en or de" ); ok( !jQuery(window).is("a"), "Checking is on a window does not throw an exception(#10178)" ); ok( !jQuery(document).is("a"), "Checking is on a document does not throw an exception(#10178)" ); ok( jQuery("#option1b").is("#select1 option:not(:first)"), "POS inside of :not() (#10970)" ); }); test("is(jQuery)", function() { expect(21); 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" ); 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" ); // Some raw elements ok( jQuery("#form").is( jQuery("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 positional selectors", function() { expect(23); var html = jQuery( "
" ).appendTo( "body" ), isit = function(sel, match, expect) { equal( jQuery( sel ).is( match ), expect, "jQuery( " + sel + " ).is( " + match + " )" ); }; 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 ); html.remove(); }); 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 :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 :radio") , 1, "Check for index within a selector" ); equal( jQuery("#form :radio").index( jQuery("#radio2") ), 1, "Check for index within a selector" ); equal( jQuery("#radio2").index("#form :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 html = jQuery( "" + "" + "" + "first" + "" + "" + "test" + "" + "" + "
" ).appendTo( "body" ), filterit = function(sel, filter, length) { equal( jQuery( sel ).filter( filter ).length, length, "jQuery( " + sel + " ).filter( " + filter + " )" ); }; 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 ); html.remove(); }); test("closest()", function() { expect( 14 ); 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)" ); deepEqual( jQuery("div:eq(1)").closest("div:first").get(), [], "closest(div:first)" ); deepEqual( jQuery("div").closest("body:first div:last").get(), q("fx-tests"), "closest(body:first div:last)" ); // Test .closest() limited by the context var 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
").appendTo(tmp)).add(jQuery("xxx
").appendTo(tmp)); equal( x[0].id, "x1", "Check on-the-fly element1" ); equal( x[1].id, "x2", "Check on-the-fly element2" ); 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" ); x = jQuery([]).add(jQuery("xxx
")).add(jQuery("xxx
")); equal( x[0].id, "x1", "Check on-the-fly element1" ); equal( x[1].id, "x2", "Check on-the-fly element2" ); x = jQuery([]).add("xxx
").add("xxx
"); equal( x[0].id, "x1", "Check on-the-fly element1" ); equal( x[1].id, "x2", "Check on-the-fly element2" ); var notDefined; equal( jQuery([]).add(notDefined).length, 0, "Check that 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" ); }); 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(1); var $list = jQuery("