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