smartxml: fix in DocumentNode.getParentSiblings
[fnpeditor.git] / src / smartxml / smartxml.test.js
1 define([
2     'libs/chai',
3     'libs/sinon',
4     'libs/underscore',
5     './smartxml.js'
6 ], function(chai, sinon, _, smartxml) {
7     
8 'use strict';
9 /*jshint expr:true */
10 /* global describe, it, beforeEach, Node, DOMParser */
11
12 var expect = chai.expect;
13
14
15 var getDocumentFromXML = function(xml) {
16     return smartxml.documentFromXML(xml);
17 };
18
19 var elementNodeFromParams = function(params) {
20     return smartxml.elementNodeFromXML('<' + params.tag + '></' + params.tag + '>');
21 };
22
23 var elementNodeFromXML = function(xml) {
24     return smartxml.elementNodeFromXML(xml);
25 };
26
27
28 describe('smartxml', function() {
29
30     describe('Basic Document properties', function() {
31         it('exposes its root element', function() {
32             var doc = getDocumentFromXML('<div></div>');
33             expect(doc.root.getTagName()).to.equal('div');
34         });
35
36         it('can resets its content entirely', function() {
37             var doc = getDocumentFromXML('<div></div>');
38
39             expect(doc.root.getTagName()).to.equal('div');
40
41             doc.loadXML('<header></header>');
42             expect(doc.root.getTagName()).to.equal('header');
43         });
44
45         it('knows if it contains an ElementNode in its tree', function() {
46             var doc = getDocumentFromXML('<root><a></a>text</root>'),
47                 root = doc.root,
48                 a = root.contents()[0],
49                 text = root.contents()[1];
50
51             expect(doc.containsNode(root)).to.equal(true, 'contains its root');
52             expect(doc.containsNode(a)).to.equal(true, 'contains Element Node');
53             expect(doc.containsNode(text)).to.equal(true, 'contains Text Node');
54         });
55
56         it('creates text nodes', function() {
57             var doc = getDocumentFromXML('<div></div>'),
58                 emptyTextNode = doc.createDocumentNode({text:''}),
59                 nonEmptyTextNode = doc.createDocumentNode({text: 'alice'});
60             expect(emptyTextNode.getText()).to.equal('', 'empty ok');
61             expect(nonEmptyTextNode.getText()).to.equal('alice', 'non empty ok');
62         });
63
64         it('creates nodes from xml strings', function() {
65             var doc = getDocumentFromXML('<div></div>'),
66                 node = doc.createDocumentNode('<a>Alice<b></b></a>');
67             expect(node.getTagName()).to.equal('a');
68             expect(node.contents().length).to.equal(2);
69             expect(node.contents()[0].getText()).to.equal('Alice');
70             expect(node.contents()[1].getTagName()).to.equal('b');
71         });
72     });
73
74     describe('DocumentNode', function() {
75         it('can be cloned', function() {
76             var doc = getDocumentFromXML('<div>Alice</div>'),
77                 text = doc.root.contents()[0],
78                 clone, suffix;
79
80             [doc.root, text].forEach(function(node) {
81                 suffix = ' (' + (node.nodeType === Node.TEXT_NODE ? 'text' : 'element')  + ')';
82                 clone = node.clone();
83                 expect(doc.containsNode(clone)).to.equal(false, 'clone is not contained in a document' + suffix);
84                 expect(node.sameNode(clone)).to.equal(false, 'clone is not same node as its originator' + suffix);
85                 expect(node.nativeNode.isEqualNode(clone.nativeNode)).to.equal(true, 'clone is identical as its originator' + suffix);
86             });
87         });
88
89         it('can be cloned with its contents and its contents data', function() {
90             var doc = getDocumentFromXML('<root><div></div></root>'),
91                 root = doc.root,
92                 div = root.contents()[0];
93
94             var ClonableObject = function(arg) {
95                 this.arg = arg;
96             };
97             ClonableObject.prototype.clone = function() {
98                 return new ClonableObject(this.arg);
99             };
100
101             div.setData('key', 'value');
102             div.setData('clonableObject', new ClonableObject('test'));
103
104             var rootClone = root.clone(),
105                 divClone = rootClone.contents()[0],
106                 stringClone = divClone.getData('key'),
107                 objClone = divClone.getData('clonableObject');
108
109             expect(stringClone).to.equal('value');
110             expect(objClone.arg).to.equal('test', 'clonable object got copied');
111             expect(objClone !== div.getData('clonableObject')).to.be.equal(true, 'copy of the clonable object is a new object');
112         });
113
114         it('knows its path in the document tree', function() {
115             var doc = getDocumentFromXML('<root><a><b><c></c>text</b></a></root>'),
116                 root = doc.root,
117                 a = root.contents()[0],
118                 b = a.contents()[0],
119                 text = b.contents()[1];
120
121             expect(root.getPath()).to.eql([], 'path of the root element is empty');
122             expect(a.getPath()).to.eql([0]);
123             expect(b.getPath()).to.eql([0, 0]);
124             expect(text.getPath()).to.eql([0,0,1]);
125
126             /* Paths relative to a given ancestor */
127             expect(text.getPath(root)).to.eql([0,0,1]);
128             expect(text.getPath(a)).to.eql([0,1]);
129             expect(text.getPath(b)).to.eql([1]);
130         });
131     });
132
133     describe('Basic ElementNode properties', function() {
134         it('exposes node contents', function() {
135             var node = elementNodeFromXML('<node>Some<node>text</node>is here</node>'),
136                 contents = node.contents();
137
138             expect(contents).to.have.length(3);
139             expect(contents[0].nodeType).to.equal(Node.TEXT_NODE, 'text node 1');
140             expect(contents[1].nodeType).to.equal(Node.ELEMENT_NODE, 'element node 1');
141             expect(contents[2].nodeType).to.equal(Node.TEXT_NODE, 'text node 2');
142         });
143
144         describe('Storing custom data', function() {
145             var node;
146
147             beforeEach(function() {
148                 node = elementNodeFromXML('<div></div>');
149             });
150
151             it('can append single value', function() {
152                 node.setData('key', 'value');
153                 expect(node.getData('key')).to.equal('value');
154             });
155
156             it('can overwrite the whole data', function() {
157                 node.setData('key1', 'value1');
158                 node.setData({key2: 'value2'});
159                 expect(node.getData('key2')).to.equal('value2');
160             });
161
162             it('can fetch the whole data at once', function() {
163                 node.setData({key1: 'value1', key2: 'value2'});
164                 expect(node.getData()).to.eql({key1: 'value1', key2: 'value2'});
165             });
166         });
167
168         describe('Changing node tag', function() {
169
170             it('can change tag name', function() {
171                 var node = elementNodeFromXML('<div></div>');
172                 node.setTag('span');
173                 expect(node.getTagName()).to.equal('span');
174             });
175
176             it('emits nodeTagChange event', function() {
177                 var node = elementNodeFromXML('<div></div>'),
178                     spy = sinon.spy();
179
180                 node.document.on('change', spy);
181                 node.setTag('span');
182                 var event = spy.args[0][0];
183
184                 expect(event.type).to.equal('nodeTagChange');
185                 expect(event.meta.node.sameNode(node)).to.be.true;
186                 expect(event.meta.oldTagName).to.equal('div');
187             });
188
189             describe('Implementation specific expectations', function() {
190                 // DOM specifies ElementNode tag as a read-only property, so
191                 // changing it in a seamless way is a little bit tricky. For this reason
192                 // the folowing expectations are required, despite the fact that they actually are
193                 // motivated by implemetation details.
194
195                 it('keeps node in the document', function() {
196                     var doc = getDocumentFromXML('<div><header></header></div>'),
197                         header = doc.root.contents()[0];
198                     header.setTag('span');
199                     expect(header.parent().sameNode(doc.root)).to.be.true;
200                 });
201                 it('keeps custom data', function() {
202                     var node = elementNodeFromXML('<div></div>');
203
204                     node.setData('key', 'value');
205                     node.setTag('header');
206                     
207                     expect(node.getTagName()).to.equal('header');
208                     expect(node.getData()).to.eql({key: 'value'});
209                 });
210
211                 it('can change document root tag name', function() {
212                     var doc = getDocumentFromXML('<div></div>');
213                     doc.root.setTag('span');
214                     expect(doc.root.getTagName()).to.equal('span');
215                 });
216
217                 it('keeps contents', function() {
218                     var node = elementNodeFromXML('<div><div></div></div>');
219                     node.setTag('header');
220                     expect(node.contents()).to.have.length(1);
221                 });
222             });
223
224         describe('Setting node attributes', function() {
225             it('can set node attribute', function() {
226                 var node = elementNodeFromXML('<div></div>');
227
228                 node.setAttr('key', 'value');
229                 expect(node.getAttr('key')).to.equal('value');
230             });
231             it('emits nodeAttrChange event', function() {
232                 var node = elementNodeFromXML('<div key="value1"></div>'),
233                     spy = sinon.spy();
234
235                 node.document.on('change', spy);
236                 node.setAttr('key', 'value2');
237                 var event = spy.args[0][0];
238
239                 expect(event.type).to.equal('nodeAttrChange');
240                 expect(event.meta.node.sameNode(node)).to.be.true;
241                 expect(event.meta.attr).to.equal('key');
242                 expect(event.meta.oldVal).to.equal('value1');
243             });
244         });
245
246         });
247     });
248
249     describe('Basic TextNode properties', function() {
250         it('can have its text set', function() {
251             var node = elementNodeFromXML('<div>Alice</div>'),
252                 textNode = node.contents()[0];
253
254             textNode.setText('Cat');
255             expect(textNode.getText()).to.equal('Cat');
256         });
257
258         it('emits nodeTextChange', function() {
259             var node = elementNodeFromXML('<div>Alice</div>'),
260                 textNode = node.contents()[0],
261                 spy = sinon.spy();
262
263             textNode.document.on('change', spy);
264             textNode.setText('Cat');
265
266             var event = spy.args[0][0];
267             expect(event.type).to.equal('nodeTextChange');
268         });
269
270         it('puts NodeElement after itself', function() {
271             var node = elementNodeFromXML('<div>Alice</div>'),
272                 textNode = node.contents()[0],
273                 returned = textNode.after({tagName:'div'});
274             expect(returned.sameNode(node.contents()[1])).to.be.true;
275         });
276
277         it('puts NodeElement before itself', function() {
278             var node = elementNodeFromXML('<div>Alice</div>'),
279                 textNode = node.contents()[0],
280                 returned = textNode.before({tagName:'div'});
281             expect(returned.sameNode(node.contents()[0])).to.be.true;
282         });
283
284         describe('Wrapping TextNode contents', function() {
285
286             it('wraps DocumentTextElement', function() {
287                 var node = elementNodeFromXML('<section>Alice</section>'),
288                     textNode = node.contents()[0];
289                 
290                 var returned = textNode.wrapWith({tagName: 'header'}),
291                     parent = textNode.parent(),
292                     parent2 = node.contents()[0];
293
294                 expect(returned.sameNode(parent)).to.be.equal(true, 'wrapper is a parent');
295                 expect(returned.sameNode(parent2)).to.be.equal(true, 'wrapper has a correct parent');
296                 expect(returned.getTagName()).to.equal('header');
297             });
298
299             describe('wrapping part of DocumentTextElement', function() {
300                 [{start: 5, end: 12}, {start: 12, end: 5}].forEach(function(offsets) {
301                     it('wraps in the middle ' + offsets.start + '/' + offsets.end, function() {
302                         var node = elementNodeFromXML('<section>Alice has a cat</section>'),
303                             textNode = node.contents()[0];
304                         
305                         var returned = textNode.wrapWith({tagName: 'header', attrs: {'attr1': 'value1'}, start: offsets.start, end: offsets.end}),
306                             contents = node.contents();
307
308                         expect(contents.length).to.equal(3);
309                         
310                         expect(contents[0].nodeType).to.be.equal(Node.TEXT_NODE, 'first node is text node');
311                         expect(contents[0].getText()).to.equal('Alice');
312
313                         expect(contents[1].sameNode(returned)).to.be.true;
314                         expect(returned.getTagName()).to.equal('header');
315                         expect(returned.getAttr('attr1')).to.equal('value1');
316                         expect(contents[1].contents().length).to.equal(1, 'wrapper has one node inside');
317                         expect(contents[1].contents()[0].getText()).to.equal(' has a ');
318
319                         expect(contents[2].nodeType).to.be.equal(Node.TEXT_NODE, 'third node is text node');
320                         expect(contents[2].getText()).to.equal('cat');
321                     });
322                 });
323
324                 it('wraps whole text inside DocumentTextElement if offsets span entire content', function() {
325                     var node = elementNodeFromXML('<section>Alice has a cat</section>'),
326                          textNode = node.contents()[0];
327                      
328                     textNode.wrapWith({tagName: 'header', start: 0, end: 15});
329                     
330                     var contents = node.contents();
331                     expect(contents.length).to.equal(1);
332                     expect(contents[0].getTagName()).to.equal('header');
333                     expect(contents[0].contents()[0].getText()).to.equal('Alice has a cat');
334                 });
335             });
336         });
337
338         describe('Dividing text node into two with element node', function() {
339                 it('can divide text node with element node, splitting text node into two', function() {
340                     var doc = getDocumentFromXML('<div>Alice has a cat</div>'),
341                         text = doc.root.contents()[0];
342
343                     var returned = text.divideWithElementNode({tagName: 'aside'}, {offset: 5}),
344                         contents = doc.root.contents(),
345                         lhsText = contents[0],
346                         rhsText = contents[2];
347
348                     expect(lhsText.getText()).to.equal('Alice');
349                     expect(returned.sameNode(contents[1]));
350                     expect(rhsText.getText()).to.equal(' has a cat');
351                 });
352
353                 it('treats dividing at the very end as appending after it', function() {
354                     var doc = getDocumentFromXML('<div>Alice has a cat</div>'),
355                         text = doc.root.contents()[0];
356
357
358                     var returned = text.divideWithElementNode({tagName: 'aside'}, {offset: 15}),
359                         contents = doc.root.contents(),
360                         textNode = contents[0],
361                         elementNode = contents[1];
362
363                     expect(contents.length).to.equal(2);
364                     expect(textNode.getText()).to.equal('Alice has a cat');
365                     expect(returned.sameNode(elementNode)).to.be.true;
366                     expect(elementNode.getTagName()).to.equal('aside');
367                 });
368
369                 it('treats dividing at the very beginning as prepending before it', function() {
370                     var doc = getDocumentFromXML('<div>Alice has a cat</div>'),
371                         text = doc.root.contents()[0];
372
373                     var returned = text.divideWithElementNode({tagName: 'aside'}, {offset: 0}),
374                         contents = doc.root.contents(),
375                         textNode = contents[1],
376                         elementNode = contents[0];
377
378                     expect(contents.length).to.equal(2);
379                     expect(textNode.getText()).to.equal('Alice has a cat');
380                     expect(returned.sameNode(elementNode)).to.be.true;
381                     expect(elementNode.getTagName()).to.equal('aside');
382                 });
383         });
384     });
385
386     describe('Manipulations', function() {
387
388         describe('replacing node with another one', function() {
389             it('replaces node with another one', function() {
390                 var doc = getDocumentFromXML('<div><a></a></div>'),
391                     a = doc.root.contents()[0];
392
393                 var c = a.replaceWith({tagName: 'b', attrs: {b:'1'}});
394
395                 expect(doc.root.contents()[0].sameNode(c));
396                 expect(c.getTagName()).to.equal('b');
397                 expect(c.getAttr('b')).to.equal('1');
398             });
399             it('can replace document root', function() {
400                 var doc = getDocumentFromXML('<div></div>');
401
402                 var header = doc.root.replaceWith({tagName: 'header'});
403
404                 expect(doc.root.sameNode(header)).to.be.true;
405                 expect(doc.containsNode(header)).to.be.true;
406             });
407         });
408
409         it('merges adjacent text nodes resulting from detaching an element node in between', function() {
410             var doc = getDocumentFromXML('<div>Alice <span>has</span>a cat</div>'),
411                 span = doc.root.contents()[1];
412
413             span.detach();
414
415             var rootContents = doc.root.contents();
416             expect(rootContents).to.have.length(1, 'one child left');
417             expect(rootContents[0].getText()).to.equal('Alice a cat');
418         });
419
420         it('inserts node at index', function() {
421             var doc = getDocumentFromXML('<div><a></a><b></b><c></c></div>'),
422                 b = doc.root.contents()[1];
423
424             var inserted = doc.root.insertAtIndex({tagName: 'test'}, 1);
425
426             expect(doc.root.contents()[1].sameNode(inserted)).to.equal(true, 'inserted node returned');
427             expect(b.getIndex()).to.equal(2, 'b node shifted right');
428         });
429
430         it('appends node when inserting node at index out of range', function() {
431             var doc = getDocumentFromXML('<div></div>');
432
433             var test1 = doc.root.insertAtIndex({tagName: 'test1'}, 0),
434                 test2 = doc.root.insertAtIndex({tagName: 'test1'}, 10);
435
436             expect(doc.root.contents()[0].sameNode(test1)).to.equal(true, 'inserting at index 0 of empty nodes appends node');
437             expect(doc.root.contents().length).to.equal(1, 'inserting at index out of range does nothing');
438             expect(test2).to.equal(undefined, 'inserting at index out of range returns undefined');
439         });
440
441         it('appends element node to another element node', function() {
442             var node1 = elementNodeFromParams({tag: 'div'}),
443                 node2 = elementNodeFromParams({tag: 'a'}),
444                 node3 = elementNodeFromParams({tag: 'p'});
445             node1.append(node2);
446             node1.append(node3);
447             expect(node1.contents()[0].sameNode(node2)).to.be.true;
448             expect(node1.contents()[1].sameNode(node3)).to.be.true;
449         });
450
451         it('prepends element node to another element node', function() {
452             var node1 = elementNodeFromParams({tag: 'div'}),
453                 node2 = elementNodeFromParams({tag: 'a'}),
454                 node3 = elementNodeFromParams({tag: 'p'});
455             node1.prepend(node2);
456             node1.prepend(node3);
457             expect(node1.contents()[0].sameNode(node3)).to.be.true;
458             expect(node1.contents()[1].sameNode(node2)).to.be.true;
459         });
460
461         describe('adding text nodes', function() {
462             it('merges text nodes on append', function() {
463                 var doc = getDocumentFromXML('<root>text1</root>'),
464                     returned;
465                 returned = doc.root.append({text: 'text2'});
466                 expect(doc.root.contents().length).to.equal(1);
467                 expect(returned.sameNode(doc.root.contents()[0])).to.equal(true, 'modified node returned');
468                 expect(doc.root.contents()[0].getText()).to.equal('text1text2');
469             });
470
471             it('merges text nodes on prepend', function() {
472                 var doc = getDocumentFromXML('<root>text1</root>'),
473                     returned;
474                 returned = doc.root.prepend({text: 'text2'});
475                 expect(doc.root.contents().length).to.equal(1);
476                 expect(returned.sameNode(doc.root.contents()[0])).to.equal(true, 'modified node returned');
477                 expect(doc.root.contents()[0].getText()).to.equal('text2text1');
478             });
479
480             it('merges text nodes on before text node', function() {
481                 var doc = getDocumentFromXML('<root>text1</root>'),
482                     textNode = doc.root.contents()[0],
483                     returned;
484                 returned = textNode.before({text: 'text2'});
485                 expect(doc.root.contents().length).to.equal(1);
486                 expect(returned.sameNode(doc.root.contents()[0])).to.equal(true, 'modified node returned');
487                 expect(doc.root.contents()[0].getText()).to.equal('text2text1');
488             });
489
490             it('merges text nodes on after text node', function() {
491                 var doc = getDocumentFromXML('<root>text1</root>'),
492                     textNode = doc.root.contents()[0],
493                     returned;
494                 returned = textNode.after({text: 'text2'});
495                 expect(doc.root.contents().length).to.equal(1);
496                 expect(returned.sameNode(doc.root.contents()[0])).to.equal(true, 'modified node returned');
497                 expect(doc.root.contents()[0].getText()).to.equal('text1text2');
498             });
499
500             it('merges text nodes on before element node', function() {
501                 var doc = getDocumentFromXML('<root>text1<div></div></root>'),
502                     textNode = doc.root.contents()[0],
503                     div = doc.root.contents()[1],
504                     returned;
505                 returned = div.before({text: 'text2'});
506                 expect(doc.root.contents().length).to.equal(2);
507                 expect(returned.sameNode(doc.root.contents()[0])).to.equal(true, 'modified node returned');
508                 expect(textNode.getText()).to.equal('text1text2');
509             });
510
511             it('merges text nodes on after element node', function() {
512                 var doc = getDocumentFromXML('<root><div></div>text1</root>'),
513                     textNode = doc.root.contents()[1],
514                     div = doc.root.contents()[0],
515                     returned;
516                 returned = div.after({text: 'text2'});
517                 expect(doc.root.contents().length).to.equal(2);
518                 expect(returned.sameNode(doc.root.contents()[1])).to.equal(true, 'modified node returned');
519                 expect(textNode.getText()).to.equal('text2text1');
520             });
521         });
522
523         it('wraps element node with another element node', function() {
524             var node = elementNodeFromXML('<div></div>'),
525                 wrapper = elementNodeFromXML('<wrapper></wrapper>');
526
527             node.wrapWith(wrapper);
528             expect(node.parent().sameNode(wrapper)).to.be.true;
529         });
530
531         it('unwraps element node contents', function() {
532             var node = elementNodeFromXML('<div>Alice <div>has <span>propably</span> a cat</div>!</div>'),
533                 outerDiv = node.contents()[1];
534             
535             outerDiv.unwrapContent();
536
537             expect(node.contents().length).to.equal(3);
538             expect(node.contents()[0].getText()).to.equal('Alice has ');
539             expect(node.contents()[1].getTagName()).to.equal('span');
540             expect(node.contents()[2].getText()).to.equal(' a cat!');
541         });
542
543         it('unwrap single element node from its parent', function() {
544             var doc = getDocumentFromXML('<div><a><b></b></a></div>'),
545                 div = doc.root,
546                 a = div.contents()[0],
547                 b = a.contents()[0];
548
549             var parent = b.unwrap();
550
551             expect(parent.sameNode(div)).to.equal(true, 'returns new parent');
552             expect(div.contents()).to.have.length(1, 'root contains only one node');
553             expect(div.contents()[0].sameNode(b)).to.equal(true, 'node got unwrapped');
554         });
555
556         it('unwrap single text node from its parent', function() {
557             var doc = getDocumentFromXML('<div>Some <span>text</span>!</div>'),
558                 div = doc.root,
559                 span = div.contents()[1],
560                 text = span.contents()[0];
561
562             var parent = text.unwrap();
563
564             expect(parent.sameNode(div)).to.equal(true, 'returns new parent');
565             expect(div.contents()).to.have.length(1, 'root contains only one node');
566             expect(div.contents()[0].getText()).to.equal('Some text!');
567         });
568
569         describe('Wrapping text', function() {
570             it('wraps text spanning multiple sibling TextNodes', function() {
571                 var section = elementNodeFromXML('<section>Alice has a <span>small</span> cat</section>'),
572                     wrapper = section.wrapText({
573                         _with: {tagName: 'span', attrs: {'attr1': 'value1'}},
574                         offsetStart: 6,
575                         offsetEnd: 4,
576                         textNodeIdx: [0,2]
577                     });
578
579                 expect(section.contents().length).to.equal(2);
580                 expect(section.contents()[0].nodeType).to.equal(Node.TEXT_NODE);
581                 expect(section.contents()[0].getText()).to.equal('Alice ');
582
583                 var wrapper2 = section.contents()[1];
584                 expect(wrapper2.sameNode(wrapper)).to.be.true;
585                 expect(wrapper.getTagName()).to.equal('span');
586
587                 var wrapperContents = wrapper.contents();
588                 expect(wrapperContents.length).to.equal(3);
589                 expect(wrapperContents[0].getText()).to.equal('has a ');
590
591                 expect(wrapperContents[1].nodeType).to.equal(Node.ELEMENT_NODE);
592                 expect(wrapperContents[1].contents().length).to.equal(1);
593                 expect(wrapperContents[1].contents()[0].getText()).to.equal('small');
594             });
595         });
596
597         describe('Wrapping Nodes', function() {
598             it('wraps multiple sibling nodes', function() {
599                 var section = elementNodeFromXML('<section>Alice<div>has</div><div>a cat</div></section>'),
600                     aliceText = section.contents()[0],
601                     firstDiv = section.contents()[1],
602                     lastDiv = section.contents()[section.contents().length -1];
603
604                 var returned = section.document.wrapNodes({
605                         node1: aliceText,
606                         node2: lastDiv,
607                         _with: {tagName: 'header'}
608                     });
609
610                 var sectionContentss = section.contents(),
611                     header = sectionContentss[0],
612                     headerContents = header.contents();
613
614                 expect(sectionContentss).to.have.length(1);
615                 expect(header.sameNode(returned)).to.equal(true, 'wrapper returned');
616                 expect(header.parent().sameNode(section)).to.be.true;
617                 expect(headerContents).to.have.length(3);
618                 expect(headerContents[0].sameNode(aliceText)).to.equal(true, 'first node wrapped');
619                 expect(headerContents[1].sameNode(firstDiv)).to.equal(true, 'second node wrapped');
620                 expect(headerContents[2].sameNode(lastDiv)).to.equal(true, 'third node wrapped');
621             });
622
623             it('wraps multiple sibling Elements - middle case', function() {
624                 var section = elementNodeFromXML('<section><div></div><div></div><div></div><div></div></section>'),
625                     div2 = section.contents()[1],
626                     div3 = section.contents()[2];
627
628                 section.document.wrapNodes({
629                         node1: div2,
630                         node2: div3,
631                         _with: {tagName: 'header'}
632                     });
633
634                 var sectionContentss = section.contents(),
635                     header = sectionContentss[1],
636                     headerChildren = header.contents();
637
638                 expect(sectionContentss).to.have.length(3);
639                 expect(headerChildren).to.have.length(2);
640                 expect(headerChildren[0].sameNode(div2)).to.equal(true, 'first node wrapped');
641                 expect(headerChildren[1].sameNode(div3)).to.equal(true, 'second node wrapped');
642             });
643         });
644
645     });
646
647     var getTextNodes = function(text, doc) {
648         /* globals Node */
649         var toret = [];
650         var search = function(node) {
651             node.contents().forEach(function(node) {
652                 if(node.nodeType === Node.TEXT_NODE) {
653                     if(node.getText() === text) {
654                         toret.push(node);
655                     }
656                 } else {
657                     search(node);
658                 }
659             });
660         };
661         search(doc.root);
662         return toret;
663     };
664
665     var getTextNode = function(text, doc) {
666         var nodes = getTextNodes(text, doc),
667             error;
668         if(nodes.length === 0) {
669             error = 'Text not found';
670         } else if(nodes.length > 1) {
671             error = 'Text not unique';
672         } else if(nodes[0].getText() !== text) {
673             error = 'I was trying to cheat your test :(';
674         }
675         if(error) {
676             throw new Error(error);
677         }
678         return nodes[0];
679     };
680
681     describe('Removing arbitrary text', function() {
682         it('removes within single text element', function() {
683             var doc = getDocumentFromXML('<div>Alice</div>'),
684                 text = getTextNode('Alice', doc);
685             doc.deleteText({
686                 from: {
687                     node: text,
688                     offset: 1
689                 },
690                 to: {
691                     node: text,
692                     offset: 4
693                 }
694             });
695             expect(doc.root.contents().length).to.equal(1);
696             expect(doc.root.contents()[0].getText()).to.equal('Ae');
697         });
698         it('removes across elements - 1', function() {
699             var doc = getDocumentFromXML('<div><a>aaa</a><b>bbb</b></div>');
700
701             doc.deleteText({
702                 from: {
703                     node: getTextNode('aaa', doc),
704                     offset: 2
705                 },
706                 to: {
707                     node: getTextNode('bbb', doc),
708                     offset: 2
709                 }
710             });
711
712             var contents = doc.root.contents();
713             expect(contents.length).to.equal(2);
714             expect(contents[0].contents()[0].getText()).to.equal('aa');
715             expect(contents[1].contents()[0].getText()).to.equal('b');
716         });
717         it('removes across elements - 2', function() {
718             var doc = getDocumentFromXML('<a><b><c>ccc</c></b>xxx</a>');
719             doc.deleteText({
720                 from: {
721                     node: getTextNode('ccc', doc),
722                     offset: 2
723                 },
724                 to: {
725                     node: getTextNode('xxx', doc),
726                     offset: 2
727                 }
728             });
729
730             var contents = doc.root.contents();
731             expect(contents.length).to.equal(2);
732             expect(contents[0].getTagName()).to.equal('b');
733             expect(contents[1].getText()).to.equal('x');
734             
735             var bContents = contents[0].contents();
736             expect(bContents.length).to.equal(1);
737             expect(bContents[0].getTagName()).to.equal('c');
738             expect(bContents[0].contents().length).to.equal(1);
739             expect(bContents[0].contents()[0].getText()).to.equal('cc');
740         });
741         it('remove across elements - 3 (merged text nodes)', function() {
742             var doc = getDocumentFromXML('<div>Alice <span>has</span> a cat</div>');
743             doc.deleteText({
744                 from: {
745                     node: getTextNode('Alice ', doc),
746                     offset: 1
747                 },
748                 to: {
749                     node: getTextNode(' a cat', doc),
750                     offset: 3
751                 }
752             });
753             var contents = doc.root.contents();
754             expect(contents.length).to.equal(1);
755             expect(contents[0].getText()).to.equal('Acat');
756         });
757         it('remove across elements - 4', function() {
758             var doc = getDocumentFromXML('<div>Alice <div>has <span>a</span> cat</div></div>');
759             doc.deleteText({
760                 from: {
761                     node: getTextNode('Alice ', doc),
762                     offset: 1
763                 },
764                 to: {
765                     node: getTextNode(' cat', doc),
766                     offset: 1
767                 }
768             });
769             var contents = doc.root.contents();
770             expect(contents.length).to.equal(2);
771             expect(contents[0].getText()).to.equal('A');
772             expect(contents[1].getTagName()).to.equal('div');
773             expect(contents[1].contents().length).to.equal(1);
774             expect(contents[1].contents()[0].getText()).to.equal('cat');
775         });
776         it('removes across elements - 5 (whole document)', function() {
777             var doc = getDocumentFromXML('<div>Alice <div>has <span>a</span> cat</div>!!!</div>');
778             doc.deleteText({
779                 from: {
780                     node: getTextNode('Alice ', doc),
781                     offset: 0
782                 },
783                 to: {
784                     node: getTextNode('!!!', doc),
785                     offset: 3
786                 }
787             });
788
789             expect(doc.root.getTagName()).to.equal('div');
790             expect(doc.root.contents().length).to.equal(1);
791             expect(doc.root.contents()[0].getText()).to.equal('');
792         });
793         it('removes nodes in between', function() {
794             var doc = getDocumentFromXML('<div><a>aaa<x>!</x></a>xxx<x></x><b><x>!</x>bbb</b></div>');
795             doc.deleteText({
796                 from: {
797                     node: getTextNode('aaa', doc),
798                     offset: 2
799                 },
800                 to: {
801                     node: getTextNode('bbb', doc),
802                     offset: 2
803                 }
804             });
805
806             var contents = doc.root.contents();
807             expect(contents.length).to.equal(2, 'two nodes survived');
808             expect(contents[0].getTagName()).to.equal('a');
809             expect(contents[1].getTagName()).to.equal('b');
810             expect(contents[0].contents().length).to.equal(1);
811             expect(contents[0].contents()[0].getText()).to.equal('aa');
812             expect(contents[1].contents().length).to.equal(1);
813             expect(contents[1].contents()[0].getText()).to.equal('b');
814         });
815     });
816
817     describe('Splitting text', function() {
818     
819         it('splits TextNode\'s parent into two ElementNodes', function() {
820             var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
821                 section = doc.root,
822                 text = section.contents()[0].contents()[0];
823
824             var returnedValue = text.split({offset: 5});
825             expect(section.contents().length).to.equal(2, 'section has two children');
826             
827             var header1 = section.contents()[0];
828             var header2 = section.contents()[1];
829
830             expect(header1.getTagName()).to.equal('header', 'first section child ok');
831             expect(header1.contents().length).to.equal(1, 'first header has one child');
832             expect(header1.contents()[0].getText()).to.equal('Some ', 'first header has correct content');
833             expect(header2.getTagName()).to.equal('header', 'second section child ok');
834             expect(header2.contents().length).to.equal(1, 'second header has one child');
835             expect(header2.contents()[0].getText()).to.equal('header', 'second header has correct content');
836
837             expect(returnedValue.first.sameNode(header1)).to.equal(true, 'first node returned');
838             expect(returnedValue.second.sameNode(header2)).to.equal(true, 'second node returned');
839         });
840
841         it('leaves empty copy of ElementNode if splitting at the very beginning', function() {
842                 var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
843                 section = doc.root,
844                 text = section.contents()[0].contents()[0];
845
846                 text.split({offset: 0});
847                 
848                 var header1 = section.contents()[0];
849                 var header2 = section.contents()[1];
850
851                 expect(header1.contents().length).to.equal(0);
852                 expect(header2.contents()[0].getText()).to.equal('Some header');
853         });
854
855         it('leaves empty copy of ElementNode if splitting at the very end', function() {
856                 var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
857                 section = doc.root,
858                 text = section.contents()[0].contents()[0];
859
860                 text.split({offset: 11});
861                 
862                 var header1 = section.contents()[0];
863                 var header2 = section.contents()[1];
864
865                 expect(header1.contents()[0].getText()).to.equal('Some header');
866                 expect(header2.contents().length).to.equal(0);
867         });
868
869         it('keeps TextNodes\'s parent\'s children elements intact', function() {
870             var doc = getDocumentFromXML('<section><header>A <span>fancy</span> and <span>nice</span> header</header></section>'),
871                 section = doc.root,
872                 header = section.contents()[0],
873                 textAnd = header.contents()[2];
874
875             textAnd.split({offset: 2});
876             
877             var sectionContents = section.contents();
878             expect(sectionContents.length).to.equal(2, 'Section has two children');
879             expect(sectionContents[0].getTagName()).to.equal('header', 'First section node is a header');
880             expect(sectionContents[1].getTagName()).to.equal('header', 'Second section node is a header');
881
882             var firstHeaderContents = sectionContents[0].contents();
883             expect(firstHeaderContents.length).to.equal(3, 'First header has three children');
884             expect(firstHeaderContents[0].getText()).to.equal('A ', 'First header starts with a text');
885             expect(firstHeaderContents[1].getTagName()).to.equal('span', 'First header has span in the middle');
886             expect(firstHeaderContents[2].getText()).to.equal(' a', 'First header ends with text');
887
888             var secondHeaderContents = sectionContents[1].contents();
889             expect(secondHeaderContents.length).to.equal(3, 'Second header has three children');
890             expect(secondHeaderContents[0].getText()).to.equal('nd ', 'Second header starts with text');
891             expect(secondHeaderContents[1].getTagName()).to.equal('span', 'Second header has span in the middle');
892             expect(secondHeaderContents[2].getText()).to.equal(' header', 'Second header ends with text');
893         });
894     });
895
896     describe('Events', function() {
897         it('emits nodeDetached event on node detach', function() {
898             var node = elementNodeFromXML('<div><div></div></div>'),
899                 innerNode = node.contents()[0],
900                 spy = sinon.spy();
901             node.document.on('change', spy);
902             
903             var detached = innerNode.detach(),
904                 event = spy.args[0][0];
905
906             expect(event.type).to.equal('nodeDetached');
907             expect(event.meta.node.sameNode(detached, 'detached node in event meta'));
908             expect(event.meta.parent.sameNode(node), 'original parent node in event meta');
909         }),
910
911         it('emits nodeAdded event when appending new node', function() {
912             var node = elementNodeFromXML('<div></div>'),
913                 spy = sinon.spy();
914             node.document.on('change', spy);
915             
916             var appended = node.append({tagName:'div'}),
917                 event = spy.args[0][0];
918             expect(event.type).to.equal('nodeAdded');
919             expect(event.meta.node.sameNode(appended)).to.be.true;
920         });
921         
922         it('emits nodeMoved when appending aready existing node', function() {
923             var node = elementNodeFromXML('<div><a></a><b></b></div>'),
924                 a = node.contents()[0],
925                 b = node.contents()[1],
926                 spy = sinon.spy();
927             node.document.on('change', spy);
928             
929             var appended = a.append(b),
930                 event = spy.args[0][0];
931
932             expect(spy.callCount).to.equal(1);
933             expect(event.type).to.equal('nodeMoved');
934             expect(event.meta.node.sameNode(appended)).to.be.true;
935             expect(node.document.root.sameNode(event.meta.parent)).to.equal(true, 'previous parent attached to event meta');
936         });
937         
938         it('emits nodeAdded event when prepending new node', function() {
939             var node = elementNodeFromXML('<div></div>'),
940                 spy = sinon.spy();
941             node.document.on('change', spy);
942             
943             var prepended = node.prepend({tagName:'div'}),
944                 event = spy.args[0][0];
945             expect(event.type).to.equal('nodeAdded');
946             expect(event.meta.node.sameNode(prepended)).to.be.true;
947         });
948         
949         it('emits nodeMoved when prepending aready existing node', function() {
950             var node = elementNodeFromXML('<div><a></a><b></b></div>'),
951                 a = node.contents()[0],
952                 b = node.contents()[1],
953                 spy = sinon.spy();
954             node.document.on('change', spy);
955             
956             var prepended = a.prepend(b),
957                 event = spy.args[0][0];
958             expect(spy.callCount).to.equal(1);
959             expect(event.type).to.equal('nodeMoved');
960             expect(event.meta.node.sameNode(prepended)).to.be.true;
961         });
962         
963         it('emits nodeAdded event when inserting node after another', function() {
964             var node = elementNodeFromXML('<div><a></a></div>').contents()[0],
965                 spy = sinon.spy();
966             node.document.on('change', spy);
967             
968             var inserted = node.after({tagName:'div'}),
969                 event = spy.args[0][0];
970             expect(event.type).to.equal('nodeAdded');
971             expect(event.meta.node.sameNode(inserted)).to.be.true;
972         });
973         
974         it('emits nodeMoved when inserting aready existing node after another', function() {
975             var node = elementNodeFromXML('<div><a></a><b></b></div>'),
976                 a = node.contents()[0],
977                 b = node.contents()[1],
978                 spy = sinon.spy();
979             node.document.on('change', spy);
980             var inserted = b.after(a),
981                 event = spy.args[0][0];
982
983             expect(spy.callCount).to.equal(1);
984             expect(event.type).to.equal('nodeMoved');
985             expect(event.meta.node.sameNode(inserted)).to.be.true;
986         });
987
988         it('emits nodeAdded event when inserting node before another', function() {
989             var node = elementNodeFromXML('<div><a></a></div>').contents()[0],
990                 spy = sinon.spy();
991             node.document.on('change', spy);
992             
993             var inserted = node.before({tagName:'div'}),
994                 event = spy.args[0][0];
995             expect(event.type).to.equal('nodeAdded');
996             expect(event.meta.node.sameNode(inserted)).to.be.true;
997         });
998         
999         it('emits nodeAdded when inserting aready existing node before another', function() {
1000             var node = elementNodeFromXML('<div><a></a><b></b></div>'),
1001                 a = node.contents()[0],
1002                 b = node.contents()[1],
1003                 spy = sinon.spy();
1004             node.document.on('change', spy);
1005             var inserted = a.before(b),
1006                 event = spy.args[0][0];
1007
1008             expect(spy.callCount).to.equal(1);
1009             expect(event.type).to.equal('nodeMoved');
1010             expect(event.meta.node.sameNode(inserted)).to.be.true;
1011         });
1012
1013         it('emits nodeDetached and nodeAdded when replacing root node with another', function() {
1014             var doc = getDocumentFromXML('<a></a>'),
1015                 oldRoot = doc.root,
1016                 spy = sinon.spy();
1017
1018             doc.on('change', spy);
1019
1020             doc.root.replaceWith({tagName: 'b'});
1021
1022             expect(spy.callCount).to.equal(2);
1023
1024             var event1 = spy.args[0][0],
1025                 event2 = spy.args[1][0];
1026
1027             expect(event1.type).to.equal('nodeDetached');
1028             expect(event1.meta.node.sameNode(oldRoot)).to.equal(true, 'root node in nodeDetached event metadata');
1029             expect(event2.type).to.equal('nodeAdded');
1030             expect(event2.meta.node.sameNode(doc.root)).to.equal(true, 'new root node in nodelAdded event meta');
1031         });
1032
1033
1034         ['append', 'prepend', 'before', 'after'].forEach(function(insertionMethod) {
1035             it('emits nodeDetached for node moved from a document tree to out of document node ' + insertionMethod, function() {
1036                 var doc = getDocumentFromXML('<div><a></a></div>'),
1037                     a = doc.root.contents()[0],
1038                     spy = sinon.spy();
1039
1040                 doc.on('change', spy);
1041
1042                 var newNode = doc.createDocumentNode({tagName: 'b'}),
1043                     newNodeInner = newNode.append({tagName:'c'});
1044
1045                 newNodeInner[insertionMethod](a);
1046
1047                 var event = spy.args[0][0];
1048                 expect(event.type).to.equal('nodeDetached');
1049                 expect(event.meta.node.sameNode(a));
1050             });
1051
1052             it('doesn\'t emit nodeDetached event for already out of document node moved to out of document node' + insertionMethod, function() {
1053                 var doc = getDocumentFromXML('<div><a></a></div>'),
1054                     spy = sinon.spy();
1055
1056                 doc.on('change', spy);
1057
1058                 var newNode = doc.createDocumentNode({tagName: 'b'});
1059                 newNode.append({tagName:'c'});
1060
1061                 expect(spy.callCount).to.equal(0);
1062             });
1063         });
1064
1065
1066     });
1067
1068     describe('Traversing', function() {
1069         describe('Basic', function() {
1070             it('can access node parent', function() {
1071                 var doc = getDocumentFromXML('<a><b></b></a>'),
1072                     a = doc.root,
1073                     b = a.contents()[0];
1074
1075                 expect(a.parent()).to.equal(null, 'parent of a root is null');
1076                 expect(b.parent().sameNode(a)).to.be.true;
1077             });
1078             it('can access node parents', function() {
1079                 var doc = getDocumentFromXML('<a><b><c></c></b></a>'),
1080                     a = doc.root,
1081                     b = a.contents()[0],
1082                     c = b.contents()[0];
1083
1084                 var parents = c.parents();
1085                 // @@
1086                 expect(parents[0].sameNode(b)).to.be.true;
1087                 expect(parents[1].sameNode(a)).to.be.true;
1088             });
1089         });
1090
1091         describe('finding sibling parents of two elements', function() {
1092             it('returns elements themself if they have direct common parent', function() {
1093                 var doc = getDocumentFromXML('<section><div><div>A</div><div>B</div></div></section>'),
1094                     wrappingDiv = doc.root.contents()[0],
1095                     divA = wrappingDiv.contents()[0],
1096                     divB = wrappingDiv.contents()[1];
1097
1098                 var siblingParents = doc.getSiblingParents({node1: divA, node2: divB});
1099
1100                 expect(siblingParents.node1.sameNode(divA)).to.equal(true, 'divA');
1101                 expect(siblingParents.node2.sameNode(divB)).to.equal(true, 'divB');
1102             });
1103
1104             it('returns sibling parents - example 1', function() {
1105                 var doc = getDocumentFromXML('<section>Alice <span>has a cat</span></section>'),
1106                     aliceText = doc.root.contents()[0],
1107                     span = doc.root.contents()[1],
1108                     spanText = span.contents()[0];
1109
1110                 var siblingParents = doc.getSiblingParents({node1: aliceText, node2: spanText});
1111
1112                 expect(siblingParents.node1.sameNode(aliceText)).to.equal(true, 'aliceText');
1113                 expect(siblingParents.node2.sameNode(span)).to.equal(true, 'span');
1114             });
1115
1116             it('returns node itself for two same nodes', function() {
1117                 var doc = getDocumentFromXML('<section><div></div></section>'),
1118                     div = doc.root.contents()[0];
1119
1120                 var siblingParents = doc.getSiblingParents({node1: div, node2: div});
1121                 expect(!!siblingParents.node1 && !!siblingParents.node2).to.equal(true, 'nodes defined');
1122                 expect(siblingParents.node1.sameNode(div)).to.be.equal(true, 'node1');
1123                 expect(siblingParents.node2.sameNode(div)).to.be.equal(true, 'node2');
1124             });
1125         });
1126     });
1127
1128     describe('Serializing document to WLXML', function() {
1129         it('keeps document intact when no changes have been made', function() {
1130             var xmlIn = '<section>Alice<div>has</div>a <span class="uri" meta-uri="http://cat.com">cat</span>!</section>',
1131                 doc = getDocumentFromXML(xmlIn),
1132                 xmlOut = doc.toXML();
1133
1134             var parser = new DOMParser(),
1135                 input = parser.parseFromString(xmlIn, 'application/xml').childNodes[0],
1136                 output = parser.parseFromString(xmlOut, 'application/xml').childNodes[0];
1137             
1138             expect(input.isEqualNode(output)).to.be.true;
1139         });
1140
1141         it('keeps entities intact', function() {
1142             var xmlIn = '<section>&lt; &gt;</section>',
1143                 doc = getDocumentFromXML(xmlIn),
1144                 xmlOut = doc.toXML();
1145             expect(xmlOut).to.equal(xmlIn);
1146         });
1147         it('keeps entities intact when they form html/xml', function() {
1148             var xmlIn = '<section>&lt;abc&gt;</section>',
1149                 doc = getDocumentFromXML(xmlIn),
1150                 xmlOut = doc.toXML();
1151             expect(xmlOut).to.equal(xmlIn);
1152         });
1153     });
1154
1155     describe('Extension API', function() {
1156         var doc, extension, elementNode, textNode;
1157
1158         beforeEach(function() {
1159             doc = getDocumentFromXML('<section>Alice<div class="test_class"></div></section>');
1160             elementNode = doc.root;
1161             textNode = doc.root.contents()[0];
1162             extension = {};
1163             
1164             expect(elementNode.testTransformation).to.be.undefined;
1165             expect(textNode.testTransformation).to.be.undefined;
1166             expect(doc.testTransformation).to.be.undefined;
1167             
1168             expect(doc.testMethod).to.be.undefined;
1169             expect(elementNode.testMethod).to.be.undefined;
1170             expect(textNode.testMethod).to.be.undefined;
1171             expect(elementNode.elementTestMethod).to.be.undefined;
1172             expect(textNode.textTestMethod).to.be.undefined;
1173         });
1174
1175         it('allows adding method to a document', function() {
1176             extension = {document: {methods: {
1177                 testMethod: function() { return this; }
1178             }}};
1179
1180             doc.registerExtension(extension);
1181             expect(doc.testMethod()).to.equal(doc, 'context is set to a document instance');
1182         });
1183
1184         it('allows adding transformation to a document', function() {
1185             extension = {document: {transformations: {
1186                 testTransformation: function() { return this; },
1187                 testTransformation2: {impl: function() { return this;}}
1188             }}};
1189
1190             doc.registerExtension(extension);
1191             expect(doc.testTransformation()).to.equal(doc, 'context is set to a document instance');
1192             expect(doc.testTransformation2()).to.equal(doc, 'context is set to a document instance');
1193         });
1194
1195         it('allows adding method to a DocumentNode instance', function() {
1196             extension = {
1197                 documentNode: {
1198                     methods: {
1199                         testMethod: function() { return this; }
1200                     }
1201                 },
1202                 textNode: {
1203                     methods: {
1204                         textTestMethod: function() { return this; }
1205                     }
1206                 },
1207                 elementNode: {
1208                     methods: {
1209                         elementTestMethod: function() { return this; }
1210                     }
1211                 }
1212             };
1213
1214             doc.registerExtension(extension);
1215
1216             /* refresh */
1217             elementNode = doc.root;
1218             textNode = doc.root.contents()[0];
1219
1220             expect(elementNode.testMethod().sameNode(elementNode)).to.equal(true, 'context is set to a node instance');
1221             expect(textNode.testMethod().sameNode(textNode)).to.equal(true, 'context is set to a node instance');
1222
1223             expect(elementNode.elementTestMethod().sameNode(elementNode)).to.be.true;
1224             expect(elementNode.textTestMethod).to.be.undefined;
1225         
1226             expect(textNode.textTestMethod().sameNode(textNode)).to.be.true;
1227             expect(textNode.elementTestMethod).to.be.undefined;
1228         });
1229
1230         it('allows adding transformation to a DocumentNode', function() {
1231             extension = {
1232                 documentNode: {
1233                     transformations: {
1234                         testTransformation: function() { return this; },
1235                         testTransformation2: {impl: function() { return this;}}
1236                     }
1237                 },
1238                 textNode: {
1239                     transformations: {
1240                         textTestTransformation: function() { return this; }
1241                     }
1242                 },
1243                 elementNode: {
1244                     transformations: {
1245                         elementTestTransformation: function() { return this; }
1246                     }
1247                 }
1248             };
1249             
1250             doc.registerExtension(extension);
1251
1252             /* refresh */
1253             elementNode = doc.root;
1254             textNode = doc.root.contents()[0];
1255             
1256             expect(elementNode.testTransformation().sameNode(elementNode)).to.equal(true, '1');
1257             expect(elementNode.testTransformation2().sameNode(elementNode)).to.equal(true, '2');
1258             expect(textNode.testTransformation().sameNode(textNode)).to.equal(true, '3');
1259             expect(textNode.testTransformation2().sameNode(textNode)).to.equal(true, '4');
1260
1261             expect(elementNode.elementTestTransformation().sameNode(elementNode)).to.be.true;
1262             expect(elementNode.textTestTransformation).to.be.undefined;
1263         
1264             expect(textNode.textTestTransformation().sameNode(textNode)).to.be.true;
1265             expect(textNode.elementTestTransfomation).to.be.undefined;
1266         });
1267
1268         it('allows text/element node methods and transformations to access node and transormations on document node', function() {
1269
1270             var doc = getDocumentFromXML('<div>text</div>');
1271
1272             doc.registerExtension({
1273                 documentNode: {
1274                     methods: {
1275                         test: function() {
1276                             return 'super';
1277                         }
1278                     },
1279                     transformations: {
1280                         testT: function() {
1281                             return 'super_trans';
1282                         }
1283                     }
1284                 },
1285                 elementNode: {
1286                     methods: {
1287                         test: function() {
1288                             return 'element_sub_' + this.__super__.test();
1289                         }
1290                     },
1291                     transformations: {
1292                         testT: function() {
1293                             return 'element_trans_sub_' + this.__super__.testT();
1294                         }
1295                     }
1296                 },
1297                 textNode: {
1298                     methods: {
1299                         test: function() {
1300                             return 'text_sub_' + this.__super__.test();
1301                         }
1302                     },
1303                     transformations: {
1304                         testT: function() {
1305                             return 'text_trans_sub_' + this.__super__.testT();
1306                         }
1307                     }
1308                 }
1309             });
1310
1311             var textNode = doc.root.contents()[0];
1312
1313             expect(doc.root.test()).to.equal('element_sub_super');
1314             expect(textNode.test()).to.equal('text_sub_super');
1315             expect(doc.root.testT()).to.equal('element_trans_sub_super_trans');
1316             expect(textNode.testT()).to.equal('text_trans_sub_super_trans');
1317         });
1318     });
1319
1320     describe('Undo/redo', function() {
1321
1322         it('smoke tests', function() {
1323             var doc = getDocumentFromXML('<div>Alice</div>'),
1324                 textNode = doc.root.contents()[0];
1325
1326             expect(doc.undoStack).to.have.length(0);
1327             
1328             textNode.wrapWith({tagName: 'span', start:1, end:2});
1329             expect(doc.undoStack).to.have.length(1, '1');
1330             expect(doc.toXML()).to.equal('<div>A<span>l</span>ice</div>');
1331
1332             doc.undo();
1333             expect(doc.undoStack).to.have.length(0, '2');
1334             expect(doc.toXML()).to.equal('<div>Alice</div>');
1335
1336             doc.redo();
1337             expect(doc.undoStack).to.have.length(1, '3');
1338             expect(doc.toXML()).to.equal('<div>A<span>l</span>ice</div>');
1339
1340             doc.undo();
1341             expect(doc.undoStack).to.have.length(0, '4');
1342             expect(doc.toXML()).to.equal('<div>Alice</div>');
1343
1344             doc.undo();
1345             expect(doc.undoStack).to.have.length(0, '5');
1346             expect(doc.toXML()).to.equal('<div>Alice</div>');
1347         });
1348
1349         it('smoke tests 2', function() {
1350             var doc = getDocumentFromXML('<div>Alice</div>'),
1351                 textNode = doc.root.contents()[0],
1352                 path = textNode.getPath();
1353
1354             textNode.setText('Alice ');
1355             textNode.setText('Alice h');
1356             textNode.setText('Alice ha');
1357             textNode.setText('Alice has');
1358
1359             expect(textNode.getText()).to.equal('Alice has');
1360
1361             doc.undo();
1362             expect(doc.root.contents()[0].getText()).to.equal('Alice ha', '1');
1363
1364             doc.undo();
1365             expect(doc.root.contents()[0].getText()).to.equal('Alice h', '2');
1366
1367             doc.redo();
1368             expect(doc.root.contents()[0].getText()).to.equal('Alice ha', '3');
1369
1370             doc.redo();
1371             expect(doc.root.contents()[0].getText()).to.equal('Alice has', '4');
1372
1373             doc.undo();
1374             doc.undo();
1375             textNode = doc.getNodeByPath(path);
1376             textNode.setText('Cat');
1377             doc.undo();
1378             textNode = doc.getNodeByPath(path);
1379             expect(textNode.getText()).to.equal('Alice h');
1380         });
1381
1382         
1383         var sampleMethod = function(val) {
1384             this._$.attr('x', val);
1385             this.triggerChangeEvent();
1386         };
1387
1388         var transformations = {
1389             'unaware': sampleMethod,
1390             'returning change root': {
1391                 impl: sampleMethod,
1392                 getChangeRoot: function() {
1393                     return this.context;
1394                 }
1395             },
1396             'implementing undo operation': {
1397                 impl: function(t, val) {
1398                     t.oldVal = this.getAttr('x');
1399                     sampleMethod.call(this, val);
1400                 },
1401                 undo: function(t) {
1402                     this.setAttr('x', t.oldVal);
1403                 }
1404             }
1405         };
1406
1407         _.pairs(transformations).forEach(function(pair) {
1408             var name = pair[0],
1409                 transformaton = pair[1];
1410
1411             describe(name + ' transformation: ', function() {
1412                 var doc, node, nodePath;
1413
1414                 beforeEach(function() {
1415                     doc = getDocumentFromXML('<div><test x="old"></test></div>');
1416
1417                     doc.registerExtension({elementNode: {transformations: {
1418                         test: transformaton
1419                     }}});
1420
1421                     node = doc.root.contents()[0];
1422                     nodePath = node.getPath();
1423                 });
1424
1425                 it('transforms as expected', function() {
1426                     node.test('new');
1427                     expect(node.getAttr('x')).to.equal('new');
1428                 });
1429
1430                 it('can be undone', function() {
1431                     node.test('new');
1432                     doc.undo();
1433                     node = doc.getNodeByPath(nodePath);
1434                     expect(node.getAttr('x')).to.equal('old');
1435                 });
1436
1437                 it('can be undone and then redone', function() {
1438                     node.test('new');
1439                     doc.undo();
1440                     doc.redo();
1441                     node = doc.getNodeByPath(nodePath);
1442                     expect(node.getAttr('x')).to.equal('new');
1443                 });
1444
1445                 it('handles a sample scenario', function() {
1446                     doc.root.contents()[0].test('1');
1447                     doc.root.contents()[0].test('2');
1448                     doc.root.contents()[0].test('3');
1449                     doc.root.contents()[0].test('4');
1450                     doc.root.contents()[0].test('5');
1451
1452                     expect(doc.root.contents()[0].getAttr('x')).to.equal('5', 'after initial transformations');
1453                     doc.undo();
1454                     expect(doc.root.contents()[0].getAttr('x')).to.equal('4', 'undo 1.1');
1455                     doc.undo();
1456                     expect(doc.root.contents()[0].getAttr('x')).to.equal('3', 'undo 1.2');
1457                     doc.redo();
1458                     expect(doc.root.contents()[0].getAttr('x')).to.equal('4', 'redo 1.1');
1459                     doc.redo();
1460                     expect(doc.root.contents()[0].getAttr('x')).to.equal('5', 'redo 1.2');
1461                     doc.undo();
1462                     expect(doc.root.contents()[0].getAttr('x')).to.equal('4', 'undo 2.1');
1463                     doc.root.contents()[0].test('10');
1464                     expect(doc.root.contents()[0].getAttr('x')).to.equal('10', 'additional transformation');
1465                     expect(doc.redoStack.length).to.equal(0, 'transformation cleared redo stack');
1466                     doc.redo();
1467                     expect(doc.root.contents()[0].getAttr('x')).to.equal('10', 'empty redoStack so redo was noop');
1468                     doc.undo();
1469                     expect(doc.root.contents()[0].getAttr('x')).to.equal('4', 'undoing additional transformation');
1470                     doc.redo();
1471                     expect(doc.root.contents()[0].getAttr('x')).to.equal('10', 'redoing additional transformation');
1472                 });
1473             });
1474         });
1475
1476         it('smoke tests nested transformations', function() {
1477             var doc = getDocumentFromXML('<div></div>');
1478
1479             doc.registerExtension({elementNode: {transformations: {
1480                 nested: function(v) {
1481                     this._$.attr('innerAttr', v);
1482                     this.triggerChangeEvent();
1483                 },
1484                 outer: function(v) {
1485                     this.nested(v);
1486                     this._$.attr('outerAttr', v);
1487                     this.triggerChangeEvent();
1488                 }
1489             }}});
1490
1491             doc.root.outer('test1');
1492             doc.root.outer('test2');
1493
1494             expect(doc.root.getAttr('innerAttr')).to.equal('test2');
1495             expect(doc.root.getAttr('outerAttr')).to.equal('test2');
1496
1497             doc.undo();
1498
1499             expect(doc.root.getAttr('innerAttr')).to.equal('test1');
1500             expect(doc.root.getAttr('outerAttr')).to.equal('test1');
1501
1502             doc.undo();
1503
1504             expect(doc.root.getAttr('innerAttr')).to.equal(undefined);
1505             expect(doc.root.getAttr('outerAttr')).to.equal(undefined);
1506
1507             doc.redo();
1508
1509             expect(doc.root.getAttr('innerAttr')).to.equal('test1');
1510             expect(doc.root.getAttr('outerAttr')).to.equal('test1');
1511
1512             doc.redo();
1513
1514             expect(doc.root.getAttr('innerAttr')).to.equal('test2');
1515             expect(doc.root.getAttr('outerAttr')).to.equal('test2');
1516
1517         });
1518
1519         it('ignores transformation if document didn\'t emit change event', function() {
1520             var doc = getDocumentFromXML('<div></div>');
1521
1522             doc.registerExtension({elementNode: {transformations: {
1523                 test: function() {
1524                     // empty
1525                 }
1526             }}});
1527
1528             doc.root.test();
1529             expect(doc.undoStack.length).to.equal(0);
1530
1531         });
1532
1533         describe('Transactions', function() {
1534             it('allows to undo/redo series of transformations at once', function() {
1535                 var doc = getDocumentFromXML('<div></div>');
1536
1537                 doc.registerExtension({
1538                     elementNode: {transformations: {
1539                         test: function(v) {
1540                             this.setAttr('test', v);
1541                         }
1542                     }}
1543                 });
1544
1545                 doc.startTransaction();
1546                 doc.root.test('1');
1547                 doc.root.test('2');
1548                 doc.root.test('3');
1549                 doc.endTransaction();
1550
1551                 doc.undo();
1552                 expect(doc.root.getAttr('test'), '1');
1553                 doc.redo();
1554                 expect(doc.root.getAttr('test'), '3');
1555                 doc.undo();
1556                 expect(doc.root.getAttr('test'), '1');
1557                 doc.redo();
1558                 expect(doc.root.getAttr('test'), '3');
1559             });
1560
1561             it('ignores empty transactions', function() {
1562                 var doc = getDocumentFromXML('<div></div>');
1563                 doc.startTransaction();
1564                 doc.endTransaction();
1565                 expect(doc.undoStack).to.have.length(0, 'empty transaction doesn\'t get pushed into undo stack');
1566             });
1567
1568             it('doesn\'t break on optimizations', function() {
1569                 // This is a smoke test checking if optimizations made to transaction undoing
1570                 // doesnt't break anything.
1571                 var doc = getDocumentFromXML('<div smart="1" unaware="1"></div>');
1572
1573                 doc.registerExtension({
1574                     elementNode: {transformations: {
1575                         unaware: function(v) {
1576                             this.setAttr('unware', v);
1577                             this.triggerChangeEvent();
1578                         },
1579                         smart: {
1580                             impl: function(t, v) {
1581                                 t.oldVal = this.getAttr('smart');
1582                                 this.setAttr('smart', v);
1583                                 this.triggerChangeEvent();
1584                             },
1585                             undo: function(t) {
1586                                 this.setAttr('smart', t.oldVal);
1587                                 this.triggerChangeEvent();
1588                             }
1589                         }
1590                     }}
1591                 });
1592
1593                 doc.startTransaction();
1594                 doc.root.smart('2');
1595                 doc.root.unaware('2');
1596                 doc.root.smart('3');
1597                 doc.root.unaware('3');
1598                 doc.endTransaction();
1599
1600                 doc.undo();
1601
1602                 expect(doc.root.getAttr('smart')).to.equal('1');
1603                 expect(doc.root.getAttr('unaware')).to.equal('1');
1604             });
1605         });
1606
1607         describe('Regression tests', function() {
1608             it('redos correctly after running its own undo followed by unaware transformation undo', function() {
1609                 var doc = getDocumentFromXML('<section t="0"></section>');
1610                 
1611                 doc.registerExtension({elementNode: {transformations: {
1612                     unaware: function() {
1613                         this.triggerChangeEvent();
1614                     },
1615                     test: {
1616                         impl: function() {
1617                             this._$.attr('t', 1);
1618                             this.triggerChangeEvent();
1619                         },
1620                         undo: function() {
1621                             this._$.attr('t', 0);
1622                         }
1623                     }
1624                 }}});
1625                 doc.root.unaware();
1626                 doc.root.test();
1627                 doc.undo();
1628                 doc.undo();
1629                 doc.redo();
1630                 doc.redo();
1631                 expect(doc.root.getAttr('t')).to.equal('1');
1632             });
1633         });
1634     });
1635
1636 });
1637
1638 });