c7cb6adaecdc56b7b435f9c018cff689415507b1
[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, Node, DOMParser */
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         it('knows its path in the document tree', function() {
80             var doc = getDocumentFromXML('<root><a><b><c></c>text</b></a></root>'),
81                 root = doc.root,
82                 a = root.contents()[0],
83                 b = a.contents()[0],
84                 text = b.contents()[1];
85
86             expect(root.getPath()).to.eql([], 'path of the root element is empty');
87             expect(a.getPath()).to.eql([0]);
88             expect(b.getPath()).to.eql([0, 0]);
89             expect(text.getPath()).to.eql([0,0,1]);
90
91             /* Paths relative to a given ancestor */
92             expect(text.getPath(root)).to.eql([0,0,1]);
93             expect(text.getPath(a)).to.eql([0,1]);
94             expect(text.getPath(b)).to.eql([1]);
95         });
96     });
97
98     describe('Basic ElementNode properties', function() {
99         it('exposes node contents', function() {
100             var node = elementNodeFromXML('<node>Some<node>text</node>is here</node>'),
101                 contents = node.contents();
102
103             expect(contents).to.have.length(3);
104             expect(contents[0].nodeType).to.equal(Node.TEXT_NODE, 'text node 1');
105             expect(contents[1].nodeType).to.equal(Node.ELEMENT_NODE, 'element node 1');
106             expect(contents[2].nodeType).to.equal(Node.TEXT_NODE, 'text node 2');
107         });
108
109         describe('Storing custom data', function() {
110             var node;
111
112             beforeEach(function() {
113                 node = elementNodeFromXML('<div></div>');
114             });
115
116             it('can append single value', function() {
117                 node.setData('key', 'value');
118                 expect(node.getData('key')).to.equal('value');
119             });
120
121             it('can overwrite the whole data', function() {
122                 node.setData('key1', 'value1');
123                 node.setData({key2: 'value2'});
124                 expect(node.getData('key2')).to.equal('value2');
125             });
126
127             it('can fetch the whole data at once', function() {
128                 node.setData({key1: 'value1', key2: 'value2'});
129                 expect(node.getData()).to.eql({key1: 'value1', key2: 'value2'});
130             });
131         });
132
133         describe('Changing node tag', function() {
134
135             it('can change tag name', function() {
136                 var node = elementNodeFromXML('<div></div>');
137                 node.setTag('span');
138                 expect(node.getTagName()).to.equal('span');
139             });
140
141             it('emits nodeTagChange event', function() {
142                 var node = elementNodeFromXML('<div></div>'),
143                     spy = sinon.spy();
144
145                 node.document.on('change', spy);
146                 node.setTag('span');
147                 var event = spy.args[0][0];
148
149                 expect(event.type).to.equal('nodeTagChange');
150                 expect(event.meta.node.sameNode(node)).to.be.true;
151                 expect(event.meta.oldTagName).to.equal('div');
152             });
153
154             describe('Implementation specific expectations', function() {
155                 // DOM specifies ElementNode tag as a read-only property, so
156                 // changing it in a seamless way is a little bit tricky. For this reason
157                 // the folowing expectations are required, despite the fact that they actually are
158                 // motivated by implemetation details.
159
160                 it('keeps node in the document', function() {
161                     var doc = getDocumentFromXML('<div><header></header></div>'),
162                         header = doc.root.contents()[0];
163                     header.setTag('span');
164                     expect(header.parent().sameNode(doc.root)).to.be.true;
165                 });
166                 it('keeps custom data', function() {
167                     var node = elementNodeFromXML('<div></div>');
168
169                     node.setData('key', 'value');
170                     node.setTag('header');
171                     
172                     expect(node.getTagName()).to.equal('header');
173                     expect(node.getData()).to.eql({key: 'value'});
174                 });
175
176                 it('can change document root tag name', function() {
177                     var doc = getDocumentFromXML('<div></div>');
178                     doc.root.setTag('span');
179                     expect(doc.root.getTagName()).to.equal('span');
180                 });
181
182                 it('keeps contents', function() {
183                     var node = elementNodeFromXML('<div><div></div></div>');
184                     node.setTag('header');
185                     expect(node.contents()).to.have.length(1);
186                 });
187             });
188
189         describe('Setting node attributes', function() {
190             it('can set node attribute', function() {
191                 var node = elementNodeFromXML('<div></div>');
192
193                 node.setAttr('key', 'value');
194                 expect(node.getAttr('key')).to.equal('value');
195             });
196             it('emits nodeAttrChange event', function() {
197                 var node = elementNodeFromXML('<div key="value1"></div>'),
198                     spy = sinon.spy();
199
200                 node.document.on('change', spy);
201                 node.setAttr('key', 'value2');
202                 var event = spy.args[0][0];
203
204                 expect(event.type).to.equal('nodeAttrChange');
205                 expect(event.meta.node.sameNode(node)).to.be.true;
206                 expect(event.meta.attr).to.equal('key');
207                 expect(event.meta.oldVal).to.equal('value1');
208             });
209         });
210
211         });
212     });
213
214     describe('Basic TextNode properties', function() {
215         it('can have its text set', function() {
216             var node = elementNodeFromXML('<div>Alice</div>'),
217                 textNode = node.contents()[0];
218
219             textNode.setText('Cat');
220             expect(textNode.getText()).to.equal('Cat');
221         });
222
223         it('emits nodeTextChange', function() {
224             var node = elementNodeFromXML('<div>Alice</div>'),
225                 textNode = node.contents()[0],
226                 spy = sinon.spy();
227
228             textNode.document.on('change', spy);
229             textNode.setText('Cat');
230
231             var event = spy.args[0][0];
232             expect(event.type).to.equal('nodeTextChange');
233         });
234
235         it('puts NodeElement after itself', function() {
236             var node = elementNodeFromXML('<div>Alice</div>'),
237                 textNode = node.contents()[0],
238                 returned = textNode.after({tagName:'div'});
239             expect(returned.sameNode(node.contents()[1])).to.be.true;
240         });
241
242         it('puts NodeElement before itself', function() {
243             var node = elementNodeFromXML('<div>Alice</div>'),
244                 textNode = node.contents()[0],
245                 returned = textNode.before({tagName:'div'});
246             expect(returned.sameNode(node.contents()[0])).to.be.true;
247         });
248
249         describe('Wrapping TextNode contents', function() {
250
251             it('wraps DocumentTextElement', function() {
252                 var node = elementNodeFromXML('<section>Alice</section>'),
253                     textNode = node.contents()[0];
254                 
255                 var returned = textNode.wrapWith({tagName: 'header'}),
256                     parent = textNode.parent(),
257                     parent2 = node.contents()[0];
258
259                 expect(returned.sameNode(parent)).to.be.equal(true, 'wrapper is a parent');
260                 expect(returned.sameNode(parent2)).to.be.equal(true, 'wrapper has a correct parent');
261                 expect(returned.getTagName()).to.equal('header');
262             });
263
264             describe('wrapping part of DocumentTextElement', function() {
265                 [{start: 5, end: 12}, {start: 12, end: 5}].forEach(function(offsets) {
266                     it('wraps in the middle ' + offsets.start + '/' + offsets.end, function() {
267                         var node = elementNodeFromXML('<section>Alice has a cat</section>'),
268                             textNode = node.contents()[0];
269                         
270                         var returned = textNode.wrapWith({tagName: 'header', attrs: {'attr1': 'value1'}, start: offsets.start, end: offsets.end}),
271                             contents = node.contents();
272
273                         expect(contents.length).to.equal(3);
274                         
275                         expect(contents[0].nodeType).to.be.equal(Node.TEXT_NODE, 'first node is text node');
276                         expect(contents[0].getText()).to.equal('Alice');
277
278                         expect(contents[1].sameNode(returned)).to.be.true;
279                         expect(returned.getTagName()).to.equal('header');
280                         expect(returned.getAttr('attr1')).to.equal('value1');
281                         expect(contents[1].contents().length).to.equal(1, 'wrapper has one node inside');
282                         expect(contents[1].contents()[0].getText()).to.equal(' has a ');
283
284                         expect(contents[2].nodeType).to.be.equal(Node.TEXT_NODE, 'third node is text node');
285                         expect(contents[2].getText()).to.equal('cat');
286                     });
287                 });
288
289                 it('wraps whole text inside DocumentTextElement if offsets span entire content', function() {
290                     var node = elementNodeFromXML('<section>Alice has a cat</section>'),
291                          textNode = node.contents()[0];
292                      
293                     textNode.wrapWith({tagName: 'header', start: 0, end: 15});
294                     
295                     var contents = node.contents();
296                     expect(contents.length).to.equal(1);
297                     expect(contents[0].getTagName()).to.equal('header');
298                     expect(contents[0].contents()[0].getText()).to.equal('Alice has a cat');
299                 });
300             });
301         });
302
303     });
304
305     describe('Manipulations', function() {
306
307         describe('replacing node with another one', function() {
308             it('replaces node with another one', function() {
309                 var doc = getDocumentFromXML('<div><a></a></div>'),
310                     a = doc.root.contents()[0];
311
312                 var c = a.replaceWith({tagName: 'b', attrs: {b:'1'}});
313
314                 expect(doc.root.contents()[0].sameNode(c));
315                 expect(c.getTagName()).to.equal('b');
316                 expect(c.getAttr('b')).to.equal('1');
317             });
318             it('can replace document root', function() {
319                 var doc = getDocumentFromXML('<div></div>');
320
321                 var header = doc.root.replaceWith({tagName: 'header'});
322
323                 expect(doc.root.sameNode(header)).to.be.true;
324                 expect(doc.containsNode(header)).to.be.true;
325             });
326         });
327
328         it('merges adjacent text nodes resulting from detaching an element node in between', function() {
329             var doc = getDocumentFromXML('<div>Alice <span>has</span>a cat</div>'),
330                 span = doc.root.contents()[1];
331
332             span.detach();
333
334             var rootContents = doc.root.contents();
335             expect(rootContents).to.have.length(1, 'one child left');
336             expect(rootContents[0].getText()).to.equal('Alice a cat');
337         });
338
339         it('inserts node at index', function() {
340             var doc = getDocumentFromXML('<div><a></a><b></b><c></c></div>'),
341                 b = doc.root.contents()[1];
342
343             var inserted = doc.root.insertAtIndex({tagName: 'test'}, 1);
344
345             expect(doc.root.contents()[1].sameNode(inserted)).to.equal(true, 'inserted node returned');
346             expect(b.getIndex()).to.equal(2, 'b node shifted right');
347         });
348
349         it('appends node when inserting node at index out of range', function() {
350             var doc = getDocumentFromXML('<div></div>');
351
352             var test1 = doc.root.insertAtIndex({tagName: 'test1'}, 0),
353                 test2 = doc.root.insertAtIndex({tagName: 'test1'}, 10);
354
355             expect(doc.root.contents()[0].sameNode(test1)).to.equal(true, 'inserting at index 0 of empty nodes appends node');
356             expect(doc.root.contents().length).to.equal(1, 'inserting at index out of range does nothing');
357             expect(test2).to.equal(undefined, 'inserting at index out of range returns undefined');
358         });
359
360         it('appends element node to another element node', function() {
361             var node1 = elementNodeFromParams({tag: 'div'}),
362                 node2 = elementNodeFromParams({tag: 'a'}),
363                 node3 = elementNodeFromParams({tag: 'p'});
364             node1.append(node2);
365             node1.append(node3);
366             expect(node1.contents()[0].sameNode(node2)).to.be.true;
367             expect(node1.contents()[1].sameNode(node3)).to.be.true;
368         });
369
370         it('prepends element node to another element node', function() {
371             var node1 = elementNodeFromParams({tag: 'div'}),
372                 node2 = elementNodeFromParams({tag: 'a'}),
373                 node3 = elementNodeFromParams({tag: 'p'});
374             node1.prepend(node2);
375             node1.prepend(node3);
376             expect(node1.contents()[0].sameNode(node3)).to.be.true;
377             expect(node1.contents()[1].sameNode(node2)).to.be.true;
378         });
379
380         it('wraps element node with another element node', function() {
381             var node = elementNodeFromXML('<div></div>'),
382                 wrapper = elementNodeFromXML('<wrapper></wrapper>');
383
384             node.wrapWith(wrapper);
385             expect(node.parent().sameNode(wrapper)).to.be.true;
386         });
387
388         it('unwraps element node contents', function() {
389             var node = elementNodeFromXML('<div>Alice <div>has <span>propably</span> a cat</div>!</div>'),
390                 outerDiv = node.contents()[1];
391             
392             outerDiv.unwrapContent();
393
394             expect(node.contents().length).to.equal(3);
395             expect(node.contents()[0].getText()).to.equal('Alice has ');
396             expect(node.contents()[1].getTagName()).to.equal('span');
397             expect(node.contents()[2].getText()).to.equal(' a cat!');
398         });
399
400         it('unwrap single element node from its parent', function() {
401             var doc = getDocumentFromXML('<div><a><b></b></a></div>'),
402                 div = doc.root,
403                 a = div.contents()[0],
404                 b = a.contents()[0];
405
406             var parent = b.unwrap();
407
408             expect(parent.sameNode(div)).to.equal(true, 'returns new parent');
409             expect(div.contents()).to.have.length(1, 'root contains only one node');
410             expect(div.contents()[0].sameNode(b)).to.equal(true, 'node got unwrapped');
411         });
412
413         it('unwrap single text node from its parent', function() {
414             var doc = getDocumentFromXML('<div>Some <span>text</span>!</div>'),
415                 div = doc.root,
416                 span = div.contents()[1],
417                 text = span.contents()[0];
418
419             var parent = text.unwrap();
420
421             expect(parent.sameNode(div)).to.equal(true, 'returns new parent');
422             expect(div.contents()).to.have.length(1, 'root contains only one node');
423             expect(div.contents()[0].getText()).to.equal('Some text!');
424         });
425
426         describe('Wrapping text', function() {
427             it('wraps text spanning multiple sibling TextNodes', function() {
428                 var section = elementNodeFromXML('<section>Alice has a <span>small</span> cat</section>'),
429                     wrapper = section.wrapText({
430                         _with: {tagName: 'span', attrs: {'attr1': 'value1'}},
431                         offsetStart: 6,
432                         offsetEnd: 4,
433                         textNodeIdx: [0,2]
434                     });
435
436                 expect(section.contents().length).to.equal(2);
437                 expect(section.contents()[0].nodeType).to.equal(Node.TEXT_NODE);
438                 expect(section.contents()[0].getText()).to.equal('Alice ');
439
440                 var wrapper2 = section.contents()[1];
441                 expect(wrapper2.sameNode(wrapper)).to.be.true;
442                 expect(wrapper.getTagName()).to.equal('span');
443
444                 var wrapperContents = wrapper.contents();
445                 expect(wrapperContents.length).to.equal(3);
446                 expect(wrapperContents[0].getText()).to.equal('has a ');
447
448                 expect(wrapperContents[1].nodeType).to.equal(Node.ELEMENT_NODE);
449                 expect(wrapperContents[1].contents().length).to.equal(1);
450                 expect(wrapperContents[1].contents()[0].getText()).to.equal('small');
451             });
452         });
453
454         describe('Wrapping Nodes', function() {
455             it('wraps multiple sibling nodes', function() {
456                 var section = elementNodeFromXML('<section>Alice<div>has</div><div>a cat</div></section>'),
457                     aliceText = section.contents()[0],
458                     firstDiv = section.contents()[1],
459                     lastDiv = section.contents()[section.contents().length -1];
460
461                 var returned = section.document.wrapNodes({
462                         node1: aliceText,
463                         node2: lastDiv,
464                         _with: {tagName: 'header'}
465                     });
466
467                 var sectionContentss = section.contents(),
468                     header = sectionContentss[0],
469                     headerContents = header.contents();
470
471                 expect(sectionContentss).to.have.length(1);
472                 expect(header.sameNode(returned)).to.equal(true, 'wrapper returned');
473                 expect(header.parent().sameNode(section)).to.be.true;
474                 expect(headerContents).to.have.length(3);
475                 expect(headerContents[0].sameNode(aliceText)).to.equal(true, 'first node wrapped');
476                 expect(headerContents[1].sameNode(firstDiv)).to.equal(true, 'second node wrapped');
477                 expect(headerContents[2].sameNode(lastDiv)).to.equal(true, 'third node wrapped');
478             });
479
480             it('wraps multiple sibling Elements - middle case', function() {
481                 var section = elementNodeFromXML('<section><div></div><div></div><div></div><div></div></section>'),
482                     div2 = section.contents()[1],
483                     div3 = section.contents()[2];
484
485                 section.document.wrapNodes({
486                         node1: div2,
487                         node2: div3,
488                         _with: {tagName: 'header'}
489                     });
490
491                 var sectionContentss = section.contents(),
492                     header = sectionContentss[1],
493                     headerChildren = header.contents();
494
495                 expect(sectionContentss).to.have.length(3);
496                 expect(headerChildren).to.have.length(2);
497                 expect(headerChildren[0].sameNode(div2)).to.equal(true, 'first node wrapped');
498                 expect(headerChildren[1].sameNode(div3)).to.equal(true, 'second node wrapped');
499             });
500         });
501
502     });
503
504     describe('Splitting text', function() {
505     
506         it('splits TextNode\'s parent into two ElementNodes', function() {
507             var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
508                 section = doc.root,
509                 text = section.contents()[0].contents()[0];
510
511             var returnedValue = text.split({offset: 5});
512             expect(section.contents().length).to.equal(2, 'section has two children');
513             
514             var header1 = section.contents()[0];
515             var header2 = section.contents()[1];
516
517             expect(header1.getTagName()).to.equal('header', 'first section child ok');
518             expect(header1.contents().length).to.equal(1, 'first header has one child');
519             expect(header1.contents()[0].getText()).to.equal('Some ', 'first header has correct content');
520             expect(header2.getTagName()).to.equal('header', 'second section child ok');
521             expect(header2.contents().length).to.equal(1, 'second header has one child');
522             expect(header2.contents()[0].getText()).to.equal('header', 'second header has correct content');
523
524             expect(returnedValue.first.sameNode(header1)).to.equal(true, 'first node returned');
525             expect(returnedValue.second.sameNode(header2)).to.equal(true, 'second node returned');
526         });
527
528         it('leaves empty copy of ElementNode if splitting at the very beginning', function() {
529                 var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
530                 section = doc.root,
531                 text = section.contents()[0].contents()[0];
532
533                 text.split({offset: 0});
534                 
535                 var header1 = section.contents()[0];
536                 var header2 = section.contents()[1];
537
538                 expect(header1.contents().length).to.equal(0);
539                 expect(header2.contents()[0].getText()).to.equal('Some header');
540         });
541
542         it('leaves empty copy of ElementNode if splitting at the very end', function() {
543                 var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
544                 section = doc.root,
545                 text = section.contents()[0].contents()[0];
546
547                 text.split({offset: 11});
548                 
549                 var header1 = section.contents()[0];
550                 var header2 = section.contents()[1];
551
552                 expect(header1.contents()[0].getText()).to.equal('Some header');
553                 expect(header2.contents().length).to.equal(0);
554         });
555
556         it('keeps TextNodes\'s parent\'s children elements intact', function() {
557             var doc = getDocumentFromXML('<section><header>A <span>fancy</span> and <span>nice</span> header</header></section>'),
558                 section = doc.root,
559                 header = section.contents()[0],
560                 textAnd = header.contents()[2];
561
562             textAnd.split({offset: 2});
563             
564             var sectionContents = section.contents();
565             expect(sectionContents.length).to.equal(2, 'Section has two children');
566             expect(sectionContents[0].getTagName()).to.equal('header', 'First section node is a header');
567             expect(sectionContents[1].getTagName()).to.equal('header', 'Second section node is a header');
568
569             var firstHeaderContents = sectionContents[0].contents();
570             expect(firstHeaderContents.length).to.equal(3, 'First header has three children');
571             expect(firstHeaderContents[0].getText()).to.equal('A ', 'First header starts with a text');
572             expect(firstHeaderContents[1].getTagName()).to.equal('span', 'First header has span in the middle');
573             expect(firstHeaderContents[2].getText()).to.equal(' a', 'First header ends with text');
574
575             var secondHeaderContents = sectionContents[1].contents();
576             expect(secondHeaderContents.length).to.equal(3, 'Second header has three children');
577             expect(secondHeaderContents[0].getText()).to.equal('nd ', 'Second header starts with text');
578             expect(secondHeaderContents[1].getTagName()).to.equal('span', 'Second header has span in the middle');
579             expect(secondHeaderContents[2].getText()).to.equal(' header', 'Second header ends with text');
580         });
581     });
582
583     describe('Events', function() {
584         it('emits nodeDetached event on node detach', function() {
585             var node = elementNodeFromXML('<div><div></div></div>'),
586                 innerNode = node.contents()[0],
587                 spy = sinon.spy();
588             node.document.on('change', spy);
589             
590             var detached = innerNode.detach(),
591                 event = spy.args[0][0];
592
593             expect(event.type).to.equal('nodeDetached');
594             expect(event.meta.node.sameNode(detached, 'detached node in event meta'));
595             expect(event.meta.parent.sameNode(node), 'original parent node in event meta');
596         }),
597
598         it('emits nodeAdded event when appending new node', function() {
599             var node = elementNodeFromXML('<div></div>'),
600                 spy = sinon.spy();
601             node.document.on('change', spy);
602             
603             var appended = node.append({tagName:'div'}),
604                 event = spy.args[0][0];
605             expect(event.type).to.equal('nodeAdded');
606             expect(event.meta.node.sameNode(appended)).to.be.true;
607         });
608         
609         it('emits nodeMoved when appending aready existing node', 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             
616             var appended = a.append(b),
617                 event = spy.args[0][0];
618
619             expect(spy.callCount).to.equal(1);
620             expect(event.type).to.equal('nodeMoved');
621             expect(event.meta.node.sameNode(appended)).to.be.true;
622         });
623         
624         it('emits nodeAdded event when prepending new node', function() {
625             var node = elementNodeFromXML('<div></div>'),
626                 spy = sinon.spy();
627             node.document.on('change', spy);
628             
629             var prepended = node.prepend({tagName:'div'}),
630                 event = spy.args[0][0];
631             expect(event.type).to.equal('nodeAdded');
632             expect(event.meta.node.sameNode(prepended)).to.be.true;
633         });
634         
635         it('emits nodeMoved when prepending aready existing node', function() {
636             var node = elementNodeFromXML('<div><a></a><b></b></div>'),
637                 a = node.contents()[0],
638                 b = node.contents()[1],
639                 spy = sinon.spy();
640             node.document.on('change', spy);
641             
642             var prepended = a.prepend(b),
643                 event = spy.args[0][0];
644             expect(spy.callCount).to.equal(1);
645             expect(event.type).to.equal('nodeMoved');
646             expect(event.meta.node.sameNode(prepended)).to.be.true;
647         });
648         
649         it('emits nodeAdded event when inserting node after another', function() {
650             var node = elementNodeFromXML('<div><a></a></div>').contents()[0],
651                 spy = sinon.spy();
652             node.document.on('change', spy);
653             
654             var inserted = node.after({tagName:'div'}),
655                 event = spy.args[0][0];
656             expect(event.type).to.equal('nodeAdded');
657             expect(event.meta.node.sameNode(inserted)).to.be.true;
658         });
659         
660         it('emits nodeMoved when inserting aready existing node after another', function() {
661             var node = elementNodeFromXML('<div><a></a><b></b></div>'),
662                 a = node.contents()[0],
663                 b = node.contents()[1],
664                 spy = sinon.spy();
665             node.document.on('change', spy);
666             var inserted = b.after(a),
667                 event = spy.args[0][0];
668
669             expect(spy.callCount).to.equal(1);
670             expect(event.type).to.equal('nodeMoved');
671             expect(event.meta.node.sameNode(inserted)).to.be.true;
672         });
673
674         it('emits nodeAdded event when inserting node before another', function() {
675             var node = elementNodeFromXML('<div><a></a></div>').contents()[0],
676                 spy = sinon.spy();
677             node.document.on('change', spy);
678             
679             var inserted = node.before({tagName:'div'}),
680                 event = spy.args[0][0];
681             expect(event.type).to.equal('nodeAdded');
682             expect(event.meta.node.sameNode(inserted)).to.be.true;
683         });
684         
685         it('emits nodeAdded when inserting aready existing node before another', function() {
686             var node = elementNodeFromXML('<div><a></a><b></b></div>'),
687                 a = node.contents()[0],
688                 b = node.contents()[1],
689                 spy = sinon.spy();
690             node.document.on('change', spy);
691             var inserted = a.before(b),
692                 event = spy.args[0][0];
693
694             expect(spy.callCount).to.equal(1);
695             expect(event.type).to.equal('nodeMoved');
696             expect(event.meta.node.sameNode(inserted)).to.be.true;
697         });
698
699         it('emits nodeDetached and nodeAdded when replacing root node with another', function() {
700             var doc = getDocumentFromXML('<a></a>'),
701                 oldRoot = doc.root,
702                 spy = sinon.spy();
703
704             doc.on('change', spy);
705
706             doc.root.replaceWith({tagName: 'b'});
707
708             expect(spy.callCount).to.equal(2);
709
710             var event1 = spy.args[0][0],
711                 event2 = spy.args[1][0];
712
713             expect(event1.type).to.equal('nodeDetached');
714             expect(event1.meta.node.sameNode(oldRoot)).to.equal(true, 'root node in nodeDetached event metadata');
715             expect(event2.type).to.equal('nodeAdded');
716             expect(event2.meta.node.sameNode(doc.root)).to.equal(true, 'new root node in nodelAdded event meta');
717         });
718
719
720         ['append', 'prepend', 'before', 'after'].forEach(function(insertionMethod) {
721             it('emits nodeDetached for node moved from a document tree to out of document node ' + insertionMethod, function() {
722                 var doc = getDocumentFromXML('<div><a></a></div>'),
723                     a = doc.root.contents()[0],
724                     spy = sinon.spy();
725
726                 doc.on('change', spy);
727
728                 var newNode = doc.createDocumentNode({tagName: 'b'}),
729                     newNodeInner = newNode.append({tagName:'c'});
730
731                 newNodeInner[insertionMethod](a);
732
733                 var event = spy.args[0][0];
734                 expect(event.type).to.equal('nodeDetached');
735                 expect(event.meta.node.sameNode(a));
736             });
737
738             it('doesn\'t emit nodeDetached event for already out of document moved to out of document node: ' + insertionMethod, function() {
739                 var doc = getDocumentFromXML('<div><a></a></div>'),
740                     a = doc.root.contents()[0],
741                     spy = sinon.spy();
742
743                 doc.on('change', spy);
744
745                 var newNode = doc.createDocumentNode({tagName: 'b'});
746                     var newNodeInner = newNode.append({tagName:'c'});
747
748                 expect(spy.callCount).to.equal(0);
749             });
750         });
751
752
753     });
754
755     describe('Traversing', function() {
756         describe('Basic', function() {
757             it('can access node parent', function() {
758                 var doc = getDocumentFromXML('<a><b></b></a>'),
759                     a = doc.root,
760                     b = a.contents()[0];
761
762                 expect(a.parent()).to.equal(null, 'parent of a root is null');
763                 expect(b.parent().sameNode(a)).to.be.true;
764             });
765             it('can access node parents', function() {
766                 var doc = getDocumentFromXML('<a><b><c></c></b></a>'),
767                     a = doc.root,
768                     b = a.contents()[0],
769                     c = b.contents()[0];
770
771                 var parents = c.parents();
772                 expect(parents).to.eql([b,a]);
773             });
774         });
775
776         describe('finding sibling parents of two elements', function() {
777             it('returns elements themself if they have direct common parent', function() {
778                 var doc = getDocumentFromXML('<section><div><div>A</div><div>B</div></div></section>'),
779                     wrappingDiv = doc.root.contents()[0],
780                     divA = wrappingDiv.contents()[0],
781                     divB = wrappingDiv.contents()[1];
782
783                 var siblingParents = doc.getSiblingParents({node1: divA, node2: divB});
784
785                 expect(siblingParents.node1.sameNode(divA)).to.equal(true, 'divA');
786                 expect(siblingParents.node2.sameNode(divB)).to.equal(true, 'divB');
787             });
788
789             it('returns sibling parents - example 1', function() {
790                 var doc = getDocumentFromXML('<section>Alice <span>has a cat</span></section>'),
791                     aliceText = doc.root.contents()[0],
792                     span = doc.root.contents()[1],
793                     spanText = span.contents()[0];
794
795                 var siblingParents = doc.getSiblingParents({node1: aliceText, node2: spanText});
796
797                 expect(siblingParents.node1.sameNode(aliceText)).to.equal(true, 'aliceText');
798                 expect(siblingParents.node2.sameNode(span)).to.equal(true, 'span');
799             });
800         });
801     });
802
803     describe('Serializing document to WLXML', function() {
804         it('keeps document intact when no changes have been made', function() {
805             var xmlIn = '<section>Alice<div>has</div>a <span class="uri" meta-uri="http://cat.com">cat</span>!</section>',
806                 doc = getDocumentFromXML(xmlIn),
807                 xmlOut = doc.toXML();
808
809             var parser = new DOMParser(),
810                 input = parser.parseFromString(xmlIn, 'application/xml').childNodes[0],
811                 output = parser.parseFromString(xmlOut, 'application/xml').childNodes[0];
812             
813             expect(input.isEqualNode(output)).to.be.true;
814         });
815
816         it('keeps entities intact', function() {
817             var xmlIn = '<section>&lt; &gt;</section>',
818                 doc = getDocumentFromXML(xmlIn),
819                 xmlOut = doc.toXML();
820             expect(xmlOut).to.equal(xmlIn);
821         });
822         it('keeps entities intact when they form html/xml', function() {
823             var xmlIn = '<section>&lt;abc&gt;</section>',
824                 doc = getDocumentFromXML(xmlIn),
825                 xmlOut = doc.toXML();
826             expect(xmlOut).to.equal(xmlIn);
827         });
828     });
829
830     describe('Undo/redo', function() {
831
832         it('does work', function() {
833             var doc = getDocumentFromXML('<section><span>Alice</span></section>'),
834                 span = doc.root.contents()[0];
835
836             span.transform('smartxml.detach');
837
838
839             doc.undo();
840
841             expect(doc.root.contents()).to.have.length(1);
842             expect(doc.root.contents()[0].getTagName()).to.equal('span');
843             expect(doc.root.contents()[0].contents()[0].getText()).to.equal('Alice');
844
845             doc.redo();
846             expect(doc.root.contents()).to.have.length(0);
847
848             doc.undo();
849             expect(doc.root.contents()).to.have.length(1);
850             expect(doc.root.contents()[0].getTagName()).to.equal('span');
851             expect(doc.root.contents()[0].contents()[0].getText()).to.equal('Alice');
852
853         });
854         it('does work - merged text nodes case', function() {
855             var doc = getDocumentFromXML('<section>Alice <span>has</span> a cat.</section>'),
856                 span = doc.root.contents()[1];
857
858             span.transform('smartxml.detach');
859
860
861             doc.undo();
862
863             expect(doc.root.contents().length).to.equal(3);
864             //console.log(doc.toXML());
865             expect(doc.root.contents()[1].contents()[0].getText()).to.equal('has');
866
867         });
868         it('dbg - don not store nodes in tranformation state!', function() {
869             var doc = getDocumentFromXML('<section><a></a><b></b></section>'),
870                 a = doc.root.contents()[0],
871                 b = doc.root.contents()[1];
872
873             a.transform('smartxml.detach');
874             b.transform('smartxml.detach');
875             doc.undo();
876             doc.undo();
877             expect(doc.root.contents().length).to.equal(2);
878             expect(doc.root.contents()[0].getTagName()).to.equal('a');
879             expect(doc.root.contents()[1].getTagName()).to.equal('b');
880
881             doc.redo();
882             doc.redo();
883             expect(doc.root.contents().length).to.equal(0);
884
885         });
886     });
887
888 });
889
890 });