1 | <!DOCTYPE html> |
---|
2 | <html> |
---|
3 | <head> |
---|
4 | <title>Parser Unit Test</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, async:true"></script> |
---|
9 | <script type="text/javascript"> |
---|
10 | require(["dojo/_base/kernel", "dojo/_base/lang", "dojo/_base/array", "dojo/_base/declare", "dojo/_base/html", "dojo/Stateful", "dojo/Evented", |
---|
11 | "doh", "dojo/date/stamp", "dojo/aspect", "dojo/parser", "dojo/domReady!"], |
---|
12 | function(dojo, dlang, darray, declare, dhtml, Stateful, Evented, doh, dstamp, aspect, parser){ |
---|
13 | |
---|
14 | var mixin = dojo.mixin, |
---|
15 | extend = dojo.extend, |
---|
16 | exists = dojo.exists; |
---|
17 | |
---|
18 | declare("tests.parser.Widget", null, { |
---|
19 | constructor: function(args, node){ |
---|
20 | this.params = args; |
---|
21 | } |
---|
22 | }); |
---|
23 | |
---|
24 | declare("tests.parser.Class1", null, { |
---|
25 | constructor: function(args, node){ |
---|
26 | this.params = args; |
---|
27 | mixin(this, args); |
---|
28 | }, |
---|
29 | preambleTestProp: 1, |
---|
30 | preamble: function(){ |
---|
31 | this.preambleTestProp++; |
---|
32 | }, |
---|
33 | intProp: 1, |
---|
34 | callCount: 0, // for connect testing |
---|
35 | callInc: function(){ this.callCount++; }, |
---|
36 | callCount2: 0, // for assignment testing |
---|
37 | strProp1: "original1", |
---|
38 | strProp2: "original2", |
---|
39 | arrProp: [], |
---|
40 | arrProp2: ["foo"], |
---|
41 | boolProp1: false, |
---|
42 | boolProp2: true, |
---|
43 | boolProp3: false, |
---|
44 | boolProp4: true, |
---|
45 | dateProp1: dstamp.fromISOString('2007-01-01'), |
---|
46 | dateProp2: dstamp.fromISOString('2007-01-01'), |
---|
47 | dateProp3: dstamp.fromISOString('2007-01-01'), |
---|
48 | funcProp: function(){}, |
---|
49 | funcProp2: function(){}, |
---|
50 | funcProp3: function(){}, |
---|
51 | onclick: function(){ this.prototypeOnclick=true; } |
---|
52 | // FIXME: have to test dates!! |
---|
53 | // FIXME: need to test the args property!! |
---|
54 | }); |
---|
55 | |
---|
56 | declare("tests.parser.Class2", null, { |
---|
57 | constructor: function(){ |
---|
58 | this.fromMarkup = false; |
---|
59 | }, |
---|
60 | fromMarkup: false, |
---|
61 | markupFactory: function(args, node, classCtor){ |
---|
62 | var i = new tests.parser.Class2(); |
---|
63 | i.fromMarkup = true; |
---|
64 | return i; |
---|
65 | } |
---|
66 | }); |
---|
67 | |
---|
68 | declare("tests.parser.Class3", tests.parser.Class2, { |
---|
69 | fromMarkup: false, |
---|
70 | markupFactory: function(args, node, classCtor){ |
---|
71 | var i = new classCtor(); |
---|
72 | i.classCtor = classCtor; |
---|
73 | i.params = args; |
---|
74 | return i; |
---|
75 | } |
---|
76 | }); |
---|
77 | |
---|
78 | declare("tests.parser.InputClass", null, { |
---|
79 | constructor: function(args, node){ |
---|
80 | this.params = args; |
---|
81 | mixin(this, args); |
---|
82 | }, |
---|
83 | |
---|
84 | // these attributes are special in HTML, they don't have a value specified |
---|
85 | disabled: false, |
---|
86 | readonly: false, |
---|
87 | checked: false, |
---|
88 | |
---|
89 | // other attributes native to HTML |
---|
90 | value: "", |
---|
91 | title: "default title", |
---|
92 | tabIndex: "0", // special because mixed case |
---|
93 | |
---|
94 | // custom widget attributes that don't match a native HTML attributes |
---|
95 | custom1: 123, |
---|
96 | custom2: 456 |
---|
97 | }); |
---|
98 | |
---|
99 | // Test that dir, lang, etc. attributes can be inherited from ancestor node |
---|
100 | declare("tests.parser.BidiClass", tests.parser.Widget, { |
---|
101 | constructor: function(args, node){ mixin(this, args); }, |
---|
102 | dir: "", |
---|
103 | lang: "", |
---|
104 | textdir: "", |
---|
105 | name: "" |
---|
106 | }); |
---|
107 | |
---|
108 | // For testing that parser recurses correctly, except when the prototype has a |
---|
109 | // stopParser flag |
---|
110 | declare("tests.parser.NormalContainer", null, { |
---|
111 | constructor: function(args, node){ mixin(this, args); } |
---|
112 | }); |
---|
113 | declare("tests.parser.ShieldedContainer", null, { |
---|
114 | constructor: function(args, node){ mixin(this, args); }, |
---|
115 | |
---|
116 | // flag to tell parser not to instantiate nodes inside of me |
---|
117 | stopParser: true |
---|
118 | }); |
---|
119 | |
---|
120 | declare("tests.parser.HTML5Props", null, { |
---|
121 | constructor: function(args, node){ mixin(this, args); }, |
---|
122 | simple:false, |
---|
123 | a:2, |
---|
124 | b:null, c:null, d: null, e:null, f:null, |
---|
125 | afn: function(){ |
---|
126 | return this.a * 2; |
---|
127 | } |
---|
128 | }); |
---|
129 | |
---|
130 | // not on .prototype: |
---|
131 | tests.parser.HTML5Props._aDefaultObj = { |
---|
132 | a:1, b:2, simple:true |
---|
133 | }; |
---|
134 | |
---|
135 | declare("tests.parser.HTML5withMethod", null, { |
---|
136 | constructor: function(args, node){ mixin(this, args); }, |
---|
137 | baseValue: 10, |
---|
138 | someMethod: function(a, b){ |
---|
139 | return this.baseValue; |
---|
140 | }, |
---|
141 | diffMethod: function(a){ |
---|
142 | this._ran = true; |
---|
143 | } |
---|
144 | }); |
---|
145 | |
---|
146 | declare("tests.parser.StatefulClass", [Evented, Stateful], { |
---|
147 | strProp1: "", |
---|
148 | objProp1: {}, |
---|
149 | boolProp1: false, |
---|
150 | prototypeOnclick: false, |
---|
151 | onclick: function() {this.prototypeOnclick=true;} |
---|
152 | }); |
---|
153 | |
---|
154 | deepTestProp = { |
---|
155 | blah: { |
---|
156 | thinger: 1 |
---|
157 | } |
---|
158 | }; |
---|
159 | |
---|
160 | tests.parser.FormClass = declare(tests.parser.Widget, { |
---|
161 | encType: "" |
---|
162 | }); |
---|
163 | |
---|
164 | dojo.ready(function(){ |
---|
165 | doh.register("basic tests", [ |
---|
166 | function parse(){ |
---|
167 | // Running the parser here so that failures appear in test log |
---|
168 | parser.parse(dhtml.byId("main")); |
---|
169 | }, |
---|
170 | |
---|
171 | function testDataDojoId(t){ |
---|
172 | t.t(typeof obj == "object"); |
---|
173 | }, |
---|
174 | function testJsId(t){ |
---|
175 | t.t(typeof obj3 == "object"); |
---|
176 | }, |
---|
177 | // Attribute parsing tests |
---|
178 | function testStrProp(t){ |
---|
179 | // normal string parameter |
---|
180 | t.t(dlang.isString(obj.strProp1)); |
---|
181 | t.is("text", obj.strProp1); |
---|
182 | |
---|
183 | // make sure that you override a string value like "foo" to a blank value |
---|
184 | t.t(dlang.isString(obj.strProp2)); |
---|
185 | t.is("", obj.strProp2); |
---|
186 | }, |
---|
187 | function testIntProp(t){ |
---|
188 | t.is("number", (typeof obj.intProp)); |
---|
189 | t.is(5, obj.intProp); |
---|
190 | }, |
---|
191 | function testArrProp(t){ |
---|
192 | t.is(3, obj.arrProp.length); |
---|
193 | t.is(3, obj.arrProp[1].length); |
---|
194 | t.is(["foo", "bar", "baz"], obj.arrProp); |
---|
195 | |
---|
196 | // make sure empty arrays are possible |
---|
197 | t.is([], obj.arrProp2); |
---|
198 | }, |
---|
199 | function testBoolProp(t){ |
---|
200 | // make sure that both true and false get read correctly, |
---|
201 | // and that unspecified attributes' values don't change |
---|
202 | |
---|
203 | // boolProp1 specified at true |
---|
204 | t.is("boolean", (typeof obj.boolProp1)); |
---|
205 | t.t(obj.boolProp1); |
---|
206 | |
---|
207 | // boolProp2 specified as false |
---|
208 | t.is("boolean", (typeof obj.boolProp2)); |
---|
209 | t.f(obj.boolProp2); |
---|
210 | |
---|
211 | // boolProp3 not specified (prototype says false) |
---|
212 | t.is("boolean", (typeof obj.boolProp3)); |
---|
213 | t.f(obj.boolProp3); |
---|
214 | |
---|
215 | // boolProp4 not specified (prototype says true) |
---|
216 | t.is("boolean", (typeof obj.boolProp4)); |
---|
217 | t.t(obj.boolProp4); |
---|
218 | }, |
---|
219 | function testDateProp(t){ |
---|
220 | // dateProp1 specified as 2006-1-1 |
---|
221 | t.is("2006-01-01", dstamp.toISOString(obj.dateProp1, {selector: 'date'})); |
---|
222 | |
---|
223 | // dateProp2="", should map to NaN (a blank value on DateTextBox) |
---|
224 | t.t(isNaN(obj.dateProp2)); |
---|
225 | |
---|
226 | // dateProp3="now", should map to current date |
---|
227 | t.is(dstamp.toISOString(new Date(), {selector: 'date'}), |
---|
228 | dstamp.toISOString(obj.dateProp3, {selector: 'date'})); |
---|
229 | }, |
---|
230 | function testUnwantedParams(t){ |
---|
231 | // Make sure that parser doesn't pass any unwanted parameters to |
---|
232 | // widget constructor, especially "toString" or "constructor". |
---|
233 | // Make exception for dir/lang which parser gleans from document itself. |
---|
234 | for(var param in obj.params){ |
---|
235 | doh.t(darray.indexOf( |
---|
236 | ["strProp1", "strProp2", |
---|
237 | "intProp", |
---|
238 | "arrProp", "arrProp2", |
---|
239 | "boolProp1", "boolProp2", |
---|
240 | "dateProp1", "dateProp2", "dateProp3", |
---|
241 | "funcProp2", "funcProp3", |
---|
242 | "preamble", |
---|
243 | "callInc1", "callInc2", "dir", "lang", "textDir"], |
---|
244 | param) >= 0, param); |
---|
245 | } |
---|
246 | }, |
---|
247 | function testDisabledFlag(t){ |
---|
248 | t.is("boolean", typeof disabledObj.disabled, "typeof disabled"); |
---|
249 | t.t(disabledObj.disabled, "disabled"); |
---|
250 | t.f(disabledObj.checked, "checked"); |
---|
251 | }, |
---|
252 | function testCheckedFlag(t){ |
---|
253 | t.is("boolean", typeof checkedObj.checked, "typeof checked"); |
---|
254 | t.f(checkedObj.disabled, "disabled"); |
---|
255 | t.t(checkedObj.checked, "checked"); |
---|
256 | }, |
---|
257 | function testFunctionProp(t){ |
---|
258 | // make sure that unspecified functions (even with common names) |
---|
259 | // don't get overridden (bug #3074) |
---|
260 | obj.onclick(); |
---|
261 | t.t(obj.prototypeOnclick); |
---|
262 | |
---|
263 | // funcProp2="foo" |
---|
264 | obj.funcProp2(); |
---|
265 | t.t(obj.fooCalled); |
---|
266 | |
---|
267 | // funcProp3="this.func3Called=true;" |
---|
268 | obj.funcProp3(); |
---|
269 | t.t(obj.func3Called); |
---|
270 | }, |
---|
271 | |
---|
272 | // test script tags inside innerHTML of source node |
---|
273 | "t.is(4, obj.preambleTestProp);", |
---|
274 | "t.is(deepTestProp, obj.deepProp);", |
---|
275 | function testConnect(t){ |
---|
276 | obj.callInc(); |
---|
277 | t.is(2, obj.callCount); |
---|
278 | }, |
---|
279 | function testFunctionAssignment(t){ |
---|
280 | obj.callInc2(); |
---|
281 | t.is(1, obj.callCount2); |
---|
282 | }, |
---|
283 | function testSubNodeParse(t){ |
---|
284 | t.f(exists("obj2")); |
---|
285 | var toParse = dhtml.byId("toParse"); |
---|
286 | toParse.setAttribute("dojoType", toParse.getAttribute("type")); |
---|
287 | parser.parse(toParse.parentNode); |
---|
288 | t.t(exists("obj2")); |
---|
289 | t.is("tests.parser.Class1", obj2.declaredClass); |
---|
290 | }, |
---|
291 | function testMarkupFactory(t){ |
---|
292 | t.t(exists("obj3")); |
---|
293 | t.t(obj3.fromMarkup); |
---|
294 | }, |
---|
295 | function testMarkupFactoryClass(t){ |
---|
296 | t.t(exists("obj4")); |
---|
297 | t.is(obj4.classCtor, tests.parser.Class3); |
---|
298 | t.t(obj4 instanceof tests.parser.Class3); |
---|
299 | t.t(obj4 instanceof tests.parser.Class2); |
---|
300 | }, |
---|
301 | function testnostart(t){ |
---|
302 | |
---|
303 | var started = false; |
---|
304 | declare("SampleThinger", null, { |
---|
305 | startup: function(){ |
---|
306 | started = true; |
---|
307 | } |
---|
308 | }); |
---|
309 | |
---|
310 | dhtml.create("div", { dojoType:"SampleThinger" }, "parsertest"); |
---|
311 | parser.parse("parsertest", { noStart:true }); |
---|
312 | |
---|
313 | t.f(started); |
---|
314 | |
---|
315 | dhtml.empty("parsertest"); |
---|
316 | |
---|
317 | started = false; |
---|
318 | |
---|
319 | dhtml.create("div", { dojoType:"SampleThinger" }, "parsertest"); |
---|
320 | parser.parse({ noStart:true, rootNode:"parsertest" }); |
---|
321 | |
---|
322 | t.f(started); |
---|
323 | }, |
---|
324 | |
---|
325 | // test the various iterations of parser test |
---|
326 | function rootTest(t){ |
---|
327 | |
---|
328 | var tmp = aspect.after(dojo, "query", function(sel, root){ |
---|
329 | t.is("parsertest2", root); |
---|
330 | }); |
---|
331 | |
---|
332 | parser.parse("parsertest2"); |
---|
333 | parser.parse({ rootNode: "parsertest2" }); |
---|
334 | parser.parse("parsertest2", { noStart:true }); |
---|
335 | |
---|
336 | tmp.remove(); |
---|
337 | }, |
---|
338 | |
---|
339 | // Test that when BorderContainer etc. extends _Widget, |
---|
340 | // parser is aware of the new parameters added (to _Widget |
---|
341 | // and all of it's subclasses) |
---|
342 | function cacheRefresh(t){ |
---|
343 | // Add new node to be parsed, referencing a widget that the parser has already |
---|
344 | // dealt with (and thus cached) |
---|
345 | var wrapper = dhtml.place("<div><div dojoType='tests.parser.Class3' newParam=12345>hi</div></div>", dhtml.body(), "last"); |
---|
346 | |
---|
347 | // Modify Class3's superclass widget to have new parameter (thus Class3 inherits it) |
---|
348 | extend(tests.parser.Class2, { |
---|
349 | newParam: 0 |
---|
350 | }); |
---|
351 | |
---|
352 | // Run the parser and see if it reads in newParam |
---|
353 | var widgets = parser.parse({rootNode: wrapper}); |
---|
354 | doh.is(1, widgets.length, "parsed newly inserted parserTest widget"); |
---|
355 | doh.is(12345, widgets[0].params.newParam, "new parameter parsed"); |
---|
356 | }, |
---|
357 | |
---|
358 | // Test that parser recurses correctly, except when there's a stopParser flag not to |
---|
359 | function recurse(){ |
---|
360 | doh.t(container1, "normal container created"); |
---|
361 | doh.t(container1.incr, "script tag works too"); |
---|
362 | doh.t(window.contained1, "child widget also created"); |
---|
363 | doh.t(window.contained2, "child widget 2 also created"); |
---|
364 | |
---|
365 | doh.t(container2, "shielded container created"); |
---|
366 | doh.t(container2.incr, "script tag works too"); |
---|
367 | doh.f(window.contained3, "child widget not created"); |
---|
368 | doh.f(window.contained4, "child widget 2 not created"); |
---|
369 | }, |
---|
370 | |
---|
371 | function simpleHTML5(){ |
---|
372 | doh.t(typeof html5simple == "object", "data-dojo-id export"); |
---|
373 | doh.t(typeof html5simple2 == "object", "data-dojo-id export"); |
---|
374 | |
---|
375 | doh.t(html5simple.simple, "default respecified in props=''"); |
---|
376 | doh.f(html5simple2.simple, "default overridden by props=''"); |
---|
377 | |
---|
378 | // test data-dojo-props="simple:false, a:1, b:'two', c:[1,2,3], d:function(){ return this; }, e:{ f:'g' }" |
---|
379 | var it = html5simple2; |
---|
380 | doh.is(1, it.a, "number in param"); |
---|
381 | doh.is("two", it.b, "string in param"); |
---|
382 | doh.t(dlang.isArray(it.c), "array in param"); |
---|
383 | doh.is(3, it.c.length, "array sanity"); |
---|
384 | doh.is("g", it.e.f, "nested object with string"); |
---|
385 | |
---|
386 | // test the function |
---|
387 | doh.is(it, it.d(), "simple 'return this' function"); |
---|
388 | |
---|
389 | }, |
---|
390 | |
---|
391 | function html5inherited(){ |
---|
392 | doh.t(typeof html5simple3 == "object"); |
---|
393 | var val = html5simple3.afn(); |
---|
394 | doh.is(html5simple3.a * 2, val, "afn() overrides default but calls inherited") |
---|
395 | }, |
---|
396 | |
---|
397 | function html5withMethod(){ |
---|
398 | // testing data-dojo-event and data-dojo-args support for dojo/method and dojo/connect |
---|
399 | doh.t(typeof htmldojomethod == "object"); |
---|
400 | doh.t(htmldojomethod._methodRan, "plain dojo/method ran"); |
---|
401 | |
---|
402 | var x = htmldojomethod.someMethod(2, 2); |
---|
403 | doh.is(14, x, "overridden dojo/method"); |
---|
404 | |
---|
405 | htmldojomethod.diffMethod(2); |
---|
406 | doh.t(htmldojomethod._ran, "ensures original was called first"); |
---|
407 | doh.is(2, htmldojomethod._fromvalue, "ensures connected was executed in scope"); |
---|
408 | }, |
---|
409 | |
---|
410 | function testOnWatch(){ |
---|
411 | // testing script-type dojo/watch and dojo/on |
---|
412 | doh.t(typeof objOnWatch == "object"); |
---|
413 | objOnWatch.set("strProp1","newValue1"); |
---|
414 | doh.is("newValue1", objOnWatch.arrProp.newValue, "ensures watch executed"); |
---|
415 | |
---|
416 | objOnWatch.onclick(); |
---|
417 | doh.t(objOnWatch.prototypeOnclick, "ensures original was called"); |
---|
418 | doh.t(objOnWatch.boolProp1, "ensure on executed in scope"); |
---|
419 | } |
---|
420 | ]); |
---|
421 | |
---|
422 | doh.register("BIDI", [ |
---|
423 | // Test that dir=rtl or dir=ltr setting trickles down from root node |
---|
424 | function dir(){ |
---|
425 | parser.parse("dirSection1"); |
---|
426 | parser.parse("dirSection2"); |
---|
427 | doh.is("rtl", setRtl.dir, "direct setting of dir=rtl works"); |
---|
428 | doh.is("rtl", inheritRtl.dir, "inherited rtl works"); |
---|
429 | doh.is("ltr", inheritLtr.dir, "inherited ltr works (closest ancestor wins)"); |
---|
430 | doh.is("rtl", inheritRtl2.dir, "inherited rtl works, from grandparent"); |
---|
431 | doh.is("ltr", setLtr.dir, "direct setting of dir=ltr overrides inherited RTL"); |
---|
432 | }, |
---|
433 | function lang(){ |
---|
434 | parser.parse("langSection"); |
---|
435 | doh.f(lang in noLang.params, "no lang"); |
---|
436 | doh.is("it_it", inheritedLang.lang, "inherited lang works"); |
---|
437 | doh.is("en_us", specifiedLang.lang,"direct setting of lang overrides inherited"); |
---|
438 | }, |
---|
439 | function textdir(){ |
---|
440 | parser.parse("textDirSection"); |
---|
441 | doh.f("textDir" in noTextdir.params, "no textdir"); |
---|
442 | doh.is("rtl", inheritedTextdir.textDir, "inherited textdir works"); |
---|
443 | doh.is("ltr", specifiedTextdir.textDir,"direct setting of textdir overrides inherited"); |
---|
444 | }, |
---|
445 | { |
---|
446 | // Test that calling parser.parse(nodeX) will inherit dir/lang/etc. settings |
---|
447 | // even from <html> |
---|
448 | name: "inheritance from HTML", |
---|
449 | setUp: function(){ |
---|
450 | dhtml.attr(dhtml.doc.documentElement, {dir: "rtl", lang: "ja-jp", "data-dojo-textdir": "auto"}); |
---|
451 | parser.parse("bidiInheritanceFromHtml"); |
---|
452 | }, |
---|
453 | runTest: function(){ |
---|
454 | doh.is("rtl", inheritedFromHtml.params.dir, "dir"); |
---|
455 | doh.is("ja-jp", inheritedFromHtml.params.lang, "lang"); |
---|
456 | doh.is("auto", inheritedFromHtml.params.textDir, "textDir"); |
---|
457 | }, |
---|
458 | tearDown: function(){ |
---|
459 | darray.forEach(["dir", "lang", "data-dojo-textdir"], function(attr){ |
---|
460 | dhtml.doc.documentElement.removeAttribute(attr); |
---|
461 | }); |
---|
462 | } |
---|
463 | } |
---|
464 | ]); |
---|
465 | |
---|
466 | doh.register("IE attribute detection", [ |
---|
467 | function input1(){ |
---|
468 | var widgets = parser.instantiate([dhtml.byId("ieInput1")]); |
---|
469 | var params = widgets[0].params; |
---|
470 | |
---|
471 | doh.is("checkbox", params.type, "type"); |
---|
472 | doh.t(params.disabled, "disabled"); |
---|
473 | doh.t(params.checked, "checked"); |
---|
474 | doh.t(params.readonly, "readonly"); |
---|
475 | doh.is("bar", params.foo, "foo"); |
---|
476 | doh.is("zaz", params.bar, "bar"); |
---|
477 | doh.is("escaped\"dq", params.bob, "bob"); |
---|
478 | doh.is("escaped\'sq", params.frank, "frank"); |
---|
479 | }, |
---|
480 | function input2(){ |
---|
481 | var widgets = parser.instantiate([dhtml.byId("ieInput2")]); |
---|
482 | var params = widgets[0].params; |
---|
483 | |
---|
484 | doh.f("type" in params, "type"); |
---|
485 | doh.f("name" in params, "name"); |
---|
486 | doh.f("value" in params, "value"); |
---|
487 | doh.f("data-dojo-type" in params, "data-dojo-type"); |
---|
488 | doh.f("data-dojo-props" in params, "data-dojo-props"); |
---|
489 | doh.is("hi", params.foo, "foo"); |
---|
490 | }, |
---|
491 | function input3(){ |
---|
492 | var widgets = parser.instantiate([dhtml.byId("ieInput3")]); |
---|
493 | var params = widgets[0].params; |
---|
494 | |
---|
495 | doh.is("password", params.type, "type"); |
---|
496 | doh.is("test", params.name, "name"); |
---|
497 | doh.is("123", params.value, "value"); |
---|
498 | doh.is("myClass", params["class"], "class"); |
---|
499 | doh.is("display:block", params["style"].replace(/[ ;]/g, "").toLowerCase(), "style"); |
---|
500 | doh.is("3", params.tabIndex, "tabIndex"); |
---|
501 | }, |
---|
502 | function textarea(){ |
---|
503 | var widgets = parser.instantiate([dhtml.byId("ieTextarea")]); |
---|
504 | var params = widgets[0].params; |
---|
505 | |
---|
506 | doh.is("attrVal", params.value, "value"); |
---|
507 | }, |
---|
508 | function button1(){ |
---|
509 | var widgets = parser.instantiate([dhtml.byId("ieButton1")]); |
---|
510 | var params = widgets[0].params; |
---|
511 | doh.t(params.checked, "checked"); |
---|
512 | doh.is("button1val", params.value, "value"); |
---|
513 | }, |
---|
514 | function button2(){ |
---|
515 | var widgets = parser.instantiate([dhtml.byId("ieButton2")]); |
---|
516 | var params = widgets[0].params; |
---|
517 | doh.f("checked" in params, "checked"); |
---|
518 | doh.f("value" in params, "value"); |
---|
519 | }, |
---|
520 | function button3(){ |
---|
521 | var widgets = parser.instantiate([dhtml.byId("ieButton3")]); |
---|
522 | var params = widgets[0].params; |
---|
523 | doh.t(params.checked, "checked"); |
---|
524 | }, |
---|
525 | function button4(){ |
---|
526 | var widgets = parser.instantiate([dhtml.byId("ieButton4")]); |
---|
527 | var params = widgets[0].params; |
---|
528 | doh.f("checked" in params); |
---|
529 | }, |
---|
530 | function form1(){ |
---|
531 | var widgets = parser.instantiate([dhtml.byId("ieForm1")]); |
---|
532 | var params = widgets[0].params; |
---|
533 | |
---|
534 | doh.is("foo", params.encType, "encType is specified"); |
---|
535 | }, |
---|
536 | function form2(){ |
---|
537 | var widgets = parser.instantiate([dhtml.byId("ieForm2")]); |
---|
538 | var params = widgets[0].params; |
---|
539 | |
---|
540 | doh.f("encType" in params, "encType not specified") |
---|
541 | }, |
---|
542 | function li(){ |
---|
543 | var widgets = parser.instantiate([dhtml.byId("li")]); |
---|
544 | var params = widgets[0].params; |
---|
545 | doh.is("home", params.value); |
---|
546 | |
---|
547 | } |
---|
548 | ]); |
---|
549 | |
---|
550 | doh.register("mixed attribute specification", function mixed(){ |
---|
551 | parser.parse(dhtml.byId("mixedContainer")); |
---|
552 | doh.is("object", typeof mixedObj, "widget created"); |
---|
553 | doh.is("mixedValue", mixedObj.value, "native attribute"); |
---|
554 | doh.is(999, mixedObj.custom1, "data-dojo-props attribute"); |
---|
555 | doh.is("custom title", mixedObj.title, "data-dojo-props overrides native"); |
---|
556 | }); |
---|
557 | |
---|
558 | doh.register("functions", function onclick(){ |
---|
559 | // Create objects referenced from markup inside of "functions" div |
---|
560 | declare("tests.parser.Button", null, { |
---|
561 | onClick: function(){ |
---|
562 | console.log("prototype click"); |
---|
563 | }, |
---|
564 | constructor: function(args, node){ |
---|
565 | mixin(this, args); |
---|
566 | this.domNode = node; |
---|
567 | aspect.after(this.domNode, "onclick", dlang.hitch(this, "onClick")); |
---|
568 | } |
---|
569 | }); |
---|
570 | buttonClicked = function(){ |
---|
571 | console.log("markup click"); |
---|
572 | }; // markup says onClick="buttonClicked" |
---|
573 | |
---|
574 | // Parse markup inside "functions" div |
---|
575 | parser.parse("functions"); |
---|
576 | |
---|
577 | // Should have created an instance called "button" where button.onClick == buttonClicked |
---|
578 | doh.is("object", typeof button, "widget created"); |
---|
579 | doh.is("function", typeof button.onClick, "created as function"); |
---|
580 | doh.t(buttonClicked == button.onClick, "points to specified function"); |
---|
581 | }); |
---|
582 | doh.run(); |
---|
583 | }) |
---|
584 | }); |
---|
585 | </script> |
---|
586 | </head> |
---|
587 | <body> |
---|
588 | <h1>Parser Unit Test</h1> |
---|
589 | |
---|
590 | <div id=main> |
---|
591 | <script> |
---|
592 | function foo(){ this.fooCalled=true; } |
---|
593 | </script> |
---|
594 | <div dojoType="tests.parser.Class1" data-dojo-id="obj" |
---|
595 | strProp1="text" strProp2="" |
---|
596 | intProp="5" |
---|
597 | arrProp="foo, bar, baz" |
---|
598 | arrProp2="" |
---|
599 | boolProp1="true" boolProp2="false" |
---|
600 | dateProp1="2006-01-01" dateProp2="" dateProp3="now" |
---|
601 | funcProp2="foo" funcProp3="this.func3Called=true;" |
---|
602 | > |
---|
603 | <script type="dojo/method" event="preamble"> |
---|
604 | this.preambleTestProp = 3; |
---|
605 | </script> |
---|
606 | <script type="dojo/method"> |
---|
607 | // this should be run immediately |
---|
608 | this.deepProp = deepTestProp; |
---|
609 | </script> |
---|
610 | <script type="dojo/connect" event="callInc"> |
---|
611 | this.callCount++; |
---|
612 | </script> |
---|
613 | <script type="dojo/method" event="callInc2"> |
---|
614 | this.callCount2++; |
---|
615 | </script> |
---|
616 | </div> |
---|
617 | <div> |
---|
618 | <div type="tests.parser.Class1" jsId="obj2" id="toParse"> |
---|
619 | </div> |
---|
620 | </div> |
---|
621 | <div dojoType="tests.parser.Class2" jsId="obj3"> |
---|
622 | </div> |
---|
623 | <div dojoType="tests.parser.Class3" jsId="obj4"> |
---|
624 | </div> |
---|
625 | <input dojoType="tests.parser.InputClass" jsId="checkedObj" checked type="checkbox"> |
---|
626 | <button dojoType="tests.parser.InputClass" jsId="disabledObj" disabled>hi</button> |
---|
627 | |
---|
628 | <div id="parsertest"></div> |
---|
629 | <div id="parsertest2"></div> |
---|
630 | |
---|
631 | <!-- section for testing parser recursion --> |
---|
632 | <div> |
---|
633 | <div dojoType="tests.parser.NormalContainer" jsId="container1"> |
---|
634 | <!-- this script tag should get passed as param to NormalContainer constructor --> |
---|
635 | <script type="dojo/method" event="incr" args="x"> |
---|
636 | return x+1; |
---|
637 | </script> |
---|
638 | |
---|
639 | <!-- and these contained widgets should get instantiated --> |
---|
640 | <div dojoType="tests.parser.Class1" jsId="contained1"></div> |
---|
641 | <div> |
---|
642 | <div dojoType="tests.parser.Class1" jsId="contained2"></div> |
---|
643 | </div> |
---|
644 | </div> |
---|
645 | </div> |
---|
646 | |
---|
647 | <div> |
---|
648 | <div dojoType="tests.parser.ShieldedContainer" jsId="container2"> |
---|
649 | <!-- this script tag should get passed as param to NormalContainer constructor --> |
---|
650 | <script type="dojo/method" event="incr" args="x"> |
---|
651 | return x+1; |
---|
652 | </script> |
---|
653 | |
---|
654 | <!-- but these contained widgets should *not* get instantiated --> |
---|
655 | <div dojoType="tests.parser.Class1" jsId="contained3"></div> |
---|
656 | <div> |
---|
657 | <div dojoType="tests.parser.Class1" jsId="contained4"></div> |
---|
658 | </div> |
---|
659 | </div> |
---|
660 | </div> |
---|
661 | |
---|
662 | <!-- tests for new data-dojo-type / data-dojo-props syntax --> |
---|
663 | <div> |
---|
664 | <div data-dojo-id="html5simple" data-dojo-type="tests.parser.HTML5Props" data-dojo-props="simple:true"></div> |
---|
665 | <div data-dojo-id="html5simple2" data-dojo-type="tests.parser.HTML5Props" |
---|
666 | data-dojo-props="simple:false, a:1, b:'two', c:[1,2,3], d:function(){ return this; }, e:{ f:'g' }" |
---|
667 | ></div> |
---|
668 | <!-- note needing to use a named inherited lookup because we're just mixing in --> |
---|
669 | <div data-dojo-id="html5simple3" data-dojo-type="tests.parser.HTML5Props" |
---|
670 | data-dojo-props="afn: function(){ return this.inherited('afn', arguments); }" |
---|
671 | ></div> |
---|
672 | |
---|
673 | <!-- not used for tests, but thinking out loud: what about a named-resource prop, via getObject --> |
---|
674 | <div data-dojo-id="html5fromobjectns" data-dojo-type="tests.parser.HTML5Props" |
---|
675 | data-dojo-obj="tests.parser.HTML5Props._aDefaultObj" |
---|
676 | ></div> |
---|
677 | <div data-dojo-id="html5fromobjectns2" data-dojo-type="tests.parser.HTML5Props" |
---|
678 | data-dojo-obj="tests.parser.HTML5Props._aDefaultObj" data-dojo-props="simple:false" |
---|
679 | ></div> |
---|
680 | |
---|
681 | </div> |
---|
682 | |
---|
683 | <div> |
---|
684 | <div data-dojo-id="htmldojomethod" data-dojo-type="tests.parser.HTML5withMethod"> |
---|
685 | <p>Some random markup</p> |
---|
686 | <script type="dojo/method" data-dojo-event="someMethod" data-dojo-args="a, b"> |
---|
687 | return this.baseValue + a + b; |
---|
688 | </script> |
---|
689 | <script type="dojo/connect" data-dojo-event="diffMethod" data-dojo-args="a"> |
---|
690 | console.log("diffMethod connect, this is ", this); |
---|
691 | this._fromvalue = a; |
---|
692 | </script> |
---|
693 | <script type="dojo/method"> |
---|
694 | this._methodRan = true; |
---|
695 | </script> |
---|
696 | </div> |
---|
697 | </div> |
---|
698 | |
---|
699 | <!-- section for testing dojo/on and dojo/watch scripts --> |
---|
700 | <div> |
---|
701 | <div data-dojo-id="objOnWatch" data-dojo-type="tests.parser.StatefulClass"> |
---|
702 | <script type="dojo/watch" data-dojo-prop="strProp1" data-dojo-args="prop,oldValue,newValue"> |
---|
703 | this.set("arrProp",{prop: prop, oldValue: oldValue, newValue: newValue}); |
---|
704 | </script> |
---|
705 | <script type="dojo/on" data-dojo-event="click" data-dojo-args="e"> |
---|
706 | console.log("diffMethod on, this is ",this); |
---|
707 | this.set("boolProp1",true); |
---|
708 | </script> |
---|
709 | </div> |
---|
710 | </div> |
---|
711 | </div> <!-- end of <div id=main> --> |
---|
712 | |
---|
713 | <!-- section for testing that dir, lang attribute trickles down from ancestor --> |
---|
714 | <div id="dirSection1"> |
---|
715 | <div dojoType="tests.parser.BidiClass" jsId="setRtl" dir="rtl" name="RTL setting"></div> |
---|
716 | <div dojoType="tests.parser.BidiClass" jsId="noDir" name="dir not inherited or set"></div> |
---|
717 | </div> |
---|
718 | <div id="dirSection2" dir="rtl"> |
---|
719 | <div dojoType="tests.parser.BidiClass" jsId="inheritRtl" name="inherited RTL from parent"></div> |
---|
720 | <div dir="ltr"> |
---|
721 | <div dojoType="tests.parser.BidiClass" jsId="inheritLtr" name="inherited LTR from parent"></div> |
---|
722 | </div> |
---|
723 | <div> |
---|
724 | <div dojoType="tests.parser.BidiClass" jsId="inheritRtl2" name="inherited RTL from grandparent"></div> |
---|
725 | </div> |
---|
726 | <div dojoType="tests.parser.BidiClass" jsId="setLtr" dir="ltr" name="LTR setting overrides inherited RTL"></div> |
---|
727 | </div> |
---|
728 | <div id="langSection"> |
---|
729 | <div dojoType="tests.parser.BidiClass" jsId="noLang" name="shouldn't get lang"></div> |
---|
730 | <div lang="it_it"> |
---|
731 | <div dojoType="tests.parser.BidiClass" jsId="inheritedLang" name="inherited lang from parent"></div> |
---|
732 | <div dojoType="tests.parser.BidiClass" jsId="specifiedLang" lang="en_us" name="specified lang overrides parent"></div> |
---|
733 | </div> |
---|
734 | </div> |
---|
735 | <div id="textDirSection"> |
---|
736 | <div dojoType="tests.parser.BidiClass" jsId="noTextdir" name="shouldn't get textdir"></div> |
---|
737 | <div data-dojo-textdir="rtl"> |
---|
738 | <div dojoType="tests.parser.BidiClass" jsId="inheritedTextdir" name="inherited textdir from parent"></div> |
---|
739 | <div dojoType="tests.parser.BidiClass" jsId="specifiedTextdir" data-dojo-textdir="ltr" name="specified textdir overrides parent"></div> |
---|
740 | </div> |
---|
741 | </div> |
---|
742 | <div id="bidiInheritanceFromHtml"> |
---|
743 | <div dojoType="tests.parser.BidiClass" jsId="inheritedFromHtml" name="should get dir/lang/textDir from HTML tag"></div> |
---|
744 | </div> |
---|
745 | |
---|
746 | <!-- tests that we can parse parameters correctly on IE6/7, not getting tripped up by escaped quotes etc. --> |
---|
747 | <div id=ie> |
---|
748 | <input id="ieInput1" data-dojo-type="tests.parser.InputClass" |
---|
749 | type=checkbox disabled foo = 'bar' readonly bar=zaz bob='escaped"dq' frank="escaped'sq" checked /> |
---|
750 | <input id="ieInput2" data-dojo-type="tests.parser.InputClass" |
---|
751 | fakeout1="type=submit" fakeout2="name='test'" fakeout3="value='123'" data-dojo-props="foo: 'hi'"/> |
---|
752 | <input id="ieInput3" data-dojo-type="tests.parser.InputClass" |
---|
753 | type=password name="test" value="123" class="myClass" style="display:block" tabindex="3"/> |
---|
754 | <textarea id="ieTextarea" data-dojo-type="tests.parser.InputClass" value="attrVal">contentVal</textarea> |
---|
755 | <button id="ieButton1" data-dojo-type="tests.parser.InputClass" checked value="button1val"> |
---|
756 | checked ToggleButton as button |
---|
757 | </button> |
---|
758 | <button id="ieButton2" data-dojo-type="tests.parser.InputClass"> |
---|
759 | unchecked ToggleButton as button |
---|
760 | </button> |
---|
761 | <div id="ieButton3" data-dojo-type="tests.parser.InputClass" checked> |
---|
762 | checked ToggleButton as div |
---|
763 | </div> |
---|
764 | <div id="ieButton4" data-dojo-type="tests.parser.InputClass"> |
---|
765 | unchecked ToggleButton as div |
---|
766 | </div> |
---|
767 | <form id="ieForm1" data-dojo-type="tests.parser.FormClass" encType="foo"></form> |
---|
768 | <form id="ieForm2" data-dojo-type="tests.parser.FormClass"></form> |
---|
769 | <ul dojoType="tests.parser.Widget" class="nav"> |
---|
770 | <li id="li" dojoType="tests.parser.Widget" value="home">Home</li> |
---|
771 | <li dojoType="tests.parser.Widget" value="contact">Contact</li> |
---|
772 | <li dojoType="tests.parser.Widget" value="group">Group</li> |
---|
773 | <li dojoType="tests.parser.Widget" value="campaign">Campaign</li> |
---|
774 | </ul> |
---|
775 | </div> |
---|
776 | |
---|
777 | <!-- tests for when parameters are specified both natively and in data-dojo-props. --> |
---|
778 | <div id="mixedContainer"> |
---|
779 | <input data-dojo-type="tests.parser.InputClass" data-dojo-id="mixedObj" |
---|
780 | value="mixedValue" title="native title" data-dojo-props="custom1: 999, title: 'custom title'"> |
---|
781 | </div> |
---|
782 | |
---|
783 | <!-- tests for function names native to HTML, specifically an issue on IE<8 --> |
---|
784 | <div id="functions"> |
---|
785 | <button dojoType="tests.parser.Button" onClick="buttonClicked" jsId="button">Click me</button> |
---|
786 | </div> |
---|
787 | |
---|
788 | </body> |
---|
789 | </html> |
---|