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