forked from stenciljs/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserialize-node.spec.ts
More file actions
322 lines (255 loc) · 10.1 KB
/
serialize-node.spec.ts
File metadata and controls
322 lines (255 loc) · 10.1 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import { MockDocument } from '../document';
import { EMPTY_ELEMENTS, serializeNodeToHtml } from '../serialize-node';
describe('serializeNodeToHtml', () => {
let doc: MockDocument;
beforeEach(() => {
doc = new MockDocument();
});
it('remove most whitespace between elements, but not all of it when not in pretty print', () => {
const elm = doc.createElement('div');
elm.innerHTML = `<div>\n\n\n <span></span>\t\t \n<b></b><code> \t<b>var</b> <span>88 </span> </code> </div>`;
const html = serializeNodeToHtml(elm);
expect(html).toBe(`<div> <span></span> <b></b><code> \t<b>var</b> <span>88 </span> </code> </div>`);
});
it('remove most whitespace in text nodes, but not all of it when not in pretty print', () => {
const elm = doc.createElement('div');
elm.innerHTML = `<div><span>var \n \t </span><b>\n\n\n\t value\n\n\n\n\n \t</b><span> =</span><code> 88 </code>;</div>`;
const html = serializeNodeToHtml(elm);
expect(html).toBe(`<div><span>var </span><b> value </b><span> =</span><code> 88 </code>;</div>`);
});
it('do not add extra indentation when pretty print <pre><code>', () => {
const elm = doc.createElement('div');
elm.innerHTML = `<section><article><pre><code><span>88</span></code></pre></article></section>`;
const html = serializeNodeToHtml(elm, { prettyHtml: true });
expect(html).toBe(`<section>\n <article><pre><code><span>88</span></code></pre>\n </article>\n</section>`);
});
it('do not pretty print <pre><code>', () => {
const elm = doc.createElement('div');
elm.innerHTML = `<pre><code><span>88</span></code></pre>`;
const html = serializeNodeToHtml(elm, { prettyHtml: true });
expect(html).toBe(`<pre><code><span>88</span></code></pre>`);
});
it('do not pretty print <pre><code> w/ highlights and new', () => {
const elm = doc.createElement('div');
elm.innerHTML = `<pre><code><span>install</span> cordova-plugin-purchase\nnpx cap update</code></pre>`;
const html = serializeNodeToHtml(elm, { prettyHtml: true });
expect(html).toBe(`<pre><code><span>install</span> cordova-plugin-purchase\nnpx cap update</code></pre>`);
});
it('do not pretty print <pre><code> w/ html comments', () => {
const elm = doc.createElement('div');
elm.innerHTML = `<pre><code><span><!--a-->88</span>c<!--b--></code></pre>`;
const html = serializeNodeToHtml(elm, { prettyHtml: true });
expect(html).toBe(`<pre><code><span><!--a-->88</span>c<!--b--></code></pre>`);
});
it('do not pretty print <script>', () => {
const elm = doc.createElement('div');
elm.innerHTML = `<script>value = '';</script>`;
const html = serializeNodeToHtml(elm, { prettyHtml: true });
expect(html).toBe(`<script>value = '';</script>`);
});
it('do not remove whitespace within <code>', () => {
const elm = doc.createElement('div');
elm.innerHTML = `
<p>
<code>
<span>var</span>
<b>value</b>
<strong>=</strong>
<em>88</em><i>;</i>
</code>
</p>
`;
const html = serializeNodeToHtml(elm);
expect(html).toBe(`<p> <code>
<span>var</span>
<b>value</b>
<strong>=</strong>
<em>88</em><i>;</i>
</code> </p>`);
});
it('pretty print with comments', () => {
const elm = doc.createElement('div');
elm.innerHTML = `
<p>
<!--comment1-->
<!--comment2-->
<span>Hello</span>
<!--comment3-->
<!--comment4-->
</p>
`;
expect(elm).toEqualHtml(`
<div>
<p>
<!--comment1-->
<!--comment2-->
<span>Hello</span>
<!--comment3-->
<!--comment4-->
</p>
</div>
`);
});
it('shadow root to template', () => {
const elm = doc.createElement('cmp-a');
expect(elm.shadowRoot).toEqual(null);
const shadowRoot = elm.attachShadow({ mode: 'open' });
expect(shadowRoot.nodeType).toEqual(11);
expect(elm.shadowRoot.nodeType).toEqual(11);
expect(shadowRoot.host).toEqual(elm);
const shadowTop = doc.createElement('article');
shadowTop.innerHTML = 'shadow top';
shadowRoot.appendChild(shadowTop);
const slot = doc.createElement('slot');
shadowRoot.appendChild(slot);
const shadowBottom = doc.createElement('section');
shadowBottom.innerHTML = 'shadow bottom';
shadowRoot.appendChild(shadowBottom);
elm.innerHTML = '<div>light dom</div>';
expect(elm).toEqualHtml(`
<cmp-a>
<mock:shadow-root>
<article>shadow top</article>
<slot></slot>
<section>shadow bottom</section>
</mock:shadow-root>
<div>light dom</div>
</cmp-a>
`);
});
it('shadow root to template with focus delegation', () => {
const elm = doc.createElement('cmp-a');
expect(elm.shadowRoot).toEqual(null);
const shadowRoot = elm.attachShadow({ mode: 'open', delegatesFocus: true });
expect(shadowRoot.nodeType).toEqual(11);
expect(elm.shadowRoot.nodeType).toEqual(11);
expect(shadowRoot.host).toEqual(elm);
expect(elm.outerHTML).toContain('<template shadowrootmode="open" shadowrootdelegatesfocus');
});
it('shadow root with focus delegation matches toEqualHtml', () => {
const elm = doc.createElement('my-tag');
elm.setAttribute('tabindex', '0');
const shadowRoot = elm.attachShadow({ mode: 'open', delegatesFocus: true });
const div = doc.createElement('div');
div.innerHTML = 'test content';
shadowRoot.appendChild(div);
// Test that the serialized output matches the expected format
// This ensures shadowrootdelegatesfocus appears without ="" when compared
expect(elm).toEqualHtml(`
<my-tag tabindex="0">
<mock:shadow-root shadowrootdelegatesfocus>
<div>test content</div>
</mock:shadow-root>
</my-tag>
`);
});
it('style', () => {
const input = `<style> \n text \n\n</style>`;
doc.body.innerHTML = input;
const output = serializeNodeToHtml(doc.body);
expect(output).toBe(`<style>text</style>`);
});
it('template', () => {
const input = `<template>text</template>`;
doc.body.innerHTML = input;
const output = serializeNodeToHtml(doc.body);
expect(output).toBe(`<template>text</template>`);
});
it('svg', () => {
const input = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"></svg>`;
doc.body.innerHTML = input;
const output = serializeNodeToHtml(doc.body);
expect(input).toBe(output);
});
it('remove boolean attributes', () => {
const input = `<input type="checkbox" checked="">`;
doc.body.innerHTML = input;
const output = serializeNodeToHtml(doc.body, { removeBooleanAttributeQuotes: true });
expect(output).toBe(`<input type="checkbox" checked>`);
});
it('do not collapse boolean attributes', () => {
const input = `<input type="checkbox" checked="">`;
doc.body.innerHTML = input;
const output = serializeNodeToHtml(doc.body);
expect(input).toBe(output);
});
it('do not remove empty attrs', () => {
const elm = doc.createElement('button');
elm.setAttribute('class', '');
elm.setAttribute('dir', '');
elm.setAttribute('my-attr', '');
elm.setAttribute('id', '');
elm.setAttribute('data-custom', '');
elm.setAttribute('lang', '');
elm.setAttribute('name', '');
elm.setAttribute('title', '');
const html = serializeNodeToHtml(elm, { outerHtml: true, removeEmptyAttributes: false });
expect(html).toBe(`<button class="" dir="" my-attr="" id="" data-custom="" lang="" name="" title=""></button>`);
});
it('remove empty attrs', () => {
const elm = doc.createElement('button');
elm.setAttribute('class', '');
elm.setAttribute('dir', '');
elm.setAttribute('my-attr', '');
elm.setAttribute('id', '');
elm.setAttribute('data-custom', '');
elm.setAttribute('lang', '');
elm.setAttribute('name', '');
elm.setAttribute('title', '');
const html = serializeNodeToHtml(elm, { outerHtml: true });
expect(html).toBe(`<button my-attr="" data-custom></button>`);
});
it('set attributes, pretty', () => {
const elm = doc.createElement('button');
elm.setAttribute('type', 'submit');
elm.setAttribute('id', 'btn');
elm.textContent = `Text`;
const html = serializeNodeToHtml(elm, { outerHtml: true, prettyHtml: true });
expect(html).toBe(`<button id="btn" type="submit">
Text
</button>`);
});
it('set attributes', () => {
const elm = doc.createElement('button');
elm.setAttribute('type', 'submit');
elm.setAttribute('id', 'btn');
elm.textContent = `Text`;
const html = serializeNodeToHtml(elm, { outerHtml: true });
expect(html).toBe(`<button type="submit" id="btn">Text</button>`);
});
it('do not escape scripts', () => {
const elm = doc.createElement('script');
elm.innerHTML = `if (true && false) { console.log('hi); }`;
const html = serializeNodeToHtml(elm, { outerHtml: true });
expect(html).toBe(`<script>if (true && false) { console.log('hi); }</script>`);
});
it('empty document', () => {
const html = serializeNodeToHtml(doc);
expect(html).toBe(`<!doctype html><html><head></head><body></body></html>`);
});
it('empty document, pretty', () => {
const html = serializeNodeToHtml(doc, { prettyHtml: true });
expect(html).toBe(`<!doctype html>
<html>
<head></head>
<body></body>
</html>`);
});
it('script innerHTML', () => {
const input = `x<y && a>b`;
const scriptElm = doc.createElement('script');
scriptElm.innerHTML = input;
expect(scriptElm.innerHTML).toBe(input);
});
it.each([...EMPTY_ELEMENTS])("does not add a closing tag for '%s'", (voidElm) => {
if (voidElm === 'frame') {
// 'frame' is a non-HTML5 compatible/deprecated element.
// Stencil technically still supports it, but it doesn't get rendered as a child element, so let's just skip it
return;
}
const elm = doc.createElement('div');
elm.innerHTML = `<${voidElm}>`;
const html = serializeNodeToHtml(elm, { prettyHtml: true });
expect(html).toBe(`<${voidElm}>`);
});
});