-
Notifications
You must be signed in to change notification settings - Fork 20
/
test.js
249 lines (218 loc) · 7.55 KB
/
test.js
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
var VNode = require('virtual-dom/vnode/vnode');
var VText = require('virtual-dom/vnode/vtext');
var h = require('virtual-dom/h');
var svg = require('virtual-dom/virtual-hyperscript/svg');
var partial = require('vdom-thunk');
var assert = require('assert');
var toHTML = require('..');
describe('toHTML()', function () {
it('should not render invalid virtual nodes', function () {
assert.equal(toHTML(null), '');
assert.equal(toHTML('hi'), '');
});
it('should render simple HTML', function () {
var node = new VNode('span');
assert.equal(toHTML(node), '<span></span>');
});
it('should render inner text', function () {
var node = new VNode('span', null, [new VText('hello')]);
assert.equal(toHTML(node), '<span>hello</span>');
});
it('should convert properties to attributes', function () {
var node = new VNode('form', {
className: 'login',
acceptCharset: 'ISO-8859-1',
accessKey: 'h' // prop to lower case
});
assert.equal(toHTML(node), '<form class="login" accept-charset="ISO-8859-1" accesskey="h"></form>');
});
it('should not render end tags for void elements', function () {
var node = new VNode('input');
assert.equal(toHTML(node), '<input>');
node = new VNode('br');
assert.equal(toHTML(node), '<br>');
});
it('should not render non-standard properties', function () {
var node = new VNode('web-component', {
'ev-click': function () {},
'random-prop': 'random!',
toString: function () {}
});
assert.equal(toHTML(node), '<web-component></web-component>');
});
it('should not render null properties', function () {
var node = new VNode('web-component', {
'className': null,
'id': null
});
assert.equal(toHTML(node), '<web-component></web-component>');
});
it('should render CSS for style property', function () {
var node = new VNode('div', {
style: {
background: 'black',
color: 'red'
}
});
assert.equal(toHTML(node), '<div style="background: black; color: red;"></div>');
});
it('should convert style property to param-case', function () {
var node = new VNode('div', {
style: {
background: 'black',
color: 'red',
zIndex: '1'
}
});
assert.equal(toHTML(node), '<div style="background: black; color: red; z-index: 1;"></div>');
});
it('should render style element correctly', function(){
var node = new VNode('style',{},[
new VText(".logo {background-image: url('/mylogo.png');}")
]);
assert.equal(toHTML(node),"<style>.logo {background-image: url('/mylogo.png');}</style>");
});
it('should render data- attributes for dataset properties', function () {
var node = new VNode('div', {
dataset: {
foo: 'bar',
num: 42
}
});
assert.equal(toHTML(node), '<div data-foo="bar" data-num="42"></div>');
});
it('should convert data- attributes to param-case', function () {
var node = new VNode('div', {
dataset: {
fooBar: 'baz'
}
});
assert.equal(toHTML(node), '<div data-foo-bar="baz"></div>');
});
it('should render boolean properties', function () {
var node = new VNode('input', {
autofocus: true,
disabled: false
});
assert.equal(toHTML(node), '<input autofocus>');
});
it('should render overloaded boolean properties', function () {
var node = new VNode('a', {
href: '/images/xxx.jpg',
download: true
});
assert.equal(toHTML(node), '<a href="/images/xxx.jpg" download></a>');
node = new VNode('a', {
href: '/images/xxx.jpg',
download: 'sfw'
});
assert.equal(toHTML(node), '<a href="/images/xxx.jpg" download="sfw"></a>');
});
it('should render any attributes', function () {
var node = new VNode('circle', {
attributes: {
cx: "60",
cy: "60",
r: "50"
}
});
assert.equal(toHTML(node), '<circle cx="60" cy="60" r="50"></circle>');
});
it('should not render null attributes', function () {
var node = new VNode('circle', {
attributes: {
cx: "60",
cy: "60",
r: null
}
});
assert.equal(toHTML(node), '<circle cx="60" cy="60" ></circle>');
});
it('should render nested children', function () {
var node = new VNode('div', null, [
new VNode('div', { id: 'a-div' }, [
new VNode('div', null, [new VText('HI!')])
]),
new VNode('div', { className: 'just-another-div' })
]);
assert.equal(toHTML(node), '<div><div id="a-div"><div>HI!</div></div><div class="just-another-div"></div></div>');
});
it('should encode attribute names/values and text contents', function () {
var node = new VNode('div', {
attributes: {
'data-"hi"': '"hello"'
}
}, [new VText('<span>sup</span>')]);
assert.equal(toHTML(node), '<div data-"hi"=""hello""><span>sup</span></div>');
});
it('should not encode script tag contents', function () {
var node = new VNode('div', null, [
new VNode('script', null, [new VText('console.log("zzz");')])
]);
assert.equal(toHTML(node), '<div><script>console.log("zzz");</script></div>');
});
it('should render `innerHTML`', function () {
var node = new VNode('div', {
innerHTML: '<span>sup</span>'
});
assert.equal(toHTML(node), '<div><span>sup</span></div>');
});
it('should render thunks', function () {
var fn = function fn(text) {
return new VNode('span', null, [new VText(text)]);
};
var node = partial(fn, 'hello');
assert.equal(toHTML(node), '<span>hello</span>');
});
it('should render widgets', function () {
var Widget = function(text) {
this.text = text;
}
Widget.prototype.type = 'Widget';
Widget.prototype.render = function() {
return new VNode('span', null, [new VText(this.text)]);
};
var node = new Widget('hello');
assert.equal(toHTML(node), '<span>hello</span>');
});
it('should render tag in lowercase', function () {
var node = new VNode('SPAN', null, [new VText('hello')]);
assert.equal(toHTML(node), '<span>hello</span>');
});
it('should render hyperscript', function () {
var node = h('span', null, 'test');
assert.equal(toHTML(node), '<span>test</span>');
});
it('should not encode script tag contents, when using hyperscript', function () {
var node = h('div', null, [
h('script', null, 'console.log("zzz");')
]);
assert.equal(toHTML(node), '<div><script>console.log("zzz");</script></div>');
});
it('should not render end tags for void elements, when using hyperscript', function () {
var node = h('input');
assert.equal(toHTML(node), '<input>');
node = h('br');
assert.equal(toHTML(node), '<br>');
});
it('should preserve UTF-8 entities and escape special html characters', function () {
var node = h('span', null, '测试&\"\'<>');
assert.equal(toHTML(node), '<span>测试&"'<></span>');
});
it('should render svg with attributes in default namespace', function () {
var node = svg('svg', {
'viewBox': '0 0 10 10'
});
assert.equal(toHTML(node), '<svg viewBox="0 0 10 10"></svg>');
});
it('should render svg with attributes in non-default namespace', function () {
var node = svg('use', {
'xlink:href': '/abc.jpg'
});
assert.equal(toHTML(node), '<use xlink:href="/abc.jpg"></use>');
});
it('should render input value', function () {
var node = h('input', { type: 'submit', value: 'add' });
assert.equal(toHTML(node), '<input type="submit" value="add">');
});
});