source: trip-planner-front/node_modules/dom-serialize/test/test.js@ 188ee53

Last change on this file since 188ee53 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 12.3 KB
Line 
1
2var assert = require('assert');
3var serialize = require('../');
4
5describe('node-serialize', function () {
6 var node;
7
8 afterEach(function () {
9 if (node) {
10 // clean up...
11 if (node.parentNode) {
12 node.parentNode.removeChild(node);
13 }
14 node = null;
15 }
16 });
17
18 it('should return an empty string on invalid input', function () {
19 assert.strictEqual('', serialize(null));
20 });
21
22 it('should serialize a SPAN element', function () {
23 node = document.createElement('span');
24 assert.equal('<span></span>', serialize(node));
25 });
26
27 it('should serialize a BR element', function () {
28 node = document.createElement('br');
29 assert.equal('<br>', serialize(node));
30 });
31
32 it('should serialize a text node', function () {
33 node = document.createTextNode('test');
34 assert.equal('test', serialize(node));
35 });
36
37 it('should serialize a text node with special HTML characters', function () {
38 node = document.createTextNode('<>\'"&');
39 assert.equal('&lt;&gt;\'"&amp;', serialize(node));
40 });
41
42 it('should serialize a DIV element with child nodes', function () {
43 node = document.createElement('div');
44 node.appendChild(document.createTextNode('hello '));
45 var b = document.createElement('b');
46 b.appendChild(document.createTextNode('world'));
47 node.appendChild(b);
48 node.appendChild(document.createTextNode('!'));
49 node.appendChild(document.createElement('br'));
50 assert.equal('<div>hello <b>world</b>!<br></div>', serialize(node));
51 });
52
53 it('should serialize a DIV element with attributes', function () {
54 node = document.createElement('div');
55 node.setAttribute('foo', 'bar');
56 node.setAttribute('escape', '<>&"\'');
57 assert.equal('<div foo="bar" escape="&lt;&gt;&amp;&quot;&apos;"></div>', serialize(node));
58 });
59
60 it('should serialize an Attribute node', function () {
61 var div = document.createElement('div');
62 div.setAttribute('foo', 'bar');
63 node = div.attributes[0];
64 assert.equal('foo="bar"', serialize(node));
65 });
66
67 it('should serialize a Comment node', function () {
68 node = document.createComment('test');
69 assert.equal('<!--test-->', serialize(node));
70 });
71
72 it('should serialize a Document node', function () {
73 node = document.implementation.createDocument('http://www.w3.org/1999/xhtml', 'html', null);
74 assert.equal('<html></html>', serialize(node));
75 });
76
77 it('should serialize a Doctype node', function () {
78 node = document.implementation.createDocumentType(
79 'html',
80 '-//W3C//DTD XHTML 1.0 Strict//EN',
81 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'
82 );
83
84 // Some older browsers require the DOCTYPE to be within a Document,
85 // otherwise the "serialize" custom event is considered "cancelled".
86 // See: https://travis-ci.org/webmodules/dom-serialize/builds/47307749
87 var doc = document.implementation.createDocument('http://www.w3.org/1999/xhtml', 'html', node);
88
89 assert.equal('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">', serialize(node));
90 assert.equal('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html></html>', serialize(doc));
91 });
92
93 it('should serialize a Doctype node with systemId', function () {
94 node = document.implementation.createDocumentType(
95 'root-element',
96 '',
97 'http://www.w3.org/1999/xhtml'
98 );
99 document.implementation.createDocument('http://www.w3.org/1999/xhtml', 'root-element', node);
100
101 assert.equal('<!DOCTYPE root-element SYSTEM "http://www.w3.org/1999/xhtml">', serialize(node));
102 });
103
104
105 it('should serialize an HTML5 Doctype node', function () {
106 node = document.implementation.createDocumentType(
107 'html',
108 '',
109 ''
110 );
111 document.implementation.createDocument('http://www.w3.org/1999/xhtml', 'node', node);
112
113 assert.equal('<!DOCTYPE html>', serialize(node));
114 });
115
116 it('should serialize a DocumentFragment node', function () {
117 node = document.createDocumentFragment();
118 node.appendChild(document.createElement('b'));
119 node.appendChild(document.createElement('i'));
120 node.lastChild.appendChild(document.createTextNode('foo'));
121 assert.equal('<b></b><i>foo</i>', serialize(node));
122 });
123
124 it('should serialize an Array of nodes', function () {
125 var array = [];
126 array.push(document.createTextNode('foo'));
127 array.push(document.createElement('div'));
128 array[1].appendChild(document.createTextNode('bar'));
129 assert.equal('foo<div>bar</div>', serialize(array));
130 });
131
132 describe('serializeText()', function () {
133
134 it('should serialize an Attribute node', function () {
135 var d = document.createElement('div');
136 d.setAttribute('foo', '<>"&');
137 assert.equal('foo="&lt;&gt;&quot;&amp;"', serialize.serializeAttribute(d.attributes[0]));
138 });
139
140 it('should allow an "options" object to be passed in', function () {
141 var d = document.createElement('div');
142 d.setAttribute('foo', '<>"&');
143 assert.equal('foo="&#60;&#62;&#34;&#38;"', serialize.serializeAttribute(d.attributes[0], { named: false }));
144 });
145
146 });
147
148 describe('serializeText()', function () {
149
150 it('should serialize a TextNode instance', function () {
151 node = document.createTextNode('<b>&');
152 assert.equal('&lt;b&gt;&amp;', serialize.serializeText(node));
153 });
154
155 it('should allow an "options" object to be passed in', function () {
156 node = document.createTextNode('<b>&');
157 assert.equal('&#60;b&#62;&#38;', serialize.serializeText(node, { named: false }));
158 });
159
160 });
161
162 describe('"serialize" event', function () {
163
164 it('should emit a "serialize" event on a DIV node', function () {
165 node = document.createElement('div');
166 var count = 0;
167 node.addEventListener('serialize', function (e) {
168 count++;
169 e.detail.serialize = 'MEOW';
170 });
171 assert.equal(0, count);
172 assert.equal('MEOW', serialize(node));
173 assert.equal(1, count);
174 });
175
176 it('should emit a "serialize" event on a Text node', function () {
177 node = document.createTextNode('whaaaaa!!!!!!');
178 var count = 0;
179 node.addEventListener('serialize', function (e) {
180 count++;
181 e.detail.serialize = 'MEOW';
182 });
183 assert.equal(0, count);
184 assert.equal('MEOW', serialize(node));
185 assert.equal(1, count);
186 });
187
188 it('should output an empty string when event is cancelled', function () {
189 node = document.createElement('div');
190 node.appendChild(document.createTextNode('!'));
191 var count = 0;
192 node.firstChild.addEventListener('serialize', function (e) {
193 count++;
194 e.preventDefault();
195 });
196 assert.equal(0, count);
197 assert.equal('<div></div>', serialize(node));
198 assert.equal(1, count);
199 });
200
201 it('should render a Number when set as `e.detail.serialize`', function () {
202 node = document.createTextNode('whaaaaa!!!!!!');
203 var count = 0;
204 node.addEventListener('serialize', function (e) {
205 count++;
206 e.detail.serialize = 123;
207 });
208 assert.equal(0, count);
209 assert.equal('123', serialize(node));
210 assert.equal(1, count);
211 });
212
213 it('should render a Node when set as `e.detail.serialize`', function () {
214 node = document.createTextNode('whaaaaa!!!!!!');
215 var count = 0;
216 node.addEventListener('serialize', function (e) {
217 count++;
218 if (count === 1) {
219 e.detail.serialize = document.createTextNode('foo');
220 }
221 });
222 assert.equal(0, count);
223 assert.equal('foo', serialize(node));
224 assert.equal(2, count);
225 });
226
227 it('should render a Node when set as `e.detail.serialize` and event is cancelled', function () {
228 node = document.createTextNode('whaaaaa!!!!!!');
229 var count = 0;
230 node.addEventListener('serialize', function (e) {
231 count++;
232 if (count === 1) {
233 e.preventDefault();
234 e.detail.serialize = document.createTextNode('foo');
235 }
236 });
237 assert.equal(0, count);
238 assert.equal('foo', serialize(node));
239 assert.equal(2, count);
240 });
241
242 it('should have `context` set on the event', function () {
243 node = document.createTextNode('');
244 var count = 0;
245 node.addEventListener('serialize', function (e) {
246 count++;
247 e.detail.serialize = e.detail.context;
248 });
249 assert.equal(0, count);
250 assert.equal('foo', serialize(node, 'foo'));
251 assert.equal(1, count);
252 assert.equal('bar', serialize(node, 'bar'));
253 assert.equal(2, count);
254 assert.equal('baz', serialize(node, 'baz'));
255 assert.equal(3, count);
256 });
257
258 it('should bubble', function () {
259 node = document.createElement('div');
260 node.appendChild(document.createTextNode('foo'));
261 node.appendChild(document.createTextNode(' '));
262 node.appendChild(document.createTextNode('bar'));
263
264 // `node` must be inside the DOM for the "serialize" event to bubble
265 document.body.appendChild(node);
266
267 var count = 0;
268 node.addEventListener('serialize', function (e) {
269 count++;
270 assert.equal('foo', e.detail.context);
271 if (e.serializeTarget === node)
272 return;
273 else if (e.serializeTarget.nodeValue === 'foo')
274 e.detail.serialize = '…';
275 else
276 e.preventDefault();
277 }, false);
278
279 assert.equal(0, count);
280 assert.equal('<div>…</div>', serialize(node, 'foo'));
281 assert.equal(4, count);
282 });
283
284 it('should support one-time callback function on Elements', function () {
285 node = document.createElement('div');
286 var count = 0;
287
288 function callback (e) {
289 count++;
290 e.detail.serialize = count;
291 }
292
293 assert.equal(0, count);
294 assert.equal('1', serialize(node, callback));
295 assert.equal(1, count);
296 assert.equal('<div></div>', serialize(node));
297 assert.equal(1, count);
298 });
299
300 it('should support one-time callback function on NodeLists', function () {
301 node = document.createElement('div');
302 node.appendChild(document.createElement('strong'));
303 node.appendChild(document.createTextNode('foo'));
304 node.appendChild(document.createElement('em'));
305 node.appendChild(document.createTextNode('bar'));
306
307 var count = 0;
308
309 function callback (e) {
310 count++;
311 e.detail.serialize = count;
312 }
313
314 assert.equal(0, count);
315 assert.equal('1234', serialize(node.childNodes, callback));
316 assert.equal(4, count);
317 assert.equal('<strong></strong>foo<em></em>bar', serialize(node.childNodes));
318 assert.equal(4, count);
319 });
320
321 it('should support one-time callback function on Nodes set in `e.detail.serialize`', function () {
322 node = document.createElement('div');
323 node.appendChild(document.createTextNode('foo'));
324
325 // `node` must be inside the DOM for the "serialize" event to bubble
326 document.body.appendChild(node);
327
328 var count = 0;
329
330 function callback (e) {
331 count++;
332
333 if (2 === count) {
334 assert.equal('foo', e.serializeTarget.nodeValue);
335 var text = document.createTextNode('bar');
336 e.detail.serialize = text;
337 } else if (3 === count) {
338 assert.equal('bar', e.serializeTarget.nodeValue);
339 var text = document.createTextNode('baz');
340 e.detail.serialize = text;
341 }
342 }
343
344 assert.equal(0, count);
345 assert.equal('<div>baz</div>', serialize(node, callback));
346 assert.equal(4, count);
347 });
348
349 it('should support one-time callback function on complex Nodes set in `e.detail.serialize`', function () {
350 node = document.createElement('div');
351 node.appendChild(document.createTextNode('foo'));
352
353 // `node` must be inside the DOM for the "serialize" event to bubble
354 document.body.appendChild(node);
355
356 var count = 0;
357
358 function callback (e) {
359 count++;
360 if (e.serializeTarget.nodeValue === 'foo') {
361 var el = document.createElement('p');
362 el.appendChild(document.createTextNode('x '));
363 el.appendChild(document.createElement('i'));
364 el.lastChild.appendChild(document.createTextNode('bar'));
365
366 e.detail.serialize = el;
367 }
368 }
369
370 assert.equal(0, count);
371 assert.equal('<div><p>x <i>bar</i></p></div>', serialize(node, callback));
372 assert.equal(6, count);
373 });
374
375 });
376
377});
Note: See TracBrowser for help on using the repository browser.