]> git.pond.sub.org Git - eow/blob - static/dojo-release-1.1.1/dojo/tests/_base/NodeList.html
add Dojo 1.1.1
[eow] / static / dojo-release-1.1.1 / dojo / tests / _base / NodeList.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2         "http://www.w3.org/TR/html4/strict.dtd">
3 <!--
4         we use a strict-mode DTD to ensure that the box model is the same for these
5         basic tests
6 -->
7 <html>
8         <head>
9                 <style type="text/css">
10                         @import "../../resources/dojo.css";
11                         html, body {
12                                 padding: 0px;
13                                 margin: 0px;
14                                 border: 0px;
15                         }
16
17                         #sq100 {
18                                 background-color: black;
19                                 color: white;
20                                 position: absolute;
21                                 left: 100px;
22                                 top: 100px;
23                                 width: 100px;
24                                 height: 100px;
25                                 border: 0px;
26                                 padding: 0px;
27                                 margin: 0px;
28                                 overflow: hidden;
29                         }
30
31                 </style>
32                 <title>testing dojo.NodeList</title>
33                 <script type="text/javascript" src="../../dojo.js" 
34                         djConfig="isDebug: true, noFirebugLite: true"></script>
35                 <script type="text/javascript">
36                         dojo.require("doh.runner");
37                         dojo.addOnLoad(function(){
38                                 var c = dojo.byId("c1");
39                                 var t = dojo.byId("t"); 
40                                 var s = dojo.byId("sq100"); 
41                                 var fourElementNL = new dojo.NodeList(c, t, c, t);
42                                 doh.register("t", 
43                                         [
44                                                 // constructor tests
45                                                 function ctor(){
46                                                         var nl = new dojo.NodeList();
47                                                         nl.push(c);
48                                                         doh.is(1, nl.length);
49                                                 },
50                                                 function ctorArgs(){
51                                                         var nl = new dojo.NodeList(4);
52                                                         nl.push(c);
53                                                         doh.is(5, nl.length);
54                                                 },
55                                                 function ctorArgs2(){
56                                                         var nl = new dojo.NodeList(c, t);
57                                                         doh.is(2, nl.length);
58                                                         doh.is(c, nl[0]);
59                                                         doh.is(t, nl[1]);
60                                                 },
61                                                 // iteration and array tests
62                                                 function forEach(){
63                                                         var lastItem;
64                                                         var nl = new dojo.NodeList(c, t);
65                                                         nl.forEach(function(i){ lastItem = i; });
66                                                         doh.is(t, lastItem);
67
68                                                         var r = nl.forEach(function(i, idx, arr){
69                                                                 doh.t(arr.constructor == dojo.NodeList);
70                                                                 doh.is(2, arr.length);
71                                                         });
72                                                         doh.t(r.constructor == dojo.NodeList);
73                                                         doh.is(r, nl);
74                                                 },
75                                                 
76                                                 function indexOf(){
77                                                         doh.is(0, fourElementNL.indexOf(c));
78                                                         doh.is(1, fourElementNL.indexOf(t));
79                                                         doh.is(-1, fourElementNL.indexOf(null));
80                                                 },
81                                                 
82                                                 function lastIndexOf(){
83                                                         doh.is(2, fourElementNL.lastIndexOf(c));
84                                                         doh.is(3, fourElementNL.lastIndexOf(t));
85                                                         doh.is(-1, fourElementNL.lastIndexOf(null));
86                                                 },
87                                                 
88                                                 function every(){
89                                                         var ctr = 0;
90                                                         var ret = fourElementNL.every(function(){
91                                                                 ctr++;
92                                                                 return true;
93                                                         });
94                                                         doh.is(4, ctr);
95                                                         doh.t(ret);
96
97                                                         ctr = 0;
98                                                         var ret = fourElementNL.every(function(){
99                                                                 ctr++;
100                                                                 return false;
101                                                         });
102                                                         doh.is(1, ctr);
103                                                         doh.f(ret);
104                                                 },
105                                                 
106                                                 function some(){
107                                                         var ret = fourElementNL.some(function(){
108                                                                 return true;
109                                                         });
110                                                         doh.t(ret);
111
112                                                         var ret = fourElementNL.some(function(i){
113                                                                 return (i.id == "t");
114                                                         });
115                                                         doh.t(ret);
116                                                 },
117                                                 
118                                                 function map(){
119                                                         var ret = fourElementNL.map(function(){
120                                                                 return true;
121                                                         });
122
123                                                         doh.is(ret, [true, true, true, true]);
124                                                         var cnt = 0;
125                                                         var ret = fourElementNL.map(function(){
126                                                                 return cnt++;
127                                                         });
128                                                         // doh.is(ret, [0, 1, 2, 3]);
129
130                                                         doh.t(ret.constructor == dojo.NodeList);
131
132                                                         // make sure that map() returns a NodeList
133                                                         var sum = 0;
134                                                         fourElementNL.map(function(){ return 2; }).forEach( function(x){ sum += x; } );
135                                                         doh.is(sum, 8);
136                                                 },
137
138                                                 function slice(){
139                                                         var pnl = new dojo.NodeList(t, t, c);
140                                                         doh.is(2, pnl.slice(1).length);
141                                                         doh.is(3, pnl.length);
142                                                         doh.is(c, pnl.slice(-1)[0]);
143                                                         doh.is(2, pnl.slice(-2).length);
144                                                 },
145
146                                                 function splice(){
147                                                         var pnl = new dojo.NodeList(t, t, c);
148                                                         console.debug(pnl.splice(1));
149                                                         /*
150                                                         doh.is(2, pnl.splice(1).length);
151                                                         doh.is(1, pnl.length);
152                                                         pnl = new dojo.NodeList(t, t, c);
153                                                         doh.is(c, pnl.splice(-1)[0]);
154                                                         doh.is(2, pnl.length);
155                                                         pnl = new dojo.NodeList(t, t, c);
156                                                         doh.is(2, pnl.splice(-2).length);
157                                                         */
158                                                 },
159
160                                                 function spliceInsert(){
161                                                         // insert 1
162                                                         var pnl = new dojo.NodeList(t, t, c);
163                                                         pnl.splice(0, 0, c);
164                                                         doh.is(4, pnl.length);
165                                                         doh.is(c, pnl[0]);
166
167                                                         // insert multiple
168                                                         pnl = new dojo.NodeList(t, t, c);
169                                                         pnl.splice(0, 0, c, s);
170                                                         doh.is(5, pnl.length);
171                                                         doh.is(c, pnl[0]);
172                                                         doh.is(s, pnl[1]);
173                                                         doh.is(t, pnl[2]);
174
175                                                         // insert multiple at offset
176                                                         pnl = new dojo.NodeList(t, t, c);
177                                                         pnl.splice(1, 0, c, s);
178                                                         doh.is(5, pnl.length);
179                                                         doh.is(t, pnl[0]);
180                                                         doh.is(c, pnl[1]);
181                                                         doh.is(s, pnl[2]);
182                                                         doh.is(t, pnl[3]);
183                                                 },
184
185                                                 function spliceDel(){
186                                                         // clobbery 1
187                                                         var pnl = new dojo.NodeList(c, t, s);
188                                                         pnl.splice(0, 1);
189                                                         doh.is(2, pnl.length);
190                                                         doh.is(t, pnl[0]);
191
192                                                         // clobber multiple
193                                                         pnl = new dojo.NodeList(c, t, s);
194                                                         pnl.splice(0, 2);
195                                                         doh.is(1, pnl.length);
196                                                         doh.is(s, pnl[0]);
197
198                                                         // ...at an offset
199                                                         pnl = new dojo.NodeList(c, t, s);
200                                                         pnl.splice(1, 1);
201                                                         doh.is(2, pnl.length);
202                                                         doh.is(c, pnl[0]);
203                                                         doh.is(s, pnl[1]);
204
205                                                 },
206
207                                                 function spliceInsertDel(){
208                                                         // clobbery 1
209                                                         var pnl = new dojo.NodeList(c, t, s);
210                                                         pnl.splice(1, 1, s);
211                                                         doh.is(3, pnl.length);
212                                                         doh.is(dojo.NodeList(c, s, s), pnl);
213
214                                                         pnl = new dojo.NodeList(c, t, s);
215                                                         pnl.splice(1, 2, s);
216                                                         doh.is(2, pnl.length);
217                                                         doh.is(dojo.NodeList(c, s), pnl);
218                                                 },
219
220                                                 // sub-search
221                                                 function query(){
222                                                         var pnl = new dojo.NodeList(t);
223                                                         doh.is(c, pnl.query("span")[0]);
224                                                         doh.is(t, dojo.query("body").query(":last-child")[0]);
225                                                         doh.is(c, dojo.query("body").query(":last-child")[1]);
226                                                         doh.is(1, pnl.query().length);
227                                                 },
228                                                 
229                                                 function filter(){
230                                                         doh.is(dojo.query("body :first-child").filter(":last-child")[0], c);
231                                                         doh.is(1, dojo.query("*").filter(function(n){ return (n.nodeName.toLowerCase() == "span"); }).length);
232
233                                                         var filterObj = {
234                                                                 filterFunc: function(n){
235                                                                         return (n.nodeName.toLowerCase() == "span");
236                                                                 }
237                                                         };
238                                                         doh.is(1, dojo.query("*").filter(filterObj.filterFunc).length);
239                                                         doh.is(1, dojo.query("*").filter(filterObj.filterFunc, filterObj).length);
240                                                 },
241
242                                                 // layout DOM functions
243                                                 function coords(){
244                                                         var tnl = new dojo.NodeList(dojo.byId('sq100'))
245                                                         doh.t(dojo.isArray(tnl));
246                                                         doh.is(100, tnl.coords()[0].w);
247                                                         doh.is(100, tnl.coords()[0].h);
248                                                         doh.is(document.body.getElementsByTagName("*").length, dojo.query("body *").coords().length);
249                                                 },
250                                                 
251                                                 function styleGet(){
252                                                         // test getting
253                                                         var tnl = new dojo.NodeList(s);
254                                                         doh.is(1, tnl.style("opacity")[0]);
255                                                         tnl.push(t);
256                                                         dojo.style(t, "opacity", 0.5);
257                                                         doh.is(0.5, tnl.style("opacity").slice(-1)[0]);
258                                                         tnl.style("opacity", 1);
259                                                 },
260
261                                                 function styleSet(){
262                                                         // test setting
263                                                         var tnl = new dojo.NodeList(s, t);
264                                                         tnl.style("opacity", 0.5);
265                                                         doh.is(0.5, dojo.style(tnl[0], "opacity"));
266                                                         doh.is(0.5, dojo.style(tnl[1], "opacity"));
267                                                         // reset
268                                                         tnl.style("opacity", 1);
269                                                 },
270                                                 
271                                                 function style(){
272                                                         var tnl = new dojo.NodeList(s, t);
273                                                         tnl.style("opacity", 1);
274                                                         doh.is(1, tnl.style("opacity")[0]);
275                                                         dojo.style(t, "opacity", 0.5);
276                                                         doh.is(1.0, tnl.style("opacity")[0]);
277                                                         doh.is(0.5, tnl.style("opacity")[1]);
278                                                         // reset things
279                                                         tnl.style("opacity", 1);
280                                                 },
281
282                                                 function concat(){
283                                                         var spans = dojo.query("span");
284                                                         var divs = dojo.query("div");
285                                                         console.debug(spans.concat(divs));
286                                                         doh.is(spans.concat(divs).constructor, dojo.NodeList);
287                                                         doh.is((divs.length + spans.length), spans.concat(divs).length);
288                                                 },
289
290                                                 function concat2(t){
291                                                         var spans = dojo.query("span");
292                                                         var divs = dojo.query("div");
293                                                         doh.is(spans.concat([]).constructor, dojo.NodeList);
294                                                 },
295                                                 
296                                                 function place(t){
297                                                         var ih = "<div><span></span></div><span class='thud'><b>blah</b></span>";
298                                                         
299                                                         var tn = document.createElement("div");
300                                                         tn.innerHTML = ih;
301                                                         dojo.body().appendChild(tn);
302                                                         var nl = dojo.query("b", tn).place(tn, "first");
303                                                         doh.t(nl.constructor == dojo.NodeList);
304                                                         doh.is(1, nl.length);
305                                                         doh.is("b", nl[0].nodeName.toLowerCase());
306                                                         doh.is(tn, nl[0].parentNode);
307                                                         doh.is(tn.firstChild, nl[0]);
308                                                 },
309                                                 
310                                                 function orphan(t){
311                                                         var ih = "<div><span></span></div><span class='thud'><b>blah</b></span>";
312                                                         
313                                                         var tn = document.createElement("div");
314                                                         tn.innerHTML = ih;
315                                                         dojo.body().appendChild(tn);
316                                                         var nl = dojo.query("span", tn).orphan();
317                                                         doh.t(nl.constructor == dojo.NodeList);
318
319                                                         doh.is(2, nl.length);
320                                                         doh.is(1, tn.getElementsByTagName("*").length);
321
322                                                         tn.innerHTML = ih;
323                                                         var nl = dojo.query("*", tn).orphan("b");
324                                                         doh.is(1, nl.length);
325                                                         doh.is("blah", nl[0].innerHTML);
326                                                 },
327
328                                                 /*                                              
329                                                 // FIXME
330                                                 function adopt(t){
331                                                 },
332                                                 
333                                                 function addContent(t){
334                                                 },
335                                                 */
336                                                 
337                                                 function connect(t){
338                                                         var ih = "<div><span></span></div><span class='thud'><button>blah</button></span>";
339                                                         
340                                                         var tn = document.createElement("div");
341                                                         tn.innerHTML = ih;
342                                                         dojo.body().appendChild(tn);
343
344                                                         var ctr = 0;
345                                                         var nl = dojo.query("button", tn).connect("onclick", function(){
346                                                                 ctr++;
347                                                         });
348                                                         nl[0].click();
349                                                         doh.is(1, ctr);
350                                                         nl[0].click();
351                                                         nl[0].click();
352                                                         doh.is(3, ctr);
353                                                 }
354                                         ]
355                                 );
356                                 doh.run();
357                         });
358                 </script>
359         </head>
360         <body>
361                 <h1>testing dojo.NodeList</h1>
362                 <div id="sq100">
363                         100px square, abs
364                 </div>
365                 <div id="t">
366                         <span id="c1">c1</span>
367                 </div>
368         </body>
369 </html>
370