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