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