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