smartxml: transactions fix - ignore empty transactions
[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('knows its path in the document tree', function() {
90             var doc = getDocumentFromXML('<root><a><b><c></c>text</b></a></root>'),
91                 root = doc.root,
92                 a = root.contents()[0],
93                 b = a.contents()[0],
94                 text = b.contents()[1];
95
96             expect(root.getPath()).to.eql([], 'path of the root element is empty');
97             expect(a.getPath()).to.eql([0]);
98             expect(b.getPath()).to.eql([0, 0]);
99             expect(text.getPath()).to.eql([0,0,1]);
100
101             /* Paths relative to a given ancestor */
102             expect(text.getPath(root)).to.eql([0,0,1]);
103             expect(text.getPath(a)).to.eql([0,1]);
104             expect(text.getPath(b)).to.eql([1]);
105         });
106     });
107
108     describe('Basic ElementNode properties', function() {
109         it('exposes node contents', function() {
110             var node = elementNodeFromXML('<node>Some<node>text</node>is here</node>'),
111                 contents = node.contents();
112
113             expect(contents).to.have.length(3);
114             expect(contents[0].nodeType).to.equal(Node.TEXT_NODE, 'text node 1');
115             expect(contents[1].nodeType).to.equal(Node.ELEMENT_NODE, 'element node 1');
116             expect(contents[2].nodeType).to.equal(Node.TEXT_NODE, 'text node 2');
117         });
118
119         describe('Storing custom data', function() {
120             var node;
121
122             beforeEach(function() {
123                 node = elementNodeFromXML('<div></div>');
124             });
125
126             it('can append single value', function() {
127                 node.setData('key', 'value');
128                 expect(node.getData('key')).to.equal('value');
129             });
130
131             it('can overwrite the whole data', function() {
132                 node.setData('key1', 'value1');
133                 node.setData({key2: 'value2'});
134                 expect(node.getData('key2')).to.equal('value2');
135             });
136
137             it('can fetch the whole data at once', function() {
138                 node.setData({key1: 'value1', key2: 'value2'});
139                 expect(node.getData()).to.eql({key1: 'value1', key2: 'value2'});
140             });
141         });
142
143         describe('Changing node tag', function() {
144
145             it('can change tag name', function() {
146                 var node = elementNodeFromXML('<div></div>');
147                 node.setTag('span');
148                 expect(node.getTagName()).to.equal('span');
149             });
150
151             it('emits nodeTagChange event', function() {
152                 var node = elementNodeFromXML('<div></div>'),
153                     spy = sinon.spy();
154
155                 node.document.on('change', spy);
156                 node.setTag('span');
157                 var event = spy.args[0][0];
158
159                 expect(event.type).to.equal('nodeTagChange');
160                 expect(event.meta.node.sameNode(node)).to.be.true;
161                 expect(event.meta.oldTagName).to.equal('div');
162             });
163
164             describe('Implementation specific expectations', function() {
165                 // DOM specifies ElementNode tag as a read-only property, so
166                 // changing it in a seamless way is a little bit tricky. For this reason
167                 // the folowing expectations are required, despite the fact that they actually are
168                 // motivated by implemetation details.
169
170                 it('keeps node in the document', function() {
171                     var doc = getDocumentFromXML('<div><header></header></div>'),
172                         header = doc.root.contents()[0];
173                     header.setTag('span');
174                     expect(header.parent().sameNode(doc.root)).to.be.true;
175                 });
176                 it('keeps custom data', function() {
177                     var node = elementNodeFromXML('<div></div>');
178
179                     node.setData('key', 'value');
180                     node.setTag('header');
181                     
182                     expect(node.getTagName()).to.equal('header');
183                     expect(node.getData()).to.eql({key: 'value'});
184                 });
185
186                 it('can change document root tag name', function() {
187                     var doc = getDocumentFromXML('<div></div>');
188                     doc.root.setTag('span');
189                     expect(doc.root.getTagName()).to.equal('span');
190                 });
191
192                 it('keeps contents', function() {
193                     var node = elementNodeFromXML('<div><div></div></div>');
194                     node.setTag('header');
195                     expect(node.contents()).to.have.length(1);
196                 });
197             });
198
199         describe('Setting node attributes', function() {
200             it('can set node attribute', function() {
201                 var node = elementNodeFromXML('<div></div>');
202
203                 node.setAttr('key', 'value');
204                 expect(node.getAttr('key')).to.equal('value');
205             });
206             it('emits nodeAttrChange event', function() {
207                 var node = elementNodeFromXML('<div key="value1"></div>'),
208                     spy = sinon.spy();
209
210                 node.document.on('change', spy);
211                 node.setAttr('key', 'value2');
212                 var event = spy.args[0][0];
213
214                 expect(event.type).to.equal('nodeAttrChange');
215                 expect(event.meta.node.sameNode(node)).to.be.true;
216                 expect(event.meta.attr).to.equal('key');
217                 expect(event.meta.oldVal).to.equal('value1');
218             });
219         });
220
221         });
222     });
223
224     describe('Basic TextNode properties', function() {
225         it('can have its text set', function() {
226             var node = elementNodeFromXML('<div>Alice</div>'),
227                 textNode = node.contents()[0];
228
229             textNode.setText('Cat');
230             expect(textNode.getText()).to.equal('Cat');
231         });
232
233         it('emits nodeTextChange', function() {
234             var node = elementNodeFromXML('<div>Alice</div>'),
235                 textNode = node.contents()[0],
236                 spy = sinon.spy();
237
238             textNode.document.on('change', spy);
239             textNode.setText('Cat');
240
241             var event = spy.args[0][0];
242             expect(event.type).to.equal('nodeTextChange');
243         });
244
245         it('puts NodeElement after itself', function() {
246             var node = elementNodeFromXML('<div>Alice</div>'),
247                 textNode = node.contents()[0],
248                 returned = textNode.after({tagName:'div'});
249             expect(returned.sameNode(node.contents()[1])).to.be.true;
250         });
251
252         it('puts NodeElement before itself', function() {
253             var node = elementNodeFromXML('<div>Alice</div>'),
254                 textNode = node.contents()[0],
255                 returned = textNode.before({tagName:'div'});
256             expect(returned.sameNode(node.contents()[0])).to.be.true;
257         });
258
259         describe('Wrapping TextNode contents', function() {
260
261             it('wraps DocumentTextElement', function() {
262                 var node = elementNodeFromXML('<section>Alice</section>'),
263                     textNode = node.contents()[0];
264                 
265                 var returned = textNode.wrapWith({tagName: 'header'}),
266                     parent = textNode.parent(),
267                     parent2 = node.contents()[0];
268
269                 expect(returned.sameNode(parent)).to.be.equal(true, 'wrapper is a parent');
270                 expect(returned.sameNode(parent2)).to.be.equal(true, 'wrapper has a correct parent');
271                 expect(returned.getTagName()).to.equal('header');
272             });
273
274             describe('wrapping part of DocumentTextElement', function() {
275                 [{start: 5, end: 12}, {start: 12, end: 5}].forEach(function(offsets) {
276                     it('wraps in the middle ' + offsets.start + '/' + offsets.end, function() {
277                         var node = elementNodeFromXML('<section>Alice has a cat</section>'),
278                             textNode = node.contents()[0];
279                         
280                         var returned = textNode.wrapWith({tagName: 'header', attrs: {'attr1': 'value1'}, start: offsets.start, end: offsets.end}),
281                             contents = node.contents();
282
283                         expect(contents.length).to.equal(3);
284                         
285                         expect(contents[0].nodeType).to.be.equal(Node.TEXT_NODE, 'first node is text node');
286                         expect(contents[0].getText()).to.equal('Alice');
287
288                         expect(contents[1].sameNode(returned)).to.be.true;
289                         expect(returned.getTagName()).to.equal('header');
290                         expect(returned.getAttr('attr1')).to.equal('value1');
291                         expect(contents[1].contents().length).to.equal(1, 'wrapper has one node inside');
292                         expect(contents[1].contents()[0].getText()).to.equal(' has a ');
293
294                         expect(contents[2].nodeType).to.be.equal(Node.TEXT_NODE, 'third node is text node');
295                         expect(contents[2].getText()).to.equal('cat');
296                     });
297                 });
298
299                 it('wraps whole text inside DocumentTextElement if offsets span entire content', function() {
300                     var node = elementNodeFromXML('<section>Alice has a cat</section>'),
301                          textNode = node.contents()[0];
302                      
303                     textNode.wrapWith({tagName: 'header', start: 0, end: 15});
304                     
305                     var contents = node.contents();
306                     expect(contents.length).to.equal(1);
307                     expect(contents[0].getTagName()).to.equal('header');
308                     expect(contents[0].contents()[0].getText()).to.equal('Alice has a cat');
309                 });
310             });
311         });
312
313         describe('Dividing text node into two with element node', function() {
314                 it('can divide text node with element node, splitting text node into two', function() {
315                     var doc = getDocumentFromXML('<div>Alice has a cat</div>'),
316                         text = doc.root.contents()[0];
317
318                     var returned = text.divideWithElementNode({tagName: 'aside'}, {offset: 5}),
319                         contents = doc.root.contents(),
320                         lhsText = contents[0],
321                         rhsText = contents[2];
322
323                     expect(lhsText.getText()).to.equal('Alice');
324                     expect(returned.sameNode(contents[1]));
325                     expect(rhsText.getText()).to.equal(' has a cat');
326                 });
327
328                 it('treats dividing at the very end as appending after it', function() {
329                     var doc = getDocumentFromXML('<div>Alice has a cat</div>'),
330                         text = doc.root.contents()[0];
331
332
333                     var returned = text.divideWithElementNode({tagName: 'aside'}, {offset: 15}),
334                         contents = doc.root.contents(),
335                         textNode = contents[0],
336                         elementNode = contents[1];
337
338                     expect(contents.length).to.equal(2);
339                     expect(textNode.getText()).to.equal('Alice has a cat');
340                     expect(returned.sameNode(elementNode)).to.be.true;
341                     expect(elementNode.getTagName()).to.equal('aside');
342                 });
343
344                 it('treats dividing at the very beginning as prepending before it', function() {
345                     var doc = getDocumentFromXML('<div>Alice has a cat</div>'),
346                         text = doc.root.contents()[0];
347
348                     var returned = text.divideWithElementNode({tagName: 'aside'}, {offset: 0}),
349                         contents = doc.root.contents(),
350                         textNode = contents[1],
351                         elementNode = contents[0];
352
353                     expect(contents.length).to.equal(2);
354                     expect(textNode.getText()).to.equal('Alice has a cat');
355                     expect(returned.sameNode(elementNode)).to.be.true;
356                     expect(elementNode.getTagName()).to.equal('aside');
357                 });
358         });
359     });
360
361     describe('Manipulations', function() {
362
363         describe('replacing node with another one', function() {
364             it('replaces node with another one', function() {
365                 var doc = getDocumentFromXML('<div><a></a></div>'),
366                     a = doc.root.contents()[0];
367
368                 var c = a.replaceWith({tagName: 'b', attrs: {b:'1'}});
369
370                 expect(doc.root.contents()[0].sameNode(c));
371                 expect(c.getTagName()).to.equal('b');
372                 expect(c.getAttr('b')).to.equal('1');
373             });
374             it('can replace document root', function() {
375                 var doc = getDocumentFromXML('<div></div>');
376
377                 var header = doc.root.replaceWith({tagName: 'header'});
378
379                 expect(doc.root.sameNode(header)).to.be.true;
380                 expect(doc.containsNode(header)).to.be.true;
381             });
382         });
383
384         it('merges adjacent text nodes resulting from detaching an element node in between', function() {
385             var doc = getDocumentFromXML('<div>Alice <span>has</span>a cat</div>'),
386                 span = doc.root.contents()[1];
387
388             span.detach();
389
390             var rootContents = doc.root.contents();
391             expect(rootContents).to.have.length(1, 'one child left');
392             expect(rootContents[0].getText()).to.equal('Alice a cat');
393         });
394
395         it('inserts node at index', function() {
396             var doc = getDocumentFromXML('<div><a></a><b></b><c></c></div>'),
397                 b = doc.root.contents()[1];
398
399             var inserted = doc.root.insertAtIndex({tagName: 'test'}, 1);
400
401             expect(doc.root.contents()[1].sameNode(inserted)).to.equal(true, 'inserted node returned');
402             expect(b.getIndex()).to.equal(2, 'b node shifted right');
403         });
404
405         it('appends node when inserting node at index out of range', function() {
406             var doc = getDocumentFromXML('<div></div>');
407
408             var test1 = doc.root.insertAtIndex({tagName: 'test1'}, 0),
409                 test2 = doc.root.insertAtIndex({tagName: 'test1'}, 10);
410
411             expect(doc.root.contents()[0].sameNode(test1)).to.equal(true, 'inserting at index 0 of empty nodes appends node');
412             expect(doc.root.contents().length).to.equal(1, 'inserting at index out of range does nothing');
413             expect(test2).to.equal(undefined, 'inserting at index out of range returns undefined');
414         });
415
416         it('appends element node to another element node', function() {
417             var node1 = elementNodeFromParams({tag: 'div'}),
418                 node2 = elementNodeFromParams({tag: 'a'}),
419                 node3 = elementNodeFromParams({tag: 'p'});
420             node1.append(node2);
421             node1.append(node3);
422             expect(node1.contents()[0].sameNode(node2)).to.be.true;
423             expect(node1.contents()[1].sameNode(node3)).to.be.true;
424         });
425
426         it('prepends element node to another element node', function() {
427             var node1 = elementNodeFromParams({tag: 'div'}),
428                 node2 = elementNodeFromParams({tag: 'a'}),
429                 node3 = elementNodeFromParams({tag: 'p'});
430             node1.prepend(node2);
431             node1.prepend(node3);
432             expect(node1.contents()[0].sameNode(node3)).to.be.true;
433             expect(node1.contents()[1].sameNode(node2)).to.be.true;
434         });
435
436         it('wraps element node with another element node', function() {
437             var node = elementNodeFromXML('<div></div>'),
438                 wrapper = elementNodeFromXML('<wrapper></wrapper>');
439
440             node.wrapWith(wrapper);
441             expect(node.parent().sameNode(wrapper)).to.be.true;
442         });
443
444         it('unwraps element node contents', function() {
445             var node = elementNodeFromXML('<div>Alice <div>has <span>propably</span> a cat</div>!</div>'),
446                 outerDiv = node.contents()[1];
447             
448             outerDiv.unwrapContent();
449
450             expect(node.contents().length).to.equal(3);
451             expect(node.contents()[0].getText()).to.equal('Alice has ');
452             expect(node.contents()[1].getTagName()).to.equal('span');
453             expect(node.contents()[2].getText()).to.equal(' a cat!');
454         });
455
456         it('unwrap single element node from its parent', function() {
457             var doc = getDocumentFromXML('<div><a><b></b></a></div>'),
458                 div = doc.root,
459                 a = div.contents()[0],
460                 b = a.contents()[0];
461
462             var parent = b.unwrap();
463
464             expect(parent.sameNode(div)).to.equal(true, 'returns new parent');
465             expect(div.contents()).to.have.length(1, 'root contains only one node');
466             expect(div.contents()[0].sameNode(b)).to.equal(true, 'node got unwrapped');
467         });
468
469         it('unwrap single text node from its parent', function() {
470             var doc = getDocumentFromXML('<div>Some <span>text</span>!</div>'),
471                 div = doc.root,
472                 span = div.contents()[1],
473                 text = span.contents()[0];
474
475             var parent = text.unwrap();
476
477             expect(parent.sameNode(div)).to.equal(true, 'returns new parent');
478             expect(div.contents()).to.have.length(1, 'root contains only one node');
479             expect(div.contents()[0].getText()).to.equal('Some text!');
480         });
481
482         describe('Wrapping text', function() {
483             it('wraps text spanning multiple sibling TextNodes', function() {
484                 var section = elementNodeFromXML('<section>Alice has a <span>small</span> cat</section>'),
485                     wrapper = section.wrapText({
486                         _with: {tagName: 'span', attrs: {'attr1': 'value1'}},
487                         offsetStart: 6,
488                         offsetEnd: 4,
489                         textNodeIdx: [0,2]
490                     });
491
492                 expect(section.contents().length).to.equal(2);
493                 expect(section.contents()[0].nodeType).to.equal(Node.TEXT_NODE);
494                 expect(section.contents()[0].getText()).to.equal('Alice ');
495
496                 var wrapper2 = section.contents()[1];
497                 expect(wrapper2.sameNode(wrapper)).to.be.true;
498                 expect(wrapper.getTagName()).to.equal('span');
499
500                 var wrapperContents = wrapper.contents();
501                 expect(wrapperContents.length).to.equal(3);
502                 expect(wrapperContents[0].getText()).to.equal('has a ');
503
504                 expect(wrapperContents[1].nodeType).to.equal(Node.ELEMENT_NODE);
505                 expect(wrapperContents[1].contents().length).to.equal(1);
506                 expect(wrapperContents[1].contents()[0].getText()).to.equal('small');
507             });
508         });
509
510         describe('Wrapping Nodes', function() {
511             it('wraps multiple sibling nodes', function() {
512                 var section = elementNodeFromXML('<section>Alice<div>has</div><div>a cat</div></section>'),
513                     aliceText = section.contents()[0],
514                     firstDiv = section.contents()[1],
515                     lastDiv = section.contents()[section.contents().length -1];
516
517                 var returned = section.document.wrapNodes({
518                         node1: aliceText,
519                         node2: lastDiv,
520                         _with: {tagName: 'header'}
521                     });
522
523                 var sectionContentss = section.contents(),
524                     header = sectionContentss[0],
525                     headerContents = header.contents();
526
527                 expect(sectionContentss).to.have.length(1);
528                 expect(header.sameNode(returned)).to.equal(true, 'wrapper returned');
529                 expect(header.parent().sameNode(section)).to.be.true;
530                 expect(headerContents).to.have.length(3);
531                 expect(headerContents[0].sameNode(aliceText)).to.equal(true, 'first node wrapped');
532                 expect(headerContents[1].sameNode(firstDiv)).to.equal(true, 'second node wrapped');
533                 expect(headerContents[2].sameNode(lastDiv)).to.equal(true, 'third node wrapped');
534             });
535
536             it('wraps multiple sibling Elements - middle case', function() {
537                 var section = elementNodeFromXML('<section><div></div><div></div><div></div><div></div></section>'),
538                     div2 = section.contents()[1],
539                     div3 = section.contents()[2];
540
541                 section.document.wrapNodes({
542                         node1: div2,
543                         node2: div3,
544                         _with: {tagName: 'header'}
545                     });
546
547                 var sectionContentss = section.contents(),
548                     header = sectionContentss[1],
549                     headerChildren = header.contents();
550
551                 expect(sectionContentss).to.have.length(3);
552                 expect(headerChildren).to.have.length(2);
553                 expect(headerChildren[0].sameNode(div2)).to.equal(true, 'first node wrapped');
554                 expect(headerChildren[1].sameNode(div3)).to.equal(true, 'second node wrapped');
555             });
556         });
557
558     });
559
560     describe('Splitting text', function() {
561     
562         it('splits TextNode\'s parent into two ElementNodes', function() {
563             var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
564                 section = doc.root,
565                 text = section.contents()[0].contents()[0];
566
567             var returnedValue = text.split({offset: 5});
568             expect(section.contents().length).to.equal(2, 'section has two children');
569             
570             var header1 = section.contents()[0];
571             var header2 = section.contents()[1];
572
573             expect(header1.getTagName()).to.equal('header', 'first section child ok');
574             expect(header1.contents().length).to.equal(1, 'first header has one child');
575             expect(header1.contents()[0].getText()).to.equal('Some ', 'first header has correct content');
576             expect(header2.getTagName()).to.equal('header', 'second section child ok');
577             expect(header2.contents().length).to.equal(1, 'second header has one child');
578             expect(header2.contents()[0].getText()).to.equal('header', 'second header has correct content');
579
580             expect(returnedValue.first.sameNode(header1)).to.equal(true, 'first node returned');
581             expect(returnedValue.second.sameNode(header2)).to.equal(true, 'second node returned');
582         });
583
584         it('leaves empty copy of ElementNode if splitting at the very beginning', function() {
585                 var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
586                 section = doc.root,
587                 text = section.contents()[0].contents()[0];
588
589                 text.split({offset: 0});
590                 
591                 var header1 = section.contents()[0];
592                 var header2 = section.contents()[1];
593
594                 expect(header1.contents().length).to.equal(0);
595                 expect(header2.contents()[0].getText()).to.equal('Some header');
596         });
597
598         it('leaves empty copy of ElementNode if splitting at the very end', function() {
599                 var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
600                 section = doc.root,
601                 text = section.contents()[0].contents()[0];
602
603                 text.split({offset: 11});
604                 
605                 var header1 = section.contents()[0];
606                 var header2 = section.contents()[1];
607
608                 expect(header1.contents()[0].getText()).to.equal('Some header');
609                 expect(header2.contents().length).to.equal(0);
610         });
611
612         it('keeps TextNodes\'s parent\'s children elements intact', function() {
613             var doc = getDocumentFromXML('<section><header>A <span>fancy</span> and <span>nice</span> header</header></section>'),
614                 section = doc.root,
615                 header = section.contents()[0],
616                 textAnd = header.contents()[2];
617
618             textAnd.split({offset: 2});
619             
620             var sectionContents = section.contents();
621             expect(sectionContents.length).to.equal(2, 'Section has two children');
622             expect(sectionContents[0].getTagName()).to.equal('header', 'First section node is a header');
623             expect(sectionContents[1].getTagName()).to.equal('header', 'Second section node is a header');
624
625             var firstHeaderContents = sectionContents[0].contents();
626             expect(firstHeaderContents.length).to.equal(3, 'First header has three children');
627             expect(firstHeaderContents[0].getText()).to.equal('A ', 'First header starts with a text');
628             expect(firstHeaderContents[1].getTagName()).to.equal('span', 'First header has span in the middle');
629             expect(firstHeaderContents[2].getText()).to.equal(' a', 'First header ends with text');
630
631             var secondHeaderContents = sectionContents[1].contents();
632             expect(secondHeaderContents.length).to.equal(3, 'Second header has three children');
633             expect(secondHeaderContents[0].getText()).to.equal('nd ', 'Second header starts with text');
634             expect(secondHeaderContents[1].getTagName()).to.equal('span', 'Second header has span in the middle');
635             expect(secondHeaderContents[2].getText()).to.equal(' header', 'Second header ends with text');
636         });
637     });
638
639     describe('Events', function() {
640         it('emits nodeDetached event on node detach', function() {
641             var node = elementNodeFromXML('<div><div></div></div>'),
642                 innerNode = node.contents()[0],
643                 spy = sinon.spy();
644             node.document.on('change', spy);
645             
646             var detached = innerNode.detach(),
647                 event = spy.args[0][0];
648
649             expect(event.type).to.equal('nodeDetached');
650             expect(event.meta.node.sameNode(detached, 'detached node in event meta'));
651             expect(event.meta.parent.sameNode(node), 'original parent node in event meta');
652         }),
653
654         it('emits nodeAdded event when appending new node', function() {
655             var node = elementNodeFromXML('<div></div>'),
656                 spy = sinon.spy();
657             node.document.on('change', spy);
658             
659             var appended = node.append({tagName:'div'}),
660                 event = spy.args[0][0];
661             expect(event.type).to.equal('nodeAdded');
662             expect(event.meta.node.sameNode(appended)).to.be.true;
663         });
664         
665         it('emits nodeMoved when appending aready existing node', function() {
666             var node = elementNodeFromXML('<div><a></a><b></b></div>'),
667                 a = node.contents()[0],
668                 b = node.contents()[1],
669                 spy = sinon.spy();
670             node.document.on('change', spy);
671             
672             var appended = a.append(b),
673                 event = spy.args[0][0];
674
675             expect(spy.callCount).to.equal(1);
676             expect(event.type).to.equal('nodeMoved');
677             expect(event.meta.node.sameNode(appended)).to.be.true;
678         });
679         
680         it('emits nodeAdded event when prepending new node', function() {
681             var node = elementNodeFromXML('<div></div>'),
682                 spy = sinon.spy();
683             node.document.on('change', spy);
684             
685             var prepended = node.prepend({tagName:'div'}),
686                 event = spy.args[0][0];
687             expect(event.type).to.equal('nodeAdded');
688             expect(event.meta.node.sameNode(prepended)).to.be.true;
689         });
690         
691         it('emits nodeMoved when prepending aready existing node', function() {
692             var node = elementNodeFromXML('<div><a></a><b></b></div>'),
693                 a = node.contents()[0],
694                 b = node.contents()[1],
695                 spy = sinon.spy();
696             node.document.on('change', spy);
697             
698             var prepended = a.prepend(b),
699                 event = spy.args[0][0];
700             expect(spy.callCount).to.equal(1);
701             expect(event.type).to.equal('nodeMoved');
702             expect(event.meta.node.sameNode(prepended)).to.be.true;
703         });
704         
705         it('emits nodeAdded event when inserting node after another', function() {
706             var node = elementNodeFromXML('<div><a></a></div>').contents()[0],
707                 spy = sinon.spy();
708             node.document.on('change', spy);
709             
710             var inserted = node.after({tagName:'div'}),
711                 event = spy.args[0][0];
712             expect(event.type).to.equal('nodeAdded');
713             expect(event.meta.node.sameNode(inserted)).to.be.true;
714         });
715         
716         it('emits nodeMoved when inserting aready existing node after another', 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             var inserted = b.after(a),
723                 event = spy.args[0][0];
724
725             expect(spy.callCount).to.equal(1);
726             expect(event.type).to.equal('nodeMoved');
727             expect(event.meta.node.sameNode(inserted)).to.be.true;
728         });
729
730         it('emits nodeAdded event when inserting node before 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.before({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 nodeAdded when inserting aready existing node before 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 = a.before(b),
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 nodeDetached and nodeAdded when replacing root node with another', function() {
756             var doc = getDocumentFromXML('<a></a>'),
757                 oldRoot = doc.root,
758                 spy = sinon.spy();
759
760             doc.on('change', spy);
761
762             doc.root.replaceWith({tagName: 'b'});
763
764             expect(spy.callCount).to.equal(2);
765
766             var event1 = spy.args[0][0],
767                 event2 = spy.args[1][0];
768
769             expect(event1.type).to.equal('nodeDetached');
770             expect(event1.meta.node.sameNode(oldRoot)).to.equal(true, 'root node in nodeDetached event metadata');
771             expect(event2.type).to.equal('nodeAdded');
772             expect(event2.meta.node.sameNode(doc.root)).to.equal(true, 'new root node in nodelAdded event meta');
773         });
774
775
776         ['append', 'prepend', 'before', 'after'].forEach(function(insertionMethod) {
777             it('emits nodeDetached for node moved from a document tree to out of document node ' + insertionMethod, function() {
778                 var doc = getDocumentFromXML('<div><a></a></div>'),
779                     a = doc.root.contents()[0],
780                     spy = sinon.spy();
781
782                 doc.on('change', spy);
783
784                 var newNode = doc.createDocumentNode({tagName: 'b'}),
785                     newNodeInner = newNode.append({tagName:'c'});
786
787                 newNodeInner[insertionMethod](a);
788
789                 var event = spy.args[0][0];
790                 expect(event.type).to.equal('nodeDetached');
791                 expect(event.meta.node.sameNode(a));
792             });
793
794             it('doesn\'t emit nodeDetached event for already out of document node moved to out of document node' + insertionMethod, function() {
795                 var doc = getDocumentFromXML('<div><a></a></div>'),
796                     spy = sinon.spy();
797
798                 doc.on('change', spy);
799
800                 var newNode = doc.createDocumentNode({tagName: 'b'});
801                 newNode.append({tagName:'c'});
802
803                 expect(spy.callCount).to.equal(0);
804             });
805         });
806
807
808     });
809
810     describe('Traversing', function() {
811         describe('Basic', function() {
812             it('can access node parent', function() {
813                 var doc = getDocumentFromXML('<a><b></b></a>'),
814                     a = doc.root,
815                     b = a.contents()[0];
816
817                 expect(a.parent()).to.equal(null, 'parent of a root is null');
818                 expect(b.parent().sameNode(a)).to.be.true;
819             });
820             it('can access node parents', function() {
821                 var doc = getDocumentFromXML('<a><b><c></c></b></a>'),
822                     a = doc.root,
823                     b = a.contents()[0],
824                     c = b.contents()[0];
825
826                 var parents = c.parents();
827                 // @@
828                 expect(parents[0].sameNode(b)).to.be.true;
829                 expect(parents[1].sameNode(a)).to.be.true;
830             });
831         });
832
833         describe('finding sibling parents of two elements', function() {
834             it('returns elements themself if they have direct common parent', function() {
835                 var doc = getDocumentFromXML('<section><div><div>A</div><div>B</div></div></section>'),
836                     wrappingDiv = doc.root.contents()[0],
837                     divA = wrappingDiv.contents()[0],
838                     divB = wrappingDiv.contents()[1];
839
840                 var siblingParents = doc.getSiblingParents({node1: divA, node2: divB});
841
842                 expect(siblingParents.node1.sameNode(divA)).to.equal(true, 'divA');
843                 expect(siblingParents.node2.sameNode(divB)).to.equal(true, 'divB');
844             });
845
846             it('returns sibling parents - example 1', function() {
847                 var doc = getDocumentFromXML('<section>Alice <span>has a cat</span></section>'),
848                     aliceText = doc.root.contents()[0],
849                     span = doc.root.contents()[1],
850                     spanText = span.contents()[0];
851
852                 var siblingParents = doc.getSiblingParents({node1: aliceText, node2: spanText});
853
854                 expect(siblingParents.node1.sameNode(aliceText)).to.equal(true, 'aliceText');
855                 expect(siblingParents.node2.sameNode(span)).to.equal(true, 'span');
856             });
857         });
858     });
859
860     describe('Serializing document to WLXML', function() {
861         it('keeps document intact when no changes have been made', function() {
862             var xmlIn = '<section>Alice<div>has</div>a <span class="uri" meta-uri="http://cat.com">cat</span>!</section>',
863                 doc = getDocumentFromXML(xmlIn),
864                 xmlOut = doc.toXML();
865
866             var parser = new DOMParser(),
867                 input = parser.parseFromString(xmlIn, 'application/xml').childNodes[0],
868                 output = parser.parseFromString(xmlOut, 'application/xml').childNodes[0];
869             
870             expect(input.isEqualNode(output)).to.be.true;
871         });
872
873         it('keeps entities intact', function() {
874             var xmlIn = '<section>&lt; &gt;</section>',
875                 doc = getDocumentFromXML(xmlIn),
876                 xmlOut = doc.toXML();
877             expect(xmlOut).to.equal(xmlIn);
878         });
879         it('keeps entities intact when they form html/xml', function() {
880             var xmlIn = '<section>&lt;abc&gt;</section>',
881                 doc = getDocumentFromXML(xmlIn),
882                 xmlOut = doc.toXML();
883             expect(xmlOut).to.equal(xmlIn);
884         });
885     });
886
887     describe('Extension API', function() {
888         var doc, extension, elementNode, textNode;
889
890         beforeEach(function() {
891             doc = getDocumentFromXML('<section>Alice<div class="test_class"></div></section>');
892             elementNode = doc.root;
893             textNode = doc.root.contents()[0];
894             extension = {};
895             
896             expect(elementNode.testTransformation).to.be.undefined;
897             expect(textNode.testTransformation).to.be.undefined;
898             expect(doc.testTransformation).to.be.undefined;
899             
900             expect(doc.testMethod).to.be.undefined;
901             expect(elementNode.testMethod).to.be.undefined;
902             expect(textNode.testMethod).to.be.undefined;
903             expect(elementNode.elementTestMethod).to.be.undefined;
904             expect(textNode.textTestMethod).to.be.undefined;
905         });
906
907         it('allows adding method to a document', function() {
908             extension = {document: {methods: {
909                 testMethod: function() { return this; }
910             }}};
911
912             doc.registerExtension(extension);
913             expect(doc.testMethod()).to.equal(doc, 'context is set to a document instance');
914         });
915
916         it('allows adding transformation to a document', function() {
917             extension = {document: {transformations: {
918                 testTransformation: function() { return this; },
919                 testTransformation2: {impl: function() { return this;}}
920             }}};
921
922             doc.registerExtension(extension);
923             expect(doc.testTransformation()).to.equal(doc, 'context is set to a document instance');
924             expect(doc.testTransformation2()).to.equal(doc, 'context is set to a document instance');
925         });
926
927         it('allows adding method to a DocumentNode instance', function() {
928             extension = {
929                 documentNode: {
930                     methods: {
931                         testMethod: function() { return this; }
932                     }
933                 },
934                 textNode: {
935                     methods: {
936                         textTestMethod: function() { return this; }
937                     }
938                 },
939                 elementNode: {
940                     methods: {
941                         elementTestMethod: function() { return this; }
942                     }
943                 }
944             };
945
946             doc.registerExtension(extension);
947
948             /* refresh */
949             elementNode = doc.root;
950             textNode = doc.root.contents()[0];
951
952             expect(elementNode.testMethod().sameNode(elementNode)).to.equal(true, 'context is set to a node instance');
953             expect(textNode.testMethod().sameNode(textNode)).to.equal(true, 'context is set to a node instance');
954
955             expect(elementNode.elementTestMethod().sameNode(elementNode)).to.be.true;
956             expect(elementNode.textTestMethod).to.be.undefined;
957         
958             expect(textNode.textTestMethod().sameNode(textNode)).to.be.true;
959             expect(textNode.elementTestMethod).to.be.undefined;
960         });
961
962         it('allows adding transformation to a DocumentNode', function() {
963             extension = {
964                 documentNode: {
965                     transformations: {
966                         testTransformation: function() { return this; },
967                         testTransformation2: {impl: function() { return this;}}
968                     }
969                 },
970                 textNode: {
971                     transformations: {
972                         textTestTransformation: function() { return this; }
973                     }
974                 },
975                 elementNode: {
976                     transformations: {
977                         elementTestTransformation: function() { return this; }
978                     }
979                 }
980             };
981             
982             doc.registerExtension(extension);
983
984             /* refresh */
985             elementNode = doc.root;
986             textNode = doc.root.contents()[0];
987             
988             expect(elementNode.testTransformation().sameNode(elementNode)).to.equal(true, '1');
989             expect(elementNode.testTransformation2().sameNode(elementNode)).to.equal(true, '2');
990             expect(textNode.testTransformation().sameNode(textNode)).to.equal(true, '3');
991             expect(textNode.testTransformation2().sameNode(textNode)).to.equal(true, '4');
992
993             expect(elementNode.elementTestTransformation().sameNode(elementNode)).to.be.true;
994             expect(elementNode.textTestTransformation).to.be.undefined;
995         
996             expect(textNode.textTestTransformation().sameNode(textNode)).to.be.true;
997             expect(textNode.elementTestTransfomation).to.be.undefined;
998         });
999
1000         it('allows text/element node methods and transformations to access node and transormations on document node', function() {
1001
1002             var doc = getDocumentFromXML('<div>text</div>');
1003
1004             doc.registerExtension({
1005                 documentNode: {
1006                     methods: {
1007                         test: function() {
1008                             return 'super';
1009                         }
1010                     },
1011                     transformations: {
1012                         testT: function() {
1013                             return 'super_trans';
1014                         }
1015                     }
1016                 },
1017                 elementNode: {
1018                     methods: {
1019                         test: function() {
1020                             return 'element_sub_' + this.__super__.test();
1021                         }
1022                     },
1023                     transformations: {
1024                         testT: function() {
1025                             return 'element_trans_sub_' + this.__super__.testT();
1026                         }
1027                     }
1028                 },
1029                 textNode: {
1030                     methods: {
1031                         test: function() {
1032                             return 'text_sub_' + this.__super__.test();
1033                         }
1034                     },
1035                     transformations: {
1036                         testT: function() {
1037                             return 'text_trans_sub_' + this.__super__.testT();
1038                         }
1039                     }
1040                 }
1041             });
1042
1043             var textNode = doc.root.contents()[0];
1044
1045             expect(doc.root.test()).to.equal('element_sub_super');
1046             expect(textNode.test()).to.equal('text_sub_super');
1047             expect(doc.root.testT()).to.equal('element_trans_sub_super_trans');
1048             expect(textNode.testT()).to.equal('text_trans_sub_super_trans');
1049         });
1050     });
1051
1052     describe('Undo/redo', function() {
1053
1054         it('smoke tests', function() {
1055             var doc = getDocumentFromXML('<div>Alice</div>'),
1056                 textNode = doc.root.contents()[0];
1057
1058             expect(doc.undoStack).to.have.length(0);
1059             
1060             textNode.wrapWith({tagName: 'span', start:1, end:2});
1061             expect(doc.undoStack).to.have.length(1, '1');
1062             expect(doc.toXML()).to.equal('<div>A<span>l</span>ice</div>');
1063
1064             doc.undo();
1065             expect(doc.undoStack).to.have.length(0, '2');
1066             expect(doc.toXML()).to.equal('<div>Alice</div>');
1067
1068             doc.redo();
1069             expect(doc.undoStack).to.have.length(1, '3');
1070             expect(doc.toXML()).to.equal('<div>A<span>l</span>ice</div>');
1071
1072             doc.undo();
1073             expect(doc.undoStack).to.have.length(0, '4');
1074             expect(doc.toXML()).to.equal('<div>Alice</div>');
1075
1076             doc.undo();
1077             expect(doc.undoStack).to.have.length(0, '5');
1078             expect(doc.toXML()).to.equal('<div>Alice</div>');
1079         });
1080
1081         it('smoke tests 2', function() {
1082             var doc = getDocumentFromXML('<div>Alice</div>'),
1083                 textNode = doc.root.contents()[0],
1084                 path = textNode.getPath();
1085
1086             textNode.setText('Alice ');
1087             textNode.setText('Alice h');
1088             textNode.setText('Alice ha');
1089             textNode.setText('Alice has');
1090
1091             expect(textNode.getText()).to.equal('Alice has');
1092
1093             doc.undo();
1094             expect(doc.root.contents()[0].getText()).to.equal('Alice ha', '1');
1095
1096             doc.undo();
1097             expect(doc.root.contents()[0].getText()).to.equal('Alice h', '2');
1098
1099             doc.redo();
1100             expect(doc.root.contents()[0].getText()).to.equal('Alice ha', '3');
1101
1102             doc.redo();
1103             expect(doc.root.contents()[0].getText()).to.equal('Alice has', '4');
1104
1105             doc.undo();
1106             doc.undo();
1107             textNode = doc.getNodeByPath(path);
1108             textNode.setText('Cat');
1109             doc.undo();
1110             textNode = doc.getNodeByPath(path);
1111             expect(textNode.getText()).to.equal('Alice h');
1112         });
1113
1114         
1115         var sampleMethod = function(val) {
1116             this._$.attr('x', val);
1117         };
1118
1119         var transformations = {
1120             'unaware': sampleMethod,
1121             'returning change root': {
1122                 impl: sampleMethod,
1123                 getChangeRoot: function() {
1124                     return this.context;
1125                 }
1126             },
1127             'implementing undo operation': {
1128                 impl: function(t, val) {
1129                     t.oldVal = this.getAttr('x');
1130                     sampleMethod.call(this, val);
1131                 },
1132                 undo: function(t) {
1133                     this.setAttr('x', t.oldVal);
1134                 }
1135             }
1136         };
1137
1138         _.pairs(transformations).forEach(function(pair) {
1139             var name = pair[0],
1140                 transformaton = pair[1];
1141
1142             describe(name + ' transformation: ', function() {
1143                 var doc, node, nodePath;
1144
1145                 beforeEach(function() {
1146                     doc = getDocumentFromXML('<div><test x="old"></test></div>');
1147
1148                     doc.registerExtension({elementNode: {transformations: {
1149                         test: transformaton
1150                     }}});
1151
1152                     node = doc.root.contents()[0];
1153                     nodePath = node.getPath();
1154                 });
1155
1156                 it('transforms as expected', function() {
1157                     node.test('new');
1158                     expect(node.getAttr('x')).to.equal('new');
1159                 });
1160
1161                 it('can be undone', function() {
1162                     node.test('new');
1163                     doc.undo();
1164                     node = doc.getNodeByPath(nodePath);
1165                     expect(node.getAttr('x')).to.equal('old');
1166                 });
1167
1168                 it('can be undone and then redone', function() {
1169                     node.test('new');
1170                     doc.undo();
1171                     doc.redo();
1172                     node = doc.getNodeByPath(nodePath);
1173                     expect(node.getAttr('x')).to.equal('new');
1174                 });
1175
1176                 it('handles a sample scenario', function() {
1177                     doc.root.contents()[0].test('1');
1178                     doc.root.contents()[0].test('2');
1179                     doc.root.contents()[0].test('3');
1180                     doc.root.contents()[0].test('4');
1181                     doc.root.contents()[0].test('5');
1182
1183                     expect(doc.root.contents()[0].getAttr('x')).to.equal('5', 'after initial transformations');
1184                     doc.undo();
1185                     expect(doc.root.contents()[0].getAttr('x')).to.equal('4', 'undo 1.1');
1186                     doc.undo();
1187                     expect(doc.root.contents()[0].getAttr('x')).to.equal('3', 'undo 1.2');
1188                     doc.redo();
1189                     expect(doc.root.contents()[0].getAttr('x')).to.equal('4', 'redo 1.1');
1190                     doc.redo();
1191                     expect(doc.root.contents()[0].getAttr('x')).to.equal('5', 'redo 1.2');
1192                     doc.undo();
1193                     expect(doc.root.contents()[0].getAttr('x')).to.equal('4', 'undo 2.1');
1194                     doc.root.contents()[0].test('10');
1195                     expect(doc.root.contents()[0].getAttr('x')).to.equal('10', 'additional transformation');
1196                     expect(doc.redoStack.length).to.equal(0, 'transformation cleared redo stack');
1197                     doc.redo();
1198                     expect(doc.root.contents()[0].getAttr('x')).to.equal('10', 'empty redoStack so redo was noop');
1199                     doc.undo();
1200                     expect(doc.root.contents()[0].getAttr('x')).to.equal('4', 'undoing additional transformation');
1201                     doc.redo();
1202                     expect(doc.root.contents()[0].getAttr('x')).to.equal('10', 'redoing additional transformation');
1203                 });
1204             });
1205         });
1206
1207         it('smoke tests nested transformations', function() {
1208             var doc = getDocumentFromXML('<div></div>');
1209
1210             doc.registerExtension({elementNode: {transformations: {
1211                 nested: function(v) {
1212                     this._$.attr('innerAttr', v);
1213                 },
1214                 outer: function(v) {
1215                     this.nested(v);
1216                     this._$.attr('outerAttr', v);
1217                 }
1218             }}});
1219
1220             doc.root.outer('test1');
1221             doc.root.outer('test2');
1222
1223             expect(doc.root.getAttr('innerAttr')).to.equal('test2');
1224             expect(doc.root.getAttr('outerAttr')).to.equal('test2');
1225
1226             doc.undo();
1227
1228             expect(doc.root.getAttr('innerAttr')).to.equal('test1');
1229             expect(doc.root.getAttr('outerAttr')).to.equal('test1');
1230
1231             doc.undo();
1232
1233             expect(doc.root.getAttr('innerAttr')).to.equal(undefined);
1234             expect(doc.root.getAttr('outerAttr')).to.equal(undefined);
1235
1236             doc.redo();
1237
1238             expect(doc.root.getAttr('innerAttr')).to.equal('test1');
1239             expect(doc.root.getAttr('outerAttr')).to.equal('test1');
1240
1241             doc.redo();
1242
1243             expect(doc.root.getAttr('innerAttr')).to.equal('test2');
1244             expect(doc.root.getAttr('outerAttr')).to.equal('test2');
1245
1246         });
1247
1248         describe('Transactions', function() {
1249             it('allows to undo/redo series of transformations at once', function() {
1250                 var doc = getDocumentFromXML('<div></div>');
1251
1252                 doc.registerExtension({
1253                     elementNode: {transformations: {
1254                         test: function(v) {
1255                             this.setAttr('test', v);
1256                         }
1257                     }}
1258                 });
1259
1260                 doc.startTransaction();
1261                 doc.root.test('1');
1262                 doc.root.test('2');
1263                 doc.root.test('3');
1264                 doc.endTransaction();
1265
1266                 doc.undo();
1267                 expect(doc.root.getAttr('test'), '1');
1268                 doc.redo();
1269                 expect(doc.root.getAttr('test'), '3');
1270                 doc.undo();
1271                 expect(doc.root.getAttr('test'), '1');
1272                 doc.redo();
1273                 expect(doc.root.getAttr('test'), '3');
1274             });
1275
1276             it('ignores empty transactions', function() {
1277                 var doc = getDocumentFromXML('<div></div>');
1278                 doc.startTransaction();
1279                 doc.endTransaction();
1280                 expect(doc.undoStack).to.have.length(0, 'empty transaction doesn\'t get pushed into undo stack');
1281             });
1282
1283             it('doesn\'t break on optimizations', function() {
1284                 // This is a smoke test checking if optimizations made to transaction undoing
1285                 // doesnt't break anything.
1286                 var doc = getDocumentFromXML('<div smart="1" unaware="1"></div>');
1287
1288                 doc.registerExtension({
1289                     elementNode: {transformations: {
1290                         unaware: function(v) {
1291                             this.setAttr('unware', v);
1292                         },
1293                         smart: {
1294                             impl: function(t, v) {
1295                                 t.oldVal = this.getAttr('smart');
1296                                 this.setAttr('smart', v);
1297                             },
1298                             undo: function(t) {
1299                                 this.setAttr('smart', t.oldVal);
1300                             }
1301                         }
1302                     }}
1303                 });
1304
1305                 doc.startTransaction();
1306                 doc.root.smart('2');
1307                 doc.root.unaware('2');
1308                 doc.root.smart('3');
1309                 doc.root.unaware('3');
1310                 doc.endTransaction();
1311
1312                 doc.undo();
1313
1314                 expect(doc.root.getAttr('smart')).to.equal('1');
1315                 expect(doc.root.getAttr('unaware')).to.equal('1');
1316             });
1317         });
1318     });
1319
1320 });
1321
1322 });