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