QUnit.module( "selector", { afterEach: moduleTeardown } ); /** * This test page is for selector tests that require jQuery in order to do the selection */ QUnit.test( "element", function( assert ) { assert.expect( 7 ); var fixture = document.getElementById( "qunit-fixture" ); assert.deepEqual( jQuery( "p", fixture ).get(), q( "firstp", "ap", "sndp", "en", "sap", "first" ), "Finding elements with a Node context." ); assert.deepEqual( jQuery( "p", "#qunit-fixture" ).get(), q( "firstp", "ap", "sndp", "en", "sap", "first" ), "Finding elements with a selector context." ); assert.deepEqual( jQuery( "p", jQuery( "#qunit-fixture" ) ).get(), q( "firstp", "ap", "sndp", "en", "sap", "first" ), "Finding elements with a jQuery object context." ); assert.deepEqual( jQuery( "#qunit-fixture" ).find( "p" ).get(), q( "firstp", "ap", "sndp", "en", "sap", "first" ), "Finding elements with a context via .find()." ); assert.ok( jQuery( "#length" ).length, " cannot be found under IE, see #945" ); assert.ok( jQuery( "#lengthtest input" ).length, " cannot be found under IE, see #945" ); // #7533 assert.equal( jQuery( "

foo

" ).find( "p" ).length, 1, "Find where context root is a node and has an ID with CSS3 meta characters" ); } ); QUnit.test( "id", function( assert ) { assert.expect( 26 ); var a; assert.t( "ID Selector", "#body", [ "body" ] ); assert.t( "ID Selector w/ Element", "body#body", [ "body" ] ); assert.t( "ID Selector w/ Element", "ul#first", [] ); assert.t( "ID selector with existing ID descendant", "#firstp #simon1", [ "simon1" ] ); assert.t( "ID selector with non-existent descendant", "#firstp #foobar", [] ); assert.t( "ID selector using UTF8", "#台北Táiběi", [ "台北Táiběi" ] ); assert.t( "Multiple ID selectors using UTF8", "#台北Táiběi, #台北", [ "台北Táiběi", "台北" ] ); assert.t( "Descendant ID selector using UTF8", "div #台北", [ "台北" ] ); assert.t( "Child ID selector using UTF8", "form > #台北", [ "台北" ] ); assert.t( "Escaped ID", "#foo\\:bar", [ "foo:bar" ] ); assert.t( "Escaped ID", "#test\\.foo\\[5\\]bar", [ "test.foo[5]bar" ] ); assert.t( "Descendant escaped ID", "div #foo\\:bar", [ "foo:bar" ] ); assert.t( "Descendant escaped ID", "div #test\\.foo\\[5\\]bar", [ "test.foo[5]bar" ] ); assert.t( "Child escaped ID", "form > #foo\\:bar", [ "foo:bar" ] ); assert.t( "Child escaped ID", "form > #test\\.foo\\[5\\]bar", [ "test.foo[5]bar" ] ); assert.t( "ID Selector, child ID present", "#form > #radio1", [ "radio1" ] ); // bug #267 assert.t( "ID Selector, not an ancestor ID", "#form #first", [] ); assert.t( "ID Selector, not a child ID", "#form > #option1a", [] ); assert.t( "All Children of ID", "#foo > *", [ "sndp", "en", "sap" ] ); assert.t( "All Children of ID with no children", "#firstUL > *", [] ); a = jQuery( "" ).appendTo( "#qunit-fixture" ); assert.t( "ID Selector contains backslash", "#backslash\\\\foo", [ "backslash\\foo" ] ); assert.t( "ID Selector on Form with an input that has a name of 'id'", "#lengthtest", [ "lengthtest" ] ); assert.t( "ID selector with non-existent ancestor", "#asdfasdf #foobar", [] ); // bug #986 assert.t( "Underscore ID", "#types_all", [ "types_all" ] ); assert.t( "Dash ID", "#qunit-fixture", [ "qunit-fixture" ] ); assert.t( "ID with weird characters in it", "#name\\+value", [ "name+value" ] ); } ); QUnit.test( "class", function( assert ) { assert.expect( 4 ); assert.deepEqual( jQuery( ".blog", document.getElementsByTagName( "p" ) ).get(), q( "mark", "simon" ), "Finding elements with a context." ); assert.deepEqual( jQuery( ".blog", "p" ).get(), q( "mark", "simon" ), "Finding elements with a context." ); assert.deepEqual( jQuery( ".blog", jQuery( "p" ) ).get(), q( "mark", "simon" ), "Finding elements with a context." ); assert.deepEqual( jQuery( "p" ).find( ".blog" ).get(), q( "mark", "simon" ), "Finding elements with a context." ); } ); QUnit.test( "name", function( assert ) { assert.expect( 5 ); var form; assert.t( "Name selector", "input[name=action]", [ "text1" ] ); assert.t( "Name selector with single quotes", "input[name='action']", [ "text1" ] ); assert.t( "Name selector with double quotes", "input[name=\"action\"]", [ "text1" ] ); assert.t( "Name selector for grouped input", "input[name='types[]']", [ "types_all", "types_anime", "types_movie" ] ); form = jQuery( "
" ).appendTo( "body" ); assert.equal( jQuery( "input", form[ 0 ] ).length, 1, "Make sure that rooted queries on forms (with possible expandos) work." ); form.remove(); } ); QUnit.test( "selectors with comma", function( assert ) { assert.expect( 4 ); var fixture = jQuery( "

" ); assert.equal( fixture.find( "h2, div p" ).filter( "p" ).length, 2, "has to find two

" ); assert.equal( fixture.find( "h2, div p" ).filter( "h2" ).length, 1, "has to find one

" ); assert.equal( fixture.find( "h2 , div p" ).filter( "p" ).length, 2, "has to find two

" ); assert.equal( fixture.find( "h2 , div p" ).filter( "h2" ).length, 1, "has to find one

" ); } ); QUnit.test( "child and adjacent", function( assert ) { assert.expect( 27 ); assert.t( "Child", "p > a", [ "simon1", "google", "groups", "mark", "yahoo", "simon" ] ); assert.t( "Child", "p> a", [ "simon1", "google", "groups", "mark", "yahoo", "simon" ] ); assert.t( "Child", "p >a", [ "simon1", "google", "groups", "mark", "yahoo", "simon" ] ); assert.t( "Child", "p>a", [ "simon1", "google", "groups", "mark", "yahoo", "simon" ] ); assert.t( "Child w/ Class", "p > a.blog", [ "mark", "simon" ] ); assert.t( "All Children", "code > *", [ "anchor1", "anchor2" ] ); assert.selectInFixture( "All Grandchildren", "p > * > *", [ "anchor1", "anchor2" ] ); assert.t( "Adjacent", "p + p", [ "ap", "en", "sap" ] ); assert.t( "Adjacent", "p#firstp + p", [ "ap" ] ); assert.t( "Adjacent", "p[lang=en] + p", [ "sap" ] ); assert.t( "Adjacent", "a.GROUPS + code + a", [ "mark" ] ); assert.t( "Element Preceded By", "#groups ~ a", [ "mark" ] ); assert.t( "Element Preceded By", "#length ~ input", [ "idTest" ] ); assert.t( "Element Preceded By", "#siblingfirst ~ em", [ "siblingnext", "siblingthird" ] ); assert.t( "Element Preceded By (multiple)", "#siblingTest em ~ em ~ em ~ span", [ "siblingspan" ] ); if ( jQuery.find.compile ) { assert.t( "Element Preceded By, Containing", "#liveHandlerOrder ~ div em:contains('1')", [ "siblingfirst" ] ); assert.t( "Combinators are not skipped when mixing general and specific", "#siblingTest > em:contains('x') + em ~ span", [] ); assert.equal( jQuery( "#listWithTabIndex li:eq(2) ~ li" ).length, 1, "Find by general sibling combinator (#8310)" ); } else { assert.ok( "skip", ":contains not supported in selector-native" ); assert.ok( "skip", ":contains not supported in selector-native" ); assert.ok( "skip", ":eq not supported in selector-native" ); } assert.t( "Multiple combinators selects all levels", "#siblingTest em *", [ "siblingchild", "siblinggrandchild", "siblinggreatgrandchild" ] ); assert.t( "Multiple combinators selects all levels", "#siblingTest > em *", [ "siblingchild", "siblinggrandchild", "siblinggreatgrandchild" ] ); assert.t( "Multiple sibling combinators doesn't miss general siblings", "#siblingTest > em:first-child + em ~ span", [ "siblingspan" ] ); assert.equal( jQuery( "#listWithTabIndex" ).length, 1, "Parent div for next test is found via ID (#8310)" ); assert.equal( jQuery( "#__sizzle__" ).length, 0, "Make sure the temporary id assigned by sizzle is cleared out (#8310)" ); assert.equal( jQuery( "#listWithTabIndex" ).length, 1, "Parent div for previous test is still found via ID (#8310)" ); assert.t( "Verify deep class selector", "div.blah > p > a", [] ); assert.t( "No element deep selector", "div.foo > span > a", [] ); assert.t( "Non-existent ancestors", ".fototab > .thumbnails > a", [] ); } ); QUnit.test( "attributes", function( assert ) { assert.expect( 54 ); var attrbad, div, withScript; assert.t( "Find elements with a tabindex attribute", "[tabindex]", [ "listWithTabIndex", "foodWithNegativeTabIndex", "linkWithTabIndex", "linkWithNegativeTabIndex", "linkWithNoHrefWithTabIndex", "linkWithNoHrefWithNegativeTabIndex" ] ); assert.t( "Attribute Exists", "#qunit-fixture a[title]", [ "google" ] ); assert.t( "Attribute Exists (case-insensitive)", "#qunit-fixture a[TITLE]", [ "google" ] ); assert.t( "Attribute Exists", "#qunit-fixture *[title]", [ "google" ] ); assert.t( "Attribute Exists", "#qunit-fixture [title]", [ "google" ] ); assert.t( "Attribute Exists", "#qunit-fixture a[ title ]", [ "google" ] ); assert.t( "Boolean attribute exists", "#select2 option[selected]", [ "option2d" ] ); assert.t( "Boolean attribute equals", "#select2 option[selected='selected']", [ "option2d" ] ); assert.t( "Attribute Equals", "#qunit-fixture a[rel='bookmark']", [ "simon1" ] ); assert.t( "Attribute Equals", "#qunit-fixture a[rel='bookmark']", [ "simon1" ] ); assert.t( "Attribute Equals", "#qunit-fixture a[rel=bookmark]", [ "simon1" ] ); assert.t( "Attribute Equals", "#qunit-fixture a[href='http://www.google.com/']", [ "google" ] ); assert.t( "Attribute Equals", "#qunit-fixture a[ rel = 'bookmark' ]", [ "simon1" ] ); assert.t( "Attribute Equals Number", "#qunit-fixture option[value='1']", [ "option1b", "option2b", "option3b", "option4b", "option5c" ] ); assert.t( "Attribute Equals Number", "#qunit-fixture li[tabIndex='-1']", [ "foodWithNegativeTabIndex" ] ); document.getElementById( "anchor2" ).href = "#2"; assert.t( "href Attribute", "p a[href^='#']", [ "anchor2" ] ); assert.t( "href Attribute", "p a[href*='#']", [ "simon1", "anchor2" ] ); assert.t( "for Attribute", "form label[for]", [ "label-for" ] ); assert.t( "for Attribute in form", "#form [for=action]", [ "label-for" ] ); assert.t( "Attribute containing []", "input[name^='foo[']", [ "hidden2" ] ); assert.t( "Attribute containing []", "input[name^='foo[bar]']", [ "hidden2" ] ); assert.t( "Attribute containing []", "input[name*='[bar]']", [ "hidden2" ] ); assert.t( "Attribute containing []", "input[name$='bar]']", [ "hidden2" ] ); assert.t( "Attribute containing []", "input[name$='[bar]']", [ "hidden2" ] ); assert.t( "Attribute containing []", "input[name$='foo[bar]']", [ "hidden2" ] ); assert.t( "Attribute containing []", "input[name*='foo[bar]']", [ "hidden2" ] ); assert.t( "Multiple Attribute Equals", "#form input[type='radio'], #form input[type='hidden']", [ "radio1", "radio2", "hidden1" ] ); assert.t( "Multiple Attribute Equals", "#form input[type='radio'], #form input[type=\"hidden\"]", [ "radio1", "radio2", "hidden1" ] ); assert.t( "Multiple Attribute Equals", "#form input[type='radio'], #form input[type=hidden]", [ "radio1", "radio2", "hidden1" ] ); assert.t( "Attribute selector using UTF8", "span[lang=中文]", [ "台北" ] ); assert.t( "Attribute Begins With", "a[href ^= 'http://www']", [ "google", "yahoo" ] ); assert.t( "Attribute Ends With", "a[href $= 'org/']", [ "mark" ] ); assert.t( "Attribute Contains", "a[href *= 'google']", [ "google", "groups" ] ); if ( jQuery.find.compile ) { assert.t( "Empty values", "#select1 option[value!='']", [ "option1b", "option1c", "option1d" ] ); assert.t( "Attribute Is Not Equal", "#ap a[hreflang!='en']", [ "google", "groups", "anchor1" ] ); assert.t( "Select options via :selected", "#select1 option:selected", [ "option1a" ] ); assert.t( "Select options via :selected", "#select2 option:selected", [ "option2d" ] ); assert.t( "Select options via :selected", "#select3 option:selected", [ "option3b", "option3c" ] ); assert.t( "Select options via :selected", "select[name='select2'] option:selected", [ "option2d" ] ); } else { assert.ok( "skip", "!= not supported in selector-native" ); assert.ok( "skip", "!= not supported in selector-native" ); assert.ok( "skip", ":selected not supported in selector-native" ); assert.ok( "skip", ":selected not supported in selector-native" ); assert.ok( "skip", ":selected not supported in selector-native" ); assert.ok( "skip", ":selected not supported in selector-native" ); } assert.t( "Empty values", "#select1 option[value='']", [ "option1a" ] ); assert.t( "Grouped Form Elements", "input[name='foo[bar]']", [ "hidden2" ] ); // Make sure attribute value quoting works correctly. See jQuery #6093; #6428; #13894 // Use seeded results to bypass querySelectorAll optimizations attrbad = jQuery( "" + "" + "" + "" + "" + "" + "" + "" + "" ).appendTo( "#qunit-fixture" ).get(); assert.t( "Underscores don't need escaping", "input[id=types_all]", [ "types_all" ] ); assert.t( "input[type=text]", "#form input[type=text]", [ "text1", "text2", "hidden2", "name" ] ); assert.t( "input[type=search]", "#form input[type=search]", [ "search" ] ); withScript = supportjQuery( "