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