smartxml: fix - handle creation of an empty text node
[fnpeditor.git] / src / smartxml / smartxml.test.js
1 define([
2     'libs/chai',
3     'libs/sinon',
4     './smartxml.js'
5 ], function(chai, sinon, smartxml) {
6     
7 'use strict';
8 /*jshint expr:true */
9 /* global describe, it, beforeEach */
10
11 var expect = chai.expect;
12
13
14 var getDocumentFromXML = function(xml) {
15     return smartxml.documentFromXML(xml);
16 };
17
18 var elementNodeFromParams = function(params) {
19     return smartxml.elementNodeFromXML('<' + params.tag + '></' + params.tag + '>');
20 };
21
22 var elementNodeFromXML = function(xml) {
23     return smartxml.elementNodeFromXML(xml);
24 };
25
26
27 describe('smartxml', function() {
28
29     describe('Basic Document properties', function() {
30         it('exposes its root element', function() {
31             var doc = getDocumentFromXML('<div></div>');
32             expect(doc.root.getTagName()).to.equal('div');
33         });
34
35         it('can resets its content entirely', function() {
36             var doc = getDocumentFromXML('<div></div>');
37
38             expect(doc.root.getTagName()).to.equal('div');
39
40             doc.loadXML('<header></header>');
41             expect(doc.root.getTagName()).to.equal('header');
42         });
43
44         it('knows if it contains an ElementNode in its tree', function() {
45             var doc = getDocumentFromXML('<root><a></a>text</root>'),
46                 root = doc.root,
47                 a = root.contents()[0],
48                 text = root.contents()[1];
49
50             expect(doc.containsNode(root)).to.equal(true, 'contains its root');
51             expect(doc.containsNode(a)).to.equal(true, 'contains Element Node');
52             expect(doc.containsNode(text)).to.equal(true, 'contains Text Node');
53         });
54
55         it('creates text nodes', function() {
56             var doc = getDocumentFromXML('<div></div>'),
57                 emptyTextNode = doc.createDocumentNode({text:''}),
58                 nonEmptyTextNode = doc.createDocumentNode({text: 'alice'});
59             expect(emptyTextNode.getText()).to.equal('', 'empty ok');
60             expect(nonEmptyTextNode.getText()).to.equal('alice', 'non empty ok');
61         });
62     });
63
64     describe('Basic ElementNode properties', function() {
65         it('exposes node contents', function() {
66             var node = elementNodeFromXML('<node>Some<node>text</node>is here</node>'),
67                 contents = node.contents();
68
69             expect(contents).to.have.length(3);
70             expect(contents[0].nodeType).to.equal(Node.TEXT_NODE, 'text node 1');
71             expect(contents[1].nodeType).to.equal(Node.ELEMENT_NODE, 'element node 1');
72             expect(contents[2].nodeType).to.equal(Node.TEXT_NODE, 'text node 2');
73         });
74
75         describe('Storing custom data', function() {
76             var node;
77
78             beforeEach(function() {
79                 node = elementNodeFromXML('<div></div>');
80             });
81
82             it('can append single value', function() {
83                 node.setData('key', 'value');
84                 expect(node.getData('key')).to.equal('value');
85             });
86
87             it('can overwrite the whole data', function() {
88                 node.setData('key1', 'value1');
89                 node.setData({key2: 'value2'});
90                 expect(node.getData('key2')).to.equal('value2');
91             });
92
93             it('can fetch the whole data at once', function() {
94                 node.setData({key1: 'value1', key2: 'value2'});
95                 expect(node.getData()).to.eql({key1: 'value1', key2: 'value2'});
96             });
97         });
98
99         describe('Changing node tag', function() {
100
101             it('can change tag name', function() {
102                 var node = elementNodeFromXML('<div></div>');
103                 node.setTag('span');
104                 expect(node.getTagName()).to.equal('span');
105             });
106
107             it('emits nodeTagChange event', function() {
108                 var node = elementNodeFromXML('<div></div>'),
109                     spy = sinon.spy();
110
111                 node.document.on('change', spy);
112                 node.setTag('span');
113                 var event = spy.args[0][0];
114
115                 expect(event.type).to.equal('nodeTagChange');
116                 expect(event.meta.node.sameNode(node)).to.be.true;
117                 expect(event.meta.oldTagName).to.equal('div');
118             });
119
120             describe('Implementation specific expectations', function() {
121                 // DOM specifies ElementNode tag as a read-only property, so
122                 // changing it in a seamless way is a little bit tricky. For this reason
123                 // the folowing expectations are required, despite the fact that they actually are
124                 // motivated by implemetation details.
125
126                 it('keeps node in the document', function() {
127                     var doc = getDocumentFromXML('<div><header></header></div>'),
128                         header = doc.root.contents()[0];
129                     header.setTag('span');
130                     expect(header.parent().sameNode(doc.root)).to.be.true;
131                 });
132                 it('keeps custom data', function() {
133                     var node = elementNodeFromXML('<div></div>');
134
135                     node.setData('key', 'value');
136                     node.setTag('header');
137                     
138                     expect(node.getTagName()).to.equal('header');
139                     expect(node.getData()).to.eql({key: 'value'});
140                 });
141
142                 it('can change document root tag name', function() {
143                     var doc = getDocumentFromXML('<div></div>');
144                     doc.root.setTag('span');
145                     expect(doc.root.getTagName()).to.equal('span');
146                 });
147
148                 it('keeps contents', function() {
149                     var node = elementNodeFromXML('<div><div></div></div>');
150                     node.setTag('header');
151                     expect(node.contents()).to.have.length(1);
152                 });
153             });
154
155         describe('Setting node attributes', function() {
156             it('can set node attribute', function() {
157                 var node = elementNodeFromXML('<div></div>');
158
159                 node.setAttr('key', 'value');
160                 expect(node.getAttr('key')).to.equal('value');
161             });
162             it('emits nodeAttrChange event', function() {
163                 var node = elementNodeFromXML('<div key="value1"></div>'),
164                     spy = sinon.spy();
165
166                 node.document.on('change', spy);
167                 node.setAttr('key', 'value2');
168                 var event = spy.args[0][0];
169
170                 expect(event.type).to.equal('nodeAttrChange');
171                 expect(event.meta.node.sameNode(node)).to.be.true;
172                 expect(event.meta.attr).to.equal('key');
173                 expect(event.meta.oldVal).to.equal('value1');
174             });
175         });
176
177         });
178     });
179
180     describe('Basic TextNode properties', function() {
181         it('can have its text set', function() {
182             var node = elementNodeFromXML('<div>Alice</div>'),
183                 textNode = node.contents()[0];
184
185             textNode.setText('Cat');
186             expect(textNode.getText()).to.equal('Cat');
187         });
188
189         it('emits nodeTextChange', function() {
190             var node = elementNodeFromXML('<div>Alice</div>'),
191                 textNode = node.contents()[0],
192                 spy = sinon.spy();
193
194             textNode.document.on('change', spy);
195             textNode.setText('Cat');
196
197             var event = spy.args[0][0];
198             expect(event.type).to.equal('nodeTextChange');
199         });
200
201         it('puts NodeElement after itself', function() {
202             var node = elementNodeFromXML('<div>Alice</div>'),
203                 textNode = node.contents()[0],
204                 returned = textNode.after({tagName:'div'});
205             expect(returned.sameNode(node.contents()[1])).to.be.true;
206         });
207
208         it('puts NodeElement before itself', function() {
209             var node = elementNodeFromXML('<div>Alice</div>'),
210                 textNode = node.contents()[0],
211                 returned = textNode.before({tagName:'div'});
212             expect(returned.sameNode(node.contents()[0])).to.be.true;
213         });
214
215         describe('Wrapping TextNode contents', function() {
216
217             it('wraps DocumentTextElement', function() {
218                 var node = elementNodeFromXML('<section>Alice</section>'),
219                     textNode = node.contents()[0];
220                 
221                 var returned = textNode.wrapWith({tagName: 'header'}),
222                     parent = textNode.parent(),
223                     parent2 = node.contents()[0];
224
225                 expect(returned.sameNode(parent)).to.be.equal(true, 'wrapper is a parent');
226                 expect(returned.sameNode(parent2)).to.be.equal(true, 'wrapper has a correct parent');
227                 expect(returned.getTagName()).to.equal('header');
228             });
229
230             describe('wrapping part of DocumentTextElement', function() {
231                 [{start: 5, end: 12}, {start: 12, end: 5}].forEach(function(offsets) {
232                     it('wraps in the middle ' + offsets.start + '/' + offsets.end, function() {
233                         var node = elementNodeFromXML('<section>Alice has a cat</section>'),
234                             textNode = node.contents()[0];
235                         
236                         var returned = textNode.wrapWith({tagName: 'header', attrs: {'attr1': 'value1'}, start: offsets.start, end: offsets.end}),
237                             contents = node.contents();
238
239                         expect(contents.length).to.equal(3);
240                         
241                         expect(contents[0].nodeType).to.be.equal(Node.TEXT_NODE, 'first node is text node');
242                         expect(contents[0].getText()).to.equal('Alice');
243
244                         expect(contents[1].sameNode(returned)).to.be.true;
245                         expect(returned.getTagName()).to.equal('header');
246                         expect(returned.getAttr('attr1')).to.equal('value1');
247                         expect(contents[1].contents().length).to.equal(1, 'wrapper has one node inside');
248                         expect(contents[1].contents()[0].getText()).to.equal(' has a ');
249
250                         expect(contents[2].nodeType).to.be.equal(Node.TEXT_NODE, 'third node is text node');
251                         expect(contents[2].getText()).to.equal('cat');
252                     });
253                 });
254
255                 it('wraps whole text inside DocumentTextElement if offsets span entire content', function() {
256                     var node = elementNodeFromXML('<section>Alice has a cat</section>'),
257                          textNode = node.contents()[0];
258                      
259                     textNode.wrapWith({tagName: 'header', start: 0, end: 15});
260                     
261                     var contents = node.contents();
262                     expect(contents.length).to.equal(1);
263                     expect(contents[0].getTagName()).to.equal('header');
264                     expect(contents[0].contents()[0].getText()).to.equal('Alice has a cat');
265                 });
266             });
267         });
268
269     });
270
271     describe('Manipulations', function() {
272
273         it('merges adjacent text nodes resulting from detaching an element node in between', function() {
274             var doc = getDocumentFromXML('<div>Alice <span>has</span>a cat</div>'),
275                 span = doc.root.contents()[1];
276
277             span.detach();
278
279             var rootContents = doc.root.contents();
280             expect(rootContents).to.have.length(1, 'one child left');
281             expect(rootContents[0].getText()).to.equal('Alice a cat');
282         });
283
284         it('appends element node to another element node', function() {
285             var node1 = elementNodeFromParams({tag: 'div'}),
286                 node2 = elementNodeFromParams({tag: 'a'}),
287                 node3 = elementNodeFromParams({tag: 'p'});
288             node1.append(node2);
289             node1.append(node3);
290             expect(node1.contents()[0].sameNode(node2)).to.be.true;
291             expect(node1.contents()[1].sameNode(node3)).to.be.true;
292         });
293
294         it('prepends element node to another element node', function() {
295             var node1 = elementNodeFromParams({tag: 'div'}),
296                 node2 = elementNodeFromParams({tag: 'a'}),
297                 node3 = elementNodeFromParams({tag: 'p'});
298             node1.prepend(node2);
299             node1.prepend(node3);
300             expect(node1.contents()[0].sameNode(node3)).to.be.true;
301             expect(node1.contents()[1].sameNode(node2)).to.be.true;
302         });
303
304         it('wraps element node with another element node', function() {
305             var node = elementNodeFromXML('<div></div>'),
306                 wrapper = elementNodeFromXML('<wrapper></wrapper>');
307
308             node.wrapWith(wrapper);
309             expect(node.parent().sameNode(wrapper)).to.be.true;
310         });
311
312         it('unwraps element node contents', function() {
313             var node = elementNodeFromXML('<div>Alice <div>has <span>propably</span> a cat</div>!</div>'),
314                 outerDiv = node.contents()[1];
315             
316             outerDiv.unwrapContent();
317
318             expect(node.contents().length).to.equal(3);
319             expect(node.contents()[0].getText()).to.equal('Alice has ');
320             expect(node.contents()[1].getTagName()).to.equal('span');
321             expect(node.contents()[2].getText()).to.equal(' a cat!');
322         });
323
324         it('unwrap single element node from its parent', function() {
325             var doc = getDocumentFromXML('<div><a><b></b></a></div>'),
326                 div = doc.root,
327                 a = div.contents()[0],
328                 b = a.contents()[0];
329
330             var parent = b.unwrap();
331
332             expect(parent.sameNode(div)).to.equal(true, 'returns new parent');
333             expect(div.contents()).to.have.length(1, 'root contains only one node');
334             expect(div.contents()[0].sameNode(b)).to.equal(true, 'node got unwrapped');
335         });
336
337         it('unwrap single text node from its parent', function() {
338             var doc = getDocumentFromXML('<div>Some <span>text</span>!</div>'),
339                 div = doc.root,
340                 span = div.contents()[1],
341                 text = span.contents()[0];
342
343             var parent = text.unwrap();
344
345             expect(parent.sameNode(div)).to.equal(true, 'returns new parent');
346             expect(div.contents()).to.have.length(1, 'root contains only one node');
347             expect(div.contents()[0].getText()).to.equal('Some text!');
348         });
349
350         describe('Wrapping text', function() {
351             it('wraps text spanning multiple sibling TextNodes', function() {
352                 var section = elementNodeFromXML('<section>Alice has a <span>small</span> cat</section>'),
353                     wrapper = section.wrapText({
354                         _with: {tagName: 'span', attrs: {'attr1': 'value1'}},
355                         offsetStart: 6,
356                         offsetEnd: 4,
357                         textNodeIdx: [0,2]
358                     });
359
360                 expect(section.contents().length).to.equal(2);
361                 expect(section.contents()[0].nodeType).to.equal(Node.TEXT_NODE);
362                 expect(section.contents()[0].getText()).to.equal('Alice ');
363
364                 var wrapper2 = section.contents()[1];
365                 expect(wrapper2.sameNode(wrapper)).to.be.true;
366                 expect(wrapper.getTagName()).to.equal('span');
367
368                 var wrapperContents = wrapper.contents();
369                 expect(wrapperContents.length).to.equal(3);
370                 expect(wrapperContents[0].getText()).to.equal('has a ');
371
372                 expect(wrapperContents[1].nodeType).to.equal(Node.ELEMENT_NODE);
373                 expect(wrapperContents[1].contents().length).to.equal(1);
374                 expect(wrapperContents[1].contents()[0].getText()).to.equal('small');
375             });
376         });
377
378         describe('Wrapping Nodes', function() {
379             it('wraps multiple sibling nodes', function() {
380                 var section = elementNodeFromXML('<section>Alice<div>has</div><div>a cat</div></section>'),
381                     aliceText = section.contents()[0],
382                     firstDiv = section.contents()[1],
383                     lastDiv = section.contents()[section.contents().length -1];
384
385                 var returned = section.document.wrapNodes({
386                         element1: aliceText,
387                         element2: lastDiv,
388                         _with: {tagName: 'header'}
389                     });
390
391                 var sectionContents = section.contents(),
392                     header = sectionContents[0],
393                     headerContents = header.contents();
394
395                 expect(sectionContents).to.have.length(1);
396                 expect(header.sameNode(returned)).to.equal(true, 'wrapper returned');
397                 expect(header.parent().sameNode(section)).to.be.true;
398                 expect(headerContents).to.have.length(3);
399                 expect(headerContents[0].sameNode(aliceText)).to.equal(true, 'first node wrapped');
400                 expect(headerContents[1].sameNode(firstDiv)).to.equal(true, 'second node wrapped');
401                 expect(headerContents[2].sameNode(lastDiv)).to.equal(true, 'third node wrapped');
402             });
403
404             it('wraps multiple sibling Elements - middle case', function() {
405                 var section = elementNodeFromXML('<section><div></div><div></div><div></div><div></div></section>'),
406                     div2 = section.contents()[1],
407                     div3 = section.contents()[2];
408
409                 section.document.wrapNodes({
410                         element1: div2,
411                         element2: div3,
412                         _with: {tagName: 'header'}
413                     });
414
415                 var sectionContents = section.contents(),
416                     header = sectionContents[1],
417                     headerChildren = header.contents();
418
419                 expect(sectionContents).to.have.length(3);
420                 expect(headerChildren).to.have.length(2);
421                 expect(headerChildren[0].sameNode(div2)).to.equal(true, 'first node wrapped');
422                 expect(headerChildren[1].sameNode(div3)).to.equal(true, 'second node wrapped');
423             });
424         });
425
426     });
427
428     describe('Events', function() {
429         it('emits nodeDetached event on node detach', function() {
430             var node = elementNodeFromXML('<div><div></div></div>'),
431                 innerNode = node.contents()[0],
432                 spy = sinon.spy();
433             node.document.on('change', spy);
434             
435             var detached = innerNode.detach(),
436                 event = spy.args[0][0];
437
438             expect(event.type).to.equal('nodeDetached');
439             expect(event.meta.node.sameNode(detached, 'detached node in event meta'));
440             expect(event.meta.parent.sameNode(node), 'original parent node in event meta');
441         }),
442
443         it('emits nodeAdded event when appending new node', function() {
444             var node = elementNodeFromXML('<div></div>'),
445                 spy = sinon.spy();
446             node.document.on('change', spy);
447             
448             var appended = node.append({tagName:'div'}),
449                 event = spy.args[0][0];
450             expect(event.type).to.equal('nodeAdded');
451             expect(event.meta.node.sameNode(appended)).to.be.true;
452         });
453         
454         it('emits nodeMoved when appending aready existing node', function() {
455             var node = elementNodeFromXML('<div><a></a><b></b></div>'),
456                 a = node.contents()[0],
457                 b = node.contents()[1],
458                 spy = sinon.spy();
459             node.document.on('change', spy);
460             
461             var appended = a.append(b),
462                 event = spy.args[0][0];
463
464             expect(spy.callCount).to.equal(1);
465             expect(event.type).to.equal('nodeMoved');
466             expect(event.meta.node.sameNode(appended)).to.be.true;
467         });
468         
469         it('emits nodeAdded event when prepending new node', function() {
470             var node = elementNodeFromXML('<div></div>'),
471                 spy = sinon.spy();
472             node.document.on('change', spy);
473             
474             var prepended = node.prepend({tagName:'div'}),
475                 event = spy.args[0][0];
476             expect(event.type).to.equal('nodeAdded');
477             expect(event.meta.node.sameNode(prepended)).to.be.true;
478         });
479         
480         it('emits nodeMoved when prepending aready existing node', function() {
481             var node = elementNodeFromXML('<div><a></a><b></b></div>'),
482                 a = node.contents()[0],
483                 b = node.contents()[1],
484                 spy = sinon.spy();
485             node.document.on('change', spy);
486             
487             var prepended = a.prepend(b),
488                 event = spy.args[0][0];
489             expect(spy.callCount).to.equal(1);
490             expect(event.type).to.equal('nodeMoved');
491             expect(event.meta.node.sameNode(prepended)).to.be.true;
492         });
493         
494         it('emits nodeAdded event when inserting node after another', function() {
495             var node = elementNodeFromXML('<div><a></a></div>').contents()[0],
496                 spy = sinon.spy();
497             node.document.on('change', spy);
498             
499             var inserted = node.after({tagName:'div'}),
500                 event = spy.args[0][0];
501             expect(event.type).to.equal('nodeAdded');
502             expect(event.meta.node.sameNode(inserted)).to.be.true;
503         });
504         
505         it('emits nodeMoved when inserting aready existing node after another', function() {
506             var node = elementNodeFromXML('<div><a></a><b></b></div>'),
507                 a = node.contents()[0],
508                 b = node.contents()[1],
509                 spy = sinon.spy();
510             node.document.on('change', spy);
511             var inserted = b.after(a),
512                 event = spy.args[0][0];
513
514             expect(spy.callCount).to.equal(1);
515             expect(event.type).to.equal('nodeMoved');
516             expect(event.meta.node.sameNode(inserted)).to.be.true;
517         });
518
519         it('emits nodeAdded event when inserting node before another', function() {
520             var node = elementNodeFromXML('<div><a></a></div>').contents()[0],
521                 spy = sinon.spy();
522             node.document.on('change', spy);
523             
524             var inserted = node.before({tagName:'div'}),
525                 event = spy.args[0][0];
526             expect(event.type).to.equal('nodeAdded');
527             expect(event.meta.node.sameNode(inserted)).to.be.true;
528         });
529         
530         it('emits nodeAdded when inserting aready existing node before another', function() {
531             var node = elementNodeFromXML('<div><a></a><b></b></div>'),
532                 a = node.contents()[0],
533                 b = node.contents()[1],
534                 spy = sinon.spy();
535             node.document.on('change', spy);
536             var inserted = a.before(b),
537                 event = spy.args[0][0];
538
539             expect(spy.callCount).to.equal(1);
540             expect(event.type).to.equal('nodeMoved');
541             expect(event.meta.node.sameNode(inserted)).to.be.true;
542         });
543     });
544
545     describe('Traversing', function() {
546         describe('Basic', function() {
547             it('can access node parent', function() {
548                 var doc = getDocumentFromXML('<a><b></b></a>'),
549                     a = doc.root,
550                     b = a.contents()[0];
551
552                 expect(a.parent()).to.equal(null, 'parent of a root is null');
553                 expect(b.parent().sameNode(a)).to.be.true;
554             });
555             it('can access node parents', function() {
556                 var doc = getDocumentFromXML('<a><b><c></c></b></a>'),
557                     a = doc.root,
558                     b = a.contents()[0],
559                     c = b.contents()[0];
560
561                 var parents = c.parents();
562                 expect(parents).to.eql([b,a]);
563             });
564         });
565
566         describe('finding sibling parents of two elements', function() {
567             it('returns elements themself if they have direct common parent', function() {
568                 var doc = getDocumentFromXML('<section><div><div>A</div><div>B</div></div></section>'),
569                     wrappingDiv = doc.root.contents()[0],
570                     divA = wrappingDiv.contents()[0],
571                     divB = wrappingDiv.contents()[1];
572
573                 var siblingParents = doc.getSiblingParents({node1: divA, node2: divB});
574
575                 expect(siblingParents.node1.sameNode(divA)).to.equal(true, 'divA');
576                 expect(siblingParents.node2.sameNode(divB)).to.equal(true, 'divB');
577             });
578
579             it('returns sibling parents - example 1', function() {
580                 var doc = getDocumentFromXML('<section>Alice <span>has a cat</span></section>'),
581                     aliceText = doc.root.contents()[0],
582                     span = doc.root.contents()[1],
583                     spanText = span.contents()[0];
584
585                 var siblingParents = doc.getSiblingParents({node1: aliceText, node2: spanText});
586
587                 expect(siblingParents.node1.sameNode(aliceText)).to.equal(true, 'aliceText');
588                 expect(siblingParents.node2.sameNode(span)).to.equal(true, 'span');
589             });
590         });
591     });
592
593     describe('Serializing document to WLXML', function() {
594         it('keeps document intact when no changes have been made', function() {
595             var xmlIn = '<section>Alice<div>has</div>a <span class="uri" meta-uri="http://cat.com">cat</span>!</section>',
596                 doc = getDocumentFromXML(xmlIn),
597                 xmlOut = doc.toXML();
598
599             var parser = new DOMParser(),
600                 input = parser.parseFromString(xmlIn, 'application/xml').childNodes[0],
601                 output = parser.parseFromString(xmlOut, 'application/xml').childNodes[0];
602             
603             expect(input.isEqualNode(output)).to.be.true;
604         });
605
606         it('keeps entities intact', function() {
607             var xmlIn = '<section>&lt; &gt;</section>',
608                 doc = getDocumentFromXML(xmlIn),
609                 xmlOut = doc.toXML();
610             expect(xmlOut).to.equal(xmlIn);
611         });
612         it('keeps entities intact when they form html/xml', function() {
613             var xmlIn = '<section>&lt;abc&gt;</section>',
614                 doc = getDocumentFromXML(xmlIn),
615                 xmlOut = doc.toXML();
616             expect(xmlOut).to.equal(xmlIn);
617         });
618     });
619
620 });
621
622 });