1 | <!DOCTYPE html> |
---|
2 | <html> |
---|
3 | <head> |
---|
4 | <title>testing dojo/_base/query</title> |
---|
5 | <style type="text/css"> |
---|
6 | @import "../../resources/dojo.css"; |
---|
7 | </style> |
---|
8 | <script type="text/javascript" src="../../dojo.js" data-dojo-config="isDebug: true"></script> |
---|
9 | <script type="text/javascript"> |
---|
10 | require(["dojo", "doh", "dojo/io/iframe", "dojo/domReady!"], function(dojo, doh){ |
---|
11 | function createDocument(xml){ |
---|
12 | var fauxXhr = { responseText: xml }; |
---|
13 | if("DOMParser" in dojo.global){ |
---|
14 | var parser = new DOMParser(); |
---|
15 | fauxXhr.responseXML = parser.parseFromString(xml, "text/xml"); |
---|
16 | } |
---|
17 | // kludge: use dojo.xhr contentHandler for XML to process IE XMLDOC as needed |
---|
18 | return dojo._contentHandlers["xml"](fauxXhr); // DOMDocument |
---|
19 | } |
---|
20 | |
---|
21 | // Legacy tests for dojo/_base/query. Replaced by dojo/tests/query/query.html. |
---|
22 | // TODO: remove in 2.0. |
---|
23 | doh.register("t", |
---|
24 | [ |
---|
25 | // basic sanity checks |
---|
26 | "doh.is(4, (dojo.query('h3')).length);", |
---|
27 | "doh.is(1, (dojo.query('h1:first-child')).length);", |
---|
28 | "doh.is(2, (dojo.query('h3:first-child')).length);", |
---|
29 | "doh.is(1, (dojo.query('#t')).length);", |
---|
30 | //"doh.is(1, (dojo.query('#bug')).length);", |
---|
31 | "doh.is(4, (dojo.query('#t h3')).length);", |
---|
32 | "doh.is(1, (dojo.query('div#t')).length);", |
---|
33 | "doh.is(4, (dojo.query('div#t h3')).length);", |
---|
34 | "doh.is(0, (dojo.query('span#t')).length);", |
---|
35 | "doh.is(0, (dojo.query('.bogus')).length);", |
---|
36 | "doh.is(0, (dojo.query('.bogus', dojo.byId('container'))).length);", |
---|
37 | "doh.is(0, (dojo.query('#bogus')).length);", |
---|
38 | "doh.is(0, (dojo.query('#bogus', dojo.byId('container'))).length);", |
---|
39 | "doh.is(1, (dojo.query('#t div > h3')).length);", |
---|
40 | "doh.is(2, (dojo.query('.foo')).length);", |
---|
41 | "doh.is(1, (dojo.query('.foo.bar')).length);", |
---|
42 | "doh.is(2, (dojo.query('.baz')).length);", |
---|
43 | "doh.is(3, (dojo.query('#t > h3')).length);", |
---|
44 | |
---|
45 | "doh.is(2, (dojo.query('#baz,#foo,#t')).length);", |
---|
46 | |
---|
47 | "doh.is(1, dojo.query('.fooBar').length);", |
---|
48 | |
---|
49 | // syntactic equivalents |
---|
50 | "doh.is(12, (dojo.query('#t > *')).length);", |
---|
51 | "doh.is(12, (dojo.query('#t >')).length);", |
---|
52 | "doh.is(3, (dojo.query('.foo >')).length);", |
---|
53 | "doh.is(3, (dojo.query('.foo > *')).length);", |
---|
54 | |
---|
55 | // with a root, by ID |
---|
56 | "doh.is(3, (dojo.query('> *', 'container')).length);", |
---|
57 | "doh.is(3, (dojo.query('> h3', 't')).length);", |
---|
58 | |
---|
59 | // compound queries |
---|
60 | "doh.is(2, (dojo.query('.foo, .bar')).length);", |
---|
61 | "doh.is(2, (dojo.query('.foo,.bar')).length);", |
---|
62 | |
---|
63 | // multiple class attribute |
---|
64 | "doh.is(1, (dojo.query('.foo.bar')).length);", |
---|
65 | "doh.is(2, (dojo.query('.foo')).length);", |
---|
66 | "doh.is(2, (dojo.query('.baz')).length);", |
---|
67 | |
---|
68 | // case sensitivity |
---|
69 | "doh.is(1, (dojo.query('span.baz')).length);", |
---|
70 | "doh.is(1, (dojo.query('sPaN.baz')).length);", |
---|
71 | "doh.is(1, (dojo.query('SPAN.baz')).length);", |
---|
72 | "doh.is(1, (dojo.query('[class = \"foo bar\"]')).length);", |
---|
73 | "doh.is(2, (dojo.query('[foo~=\"bar\"]')).length);", |
---|
74 | "doh.is(2, (dojo.query('[ foo ~= \"bar\" ]')).length);", |
---|
75 | |
---|
76 | // "t.is(0, (dojo.query('[ foo ~= \"\\'bar\\'\" ]')).length);", |
---|
77 | "doh.is(3, (dojo.query('[foo]')).length);", |
---|
78 | "doh.is(1, (dojo.query('[foo$=\"thud\"]')).length);", |
---|
79 | "doh.is(1, (dojo.query('[foo$=thud]')).length);", |
---|
80 | "doh.is(1, (dojo.query('[foo$=\"thudish\"]')).length);", |
---|
81 | "doh.is(1, (dojo.query('[id$=\\'55555\\']')).length);", |
---|
82 | "doh.is(1, (dojo.query('#t [foo$=thud]')).length);", |
---|
83 | "doh.is(1, (dojo.query('#t [ title $= thud ]')).length);", |
---|
84 | "doh.is(0, (dojo.query('#t span[ title $= thud ]')).length);", |
---|
85 | "doh.is(2, (dojo.query('[foo|=\"bar\"]')).length);", |
---|
86 | "doh.is(1, (dojo.query('[foo|=\"bar-baz\"]')).length);", |
---|
87 | "doh.is(0, (dojo.query('[foo|=\"baz\"]')).length);", |
---|
88 | "doh.is(dojo.byId('_foo'), dojo.query('.foo:nth-child(2)')[0]);", |
---|
89 | "doh.is(dojo.query('style')[0], dojo.query(':nth-child(2)')[0]);", |
---|
90 | |
---|
91 | // descendant selectors |
---|
92 | "doh.is(3, dojo.query('>', 'container').length);", |
---|
93 | "doh.is(0, dojo.query('> .not-there').length);", |
---|
94 | "doh.is(3, dojo.query('> *', 'container').length);", |
---|
95 | "doh.is(2, dojo.query('> [qux]', 'container').length);", |
---|
96 | "doh.is('child1', dojo.query('> [qux]', 'container')[0].id);", |
---|
97 | "doh.is('child3', dojo.query('> [qux]', 'container')[1].id);", |
---|
98 | "doh.is(3, dojo.query('>', 'container').length);", |
---|
99 | "doh.is(3, dojo.query('> *', 'container').length);", |
---|
100 | "doh.is(3, dojo.query('>*', 'container').length);", |
---|
101 | "doh.is('passed', dojo.query('#bug')[0].value);", |
---|
102 | |
---|
103 | // bug 9071 |
---|
104 | "doh.is(2, (dojo.query('a', 't4')).length);", |
---|
105 | "doh.is(2, (dojo.query('p a', 't4')).length);", |
---|
106 | "doh.is(2, (dojo.query('div p', 't4')).length);", |
---|
107 | "doh.is(2, (dojo.query('div p a', 't4')).length);", |
---|
108 | "doh.is(2, (dojo.query('.subA', 't4')).length);", |
---|
109 | "doh.is(2, (dojo.query('.subP .subA', 't4')).length);", |
---|
110 | "doh.is(2, (dojo.query('.subDiv .subP', 't4')).length);", |
---|
111 | "doh.is(2, (dojo.query('.subDiv .subP .subA', 't4')).length);", |
---|
112 | |
---|
113 | |
---|
114 | // failed scope arg |
---|
115 | "doh.is(0, (dojo.query('*', 'thinger')).length);", |
---|
116 | "doh.is(0, (dojo.query('div#foo').length));", |
---|
117 | |
---|
118 | // sibling selectors |
---|
119 | "doh.is(1, dojo.query('+', 'container').length);", |
---|
120 | "doh.is(3, dojo.query('~', 'container').length);", |
---|
121 | "doh.is(1, (dojo.query('.foo+ span')).length);", |
---|
122 | "doh.is(1, (dojo.query('.foo+span')).length);", |
---|
123 | "doh.is(1, (dojo.query('.foo +span')).length);", |
---|
124 | "doh.is(1, (dojo.query('.foo + span')).length);", |
---|
125 | "doh.is(4, (dojo.query('.foo~ span')).length);", |
---|
126 | "doh.is(4, (dojo.query('.foo~span')).length);", |
---|
127 | "doh.is(4, (dojo.query('.foo ~span')).length);", |
---|
128 | "doh.is(4, (dojo.query('.foo ~ span')).length);", |
---|
129 | "doh.is(1, (dojo.query('#foo~ *')).length);", |
---|
130 | "doh.is(1, (dojo.query('#foo ~*')).length);", |
---|
131 | "doh.is(1, (dojo.query('#foo ~*')).length);", |
---|
132 | "doh.is(1, (dojo.query('#foo ~ *')).length);", |
---|
133 | "doh.is(1, (dojo.query('#foo ~')).length);", |
---|
134 | "doh.is(1, (dojo.query('#foo~')).length);", |
---|
135 | |
---|
136 | // sub-selector parsing |
---|
137 | "doh.is(1, dojo.query('#t span.foo:not(span:first-child)').length);", |
---|
138 | "doh.is(1, dojo.query('#t span.foo:not(:first-child)').length);", |
---|
139 | |
---|
140 | // nth-child tests |
---|
141 | "doh.is(2, dojo.query('#t > h3:nth-child(odd)').length);", |
---|
142 | "doh.is(3, dojo.query('#t h3:nth-child(odd)').length);", |
---|
143 | "doh.is(3, dojo.query('#t h3:nth-child(2n+1)').length);", |
---|
144 | "doh.is(1, dojo.query('#t h3:nth-child(even)').length);", |
---|
145 | "doh.is(1, dojo.query('#t h3:nth-child(2n)').length);", |
---|
146 | "doh.is(1, dojo.query('#t h3:nth-child(2n+3)').length);", |
---|
147 | "doh.is(2, dojo.query('#t h3:nth-child(1)').length);", |
---|
148 | "doh.is(1, dojo.query('#t > h3:nth-child(1)').length);", |
---|
149 | "doh.is(3, dojo.query('#t :nth-child(3)').length);", |
---|
150 | "doh.is(0, dojo.query('#t > div:nth-child(1)').length);", |
---|
151 | "doh.is(7, dojo.query('#t span').length);", |
---|
152 | "doh.is(3, dojo.query('#t > *:nth-child(n+10)').length);", |
---|
153 | "doh.is(1, dojo.query('#t > *:nth-child(n+12)').length);", |
---|
154 | "doh.is(10, dojo.query('#t > *:nth-child(-n+10)').length);", |
---|
155 | "doh.is(5, dojo.query('#t > *:nth-child(-2n+10)').length);", |
---|
156 | "doh.is(6, dojo.query('#t > *:nth-child(2n+2)').length);", |
---|
157 | "doh.is(5, dojo.query('#t > *:nth-child(2n+4)').length);", |
---|
158 | "doh.is(5, dojo.query('#t > *:nth-child(2n+4)').length);", |
---|
159 | "doh.is(5, dojo.query('#t> *:nth-child(2n+4)').length);", |
---|
160 | "doh.is(12, dojo.query('#t > *:nth-child(n-5)').length);", |
---|
161 | "doh.is(12, dojo.query('#t >*:nth-child(n-5)').length);", |
---|
162 | "doh.is(6, dojo.query('#t > *:nth-child(2n-5)').length);", |
---|
163 | "doh.is(6, dojo.query('#t>*:nth-child(2n-5)').length);", |
---|
164 | |
---|
165 | // :checked pseudo-selector |
---|
166 | "doh.is(2, dojo.query('#t2 > :checked').length);", |
---|
167 | "doh.is(dojo.byId('checkbox2'), dojo.query('#t2 > input[type=checkbox]:checked')[0]);", |
---|
168 | "doh.is(dojo.byId('radio2'), dojo.query('#t2 > input[type=radio]:checked')[0]);", |
---|
169 | "doh.is(2, dojo.query('#t2select option:checked').length);", |
---|
170 | |
---|
171 | // special characters in attribute values |
---|
172 | function attrSpecialChars(){ |
---|
173 | // bug 10651 |
---|
174 | doh.is(1, dojo.query('option[value=a+b]', 'attrSpecialChars').length, "value=a+b"); |
---|
175 | doh.is(1, dojo.query('option[value=a~b]', 'attrSpecialChars').length, "value=a~b"); |
---|
176 | doh.is(1, dojo.query('option[value=a^b]', 'attrSpecialChars').length, "value=a^b"); |
---|
177 | doh.is(1, dojo.query('option[value=\"a+b\"]', 'attrSpecialChars').length, "value=\"a^b\""); |
---|
178 | doh.is(1, dojo.query('option[value=\"a~b\"]', 'attrSpecialChars').length, "value=\"a~b\""); |
---|
179 | doh.is(1, dojo.query('option[value=\"a^b\"]', 'attrSpecialChars').length, "value=\"a^b\""); |
---|
180 | |
---|
181 | // selector with substring that contains equals sign (bug 7479) |
---|
182 | doh.is(1, dojo.query("a[href*='foo=bar']", 'attrSpecialChars').length, "a[href*='foo=bar']"); |
---|
183 | |
---|
184 | // selector with substring that contains brackets (bug 9193, 11189, 13084) |
---|
185 | // note: inner quotes don't work in second dojo.query(), not sure why |
---|
186 | doh.is(1, dojo.query('input[name="data[foo][bar]"]', 'attrSpecialChars').length, "data[foo][bar]"); |
---|
187 | doh.is(1, dojo.query('input[name=data\\[foo\\]\\[bar\\]]', 'attrSpecialChars').length, "data\\[foo\\]\\[bar\\]"); |
---|
188 | doh.is(1, dojo.query('input[name="foo\\[0\\].bar"]', 'attrSpecialChars').length, "foo[0].bar"); |
---|
189 | doh.is(1, dojo.query('input[name="test[0]"]', 'attrSpecialChars').length, "test[0]"); |
---|
190 | }, |
---|
191 | |
---|
192 | // check for correct document order |
---|
193 | function domOrder() { |
---|
194 | var inputs = dojo.query(".upperclass .lowerclass input"); |
---|
195 | doh.is("notbug", inputs[0].id); |
---|
196 | doh.is("bug", inputs[1].id); |
---|
197 | doh.is("checkbox1", inputs[2].id); |
---|
198 | doh.is("checkbox2", inputs[3].id); |
---|
199 | doh.is("radio1", inputs[4].id); |
---|
200 | doh.is("radio2", inputs[5].id); |
---|
201 | doh.is("radio3", inputs[6].id); |
---|
202 | }, |
---|
203 | |
---|
204 | // cross-document queries |
---|
205 | { |
---|
206 | |
---|
207 | name: "crossDocumentQuery", |
---|
208 | setUp: function(){ |
---|
209 | this.t3 = window.frames["t3"]; |
---|
210 | this.doc = dojo.io.iframe.doc(t3); |
---|
211 | this.doc.open(); |
---|
212 | this.doc.write([ |
---|
213 | "<html><head>", |
---|
214 | "<title>inner document</title>", |
---|
215 | "</head>", |
---|
216 | "<body>", |
---|
217 | "<div id='st1'><h3>h3 <span>span <span> inner <span>inner-inner</span></span></span> endh3 </h3></div>", |
---|
218 | "</body>", |
---|
219 | "</html>" |
---|
220 | ].join("")); |
---|
221 | }, |
---|
222 | runTest: function(){ |
---|
223 | doh.is(1, dojo.query('h3', dojo.byId("st1", this.doc)).length); |
---|
224 | // use a long query to force a test of the XPath system on FF. see bug #7075 |
---|
225 | doh.is(1, dojo.query('h3 > span > span > span', dojo.byId("st1", this.doc)).length); |
---|
226 | doh.is(1, dojo.query('h3 > span > span > span', this.doc.body.firstChild).length); |
---|
227 | } |
---|
228 | }, |
---|
229 | |
---|
230 | // :empty pseudo-selector |
---|
231 | "doh.is(4, dojo.query('#t > span:empty').length);", |
---|
232 | "doh.is(6, dojo.query('#t span:empty').length);", |
---|
233 | "doh.is(0, dojo.query('h3 span:empty').length);", |
---|
234 | "doh.is(1, dojo.query('h3 :not(:empty)').length);", |
---|
235 | |
---|
236 | // escaping of ":" chars inside an ID |
---|
237 | function silly_IDs1(){ |
---|
238 | doh.t(document.getElementById("silly:id::with:colons")); |
---|
239 | doh.is(1, dojo.query("#silly\\:id\\:\\:with\\:colons").length); |
---|
240 | }, |
---|
241 | function NodeList_identity(){ |
---|
242 | var foo = new dojo.NodeList([dojo.byId("container")]); |
---|
243 | doh.is(foo, dojo.query(foo)); |
---|
244 | }, |
---|
245 | function xml(){ |
---|
246 | var doc = createDocument([ |
---|
247 | "<ResultSet>", |
---|
248 | "<Result>One</Result>", |
---|
249 | "<RESULT>Two</RESULT>", |
---|
250 | "<result>Three</result>", |
---|
251 | "<result>Four</result>", |
---|
252 | "</ResultSet>" |
---|
253 | ].join("") |
---|
254 | ); |
---|
255 | var de = doc.documentElement; |
---|
256 | |
---|
257 | doh.is(2, dojo.query("result", de).length, "all lower"); |
---|
258 | doh.is(1, dojo.query("Result", de).length, "mixed case"); |
---|
259 | doh.is(1, dojo.query("RESULT", de).length, "all upper"); |
---|
260 | doh.is(0, dojo.query("resulT", de).length, "no match"); |
---|
261 | doh.is(0, dojo.query("rEsulT", de).length, "no match"); |
---|
262 | }, |
---|
263 | function xml_attrs(){ |
---|
264 | var doc = createDocument([ |
---|
265 | "<ResultSet>", |
---|
266 | "<RESULT thinger='blah'>ONE</RESULT>", |
---|
267 | "<RESULT thinger='gadzooks'><CHILD>Two</CHILD></RESULT>", |
---|
268 | "</ResultSet>" |
---|
269 | ].join("")); |
---|
270 | var de = doc.documentElement; |
---|
271 | |
---|
272 | doh.is(2, dojo.query("RESULT", de).length, "result elements"); |
---|
273 | doh.is(0, dojo.query("RESULT[THINGER]", de).length, "result elements with attrs (wrong)"); |
---|
274 | doh.is(2, dojo.query("RESULT[thinger]", de).length, "result elements with attrs"); |
---|
275 | doh.is(1, dojo.query("RESULT[thinger=blah]", de).length, "result elements with attr value"); |
---|
276 | doh.is(1, dojo.query("RESULT > CHILD", de).length, "Using child operator"); |
---|
277 | }, |
---|
278 | function sort(){ |
---|
279 | var i = dojo.query("div"); |
---|
280 | // smoke test |
---|
281 | i.sort(function(a,b){ return 1; }) |
---|
282 | } |
---|
283 | ] |
---|
284 | ); |
---|
285 | doh.runOnLoad(); |
---|
286 | }); |
---|
287 | </script> |
---|
288 | </head> |
---|
289 | <body class="upperclass"> |
---|
290 | <h1>Back-compat tests for dojo.query()</h1> |
---|
291 | <div id="t" class="lowerclass"> |
---|
292 | <h3>h3 <span>span</span> endh3 </h3> |
---|
293 | <!-- comment to throw things off --> |
---|
294 | <div class="foo bar" id="_foo"> |
---|
295 | <h3>h3</h3> |
---|
296 | <span id="foo"></span> |
---|
297 | <span></span> |
---|
298 | </div> |
---|
299 | <h3>h3</h3> |
---|
300 | <h3 class="baz foobar" title="thud">h3</h3> |
---|
301 | <span class="fooBar baz foo"></span> |
---|
302 | <span foo="bar"></span> |
---|
303 | <span foo="baz bar thud"></span> |
---|
304 | <!-- FIXME: should foo="bar-baz-thud" match? [foo$=thud] ??? --> |
---|
305 | <span foo="bar-baz-thudish" id="silly:id::with:colons"></span> |
---|
306 | <div id="container"> |
---|
307 | <div id="child1" qux="true"></div> |
---|
308 | <div id="child2"></div> |
---|
309 | <div id="child3" qux="true"></div> |
---|
310 | </div> |
---|
311 | <div qux="true"></div> |
---|
312 | <input id="notbug" name="bug" type="hidden" value="failed"> |
---|
313 | <input id="bug" type="hidden" value="passed"> |
---|
314 | </div> |
---|
315 | <div id="t2" class="lowerclass"> |
---|
316 | <input type="checkbox" name="checkbox1" id="checkbox1" value="foo"> |
---|
317 | <input type="checkbox" name="checkbox2" id="checkbox2" value="bar" checked> |
---|
318 | |
---|
319 | <input type="radio" name="radio" id="radio1" value="thinger"> |
---|
320 | <input type="radio" name="radio" id="radio2" value="stuff" checked> |
---|
321 | <input type="radio" name="radio" id="radio3" value="blah"> |
---|
322 | </div> |
---|
323 | <select id="t2select" multiple="multiple"> |
---|
324 | <option>0</option> |
---|
325 | <option selected="selected">1</option> |
---|
326 | <option selected="selected">2</option> |
---|
327 | </select> |
---|
328 | |
---|
329 | <iframe id="t3" name="t3" src="../../resources/blank.html"></iframe> |
---|
330 | <div id="t4"> |
---|
331 | <div id="one" class="subDiv"> |
---|
332 | <p class="one subP"><a class="subA">one</a></p> |
---|
333 | <div id="two" class="subDiv"> |
---|
334 | <p class="two subP"><a class="subA">two</a></p> |
---|
335 | </div> |
---|
336 | </div> |
---|
337 | </div> |
---|
338 | <div id='other'> |
---|
339 | <div id='abc55555'></div> |
---|
340 | <div id='abd55555efg'></div> |
---|
341 | <div id='55555abc'></div> |
---|
342 | <div id='1'></div> |
---|
343 | <div id='2c'></div> |
---|
344 | <div id='3ch'></div> |
---|
345 | <div id='4chr'></div> |
---|
346 | <div id='5char'></div> |
---|
347 | <div id='6chars'></div> |
---|
348 | </div> |
---|
349 | |
---|
350 | <div id="attrSpecialChars"> |
---|
351 | <select name="special"> |
---|
352 | <!-- tests for special characters in attribute values (characters that are part of query syntax) --> |
---|
353 | <option value="a+b">1</option> |
---|
354 | <option value="a~b">2</option> |
---|
355 | <option value="a^b">3</option> |
---|
356 | </select> |
---|
357 | |
---|
358 | <!-- tests for quotes in attribute values --> |
---|
359 | <a href="foo=bar">hi</a> |
---|
360 | <!-- tests for brackets in attribute values --> |
---|
361 | <input name="data[foo][bar]"> |
---|
362 | <!-- attribute name with a dot, goes down separate code path --> |
---|
363 | <input name="foo[0].bar"> |
---|
364 | <input name="test[0]"> |
---|
365 | </div> |
---|
366 | </body> |
---|
367 | </html> |
---|
368 | |
---|