/
XMLSerializer.test.ts
219 lines (174 loc) · 6.63 KB
/
XMLSerializer.test.ts
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
import XMLSerializer from '../../src/xml-serializer/XMLSerializer';
import Window from '../../src/window/Window';
import Document from '../../src/nodes/document/Document';
import CustomElement from '../CustomElement';
describe('XMLSerializer', () => {
let window: Window;
let document: Document;
let xmlSerializer: XMLSerializer;
beforeEach(() => {
window = new Window();
document = window.document;
xmlSerializer = new XMLSerializer();
window.customElements.define('custom-element', CustomElement);
});
afterEach(() => {
CustomElement.shadowRootMode = 'open';
});
describe('serializeToString()', () => {
it('Serializes a <div> element.', () => {
const div = document.createElement('div');
const span = document.createElement('span');
span.setAttribute('attr1', 'value1');
span.setAttribute('attr2', 'value2');
span.setAttribute('attr3', '');
div.setAttribute('attr1', 'value1');
div.setAttribute('attr2', 'value2');
div.setAttribute('attr3', '');
div.appendChild(span);
expect(xmlSerializer.serializeToString(div)).toBe(
'<div attr1="value1" attr2="value2" attr3=""><span attr1="value1" attr2="value2" attr3=""></span></div>'
);
});
it('Serializes void elements elements like img correctly.', () => {
const div = document.createElement('div');
const img = document.createElement('img');
img.setAttribute('src', 'https://localhost/img.jpg');
div.appendChild(img);
expect(xmlSerializer.serializeToString(div)).toBe(
'<div><img src="https://localhost/img.jpg"></div>'
);
});
it('Serializes a comment node.', () => {
const div = document.createElement('div');
const comment = document.createComment('');
comment.textContent = 'Some comment.';
div.appendChild(comment);
expect(xmlSerializer.serializeToString(div)).toBe('<div><!--Some comment.--></div>');
});
it('Serializes a text nodes.', () => {
const div = document.createElement('div');
const text1 = document.createTextNode('Text 1.');
const text2 = document.createTextNode('Text 2.');
div.appendChild(text1);
div.appendChild(text2);
expect(xmlSerializer.serializeToString(div)).toBe('<div>Text 1.Text 2.</div>');
});
it('Serializes a template node.', () => {
const template = document.createElement('template');
template.innerHTML = '<div>Test</div>';
expect(xmlSerializer.serializeToString(template)).toBe(
'<template><div>Test</div></template>'
);
});
it('Serializes a mix of nodes.', () => {
const div = document.createElement('div');
const comment1 = document.createComment('Comment 1.');
const comment2 = document.createComment('Comment 2.');
const text1 = document.createTextNode('Text 1.');
const text2 = document.createTextNode('Text 2.');
const text3 = document.createTextNode('Text 3.');
const span1 = document.createElement('span');
const span2 = document.createElement('span');
span2.setAttribute('attr1', 'value1');
span2.appendChild(text3);
span1.setAttribute('attr1', 'value1');
span1.setAttribute('attr2', 'value2');
span1.setAttribute('attr3', '');
span1.appendChild(span2);
div.appendChild(comment1);
div.appendChild(text1);
div.appendChild(comment2);
div.appendChild(text2);
div.appendChild(span1);
expect(xmlSerializer.serializeToString(div)).toBe(
'<div><!--Comment 1.-->Text 1.<!--Comment 2.-->Text 2.<span attr1="value1" attr2="value2" attr3=""><span attr1="value1">Text 3.</span></span></div>'
);
});
it('Serializes a custom element.', () => {
const div = document.createElement('div');
const customElement = document.createElement('custom-element');
customElement.setAttribute('attr1', 'value1');
customElement.setAttribute('attr2', 'value2');
customElement.setAttribute('attr3', '');
div.appendChild(customElement);
// Connects the custom element to DOM which will trigger connectedCallback() on it
document.body.appendChild(div);
expect(xmlSerializer.serializeToString(div)).toBe(
'<div><custom-element attr1="value1" attr2="value2" attr3=""></custom-element></div>'
);
});
it('Includes shadow roots of custom elements when the "includeShadowRoots" sent in as an option.', () => {
const div = document.createElement('div');
const customElement1 = document.createElement('custom-element');
CustomElement.shadowRootMode = 'closed';
const customElement2 = document.createElement('custom-element');
customElement1.setAttribute('key1', 'value1');
customElement1.setAttribute('key2', 'value2');
customElement1.innerHTML = '<span>Slotted content</span>';
customElement2.setAttribute('key1', 'value4');
customElement2.setAttribute('key2', 'value5');
div.appendChild(customElement1);
div.appendChild(customElement2);
// Connects the custom element to DOM which will trigger connectedCallback() on it
document.body.appendChild(div);
expect(
xmlSerializer.serializeToString(div, { includeShadowRoots: true }).replace(/[\s]/gm, '')
).toBe(
`
<div>
<custom-element key1="value1" key2="value2">
<span>Slotted content</span>
<template shadowroot="open">
<style>
:host {
display: block;
}
div {
color: red;
}
.class1 {
color: blue;
}
.class1.class2 span {
color: green;
}
.class1[attr1="value1"] {
color: yellow;
}
[attr1="value1"] {
color: yellow;
}
</style>
<div>
<span>
key1 is "value1" and key2 is "value2".
</span>
<span><slot></slot></span>
</div>
</template>
</custom-element>
<custom-element key1="value4" key2="value5"></custom-element>
</div>`.replace(/[\s]/gm, '')
);
});
it('Does not escape unicode attributes.', () => {
const div = document.createElement('div');
div.setAttribute('attr1', 'Hello \u{2068}John\u{2069}');
div.setAttribute('attr2', '<span> test');
div.setAttribute('attr3', '');
expect(xmlSerializer.serializeToString(div)).toBe(
'<div attr1="Hello \u{2068}John\u{2069}" attr2="<span> test" attr3=""></div>'
);
});
it('Serializes the is value.', () => {
const div = document.createElement('div', { is: 'custom-element' });
expect(xmlSerializer.serializeToString(div)).toBe('<div is="custom-element"></div>');
});
it('Ignores the is value if the is attribute is present.', () => {
const div = document.createElement('div', { is: 'custom-element' });
div.setAttribute('is', 'custom-replacement');
expect(xmlSerializer.serializeToString(div)).toBe('<div is="custom-replacement"></div>');
});
});
});