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