3 test("element", function() {
5 ok( $("*").size() >= 30, "Select all" );
6 var all = $("*"), good = true;
7 for ( var i = 0; i < all.length; i++ )
8 if ( all[i].nodeType == 8 )
10 ok( good, "Select all elements, no comment nodes" );
11 t( "Element Selector", "p", ["firstp","ap","sndp","en","sap","first"] );
12 t( "Element Selector", "body", ["body"] );
13 t( "Element Selector", "html", ["html"] );
14 t( "Parent Element", "div p", ["firstp","ap","sndp","en","sap","first"] );
15 ok( $("param", "#object1").length == 2, "Object/param as context" );
17 ok( $("#length").length, '<input name="length"> cannot be found under IE, see #945' );
18 ok( $("#lengthtest input").length, '<input name="length"> cannot be found under IE, see #945' );
21 if ( location.protocol != "file:" ) {
22 test("Element Selector with underscore", function() {
25 $.get("data/with_fries.xml", function(xml) {
26 ok( $("foo_bar", xml).length == 1, "Element Selector with underscore" );
32 test("broken", function() {
34 t( "Broken Selector", "[", [] );
35 t( "Broken Selector", "(", [] );
36 t( "Broken Selector", "{", [] );
37 t( "Broken Selector", "<", [] );
38 t( "Broken Selector", "()", [] );
39 t( "Broken Selector", "<>", [] );
40 t( "Broken Selector", "{}", [] );
43 test("id", function() {
45 t( "ID Selector", "#body", ["body"] );
46 t( "ID Selector w/ Element", "body#body", ["body"] );
47 t( "ID Selector w/ Element", "ul#first", [] );
48 t( "ID selector with existing ID descendant", "#firstp #simon1", ["simon1"] );
49 t( "ID selector with non-existant descendant", "#firstp #foobar", [] );
50 t( "ID selector using UTF8", "#台北Táiběi", ["台北Táiběi"] );
51 t( "Multiple ID selectors using UTF8", "#台北Táiběi, #台北", ["台北Táiběi","台北"] );
52 t( "Descendant ID selector using UTF8", "div #台北", ["台北"] );
53 t( "Child ID selector using UTF8", "form > #台北", ["台北"] );
55 t( "Escaped ID", "#foo\\:bar", ["foo:bar"] );
56 t( "Escaped ID", "#test\\.foo\\[5\\]bar", ["test.foo[5]bar"] );
57 t( "Descendant escaped ID", "div #foo\\:bar", ["foo:bar"] );
58 t( "Descendant escaped ID", "div #test\\.foo\\[5\\]bar", ["test.foo[5]bar"] );
59 t( "Child escaped ID", "form > #foo\\:bar", ["foo:bar"] );
60 t( "Child escaped ID", "form > #test\\.foo\\[5\\]bar", ["test.foo[5]bar"] );
62 t( "ID Selector, child ID present", "#form > #radio1", ["radio1"] ); // bug #267
63 t( "ID Selector, not an ancestor ID", "#form #first", [] );
64 t( "ID Selector, not a child ID", "#form > #option1a", [] );
66 t( "All Children of ID", "#foo/*", ["sndp", "en", "sap"] );
67 t( "All Children of ID with no children", "#firstUL/*", [] );
69 $('<a name="tName1">tName1 A</a><a name="tName2">tName2 A</a><div id="tName1">tName1 Div</div>').appendTo('#main');
70 ok( $("#tName1")[0].id == 'tName1', "ID selector with same value for a name attribute" );
71 ok( $("#tName2").length == 0, "ID selector non-existing but name attribute on an A tag" );
72 t( "ID Selector on Form with an input that has a name of 'id'", "#lengthtest", ["lengthtest"] );
74 t( "ID selector with non-existant ancestor", "#asdfasdf #foobar", [] ); // bug #986
77 test("class", function() {
79 t( "Class Selector", ".blog", ["mark","simon"] );
80 t( "Class Selector", ".blog.link", ["simon"] );
81 t( "Class Selector w/ Element", "a.blog", ["mark","simon"] );
82 t( "Parent Class Selector", "p .blog", ["mark","simon"] );
84 t( "Class selector using UTF8", ".台北Táiběi", ["utf8class1"] );
85 t( "Class selector using UTF8", ".台北", ["utf8class1","utf8class2"] );
86 t( "Class selector using UTF8", ".台北Táiběi.台北", ["utf8class1"] );
87 t( "Class selector using UTF8", ".台北Táiběi, .台北", ["utf8class1","utf8class2"] );
88 t( "Descendant class selector using UTF8", "div .台北Táiběi", ["utf8class1"] );
89 t( "Child class selector using UTF8", "form > .台北Táiběi", ["utf8class1"] );
91 t( "Escaped Class", ".foo\\:bar", ["foo:bar"] );
92 t( "Escaped Class", ".test\\.foo\\[5\\]bar", ["test.foo[5]bar"] );
93 t( "Descendant scaped Class", "div .foo\\:bar", ["foo:bar"] );
94 t( "Descendant scaped Class", "div .test\\.foo\\[5\\]bar", ["test.foo[5]bar"] );
95 t( "Child escaped Class", "form > .foo\\:bar", ["foo:bar"] );
96 t( "Child escaped Class", "form > .test\\.foo\\[5\\]bar", ["test.foo[5]bar"] );
99 test("multiple", function() {
101 t( "Comma Support", "a.blog, p", ["mark","simon","firstp","ap","sndp","en","sap","first"] );
102 t( "Comma Support", "a.blog , p", ["mark","simon","firstp","ap","sndp","en","sap","first"] );
103 t( "Comma Support", "a.blog ,p", ["mark","simon","firstp","ap","sndp","en","sap","first"] );
104 t( "Comma Support", "a.blog,p", ["mark","simon","firstp","ap","sndp","en","sap","first"] );
107 test("child and adjacent", function() {
109 t( "Child", "p > a", ["simon1","google","groups","mark","yahoo","simon"] );
110 t( "Child", "p> a", ["simon1","google","groups","mark","yahoo","simon"] );
111 t( "Child", "p >a", ["simon1","google","groups","mark","yahoo","simon"] );
112 t( "Child", "p>a", ["simon1","google","groups","mark","yahoo","simon"] );
113 t( "Child w/ Class", "p > a.blog", ["mark","simon"] );
114 t( "All Children", "code > *", ["anchor1","anchor2"] );
115 t( "All Grandchildren", "p > * > *", ["anchor1","anchor2"] );
116 t( "Adjacent", "a + a", ["groups"] );
117 t( "Adjacent", "a +a", ["groups"] );
118 t( "Adjacent", "a+ a", ["groups"] );
119 t( "Adjacent", "a+a", ["groups"] );
120 t( "Adjacent", "p + p", ["ap","en","sap"] );
121 t( "Comma, Child, and Adjacent", "a + a, code > a", ["groups","anchor1","anchor2"] );
123 t( "First Child", "p:first-child", ["firstp","sndp"] );
124 t( "Nth Child", "p:nth-child(1)", ["firstp","sndp"] );
126 t( "Last Child", "p:last-child", ["sap"] );
127 t( "Last Child", "a:last-child", ["simon1","anchor1","mark","yahoo","anchor2","simon"] );
129 t( "Nth-child", "#main form > *:nth-child(2)", ["text2","idTest"] );
130 t( "Nth-child", "#main form > :nth-child(2)", ["text2","idTest"] );
133 test("attributes", function() {
135 t( "Attribute Exists", "a[@title]", ["google"] );
136 t( "Attribute Exists", "*[@title]", ["google"] );
137 t( "Attribute Exists", "[@title]", ["google"] );
139 t( "Attribute Equals", "a[@rel='bookmark']", ["simon1"] );
140 t( "Attribute Equals", 'a[@rel="bookmark"]', ["simon1"] );
141 t( "Attribute Equals", "a[@rel=bookmark]", ["simon1"] );
142 t( "Multiple Attribute Equals", "input[@type='hidden'],input[@type='radio']", ["hidden1","radio1","radio2"] );
143 t( "Multiple Attribute Equals", "input[@type=\"hidden\"],input[@type='radio']", ["hidden1","radio1","radio2"] );
144 t( "Multiple Attribute Equals", "input[@type=hidden],input[@type=radio]", ["hidden1","radio1","radio2"] );
146 t( "Attribute selector using UTF8", "span[@lang=中文]", ["台北"] );
148 t( "Attribute Begins With", "a[@href ^= 'http://www']", ["google","yahoo"] );
149 t( "Attribute Ends With", "a[@href $= 'org/']", ["mark"] );
150 t( "Attribute Contains", "a[@href *= 'google']", ["google","groups"] );
152 t("Select options via [@selected]", "#select1 option[@selected]", ["option1a"] );
153 t("Select options via [@selected]", "#select2 option[@selected]", ["option2d"] );
154 t("Select options via [@selected]", "#select3 option[@selected]", ["option3b", "option3c"] );
156 t( "Grouped Form Elements", "input[@name='foo[bar]']", ["hidden2"] );
158 t( ":not() Existing attribute", "select:not([@multiple])", ["select1", "select2"]);
159 t( ":not() Equals attribute", "select:not([@name=select1])", ["select2", "select3"]);
160 t( ":not() Equals quoted attribute", "select:not([@name='select1'])", ["select2", "select3"]);
163 test("pseudo (:) selectors", function() {
165 t( "First Child", "p:first-child", ["firstp","sndp"] );
166 t( "Last Child", "p:last-child", ["sap"] );
167 t( "Only Child", "a:only-child", ["simon1","anchor1","yahoo","anchor2"] );
168 t( "Empty", "ul:empty", ["firstUL"] );
169 t( "Enabled UI Element", "input:enabled", ["text1","radio1","radio2","check1","check2","hidden1","hidden2","name","length","idTest"] );
170 t( "Disabled UI Element", "input:disabled", ["text2"] );
171 t( "Checked UI Element", "input:checked", ["radio2","check1"] );
172 t( "Selected Option Element", "option:selected", ["option1a","option2d","option3b","option3c"] );
173 t( "Text Contains", "a:contains('Google')", ["google","groups"] );
174 t( "Text Contains", "a:contains('Google Groups')", ["groups"] );
175 t( "Element Preceded By", "p ~ div", ["foo","fx-queue","fx-tests"] );
176 t( "Not", "a.blog:not(.link)", ["mark"] );
178 t( "nth Element", "p:nth(1)", ["ap"] );
179 t( "First Element", "p:first", ["firstp"] );
180 t( "Last Element", "p:last", ["first"] );
181 t( "Even Elements", "p:even", ["firstp","sndp","sap"] );
182 t( "Odd Elements", "p:odd", ["ap","en","first"] );
183 t( "Position Equals", "p:eq(1)", ["ap"] );
184 t( "Position Greater Than", "p:gt(0)", ["ap","sndp","en","sap","first"] );
185 t( "Position Less Than", "p:lt(3)", ["firstp","ap","sndp"] );
186 t( "Is A Parent", "p:parent", ["firstp","ap","sndp","en","sap","first"] );
187 t( "Is Visible", "input:visible", ["text1","text2","radio1","radio2","check1","check2","name","length","idTest"] );
188 t( "Is Hidden", "input:hidden", ["hidden1","hidden2"] );
190 t( "Form element :input", ":input", ["text1", "text2", "radio1", "radio2", "check1", "check2", "hidden1", "hidden2", "name", "button", "area1", "select1", "select2", "select3", "length", "idTest"] );
191 t( "Form element :radio", ":radio", ["radio1", "radio2"] );
192 t( "Form element :checkbox", ":checkbox", ["check1", "check2"] );
193 t( "Form element :text", ":text", ["text1", "text2", "hidden2", "name", "length", "idTest"] );
194 t( "Form element :radio:checked", ":radio:checked", ["radio2"] );
195 t( "Form element :checkbox:checked", ":checkbox:checked", ["check1"] );
196 t( "Form element :checkbox:checked, :radio:checked", ":checkbox:checked, :radio:checked", ["check1", "radio2"] );
199 test("basic xpath", function() {
201 ok( jQuery.find("//*").length >= 30, "All Elements (//*)" );
202 ok( jQuery.find("//div", q("main")[0])[0] = q("foo")[0], "All Relative (#main//div)" );
203 t( "All P Elements", "//p", ["firstp","ap","sndp","en","sap","first"] );
204 t( "Absolute Path", "/html/body", ["body"] );
205 t( "Absolute Path w/ *", "/* /body", ["body"] );
206 t( "Long Absolute Path", "/html/body/dl/div/div/p", ["sndp","en","sap"] );
207 t( "Absolute and Relative Paths", "/html//p", ["firstp","ap","sndp","en","sap","first"] );
208 t( "All Children, Explicit", "//code/*", ["anchor1","anchor2"] );
209 t( "All Children, Implicit", "//code/", ["anchor1","anchor2"] );
210 t( "Attribute Exists", "//a[@title]", ["google"] );
211 t( "Attribute Equals", "//a[@rel='bookmark']", ["simon1"] );
212 t( "Parent Axis", "//p/..", ["main","foo"] );
213 t( "Sibling Axis", "//p/../", ["firstp","ap","foo","first","firstUL","empty","form","floatTest","iframe","lengthtest","table","fx-queue","fx-tests","sndp","en","sap"] );
214 t( "Sibling Axis", "//p/../*", ["firstp","ap","foo","first","firstUL","empty","form","floatTest","iframe","lengthtest","table","fx-queue","fx-tests","sndp","en","sap"] );
215 t( "Has Children", "//p[a]", ["firstp","ap","en","sap"] );
217 $("#foo").each(function() {
218 isSet( $("/p", this).get(), q("sndp", "en", "sap"), "Check XPath context" );