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'});
167 it('can remove specific data', function() {
168 node.setData('key', 'value');
169 node.setData('key', undefined);
170 expect(node.getData('key')).to.be.undefined;
174 describe('Changing node tag', function() {
176 it('can change tag name', function() {
177 var node = elementNodeFromXML('<div></div>');
178 node = node.setTag('span');
179 expect(node.getTagName()).to.equal('span');
182 describe('Implementation specific expectations', function() {
183 it('keeps custom data', function() {
184 var node = elementNodeFromXML('<div></div>');
186 node.setData('key', 'value');
187 node = node.setTag('header');
189 expect(node.getTagName()).to.equal('header');
190 expect(node.getData()).to.eql({key: 'value'});
193 it('can change document root tag name', function() {
194 var doc = getDocumentFromXML('<div></div>');
195 doc.root.setTag('span');
196 expect(doc.root.getTagName()).to.equal('span');
199 it('keeps node contents', function() {
200 var node = elementNodeFromXML('<div><div></div></div>');
201 node = node.setTag('header');
202 expect(node.contents()).to.have.length(1);
207 describe('Setting node attributes', function() {
208 it('can set node attribute', function() {
209 var node = elementNodeFromXML('<div></div>');
211 node.setAttr('key', 'value');
212 expect(node.getAttr('key')).to.equal('value');
214 it('emits nodeAttrChange event', function() {
215 var node = elementNodeFromXML('<div key="value1"></div>'),
218 node.document.on('change', spy);
219 node.setAttr('key', 'value2');
220 var event = spy.args[0][0];
222 expect(event.type).to.equal('nodeAttrChange');
223 expect(event.meta.node.sameNode(node)).to.be.true;
224 expect(event.meta.attr).to.equal('key');
225 expect(event.meta.oldVal).to.equal('value1');
229 describe('Searching for the last child text node', function() {
231 '<div>xxx<div></div>last</div>',
232 '<div><div>last</div></div>',
233 '<div>xxx<div>last</div><div></div></div>'
234 ].forEach(function(xml, i) {
235 var example = 'example ' + i;
236 it('returns last child text node ' + example + ')', function() {
237 var doc = getDocumentFromXML(xml),
238 lastTextNode = doc.root.getLastTextNode();
239 expect(lastTextNode.getText()).to.equal('last', example);
245 describe('Basic TextNode properties', function() {
246 it('can have its text set', function() {
247 var node = elementNodeFromXML('<div>Alice</div>'),
248 textNode = node.contents()[0];
250 textNode.setText('Cat');
251 expect(textNode.getText()).to.equal('Cat');
254 it('emits nodeTextChange', function() {
255 var node = elementNodeFromXML('<div>Alice</div>'),
256 textNode = node.contents()[0],
259 textNode.document.on('change', spy);
260 textNode.setText('Cat');
262 var event = spy.args[0][0];
263 expect(event.type).to.equal('nodeTextChange');
266 it('puts NodeElement after itself', function() {
267 var node = elementNodeFromXML('<div>Alice</div>'),
268 textNode = node.contents()[0],
269 returned = textNode.after({tagName:'div'});
270 expect(returned.sameNode(node.contents()[1])).to.be.true;
273 it('puts NodeElement before itself', function() {
274 var node = elementNodeFromXML('<div>Alice</div>'),
275 textNode = node.contents()[0],
276 returned = textNode.before({tagName:'div'});
277 expect(returned.sameNode(node.contents()[0])).to.be.true;
280 describe('Wrapping TextNode contents', function() {
282 it('wraps DocumentTextElement', function() {
283 var node = elementNodeFromXML('<section>Alice</section>'),
284 textNode = node.contents()[0];
286 var returned = textNode.wrapWith({tagName: 'header'}),
287 parent = textNode.parent(),
288 parent2 = node.contents()[0];
290 expect(returned.sameNode(parent)).to.be.equal(true, 'wrapper is a parent');
291 expect(returned.sameNode(parent2)).to.be.equal(true, 'wrapper has a correct parent');
292 expect(returned.getTagName()).to.equal('header');
295 describe('wrapping part of DocumentTextElement', function() {
296 [{start: 5, end: 12}, {start: 12, end: 5}].forEach(function(offsets) {
297 it('wraps in the middle ' + offsets.start + '/' + offsets.end, function() {
298 var node = elementNodeFromXML('<section>Alice has a cat</section>'),
299 textNode = node.contents()[0];
301 var returned = textNode.wrapWith({tagName: 'header', attrs: {'attr1': 'value1'}, start: offsets.start, end: offsets.end}),
302 contents = node.contents();
304 expect(contents.length).to.equal(3);
306 expect(contents[0].nodeType).to.be.equal(Node.TEXT_NODE, 'first node is text node');
307 expect(contents[0].getText()).to.equal('Alice');
309 expect(contents[1].sameNode(returned)).to.be.true;
310 expect(returned.getTagName()).to.equal('header');
311 expect(returned.getAttr('attr1')).to.equal('value1');
312 expect(contents[1].contents().length).to.equal(1, 'wrapper has one node inside');
313 expect(contents[1].contents()[0].getText()).to.equal(' has a ');
315 expect(contents[2].nodeType).to.be.equal(Node.TEXT_NODE, 'third node is text node');
316 expect(contents[2].getText()).to.equal('cat');
320 it('wraps whole text inside DocumentTextElement if offsets span entire content', function() {
321 var node = elementNodeFromXML('<section>Alice has a cat</section>'),
322 textNode = node.contents()[0];
324 textNode.wrapWith({tagName: 'header', start: 0, end: 15});
326 var contents = node.contents();
327 expect(contents.length).to.equal(1);
328 expect(contents[0].getTagName()).to.equal('header');
329 expect(contents[0].contents()[0].getText()).to.equal('Alice has a cat');
334 describe('Dividing text node into two with element node', function() {
335 it('can divide text node with element node, splitting text node into two', function() {
336 var doc = getDocumentFromXML('<div>Alice has a cat</div>'),
337 text = doc.root.contents()[0];
339 var returned = text.divideWithElementNode({tagName: 'aside'}, {offset: 5}),
340 contents = doc.root.contents(),
341 lhsText = contents[0],
342 rhsText = contents[2];
344 expect(lhsText.getText()).to.equal('Alice');
345 expect(returned.sameNode(contents[1]));
346 expect(rhsText.getText()).to.equal(' has a cat');
349 it('treats dividing at the very end as appending after it', function() {
350 var doc = getDocumentFromXML('<div>Alice has a cat</div>'),
351 text = doc.root.contents()[0];
354 var returned = text.divideWithElementNode({tagName: 'aside'}, {offset: 15}),
355 contents = doc.root.contents(),
356 textNode = contents[0],
357 elementNode = contents[1];
359 expect(contents.length).to.equal(2);
360 expect(textNode.getText()).to.equal('Alice has a cat');
361 expect(returned.sameNode(elementNode)).to.be.true;
362 expect(elementNode.getTagName()).to.equal('aside');
365 it('treats dividing at the very beginning as prepending before it', function() {
366 var doc = getDocumentFromXML('<div>Alice has a cat</div>'),
367 text = doc.root.contents()[0];
369 var returned = text.divideWithElementNode({tagName: 'aside'}, {offset: 0}),
370 contents = doc.root.contents(),
371 textNode = contents[1],
372 elementNode = contents[0];
374 expect(contents.length).to.equal(2);
375 expect(textNode.getText()).to.equal('Alice has a cat');
376 expect(returned.sameNode(elementNode)).to.be.true;
377 expect(elementNode.getTagName()).to.equal('aside');
382 describe('Manipulations', function() {
384 describe('detaching nodes', function() {
385 it('can detach document root node', function() {
386 var doc = getDocumentFromXML('<div></div>');
389 expect(doc.root).to.equal(null);
393 describe('replacing node with another one', function() {
394 it('replaces node with another one', function() {
395 var doc = getDocumentFromXML('<div><a></a></div>'),
396 a = doc.root.contents()[0];
398 var c = a.replaceWith({tagName: 'b', attrs: {b:'1'}});
400 expect(doc.root.contents()[0].sameNode(c));
401 expect(c.getTagName()).to.equal('b');
402 expect(c.getAttr('b')).to.equal('1');
404 it('can replace document root', function() {
405 var doc = getDocumentFromXML('<div></div>');
407 var header = doc.root.replaceWith({tagName: 'header'});
409 expect(doc.root.sameNode(header)).to.be.true;
410 expect(doc.containsNode(header)).to.be.true;
414 it('merges adjacent text nodes resulting from detaching an element node in between', function() {
415 var doc = getDocumentFromXML('<div>Alice <span>has</span>a cat</div>'),
416 span = doc.root.contents()[1];
420 var rootContents = doc.root.contents();
421 expect(rootContents).to.have.length(1, 'one child left');
422 expect(rootContents[0].getText()).to.equal('Alice a cat');
425 it('merges adjacent text nodes resulting from moving an element node in between', function() {
426 var doc = getDocumentFromXML('<div><a></a>Alice <span>has</span>a cat</div>'),
427 span = doc.root.contents()[2],
428 a = doc.root.contents()[0];
432 var rootContents = doc.root.contents();
433 expect(rootContents).to.have.length(2, 'one child left');
434 expect(rootContents[1].getText()).to.equal('Alice a cat');
437 it('inserts node at index', function() {
438 var doc = getDocumentFromXML('<div><a></a><b></b><c></c></div>'),
439 b = doc.root.contents()[1];
441 var inserted = doc.root.insertAtIndex({tagName: 'test'}, 1);
443 expect(doc.root.contents()[1].sameNode(inserted)).to.equal(true, 'inserted node returned');
444 expect(b.getIndex()).to.equal(2, 'b node shifted right');
447 it('appends node when inserting node at index out of range', function() {
448 var doc = getDocumentFromXML('<div></div>');
450 var test1 = doc.root.insertAtIndex({tagName: 'test1'}, 0),
451 test2 = doc.root.insertAtIndex({tagName: 'test1'}, 10);
453 expect(doc.root.contents()[0].sameNode(test1)).to.equal(true, 'inserting at index 0 of empty nodes appends node');
454 expect(doc.root.contents().length).to.equal(1, 'inserting at index out of range does nothing');
455 expect(test2).to.equal(undefined, 'inserting at index out of range returns undefined');
458 it('appends element node to another element node', function() {
459 var node1 = elementNodeFromParams({tag: 'div'}),
460 node2 = elementNodeFromParams({tag: 'a'}),
461 node3 = elementNodeFromParams({tag: 'p'});
464 expect(node1.contents()[0].sameNode(node2)).to.be.true;
465 expect(node1.contents()[1].sameNode(node3)).to.be.true;
468 it('prepends element node to another element node', function() {
469 var node1 = elementNodeFromParams({tag: 'div'}),
470 node2 = elementNodeFromParams({tag: 'a'}),
471 node3 = elementNodeFromParams({tag: 'p'});
472 node1.prepend(node2);
473 node1.prepend(node3);
474 expect(node1.contents()[0].sameNode(node3)).to.be.true;
475 expect(node1.contents()[1].sameNode(node2)).to.be.true;
478 describe('adding text nodes', function() {
479 it('merges text nodes on append', function() {
480 var doc = getDocumentFromXML('<root>text1</root>'),
482 returned = doc.root.append({text: 'text2'});
483 expect(doc.root.contents().length).to.equal(1);
484 expect(returned.sameNode(doc.root.contents()[0])).to.equal(true, 'modified node returned');
485 expect(doc.root.contents()[0].getText()).to.equal('text1text2');
488 it('merges text nodes on prepend', function() {
489 var doc = getDocumentFromXML('<root>text1</root>'),
491 returned = doc.root.prepend({text: 'text2'});
492 expect(doc.root.contents().length).to.equal(1);
493 expect(returned.sameNode(doc.root.contents()[0])).to.equal(true, 'modified node returned');
494 expect(doc.root.contents()[0].getText()).to.equal('text2text1');
497 it('merges text nodes on before text node', function() {
498 var doc = getDocumentFromXML('<root>text1</root>'),
499 textNode = doc.root.contents()[0],
501 returned = textNode.before({text: 'text2'});
502 expect(doc.root.contents().length).to.equal(1);
503 expect(returned.sameNode(doc.root.contents()[0])).to.equal(true, 'modified node returned');
504 expect(doc.root.contents()[0].getText()).to.equal('text2text1');
507 it('merges text nodes on after text node', function() {
508 var doc = getDocumentFromXML('<root>text1</root>'),
509 textNode = doc.root.contents()[0],
511 returned = textNode.after({text: 'text2'});
512 expect(doc.root.contents().length).to.equal(1);
513 expect(returned.sameNode(doc.root.contents()[0])).to.equal(true, 'modified node returned');
514 expect(doc.root.contents()[0].getText()).to.equal('text1text2');
517 it('merges text nodes on before element node', function() {
518 var doc = getDocumentFromXML('<root>text1<div></div></root>'),
519 textNode = doc.root.contents()[0],
520 div = doc.root.contents()[1],
522 returned = div.before({text: 'text2'});
523 expect(doc.root.contents().length).to.equal(2);
524 expect(returned.sameNode(doc.root.contents()[0])).to.equal(true, 'modified node returned');
525 expect(textNode.getText()).to.equal('text1text2');
528 it('merges text nodes on after element node', function() {
529 var doc = getDocumentFromXML('<root><div></div>text1</root>'),
530 textNode = doc.root.contents()[1],
531 div = doc.root.contents()[0],
533 returned = div.after({text: 'text2'});
534 expect(doc.root.contents().length).to.equal(2);
535 expect(returned.sameNode(doc.root.contents()[1])).to.equal(true, 'modified node returned');
536 expect(textNode.getText()).to.equal('text2text1');
540 it('wraps root element node with another element node', function() {
541 var node = elementNodeFromXML('<div></div>'),
542 wrapper = elementNodeFromXML('<wrapper></wrapper>');
544 node.wrapWith(wrapper);
545 expect(node.parent().sameNode(wrapper)).to.be.true;
546 expect(node.document.root.sameNode(wrapper)).to.be.true;
549 it('wraps element node with another element node', function() {
550 var doc = getDocumentFromXML('<section><div></div></section>'),
551 div = doc.root.contents()[0];
553 var wrapper = div.wrapWith({tagName: 'wrapper'});
554 expect(wrapper.sameNode(doc.root.contents()[0])).to.equal(true, '1');
555 expect(div.parent().sameNode(wrapper)).to.equal(true, '2');
556 expect(wrapper.contents()[0].sameNode(div)).to.equal(true, '3');
559 it('wraps element outside of document tree', function() {
560 var doc = getDocumentFromXML('<section><div></div></section>'),
561 node = doc.createDocumentNode({tagName: 'node'});
563 node.wrapWith({tagName: 'wrapper'});
564 expect(node.parent().getTagName()).to.equal('wrapper');
565 expect(node.parent().contents()[0].sameNode(node)).to.be.true;
566 expect(doc.root.getTagName()).to.equal('section');
569 it('unwraps element node contents', function() {
570 var node = elementNodeFromXML('<div>Alice <div>has <span>propably</span> a cat</div>!</div>'),
571 outerDiv = node.contents()[1];
573 outerDiv.unwrapContent();
575 expect(node.contents().length).to.equal(3);
576 expect(node.contents()[0].getText()).to.equal('Alice has ');
577 expect(node.contents()[1].getTagName()).to.equal('span');
578 expect(node.contents()[2].getText()).to.equal(' a cat!');
581 it('unwrap single element node from its parent', function() {
582 var doc = getDocumentFromXML('<div><a><b></b></a></div>'),
584 a = div.contents()[0],
587 var parent = b.unwrap();
589 expect(parent.sameNode(div)).to.equal(true, 'returns new parent');
590 expect(div.contents()).to.have.length(1, 'root contains only one node');
591 expect(div.contents()[0].sameNode(b)).to.equal(true, 'node got unwrapped');
594 it('unwrap single text node from its parent', function() {
595 var doc = getDocumentFromXML('<div>Some <span>text</span>!</div>'),
597 span = div.contents()[1],
598 text = span.contents()[0];
600 var parent = text.unwrap();
602 expect(parent.sameNode(div)).to.equal(true, 'returns new parent');
603 expect(div.contents()).to.have.length(1, 'root contains only one node');
604 expect(div.contents()[0].getText()).to.equal('Some text!');
607 describe('Wrapping text', function() {
608 it('wraps text spanning multiple sibling TextNodes', function() {
609 var section = elementNodeFromXML('<section>Alice has a <span>small</span> cat</section>'),
610 wrapper = section.wrapText({
611 _with: {tagName: 'span', attrs: {'attr1': 'value1'}},
617 expect(section.contents().length).to.equal(2);
618 expect(section.contents()[0].nodeType).to.equal(Node.TEXT_NODE);
619 expect(section.contents()[0].getText()).to.equal('Alice ');
621 var wrapper2 = section.contents()[1];
622 expect(wrapper2.sameNode(wrapper)).to.be.true;
623 expect(wrapper.getTagName()).to.equal('span');
625 var wrapperContents = wrapper.contents();
626 expect(wrapperContents.length).to.equal(3);
627 expect(wrapperContents[0].getText()).to.equal('has a ');
629 expect(wrapperContents[1].nodeType).to.equal(Node.ELEMENT_NODE);
630 expect(wrapperContents[1].contents().length).to.equal(1);
631 expect(wrapperContents[1].contents()[0].getText()).to.equal('small');
635 describe('Wrapping Nodes', function() {
636 it('wraps multiple sibling nodes', function() {
637 var section = elementNodeFromXML('<section>Alice<div>has</div><div>a cat</div></section>'),
638 aliceText = section.contents()[0],
639 firstDiv = section.contents()[1],
640 lastDiv = section.contents()[section.contents().length -1];
642 var returned = section.document.wrapNodes({
645 _with: {tagName: 'header'}
648 var sectionContentss = section.contents(),
649 header = sectionContentss[0],
650 headerContents = header.contents();
652 expect(sectionContentss).to.have.length(1);
653 expect(header.sameNode(returned)).to.equal(true, 'wrapper returned');
654 expect(header.parent().sameNode(section)).to.be.true;
655 expect(headerContents).to.have.length(3);
656 expect(headerContents[0].sameNode(aliceText)).to.equal(true, 'first node wrapped');
657 expect(headerContents[1].sameNode(firstDiv)).to.equal(true, 'second node wrapped');
658 expect(headerContents[2].sameNode(lastDiv)).to.equal(true, 'third node wrapped');
661 it('wraps multiple sibling Elements - middle case', function() {
662 var section = elementNodeFromXML('<section><div></div><div></div><div></div><div></div></section>'),
663 div2 = section.contents()[1],
664 div3 = section.contents()[2];
666 section.document.wrapNodes({
669 _with: {tagName: 'header'}
672 var sectionContentss = section.contents(),
673 header = sectionContentss[1],
674 headerChildren = header.contents();
676 expect(sectionContentss).to.have.length(3);
677 expect(headerChildren).to.have.length(2);
678 expect(headerChildren[0].sameNode(div2)).to.equal(true, 'first node wrapped');
679 expect(headerChildren[1].sameNode(div3)).to.equal(true, 'second node wrapped');
685 var getTextNodes = function(text, doc) {
688 var search = function(node) {
689 node.contents().forEach(function(node) {
690 if(node.nodeType === Node.TEXT_NODE) {
691 if(node.getText() === text) {
703 var getTextNode = function(text, doc) {
704 var nodes = getTextNodes(text, doc),
706 if(nodes.length === 0) {
707 error = 'Text not found';
708 } else if(nodes.length > 1) {
709 error = 'Text not unique';
710 } else if(nodes[0].getText() !== text) {
711 error = 'I was trying to cheat your test :(';
714 throw new Error(error);
719 describe('Removing arbitrary text', function() {
720 it('removes within single text element', function() {
721 var doc = getDocumentFromXML('<div>Alice</div>'),
722 text = getTextNode('Alice', doc);
733 expect(doc.root.contents().length).to.equal(1);
734 expect(doc.root.contents()[0].getText()).to.equal('Ae');
736 it('removes across elements - 1', function() {
737 var doc = getDocumentFromXML('<div><a>aaa</a><b>bbb</b></div>');
741 node: getTextNode('aaa', doc),
745 node: getTextNode('bbb', doc),
750 var contents = doc.root.contents();
751 expect(contents.length).to.equal(2);
752 expect(contents[0].contents()[0].getText()).to.equal('aa');
753 expect(contents[1].contents()[0].getText()).to.equal('b');
755 it('removes across elements - 2', function() {
756 var doc = getDocumentFromXML('<a><b><c>ccc</c></b>xxx</a>');
759 node: getTextNode('ccc', doc),
763 node: getTextNode('xxx', doc),
768 var contents = doc.root.contents();
769 expect(contents.length).to.equal(2);
770 expect(contents[0].getTagName()).to.equal('b');
771 expect(contents[1].getText()).to.equal('x');
773 var bContents = contents[0].contents();
774 expect(bContents.length).to.equal(1);
775 expect(bContents[0].getTagName()).to.equal('c');
776 expect(bContents[0].contents().length).to.equal(1);
777 expect(bContents[0].contents()[0].getText()).to.equal('cc');
779 it('remove across elements - 3 (merged text nodes)', function() {
780 var doc = getDocumentFromXML('<div>Alice <span>has</span> a cat</div>');
783 node: getTextNode('Alice ', doc),
787 node: getTextNode(' a cat', doc),
791 var contents = doc.root.contents();
792 expect(contents.length).to.equal(1);
793 expect(contents[0].getText()).to.equal('Acat');
795 it('remove across elements - 4', function() {
796 var doc = getDocumentFromXML('<div>Alice <div>has <span>a</span> cat</div></div>');
799 node: getTextNode('Alice ', doc),
803 node: getTextNode(' cat', doc),
807 var contents = doc.root.contents();
808 expect(contents.length).to.equal(2);
809 expect(contents[0].getText()).to.equal('A');
810 expect(contents[1].getTagName()).to.equal('div');
811 expect(contents[1].contents().length).to.equal(1);
812 expect(contents[1].contents()[0].getText()).to.equal('cat');
814 it('removes across elements - 5 (whole document)', function() {
815 var doc = getDocumentFromXML('<div>Alice <div>has <span>a</span> cat</div>!!!</div>');
818 node: getTextNode('Alice ', doc),
822 node: getTextNode('!!!', doc),
827 expect(doc.root.getTagName()).to.equal('div');
828 expect(doc.root.contents().length).to.equal(1);
829 expect(doc.root.contents()[0].getText()).to.equal('');
831 it('removes nodes in between', function() {
832 var doc = getDocumentFromXML('<div><a>aaa<x>!</x></a>xxx<x></x><b><x>!</x>bbb</b></div>');
835 node: getTextNode('aaa', doc),
839 node: getTextNode('bbb', doc),
844 var contents = doc.root.contents();
845 expect(contents.length).to.equal(2, 'two nodes survived');
846 expect(contents[0].getTagName()).to.equal('a');
847 expect(contents[1].getTagName()).to.equal('b');
848 expect(contents[0].contents().length).to.equal(1);
849 expect(contents[0].contents()[0].getText()).to.equal('aa');
850 expect(contents[1].contents().length).to.equal(1);
851 expect(contents[1].contents()[0].getText()).to.equal('b');
855 describe('Splitting text', function() {
857 it('splits TextNode\'s parent into two ElementNodes', function() {
858 var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
860 text = section.contents()[0].contents()[0];
862 var returnedValue = text.split({offset: 5});
863 expect(section.contents().length).to.equal(2, 'section has two children');
865 var header1 = section.contents()[0];
866 var header2 = section.contents()[1];
868 expect(header1.getTagName()).to.equal('header', 'first section child ok');
869 expect(header1.contents().length).to.equal(1, 'first header has one child');
870 expect(header1.contents()[0].getText()).to.equal('Some ', 'first header has correct content');
871 expect(header2.getTagName()).to.equal('header', 'second section child ok');
872 expect(header2.contents().length).to.equal(1, 'second header has one child');
873 expect(header2.contents()[0].getText()).to.equal('header', 'second header has correct content');
875 expect(returnedValue.first.sameNode(header1)).to.equal(true, 'first node returned');
876 expect(returnedValue.second.sameNode(header2)).to.equal(true, 'second node returned');
879 it('leaves empty copy of ElementNode if splitting at the very beginning', function() {
880 var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
882 text = section.contents()[0].contents()[0];
884 text.split({offset: 0});
886 var header1 = section.contents()[0];
887 var header2 = section.contents()[1];
889 expect(header1.contents().length).to.equal(0);
890 expect(header2.contents()[0].getText()).to.equal('Some header');
893 it('leaves empty copy of ElementNode if splitting at the very end', function() {
894 var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
896 text = section.contents()[0].contents()[0];
898 text.split({offset: 11});
900 var header1 = section.contents()[0];
901 var header2 = section.contents()[1];
903 expect(header1.contents()[0].getText()).to.equal('Some header');
904 expect(header2.contents().length).to.equal(0);
907 it('keeps TextNodes\'s parent\'s children elements intact', function() {
908 var doc = getDocumentFromXML('<section><header>A <span>fancy</span> and <span>nice</span> header</header></section>'),
910 header = section.contents()[0],
911 textAnd = header.contents()[2];
913 textAnd.split({offset: 2});
915 var sectionContents = section.contents();
916 expect(sectionContents.length).to.equal(2, 'Section has two children');
917 expect(sectionContents[0].getTagName()).to.equal('header', 'First section node is a header');
918 expect(sectionContents[1].getTagName()).to.equal('header', 'Second section node is a header');
920 var firstHeaderContents = sectionContents[0].contents();
921 expect(firstHeaderContents.length).to.equal(3, 'First header has three children');
922 expect(firstHeaderContents[0].getText()).to.equal('A ', 'First header starts with a text');
923 expect(firstHeaderContents[1].getTagName()).to.equal('span', 'First header has span in the middle');
924 expect(firstHeaderContents[2].getText()).to.equal(' a', 'First header ends with text');
926 var secondHeaderContents = sectionContents[1].contents();
927 expect(secondHeaderContents.length).to.equal(3, 'Second header has three children');
928 expect(secondHeaderContents[0].getText()).to.equal('nd ', 'Second header starts with text');
929 expect(secondHeaderContents[1].getTagName()).to.equal('span', 'Second header has span in the middle');
930 expect(secondHeaderContents[2].getText()).to.equal(' header', 'Second header ends with text');
934 describe('Events', function() {
935 it('emits nodeDetached event on node detach', function() {
936 var node = elementNodeFromXML('<div><div></div></div>'),
937 innerNode = node.contents()[0],
939 node.document.on('change', spy);
941 var detached = innerNode.detach(),
942 event = spy.args[0][0];
944 expect(event.type).to.equal('nodeDetached');
945 expect(event.meta.node.sameNode(detached, 'detached node in event meta'));
946 expect(event.meta.parent.sameNode(node), 'original parent node in event meta');
949 it('emits nodeAdded event when appending new node', function() {
950 var node = elementNodeFromXML('<div></div>'),
952 node.document.on('change', spy);
954 var appended = node.append({tagName:'div'}),
955 event = spy.args[0][0];
956 expect(event.type).to.equal('nodeAdded');
957 expect(event.meta.node.sameNode(appended)).to.be.true;
960 it('emits nodeMoved when appending aready existing node', function() {
961 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
962 a = node.contents()[0],
963 b = node.contents()[1],
965 node.document.on('change', spy);
967 var appended = a.append(b),
968 event = spy.args[0][0];
970 expect(spy.callCount).to.equal(1);
971 expect(event.type).to.equal('nodeMoved');
972 expect(event.meta.node.sameNode(appended)).to.be.true;
973 expect(node.document.root.sameNode(event.meta.parent)).to.equal(true, 'previous parent attached to event meta');
976 it('emits nodeAdded event when prepending new node', function() {
977 var node = elementNodeFromXML('<div></div>'),
979 node.document.on('change', spy);
981 var prepended = node.prepend({tagName:'div'}),
982 event = spy.args[0][0];
983 expect(event.type).to.equal('nodeAdded');
984 expect(event.meta.node.sameNode(prepended)).to.be.true;
987 it('emits nodeMoved when prepending aready existing node', function() {
988 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
989 a = node.contents()[0],
990 b = node.contents()[1],
992 node.document.on('change', spy);
994 var prepended = a.prepend(b),
995 event = spy.args[0][0];
996 expect(spy.callCount).to.equal(1);
997 expect(event.type).to.equal('nodeMoved');
998 expect(event.meta.node.sameNode(prepended)).to.be.true;
1001 it('emits nodeAdded event when inserting node after another', function() {
1002 var node = elementNodeFromXML('<div><a></a></div>').contents()[0],
1004 node.document.on('change', spy);
1006 var inserted = node.after({tagName:'div'}),
1007 event = spy.args[0][0];
1008 expect(event.type).to.equal('nodeAdded');
1009 expect(event.meta.node.sameNode(inserted)).to.be.true;
1012 it('emits nodeMoved when inserting aready existing node after another', function() {
1013 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
1014 a = node.contents()[0],
1015 b = node.contents()[1],
1017 node.document.on('change', spy);
1018 var inserted = b.after(a),
1019 event = spy.args[0][0];
1021 expect(spy.callCount).to.equal(1);
1022 expect(event.type).to.equal('nodeMoved');
1023 expect(event.meta.node.sameNode(inserted)).to.be.true;
1026 it('emits nodeAdded event when inserting node before another', function() {
1027 var node = elementNodeFromXML('<div><a></a></div>').contents()[0],
1029 node.document.on('change', spy);
1031 var inserted = node.before({tagName:'div'}),
1032 event = spy.args[0][0];
1033 expect(event.type).to.equal('nodeAdded');
1034 expect(event.meta.node.sameNode(inserted)).to.be.true;
1037 it('emits nodeAdded when inserting aready existing node before another', function() {
1038 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
1039 a = node.contents()[0],
1040 b = node.contents()[1],
1042 node.document.on('change', spy);
1043 var inserted = a.before(b),
1044 event = spy.args[0][0];
1046 expect(spy.callCount).to.equal(1);
1047 expect(event.type).to.equal('nodeMoved');
1048 expect(event.meta.node.sameNode(inserted)).to.be.true;
1051 it('emits nodeDetached and nodeAdded when replacing root node with another', function() {
1052 var doc = getDocumentFromXML('<a></a>'),
1056 doc.on('change', spy);
1058 doc.root.replaceWith({tagName: 'b'});
1060 expect(spy.callCount).to.equal(2);
1062 var event1 = spy.args[0][0],
1063 event2 = spy.args[1][0];
1065 expect(event1.type).to.equal('nodeDetached');
1066 expect(event1.meta.node.sameNode(oldRoot)).to.equal(true, 'root node in nodeDetached event metadata');
1067 expect(event2.type).to.equal('nodeAdded');
1068 expect(event2.meta.node.sameNode(doc.root)).to.equal(true, 'new root node in nodelAdded event meta');
1072 ['append', 'prepend', 'before', 'after'].forEach(function(insertionMethod) {
1073 it('emits nodeDetached for node moved from a document tree to out of document node ' + insertionMethod, function() {
1074 var doc = getDocumentFromXML('<div><a></a></div>'),
1075 a = doc.root.contents()[0],
1078 doc.on('change', spy);
1080 var newNode = doc.createDocumentNode({tagName: 'b'}),
1081 newNodeInner = newNode.append({tagName:'c'});
1083 newNodeInner[insertionMethod](a);
1085 var event = spy.args[0][0];
1086 expect(event.type).to.equal('nodeDetached');
1087 expect(event.meta.node.sameNode(a));
1090 it('doesn\'t emit nodeDetached event for already out of document node moved to out of document node' + insertionMethod, function() {
1091 var doc = getDocumentFromXML('<div><a></a></div>'),
1094 doc.on('change', spy);
1096 var newNode = doc.createDocumentNode({tagName: 'b'});
1097 newNode.append({tagName:'c'});
1099 expect(spy.callCount).to.equal(0);
1106 describe('Traversing', function() {
1107 describe('Basic', function() {
1108 it('can access node parent', function() {
1109 var doc = getDocumentFromXML('<a><b></b></a>'),
1111 b = a.contents()[0];
1113 expect(a.parent()).to.equal(null, 'parent of a root is null');
1114 expect(b.parent().sameNode(a)).to.be.true;
1116 it('can access node parents', function() {
1117 var doc = getDocumentFromXML('<a><b><c></c></b></a>'),
1119 b = a.contents()[0],
1120 c = b.contents()[0];
1122 var parents = c.parents();
1124 expect(parents[0].sameNode(b)).to.be.true;
1125 expect(parents[1].sameNode(a)).to.be.true;
1129 describe('finding sibling parents of two elements', function() {
1130 it('returns elements themself if they have direct common parent', function() {
1131 var doc = getDocumentFromXML('<section><div><div>A</div><div>B</div></div></section>'),
1132 wrappingDiv = doc.root.contents()[0],
1133 divA = wrappingDiv.contents()[0],
1134 divB = wrappingDiv.contents()[1];
1136 var siblingParents = doc.getSiblingParents({node1: divA, node2: divB});
1138 expect(siblingParents.node1.sameNode(divA)).to.equal(true, 'divA');
1139 expect(siblingParents.node2.sameNode(divB)).to.equal(true, 'divB');
1142 it('returns sibling parents - example 1', function() {
1143 var doc = getDocumentFromXML('<section>Alice <span>has a cat</span></section>'),
1144 aliceText = doc.root.contents()[0],
1145 span = doc.root.contents()[1],
1146 spanText = span.contents()[0];
1148 var siblingParents = doc.getSiblingParents({node1: aliceText, node2: spanText});
1150 expect(siblingParents.node1.sameNode(aliceText)).to.equal(true, 'aliceText');
1151 expect(siblingParents.node2.sameNode(span)).to.equal(true, 'span');
1154 it('returns node itself for two same nodes', function() {
1155 var doc = getDocumentFromXML('<section><div></div></section>'),
1156 div = doc.root.contents()[0];
1158 var siblingParents = doc.getSiblingParents({node1: div, node2: div});
1159 expect(!!siblingParents.node1 && !!siblingParents.node2).to.equal(true, 'nodes defined');
1160 expect(siblingParents.node1.sameNode(div)).to.be.equal(true, 'node1');
1161 expect(siblingParents.node2.sameNode(div)).to.be.equal(true, 'node2');
1166 describe('Serializing document to WLXML', function() {
1167 it('keeps document intact when no changes have been made', function() {
1168 var xmlIn = '<section>Alice<div>has</div>a <span class="uri" meta-uri="http://cat.com">cat</span>!</section>',
1169 doc = getDocumentFromXML(xmlIn),
1170 xmlOut = doc.toXML();
1172 var parser = new DOMParser(),
1173 input = parser.parseFromString(xmlIn, 'application/xml').childNodes[0],
1174 output = parser.parseFromString(xmlOut, 'application/xml').childNodes[0];
1176 expect(input.isEqualNode(output)).to.be.true;
1179 it('keeps entities intact', function() {
1180 var xmlIn = '<section>< ></section>',
1181 doc = getDocumentFromXML(xmlIn),
1182 xmlOut = doc.toXML();
1183 expect(xmlOut).to.equal(xmlIn);
1185 it('keeps entities intact when they form html/xml', function() {
1186 var xmlIn = '<section><abc></section>',
1187 doc = getDocumentFromXML(xmlIn),
1188 xmlOut = doc.toXML();
1189 expect(xmlOut).to.equal(xmlIn);
1193 describe('Extension API', function() {
1194 var doc, extension, elementNode, textNode;
1196 beforeEach(function() {
1197 doc = getDocumentFromXML('<section>Alice<div class="test_class"></div></section>');
1198 elementNode = doc.root;
1199 textNode = doc.root.contents()[0];
1202 expect(elementNode.testTransformation).to.be.undefined;
1203 expect(textNode.testTransformation).to.be.undefined;
1204 expect(doc.testTransformation).to.be.undefined;
1206 expect(doc.testMethod).to.be.undefined;
1207 expect(elementNode.testMethod).to.be.undefined;
1208 expect(textNode.testMethod).to.be.undefined;
1209 expect(elementNode.elementTestMethod).to.be.undefined;
1210 expect(textNode.textTestMethod).to.be.undefined;
1213 it('allows adding method to a document', function() {
1214 extension = {document: {methods: {
1215 testMethod: function() { return this; }
1218 doc.registerExtension(extension);
1219 expect(doc.testMethod()).to.equal(doc, 'context is set to a document instance');
1222 it('allows adding transformation to a document', function() {
1223 extension = {document: {transformations: {
1224 testTransformation: function() { return this; },
1225 testTransformation2: {impl: function() { return this;}}
1228 doc.registerExtension(extension);
1229 expect(doc.testTransformation()).to.equal(doc, 'context is set to a document instance');
1230 expect(doc.testTransformation2()).to.equal(doc, 'context is set to a document instance');
1233 it('allows adding method to a DocumentNode instance', function() {
1237 testMethod: function() { return this; }
1242 textTestMethod: function() { return this; }
1247 elementTestMethod: function() { return this; }
1252 doc.registerExtension(extension);
1255 elementNode = doc.root;
1256 textNode = doc.root.contents()[0];
1258 expect(elementNode.testMethod().sameNode(elementNode)).to.equal(true, 'context is set to a node instance');
1259 expect(textNode.testMethod().sameNode(textNode)).to.equal(true, 'context is set to a node instance');
1261 expect(elementNode.elementTestMethod().sameNode(elementNode)).to.be.true;
1262 expect(elementNode.textTestMethod).to.be.undefined;
1264 expect(textNode.textTestMethod().sameNode(textNode)).to.be.true;
1265 expect(textNode.elementTestMethod).to.be.undefined;
1268 it('allows adding transformation to a DocumentNode', function() {
1272 testTransformation: function() { return this; },
1273 testTransformation2: {impl: function() { return this;}}
1278 textTestTransformation: function() { return this; }
1283 elementTestTransformation: function() { return this; }
1288 doc.registerExtension(extension);
1291 elementNode = doc.root;
1292 textNode = doc.root.contents()[0];
1294 expect(elementNode.testTransformation().sameNode(elementNode)).to.equal(true, '1');
1295 expect(elementNode.testTransformation2().sameNode(elementNode)).to.equal(true, '2');
1296 expect(textNode.testTransformation().sameNode(textNode)).to.equal(true, '3');
1297 expect(textNode.testTransformation2().sameNode(textNode)).to.equal(true, '4');
1299 expect(elementNode.elementTestTransformation().sameNode(elementNode)).to.be.true;
1300 expect(elementNode.textTestTransformation).to.be.undefined;
1302 expect(textNode.textTestTransformation().sameNode(textNode)).to.be.true;
1303 expect(textNode.elementTestTransfomation).to.be.undefined;
1306 it('allows text/element node methods and transformations to access node and transormations on document node', function() {
1308 var doc = getDocumentFromXML('<div>text</div>');
1310 doc.registerExtension({
1319 return 'super_trans';
1326 return 'element_sub_' + this.__super__.test();
1331 return 'element_trans_sub_' + this.__super__.testT();
1338 return 'text_sub_' + this.__super__.test();
1343 return 'text_trans_sub_' + this.__super__.testT();
1349 var textNode = doc.root.contents()[0];
1351 expect(doc.root.test()).to.equal('element_sub_super');
1352 expect(textNode.test()).to.equal('text_sub_super');
1353 expect(doc.root.testT()).to.equal('element_trans_sub_super_trans');
1354 expect(textNode.testT()).to.equal('text_trans_sub_super_trans');
1358 describe('Undo/redo', function() {
1360 it('smoke tests', function() {
1361 var doc = getDocumentFromXML('<div>Alice</div>'),
1362 textNode = doc.root.contents()[0];
1364 expect(doc.undoStack).to.have.length(0);
1366 textNode.wrapWith({tagName: 'span', start:1, end:2});
1367 expect(doc.undoStack).to.have.length(1, '1');
1368 expect(doc.toXML()).to.equal('<div>A<span>l</span>ice</div>');
1371 expect(doc.undoStack).to.have.length(0, '2');
1372 expect(doc.toXML()).to.equal('<div>Alice</div>');
1375 expect(doc.undoStack).to.have.length(1, '3');
1376 expect(doc.toXML()).to.equal('<div>A<span>l</span>ice</div>');
1379 expect(doc.undoStack).to.have.length(0, '4');
1380 expect(doc.toXML()).to.equal('<div>Alice</div>');
1383 expect(doc.undoStack).to.have.length(0, '5');
1384 expect(doc.toXML()).to.equal('<div>Alice</div>');
1387 it('smoke tests 2', function() {
1388 var doc = getDocumentFromXML('<div>Alice</div>'),
1389 textNode = doc.root.contents()[0],
1390 path = textNode.getPath();
1392 textNode.setText('Alice ');
1393 textNode.setText('Alice h');
1394 textNode.setText('Alice ha');
1395 textNode.setText('Alice has');
1397 expect(textNode.getText()).to.equal('Alice has');
1400 expect(doc.root.contents()[0].getText()).to.equal('Alice ha', '1');
1403 expect(doc.root.contents()[0].getText()).to.equal('Alice h', '2');
1406 expect(doc.root.contents()[0].getText()).to.equal('Alice ha', '3');
1409 expect(doc.root.contents()[0].getText()).to.equal('Alice has', '4');
1413 textNode = doc.getNodeByPath(path);
1414 textNode.setText('Cat');
1416 textNode = doc.getNodeByPath(path);
1417 expect(textNode.getText()).to.equal('Alice h');
1421 var sampleMethod = function(val) {
1422 this._$.attr('x', val);
1423 this.triggerChangeEvent();
1426 var transformations = {
1427 'unaware': sampleMethod,
1428 'returning change root': {
1430 getChangeRoot: function() {
1431 return this.context;
1434 'implementing undo operation': {
1435 impl: function(t, val) {
1436 t.oldVal = this.getAttr('x');
1437 sampleMethod.call(this, val);
1440 this.setAttr('x', t.oldVal);
1445 _.pairs(transformations).forEach(function(pair) {
1447 transformaton = pair[1];
1449 describe(name + ' transformation: ', function() {
1450 var doc, node, nodePath;
1452 beforeEach(function() {
1453 doc = getDocumentFromXML('<div><test x="old"></test></div>');
1455 doc.registerExtension({elementNode: {transformations: {
1459 node = doc.root.contents()[0];
1460 nodePath = node.getPath();
1463 it('transforms as expected', function() {
1465 expect(node.getAttr('x')).to.equal('new');
1468 it('can be undone', function() {
1471 node = doc.getNodeByPath(nodePath);
1472 expect(node.getAttr('x')).to.equal('old');
1475 it('can be undone and then redone', function() {
1479 node = doc.getNodeByPath(nodePath);
1480 expect(node.getAttr('x')).to.equal('new');
1483 it('handles a sample scenario', function() {
1484 doc.root.contents()[0].test('1');
1485 doc.root.contents()[0].test('2');
1486 doc.root.contents()[0].test('3');
1487 doc.root.contents()[0].test('4');
1488 doc.root.contents()[0].test('5');
1490 expect(doc.root.contents()[0].getAttr('x')).to.equal('5', 'after initial transformations');
1492 expect(doc.root.contents()[0].getAttr('x')).to.equal('4', 'undo 1.1');
1494 expect(doc.root.contents()[0].getAttr('x')).to.equal('3', 'undo 1.2');
1496 expect(doc.root.contents()[0].getAttr('x')).to.equal('4', 'redo 1.1');
1498 expect(doc.root.contents()[0].getAttr('x')).to.equal('5', 'redo 1.2');
1500 expect(doc.root.contents()[0].getAttr('x')).to.equal('4', 'undo 2.1');
1501 doc.root.contents()[0].test('10');
1502 expect(doc.root.contents()[0].getAttr('x')).to.equal('10', 'additional transformation');
1503 expect(doc.redoStack.length).to.equal(0, 'transformation cleared redo stack');
1505 expect(doc.root.contents()[0].getAttr('x')).to.equal('10', 'empty redoStack so redo was noop');
1507 expect(doc.root.contents()[0].getAttr('x')).to.equal('4', 'undoing additional transformation');
1509 expect(doc.root.contents()[0].getAttr('x')).to.equal('10', 'redoing additional transformation');
1514 it('smoke tests nested transformations', function() {
1515 var doc = getDocumentFromXML('<div></div>');
1517 doc.registerExtension({elementNode: {transformations: {
1518 nested: function(v) {
1519 this._$.attr('innerAttr', v);
1520 this.triggerChangeEvent();
1522 outer: function(v) {
1524 this._$.attr('outerAttr', v);
1525 this.triggerChangeEvent();
1529 doc.root.outer('test1');
1530 doc.root.outer('test2');
1532 expect(doc.root.getAttr('innerAttr')).to.equal('test2');
1533 expect(doc.root.getAttr('outerAttr')).to.equal('test2');
1537 expect(doc.root.getAttr('innerAttr')).to.equal('test1');
1538 expect(doc.root.getAttr('outerAttr')).to.equal('test1');
1542 expect(doc.root.getAttr('innerAttr')).to.equal(undefined);
1543 expect(doc.root.getAttr('outerAttr')).to.equal(undefined);
1547 expect(doc.root.getAttr('innerAttr')).to.equal('test1');
1548 expect(doc.root.getAttr('outerAttr')).to.equal('test1');
1552 expect(doc.root.getAttr('innerAttr')).to.equal('test2');
1553 expect(doc.root.getAttr('outerAttr')).to.equal('test2');
1557 it('ignores transformation if document didn\'t emit change event', function() {
1558 var doc = getDocumentFromXML('<div></div>');
1560 doc.registerExtension({elementNode: {transformations: {
1567 expect(doc.undoStack.length).to.equal(0);
1571 describe('Transactions', function() {
1572 it('allows to undo/redo series of transformations at once', function() {
1573 var doc = getDocumentFromXML('<div></div>');
1575 doc.registerExtension({
1576 elementNode: {transformations: {
1578 this.setAttr('test', v);
1583 doc.startTransaction();
1587 doc.endTransaction();
1590 expect(doc.root.getAttr('test'), '1');
1592 expect(doc.root.getAttr('test'), '3');
1594 expect(doc.root.getAttr('test'), '1');
1596 expect(doc.root.getAttr('test'), '3');
1599 it('ignores empty transactions', function() {
1600 var doc = getDocumentFromXML('<div></div>');
1601 doc.startTransaction();
1602 doc.endTransaction();
1603 expect(doc.undoStack).to.have.length(0, 'empty transaction doesn\'t get pushed into undo stack');
1606 it('doesn\'t break on optimizations', function() {
1607 // This is a smoke test checking if optimizations made to transaction undoing
1608 // doesnt't break anything.
1609 var doc = getDocumentFromXML('<div smart="1" unaware="1"></div>');
1611 doc.registerExtension({
1612 elementNode: {transformations: {
1613 unaware: function(v) {
1614 this.setAttr('unware', v);
1615 this.triggerChangeEvent();
1618 impl: function(t, v) {
1619 t.oldVal = this.getAttr('smart');
1620 this.setAttr('smart', v);
1621 this.triggerChangeEvent();
1624 this.setAttr('smart', t.oldVal);
1625 this.triggerChangeEvent();
1631 doc.startTransaction();
1632 doc.root.smart('2');
1633 doc.root.unaware('2');
1634 doc.root.smart('3');
1635 doc.root.unaware('3');
1636 doc.endTransaction();
1640 expect(doc.root.getAttr('smart')).to.equal('1');
1641 expect(doc.root.getAttr('unaware')).to.equal('1');
1644 it('can have associated metadata', function() {
1645 var doc = getDocumentFromXML('<div></div>'),
1646 metadata = Object.create({});
1648 doc.registerExtension({document: {transformations: {
1650 this.trigger('change');
1654 doc.startTransaction(metadata);
1656 doc.endTransaction();
1658 var transaction = doc.undoStack[0];
1659 expect(transaction.metadata).to.equal(metadata);
1662 it('can be rolled back', function() {
1663 var doc = getDocumentFromXML('<root></root>');
1665 doc.startTransaction();
1666 doc.root.append({tagName: 'div'});
1667 doc.rollbackTransaction();
1669 expect(doc.undoStack.length).to.equal(0, 'nothing to undo');
1670 expect(doc.root.contents().length).to.equal(0);
1673 it('rollbacks and calls error handleor if error gets thrown', function() {
1674 var doc = getDocumentFromXML('<root></root>'),
1678 doc.transaction(function() {
1679 doc.root.append({tagName: 'div'});
1683 expect(spy.args[0][0]).to.equal(err);
1684 expect(doc.root.contents().length).to.equal(0);
1685 expect(doc.undoStack.length).to.equal(0);
1689 describe('Regression tests', function() {
1690 it('redos correctly after running its own undo followed by unaware transformation undo', function() {
1691 var doc = getDocumentFromXML('<section t="0"></section>');
1693 doc.registerExtension({elementNode: {transformations: {
1694 unaware: function() {
1695 this.triggerChangeEvent();
1699 this._$.attr('t', 1);
1700 this.triggerChangeEvent();
1703 this._$.attr('t', 0);
1713 expect(doc.root.getAttr('t')).to.equal('1');
1715 it('can perform undo of an operation performed after automatic transaction rollback', function() {
1716 var doc = getDocumentFromXML('<section></section>'),
1717 extension = {document: {transformations: {
1718 throwingTransformation: function() { throw new Error(); }
1721 doc.registerExtension(extension);
1723 doc.throwingTransformation();
1725 doc.transaction(function() {
1726 doc.root.setAttr('x', '2');
1729 expect(doc.undoStack.length).to.equal(1);
1730 expect(doc.root.getAttr('x')).to.equal('2');
1734 expect(doc.undoStack.length).to.equal(0);
1735 expect(doc.root.getAttr('x')).to.be.undefined;