source: Dev/trunk/src/client/dojo/tests/html/test_set.html

Last change on this file was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 15.1 KB
Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2                "http://www.w3.org/TR/html4/strict.dtd">
3<html>
4<head>
5        <title>dojo.html.set test</title>
6
7        <script src="../../dojo.js" data-dojo-config="isDebug:true, parseOnLoad:false"></script>
8        <script>
9                require([
10                        "doh",
11                        "dojo/_base/array", "dojo/aspect",
12                        "dojo/_base/declare", "dojo/dom", "dojo/dom-construct", "dojo/html", "dojo/_base/lang", "dojo/parser",
13                        "dojo/query", "dojo/NodeList-html","dojo/domReady!"
14                ], function(doh, array, aspect, declare, dom, domConstruct, html, lang, parser, query){
15
16                        /* test goals
17                         * injecting content as string, node, nodelist.
18                         * injecting exotic nodes/markup e.g. table rows, lists
19                         * injecting whole html documents (extractContent option)
20                         * parsing resulting content
21                         
22                         * cleanup when setting content
23                        */
24                        declare("SimpleThing", null, {
25                                constructor: function(params, node) {
26                                        node.setAttribute("test", "ok");
27                                }
28                        });
29                       
30                        declare("ParserInstantiateTester", null, {
31                                constructor: function(params, node) {
32                                        node.setAttribute("test", "ok");
33                                }
34                        });
35                        declare("DeclarativeContentSetter", html._ContentSetter, {
36                                postscript: function() {
37                                        this.set();
38                                }
39                        });
40       
41                        function ieTrimSpaceBetweenTags(str){
42                                return str.replace(/(<[a-z]*[^>]*>)\s*/ig, "$1");
43                        }
44
45
46                        targetNode = null;
47                       
48                        doh.register("basicChecks", [
49                                {
50                                        name: 'set',
51                                        runTest: function(t){
52                                                console.log("basicChecks: " + this.name);
53                                                targetNode = dom.byId("pane1");
54                                                var msg = "Simple No-params Test";
55                                                console.log("targetNode has content: ", targetNode.innerHTML);
56                                                var result = "";
57                                                html.set(
58                                                        targetNode,
59                                                        msg
60                                                );
61                                                console.log("after set, targetNode has content: ", targetNode.innerHTML);
62                                                doh.is(msg, targetNode.innerHTML);
63                                        }
64                                },
65                                {
66                                        name: 'setContentWithOnEnd',
67                                        runTest: function(t){
68                                                console.log("basicChecks: " + this.name);
69                                                targetNode = dom.byId("pane1");
70                                                var msg = "setContentWithOnEnd Test";
71                                                var result = false;
72                                                html.set(
73                                                        targetNode,
74                                                        msg,
75                                                        {
76                                                                onEnd: function() {
77                                                                        lang.getObject(this.declaredClass).prototype.onEnd.call(this);
78                                                                        result = true;
79                                                                }
80                                                        }
81                                                );
82                                                doh.is(msg, targetNode.innerHTML);
83                                                doh.t(result);
84                                        }
85                                },
86                                {
87                                        name: 'setContent_with_parsing',
88                                        runTest: function(t){
89                                                console.log("basicChecks: " + this.name);
90                                                var cont = '<div data-dojo-type="SimpleThing" data-dojo-id="ifrs" data="{}"></div>';
91                                                html.set(
92                                                        dom.byId("pane1"),
93                                                        cont,
94                                                        {
95                                                                postscript: function() {
96                                                                        this.set();
97
98                                                                        doh.t(typeof ifrs != "undefined" && ifrs.declaredClass=="SimpleThing");
99                                                                        doh.t(this.parseResults.length > 0);
100                                                                },
101                                                                parseContent: true
102                                                        }
103                                                );
104                                        }
105                                },
106                                {
107                                        name: 'emptyElement',
108                                        runTest: function(t){
109                                                // Test of deprecated _emptyNode() method
110                                                console.log("basicChecks: " + this.name);
111                                                var msg = "setContentWithOnEnd Test";
112                                                var node = dom.byId("pane1");
113                                                node.innerHTML = '<div><span>just</span>some test<br/></div>text';
114                                                var cNodes = node.childNodes.length;
115
116                                                html._emptyNode(dom.byId("pane1"));
117                                                doh.t(node.childNodes.length == 0 && node.innerHTML == "");
118                                        }
119                                },
120                                {
121                                        name: 'changeContentTRHead',
122                                        runTest: function(t){
123                                                console.log("basicChecks: " + this.name);
124                                                targetNode = query('table#tableTest > thead > tr')[0];
125
126                                                var markup = "<td><div>This</div>Should<u>Work</u></td>";
127                                                html.set(
128                                                        targetNode,
129                                                                markup,
130                                                        {
131                                                                "testname": "basicChecks changeContentTRHead"
132                                                        }
133                                                );
134                                                var res = ieTrimSpaceBetweenTags(targetNode.innerHTML.toLowerCase());
135                                                doh.is(markup.toLowerCase(), res);
136                                        },
137                                        tearDown: function(){
138                                                domConstruct.empty(targetNode);
139                                        }
140                                },
141                                {
142                                        name: 'changeContentTHead',
143                                        runTest: function(t){
144                                                console.log("basicChecks: " + this.name);
145                                                targetNode = query('table#tableTest > thead')[0];
146
147                                                var markup = "<tr><td><div>This</div>Should<u>Work</u></td></tr>";
148                                                html.set(
149                                                        targetNode,
150                                                                markup,
151                                                        {
152                                                                "testname": "basicChecks changeContentTHead"
153                                                        }
154                                                );
155                                                var res = ieTrimSpaceBetweenTags(targetNode.innerHTML.toLowerCase());
156                                                doh.is(markup.toLowerCase(), res);
157                                        },
158                                        tearDown: function(){
159                                                domConstruct.empty(targetNode);
160                                        }
161                                },
162                                {
163                                        name: 'changeContentTRBody',
164                                        runTest: function(t){
165                                                console.log("basicChecks: " + this.name);
166                                                targetNode = query('table#tableTest > tbody > tr')[0];
167                                                var markup = "<td><div>This</div>Should<u>Work</u></td>";
168                                                html.set(
169                                                        targetNode,
170                                                                markup,
171                                                        {
172                                                                "testname": "basicChecks changeContentTRBody"
173                                                        });
174                                                var res = ieTrimSpaceBetweenTags(targetNode.innerHTML.toLowerCase());
175                                                doh.is(markup.toLowerCase(), res);
176                                        },
177                                        tearDown: function(){
178                                                domConstruct.empty(targetNode);
179                                        }
180                                },
181                                {
182                                        name: 'changeContentTBody',
183                                        runTest: function(t){
184                                                console.log("basicChecks: " + this.name);
185                                                targetNode = query('table#tableTest > tbody')[0];
186                                                var markup = "<tr><td><div>This</div>Should<u>Work</u></td></tr>";
187                                                html.set(
188                                                        targetNode, markup,
189                                                        {
190                                                                "testname": "basicChecks changeContentTBody"
191                                                        });
192                                                var res = ieTrimSpaceBetweenTags(targetNode.innerHTML.toLowerCase());
193                                                doh.is(markup.toLowerCase(), res);
194                                        },
195                                        tearDown: function(){
196                                                domConstruct.empty(targetNode);
197                                        }
198                                },
199                                {
200                                        name: 'changeContentTable',
201                                        runTest: function(t){
202                                                console.log("basicChecks: " + this.name);
203                                                targetNode = query('table#tableTest')[0];
204                                                var markup = "<tbody><tr><td><div>This</div>Should<u>Work</u></td></tr></tbody>";
205                                                html.set(
206                                                        targetNode, markup,
207                                                        {
208                                                                "testname": "basicChecks changeContentTable"
209                                                        });
210                                                var res = ieTrimSpaceBetweenTags(targetNode.innerHTML.toLowerCase());
211                                                doh.is(markup.toLowerCase(), res);
212                                        },
213                                        tearDown: function(){
214                                                domConstruct.empty(targetNode);
215                                        }
216                                },
217                                {
218                                        name: 'setNodeList',
219                                        runTest: function(t){
220                                                console.log("basicChecks: " + this.name);
221                                                var tmpUL = domConstruct.create("ul");
222                                                domConstruct.create("li", { innerHTML: "item 1" }, tmpUL);
223                                                domConstruct.create("li", { innerHTML: "item 2" }, tmpUL);
224                                                console.log("ul content: ", tmpUL.innerHTML, tmpUL.childNodes.length);
225                                                targetNode = dom.byId("pane1");
226                                                html.set(
227                                                        targetNode, tmpUL.childNodes,
228                                                        {
229                                                                "testname": "basicChecks setNodeList"
230                                                        });
231                                                var res = query("li", dom.byId("pane1")).length;
232                                                doh.is(2, res);
233                                        },
234                                        tearDown: function(){
235                                                domConstruct.empty(targetNode);
236                                        }
237                                },
238                                {
239                                        name: 'setMixedContent',
240                                        runTest: function(t){
241                                                console.log("basicChecks: " + this.name);
242
243                                                targetNode = dom.byId("pane1");
244                                                var markup = '<h4>See Jane</h4>'
245                                                 + 'Look at her <span>Run</span>!';
246                                                html.set(
247                                                        targetNode, markup,
248                                                        {
249                                                                "testname": "basicChecks setMixedContent"
250                                                        });
251                                                var res = ieTrimSpaceBetweenTags(targetNode.innerHTML.toLowerCase());
252                                                doh.is(markup.toLowerCase(), res);
253                                        },
254                                        tearDown: function(){
255                                                domConstruct.empty(targetNode);
256                                        }
257                                },
258                                {
259                                        name: 'extractContent',
260                                        runTest: function(t){
261                                                console.log("basicChecks: " + this.name);
262                                                targetNode = dom.byId("pane1");
263                                                var markup = ''
264                                                +'<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">'
265                                                +'<html>                                                                                        '
266                                                +'      <head>                                                                                  '
267                                                +'              <title>                                                                         '
268                                                +'                      the title                                                                        '
269                                                +'              </title>                                                                        '
270                                                +'      </head>                                                                                 '
271                                                +'      <body>                                                                                  '
272                                                +'              <p>                                                                                     '
273                                                +'                      This is the <b>Good Stuff</b><br>               '
274                                                +'              </p>                                                                            '
275                                                +'      </body>                                                                                 '
276                                                +'</html>                                                                                       ';
277
278                                                html.set(
279                                                        targetNode, markup,
280                                                        {
281                                                                "testname": "basicChecks changeContentTable",
282                                                                extractContent: true
283                                                        });
284                                                doh.t(targetNode.innerHTML.indexOf("title") == -1);
285                                                doh.t(query("*", targetNode).length == 3);
286                                        },
287                                        tearDown: function(){
288                                                domConstruct.empty(targetNode);
289                                        }
290                                }
291                        ]);
292                        doh.register("nodelistExtension", [
293                                {
294                                        name: 'nodelistHtml',
295                                        runTest: function(t){
296                                                console.log("nodelistExtension: " + this.name);
297
298                                                query(".zork").html("<li data-dojo-type='ParserInstantiateTester'>1</li><li data-dojo-type='ParserInstantiateTester'>2</li><li data-dojo-type='ParserInstantiateTester'>3</li>",
299                                                {
300                                                        parseContent: true,
301                                                        onBegin: function() {
302                                                                this.content = this.content.replace(/([0-9])/g, "MOOO");
303                                                                this.inherited("onBegin", arguments);
304                                                        }
305                                                }).removeClass("notdone").addClass("done");
306
307                                                var liNodes = query(".zork > li");
308
309                                                // test to make sure three li's were added to class="zork" node (3x 3 set li's)
310                                                doh.is(9, liNodes.length);
311
312                                                // test the innerHTML's got replaced in our onBegin
313                                                doh.t( liNodes.every(function(n) { return n.innerHTML.match(/MOOO/) }) );
314                                                console.log(this.name + ": innerHTML.match subtest was ok");
315
316                                                // test the parent elements got the correct className
317                                                doh.t( query(".zork").every(function(n) { return n.className == "zork done"; }) );
318                                                console.log(this.name + ": li.className subtest was ok");
319
320                                                // and test the parser correctly created object from the child nodes
321                                                // ...they should all have a test attribute now
322                                                doh.t( liNodes.every(function(n) { return n.getAttribute("test") == "ok"; }) );
323                                                console.log(this.name + ": Tester instantiation subtest(getAttribute) was ok");
324
325                                        },
326                                        tearDown: function(){
327                                                // domConstruct.empty(targetNode);
328                                        }
329                                },
330                                {
331                                        name: "nodeListSimple",
332                                        runTest: function(t){
333                                                var txt = "foo";
334                                                query("#simpleText").html("<p>"+txt+"</p>");
335
336                                                // check if its there at all
337                                                var len = query("#simpleText p").length;
338                                                doh.is(1, len);
339
340                                                // check the inner html is right:
341                                                var p = query("#simpleText p")[0];
342                                                doh.t( p && p.innerHTML == txt );
343                                        }
344                                }
345                        ]);
346                        doh.register("fromMarkup", [
347                                {
348                                        name: 'contentOpFromMarkup',
349                                        runTest: function(t){
350                                                console.log("fromMarkup: " + this.name);
351
352                                                parser.parse("markupSetContentOp");
353                                                doh.t(dom.byId("markupPane").innerHTML == "markupSetContentOp: new node content");
354                                        },
355                                        tearDown: function(){
356                                                dom.byId("markupPane").innerHTML = "initial content";
357                                        }
358                                },
359                                {
360                                        name: 'extendedContentOpFromMarkup',
361                                        runTest: function(t){
362                                                console.log("fromMarkup: " + this.name);
363
364                                                parser.parse("markupSetContentOpX");
365
366                                                doh.t(dom.byId("markupPane").innerHTML == "markupSetContentOpX: new node content".toUpperCase());
367                                        },
368                                        tearDown: function(){
369                                                dom.byId("markupPane").innerHTML = "initial content";
370                                        }
371                                }
372                        ]);
373                        doh.register("reuse", [
374                                {
375                                        name: 'ContentSetterReUse',
376                                        runTest: function(t){
377                                                console.log("fromMarkup: " + this.name);
378
379                                                targetNode = dom.byId('pane1');
380                                                var args = [
381                                                        [
382                                                                "simple"
383                                                        ],
384                                                        [
385                                                                '<div data-dojo-type="SimpleThing" data-dojo-id="id00">parsed content</div>',
386                                                                {
387                                                                        parseContent: true
388                                                                }
389                                                        ],
390                                                        [
391                                                                '<div data-dojo-type="SimpleThing" data-dojo-id="id01">parsed content</div>',
392                                                                {
393                                                                        parseContent: true
394                                                                }
395                                                        ]
396                                                ];
397                                                var setter = new html._ContentSetter({
398                                                        node: targetNode
399                                                });
400                                                array.forEach(args, function(applyArgs) {
401                                                        setter.node = targetNode;
402                                                        setter.set.apply(setter, applyArgs);
403                                                        setter.tearDown();
404                                                });
405                                                doh.t(id00 && id01);
406                                                // check we cleaned up after ourselves
407                                                doh.f(setter.parseResults);
408                                        },
409                                        tearDown: function(){
410                                                dom.byId("markupPane").innerHTML = "initial content";
411                                        }
412                                }
413                        ]);
414
415                        // Test specification of inherited attributes dir, lang, etc.
416                        var handle;
417                        doh.register("inherited", [
418                                {
419                                        name: 'unspecified',
420                                        runTest: function(t){
421                                                var cont = '<div data-dojo-type="SimpleThing" data-dojo-id="ifrs" data="{}"></div>';
422
423                                                var parserCalled, inherited;
424                                                handle = aspect.after(parser, "parse", function(args){
425                                                        parserCalled = true;
426                                                        inherited = args.inherited;
427                                                }, true);
428
429                                                html.set(
430                                                        dom.byId("pane1"),
431                                                        cont,
432                                                        {
433                                                                parseContent: true
434                                                        }
435                                                );
436                                                doh.t(parserCalled, "parser was called");
437                                                doh.f("dir" in inherited, "no dir specified");
438                                                doh.f("lang" in inherited, "no lang specified");
439                                        },
440                                        tearDown: function(){
441                                                handle.remove();
442                                        }
443                                },
444                                {
445                                        name: 'specified',
446                                        runTest: function(t){
447                                                var cont = '<div data-dojo-type="SimpleThing" data-dojo-id="ifrs" data="{}"></div>';
448
449                                                var parserCalled, inherited;
450                                                handle = aspect.after(parser, "parse", function(args){
451                                                        parserCalled = true;
452                                                        inherited = args.inherited;
453                                                }, true);
454
455                                                html.set(
456                                                        dom.byId("pane1"),
457                                                        cont,
458                                                        {
459                                                                dir: "rtl",
460                                                                lang: "it_it",
461                                                                textDir: "ltr",
462                                                                parseContent: true
463                                                        }
464                                                );
465                                                doh.t(parserCalled, "parser was called");
466                                                doh.is("rtl", inherited.dir, "dir");
467                                                doh.is("it_it", inherited.lang, "lang");
468                                                doh.is("ltr", inherited.textDir, "textdir");
469                                        },
470                                        tearDown: function(){
471                                                handle.remove();
472                                        }
473                                }
474
475                        ]);
476
477                        doh.run();
478                });
479        </script>
480        <style>
481                @import "../../../dojo/resources/dojo.css";
482
483                .box {
484                        border: 1px solid black;
485                        height: 190px;
486                        width: 80%;
487                        overflow: auto;
488                }
489                .zork {
490                        width: 200px;
491                        margin: 10px;
492                        list-style-type: none;
493                }
494                .notdone {
495                        color: #999; background-color: #ccc;
496                }
497                .done {
498                        color: #fff; background-color: #090;
499                }
500                .done li {
501                        border: 1px; background-color: orange;
502                        width: 180px;
503                }
504                .red {
505                        color: red;
506                }
507
508        </style>
509</head>
510<body class='tundra'>
511        <h1>dojo.html.set</h1>
512
513        <div id="pane1"></div>
514        <div id="pane2"></div>
515        <table id='tableTest' class='box'>
516                <thead>
517                        <tr>
518                                <td></td>
519                        </tr>
520                </thead>
521                <tbody>
522                        <tr>
523                                <td></td>
524                        </tr>
525                <tbody>
526        </table>
527        <ul class="zork notdone">initial content</ul>
528        <ul class="zork notdone">initial content</ul>
529        <ul class="zork notdone">initial content</ul>
530
531        <div id="markupPane">initial content</div>
532        <div id="markupSetContentOp">
533                <div data-dojo-type="DeclarativeContentSetter" node="markupPane" content="markupSetContentOp: new node content"></div>
534        </div>
535        <div id="markupSetContentOpX">
536                <div data-dojo-type="DeclarativeContentSetter" data-dojo-id="markupSetContentOpX_setter" node="markupPane" content="markupSetContentOpX: new node content">
537                <script type="dojo/method" data-dojo-event="onBegin">
538                        this.content = this.content.toUpperCase();
539                        console.log(this.id + ", made my content look like this:" + this.content);
540                </script>
541                <script type="dojo/method" data-dojo-event="onFoo">
542                        console.log("dojo/method supplied onBegin");
543                        this.content = this.content.toUpperCase();
544                </script>
545                </div>
546        </div>
547        <div id="another">
548                <div id="myTest9" data-dojo-type="DeclarativeContentSetter">
549                        Some content
550                        <script type="dojo/method">
551                                console.log("parsed me!");
552                        </script>
553                </div>
554        </div>
555        <h1 id="simpleText">test</h1>
556</body>
557</html>
Note: See TracBrowser for help on using the repository browser.