Landed cross-browser support for tabIndex, by Scott, closes ticket #3649.
[jquery.git] / test / unit / selector.js
1 module("selector");
2
3 test("element", function() {
4         expect(9);
5         ok( jQuery("*").size() >= 30, "Select all" );
6         var all = jQuery("*"), good = true;
7         for ( var i = 0; i < all.length; i++ )
8                 if ( all[i].nodeType == 8 )
9                         good = false;
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         equals( jQuery("param", "#object1").length, 2, "Object/param as context" );
16         
17         ok( jQuery("#length").length, '&lt;input name="length"&gt; cannot be found under IE, see #945' );
18         ok( jQuery("#lengthtest input").length, '&lt;input name="length"&gt; cannot be found under IE, see #945' );
19 });
20
21 if ( location.protocol != "file:" ) {
22         test("Element Selector with underscore", function() {
23                 expect(1);
24                 stop();
25                 jQuery.get("data/with_fries.xml", function(xml) {
26                         equals( jQuery("foo_bar", xml).length, 1, "Element Selector with underscore" );
27                         start();
28                 });
29         });
30 }
31
32 test("broken", function() {
33         expect(7);
34         function broken(name, selector) {
35                 try {
36                         t( name, selector, [] );
37                 } catch(e){
38                         ok(  typeof e === "string" && e.indexOf("Syntax error") >= 0,
39                                 name + ": " + selector );
40                 }
41         }
42         
43         broken( "Broken Selector", "[", [] );
44         broken( "Broken Selector", "(", [] );
45         broken( "Broken Selector", "{", [] );
46         broken( "Broken Selector", "<", [] );
47         broken( "Broken Selector", "()", [] );
48         broken( "Broken Selector", "<>", [] );
49         broken( "Broken Selector", "{}", [] );
50 });
51
52 test("id", function() {
53         expect(25);
54         t( "ID Selector", "#body", ["body"] );
55         t( "ID Selector w/ Element", "body#body", ["body"] );
56         t( "ID Selector w/ Element", "ul#first", [] );
57         t( "ID selector with existing ID descendant", "#firstp #simon1", ["simon1"] );
58         t( "ID selector with non-existant descendant", "#firstp #foobar", [] );
59         t( "ID selector using UTF8", "#台北Táiběi", ["台北Táiběi"] );
60         t( "Multiple ID selectors using UTF8", "#台北Táiběi, #台北", ["台北Táiběi","台北"] );
61         t( "Descendant ID selector using UTF8", "div #台北", ["台北"] );
62         t( "Child ID selector using UTF8", "form > #台北", ["台北"] );
63         
64         t( "Escaped ID", "#foo\\:bar", ["foo:bar"] );
65         t( "Escaped ID", "#test\\.foo\\[5\\]bar", ["test.foo[5]bar"] );
66         t( "Descendant escaped ID", "div #foo\\:bar", ["foo:bar"] );
67         t( "Descendant escaped ID", "div #test\\.foo\\[5\\]bar", ["test.foo[5]bar"] );
68         t( "Child escaped ID", "form > #foo\\:bar", ["foo:bar"] );
69         t( "Child escaped ID", "form > #test\\.foo\\[5\\]bar", ["test.foo[5]bar"] );
70         
71         t( "ID Selector, child ID present", "#form > #radio1", ["radio1"] ); // bug #267
72         t( "ID Selector, not an ancestor ID", "#form #first", [] );
73         t( "ID Selector, not a child ID", "#form > #option1a", [] );
74         
75         t( "All Children of ID", "#foo > *", ["sndp", "en", "sap"] );
76         t( "All Children of ID with no children", "#firstUL > *", [] );
77         
78         jQuery('<a name="tName1">tName1 A</a><a name="tName2">tName2 A</a><div id="tName1">tName1 Div</div>').appendTo('#main');
79         equals( jQuery("#tName1")[0].id, 'tName1', "ID selector with same value for a name attribute" );
80         equals( jQuery("#tName2").length, 0, "ID selector non-existing but name attribute on an A tag" );
81         t( "ID Selector on Form with an input that has a name of 'id'", "#lengthtest", ["lengthtest"] );
82         
83         t( "ID selector with non-existant ancestor", "#asdfasdf #foobar", [] ); // bug #986
84
85         isSet( jQuery("body").find("div#form"), [], "ID selector within the context of another element" );
86 });
87
88 test("class", function() {
89         expect(15);
90         t( "Class Selector", ".blog", ["mark","simon"] );
91         t( "Class Selector", ".blog.link", ["simon"] );
92         t( "Class Selector w/ Element", "a.blog", ["mark","simon"] );
93         t( "Parent Class Selector", "p .blog", ["mark","simon"] );
94         
95         t( "Class selector using UTF8", ".台北Táiběi", ["utf8class1"] );
96         //t( "Class selector using UTF8", ".台北", ["utf8class1","utf8class2"] );
97         t( "Class selector using UTF8", ".台北Táiběi.台北", ["utf8class1"] );
98         t( "Class selector using UTF8", ".台北Táiběi, .台北", ["utf8class1","utf8class2"] );
99         t( "Descendant class selector using UTF8", "div .台北Táiběi", ["utf8class1"] );
100         t( "Child class selector using UTF8", "form > .台北Táiběi", ["utf8class1"] );
101         
102         t( "Escaped Class", ".foo\\:bar", ["foo:bar"] );
103         t( "Escaped Class", ".test\\.foo\\[5\\]bar", ["test.foo[5]bar"] );
104         t( "Descendant scaped Class", "div .foo\\:bar", ["foo:bar"] );
105         t( "Descendant scaped Class", "div .test\\.foo\\[5\\]bar", ["test.foo[5]bar"] );
106         t( "Child escaped Class", "form > .foo\\:bar", ["foo:bar"] );
107         t( "Child escaped Class", "form > .test\\.foo\\[5\\]bar", ["test.foo[5]bar"] );
108 });
109
110 test("name", function() {
111         expect(7);
112
113         t( "Name selector", "input[name=action]", ["text1"] );
114         t( "Name selector with single quotes", "input[name='action']", ["text1"] );
115         t( "Name selector with double quotes", 'input[name="action"]', ["text1"] );
116
117         t( "Name selector non-input", "*[name=iframe]", ["iframe"] );
118
119         t( "Name selector for grouped input", "input[name='types[]']", ["types_all", "types_anime", "types_movie"] )
120
121         isSet( jQuery("#form").find("input[name=action]"), q("text1"), "Name selector within the context of another element" );
122         isSet( jQuery("#form").find("input[name='foo[bar]']"), q("hidden2"), "Name selector for grouped form element within the context of another element" );
123 });
124
125
126 test("multiple", function() {
127         expect(4);
128         
129         var results = ["mark","simon","firstp","ap","sndp","en","sap","first"];
130         
131         if ( document.querySelectorAll ) {
132                 results = ["firstp","ap","mark","sndp","en","sap","simon","first"];
133         }
134         
135         t( "Comma Support", "a.blog, p", results);
136         t( "Comma Support", "a.blog , p", results);
137         t( "Comma Support", "a.blog ,p", results);
138         t( "Comma Support", "a.blog,p", results);
139 });
140
141 test("child and adjacent", function() {
142         expect(38);
143         t( "Child", "p > a", ["simon1","google","groups","mark","yahoo","simon"] );
144         t( "Child", "p> a", ["simon1","google","groups","mark","yahoo","simon"] );
145         t( "Child", "p >a", ["simon1","google","groups","mark","yahoo","simon"] );
146         t( "Child", "p>a", ["simon1","google","groups","mark","yahoo","simon"] );
147         t( "Child w/ Class", "p > a.blog", ["mark","simon"] );
148         t( "All Children", "code > *", ["anchor1","anchor2"] );
149         t( "All Grandchildren", "p > * > *", ["anchor1","anchor2"] );
150         t( "Adjacent", "a + a", ["groups"] );
151         t( "Adjacent", "a +a", ["groups"] );
152         t( "Adjacent", "a+ a", ["groups"] );
153         t( "Adjacent", "a+a", ["groups"] );
154         t( "Adjacent", "p + p", ["ap","en","sap"] );
155         t( "Comma, Child, and Adjacent", "a + a, code > a", ["groups","anchor1","anchor2"] );
156
157         t( "Non-existant ancestors", ".fototab > .thumbnails > a", [] );
158         
159         t( "First Child", "p:first-child", ["firstp","sndp"] );
160         t( "Nth Child", "p:nth-child(1)", ["firstp","sndp"] );
161         
162         t( "Last Child", "p:last-child", ["sap"] );
163         t( "Last Child", "a:last-child", ["simon1","anchor1","mark","yahoo","anchor2","simon"] );
164         
165         t( "Nth-child", "#main form#form > *:nth-child(2)", ["text2"] );
166         t( "Nth-child", "#main form#form > :nth-child(2)", ["text2"] );
167
168         t( "Nth-child", "#form select:first option:nth-child(3)", ["option1c"] );
169         t( "Nth-child", "#form select:first option:nth-child(0n+3)", ["option1c"] );
170         t( "Nth-child", "#form select:first option:nth-child(1n+0)", ["option1a", "option1b", "option1c", "option1d"] );
171         t( "Nth-child", "#form select:first option:nth-child(1n)", ["option1a", "option1b", "option1c", "option1d"] );
172         t( "Nth-child", "#form select:first option:nth-child(n)", ["option1a", "option1b", "option1c", "option1d"] );
173         t( "Nth-child", "#form select:first option:nth-child(even)", ["option1b", "option1d"] );
174         t( "Nth-child", "#form select:first option:nth-child(odd)", ["option1a", "option1c"] );
175         t( "Nth-child", "#form select:first option:nth-child(2n)", ["option1b", "option1d"] );
176         t( "Nth-child", "#form select:first option:nth-child(2n+1)", ["option1a", "option1c"] );
177         t( "Nth-child", "#form select:first option:nth-child(3n)", ["option1c"] );
178         t( "Nth-child", "#form select:first option:nth-child(3n+1)", ["option1a", "option1d"] );
179         t( "Nth-child", "#form select:first option:nth-child(3n+2)", ["option1b"] );
180         t( "Nth-child", "#form select:first option:nth-child(3n+3)", ["option1c"] );
181         t( "Nth-child", "#form select:first option:nth-child(3n-1)", ["option1b"] );
182         t( "Nth-child", "#form select:first option:nth-child(3n-2)", ["option1a", "option1d"] );
183         t( "Nth-child", "#form select:first option:nth-child(3n-3)", ["option1c"] );
184         t( "Nth-child", "#form select:first option:nth-child(3n+0)", ["option1c"] );
185         t( "Nth-child", "#form select:first option:nth-child(-n+3)", ["option1a", "option1b", "option1c"] );
186 });
187
188 test("attributes", function() {
189         expect(21);
190         t( "Attribute Exists", "a[title]", ["google"] );
191         t( "Attribute Exists", "*[title]", ["google"] );
192         t( "Attribute Exists", "[title]", ["google"] );
193         
194         t( "Attribute Equals", "a[rel='bookmark']", ["simon1"] );
195         t( "Attribute Equals", 'a[rel="bookmark"]', ["simon1"] );
196         t( "Attribute Equals", "a[rel=bookmark]", ["simon1"] );
197         t( "Attribute Equals", "a[href='http://www.google.com/']", ["google"] );
198         
199         var results = ["hidden1","radio1","radio2"];
200         
201         if ( document.querySelectorAll ) {
202                 results = ["radio1", "radio2", "hidden1"];
203         }
204         
205         t( "Multiple Attribute Equals", "#form input[type='hidden'],#form input[type='radio']", results );
206         t( "Multiple Attribute Equals", "#form input[type=\"hidden\"],#form input[type='radio']", results );
207         t( "Multiple Attribute Equals", "#form input[type=hidden],#form input[type=radio]", results );
208         
209         t( "Attribute selector using UTF8", "span[lang=中文]", ["台北"] );
210         
211         t( "Attribute Begins With", "a[href ^= 'http://www']", ["google","yahoo"] );
212         t( "Attribute Ends With", "a[href $= 'org/']", ["mark"] );
213         t( "Attribute Contains", "a[href *= 'google']", ["google","groups"] );
214         
215         t("Select options via :selected", "#select1 option:selected", ["option1a"] );
216         t("Select options via :selected", "#select2 option:selected", ["option2d"] );
217         t("Select options via :selected", "#select3 option:selected", ["option3b", "option3c"] );
218         
219         t( "Grouped Form Elements", "input[name='foo[bar]']", ["hidden2"] );
220         
221         t( ":not() Existing attribute", "#form select:not([multiple])", ["select1", "select2"]);
222         t( ":not() Equals attribute", "#form select:not([name=select1])", ["select2", "select3"]);
223         t( ":not() Equals quoted attribute", "#form select:not([name='select1'])", ["select2", "select3"]);
224 });
225
226 test("pseudo (:) selectors", function() {
227         expect(34);
228         t( "First Child", "p:first-child", ["firstp","sndp"] );
229         t( "Last Child", "p:last-child", ["sap"] );
230         t( "Only Child", "a:only-child", ["simon1","anchor1","yahoo","anchor2"] );
231         t( "Empty", "ul:empty", ["firstUL"] );
232         t( "Enabled UI Element", "#form input:not([type=hidden]):enabled", ["text1","radio1","radio2","check1","check2","hidden2","name"] );
233         t( "Disabled UI Element", "#form input:disabled", ["text2"] );
234         t( "Checked UI Element", "#form input:checked", ["radio2","check1"] );
235         t( "Selected Option Element", "#form option:selected", ["option1a","option2d","option3b","option3c"] );
236         t( "Text Contains", "a:contains('Google')", ["google","groups"] );
237         t( "Text Contains", "a:contains('Google Groups')", ["groups"] );
238         t( "Element Preceded By", "p ~ div", ["foo","fx-queue","fx-tests", "moretests","tabindex-tests"] );
239         t( "Not", "a.blog:not(.link)", ["mark"] );
240         t( "Not - multiple", "#form option:not(:contains('Nothing'),#option1b,:selected)", ["option1c", "option1d", "option2b", "option2c", "option3d", "option3e"] );
241         //t( "Not - complex", "#form option:not([id^='opt']:nth-child(-n+3))", [ "option1a", "option1d", "option2d", "option3d", "option3e"] );
242         t( "Not - recursive", "#form option:not(:not(:selected))[id^='option3']", [ "option3b", "option3c"] );
243         
244         t( "nth Element", "p:nth(1)", ["ap"] );
245         t( "First Element", "p:first", ["firstp"] );
246         t( "Last Element", "p:last", ["first"] );
247         t( "Even Elements", "p:even", ["firstp","sndp","sap"] );
248         t( "Odd Elements", "p:odd", ["ap","en","first"] );
249         t( "Position Equals", "p:eq(1)", ["ap"] );
250         t( "Position Greater Than", "p:gt(0)", ["ap","sndp","en","sap","first"] );
251         t( "Position Less Than", "p:lt(3)", ["firstp","ap","sndp"] );
252         t( "Is A Parent", "p:parent", ["firstp","ap","sndp","en","sap","first"] );
253         t( "Is Visible", "#form input:visible", ["text1","text2","radio1","radio2","check1","check2","name"] );
254         t( "Is Hidden", "#form input:hidden", ["hidden1","hidden2"] );
255         
256         t( "Form element :input", "#form :input", ["text1", "text2", "radio1", "radio2", "check1", "check2", "hidden1", "hidden2", "name", "button", "area1", "select1", "select2", "select3"] );
257         t( "Form element :radio", "#form :radio", ["radio1", "radio2"] );
258         t( "Form element :checkbox", "#form :checkbox", ["check1", "check2"] );
259         t( "Form element :text", "#form :text", ["text1", "text2", "hidden2", "name"] );
260         t( "Form element :radio:checked", "#form :radio:checked", ["radio2"] );
261         t( "Form element :checkbox:checked", "#form :checkbox:checked", ["check1"] );
262         t( "Form element :checkbox:checked, :radio:checked", "#form :checkbox:checked, #form :radio:checked", ["check1", "radio2"] );
263
264         t( "Headers", ":header", ["header", "banner", "userAgent"] );
265         t( "Has Children - :has()", "p:has(a)", ["firstp","ap","en","sap"] );
266 });