smartxml: allow objects set with setData to clone themselves on node cloning
[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         it('wraps element node with another element node', function() {
462             var node = elementNodeFromXML('<div></div>'),
463                 wrapper = elementNodeFromXML('<wrapper></wrapper>');
464
465             node.wrapWith(wrapper);
466             expect(node.parent().sameNode(wrapper)).to.be.true;
467         });
468
469         it('unwraps element node contents', function() {
470             var node = elementNodeFromXML('<div>Alice <div>has <span>propably</span> a cat</div>!</div>'),
471                 outerDiv = node.contents()[1];
472             
473             outerDiv.unwrapContent();
474
475             expect(node.contents().length).to.equal(3);
476             expect(node.contents()[0].getText()).to.equal('Alice has ');
477             expect(node.contents()[1].getTagName()).to.equal('span');
478             expect(node.contents()[2].getText()).to.equal(' a cat!');
479         });
480
481         it('unwrap single element node from its parent', function() {
482             var doc = getDocumentFromXML('<div><a><b></b></a></div>'),
483                 div = doc.root,
484                 a = div.contents()[0],
485                 b = a.contents()[0];
486
487             var parent = b.unwrap();
488
489             expect(parent.sameNode(div)).to.equal(true, 'returns new parent');
490             expect(div.contents()).to.have.length(1, 'root contains only one node');
491             expect(div.contents()[0].sameNode(b)).to.equal(true, 'node got unwrapped');
492         });
493
494         it('unwrap single text node from its parent', function() {
495             var doc = getDocumentFromXML('<div>Some <span>text</span>!</div>'),
496                 div = doc.root,
497                 span = div.contents()[1],
498                 text = span.contents()[0];
499
500             var parent = text.unwrap();
501
502             expect(parent.sameNode(div)).to.equal(true, 'returns new parent');
503             expect(div.contents()).to.have.length(1, 'root contains only one node');
504             expect(div.contents()[0].getText()).to.equal('Some text!');
505         });
506
507         describe('Wrapping text', function() {
508             it('wraps text spanning multiple sibling TextNodes', function() {
509                 var section = elementNodeFromXML('<section>Alice has a <span>small</span> cat</section>'),
510                     wrapper = section.wrapText({
511                         _with: {tagName: 'span', attrs: {'attr1': 'value1'}},
512                         offsetStart: 6,
513                         offsetEnd: 4,
514                         textNodeIdx: [0,2]
515                     });
516
517                 expect(section.contents().length).to.equal(2);
518                 expect(section.contents()[0].nodeType).to.equal(Node.TEXT_NODE);
519                 expect(section.contents()[0].getText()).to.equal('Alice ');
520
521                 var wrapper2 = section.contents()[1];
522                 expect(wrapper2.sameNode(wrapper)).to.be.true;
523                 expect(wrapper.getTagName()).to.equal('span');
524
525                 var wrapperContents = wrapper.contents();
526                 expect(wrapperContents.length).to.equal(3);
527                 expect(wrapperContents[0].getText()).to.equal('has a ');
528
529                 expect(wrapperContents[1].nodeType).to.equal(Node.ELEMENT_NODE);
530                 expect(wrapperContents[1].contents().length).to.equal(1);
531                 expect(wrapperContents[1].contents()[0].getText()).to.equal('small');
532             });
533         });
534
535         describe('Wrapping Nodes', function() {
536             it('wraps multiple sibling nodes', function() {
537                 var section = elementNodeFromXML('<section>Alice<div>has</div><div>a cat</div></section>'),
538                     aliceText = section.contents()[0],
539                     firstDiv = section.contents()[1],
540                     lastDiv = section.contents()[section.contents().length -1];
541
542                 var returned = section.document.wrapNodes({
543                         node1: aliceText,
544                         node2: lastDiv,
545                         _with: {tagName: 'header'}
546                     });
547
548                 var sectionContentss = section.contents(),
549                     header = sectionContentss[0],
550                     headerContents = header.contents();
551
552                 expect(sectionContentss).to.have.length(1);
553                 expect(header.sameNode(returned)).to.equal(true, 'wrapper returned');
554                 expect(header.parent().sameNode(section)).to.be.true;
555                 expect(headerContents).to.have.length(3);
556                 expect(headerContents[0].sameNode(aliceText)).to.equal(true, 'first node wrapped');
557                 expect(headerContents[1].sameNode(firstDiv)).to.equal(true, 'second node wrapped');
558                 expect(headerContents[2].sameNode(lastDiv)).to.equal(true, 'third node wrapped');
559             });
560
561             it('wraps multiple sibling Elements - middle case', function() {
562                 var section = elementNodeFromXML('<section><div></div><div></div><div></div><div></div></section>'),
563                     div2 = section.contents()[1],
564                     div3 = section.contents()[2];
565
566                 section.document.wrapNodes({
567                         node1: div2,
568                         node2: div3,
569                         _with: {tagName: 'header'}
570                     });
571
572                 var sectionContentss = section.contents(),
573                     header = sectionContentss[1],
574                     headerChildren = header.contents();
575
576                 expect(sectionContentss).to.have.length(3);
577                 expect(headerChildren).to.have.length(2);
578                 expect(headerChildren[0].sameNode(div2)).to.equal(true, 'first node wrapped');
579                 expect(headerChildren[1].sameNode(div3)).to.equal(true, 'second node wrapped');
580             });
581         });
582
583     });
584
585     describe('Splitting text', function() {
586     
587         it('splits TextNode\'s parent into two ElementNodes', function() {
588             var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
589                 section = doc.root,
590                 text = section.contents()[0].contents()[0];
591
592             var returnedValue = text.split({offset: 5});
593             expect(section.contents().length).to.equal(2, 'section has two children');
594             
595             var header1 = section.contents()[0];
596             var header2 = section.contents()[1];
597
598             expect(header1.getTagName()).to.equal('header', 'first section child ok');
599             expect(header1.contents().length).to.equal(1, 'first header has one child');
600             expect(header1.contents()[0].getText()).to.equal('Some ', 'first header has correct content');
601             expect(header2.getTagName()).to.equal('header', 'second section child ok');
602             expect(header2.contents().length).to.equal(1, 'second header has one child');
603             expect(header2.contents()[0].getText()).to.equal('header', 'second header has correct content');
604
605             expect(returnedValue.first.sameNode(header1)).to.equal(true, 'first node returned');
606             expect(returnedValue.second.sameNode(header2)).to.equal(true, 'second node returned');
607         });
608
609         it('leaves empty copy of ElementNode if splitting at the very beginning', function() {
610                 var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
611                 section = doc.root,
612                 text = section.contents()[0].contents()[0];
613
614                 text.split({offset: 0});
615                 
616                 var header1 = section.contents()[0];
617                 var header2 = section.contents()[1];
618
619                 expect(header1.contents().length).to.equal(0);
620                 expect(header2.contents()[0].getText()).to.equal('Some header');
621         });
622
623         it('leaves empty copy of ElementNode if splitting at the very end', function() {
624                 var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
625                 section = doc.root,
626                 text = section.contents()[0].contents()[0];
627
628                 text.split({offset: 11});
629                 
630                 var header1 = section.contents()[0];
631                 var header2 = section.contents()[1];
632
633                 expect(header1.contents()[0].getText()).to.equal('Some header');
634                 expect(header2.contents().length).to.equal(0);
635         });
636
637         it('keeps TextNodes\'s parent\'s children elements intact', function() {
638             var doc = getDocumentFromXML('<section><header>A <span>fancy</span> and <span>nice</span> header</header></section>'),
639                 section = doc.root,
640                 header = section.contents()[0],
641                 textAnd = header.contents()[2];
642
643             textAnd.split({offset: 2});
644             
645             var sectionContents = section.contents();
646             expect(sectionContents.length).to.equal(2, 'Section has two children');
647             expect(sectionContents[0].getTagName()).to.equal('header', 'First section node is a header');
648             expect(sectionContents[1].getTagName()).to.equal('header', 'Second section node is a header');
649
650             var firstHeaderContents = sectionContents[0].contents();
651             expect(firstHeaderContents.length).to.equal(3, 'First header has three children');
652             expect(firstHeaderContents[0].getText()).to.equal('A ', 'First header starts with a text');
653             expect(firstHeaderContents[1].getTagName()).to.equal('span', 'First header has span in the middle');
654             expect(firstHeaderContents[2].getText()).to.equal(' a', 'First header ends with text');
655
656             var secondHeaderContents = sectionContents[1].contents();
657             expect(secondHeaderContents.length).to.equal(3, 'Second header has three children');
658             expect(secondHeaderContents[0].getText()).to.equal('nd ', 'Second header starts with text');
659             expect(secondHeaderContents[1].getTagName()).to.equal('span', 'Second header has span in the middle');
660             expect(secondHeaderContents[2].getText()).to.equal(' header', 'Second header ends with text');
661         });
662     });
663
664     describe('Events', function() {
665         it('emits nodeDetached event on node detach', function() {
666             var node = elementNodeFromXML('<div><div></div></div>'),
667                 innerNode = node.contents()[0],
668                 spy = sinon.spy();
669             node.document.on('change', spy);
670             
671             var detached = innerNode.detach(),
672                 event = spy.args[0][0];
673
674             expect(event.type).to.equal('nodeDetached');
675             expect(event.meta.node.sameNode(detached, 'detached node in event meta'));
676             expect(event.meta.parent.sameNode(node), 'original parent node in event meta');
677         }),
678
679         it('emits nodeAdded event when appending new node', function() {
680             var node = elementNodeFromXML('<div></div>'),
681                 spy = sinon.spy();
682             node.document.on('change', spy);
683             
684             var appended = node.append({tagName:'div'}),
685                 event = spy.args[0][0];
686             expect(event.type).to.equal('nodeAdded');
687             expect(event.meta.node.sameNode(appended)).to.be.true;
688         });
689         
690         it('emits nodeMoved when appending aready existing node', function() {
691             var node = elementNodeFromXML('<div><a></a><b></b></div>'),
692                 a = node.contents()[0],
693                 b = node.contents()[1],
694                 spy = sinon.spy();
695             node.document.on('change', spy);
696             
697             var appended = a.append(b),
698                 event = spy.args[0][0];
699
700             expect(spy.callCount).to.equal(1);
701             expect(event.type).to.equal('nodeMoved');
702             expect(event.meta.node.sameNode(appended)).to.be.true;
703         });
704         
705         it('emits nodeAdded event when prepending new node', function() {
706             var node = elementNodeFromXML('<div></div>'),
707                 spy = sinon.spy();
708             node.document.on('change', spy);
709             
710             var prepended = node.prepend({tagName:'div'}),
711                 event = spy.args[0][0];
712             expect(event.type).to.equal('nodeAdded');
713             expect(event.meta.node.sameNode(prepended)).to.be.true;
714         });
715         
716         it('emits nodeMoved when prepending aready existing node', function() {
717             var node = elementNodeFromXML('<div><a></a><b></b></div>'),
718                 a = node.contents()[0],
719                 b = node.contents()[1],
720                 spy = sinon.spy();
721             node.document.on('change', spy);
722             
723             var prepended = a.prepend(b),
724                 event = spy.args[0][0];
725             expect(spy.callCount).to.equal(1);
726             expect(event.type).to.equal('nodeMoved');
727             expect(event.meta.node.sameNode(prepended)).to.be.true;
728         });
729         
730         it('emits nodeAdded event when inserting node after another', function() {
731             var node = elementNodeFromXML('<div><a></a></div>').contents()[0],
732                 spy = sinon.spy();
733             node.document.on('change', spy);
734             
735             var inserted = node.after({tagName:'div'}),
736                 event = spy.args[0][0];
737             expect(event.type).to.equal('nodeAdded');
738             expect(event.meta.node.sameNode(inserted)).to.be.true;
739         });
740         
741         it('emits nodeMoved when inserting aready existing node after another', function() {
742             var node = elementNodeFromXML('<div><a></a><b></b></div>'),
743                 a = node.contents()[0],
744                 b = node.contents()[1],
745                 spy = sinon.spy();
746             node.document.on('change', spy);
747             var inserted = b.after(a),
748                 event = spy.args[0][0];
749
750             expect(spy.callCount).to.equal(1);
751             expect(event.type).to.equal('nodeMoved');
752             expect(event.meta.node.sameNode(inserted)).to.be.true;
753         });
754
755         it('emits nodeAdded event when inserting node before another', function() {
756             var node = elementNodeFromXML('<div><a></a></div>').contents()[0],
757                 spy = sinon.spy();
758             node.document.on('change', spy);
759             
760             var inserted = node.before({tagName:'div'}),
761                 event = spy.args[0][0];
762             expect(event.type).to.equal('nodeAdded');
763             expect(event.meta.node.sameNode(inserted)).to.be.true;
764         });
765         
766         it('emits nodeAdded when inserting aready existing node before another', function() {
767             var node = elementNodeFromXML('<div><a></a><b></b></div>'),
768                 a = node.contents()[0],
769                 b = node.contents()[1],
770                 spy = sinon.spy();
771             node.document.on('change', spy);
772             var inserted = a.before(b),
773                 event = spy.args[0][0];
774
775             expect(spy.callCount).to.equal(1);
776             expect(event.type).to.equal('nodeMoved');
777             expect(event.meta.node.sameNode(inserted)).to.be.true;
778         });
779
780         it('emits nodeDetached and nodeAdded when replacing root node with another', function() {
781             var doc = getDocumentFromXML('<a></a>'),
782                 oldRoot = doc.root,
783                 spy = sinon.spy();
784
785             doc.on('change', spy);
786
787             doc.root.replaceWith({tagName: 'b'});
788
789             expect(spy.callCount).to.equal(2);
790
791             var event1 = spy.args[0][0],
792                 event2 = spy.args[1][0];
793
794             expect(event1.type).to.equal('nodeDetached');
795             expect(event1.meta.node.sameNode(oldRoot)).to.equal(true, 'root node in nodeDetached event metadata');
796             expect(event2.type).to.equal('nodeAdded');
797             expect(event2.meta.node.sameNode(doc.root)).to.equal(true, 'new root node in nodelAdded event meta');
798         });
799
800
801         ['append', 'prepend', 'before', 'after'].forEach(function(insertionMethod) {
802             it('emits nodeDetached for node moved from a document tree to out of document node ' + insertionMethod, function() {
803                 var doc = getDocumentFromXML('<div><a></a></div>'),
804                     a = doc.root.contents()[0],
805                     spy = sinon.spy();
806
807                 doc.on('change', spy);
808
809                 var newNode = doc.createDocumentNode({tagName: 'b'}),
810                     newNodeInner = newNode.append({tagName:'c'});
811
812                 newNodeInner[insertionMethod](a);
813
814                 var event = spy.args[0][0];
815                 expect(event.type).to.equal('nodeDetached');
816                 expect(event.meta.node.sameNode(a));
817             });
818
819             it('doesn\'t emit nodeDetached event for already out of document node moved to out of document node' + insertionMethod, function() {
820                 var doc = getDocumentFromXML('<div><a></a></div>'),
821                     spy = sinon.spy();
822
823                 doc.on('change', spy);
824
825                 var newNode = doc.createDocumentNode({tagName: 'b'});
826                 newNode.append({tagName:'c'});
827
828                 expect(spy.callCount).to.equal(0);
829             });
830         });
831
832
833     });
834
835     describe('Traversing', function() {
836         describe('Basic', function() {
837             it('can access node parent', function() {
838                 var doc = getDocumentFromXML('<a><b></b></a>'),
839                     a = doc.root,
840                     b = a.contents()[0];
841
842                 expect(a.parent()).to.equal(null, 'parent of a root is null');
843                 expect(b.parent().sameNode(a)).to.be.true;
844             });
845             it('can access node parents', function() {
846                 var doc = getDocumentFromXML('<a><b><c></c></b></a>'),
847                     a = doc.root,
848                     b = a.contents()[0],
849                     c = b.contents()[0];
850
851                 var parents = c.parents();
852                 // @@
853                 expect(parents[0].sameNode(b)).to.be.true;
854                 expect(parents[1].sameNode(a)).to.be.true;
855             });
856         });
857
858         describe('finding sibling parents of two elements', function() {
859             it('returns elements themself if they have direct common parent', function() {
860                 var doc = getDocumentFromXML('<section><div><div>A</div><div>B</div></div></section>'),
861                     wrappingDiv = doc.root.contents()[0],
862                     divA = wrappingDiv.contents()[0],
863                     divB = wrappingDiv.contents()[1];
864
865                 var siblingParents = doc.getSiblingParents({node1: divA, node2: divB});
866
867                 expect(siblingParents.node1.sameNode(divA)).to.equal(true, 'divA');
868                 expect(siblingParents.node2.sameNode(divB)).to.equal(true, 'divB');
869             });
870
871             it('returns sibling parents - example 1', function() {
872                 var doc = getDocumentFromXML('<section>Alice <span>has a cat</span></section>'),
873                     aliceText = doc.root.contents()[0],
874                     span = doc.root.contents()[1],
875                     spanText = span.contents()[0];
876
877                 var siblingParents = doc.getSiblingParents({node1: aliceText, node2: spanText});
878
879                 expect(siblingParents.node1.sameNode(aliceText)).to.equal(true, 'aliceText');
880                 expect(siblingParents.node2.sameNode(span)).to.equal(true, 'span');
881             });
882         });
883     });
884
885     describe('Serializing document to WLXML', function() {
886         it('keeps document intact when no changes have been made', function() {
887             var xmlIn = '<section>Alice<div>has</div>a <span class="uri" meta-uri="http://cat.com">cat</span>!</section>',
888                 doc = getDocumentFromXML(xmlIn),
889                 xmlOut = doc.toXML();
890
891             var parser = new DOMParser(),
892                 input = parser.parseFromString(xmlIn, 'application/xml').childNodes[0],
893                 output = parser.parseFromString(xmlOut, 'application/xml').childNodes[0];
894             
895             expect(input.isEqualNode(output)).to.be.true;
896         });
897
898         it('keeps entities intact', function() {
899             var xmlIn = '<section>&lt; &gt;</section>',
900                 doc = getDocumentFromXML(xmlIn),
901                 xmlOut = doc.toXML();
902             expect(xmlOut).to.equal(xmlIn);
903         });
904         it('keeps entities intact when they form html/xml', function() {
905             var xmlIn = '<section>&lt;abc&gt;</section>',
906                 doc = getDocumentFromXML(xmlIn),
907                 xmlOut = doc.toXML();
908             expect(xmlOut).to.equal(xmlIn);
909         });
910     });
911
912     describe('Extension API', function() {
913         var doc, extension, elementNode, textNode;
914
915         beforeEach(function() {
916             doc = getDocumentFromXML('<section>Alice<div class="test_class"></div></section>');
917             elementNode = doc.root;
918             textNode = doc.root.contents()[0];
919             extension = {};
920             
921             expect(elementNode.testTransformation).to.be.undefined;
922             expect(textNode.testTransformation).to.be.undefined;
923             expect(doc.testTransformation).to.be.undefined;
924             
925             expect(doc.testMethod).to.be.undefined;
926             expect(elementNode.testMethod).to.be.undefined;
927             expect(textNode.testMethod).to.be.undefined;
928             expect(elementNode.elementTestMethod).to.be.undefined;
929             expect(textNode.textTestMethod).to.be.undefined;
930         });
931
932         it('allows adding method to a document', function() {
933             extension = {document: {methods: {
934                 testMethod: function() { return this; }
935             }}};
936
937             doc.registerExtension(extension);
938             expect(doc.testMethod()).to.equal(doc, 'context is set to a document instance');
939         });
940
941         it('allows adding transformation to a document', function() {
942             extension = {document: {transformations: {
943                 testTransformation: function() { return this; },
944                 testTransformation2: {impl: function() { return this;}}
945             }}};
946
947             doc.registerExtension(extension);
948             expect(doc.testTransformation()).to.equal(doc, 'context is set to a document instance');
949             expect(doc.testTransformation2()).to.equal(doc, 'context is set to a document instance');
950         });
951
952         it('allows adding method to a DocumentNode instance', function() {
953             extension = {
954                 documentNode: {
955                     methods: {
956                         testMethod: function() { return this; }
957                     }
958                 },
959                 textNode: {
960                     methods: {
961                         textTestMethod: function() { return this; }
962                     }
963                 },
964                 elementNode: {
965                     methods: {
966                         elementTestMethod: function() { return this; }
967                     }
968                 }
969             };
970
971             doc.registerExtension(extension);
972
973             /* refresh */
974             elementNode = doc.root;
975             textNode = doc.root.contents()[0];
976
977             expect(elementNode.testMethod().sameNode(elementNode)).to.equal(true, 'context is set to a node instance');
978             expect(textNode.testMethod().sameNode(textNode)).to.equal(true, 'context is set to a node instance');
979
980             expect(elementNode.elementTestMethod().sameNode(elementNode)).to.be.true;
981             expect(elementNode.textTestMethod).to.be.undefined;
982         
983             expect(textNode.textTestMethod().sameNode(textNode)).to.be.true;
984             expect(textNode.elementTestMethod).to.be.undefined;
985         });
986
987         it('allows adding transformation to a DocumentNode', function() {
988             extension = {
989                 documentNode: {
990                     transformations: {
991                         testTransformation: function() { return this; },
992                         testTransformation2: {impl: function() { return this;}}
993                     }
994                 },
995                 textNode: {
996                     transformations: {
997                         textTestTransformation: function() { return this; }
998                     }
999                 },
1000                 elementNode: {
1001                     transformations: {
1002                         elementTestTransformation: function() { return this; }
1003                     }
1004                 }
1005             };
1006             
1007             doc.registerExtension(extension);
1008
1009             /* refresh */
1010             elementNode = doc.root;
1011             textNode = doc.root.contents()[0];
1012             
1013             expect(elementNode.testTransformation().sameNode(elementNode)).to.equal(true, '1');
1014             expect(elementNode.testTransformation2().sameNode(elementNode)).to.equal(true, '2');
1015             expect(textNode.testTransformation().sameNode(textNode)).to.equal(true, '3');
1016             expect(textNode.testTransformation2().sameNode(textNode)).to.equal(true, '4');
1017
1018             expect(elementNode.elementTestTransformation().sameNode(elementNode)).to.be.true;
1019             expect(elementNode.textTestTransformation).to.be.undefined;
1020         
1021             expect(textNode.textTestTransformation().sameNode(textNode)).to.be.true;
1022             expect(textNode.elementTestTransfomation).to.be.undefined;
1023         });
1024
1025         it('allows text/element node methods and transformations to access node and transormations on document node', function() {
1026
1027             var doc = getDocumentFromXML('<div>text</div>');
1028
1029             doc.registerExtension({
1030                 documentNode: {
1031                     methods: {
1032                         test: function() {
1033                             return 'super';
1034                         }
1035                     },
1036                     transformations: {
1037                         testT: function() {
1038                             return 'super_trans';
1039                         }
1040                     }
1041                 },
1042                 elementNode: {
1043                     methods: {
1044                         test: function() {
1045                             return 'element_sub_' + this.__super__.test();
1046                         }
1047                     },
1048                     transformations: {
1049                         testT: function() {
1050                             return 'element_trans_sub_' + this.__super__.testT();
1051                         }
1052                     }
1053                 },
1054                 textNode: {
1055                     methods: {
1056                         test: function() {
1057                             return 'text_sub_' + this.__super__.test();
1058                         }
1059                     },
1060                     transformations: {
1061                         testT: function() {
1062                             return 'text_trans_sub_' + this.__super__.testT();
1063                         }
1064                     }
1065                 }
1066             });
1067
1068             var textNode = doc.root.contents()[0];
1069
1070             expect(doc.root.test()).to.equal('element_sub_super');
1071             expect(textNode.test()).to.equal('text_sub_super');
1072             expect(doc.root.testT()).to.equal('element_trans_sub_super_trans');
1073             expect(textNode.testT()).to.equal('text_trans_sub_super_trans');
1074         });
1075     });
1076
1077     describe('Undo/redo', function() {
1078
1079         it('smoke tests', function() {
1080             var doc = getDocumentFromXML('<div>Alice</div>'),
1081                 textNode = doc.root.contents()[0];
1082
1083             expect(doc.undoStack).to.have.length(0);
1084             
1085             textNode.wrapWith({tagName: 'span', start:1, end:2});
1086             expect(doc.undoStack).to.have.length(1, '1');
1087             expect(doc.toXML()).to.equal('<div>A<span>l</span>ice</div>');
1088
1089             doc.undo();
1090             expect(doc.undoStack).to.have.length(0, '2');
1091             expect(doc.toXML()).to.equal('<div>Alice</div>');
1092
1093             doc.redo();
1094             expect(doc.undoStack).to.have.length(1, '3');
1095             expect(doc.toXML()).to.equal('<div>A<span>l</span>ice</div>');
1096
1097             doc.undo();
1098             expect(doc.undoStack).to.have.length(0, '4');
1099             expect(doc.toXML()).to.equal('<div>Alice</div>');
1100
1101             doc.undo();
1102             expect(doc.undoStack).to.have.length(0, '5');
1103             expect(doc.toXML()).to.equal('<div>Alice</div>');
1104         });
1105
1106         it('smoke tests 2', function() {
1107             var doc = getDocumentFromXML('<div>Alice</div>'),
1108                 textNode = doc.root.contents()[0],
1109                 path = textNode.getPath();
1110
1111             textNode.setText('Alice ');
1112             textNode.setText('Alice h');
1113             textNode.setText('Alice ha');
1114             textNode.setText('Alice has');
1115
1116             expect(textNode.getText()).to.equal('Alice has');
1117
1118             doc.undo();
1119             expect(doc.root.contents()[0].getText()).to.equal('Alice ha', '1');
1120
1121             doc.undo();
1122             expect(doc.root.contents()[0].getText()).to.equal('Alice h', '2');
1123
1124             doc.redo();
1125             expect(doc.root.contents()[0].getText()).to.equal('Alice ha', '3');
1126
1127             doc.redo();
1128             expect(doc.root.contents()[0].getText()).to.equal('Alice has', '4');
1129
1130             doc.undo();
1131             doc.undo();
1132             textNode = doc.getNodeByPath(path);
1133             textNode.setText('Cat');
1134             doc.undo();
1135             textNode = doc.getNodeByPath(path);
1136             expect(textNode.getText()).to.equal('Alice h');
1137         });
1138
1139         
1140         var sampleMethod = function(val) {
1141             this._$.attr('x', val);
1142         };
1143
1144         var transformations = {
1145             'unaware': sampleMethod,
1146             'returning change root': {
1147                 impl: sampleMethod,
1148                 getChangeRoot: function() {
1149                     return this.context;
1150                 }
1151             },
1152             'implementing undo operation': {
1153                 impl: function(t, val) {
1154                     t.oldVal = this.getAttr('x');
1155                     sampleMethod.call(this, val);
1156                 },
1157                 undo: function(t) {
1158                     this.setAttr('x', t.oldVal);
1159                 }
1160             }
1161         };
1162
1163         _.pairs(transformations).forEach(function(pair) {
1164             var name = pair[0],
1165                 transformaton = pair[1];
1166
1167             describe(name + ' transformation: ', function() {
1168                 var doc, node, nodePath;
1169
1170                 beforeEach(function() {
1171                     doc = getDocumentFromXML('<div><test x="old"></test></div>');
1172
1173                     doc.registerExtension({elementNode: {transformations: {
1174                         test: transformaton
1175                     }}});
1176
1177                     node = doc.root.contents()[0];
1178                     nodePath = node.getPath();
1179                 });
1180
1181                 it('transforms as expected', function() {
1182                     node.test('new');
1183                     expect(node.getAttr('x')).to.equal('new');
1184                 });
1185
1186                 it('can be undone', function() {
1187                     node.test('new');
1188                     doc.undo();
1189                     node = doc.getNodeByPath(nodePath);
1190                     expect(node.getAttr('x')).to.equal('old');
1191                 });
1192
1193                 it('can be undone and then redone', function() {
1194                     node.test('new');
1195                     doc.undo();
1196                     doc.redo();
1197                     node = doc.getNodeByPath(nodePath);
1198                     expect(node.getAttr('x')).to.equal('new');
1199                 });
1200
1201                 it('handles a sample scenario', function() {
1202                     doc.root.contents()[0].test('1');
1203                     doc.root.contents()[0].test('2');
1204                     doc.root.contents()[0].test('3');
1205                     doc.root.contents()[0].test('4');
1206                     doc.root.contents()[0].test('5');
1207
1208                     expect(doc.root.contents()[0].getAttr('x')).to.equal('5', 'after initial transformations');
1209                     doc.undo();
1210                     expect(doc.root.contents()[0].getAttr('x')).to.equal('4', 'undo 1.1');
1211                     doc.undo();
1212                     expect(doc.root.contents()[0].getAttr('x')).to.equal('3', 'undo 1.2');
1213                     doc.redo();
1214                     expect(doc.root.contents()[0].getAttr('x')).to.equal('4', 'redo 1.1');
1215                     doc.redo();
1216                     expect(doc.root.contents()[0].getAttr('x')).to.equal('5', 'redo 1.2');
1217                     doc.undo();
1218                     expect(doc.root.contents()[0].getAttr('x')).to.equal('4', 'undo 2.1');
1219                     doc.root.contents()[0].test('10');
1220                     expect(doc.root.contents()[0].getAttr('x')).to.equal('10', 'additional transformation');
1221                     expect(doc.redoStack.length).to.equal(0, 'transformation cleared redo stack');
1222                     doc.redo();
1223                     expect(doc.root.contents()[0].getAttr('x')).to.equal('10', 'empty redoStack so redo was noop');
1224                     doc.undo();
1225                     expect(doc.root.contents()[0].getAttr('x')).to.equal('4', 'undoing additional transformation');
1226                     doc.redo();
1227                     expect(doc.root.contents()[0].getAttr('x')).to.equal('10', 'redoing additional transformation');
1228                 });
1229             });
1230         });
1231
1232         it('smoke tests nested transformations', function() {
1233             var doc = getDocumentFromXML('<div></div>');
1234
1235             doc.registerExtension({elementNode: {transformations: {
1236                 nested: function(v) {
1237                     this._$.attr('innerAttr', v);
1238                 },
1239                 outer: function(v) {
1240                     this.nested(v);
1241                     this._$.attr('outerAttr', v);
1242                 }
1243             }}});
1244
1245             doc.root.outer('test1');
1246             doc.root.outer('test2');
1247
1248             expect(doc.root.getAttr('innerAttr')).to.equal('test2');
1249             expect(doc.root.getAttr('outerAttr')).to.equal('test2');
1250
1251             doc.undo();
1252
1253             expect(doc.root.getAttr('innerAttr')).to.equal('test1');
1254             expect(doc.root.getAttr('outerAttr')).to.equal('test1');
1255
1256             doc.undo();
1257
1258             expect(doc.root.getAttr('innerAttr')).to.equal(undefined);
1259             expect(doc.root.getAttr('outerAttr')).to.equal(undefined);
1260
1261             doc.redo();
1262
1263             expect(doc.root.getAttr('innerAttr')).to.equal('test1');
1264             expect(doc.root.getAttr('outerAttr')).to.equal('test1');
1265
1266             doc.redo();
1267
1268             expect(doc.root.getAttr('innerAttr')).to.equal('test2');
1269             expect(doc.root.getAttr('outerAttr')).to.equal('test2');
1270
1271         });
1272
1273         describe('Transactions', function() {
1274             it('allows to undo/redo series of transformations at once', function() {
1275                 var doc = getDocumentFromXML('<div></div>');
1276
1277                 doc.registerExtension({
1278                     elementNode: {transformations: {
1279                         test: function(v) {
1280                             this.setAttr('test', v);
1281                         }
1282                     }}
1283                 });
1284
1285                 doc.startTransaction();
1286                 doc.root.test('1');
1287                 doc.root.test('2');
1288                 doc.root.test('3');
1289                 doc.endTransaction();
1290
1291                 doc.undo();
1292                 expect(doc.root.getAttr('test'), '1');
1293                 doc.redo();
1294                 expect(doc.root.getAttr('test'), '3');
1295                 doc.undo();
1296                 expect(doc.root.getAttr('test'), '1');
1297                 doc.redo();
1298                 expect(doc.root.getAttr('test'), '3');
1299             });
1300
1301             it('ignores empty transactions', function() {
1302                 var doc = getDocumentFromXML('<div></div>');
1303                 doc.startTransaction();
1304                 doc.endTransaction();
1305                 expect(doc.undoStack).to.have.length(0, 'empty transaction doesn\'t get pushed into undo stack');
1306             });
1307
1308             it('doesn\'t break on optimizations', function() {
1309                 // This is a smoke test checking if optimizations made to transaction undoing
1310                 // doesnt't break anything.
1311                 var doc = getDocumentFromXML('<div smart="1" unaware="1"></div>');
1312
1313                 doc.registerExtension({
1314                     elementNode: {transformations: {
1315                         unaware: function(v) {
1316                             this.setAttr('unware', v);
1317                         },
1318                         smart: {
1319                             impl: function(t, v) {
1320                                 t.oldVal = this.getAttr('smart');
1321                                 this.setAttr('smart', v);
1322                             },
1323                             undo: function(t) {
1324                                 this.setAttr('smart', t.oldVal);
1325                             }
1326                         }
1327                     }}
1328                 });
1329
1330                 doc.startTransaction();
1331                 doc.root.smart('2');
1332                 doc.root.unaware('2');
1333                 doc.root.smart('3');
1334                 doc.root.unaware('3');
1335                 doc.endTransaction();
1336
1337                 doc.undo();
1338
1339                 expect(doc.root.getAttr('smart')).to.equal('1');
1340                 expect(doc.root.getAttr('unaware')).to.equal('1');
1341             });
1342         });
1343     });
1344
1345 });
1346
1347 });