smartxml: splitting text
[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 sectionContentss = section.contents(),
392                     header = sectionContentss[0],
393                     headerContents = header.contents();
394
395                 expect(sectionContentss).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 sectionContentss = section.contents(),
416                     header = sectionContentss[1],
417                     headerChildren = header.contents();
418
419                 expect(sectionContentss).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('Splitting text', function() {
429     
430         it('splits TextNode\'s parent into two ElementNodes', function() {
431             var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
432                 section = doc.root,
433                 text = section.contents()[0].contents()[0];
434
435             var returnedValue = text.split({offset: 5});
436             expect(section.contents().length).to.equal(2, 'section has two children');
437             
438             var header1 = section.contents()[0];
439             var header2 = section.contents()[1];
440
441             expect(header1.getTagName()).to.equal('header', 'first section child ok');
442             expect(header1.contents().length).to.equal(1, 'first header has one child');
443             expect(header1.contents()[0].getText()).to.equal('Some ', 'first header has correct content');
444             expect(header2.getTagName()).to.equal('header', 'second section child ok');
445             expect(header2.contents().length).to.equal(1, 'second header has one child');
446             expect(header2.contents()[0].getText()).to.equal('header', 'second header has correct content');
447
448             expect(returnedValue.first.sameNode(header1)).to.equal(true, 'first node returned');
449             expect(returnedValue.second.sameNode(header2)).to.equal(true, 'second node returned');
450         });
451
452         it('leaves empty copy of ElementNode if splitting at the very beginning', function() {
453                 var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
454                 section = doc.root,
455                 text = section.contents()[0].contents()[0];
456
457                 text.split({offset: 0});
458                 
459                 var header1 = section.contents()[0];
460                 var header2 = section.contents()[1];
461
462                 expect(header1.contents().length).to.equal(0);
463                 expect(header2.contents()[0].getText()).to.equal('Some header');
464         });
465
466         it('leaves empty copy of ElementNode if splitting at the very end', function() {
467                 var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
468                 section = doc.root,
469                 text = section.contents()[0].contents()[0];
470
471                 text.split({offset: 11});
472                 
473                 var header1 = section.contents()[0];
474                 var header2 = section.contents()[1];
475
476                 expect(header1.contents()[0].getText()).to.equal('Some header');
477                 expect(header2.contents().length).to.equal(0);
478         });
479
480         it('keeps TextNodes\'s parent\'s children elements intact', function() {
481             var doc = getDocumentFromXML('<section><header>A <span>fancy</span> and <span>nice</span> header</header></section>'),
482                 section = doc.root,
483                 header = section.contents()[0],
484                 textAnd = header.contents()[2];
485
486             textAnd.split({offset: 2});
487             
488             var sectionContents = section.contents();
489             expect(sectionContents.length).to.equal(2, 'Section has two children');
490             expect(sectionContents[0].getTagName()).to.equal('header', 'First section node is a header');
491             expect(sectionContents[1].getTagName()).to.equal('header', 'Second section node is a header');
492
493             var firstHeaderContents = sectionContents[0].contents();
494             expect(firstHeaderContents.length).to.equal(3, 'First header has three children');
495             expect(firstHeaderContents[0].getText()).to.equal('A ', 'First header starts with a text');
496             expect(firstHeaderContents[1].getTagName()).to.equal('span', 'First header has span in the middle');
497             expect(firstHeaderContents[2].getText()).to.equal(' a', 'First header ends with text');
498
499             var secondHeaderContents = sectionContents[1].contents();
500             expect(secondHeaderContents.length).to.equal(3, 'Second header has three children');
501             expect(secondHeaderContents[0].getText()).to.equal('nd ', 'Second header starts with text');
502             expect(secondHeaderContents[1].getTagName()).to.equal('span', 'Second header has span in the middle');
503             expect(secondHeaderContents[2].getText()).to.equal(' header', 'Second header ends with text');
504         });
505     });
506
507     describe('Events', function() {
508         it('emits nodeDetached event on node detach', function() {
509             var node = elementNodeFromXML('<div><div></div></div>'),
510                 innerNode = node.contents()[0],
511                 spy = sinon.spy();
512             node.document.on('change', spy);
513             
514             var detached = innerNode.detach(),
515                 event = spy.args[0][0];
516
517             expect(event.type).to.equal('nodeDetached');
518             expect(event.meta.node.sameNode(detached, 'detached node in event meta'));
519             expect(event.meta.parent.sameNode(node), 'original parent node in event meta');
520         }),
521
522         it('emits nodeAdded event when appending new node', function() {
523             var node = elementNodeFromXML('<div></div>'),
524                 spy = sinon.spy();
525             node.document.on('change', spy);
526             
527             var appended = node.append({tagName:'div'}),
528                 event = spy.args[0][0];
529             expect(event.type).to.equal('nodeAdded');
530             expect(event.meta.node.sameNode(appended)).to.be.true;
531         });
532         
533         it('emits nodeMoved when appending aready existing node', function() {
534             var node = elementNodeFromXML('<div><a></a><b></b></div>'),
535                 a = node.contents()[0],
536                 b = node.contents()[1],
537                 spy = sinon.spy();
538             node.document.on('change', spy);
539             
540             var appended = a.append(b),
541                 event = spy.args[0][0];
542
543             expect(spy.callCount).to.equal(1);
544             expect(event.type).to.equal('nodeMoved');
545             expect(event.meta.node.sameNode(appended)).to.be.true;
546         });
547         
548         it('emits nodeAdded event when prepending new node', function() {
549             var node = elementNodeFromXML('<div></div>'),
550                 spy = sinon.spy();
551             node.document.on('change', spy);
552             
553             var prepended = node.prepend({tagName:'div'}),
554                 event = spy.args[0][0];
555             expect(event.type).to.equal('nodeAdded');
556             expect(event.meta.node.sameNode(prepended)).to.be.true;
557         });
558         
559         it('emits nodeMoved when prepending aready existing node', function() {
560             var node = elementNodeFromXML('<div><a></a><b></b></div>'),
561                 a = node.contents()[0],
562                 b = node.contents()[1],
563                 spy = sinon.spy();
564             node.document.on('change', spy);
565             
566             var prepended = a.prepend(b),
567                 event = spy.args[0][0];
568             expect(spy.callCount).to.equal(1);
569             expect(event.type).to.equal('nodeMoved');
570             expect(event.meta.node.sameNode(prepended)).to.be.true;
571         });
572         
573         it('emits nodeAdded event when inserting node after another', function() {
574             var node = elementNodeFromXML('<div><a></a></div>').contents()[0],
575                 spy = sinon.spy();
576             node.document.on('change', spy);
577             
578             var inserted = node.after({tagName:'div'}),
579                 event = spy.args[0][0];
580             expect(event.type).to.equal('nodeAdded');
581             expect(event.meta.node.sameNode(inserted)).to.be.true;
582         });
583         
584         it('emits nodeMoved when inserting aready existing node after another', function() {
585             var node = elementNodeFromXML('<div><a></a><b></b></div>'),
586                 a = node.contents()[0],
587                 b = node.contents()[1],
588                 spy = sinon.spy();
589             node.document.on('change', spy);
590             var inserted = b.after(a),
591                 event = spy.args[0][0];
592
593             expect(spy.callCount).to.equal(1);
594             expect(event.type).to.equal('nodeMoved');
595             expect(event.meta.node.sameNode(inserted)).to.be.true;
596         });
597
598         it('emits nodeAdded event when inserting node before another', function() {
599             var node = elementNodeFromXML('<div><a></a></div>').contents()[0],
600                 spy = sinon.spy();
601             node.document.on('change', spy);
602             
603             var inserted = node.before({tagName:'div'}),
604                 event = spy.args[0][0];
605             expect(event.type).to.equal('nodeAdded');
606             expect(event.meta.node.sameNode(inserted)).to.be.true;
607         });
608         
609         it('emits nodeAdded when inserting aready existing node before another', function() {
610             var node = elementNodeFromXML('<div><a></a><b></b></div>'),
611                 a = node.contents()[0],
612                 b = node.contents()[1],
613                 spy = sinon.spy();
614             node.document.on('change', spy);
615             var inserted = a.before(b),
616                 event = spy.args[0][0];
617
618             expect(spy.callCount).to.equal(1);
619             expect(event.type).to.equal('nodeMoved');
620             expect(event.meta.node.sameNode(inserted)).to.be.true;
621         });
622     });
623
624     describe('Traversing', function() {
625         describe('Basic', function() {
626             it('can access node parent', function() {
627                 var doc = getDocumentFromXML('<a><b></b></a>'),
628                     a = doc.root,
629                     b = a.contents()[0];
630
631                 expect(a.parent()).to.equal(null, 'parent of a root is null');
632                 expect(b.parent().sameNode(a)).to.be.true;
633             });
634             it('can access node parents', function() {
635                 var doc = getDocumentFromXML('<a><b><c></c></b></a>'),
636                     a = doc.root,
637                     b = a.contents()[0],
638                     c = b.contents()[0];
639
640                 var parents = c.parents();
641                 expect(parents).to.eql([b,a]);
642             });
643         });
644
645         describe('finding sibling parents of two elements', function() {
646             it('returns elements themself if they have direct common parent', function() {
647                 var doc = getDocumentFromXML('<section><div><div>A</div><div>B</div></div></section>'),
648                     wrappingDiv = doc.root.contents()[0],
649                     divA = wrappingDiv.contents()[0],
650                     divB = wrappingDiv.contents()[1];
651
652                 var siblingParents = doc.getSiblingParents({node1: divA, node2: divB});
653
654                 expect(siblingParents.node1.sameNode(divA)).to.equal(true, 'divA');
655                 expect(siblingParents.node2.sameNode(divB)).to.equal(true, 'divB');
656             });
657
658             it('returns sibling parents - example 1', function() {
659                 var doc = getDocumentFromXML('<section>Alice <span>has a cat</span></section>'),
660                     aliceText = doc.root.contents()[0],
661                     span = doc.root.contents()[1],
662                     spanText = span.contents()[0];
663
664                 var siblingParents = doc.getSiblingParents({node1: aliceText, node2: spanText});
665
666                 expect(siblingParents.node1.sameNode(aliceText)).to.equal(true, 'aliceText');
667                 expect(siblingParents.node2.sameNode(span)).to.equal(true, 'span');
668             });
669         });
670     });
671
672     describe('Serializing document to WLXML', function() {
673         it('keeps document intact when no changes have been made', function() {
674             var xmlIn = '<section>Alice<div>has</div>a <span class="uri" meta-uri="http://cat.com">cat</span>!</section>',
675                 doc = getDocumentFromXML(xmlIn),
676                 xmlOut = doc.toXML();
677
678             var parser = new DOMParser(),
679                 input = parser.parseFromString(xmlIn, 'application/xml').childNodes[0],
680                 output = parser.parseFromString(xmlOut, 'application/xml').childNodes[0];
681             
682             expect(input.isEqualNode(output)).to.be.true;
683         });
684
685         it('keeps entities intact', function() {
686             var xmlIn = '<section>&lt; &gt;</section>',
687                 doc = getDocumentFromXML(xmlIn),
688                 xmlOut = doc.toXML();
689             expect(xmlOut).to.equal(xmlIn);
690         });
691         it('keeps entities intact when they form html/xml', function() {
692             var xmlIn = '<section>&lt;abc&gt;</section>',
693                 doc = getDocumentFromXML(xmlIn),
694                 xmlOut = doc.toXML();
695             expect(xmlOut).to.equal(xmlIn);
696         });
697     });
698
699 });
700
701 });