bard / seethrough_js

E4X-based template engine

This URL has Read+Write access

seethrough_js / examples.js
100644 307 lines (244 sloc) 8.311 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
* Copyright 2008 by Massimiliano Mirra
*
* This file is part of seethrough.
*
* seethrough is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 3 of the License, or (at your
* option) any later version.
*
* SamePlace is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code
* versions of this program must display Appropriate Legal Notices, as
* required under Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License
* version 3, modified versions must display the "Powered by SamePlace"
* logo to users in a legible manner and the GPLv3 text must be made
* available to them.
*
* Author: Massimiliano Mirra, <bard [at] hyperstruct [dot] net>
*
*/
 
 
var ns_st = 'http://hyperstruct.net/seethrough#js';
 
XML.ignoreComments = false;
 
var examples = [
    {
        name: 'content',
 
        template: <div xmlns:st={ns_st} st:content='meta.title'/>,
 
        env: {
            meta: { title: 'Hello, world!' }
        },
 
        result: <div xmlns:st={ns_st}>Hello, world!</div>
    },
 
    {
        name: 'replace',
 
        template:
            <div xmlns:st={ns_st}>
            <span st:replace='meta.title'/>
            </div>,
 
        env: {
            meta: { title: 'Hello, world!' }
        },
 
        result:
            <div xmlns:st={ns_st}>Hello, world!</div>
    },
 
    {
        name: 'disable',
 
        template:
            <div xmlns:st={ns_st}>
            <span st:disable='true'>Hello, world!</span>
            </div>,
 
        env: {},
 
        result:
            <div xmlns:st={ns_st}/>,
    },
 
    {
        name: 'loop',
 
        template:
            <ul xmlns:st={ns_st}>
            <li st:loop='person people'>
            <span st:replace='person'/>
            </li>
            </ul>,
 
        env: {
            people: ['jim', 'spock', 'mccoy']
        },
 
        result:
            <ul xmlns:st={ns_st}>
            <li>jim</li>
            <li>spock</li>
            <li>mccoy</li>
            </ul>
    },
 
    {
        name: 'condition',
 
        template:
            <div xmlns:st={ns_st}>
            <span st:condition="flag1">I will be here</span>
            <span st:condition="flag2">I probably will not</span>
            </div>,
 
        env: {
            flag1: true,
            flag2: false
        },
 
        result:
            <div xmlns:st={ns_st}>
            <span>I will be here</span>
            </div>
    },
 
    {
        name: 'attribute',
 
        template:
            <div xmlns:st={ns_st}>
            <div><st:attr name="foo">bar</st:attr></div>
            </div>,
 
        env: {},
 
        result:
            <div xmlns:st={ns_st}>
            <div foo="bar"/>
            </div>
    },
 
    {
        name: 'empty attribute',
 
        template:
            <div xmlns:st={ns_st}>
            <div><st:attr name="foo"></st:attr></div>
            </div>,
 
        env: {},
 
        result:
            <div xmlns:st={ns_st}>
            <div/>
            </div>
    },
 
    // The next ones don't describe any new tag or attribute, they
    // just check formerly buggy situations.
 
    {
        name: 'attribute on looping item',
 
        template:
            <div xmlns:st={ns_st}>
            <ol>
            <li st:loop="item items">
            <st:attr name="_id"><span st:replace="item._id"/></st:attr>
            <span st:replace="item.name"/>
            </li>
            </ol>
            </div>,
 
        env: {
            items: [
                { _id: 1, name: 'foo' },
                { _id: 2, name: 'bar' }
            ]
        },
 
        result:
            <div xmlns:st={ns_st}>
            <ol>
            <li _id="1">foo</li>
            <li _id="2">bar</li>
            </ol>
            </div>
    },
 
    {
        name: 'handle comments',
 
        template:
            <head><!-- [if lt IE 8]>
            <script type="text/javascript" src="/static/IE8.js"> </script>
            <![endif]-->
            </head>,
 
        env: {},
 
        result:
            <head><!-- [if lt IE 8]>
            <script type="text/javascript" src="/static/IE8.js"> </script>
            <![endif]-->
            </head>
    },
 
    {
        name: 'preserve spaces',
 
        template: <head><script> </script></head>,
 
        env: {},
 
        result: <head><script> </script></head>
    },
 
    {
        name: 'empty children',
 
        template: <div xmlns:st={ns_st}><head/></div>,
 
        env: {},
 
        result: <div xmlns:st={ns_st}><head/></div>
    }
];
 
function verify() {
    function compareXML(t1, t2) {
        function compareXML1(tree1, tree2) {
            if(tree1.nodeKind() != tree2.nodeKind())
                throw new Error('Different node kinds. (' +
                                tree1.nodeKind() + ',' + tree2.nodeKind() + ')');
 
            switch(tree1.nodeKind()) {
            case 'element':
                if(tree1.name() != tree2.name())
                    throw new Error('Different tag names. (' +
                                    '<' + tree1.name() + '>, ' + '<' + tree2.name() + '>)');
                break;
            case 'text':
            case 'comment':
                if(tree1.valueOf() != tree2.valueOf())
                    throw new Error('Different ' + tree1.nodeKind() + ' values. (' +
                                    '<' + tree1.valueOf() + '>, ' + '<' + tree2.valueOf() + '>)');
 
                break;
            default:
                throw new Error('Unhandled node kind. (' + tree1.nodeKind() + ')');
            }
 
            var attrList1 = tree1.@*;
            var attrList2 = tree2.@*;
            if(attrList1.length() != attrList2.length())
                throw new Error('Different attribute count for <' + tree1.name() + '>. (expected ' +
                                attrList1.length() + ', got ' + attrList2.length() + ')');
 
            var childList1 = tree1.*;
            var childList2 = tree2.*;
            if(childList1.length() != childList2.length())
                throw new Error('Different child count for <' + tree1.name() + '>. (expected ' +
                                childList1.length() + ', got ' + childList2.length() + ')');
 
            for each(var attr in attrList1) {
                if(tree1['@' + attr.name()] != tree2['@' + attr.name()])
                    throw new Error('Different values for attribute @' + attr.name() + '. (expected ' +
                                    tree1['@' + attr.name()] + ', got ' + tree2['@' + attr.name()] + ')');
            }
 
            for(var i=0; i<childList1.length(); i++)
                compareXML(childList1[i], childList2[i])
 
        }
 
        t1.normalize();
        t2.normalize();
        compareXML1(t1, t2);
        return true;
    }
 
    var results = examples.map(function(example) {
        try {
            d.on = example.debug;
            var rendered = seethrough
                .compile(example.template)
                .call(null, example.env)
            compareXML(example.result, rendered);
        } catch(e) {
            return 'FAILURE: ' + example.name + '\n' +
                e.message + '\n' +
                e.stack + '\n' +
                rendered.toXMLString();
        }
    }).filter(function(result) { return result; });
 
    if(results.length > 0)
        return results.join('\n');
    else
        return ('\n**************************************************\n' +
                'Verified successfully.\n\n' +
                '**************************************************');
};
 
print(verify());
 
// Use `M-x recompile' to verify tests from Emacs (assumes
// spidermonkey is available somewhere as `js'')
 
// -*- compile-command: "cat seethrough.js examples.js | js"