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