6 ], function(chai, sinon, _, smartxml) {
10 /* global describe, it, beforeEach, Node, DOMParser */
12 var expect = chai.expect;
15 var getDocumentFromXML = function(xml) {
16 return smartxml.documentFromXML(xml);
19 var elementNodeFromParams = function(params) {
20 return smartxml.elementNodeFromXML('<' + params.tag + '></' + params.tag + '>');
23 var elementNodeFromXML = function(xml) {
24 return smartxml.elementNodeFromXML(xml);
28 describe('smartxml', function() {
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');
36 it('can resets its content entirely', function() {
37 var doc = getDocumentFromXML('<div></div>');
39 expect(doc.root.getTagName()).to.equal('div');
41 doc.loadXML('<header></header>');
42 expect(doc.root.getTagName()).to.equal('header');
45 it('knows if it contains an ElementNode in its tree', function() {
46 var doc = getDocumentFromXML('<root><a></a>text</root>'),
48 a = root.contents()[0],
49 text = root.contents()[1];
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');
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');
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');
74 describe('DocumentNode', function() {
75 it('can be cloned', function() {
76 var doc = getDocumentFromXML('<div>Alice</div>'),
77 text = doc.root.contents()[0],
80 [doc.root, text].forEach(function(node) {
81 suffix = ' (' + (node.nodeType === Node.TEXT_NODE ? 'text' : 'element') + ')';
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);
89 it('can be cloned with its contents and its contents data', function() {
90 var doc = getDocumentFromXML('<root><div></div></root>'),
92 div = root.contents()[0];
94 var ClonableObject = function(arg) {
97 ClonableObject.prototype.clone = function() {
98 return new ClonableObject(this.arg);
101 div.setData('key', 'value');
102 div.setData('clonableObject', new ClonableObject('test'));
104 var rootClone = root.clone(),
105 divClone = rootClone.contents()[0],
106 stringClone = divClone.getData('key'),
107 objClone = divClone.getData('clonableObject');
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');
114 it('knows its path in the document tree', function() {
115 var doc = getDocumentFromXML('<root><a><b><c></c>text</b></a></root>'),
117 a = root.contents()[0],
119 text = b.contents()[1];
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]);
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]);
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();
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');
144 describe('Storing custom data', function() {
147 beforeEach(function() {
148 node = elementNodeFromXML('<div></div>');
151 it('can append single value', function() {
152 node.setData('key', 'value');
153 expect(node.getData('key')).to.equal('value');
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');
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'});
168 describe('Changing node tag', function() {
170 it('can change tag name', function() {
171 var node = elementNodeFromXML('<div></div>');
172 node = node.setTag('span');
173 expect(node.getTagName()).to.equal('span');
176 describe('Implementation specific expectations', function() {
177 it('keeps custom data', function() {
178 var node = elementNodeFromXML('<div></div>');
180 node.setData('key', 'value');
181 node = node.setTag('header');
183 expect(node.getTagName()).to.equal('header');
184 expect(node.getData()).to.eql({key: 'value'});
187 it('can change document root tag name', function() {
188 var doc = getDocumentFromXML('<div></div>');
189 doc.root.setTag('span');
190 expect(doc.root.getTagName()).to.equal('span');
193 it('keeps node contents', function() {
194 var node = elementNodeFromXML('<div><div></div></div>');
195 node = node.setTag('header');
196 expect(node.contents()).to.have.length(1);
201 describe('Setting node attributes', function() {
202 it('can set node attribute', function() {
203 var node = elementNodeFromXML('<div></div>');
205 node.setAttr('key', 'value');
206 expect(node.getAttr('key')).to.equal('value');
208 it('emits nodeAttrChange event', function() {
209 var node = elementNodeFromXML('<div key="value1"></div>'),
212 node.document.on('change', spy);
213 node.setAttr('key', 'value2');
214 var event = spy.args[0][0];
216 expect(event.type).to.equal('nodeAttrChange');
217 expect(event.meta.node.sameNode(node)).to.be.true;
218 expect(event.meta.attr).to.equal('key');
219 expect(event.meta.oldVal).to.equal('value1');
223 describe('Searching for the last child text node', function() {
225 '<div>xxx<div></div>last</div>',
226 '<div><div>last</div></div>',
227 '<div>xxx<div>last</div><div></div></div>'
228 ].forEach(function(xml, i) {
229 var example = 'example ' + i;
230 it('returns last child text node ' + example + ')', function() {
231 var doc = getDocumentFromXML(xml),
232 lastTextNode = doc.root.getLastTextNode();
233 expect(lastTextNode.getText()).to.equal('last', example);
239 describe('Basic TextNode properties', function() {
240 it('can have its text set', function() {
241 var node = elementNodeFromXML('<div>Alice</div>'),
242 textNode = node.contents()[0];
244 textNode.setText('Cat');
245 expect(textNode.getText()).to.equal('Cat');
248 it('emits nodeTextChange', function() {
249 var node = elementNodeFromXML('<div>Alice</div>'),
250 textNode = node.contents()[0],
253 textNode.document.on('change', spy);
254 textNode.setText('Cat');
256 var event = spy.args[0][0];
257 expect(event.type).to.equal('nodeTextChange');
260 it('puts NodeElement after itself', function() {
261 var node = elementNodeFromXML('<div>Alice</div>'),
262 textNode = node.contents()[0],
263 returned = textNode.after({tagName:'div'});
264 expect(returned.sameNode(node.contents()[1])).to.be.true;
267 it('puts NodeElement before itself', function() {
268 var node = elementNodeFromXML('<div>Alice</div>'),
269 textNode = node.contents()[0],
270 returned = textNode.before({tagName:'div'});
271 expect(returned.sameNode(node.contents()[0])).to.be.true;
274 describe('Wrapping TextNode contents', function() {
276 it('wraps DocumentTextElement', function() {
277 var node = elementNodeFromXML('<section>Alice</section>'),
278 textNode = node.contents()[0];
280 var returned = textNode.wrapWith({tagName: 'header'}),
281 parent = textNode.parent(),
282 parent2 = node.contents()[0];
284 expect(returned.sameNode(parent)).to.be.equal(true, 'wrapper is a parent');
285 expect(returned.sameNode(parent2)).to.be.equal(true, 'wrapper has a correct parent');
286 expect(returned.getTagName()).to.equal('header');
289 describe('wrapping part of DocumentTextElement', function() {
290 [{start: 5, end: 12}, {start: 12, end: 5}].forEach(function(offsets) {
291 it('wraps in the middle ' + offsets.start + '/' + offsets.end, function() {
292 var node = elementNodeFromXML('<section>Alice has a cat</section>'),
293 textNode = node.contents()[0];
295 var returned = textNode.wrapWith({tagName: 'header', attrs: {'attr1': 'value1'}, start: offsets.start, end: offsets.end}),
296 contents = node.contents();
298 expect(contents.length).to.equal(3);
300 expect(contents[0].nodeType).to.be.equal(Node.TEXT_NODE, 'first node is text node');
301 expect(contents[0].getText()).to.equal('Alice');
303 expect(contents[1].sameNode(returned)).to.be.true;
304 expect(returned.getTagName()).to.equal('header');
305 expect(returned.getAttr('attr1')).to.equal('value1');
306 expect(contents[1].contents().length).to.equal(1, 'wrapper has one node inside');
307 expect(contents[1].contents()[0].getText()).to.equal(' has a ');
309 expect(contents[2].nodeType).to.be.equal(Node.TEXT_NODE, 'third node is text node');
310 expect(contents[2].getText()).to.equal('cat');
314 it('wraps whole text inside DocumentTextElement if offsets span entire content', function() {
315 var node = elementNodeFromXML('<section>Alice has a cat</section>'),
316 textNode = node.contents()[0];
318 textNode.wrapWith({tagName: 'header', start: 0, end: 15});
320 var contents = node.contents();
321 expect(contents.length).to.equal(1);
322 expect(contents[0].getTagName()).to.equal('header');
323 expect(contents[0].contents()[0].getText()).to.equal('Alice has a cat');
328 describe('Dividing text node into two with element node', function() {
329 it('can divide text node with element node, splitting text node into two', function() {
330 var doc = getDocumentFromXML('<div>Alice has a cat</div>'),
331 text = doc.root.contents()[0];
333 var returned = text.divideWithElementNode({tagName: 'aside'}, {offset: 5}),
334 contents = doc.root.contents(),
335 lhsText = contents[0],
336 rhsText = contents[2];
338 expect(lhsText.getText()).to.equal('Alice');
339 expect(returned.sameNode(contents[1]));
340 expect(rhsText.getText()).to.equal(' has a cat');
343 it('treats dividing at the very end as appending after it', function() {
344 var doc = getDocumentFromXML('<div>Alice has a cat</div>'),
345 text = doc.root.contents()[0];
348 var returned = text.divideWithElementNode({tagName: 'aside'}, {offset: 15}),
349 contents = doc.root.contents(),
350 textNode = contents[0],
351 elementNode = contents[1];
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');
359 it('treats dividing at the very beginning as prepending before it', function() {
360 var doc = getDocumentFromXML('<div>Alice has a cat</div>'),
361 text = doc.root.contents()[0];
363 var returned = text.divideWithElementNode({tagName: 'aside'}, {offset: 0}),
364 contents = doc.root.contents(),
365 textNode = contents[1],
366 elementNode = contents[0];
368 expect(contents.length).to.equal(2);
369 expect(textNode.getText()).to.equal('Alice has a cat');
370 expect(returned.sameNode(elementNode)).to.be.true;
371 expect(elementNode.getTagName()).to.equal('aside');
376 describe('Manipulations', function() {
378 describe('detaching nodes', function() {
379 it('can detach document root node', function() {
380 var doc = getDocumentFromXML('<div></div>');
383 expect(doc.root).to.equal(null);
387 describe('replacing node with another one', function() {
388 it('replaces node with another one', function() {
389 var doc = getDocumentFromXML('<div><a></a></div>'),
390 a = doc.root.contents()[0];
392 var c = a.replaceWith({tagName: 'b', attrs: {b:'1'}});
394 expect(doc.root.contents()[0].sameNode(c));
395 expect(c.getTagName()).to.equal('b');
396 expect(c.getAttr('b')).to.equal('1');
398 it('can replace document root', function() {
399 var doc = getDocumentFromXML('<div></div>');
401 var header = doc.root.replaceWith({tagName: 'header'});
403 expect(doc.root.sameNode(header)).to.be.true;
404 expect(doc.containsNode(header)).to.be.true;
408 it('merges adjacent text nodes resulting from detaching an element node in between', function() {
409 var doc = getDocumentFromXML('<div>Alice <span>has</span>a cat</div>'),
410 span = doc.root.contents()[1];
414 var rootContents = doc.root.contents();
415 expect(rootContents).to.have.length(1, 'one child left');
416 expect(rootContents[0].getText()).to.equal('Alice a cat');
419 it('inserts node at index', function() {
420 var doc = getDocumentFromXML('<div><a></a><b></b><c></c></div>'),
421 b = doc.root.contents()[1];
423 var inserted = doc.root.insertAtIndex({tagName: 'test'}, 1);
425 expect(doc.root.contents()[1].sameNode(inserted)).to.equal(true, 'inserted node returned');
426 expect(b.getIndex()).to.equal(2, 'b node shifted right');
429 it('appends node when inserting node at index out of range', function() {
430 var doc = getDocumentFromXML('<div></div>');
432 var test1 = doc.root.insertAtIndex({tagName: 'test1'}, 0),
433 test2 = doc.root.insertAtIndex({tagName: 'test1'}, 10);
435 expect(doc.root.contents()[0].sameNode(test1)).to.equal(true, 'inserting at index 0 of empty nodes appends node');
436 expect(doc.root.contents().length).to.equal(1, 'inserting at index out of range does nothing');
437 expect(test2).to.equal(undefined, 'inserting at index out of range returns undefined');
440 it('appends element node to another element node', function() {
441 var node1 = elementNodeFromParams({tag: 'div'}),
442 node2 = elementNodeFromParams({tag: 'a'}),
443 node3 = elementNodeFromParams({tag: 'p'});
446 expect(node1.contents()[0].sameNode(node2)).to.be.true;
447 expect(node1.contents()[1].sameNode(node3)).to.be.true;
450 it('prepends element node to another element node', function() {
451 var node1 = elementNodeFromParams({tag: 'div'}),
452 node2 = elementNodeFromParams({tag: 'a'}),
453 node3 = elementNodeFromParams({tag: 'p'});
454 node1.prepend(node2);
455 node1.prepend(node3);
456 expect(node1.contents()[0].sameNode(node3)).to.be.true;
457 expect(node1.contents()[1].sameNode(node2)).to.be.true;
460 describe('adding text nodes', function() {
461 it('merges text nodes on append', function() {
462 var doc = getDocumentFromXML('<root>text1</root>'),
464 returned = doc.root.append({text: 'text2'});
465 expect(doc.root.contents().length).to.equal(1);
466 expect(returned.sameNode(doc.root.contents()[0])).to.equal(true, 'modified node returned');
467 expect(doc.root.contents()[0].getText()).to.equal('text1text2');
470 it('merges text nodes on prepend', function() {
471 var doc = getDocumentFromXML('<root>text1</root>'),
473 returned = doc.root.prepend({text: 'text2'});
474 expect(doc.root.contents().length).to.equal(1);
475 expect(returned.sameNode(doc.root.contents()[0])).to.equal(true, 'modified node returned');
476 expect(doc.root.contents()[0].getText()).to.equal('text2text1');
479 it('merges text nodes on before text node', function() {
480 var doc = getDocumentFromXML('<root>text1</root>'),
481 textNode = doc.root.contents()[0],
483 returned = textNode.before({text: 'text2'});
484 expect(doc.root.contents().length).to.equal(1);
485 expect(returned.sameNode(doc.root.contents()[0])).to.equal(true, 'modified node returned');
486 expect(doc.root.contents()[0].getText()).to.equal('text2text1');
489 it('merges text nodes on after text node', function() {
490 var doc = getDocumentFromXML('<root>text1</root>'),
491 textNode = doc.root.contents()[0],
493 returned = textNode.after({text: 'text2'});
494 expect(doc.root.contents().length).to.equal(1);
495 expect(returned.sameNode(doc.root.contents()[0])).to.equal(true, 'modified node returned');
496 expect(doc.root.contents()[0].getText()).to.equal('text1text2');
499 it('merges text nodes on before element node', function() {
500 var doc = getDocumentFromXML('<root>text1<div></div></root>'),
501 textNode = doc.root.contents()[0],
502 div = doc.root.contents()[1],
504 returned = div.before({text: 'text2'});
505 expect(doc.root.contents().length).to.equal(2);
506 expect(returned.sameNode(doc.root.contents()[0])).to.equal(true, 'modified node returned');
507 expect(textNode.getText()).to.equal('text1text2');
510 it('merges text nodes on after element node', function() {
511 var doc = getDocumentFromXML('<root><div></div>text1</root>'),
512 textNode = doc.root.contents()[1],
513 div = doc.root.contents()[0],
515 returned = div.after({text: 'text2'});
516 expect(doc.root.contents().length).to.equal(2);
517 expect(returned.sameNode(doc.root.contents()[1])).to.equal(true, 'modified node returned');
518 expect(textNode.getText()).to.equal('text2text1');
522 it('wraps root element node with another element node', function() {
523 var node = elementNodeFromXML('<div></div>'),
524 wrapper = elementNodeFromXML('<wrapper></wrapper>');
526 node.wrapWith(wrapper);
527 expect(node.parent().sameNode(wrapper)).to.be.true;
528 expect(node.document.root.sameNode(wrapper)).to.be.true;
531 it('wraps element node with another element node', function() {
532 var doc = getDocumentFromXML('<section><div></div></section>'),
533 div = doc.root.contents()[0];
535 var wrapper = div.wrapWith({tagName: 'wrapper'});
536 expect(wrapper.sameNode(doc.root.contents()[0])).to.equal(true, '1');
537 expect(div.parent().sameNode(wrapper)).to.equal(true, '2');
538 expect(wrapper.contents()[0].sameNode(div)).to.equal(true, '3');
541 it('wraps element outside of document tree', function() {
542 var doc = getDocumentFromXML('<section><div></div></section>'),
543 node = doc.createDocumentNode({tagName: 'node'});
545 node.wrapWith({tagName: 'wrapper'});
546 expect(node.parent().getTagName()).to.equal('wrapper');
547 expect(node.parent().contents()[0].sameNode(node)).to.be.true;
548 expect(doc.root.getTagName()).to.equal('section');
551 it('unwraps element node contents', function() {
552 var node = elementNodeFromXML('<div>Alice <div>has <span>propably</span> a cat</div>!</div>'),
553 outerDiv = node.contents()[1];
555 outerDiv.unwrapContent();
557 expect(node.contents().length).to.equal(3);
558 expect(node.contents()[0].getText()).to.equal('Alice has ');
559 expect(node.contents()[1].getTagName()).to.equal('span');
560 expect(node.contents()[2].getText()).to.equal(' a cat!');
563 it('unwrap single element node from its parent', function() {
564 var doc = getDocumentFromXML('<div><a><b></b></a></div>'),
566 a = div.contents()[0],
569 var parent = b.unwrap();
571 expect(parent.sameNode(div)).to.equal(true, 'returns new parent');
572 expect(div.contents()).to.have.length(1, 'root contains only one node');
573 expect(div.contents()[0].sameNode(b)).to.equal(true, 'node got unwrapped');
576 it('unwrap single text node from its parent', function() {
577 var doc = getDocumentFromXML('<div>Some <span>text</span>!</div>'),
579 span = div.contents()[1],
580 text = span.contents()[0];
582 var parent = text.unwrap();
584 expect(parent.sameNode(div)).to.equal(true, 'returns new parent');
585 expect(div.contents()).to.have.length(1, 'root contains only one node');
586 expect(div.contents()[0].getText()).to.equal('Some text!');
589 describe('Wrapping text', function() {
590 it('wraps text spanning multiple sibling TextNodes', function() {
591 var section = elementNodeFromXML('<section>Alice has a <span>small</span> cat</section>'),
592 wrapper = section.wrapText({
593 _with: {tagName: 'span', attrs: {'attr1': 'value1'}},
599 expect(section.contents().length).to.equal(2);
600 expect(section.contents()[0].nodeType).to.equal(Node.TEXT_NODE);
601 expect(section.contents()[0].getText()).to.equal('Alice ');
603 var wrapper2 = section.contents()[1];
604 expect(wrapper2.sameNode(wrapper)).to.be.true;
605 expect(wrapper.getTagName()).to.equal('span');
607 var wrapperContents = wrapper.contents();
608 expect(wrapperContents.length).to.equal(3);
609 expect(wrapperContents[0].getText()).to.equal('has a ');
611 expect(wrapperContents[1].nodeType).to.equal(Node.ELEMENT_NODE);
612 expect(wrapperContents[1].contents().length).to.equal(1);
613 expect(wrapperContents[1].contents()[0].getText()).to.equal('small');
617 describe('Wrapping Nodes', function() {
618 it('wraps multiple sibling nodes', function() {
619 var section = elementNodeFromXML('<section>Alice<div>has</div><div>a cat</div></section>'),
620 aliceText = section.contents()[0],
621 firstDiv = section.contents()[1],
622 lastDiv = section.contents()[section.contents().length -1];
624 var returned = section.document.wrapNodes({
627 _with: {tagName: 'header'}
630 var sectionContentss = section.contents(),
631 header = sectionContentss[0],
632 headerContents = header.contents();
634 expect(sectionContentss).to.have.length(1);
635 expect(header.sameNode(returned)).to.equal(true, 'wrapper returned');
636 expect(header.parent().sameNode(section)).to.be.true;
637 expect(headerContents).to.have.length(3);
638 expect(headerContents[0].sameNode(aliceText)).to.equal(true, 'first node wrapped');
639 expect(headerContents[1].sameNode(firstDiv)).to.equal(true, 'second node wrapped');
640 expect(headerContents[2].sameNode(lastDiv)).to.equal(true, 'third node wrapped');
643 it('wraps multiple sibling Elements - middle case', function() {
644 var section = elementNodeFromXML('<section><div></div><div></div><div></div><div></div></section>'),
645 div2 = section.contents()[1],
646 div3 = section.contents()[2];
648 section.document.wrapNodes({
651 _with: {tagName: 'header'}
654 var sectionContentss = section.contents(),
655 header = sectionContentss[1],
656 headerChildren = header.contents();
658 expect(sectionContentss).to.have.length(3);
659 expect(headerChildren).to.have.length(2);
660 expect(headerChildren[0].sameNode(div2)).to.equal(true, 'first node wrapped');
661 expect(headerChildren[1].sameNode(div3)).to.equal(true, 'second node wrapped');
667 var getTextNodes = function(text, doc) {
670 var search = function(node) {
671 node.contents().forEach(function(node) {
672 if(node.nodeType === Node.TEXT_NODE) {
673 if(node.getText() === text) {
685 var getTextNode = function(text, doc) {
686 var nodes = getTextNodes(text, doc),
688 if(nodes.length === 0) {
689 error = 'Text not found';
690 } else if(nodes.length > 1) {
691 error = 'Text not unique';
692 } else if(nodes[0].getText() !== text) {
693 error = 'I was trying to cheat your test :(';
696 throw new Error(error);
701 describe('Removing arbitrary text', function() {
702 it('removes within single text element', function() {
703 var doc = getDocumentFromXML('<div>Alice</div>'),
704 text = getTextNode('Alice', doc);
715 expect(doc.root.contents().length).to.equal(1);
716 expect(doc.root.contents()[0].getText()).to.equal('Ae');
718 it('removes across elements - 1', function() {
719 var doc = getDocumentFromXML('<div><a>aaa</a><b>bbb</b></div>');
723 node: getTextNode('aaa', doc),
727 node: getTextNode('bbb', doc),
732 var contents = doc.root.contents();
733 expect(contents.length).to.equal(2);
734 expect(contents[0].contents()[0].getText()).to.equal('aa');
735 expect(contents[1].contents()[0].getText()).to.equal('b');
737 it('removes across elements - 2', function() {
738 var doc = getDocumentFromXML('<a><b><c>ccc</c></b>xxx</a>');
741 node: getTextNode('ccc', doc),
745 node: getTextNode('xxx', doc),
750 var contents = doc.root.contents();
751 expect(contents.length).to.equal(2);
752 expect(contents[0].getTagName()).to.equal('b');
753 expect(contents[1].getText()).to.equal('x');
755 var bContents = contents[0].contents();
756 expect(bContents.length).to.equal(1);
757 expect(bContents[0].getTagName()).to.equal('c');
758 expect(bContents[0].contents().length).to.equal(1);
759 expect(bContents[0].contents()[0].getText()).to.equal('cc');
761 it('remove across elements - 3 (merged text nodes)', function() {
762 var doc = getDocumentFromXML('<div>Alice <span>has</span> a cat</div>');
765 node: getTextNode('Alice ', doc),
769 node: getTextNode(' a cat', doc),
773 var contents = doc.root.contents();
774 expect(contents.length).to.equal(1);
775 expect(contents[0].getText()).to.equal('Acat');
777 it('remove across elements - 4', function() {
778 var doc = getDocumentFromXML('<div>Alice <div>has <span>a</span> cat</div></div>');
781 node: getTextNode('Alice ', doc),
785 node: getTextNode(' cat', doc),
789 var contents = doc.root.contents();
790 expect(contents.length).to.equal(2);
791 expect(contents[0].getText()).to.equal('A');
792 expect(contents[1].getTagName()).to.equal('div');
793 expect(contents[1].contents().length).to.equal(1);
794 expect(contents[1].contents()[0].getText()).to.equal('cat');
796 it('removes across elements - 5 (whole document)', function() {
797 var doc = getDocumentFromXML('<div>Alice <div>has <span>a</span> cat</div>!!!</div>');
800 node: getTextNode('Alice ', doc),
804 node: getTextNode('!!!', doc),
809 expect(doc.root.getTagName()).to.equal('div');
810 expect(doc.root.contents().length).to.equal(1);
811 expect(doc.root.contents()[0].getText()).to.equal('');
813 it('removes nodes in between', function() {
814 var doc = getDocumentFromXML('<div><a>aaa<x>!</x></a>xxx<x></x><b><x>!</x>bbb</b></div>');
817 node: getTextNode('aaa', doc),
821 node: getTextNode('bbb', doc),
826 var contents = doc.root.contents();
827 expect(contents.length).to.equal(2, 'two nodes survived');
828 expect(contents[0].getTagName()).to.equal('a');
829 expect(contents[1].getTagName()).to.equal('b');
830 expect(contents[0].contents().length).to.equal(1);
831 expect(contents[0].contents()[0].getText()).to.equal('aa');
832 expect(contents[1].contents().length).to.equal(1);
833 expect(contents[1].contents()[0].getText()).to.equal('b');
837 describe('Splitting text', function() {
839 it('splits TextNode\'s parent into two ElementNodes', function() {
840 var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
842 text = section.contents()[0].contents()[0];
844 var returnedValue = text.split({offset: 5});
845 expect(section.contents().length).to.equal(2, 'section has two children');
847 var header1 = section.contents()[0];
848 var header2 = section.contents()[1];
850 expect(header1.getTagName()).to.equal('header', 'first section child ok');
851 expect(header1.contents().length).to.equal(1, 'first header has one child');
852 expect(header1.contents()[0].getText()).to.equal('Some ', 'first header has correct content');
853 expect(header2.getTagName()).to.equal('header', 'second section child ok');
854 expect(header2.contents().length).to.equal(1, 'second header has one child');
855 expect(header2.contents()[0].getText()).to.equal('header', 'second header has correct content');
857 expect(returnedValue.first.sameNode(header1)).to.equal(true, 'first node returned');
858 expect(returnedValue.second.sameNode(header2)).to.equal(true, 'second node returned');
861 it('leaves empty copy of ElementNode if splitting at the very beginning', function() {
862 var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
864 text = section.contents()[0].contents()[0];
866 text.split({offset: 0});
868 var header1 = section.contents()[0];
869 var header2 = section.contents()[1];
871 expect(header1.contents().length).to.equal(0);
872 expect(header2.contents()[0].getText()).to.equal('Some header');
875 it('leaves empty copy of ElementNode if splitting at the very end', function() {
876 var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
878 text = section.contents()[0].contents()[0];
880 text.split({offset: 11});
882 var header1 = section.contents()[0];
883 var header2 = section.contents()[1];
885 expect(header1.contents()[0].getText()).to.equal('Some header');
886 expect(header2.contents().length).to.equal(0);
889 it('keeps TextNodes\'s parent\'s children elements intact', function() {
890 var doc = getDocumentFromXML('<section><header>A <span>fancy</span> and <span>nice</span> header</header></section>'),
892 header = section.contents()[0],
893 textAnd = header.contents()[2];
895 textAnd.split({offset: 2});
897 var sectionContents = section.contents();
898 expect(sectionContents.length).to.equal(2, 'Section has two children');
899 expect(sectionContents[0].getTagName()).to.equal('header', 'First section node is a header');
900 expect(sectionContents[1].getTagName()).to.equal('header', 'Second section node is a header');
902 var firstHeaderContents = sectionContents[0].contents();
903 expect(firstHeaderContents.length).to.equal(3, 'First header has three children');
904 expect(firstHeaderContents[0].getText()).to.equal('A ', 'First header starts with a text');
905 expect(firstHeaderContents[1].getTagName()).to.equal('span', 'First header has span in the middle');
906 expect(firstHeaderContents[2].getText()).to.equal(' a', 'First header ends with text');
908 var secondHeaderContents = sectionContents[1].contents();
909 expect(secondHeaderContents.length).to.equal(3, 'Second header has three children');
910 expect(secondHeaderContents[0].getText()).to.equal('nd ', 'Second header starts with text');
911 expect(secondHeaderContents[1].getTagName()).to.equal('span', 'Second header has span in the middle');
912 expect(secondHeaderContents[2].getText()).to.equal(' header', 'Second header ends with text');
916 describe('Events', function() {
917 it('emits nodeDetached event on node detach', function() {
918 var node = elementNodeFromXML('<div><div></div></div>'),
919 innerNode = node.contents()[0],
921 node.document.on('change', spy);
923 var detached = innerNode.detach(),
924 event = spy.args[0][0];
926 expect(event.type).to.equal('nodeDetached');
927 expect(event.meta.node.sameNode(detached, 'detached node in event meta'));
928 expect(event.meta.parent.sameNode(node), 'original parent node in event meta');
931 it('emits nodeAdded event when appending new node', function() {
932 var node = elementNodeFromXML('<div></div>'),
934 node.document.on('change', spy);
936 var appended = node.append({tagName:'div'}),
937 event = spy.args[0][0];
938 expect(event.type).to.equal('nodeAdded');
939 expect(event.meta.node.sameNode(appended)).to.be.true;
942 it('emits nodeMoved when appending aready existing node', function() {
943 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
944 a = node.contents()[0],
945 b = node.contents()[1],
947 node.document.on('change', spy);
949 var appended = a.append(b),
950 event = spy.args[0][0];
952 expect(spy.callCount).to.equal(1);
953 expect(event.type).to.equal('nodeMoved');
954 expect(event.meta.node.sameNode(appended)).to.be.true;
955 expect(node.document.root.sameNode(event.meta.parent)).to.equal(true, 'previous parent attached to event meta');
958 it('emits nodeAdded event when prepending new node', function() {
959 var node = elementNodeFromXML('<div></div>'),
961 node.document.on('change', spy);
963 var prepended = node.prepend({tagName:'div'}),
964 event = spy.args[0][0];
965 expect(event.type).to.equal('nodeAdded');
966 expect(event.meta.node.sameNode(prepended)).to.be.true;
969 it('emits nodeMoved when prepending aready existing node', function() {
970 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
971 a = node.contents()[0],
972 b = node.contents()[1],
974 node.document.on('change', spy);
976 var prepended = a.prepend(b),
977 event = spy.args[0][0];
978 expect(spy.callCount).to.equal(1);
979 expect(event.type).to.equal('nodeMoved');
980 expect(event.meta.node.sameNode(prepended)).to.be.true;
983 it('emits nodeAdded event when inserting node after another', function() {
984 var node = elementNodeFromXML('<div><a></a></div>').contents()[0],
986 node.document.on('change', spy);
988 var inserted = node.after({tagName:'div'}),
989 event = spy.args[0][0];
990 expect(event.type).to.equal('nodeAdded');
991 expect(event.meta.node.sameNode(inserted)).to.be.true;
994 it('emits nodeMoved when inserting aready existing node after another', function() {
995 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
996 a = node.contents()[0],
997 b = node.contents()[1],
999 node.document.on('change', spy);
1000 var inserted = b.after(a),
1001 event = spy.args[0][0];
1003 expect(spy.callCount).to.equal(1);
1004 expect(event.type).to.equal('nodeMoved');
1005 expect(event.meta.node.sameNode(inserted)).to.be.true;
1008 it('emits nodeAdded event when inserting node before another', function() {
1009 var node = elementNodeFromXML('<div><a></a></div>').contents()[0],
1011 node.document.on('change', spy);
1013 var inserted = node.before({tagName:'div'}),
1014 event = spy.args[0][0];
1015 expect(event.type).to.equal('nodeAdded');
1016 expect(event.meta.node.sameNode(inserted)).to.be.true;
1019 it('emits nodeAdded when inserting aready existing node before another', function() {
1020 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
1021 a = node.contents()[0],
1022 b = node.contents()[1],
1024 node.document.on('change', spy);
1025 var inserted = a.before(b),
1026 event = spy.args[0][0];
1028 expect(spy.callCount).to.equal(1);
1029 expect(event.type).to.equal('nodeMoved');
1030 expect(event.meta.node.sameNode(inserted)).to.be.true;
1033 it('emits nodeDetached and nodeAdded when replacing root node with another', function() {
1034 var doc = getDocumentFromXML('<a></a>'),
1038 doc.on('change', spy);
1040 doc.root.replaceWith({tagName: 'b'});
1042 expect(spy.callCount).to.equal(2);
1044 var event1 = spy.args[0][0],
1045 event2 = spy.args[1][0];
1047 expect(event1.type).to.equal('nodeDetached');
1048 expect(event1.meta.node.sameNode(oldRoot)).to.equal(true, 'root node in nodeDetached event metadata');
1049 expect(event2.type).to.equal('nodeAdded');
1050 expect(event2.meta.node.sameNode(doc.root)).to.equal(true, 'new root node in nodelAdded event meta');
1054 ['append', 'prepend', 'before', 'after'].forEach(function(insertionMethod) {
1055 it('emits nodeDetached for node moved from a document tree to out of document node ' + insertionMethod, function() {
1056 var doc = getDocumentFromXML('<div><a></a></div>'),
1057 a = doc.root.contents()[0],
1060 doc.on('change', spy);
1062 var newNode = doc.createDocumentNode({tagName: 'b'}),
1063 newNodeInner = newNode.append({tagName:'c'});
1065 newNodeInner[insertionMethod](a);
1067 var event = spy.args[0][0];
1068 expect(event.type).to.equal('nodeDetached');
1069 expect(event.meta.node.sameNode(a));
1072 it('doesn\'t emit nodeDetached event for already out of document node moved to out of document node' + insertionMethod, function() {
1073 var doc = getDocumentFromXML('<div><a></a></div>'),
1076 doc.on('change', spy);
1078 var newNode = doc.createDocumentNode({tagName: 'b'});
1079 newNode.append({tagName:'c'});
1081 expect(spy.callCount).to.equal(0);
1088 describe('Traversing', function() {
1089 describe('Basic', function() {
1090 it('can access node parent', function() {
1091 var doc = getDocumentFromXML('<a><b></b></a>'),
1093 b = a.contents()[0];
1095 expect(a.parent()).to.equal(null, 'parent of a root is null');
1096 expect(b.parent().sameNode(a)).to.be.true;
1098 it('can access node parents', function() {
1099 var doc = getDocumentFromXML('<a><b><c></c></b></a>'),
1101 b = a.contents()[0],
1102 c = b.contents()[0];
1104 var parents = c.parents();
1106 expect(parents[0].sameNode(b)).to.be.true;
1107 expect(parents[1].sameNode(a)).to.be.true;
1111 describe('finding sibling parents of two elements', function() {
1112 it('returns elements themself if they have direct common parent', function() {
1113 var doc = getDocumentFromXML('<section><div><div>A</div><div>B</div></div></section>'),
1114 wrappingDiv = doc.root.contents()[0],
1115 divA = wrappingDiv.contents()[0],
1116 divB = wrappingDiv.contents()[1];
1118 var siblingParents = doc.getSiblingParents({node1: divA, node2: divB});
1120 expect(siblingParents.node1.sameNode(divA)).to.equal(true, 'divA');
1121 expect(siblingParents.node2.sameNode(divB)).to.equal(true, 'divB');
1124 it('returns sibling parents - example 1', function() {
1125 var doc = getDocumentFromXML('<section>Alice <span>has a cat</span></section>'),
1126 aliceText = doc.root.contents()[0],
1127 span = doc.root.contents()[1],
1128 spanText = span.contents()[0];
1130 var siblingParents = doc.getSiblingParents({node1: aliceText, node2: spanText});
1132 expect(siblingParents.node1.sameNode(aliceText)).to.equal(true, 'aliceText');
1133 expect(siblingParents.node2.sameNode(span)).to.equal(true, 'span');
1136 it('returns node itself for two same nodes', function() {
1137 var doc = getDocumentFromXML('<section><div></div></section>'),
1138 div = doc.root.contents()[0];
1140 var siblingParents = doc.getSiblingParents({node1: div, node2: div});
1141 expect(!!siblingParents.node1 && !!siblingParents.node2).to.equal(true, 'nodes defined');
1142 expect(siblingParents.node1.sameNode(div)).to.be.equal(true, 'node1');
1143 expect(siblingParents.node2.sameNode(div)).to.be.equal(true, 'node2');
1148 describe('Serializing document to WLXML', function() {
1149 it('keeps document intact when no changes have been made', function() {
1150 var xmlIn = '<section>Alice<div>has</div>a <span class="uri" meta-uri="http://cat.com">cat</span>!</section>',
1151 doc = getDocumentFromXML(xmlIn),
1152 xmlOut = doc.toXML();
1154 var parser = new DOMParser(),
1155 input = parser.parseFromString(xmlIn, 'application/xml').childNodes[0],
1156 output = parser.parseFromString(xmlOut, 'application/xml').childNodes[0];
1158 expect(input.isEqualNode(output)).to.be.true;
1161 it('keeps entities intact', function() {
1162 var xmlIn = '<section>< ></section>',
1163 doc = getDocumentFromXML(xmlIn),
1164 xmlOut = doc.toXML();
1165 expect(xmlOut).to.equal(xmlIn);
1167 it('keeps entities intact when they form html/xml', function() {
1168 var xmlIn = '<section><abc></section>',
1169 doc = getDocumentFromXML(xmlIn),
1170 xmlOut = doc.toXML();
1171 expect(xmlOut).to.equal(xmlIn);
1175 describe('Extension API', function() {
1176 var doc, extension, elementNode, textNode;
1178 beforeEach(function() {
1179 doc = getDocumentFromXML('<section>Alice<div class="test_class"></div></section>');
1180 elementNode = doc.root;
1181 textNode = doc.root.contents()[0];
1184 expect(elementNode.testTransformation).to.be.undefined;
1185 expect(textNode.testTransformation).to.be.undefined;
1186 expect(doc.testTransformation).to.be.undefined;
1188 expect(doc.testMethod).to.be.undefined;
1189 expect(elementNode.testMethod).to.be.undefined;
1190 expect(textNode.testMethod).to.be.undefined;
1191 expect(elementNode.elementTestMethod).to.be.undefined;
1192 expect(textNode.textTestMethod).to.be.undefined;
1195 it('allows adding method to a document', function() {
1196 extension = {document: {methods: {
1197 testMethod: function() { return this; }
1200 doc.registerExtension(extension);
1201 expect(doc.testMethod()).to.equal(doc, 'context is set to a document instance');
1204 it('allows adding transformation to a document', function() {
1205 extension = {document: {transformations: {
1206 testTransformation: function() { return this; },
1207 testTransformation2: {impl: function() { return this;}}
1210 doc.registerExtension(extension);
1211 expect(doc.testTransformation()).to.equal(doc, 'context is set to a document instance');
1212 expect(doc.testTransformation2()).to.equal(doc, 'context is set to a document instance');
1215 it('allows adding method to a DocumentNode instance', function() {
1219 testMethod: function() { return this; }
1224 textTestMethod: function() { return this; }
1229 elementTestMethod: function() { return this; }
1234 doc.registerExtension(extension);
1237 elementNode = doc.root;
1238 textNode = doc.root.contents()[0];
1240 expect(elementNode.testMethod().sameNode(elementNode)).to.equal(true, 'context is set to a node instance');
1241 expect(textNode.testMethod().sameNode(textNode)).to.equal(true, 'context is set to a node instance');
1243 expect(elementNode.elementTestMethod().sameNode(elementNode)).to.be.true;
1244 expect(elementNode.textTestMethod).to.be.undefined;
1246 expect(textNode.textTestMethod().sameNode(textNode)).to.be.true;
1247 expect(textNode.elementTestMethod).to.be.undefined;
1250 it('allows adding transformation to a DocumentNode', function() {
1254 testTransformation: function() { return this; },
1255 testTransformation2: {impl: function() { return this;}}
1260 textTestTransformation: function() { return this; }
1265 elementTestTransformation: function() { return this; }
1270 doc.registerExtension(extension);
1273 elementNode = doc.root;
1274 textNode = doc.root.contents()[0];
1276 expect(elementNode.testTransformation().sameNode(elementNode)).to.equal(true, '1');
1277 expect(elementNode.testTransformation2().sameNode(elementNode)).to.equal(true, '2');
1278 expect(textNode.testTransformation().sameNode(textNode)).to.equal(true, '3');
1279 expect(textNode.testTransformation2().sameNode(textNode)).to.equal(true, '4');
1281 expect(elementNode.elementTestTransformation().sameNode(elementNode)).to.be.true;
1282 expect(elementNode.textTestTransformation).to.be.undefined;
1284 expect(textNode.textTestTransformation().sameNode(textNode)).to.be.true;
1285 expect(textNode.elementTestTransfomation).to.be.undefined;
1288 it('allows text/element node methods and transformations to access node and transormations on document node', function() {
1290 var doc = getDocumentFromXML('<div>text</div>');
1292 doc.registerExtension({
1301 return 'super_trans';
1308 return 'element_sub_' + this.__super__.test();
1313 return 'element_trans_sub_' + this.__super__.testT();
1320 return 'text_sub_' + this.__super__.test();
1325 return 'text_trans_sub_' + this.__super__.testT();
1331 var textNode = doc.root.contents()[0];
1333 expect(doc.root.test()).to.equal('element_sub_super');
1334 expect(textNode.test()).to.equal('text_sub_super');
1335 expect(doc.root.testT()).to.equal('element_trans_sub_super_trans');
1336 expect(textNode.testT()).to.equal('text_trans_sub_super_trans');
1340 describe('Undo/redo', function() {
1342 it('smoke tests', function() {
1343 var doc = getDocumentFromXML('<div>Alice</div>'),
1344 textNode = doc.root.contents()[0];
1346 expect(doc.undoStack).to.have.length(0);
1348 textNode.wrapWith({tagName: 'span', start:1, end:2});
1349 expect(doc.undoStack).to.have.length(1, '1');
1350 expect(doc.toXML()).to.equal('<div>A<span>l</span>ice</div>');
1353 expect(doc.undoStack).to.have.length(0, '2');
1354 expect(doc.toXML()).to.equal('<div>Alice</div>');
1357 expect(doc.undoStack).to.have.length(1, '3');
1358 expect(doc.toXML()).to.equal('<div>A<span>l</span>ice</div>');
1361 expect(doc.undoStack).to.have.length(0, '4');
1362 expect(doc.toXML()).to.equal('<div>Alice</div>');
1365 expect(doc.undoStack).to.have.length(0, '5');
1366 expect(doc.toXML()).to.equal('<div>Alice</div>');
1369 it('smoke tests 2', function() {
1370 var doc = getDocumentFromXML('<div>Alice</div>'),
1371 textNode = doc.root.contents()[0],
1372 path = textNode.getPath();
1374 textNode.setText('Alice ');
1375 textNode.setText('Alice h');
1376 textNode.setText('Alice ha');
1377 textNode.setText('Alice has');
1379 expect(textNode.getText()).to.equal('Alice has');
1382 expect(doc.root.contents()[0].getText()).to.equal('Alice ha', '1');
1385 expect(doc.root.contents()[0].getText()).to.equal('Alice h', '2');
1388 expect(doc.root.contents()[0].getText()).to.equal('Alice ha', '3');
1391 expect(doc.root.contents()[0].getText()).to.equal('Alice has', '4');
1395 textNode = doc.getNodeByPath(path);
1396 textNode.setText('Cat');
1398 textNode = doc.getNodeByPath(path);
1399 expect(textNode.getText()).to.equal('Alice h');
1403 var sampleMethod = function(val) {
1404 this._$.attr('x', val);
1405 this.triggerChangeEvent();
1408 var transformations = {
1409 'unaware': sampleMethod,
1410 'returning change root': {
1412 getChangeRoot: function() {
1413 return this.context;
1416 'implementing undo operation': {
1417 impl: function(t, val) {
1418 t.oldVal = this.getAttr('x');
1419 sampleMethod.call(this, val);
1422 this.setAttr('x', t.oldVal);
1427 _.pairs(transformations).forEach(function(pair) {
1429 transformaton = pair[1];
1431 describe(name + ' transformation: ', function() {
1432 var doc, node, nodePath;
1434 beforeEach(function() {
1435 doc = getDocumentFromXML('<div><test x="old"></test></div>');
1437 doc.registerExtension({elementNode: {transformations: {
1441 node = doc.root.contents()[0];
1442 nodePath = node.getPath();
1445 it('transforms as expected', function() {
1447 expect(node.getAttr('x')).to.equal('new');
1450 it('can be undone', function() {
1453 node = doc.getNodeByPath(nodePath);
1454 expect(node.getAttr('x')).to.equal('old');
1457 it('can be undone and then redone', function() {
1461 node = doc.getNodeByPath(nodePath);
1462 expect(node.getAttr('x')).to.equal('new');
1465 it('handles a sample scenario', function() {
1466 doc.root.contents()[0].test('1');
1467 doc.root.contents()[0].test('2');
1468 doc.root.contents()[0].test('3');
1469 doc.root.contents()[0].test('4');
1470 doc.root.contents()[0].test('5');
1472 expect(doc.root.contents()[0].getAttr('x')).to.equal('5', 'after initial transformations');
1474 expect(doc.root.contents()[0].getAttr('x')).to.equal('4', 'undo 1.1');
1476 expect(doc.root.contents()[0].getAttr('x')).to.equal('3', 'undo 1.2');
1478 expect(doc.root.contents()[0].getAttr('x')).to.equal('4', 'redo 1.1');
1480 expect(doc.root.contents()[0].getAttr('x')).to.equal('5', 'redo 1.2');
1482 expect(doc.root.contents()[0].getAttr('x')).to.equal('4', 'undo 2.1');
1483 doc.root.contents()[0].test('10');
1484 expect(doc.root.contents()[0].getAttr('x')).to.equal('10', 'additional transformation');
1485 expect(doc.redoStack.length).to.equal(0, 'transformation cleared redo stack');
1487 expect(doc.root.contents()[0].getAttr('x')).to.equal('10', 'empty redoStack so redo was noop');
1489 expect(doc.root.contents()[0].getAttr('x')).to.equal('4', 'undoing additional transformation');
1491 expect(doc.root.contents()[0].getAttr('x')).to.equal('10', 'redoing additional transformation');
1496 it('smoke tests nested transformations', function() {
1497 var doc = getDocumentFromXML('<div></div>');
1499 doc.registerExtension({elementNode: {transformations: {
1500 nested: function(v) {
1501 this._$.attr('innerAttr', v);
1502 this.triggerChangeEvent();
1504 outer: function(v) {
1506 this._$.attr('outerAttr', v);
1507 this.triggerChangeEvent();
1511 doc.root.outer('test1');
1512 doc.root.outer('test2');
1514 expect(doc.root.getAttr('innerAttr')).to.equal('test2');
1515 expect(doc.root.getAttr('outerAttr')).to.equal('test2');
1519 expect(doc.root.getAttr('innerAttr')).to.equal('test1');
1520 expect(doc.root.getAttr('outerAttr')).to.equal('test1');
1524 expect(doc.root.getAttr('innerAttr')).to.equal(undefined);
1525 expect(doc.root.getAttr('outerAttr')).to.equal(undefined);
1529 expect(doc.root.getAttr('innerAttr')).to.equal('test1');
1530 expect(doc.root.getAttr('outerAttr')).to.equal('test1');
1534 expect(doc.root.getAttr('innerAttr')).to.equal('test2');
1535 expect(doc.root.getAttr('outerAttr')).to.equal('test2');
1539 it('ignores transformation if document didn\'t emit change event', function() {
1540 var doc = getDocumentFromXML('<div></div>');
1542 doc.registerExtension({elementNode: {transformations: {
1549 expect(doc.undoStack.length).to.equal(0);
1553 describe('Transactions', function() {
1554 it('allows to undo/redo series of transformations at once', function() {
1555 var doc = getDocumentFromXML('<div></div>');
1557 doc.registerExtension({
1558 elementNode: {transformations: {
1560 this.setAttr('test', v);
1565 doc.startTransaction();
1569 doc.endTransaction();
1572 expect(doc.root.getAttr('test'), '1');
1574 expect(doc.root.getAttr('test'), '3');
1576 expect(doc.root.getAttr('test'), '1');
1578 expect(doc.root.getAttr('test'), '3');
1581 it('ignores empty transactions', function() {
1582 var doc = getDocumentFromXML('<div></div>');
1583 doc.startTransaction();
1584 doc.endTransaction();
1585 expect(doc.undoStack).to.have.length(0, 'empty transaction doesn\'t get pushed into undo stack');
1588 it('doesn\'t break on optimizations', function() {
1589 // This is a smoke test checking if optimizations made to transaction undoing
1590 // doesnt't break anything.
1591 var doc = getDocumentFromXML('<div smart="1" unaware="1"></div>');
1593 doc.registerExtension({
1594 elementNode: {transformations: {
1595 unaware: function(v) {
1596 this.setAttr('unware', v);
1597 this.triggerChangeEvent();
1600 impl: function(t, v) {
1601 t.oldVal = this.getAttr('smart');
1602 this.setAttr('smart', v);
1603 this.triggerChangeEvent();
1606 this.setAttr('smart', t.oldVal);
1607 this.triggerChangeEvent();
1613 doc.startTransaction();
1614 doc.root.smart('2');
1615 doc.root.unaware('2');
1616 doc.root.smart('3');
1617 doc.root.unaware('3');
1618 doc.endTransaction();
1622 expect(doc.root.getAttr('smart')).to.equal('1');
1623 expect(doc.root.getAttr('unaware')).to.equal('1');
1626 it('can have associated metadata', function() {
1627 var doc = getDocumentFromXML('<div></div>'),
1628 metadata = Object.create({});
1630 doc.registerExtension({document: {transformations: {
1632 this.trigger('change');
1636 doc.startTransaction(metadata);
1638 doc.endTransaction();
1640 var transaction = doc.undoStack[0];
1641 expect(transaction.metadata).to.equal(metadata);
1644 it('can be rolled back', function() {
1645 var doc = getDocumentFromXML('<root></root>');
1647 doc.startTransaction();
1648 doc.root.append({tagName: 'div'});
1649 doc.rollbackTransaction();
1651 expect(doc.undoStack.length).to.equal(0, 'nothing to undo');
1652 expect(doc.root.contents().length).to.equal(0);
1655 it('rollbacks and calls error handleor if error gets thrown', function() {
1656 var doc = getDocumentFromXML('<root></root>'),
1660 doc.transaction(function() {
1661 doc.root.append({tagName: 'div'});
1665 expect(spy.args[0][0]).to.equal(err);
1666 expect(doc.root.contents().length).to.equal(0);
1667 expect(doc.undoStack.length).to.equal(0);
1671 describe('Regression tests', function() {
1672 it('redos correctly after running its own undo followed by unaware transformation undo', function() {
1673 var doc = getDocumentFromXML('<section t="0"></section>');
1675 doc.registerExtension({elementNode: {transformations: {
1676 unaware: function() {
1677 this.triggerChangeEvent();
1681 this._$.attr('t', 1);
1682 this.triggerChangeEvent();
1685 this._$.attr('t', 0);
1695 expect(doc.root.getAttr('t')).to.equal('1');
1697 it('can perform undo of an operation performed after automatic transaction rollback', function() {
1698 var doc = getDocumentFromXML('<section></section>'),
1699 extension = {document: {transformations: {
1700 throwingTransformation: function() { throw new Error(); }
1703 doc.registerExtension(extension);
1705 doc.throwingTransformation();
1707 doc.transaction(function() {
1708 doc.root.setAttr('x', '2');
1711 expect(doc.undoStack.length).to.equal(1);
1712 expect(doc.root.getAttr('x')).to.equal('2');
1716 expect(doc.undoStack.length).to.equal(0);
1717 expect(doc.root.getAttr('x')).to.be.undefined;