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');
73 describe('Retrieving node by path', function() {
74 it('passes smoke tests', function() {
75 var doc = getDocumentFromXML('<root><a><b>c</b></a>');
76 expect(doc.getNodeByPath([0]).sameNode(doc.root.contents()[0])).to.be.true;
77 expect(doc.getNodeByPath([0,0]).sameNode(doc.root.contents()[0].contents()[0])).to.be.true;
79 it('treats empty path as a root path', function() {
80 var doc = getDocumentFromXML('<root></root>');
81 expect(doc.getNodeByPath([]).sameNode(doc.root)).to.be.true;
83 it('returns undefined for non existing paths', function() {
84 var doc = getDocumentFromXML('<root><a></a></root>');
85 expect(doc.getNodeByPath([1])).to.be.undefined;
86 expect(doc.getNodeByPath([0,1])).to.be.undefined;
87 expect(doc.getNodeByPath([10,1])).to.be.undefined;
92 describe('DocumentNode', function() {
93 it('can be cloned', function() {
94 var doc = getDocumentFromXML('<div>Alice</div>'),
95 text = doc.root.contents()[0],
98 [doc.root, text].forEach(function(node) {
99 suffix = ' (' + (node.nodeType === Node.TEXT_NODE ? 'text' : 'element') + ')';
100 clone = node.clone();
101 expect(doc.containsNode(clone)).to.equal(false, 'clone is not contained in a document' + suffix);
102 expect(node.sameNode(clone)).to.equal(false, 'clone is not same node as its originator' + suffix);
103 expect(node.nativeNode.isEqualNode(clone.nativeNode)).to.equal(true, 'clone is identical as its originator' + suffix);
107 it('can be cloned with its contents and its contents data', function() {
108 var doc = getDocumentFromXML('<root><div></div></root>'),
110 div = root.contents()[0];
112 var ClonableObject = function(arg) {
115 ClonableObject.prototype.clone = function() {
116 return new ClonableObject(this.arg);
119 div.setData('key', 'value');
120 div.setData('clonableObject', new ClonableObject('test'));
122 var rootClone = root.clone(),
123 divClone = rootClone.contents()[0],
124 stringClone = divClone.getData('key'),
125 objClone = divClone.getData('clonableObject');
127 expect(stringClone).to.equal('value');
128 expect(objClone.arg).to.equal('test', 'clonable object got copied');
129 expect(objClone !== div.getData('clonableObject')).to.be.equal(true, 'copy of the clonable object is a new object');
132 it('knows its path in the document tree', function() {
133 var doc = getDocumentFromXML('<root><a><b><c></c>text</b></a></root>'),
135 a = root.contents()[0],
137 text = b.contents()[1];
139 expect(root.getPath()).to.eql([], 'path of the root element is empty');
140 expect(a.getPath()).to.eql([0]);
141 expect(b.getPath()).to.eql([0, 0]);
142 expect(text.getPath()).to.eql([0,0,1]);
144 /* Paths relative to a given ancestor */
145 expect(text.getPath(root)).to.eql([0,0,1]);
146 expect(text.getPath(a)).to.eql([0,1]);
147 expect(text.getPath(b)).to.eql([1]);
151 describe('Basic ElementNode properties', function() {
152 it('exposes node contents', function() {
153 var node = elementNodeFromXML('<node>Some<node>text</node>is here</node>'),
154 contents = node.contents();
156 expect(contents).to.have.length(3);
157 expect(contents[0].nodeType).to.equal(Node.TEXT_NODE, 'text node 1');
158 expect(contents[1].nodeType).to.equal(Node.ELEMENT_NODE, 'element node 1');
159 expect(contents[2].nodeType).to.equal(Node.TEXT_NODE, 'text node 2');
162 describe('Storing custom data', function() {
165 beforeEach(function() {
166 node = elementNodeFromXML('<div></div>');
169 it('can append single value', function() {
170 node.setData('key', 'value');
171 expect(node.getData('key')).to.equal('value');
174 it('can overwrite the whole data', function() {
175 node.setData('key1', 'value1');
176 node.setData({key2: 'value2'});
177 expect(node.getData('key2')).to.equal('value2');
180 it('can fetch the whole data at once', function() {
181 node.setData({key1: 'value1', key2: 'value2'});
182 expect(node.getData()).to.eql({key1: 'value1', key2: 'value2'});
185 it('can remove specific data', function() {
186 node.setData('key', 'value');
187 node.setData('key', undefined);
188 expect(node.getData('key')).to.be.undefined;
192 describe('Changing node tag', function() {
194 it('can change tag name', function() {
195 var node = elementNodeFromXML('<div></div>');
196 node = node.setTag('span');
197 expect(node.getTagName()).to.equal('span');
200 describe('Implementation specific expectations', function() {
201 it('keeps custom data', function() {
202 var node = elementNodeFromXML('<div></div>');
204 node.setData('key', 'value');
205 node = node.setTag('header');
207 expect(node.getTagName()).to.equal('header');
208 expect(node.getData()).to.eql({key: 'value'});
211 it('can change document root tag name', function() {
212 var doc = getDocumentFromXML('<div></div>');
213 doc.root.setTag('span');
214 expect(doc.root.getTagName()).to.equal('span');
217 it('keeps node contents', function() {
218 var node = elementNodeFromXML('<div><div></div></div>');
219 node = node.setTag('header');
220 expect(node.contents()).to.have.length(1);
225 describe('Setting node attributes', function() {
226 it('can set node attribute', function() {
227 var node = elementNodeFromXML('<div></div>');
229 node.setAttr('key', 'value');
230 expect(node.getAttr('key')).to.equal('value');
232 it('emits nodeAttrChange event', function() {
233 var node = elementNodeFromXML('<div key="value1"></div>'),
236 node.document.on('change', spy);
237 node.setAttr('key', 'value2');
238 var event = spy.args[0][0];
240 expect(event.type).to.equal('nodeAttrChange');
241 expect(event.meta.node.sameNode(node)).to.be.true;
242 expect(event.meta.attr).to.equal('key');
243 expect(event.meta.oldVal).to.equal('value1');
247 describe('Searching for the last child text node', function() {
249 '<div>xxx<div></div>last</div>',
250 '<div><div>last</div></div>',
251 '<div>xxx<div>last</div><div></div></div>'
252 ].forEach(function(xml, i) {
253 var example = 'example ' + i;
254 it('returns last child text node ' + example + ')', function() {
255 var doc = getDocumentFromXML(xml),
256 lastTextNode = doc.root.getLastTextNode();
257 expect(lastTextNode.getText()).to.equal('last', example);
262 describe('Putting nodes around', function() {
263 it('will not allow to put node before or after root node', function() {
264 var doc = getDocumentFromXML('<root></root>'),
269 doc.on('change', spy);
271 result = doc.root.before({tagName: 'test'});
273 expect(spy.callCount).to.equal(0);
274 expect(result).to.undefined;
276 result = doc.root.after({tagName: 'test'});
278 expect(spy.callCount).to.equal(0);
279 expect(result).to.undefined;
281 expect(doc.root.sameNode(root));
286 describe('Basic TextNode properties', function() {
287 it('can have its text set', function() {
288 var node = elementNodeFromXML('<div>Alice</div>'),
289 textNode = node.contents()[0];
291 textNode.setText('Cat');
292 expect(textNode.getText()).to.equal('Cat');
295 it('emits nodeTextChange', function() {
296 var node = elementNodeFromXML('<div>Alice</div>'),
297 textNode = node.contents()[0],
300 textNode.document.on('change', spy);
301 textNode.setText('Cat');
303 var event = spy.args[0][0];
304 expect(event.type).to.equal('nodeTextChange');
307 it('puts NodeElement after itself', function() {
308 var node = elementNodeFromXML('<div>Alice</div>'),
309 textNode = node.contents()[0],
310 returned = textNode.after({tagName:'div'});
311 expect(returned.sameNode(node.contents()[1])).to.be.true;
314 it('puts NodeElement before itself', function() {
315 var node = elementNodeFromXML('<div>Alice</div>'),
316 textNode = node.contents()[0],
317 returned = textNode.before({tagName:'div'});
318 expect(returned.sameNode(node.contents()[0])).to.be.true;
321 describe('Wrapping TextNode contents', function() {
323 it('wraps DocumentTextElement', function() {
324 var node = elementNodeFromXML('<section>Alice</section>'),
325 textNode = node.contents()[0];
327 var returned = textNode.wrapWith({tagName: 'header'}),
328 parent = textNode.parent(),
329 parent2 = node.contents()[0];
331 expect(returned.sameNode(parent)).to.be.equal(true, 'wrapper is a parent');
332 expect(returned.sameNode(parent2)).to.be.equal(true, 'wrapper has a correct parent');
333 expect(returned.getTagName()).to.equal('header');
336 describe('wrapping part of DocumentTextElement', function() {
337 [{start: 5, end: 12}, {start: 12, end: 5}].forEach(function(offsets) {
338 it('wraps in the middle ' + offsets.start + '/' + offsets.end, function() {
339 var node = elementNodeFromXML('<section>Alice has a cat</section>'),
340 textNode = node.contents()[0];
342 var returned = textNode.wrapWith({tagName: 'header', attrs: {'attr1': 'value1'}, start: offsets.start, end: offsets.end}),
343 contents = node.contents();
345 expect(contents.length).to.equal(3);
347 expect(contents[0].nodeType).to.be.equal(Node.TEXT_NODE, 'first node is text node');
348 expect(contents[0].getText()).to.equal('Alice');
350 expect(contents[1].sameNode(returned)).to.be.true;
351 expect(returned.getTagName()).to.equal('header');
352 expect(returned.getAttr('attr1')).to.equal('value1');
353 expect(contents[1].contents().length).to.equal(1, 'wrapper has one node inside');
354 expect(contents[1].contents()[0].getText()).to.equal(' has a ');
356 expect(contents[2].nodeType).to.be.equal(Node.TEXT_NODE, 'third node is text node');
357 expect(contents[2].getText()).to.equal('cat');
361 it('wraps whole text inside DocumentTextElement if offsets span entire content', function() {
362 var node = elementNodeFromXML('<section>Alice has a cat</section>'),
363 textNode = node.contents()[0];
365 textNode.wrapWith({tagName: 'header', start: 0, end: 15});
367 var contents = node.contents();
368 expect(contents.length).to.equal(1);
369 expect(contents[0].getTagName()).to.equal('header');
370 expect(contents[0].contents()[0].getText()).to.equal('Alice has a cat');
375 describe('Dividing text node into two with element node', function() {
376 it('can divide text node with element node, splitting text node into two', function() {
377 var doc = getDocumentFromXML('<div>Alice has a cat</div>'),
378 text = doc.root.contents()[0];
380 var returned = text.divideWithElementNode({tagName: 'aside'}, {offset: 5}),
381 contents = doc.root.contents(),
382 lhsText = contents[0],
383 rhsText = contents[2];
385 expect(lhsText.getText()).to.equal('Alice');
386 expect(returned.sameNode(contents[1]));
387 expect(rhsText.getText()).to.equal(' has a cat');
390 it('treats dividing at the very end as appending after it', function() {
391 var doc = getDocumentFromXML('<div>Alice has a cat</div>'),
392 text = doc.root.contents()[0];
395 var returned = text.divideWithElementNode({tagName: 'aside'}, {offset: 15}),
396 contents = doc.root.contents(),
397 textNode = contents[0],
398 elementNode = contents[1];
400 expect(contents.length).to.equal(2);
401 expect(textNode.getText()).to.equal('Alice has a cat');
402 expect(returned.sameNode(elementNode)).to.be.true;
403 expect(elementNode.getTagName()).to.equal('aside');
406 it('treats dividing at the very beginning as prepending before it', function() {
407 var doc = getDocumentFromXML('<div>Alice has a cat</div>'),
408 text = doc.root.contents()[0];
410 var returned = text.divideWithElementNode({tagName: 'aside'}, {offset: 0}),
411 contents = doc.root.contents(),
412 textNode = contents[1],
413 elementNode = contents[0];
415 expect(contents.length).to.equal(2);
416 expect(textNode.getText()).to.equal('Alice has a cat');
417 expect(returned.sameNode(elementNode)).to.be.true;
418 expect(elementNode.getTagName()).to.equal('aside');
423 describe('Manipulations', function() {
425 describe('detaching nodes', function() {
426 it('can detach document root node', function() {
427 var doc = getDocumentFromXML('<div></div>');
430 expect(doc.root).to.equal(null);
434 describe('replacing node with another one', function() {
435 it('replaces node with another one', function() {
436 var doc = getDocumentFromXML('<div><a></a></div>'),
437 a = doc.root.contents()[0];
439 var c = a.replaceWith({tagName: 'b', attrs: {b:'1'}});
441 expect(doc.root.contents()[0].sameNode(c));
442 expect(c.getTagName()).to.equal('b');
443 expect(c.getAttr('b')).to.equal('1');
445 it('can replace document root', function() {
446 var doc = getDocumentFromXML('<div></div>');
448 var header = doc.root.replaceWith({tagName: 'header'});
450 expect(doc.root.sameNode(header)).to.be.true;
451 expect(doc.containsNode(header)).to.be.true;
455 it('merges adjacent text nodes resulting from detaching an element node in between', function() {
456 var doc = getDocumentFromXML('<div>Alice <span>has</span>a cat</div>'),
457 span = doc.root.contents()[1];
461 var rootContents = doc.root.contents();
462 expect(rootContents).to.have.length(1, 'one child left');
463 expect(rootContents[0].getText()).to.equal('Alice a cat');
466 it('merges adjacent text nodes resulting from moving an element node in between', function() {
467 var doc = getDocumentFromXML('<div><a></a>Alice <span>has</span>a cat</div>'),
468 span = doc.root.contents()[2],
469 a = doc.root.contents()[0];
473 var rootContents = doc.root.contents();
474 expect(rootContents).to.have.length(2, 'one child left');
475 expect(rootContents[1].getText()).to.equal('Alice a cat');
478 it('inserts node at index', function() {
479 var doc = getDocumentFromXML('<div><a></a><b></b><c></c></div>'),
480 b = doc.root.contents()[1];
482 var inserted = doc.root.insertAtIndex({tagName: 'test'}, 1);
484 expect(doc.root.contents()[1].sameNode(inserted)).to.equal(true, 'inserted node returned');
485 expect(b.getIndex()).to.equal(2, 'b node shifted right');
488 it('appends node when inserting node at index out of range', function() {
489 var doc = getDocumentFromXML('<div></div>');
491 var test1 = doc.root.insertAtIndex({tagName: 'test1'}, 0),
492 test2 = doc.root.insertAtIndex({tagName: 'test1'}, 10);
494 expect(doc.root.contents()[0].sameNode(test1)).to.equal(true, 'inserting at index 0 of empty nodes appends node');
495 expect(doc.root.contents().length).to.equal(1, 'inserting at index out of range does nothing');
496 expect(test2).to.equal(undefined, 'inserting at index out of range returns undefined');
499 it('appends element node to another element node', function() {
500 var node1 = elementNodeFromParams({tag: 'div'}),
501 node2 = elementNodeFromParams({tag: 'a'}),
502 node3 = elementNodeFromParams({tag: 'p'});
505 expect(node1.contents()[0].sameNode(node2)).to.be.true;
506 expect(node1.contents()[1].sameNode(node3)).to.be.true;
509 it('prepends element node to another element node', function() {
510 var node1 = elementNodeFromParams({tag: 'div'}),
511 node2 = elementNodeFromParams({tag: 'a'}),
512 node3 = elementNodeFromParams({tag: 'p'});
513 node1.prepend(node2);
514 node1.prepend(node3);
515 expect(node1.contents()[0].sameNode(node3)).to.be.true;
516 expect(node1.contents()[1].sameNode(node2)).to.be.true;
519 describe('adding text nodes', function() {
520 it('merges text nodes on append', function() {
521 var doc = getDocumentFromXML('<root>text1</root>'),
523 returned = doc.root.append({text: 'text2'});
524 expect(doc.root.contents().length).to.equal(1);
525 expect(returned.sameNode(doc.root.contents()[0])).to.equal(true, 'modified node returned');
526 expect(doc.root.contents()[0].getText()).to.equal('text1text2');
529 it('merges text nodes on prepend', function() {
530 var doc = getDocumentFromXML('<root>text1</root>'),
532 returned = doc.root.prepend({text: 'text2'});
533 expect(doc.root.contents().length).to.equal(1);
534 expect(returned.sameNode(doc.root.contents()[0])).to.equal(true, 'modified node returned');
535 expect(doc.root.contents()[0].getText()).to.equal('text2text1');
538 it('merges text nodes on before text node', function() {
539 var doc = getDocumentFromXML('<root>text1</root>'),
540 textNode = doc.root.contents()[0],
542 returned = textNode.before({text: 'text2'});
543 expect(doc.root.contents().length).to.equal(1);
544 expect(returned.sameNode(doc.root.contents()[0])).to.equal(true, 'modified node returned');
545 expect(doc.root.contents()[0].getText()).to.equal('text2text1');
548 it('merges text nodes on after text node', function() {
549 var doc = getDocumentFromXML('<root>text1</root>'),
550 textNode = doc.root.contents()[0],
552 returned = textNode.after({text: 'text2'});
553 expect(doc.root.contents().length).to.equal(1);
554 expect(returned.sameNode(doc.root.contents()[0])).to.equal(true, 'modified node returned');
555 expect(doc.root.contents()[0].getText()).to.equal('text1text2');
558 it('merges text nodes on before element node', function() {
559 var doc = getDocumentFromXML('<root>text1<div></div></root>'),
560 textNode = doc.root.contents()[0],
561 div = doc.root.contents()[1],
563 returned = div.before({text: 'text2'});
564 expect(doc.root.contents().length).to.equal(2);
565 expect(returned.sameNode(doc.root.contents()[0])).to.equal(true, 'modified node returned');
566 expect(textNode.getText()).to.equal('text1text2');
569 it('merges text nodes on after element node', function() {
570 var doc = getDocumentFromXML('<root><div></div>text1</root>'),
571 textNode = doc.root.contents()[1],
572 div = doc.root.contents()[0],
574 returned = div.after({text: 'text2'});
575 expect(doc.root.contents().length).to.equal(2);
576 expect(returned.sameNode(doc.root.contents()[1])).to.equal(true, 'modified node returned');
577 expect(textNode.getText()).to.equal('text2text1');
581 it('wraps root element node with another element node', function() {
582 var node = elementNodeFromXML('<div></div>'),
583 wrapper = elementNodeFromXML('<wrapper></wrapper>');
585 node.wrapWith(wrapper);
586 expect(node.parent().sameNode(wrapper)).to.be.true;
587 expect(node.document.root.sameNode(wrapper)).to.be.true;
590 it('wraps element node with another element node', function() {
591 var doc = getDocumentFromXML('<section><div></div></section>'),
592 div = doc.root.contents()[0];
594 var wrapper = div.wrapWith({tagName: 'wrapper'});
595 expect(wrapper.sameNode(doc.root.contents()[0])).to.equal(true, '1');
596 expect(div.parent().sameNode(wrapper)).to.equal(true, '2');
597 expect(wrapper.contents()[0].sameNode(div)).to.equal(true, '3');
600 it('wraps element outside of document tree', function() {
601 var doc = getDocumentFromXML('<section><div></div></section>'),
602 node = doc.createDocumentNode({tagName: 'node'});
604 node.wrapWith({tagName: 'wrapper'});
605 expect(node.parent().getTagName()).to.equal('wrapper');
606 expect(node.parent().contents()[0].sameNode(node)).to.be.true;
607 expect(doc.root.getTagName()).to.equal('section');
610 it('unwraps element node contents', function() {
611 var node = elementNodeFromXML('<div>Alice <div>has <span>propably</span> a cat</div>!</div>'),
612 outerDiv = node.contents()[1];
614 outerDiv.unwrapContent();
616 expect(node.contents().length).to.equal(3);
617 expect(node.contents()[0].getText()).to.equal('Alice has ');
618 expect(node.contents()[1].getTagName()).to.equal('span');
619 expect(node.contents()[2].getText()).to.equal(' a cat!');
622 it('removes parent-describing sibling nodes of unwrapped node', function() {
623 var doc = getDocumentFromXML('<root><div><a></a><x></x><a></a></div></root>'),
624 div = doc.root.contents()[0],
625 x = div.contents()[1];
627 doc.registerExtension({documentNode: {methods: {
629 describesParent: function() {
630 return this.getTagName() === 'x';
636 expect(doc.root.contents().length).to.equal(2);
637 expect(x.isInDocument()).to.be.false;
640 it('unwrap single element node from its parent', function() {
641 var doc = getDocumentFromXML('<div><a><b></b></a></div>'),
643 a = div.contents()[0],
646 var parent = b.unwrap();
648 expect(parent.sameNode(div)).to.equal(true, 'returns new parent');
649 expect(div.contents()).to.have.length(1, 'root contains only one node');
650 expect(div.contents()[0].sameNode(b)).to.equal(true, 'node got unwrapped');
653 it('unwrap single text node from its parent', function() {
654 var doc = getDocumentFromXML('<div>Some <span>text</span>!</div>'),
656 span = div.contents()[1],
657 text = span.contents()[0];
659 var parent = text.unwrap();
661 expect(parent.sameNode(div)).to.equal(true, 'returns new parent');
662 expect(div.contents()).to.have.length(1, 'root contains only one node');
663 expect(div.contents()[0].getText()).to.equal('Some text!');
666 describe('Wrapping text', function() {
667 it('wraps text spanning multiple sibling TextNodes', function() {
668 var section = elementNodeFromXML('<section>Alice has a <span>small</span> cat</section>'),
669 wrapper = section.wrapText({
670 _with: {tagName: 'span', attrs: {'attr1': 'value1'}},
676 expect(section.contents().length).to.equal(2);
677 expect(section.contents()[0].nodeType).to.equal(Node.TEXT_NODE);
678 expect(section.contents()[0].getText()).to.equal('Alice ');
680 var wrapper2 = section.contents()[1];
681 expect(wrapper2.sameNode(wrapper)).to.be.true;
682 expect(wrapper.getTagName()).to.equal('span');
684 var wrapperContents = wrapper.contents();
685 expect(wrapperContents.length).to.equal(3);
686 expect(wrapperContents[0].getText()).to.equal('has a ');
688 expect(wrapperContents[1].nodeType).to.equal(Node.ELEMENT_NODE);
689 expect(wrapperContents[1].contents().length).to.equal(1);
690 expect(wrapperContents[1].contents()[0].getText()).to.equal('small');
693 it('keeps parent-describing nodes in place', function() {
694 var doc = getDocumentFromXML('<root>Alice <x></x> probably <y></y> has a cat</root>'),
696 x = root.contents()[1],
697 y = root.contents()[3];
699 doc.registerExtension({documentNode: {methods: {
701 describesParent: function() {
703 return this.nodeType === Node.ELEMENT_NODE && this.getTagName() === 'x';
709 _with: {tagName: 'span', attrs: {'attr1': 'value1'}},
715 expect(x.parent().sameNode(root)).to.be.true;
716 expect(y.parent().getTagName()).to.equal('span');
720 describe('Wrapping Nodes', function() {
721 it('wraps multiple sibling nodes', function() {
722 var section = elementNodeFromXML('<section>Alice<div>has</div><div>a cat</div></section>'),
723 aliceText = section.contents()[0],
724 firstDiv = section.contents()[1],
725 lastDiv = section.contents()[section.contents().length -1];
727 var returned = section.document.wrapNodes({
730 _with: {tagName: 'header'}
733 var sectionContentss = section.contents(),
734 header = sectionContentss[0],
735 headerContents = header.contents();
737 expect(sectionContentss).to.have.length(1);
738 expect(header.sameNode(returned)).to.equal(true, 'wrapper returned');
739 expect(header.parent().sameNode(section)).to.be.true;
740 expect(headerContents).to.have.length(3);
741 expect(headerContents[0].sameNode(aliceText)).to.equal(true, 'first node wrapped');
742 expect(headerContents[1].sameNode(firstDiv)).to.equal(true, 'second node wrapped');
743 expect(headerContents[2].sameNode(lastDiv)).to.equal(true, 'third node wrapped');
746 it('wraps multiple sibling Elements - middle case', function() {
747 var section = elementNodeFromXML('<section><div></div><div></div><div></div><div></div></section>'),
748 div2 = section.contents()[1],
749 div3 = section.contents()[2];
751 section.document.wrapNodes({
754 _with: {tagName: 'header'}
757 var sectionContentss = section.contents(),
758 header = sectionContentss[1],
759 headerChildren = header.contents();
761 expect(sectionContentss).to.have.length(3);
762 expect(headerChildren).to.have.length(2);
763 expect(headerChildren[0].sameNode(div2)).to.equal(true, 'first node wrapped');
764 expect(headerChildren[1].sameNode(div3)).to.equal(true, 'second node wrapped');
767 it('keeps parent-describing nodes in place', function() {
768 var section = elementNodeFromXML('<section>Alice<x></x><div>a cat</div></section>'),
769 aliceText = section.contents()[0],
770 x = section.contents()[1],
771 lastDiv = section.contents()[2];
773 section.document.registerExtension({documentNode: {methods: {
775 describesParent: function() {
776 return this.nodeType === Node.ELEMENT_NODE && this.getTagName() === 'x';
781 section.document.wrapNodes({
784 _with: {tagName: 'header'}
787 expect(x.parent().sameNode(section)).to.be.true;
788 expect(aliceText.parent().getTagName()).to.equal('header');
789 expect(lastDiv.parent().getTagName()).to.equal('header');
795 var getTextNodes = function(text, doc) {
798 var search = function(node) {
799 node.contents().forEach(function(node) {
800 if(node.nodeType === Node.TEXT_NODE) {
801 if(node.getText() === text) {
813 var getTextNode = function(text, doc) {
814 var nodes = getTextNodes(text, doc),
816 if(nodes.length === 0) {
817 error = 'Text not found';
818 } else if(nodes.length > 1) {
819 error = 'Text not unique';
820 } else if(nodes[0].getText() !== text) {
821 error = 'I was trying to cheat your test :(';
824 throw new Error(error);
829 describe('Removing arbitrary text', function() {
830 it('removes within single text element', function() {
831 var doc = getDocumentFromXML('<div>Alice</div>'),
832 text = getTextNode('Alice', doc);
843 expect(doc.root.contents().length).to.equal(1);
844 expect(doc.root.contents()[0].getText()).to.equal('Ae');
846 it('removes across elements - 1', function() {
847 var doc = getDocumentFromXML('<div><a>aaa</a><b>bbb</b></div>');
851 node: getTextNode('aaa', doc),
855 node: getTextNode('bbb', doc),
860 var contents = doc.root.contents();
861 expect(contents.length).to.equal(2);
862 expect(contents[0].contents()[0].getText()).to.equal('aa');
863 expect(contents[1].contents()[0].getText()).to.equal('b');
865 it('removes across elements - 2', function() {
866 var doc = getDocumentFromXML('<a><b><c>ccc</c></b>xxx</a>');
869 node: getTextNode('ccc', doc),
873 node: getTextNode('xxx', doc),
878 var contents = doc.root.contents();
879 expect(contents.length).to.equal(2);
880 expect(contents[0].getTagName()).to.equal('b');
881 expect(contents[1].getText()).to.equal('x');
883 var bContents = contents[0].contents();
884 expect(bContents.length).to.equal(1);
885 expect(bContents[0].getTagName()).to.equal('c');
886 expect(bContents[0].contents().length).to.equal(1);
887 expect(bContents[0].contents()[0].getText()).to.equal('cc');
889 it('remove across elements - 3 (merged text nodes)', function() {
890 var doc = getDocumentFromXML('<div>Alice <span>has</span> a cat</div>');
893 node: getTextNode('Alice ', doc),
897 node: getTextNode(' a cat', doc),
901 var contents = doc.root.contents();
902 expect(contents.length).to.equal(1);
903 expect(contents[0].getText()).to.equal('Acat');
905 it('remove across elements - 4', function() {
906 var doc = getDocumentFromXML('<div>Alice <div>has <span>a</span> cat</div></div>');
909 node: getTextNode('Alice ', doc),
913 node: getTextNode(' cat', doc),
917 var contents = doc.root.contents();
918 expect(contents.length).to.equal(2);
919 expect(contents[0].getText()).to.equal('A');
920 expect(contents[1].getTagName()).to.equal('div');
921 expect(contents[1].contents().length).to.equal(1);
922 expect(contents[1].contents()[0].getText()).to.equal('cat');
924 it('removes across elements - 5 (whole document)', function() {
925 var doc = getDocumentFromXML('<div>Alice <div>has <span>a</span> cat</div>!!!</div>');
928 node: getTextNode('Alice ', doc),
932 node: getTextNode('!!!', doc),
937 expect(doc.root.getTagName()).to.equal('div');
938 expect(doc.root.contents().length).to.equal(1);
939 expect(doc.root.contents()[0].getText()).to.equal('');
941 it('removes nodes in between', function() {
942 var doc = getDocumentFromXML('<div><a>aaa<x>!</x></a>xxx<x></x><b><x>!</x>bbb</b></div>');
945 node: getTextNode('aaa', doc),
949 node: getTextNode('bbb', doc),
954 var contents = doc.root.contents();
955 expect(contents.length).to.equal(2, 'two nodes survived');
956 expect(contents[0].getTagName()).to.equal('a');
957 expect(contents[1].getTagName()).to.equal('b');
958 expect(contents[0].contents().length).to.equal(1);
959 expect(contents[0].contents()[0].getText()).to.equal('aa');
960 expect(contents[1].contents().length).to.equal(1);
961 expect(contents[1].contents()[0].getText()).to.equal('b');
963 it('removes across elements - 6', function() {
964 var doc = getDocumentFromXML('<root><div>aaa<span>bbb</span>ccc</div><div>ddd</div></root>');
967 node: getTextNode('aaa', doc),
971 node: getTextNode('ddd', doc),
975 error: function(e) {throw e;}
978 var contents = doc.root.contents();
979 expect(contents.length).to.equal(2);
980 expect(contents[0].contents().length).to.equal(1);
981 expect(contents[0].contents()[0].getText()).to.equal('a');
982 expect(contents[1].contents().length).to.equal(1);
983 expect(contents[1].contents()[0].getText()).to.equal('dd');
987 describe('Splitting text', function() {
989 it('splits TextNode\'s parent into two ElementNodes', function() {
990 var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
992 text = section.contents()[0].contents()[0];
994 var returnedValue = text.split({offset: 5});
995 expect(section.contents().length).to.equal(2, 'section has two children');
997 var header1 = section.contents()[0];
998 var header2 = section.contents()[1];
1000 expect(header1.getTagName()).to.equal('header', 'first section child ok');
1001 expect(header1.contents().length).to.equal(1, 'first header has one child');
1002 expect(header1.contents()[0].getText()).to.equal('Some ', 'first header has correct content');
1003 expect(header2.getTagName()).to.equal('header', 'second section child ok');
1004 expect(header2.contents().length).to.equal(1, 'second header has one child');
1005 expect(header2.contents()[0].getText()).to.equal('header', 'second header has correct content');
1007 expect(returnedValue.first.sameNode(header1)).to.equal(true, 'first node returned');
1008 expect(returnedValue.second.sameNode(header2)).to.equal(true, 'second node returned');
1011 it('leaves empty copy of ElementNode if splitting at the very beginning', function() {
1012 var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
1014 text = section.contents()[0].contents()[0];
1016 text.split({offset: 0});
1018 var header1 = section.contents()[0];
1019 var header2 = section.contents()[1];
1021 expect(header1.contents().length).to.equal(0);
1022 expect(header2.contents()[0].getText()).to.equal('Some header');
1025 it('leaves empty copy of ElementNode if splitting at the very end', function() {
1026 var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
1028 text = section.contents()[0].contents()[0];
1030 text.split({offset: 11});
1032 var header1 = section.contents()[0];
1033 var header2 = section.contents()[1];
1035 expect(header1.contents()[0].getText()).to.equal('Some header');
1036 expect(header2.contents().length).to.equal(0);
1039 it('keeps TextNodes\'s parent\'s children elements intact', function() {
1040 var doc = getDocumentFromXML('<section><header>A <span>fancy</span> and <span>nice</span> header</header></section>'),
1042 header = section.contents()[0],
1043 textAnd = header.contents()[2];
1045 textAnd.split({offset: 2});
1047 var sectionContents = section.contents();
1048 expect(sectionContents.length).to.equal(2, 'Section has two children');
1049 expect(sectionContents[0].getTagName()).to.equal('header', 'First section node is a header');
1050 expect(sectionContents[1].getTagName()).to.equal('header', 'Second section node is a header');
1052 var firstHeaderContents = sectionContents[0].contents();
1053 expect(firstHeaderContents.length).to.equal(3, 'First header has three children');
1054 expect(firstHeaderContents[0].getText()).to.equal('A ', 'First header starts with a text');
1055 expect(firstHeaderContents[1].getTagName()).to.equal('span', 'First header has span in the middle');
1056 expect(firstHeaderContents[2].getText()).to.equal(' a', 'First header ends with text');
1058 var secondHeaderContents = sectionContents[1].contents();
1059 expect(secondHeaderContents.length).to.equal(3, 'Second header has three children');
1060 expect(secondHeaderContents[0].getText()).to.equal('nd ', 'Second header starts with text');
1061 expect(secondHeaderContents[1].getTagName()).to.equal('span', 'Second header has span in the middle');
1062 expect(secondHeaderContents[2].getText()).to.equal(' header', 'Second header ends with text');
1066 describe('Events', function() {
1067 it('emits nodeDetached event on node detach', function() {
1068 var node = elementNodeFromXML('<div><div></div></div>'),
1069 innerNode = node.contents()[0],
1071 node.document.on('change', spy);
1073 var detached = innerNode.detach(),
1074 event = spy.args[0][0];
1076 expect(event.type).to.equal('nodeDetached');
1077 expect(event.meta.node.sameNode(detached, 'detached node in event meta'));
1078 expect(event.meta.parent.sameNode(node), 'original parent node in event meta');
1081 it('emits nodeAdded event when appending new node', function() {
1082 var node = elementNodeFromXML('<div></div>'),
1084 node.document.on('change', spy);
1086 var appended = node.append({tagName:'div'}),
1087 event = spy.args[0][0];
1088 expect(event.type).to.equal('nodeAdded');
1089 expect(event.meta.node.sameNode(appended)).to.be.true;
1092 it('emits nodeDetached/nodeAdded events with `move` flag when appending aready existing node', function() {
1093 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
1094 a = node.contents()[0],
1095 b = node.contents()[1],
1097 node.document.on('change', spy);
1099 var appended = a.append(b),
1100 detachedEvent = spy.args[0][0],
1101 addedEvent = spy.args[1][0];
1103 expect(spy.callCount).to.equal(2);
1104 expect(detachedEvent.type).to.equal('nodeDetached');
1105 expect(detachedEvent.meta.node.sameNode(appended)).to.be.true;
1106 expect(detachedEvent.meta.move).to.equal(true, 'move flag set to true for nodeDetachedEvent');
1107 expect(addedEvent.type).to.equal('nodeAdded');
1108 expect(addedEvent.meta.node.sameNode(appended)).to.be.true;
1109 expect(addedEvent.meta.move).to.equal(true, 'move flag set to true for nodeAddedEvent');
1113 it('emits nodeAdded event when prepending new node', function() {
1114 var node = elementNodeFromXML('<div></div>'),
1116 node.document.on('change', spy);
1118 var prepended = node.prepend({tagName:'div'}),
1119 event = spy.args[0][0];
1120 expect(event.type).to.equal('nodeAdded');
1121 expect(event.meta.node.sameNode(prepended)).to.be.true;
1124 it('emits nodeDetached/nodeAdded events with `move` flag when prepending aready existing node', function() {
1125 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
1126 a = node.contents()[0],
1127 b = node.contents()[1],
1129 node.document.on('change', spy);
1131 var prepended = a.prepend(b),
1132 detachedEvent = spy.args[0][0],
1133 addedEvent = spy.args[1][0];
1135 expect(spy.callCount).to.equal(2);
1136 expect(detachedEvent.type).to.equal('nodeDetached');
1137 expect(detachedEvent.meta.node.sameNode(prepended)).to.be.true;
1138 expect(detachedEvent.meta.move).to.equal(true, 'move flag set to true for nodeDetachedEvent');
1139 expect(addedEvent.type).to.equal('nodeAdded');
1140 expect(addedEvent.meta.node.sameNode(prepended)).to.be.true;
1141 expect(addedEvent.meta.move).to.equal(true, 'move flag set to true for nodeAddedEvent');
1144 it('emits nodeAdded event when inserting node after another', function() {
1145 var node = elementNodeFromXML('<div><a></a></div>').contents()[0],
1147 node.document.on('change', spy);
1149 var inserted = node.after({tagName:'div'}),
1150 event = spy.args[0][0];
1151 expect(event.type).to.equal('nodeAdded');
1152 expect(event.meta.node.sameNode(inserted)).to.be.true;
1155 it('emits nodeDetached/nodeAdded events with `move` flag when inserting aready existing node after another', function() {
1156 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
1157 a = node.contents()[0],
1158 b = node.contents()[1],
1160 node.document.on('change', spy);
1161 var inserted = b.after(a),
1162 detachedEvent = spy.args[0][0],
1163 addedEvent = spy.args[1][0];
1165 expect(spy.callCount).to.equal(2);
1166 expect(detachedEvent.type).to.equal('nodeDetached');
1167 expect(detachedEvent.meta.node.sameNode(inserted)).to.be.true;
1168 expect(detachedEvent.meta.move).to.equal(true, 'move flag set to true for nodeDetachedEvent');
1169 expect(addedEvent.type).to.equal('nodeAdded');
1170 expect(addedEvent.meta.node.sameNode(inserted)).to.be.true;
1171 expect(addedEvent.meta.move).to.equal(true, 'move flag set to true for nodeAddedEvent');
1174 it('emits nodeAdded event when inserting node before another', function() {
1175 var node = elementNodeFromXML('<div><a></a></div>').contents()[0],
1177 node.document.on('change', spy);
1179 var inserted = node.before({tagName:'div'}),
1180 event = spy.args[0][0];
1181 expect(event.type).to.equal('nodeAdded');
1182 expect(event.meta.node.sameNode(inserted)).to.be.true;
1185 it('emits nodeDetached/nodeAdded events with `move` flag when inserting aready existing node before another', function() {
1186 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
1187 a = node.contents()[0],
1188 b = node.contents()[1],
1190 node.document.on('change', spy);
1191 var inserted = a.before(b),
1192 detachedEvent = spy.args[0][0],
1193 addedEvent = spy.args[1][0];
1195 expect(spy.callCount).to.equal(2);
1196 expect(detachedEvent.type).to.equal('nodeDetached');
1197 expect(detachedEvent.meta.node.sameNode(inserted)).to.be.true;
1198 expect(detachedEvent.meta.move).to.equal(true, 'move flag set to true for nodeDetachedEvent');
1199 expect(addedEvent.type).to.equal('nodeAdded');
1200 expect(addedEvent.meta.node.sameNode(inserted)).to.be.true;
1201 expect(addedEvent.meta.move).to.equal(true, 'move flag set to true for nodeAddedEvent');
1204 it('emits nodeDetached and nodeAdded when replacing root node with another', function() {
1205 var doc = getDocumentFromXML('<a></a>'),
1209 doc.on('change', spy);
1211 doc.root.replaceWith({tagName: 'b'});
1213 expect(spy.callCount).to.equal(2);
1215 var event1 = spy.args[0][0],
1216 event2 = spy.args[1][0];
1218 expect(event1.type).to.equal('nodeDetached');
1219 expect(event1.meta.node.sameNode(oldRoot)).to.equal(true, 'root node in nodeDetached event metadata');
1220 expect(event2.type).to.equal('nodeAdded');
1221 expect(event2.meta.node.sameNode(doc.root)).to.equal(true, 'new root node in nodelAdded event meta');
1225 ['append', 'prepend', 'before', 'after'].forEach(function(insertionMethod) {
1226 it('emits nodeDetached for node moved from a document tree to out of document node ' + insertionMethod, function() {
1227 var doc = getDocumentFromXML('<div><a></a></div>'),
1228 a = doc.root.contents()[0],
1231 doc.on('change', spy);
1233 var newNode = doc.createDocumentNode({tagName: 'b'}),
1234 newNodeInner = newNode.append({tagName:'c'});
1236 newNodeInner[insertionMethod](a);
1238 var event = spy.args[0][0];
1239 expect(event.type).to.equal('nodeDetached');
1240 expect(event.meta.node.sameNode(a));
1243 it('doesn\'t emit nodeDetached event for already out of document node moved to out of document node' + insertionMethod, function() {
1244 var doc = getDocumentFromXML('<div><a></a></div>'),
1247 doc.on('change', spy);
1249 var newNode = doc.createDocumentNode({tagName: 'b'});
1250 newNode.append({tagName:'c'});
1252 expect(spy.callCount).to.equal(0);
1259 describe('Traversing', function() {
1260 describe('Basic', function() {
1261 it('can access node parent', function() {
1262 var doc = getDocumentFromXML('<a><b></b></a>'),
1264 b = a.contents()[0];
1266 expect(a.parent()).to.equal(null, 'parent of a root is null');
1267 expect(b.parent().sameNode(a)).to.be.true;
1269 it('can access node parents', function() {
1270 var doc = getDocumentFromXML('<a><b><c></c></b></a>'),
1272 b = a.contents()[0],
1273 c = b.contents()[0];
1275 var parents = c.parents();
1277 expect(parents[0].sameNode(b)).to.be.true;
1278 expect(parents[1].sameNode(a)).to.be.true;
1282 describe('finding sibling parents of two elements', function() {
1283 it('returns elements themself if they have direct common parent', function() {
1284 var doc = getDocumentFromXML('<section><div><div>A</div><div>B</div></div></section>'),
1285 wrappingDiv = doc.root.contents()[0],
1286 divA = wrappingDiv.contents()[0],
1287 divB = wrappingDiv.contents()[1];
1289 var siblingParents = doc.getSiblingParents({node1: divA, node2: divB});
1291 expect(siblingParents.node1.sameNode(divA)).to.equal(true, 'divA');
1292 expect(siblingParents.node2.sameNode(divB)).to.equal(true, 'divB');
1295 it('returns sibling parents - example 1', function() {
1296 var doc = getDocumentFromXML('<section>Alice <span>has a cat</span></section>'),
1297 aliceText = doc.root.contents()[0],
1298 span = doc.root.contents()[1],
1299 spanText = span.contents()[0];
1301 var siblingParents = doc.getSiblingParents({node1: aliceText, node2: spanText});
1303 expect(siblingParents.node1.sameNode(aliceText)).to.equal(true, 'aliceText');
1304 expect(siblingParents.node2.sameNode(span)).to.equal(true, 'span');
1307 it('returns node itself for two same nodes', function() {
1308 var doc = getDocumentFromXML('<section><div></div></section>'),
1309 div = doc.root.contents()[0];
1311 var siblingParents = doc.getSiblingParents({node1: div, node2: div});
1312 expect(!!siblingParents.node1 && !!siblingParents.node2).to.equal(true, 'nodes defined');
1313 expect(siblingParents.node1.sameNode(div)).to.be.equal(true, 'node1');
1314 expect(siblingParents.node2.sameNode(div)).to.be.equal(true, 'node2');
1319 describe('Serializing document to WLXML', function() {
1320 it('keeps document intact when no changes have been made', function() {
1321 var xmlIn = '<section>Alice<div>has</div>a <span class="uri" meta-uri="http://cat.com">cat</span>!</section>',
1322 doc = getDocumentFromXML(xmlIn),
1323 xmlOut = doc.toXML();
1325 var parser = new DOMParser(),
1326 input = parser.parseFromString(xmlIn, 'application/xml').childNodes[0],
1327 output = parser.parseFromString(xmlOut, 'application/xml').childNodes[0];
1329 expect(input.isEqualNode(output)).to.be.true;
1332 it('keeps entities intact', function() {
1333 var xmlIn = '<section>< ></section>',
1334 doc = getDocumentFromXML(xmlIn),
1335 xmlOut = doc.toXML();
1336 expect(xmlOut).to.equal(xmlIn);
1338 it('keeps entities intact when they form html/xml', function() {
1339 var xmlIn = '<section><abc></section>',
1340 doc = getDocumentFromXML(xmlIn),
1341 xmlOut = doc.toXML();
1342 expect(xmlOut).to.equal(xmlIn);
1346 describe('Extension API', function() {
1347 var doc, extension, elementNode, textNode;
1349 beforeEach(function() {
1350 doc = getDocumentFromXML('<section>Alice<div class="test_class"></div></section>');
1351 elementNode = doc.root;
1352 textNode = doc.root.contents()[0];
1355 expect(elementNode.testTransformation).to.be.undefined;
1356 expect(textNode.testTransformation).to.be.undefined;
1357 expect(doc.testTransformation).to.be.undefined;
1359 expect(doc.testMethod).to.be.undefined;
1360 expect(elementNode.testMethod).to.be.undefined;
1361 expect(textNode.testMethod).to.be.undefined;
1362 expect(elementNode.elementTestMethod).to.be.undefined;
1363 expect(textNode.textTestMethod).to.be.undefined;
1366 it('allows adding method to a document', function() {
1367 extension = {document: {methods: {
1368 testMethod: function() { return this; }
1371 doc.registerExtension(extension);
1372 expect(doc.testMethod()).to.equal(doc, 'context is set to a document instance');
1375 it('allows adding transformation to a document', function() {
1376 extension = {document: {transformations: {
1377 testTransformation: function() { return this; },
1378 testTransformation2: {impl: function() { return this;}}
1381 doc.registerExtension(extension);
1382 expect(doc.testTransformation()).to.equal(doc, 'context is set to a document instance');
1383 expect(doc.testTransformation2()).to.equal(doc, 'context is set to a document instance');
1386 it('allows adding method to a DocumentNode instance', function() {
1390 testMethod: function() { return this; }
1395 textTestMethod: function() { return this; }
1400 elementTestMethod: function() { return this; }
1405 doc.registerExtension(extension);
1408 elementNode = doc.root;
1409 textNode = doc.root.contents()[0];
1411 expect(elementNode.testMethod().sameNode(elementNode)).to.equal(true, 'context is set to a node instance');
1412 expect(textNode.testMethod().sameNode(textNode)).to.equal(true, 'context is set to a node instance');
1414 expect(elementNode.elementTestMethod().sameNode(elementNode)).to.be.true;
1415 expect(elementNode.textTestMethod).to.be.undefined;
1417 expect(textNode.textTestMethod().sameNode(textNode)).to.be.true;
1418 expect(textNode.elementTestMethod).to.be.undefined;
1421 it('allows adding transformation to a DocumentNode', function() {
1425 testTransformation: function() { return this; },
1426 testTransformation2: {impl: function() { return this;}}
1431 textTestTransformation: function() { return this; }
1436 elementTestTransformation: function() { return this; }
1441 doc.registerExtension(extension);
1444 elementNode = doc.root;
1445 textNode = doc.root.contents()[0];
1447 expect(elementNode.testTransformation().sameNode(elementNode)).to.equal(true, '1');
1448 expect(elementNode.testTransformation2().sameNode(elementNode)).to.equal(true, '2');
1449 expect(textNode.testTransformation().sameNode(textNode)).to.equal(true, '3');
1450 expect(textNode.testTransformation2().sameNode(textNode)).to.equal(true, '4');
1452 expect(elementNode.elementTestTransformation().sameNode(elementNode)).to.be.true;
1453 expect(elementNode.textTestTransformation).to.be.undefined;
1455 expect(textNode.textTestTransformation().sameNode(textNode)).to.be.true;
1456 expect(textNode.elementTestTransfomation).to.be.undefined;
1459 it('allows text/element node methods and transformations to access node and transormations on document node', function() {
1461 var doc = getDocumentFromXML('<div>text</div>');
1463 doc.registerExtension({
1472 return 'super_trans';
1479 return 'element_sub_' + this.__super__.test();
1484 return 'element_trans_sub_' + this.__super__.testT();
1491 return 'text_sub_' + this.__super__.test();
1496 return 'text_trans_sub_' + this.__super__.testT();
1502 var textNode = doc.root.contents()[0];
1504 expect(doc.root.test()).to.equal('element_sub_super');
1505 expect(textNode.test()).to.equal('text_sub_super');
1506 expect(doc.root.testT()).to.equal('element_trans_sub_super_trans');
1507 expect(textNode.testT()).to.equal('text_trans_sub_super_trans');
1511 describe('Undo/redo', function() {
1513 it('smoke tests', function() {
1514 var doc = getDocumentFromXML('<div>Alice</div>'),
1515 textNode = doc.root.contents()[0];
1517 expect(doc.undoStack).to.have.length(0);
1519 textNode.wrapWith({tagName: 'span', start:1, end:2});
1520 expect(doc.undoStack).to.have.length(1, '1');
1521 expect(doc.toXML()).to.equal('<div>A<span>l</span>ice</div>');
1524 expect(doc.undoStack).to.have.length(0, '2');
1525 expect(doc.toXML()).to.equal('<div>Alice</div>');
1528 expect(doc.undoStack).to.have.length(1, '3');
1529 expect(doc.toXML()).to.equal('<div>A<span>l</span>ice</div>');
1532 expect(doc.undoStack).to.have.length(0, '4');
1533 expect(doc.toXML()).to.equal('<div>Alice</div>');
1536 expect(doc.undoStack).to.have.length(0, '5');
1537 expect(doc.toXML()).to.equal('<div>Alice</div>');
1540 it('smoke tests 2', function() {
1541 var doc = getDocumentFromXML('<div>Alice</div>'),
1542 textNode = doc.root.contents()[0],
1543 path = textNode.getPath();
1545 textNode.setText('Alice ');
1546 textNode.setText('Alice h');
1547 textNode.setText('Alice ha');
1548 textNode.setText('Alice has');
1550 expect(textNode.getText()).to.equal('Alice has');
1553 expect(doc.root.contents()[0].getText()).to.equal('Alice ha', '1');
1556 expect(doc.root.contents()[0].getText()).to.equal('Alice h', '2');
1559 expect(doc.root.contents()[0].getText()).to.equal('Alice ha', '3');
1562 expect(doc.root.contents()[0].getText()).to.equal('Alice has', '4');
1566 textNode = doc.getNodeByPath(path);
1567 textNode.setText('Cat');
1569 textNode = doc.getNodeByPath(path);
1570 expect(textNode.getText()).to.equal('Alice h');
1574 var sampleMethod = function(val) {
1575 this._$.attr('x', val);
1576 this.triggerChangeEvent();
1579 var transformations = {
1580 'unaware': sampleMethod,
1581 'returning change root': {
1583 getChangeRoot: function() {
1584 return this.context;
1587 'implementing undo operation': {
1588 impl: function(t, val) {
1589 t.oldVal = this.getAttr('x');
1590 sampleMethod.call(this, val);
1593 this.setAttr('x', t.oldVal);
1598 _.pairs(transformations).forEach(function(pair) {
1600 transformaton = pair[1];
1602 describe(name + ' transformation: ', function() {
1603 var doc, node, nodePath;
1605 beforeEach(function() {
1606 doc = getDocumentFromXML('<div><test x="old"></test></div>');
1608 doc.registerExtension({elementNode: {transformations: {
1612 node = doc.root.contents()[0];
1613 nodePath = node.getPath();
1616 it('transforms as expected', function() {
1618 expect(node.getAttr('x')).to.equal('new');
1621 it('can be undone', function() {
1624 node = doc.getNodeByPath(nodePath);
1625 expect(node.getAttr('x')).to.equal('old');
1628 it('can be undone and then redone', function() {
1632 node = doc.getNodeByPath(nodePath);
1633 expect(node.getAttr('x')).to.equal('new');
1636 it('handles a sample scenario', function() {
1637 doc.root.contents()[0].test('1');
1638 doc.root.contents()[0].test('2');
1639 doc.root.contents()[0].test('3');
1640 doc.root.contents()[0].test('4');
1641 doc.root.contents()[0].test('5');
1643 expect(doc.root.contents()[0].getAttr('x')).to.equal('5', 'after initial transformations');
1645 expect(doc.root.contents()[0].getAttr('x')).to.equal('4', 'undo 1.1');
1647 expect(doc.root.contents()[0].getAttr('x')).to.equal('3', 'undo 1.2');
1649 expect(doc.root.contents()[0].getAttr('x')).to.equal('4', 'redo 1.1');
1651 expect(doc.root.contents()[0].getAttr('x')).to.equal('5', 'redo 1.2');
1653 expect(doc.root.contents()[0].getAttr('x')).to.equal('4', 'undo 2.1');
1654 doc.root.contents()[0].test('10');
1655 expect(doc.root.contents()[0].getAttr('x')).to.equal('10', 'additional transformation');
1656 expect(doc.redoStack.length).to.equal(0, 'transformation cleared redo stack');
1658 expect(doc.root.contents()[0].getAttr('x')).to.equal('10', 'empty redoStack so redo was noop');
1660 expect(doc.root.contents()[0].getAttr('x')).to.equal('4', 'undoing additional transformation');
1662 expect(doc.root.contents()[0].getAttr('x')).to.equal('10', 'redoing additional transformation');
1667 it('smoke tests nested transformations', function() {
1668 var doc = getDocumentFromXML('<div></div>');
1670 doc.registerExtension({elementNode: {transformations: {
1671 nested: function(v) {
1672 this._$.attr('innerAttr', v);
1673 this.triggerChangeEvent();
1675 outer: function(v) {
1677 this._$.attr('outerAttr', v);
1678 this.triggerChangeEvent();
1682 doc.root.outer('test1');
1683 doc.root.outer('test2');
1685 expect(doc.root.getAttr('innerAttr')).to.equal('test2');
1686 expect(doc.root.getAttr('outerAttr')).to.equal('test2');
1690 expect(doc.root.getAttr('innerAttr')).to.equal('test1');
1691 expect(doc.root.getAttr('outerAttr')).to.equal('test1');
1695 expect(doc.root.getAttr('innerAttr')).to.equal(undefined);
1696 expect(doc.root.getAttr('outerAttr')).to.equal(undefined);
1700 expect(doc.root.getAttr('innerAttr')).to.equal('test1');
1701 expect(doc.root.getAttr('outerAttr')).to.equal('test1');
1705 expect(doc.root.getAttr('innerAttr')).to.equal('test2');
1706 expect(doc.root.getAttr('outerAttr')).to.equal('test2');
1710 it('ignores transformation if document didn\'t emit change event', function() {
1711 var doc = getDocumentFromXML('<div></div>');
1713 doc.registerExtension({elementNode: {transformations: {
1720 expect(doc.undoStack.length).to.equal(0);
1724 describe('Transactions', function() {
1725 it('allows to undo/redo series of transformations at once', function() {
1726 var doc = getDocumentFromXML('<div></div>');
1728 doc.registerExtension({
1729 elementNode: {transformations: {
1731 this.setAttr('test', v);
1736 doc.startTransaction();
1740 doc.endTransaction();
1743 expect(doc.root.getAttr('test'), '1');
1745 expect(doc.root.getAttr('test'), '3');
1747 expect(doc.root.getAttr('test'), '1');
1749 expect(doc.root.getAttr('test'), '3');
1752 it('ignores empty transactions', function() {
1753 var doc = getDocumentFromXML('<div></div>');
1754 doc.startTransaction();
1755 doc.endTransaction();
1756 expect(doc.undoStack).to.have.length(0, 'empty transaction doesn\'t get pushed into undo stack');
1759 it('doesn\'t break on optimizations', function() {
1760 // This is a smoke test checking if optimizations made to transaction undoing
1761 // doesnt't break anything.
1762 var doc = getDocumentFromXML('<div smart="1" unaware="1"></div>');
1764 doc.registerExtension({
1765 elementNode: {transformations: {
1766 unaware: function(v) {
1767 this.setAttr('unware', v);
1768 this.triggerChangeEvent();
1771 impl: function(t, v) {
1772 t.oldVal = this.getAttr('smart');
1773 this.setAttr('smart', v);
1774 this.triggerChangeEvent();
1777 this.setAttr('smart', t.oldVal);
1778 this.triggerChangeEvent();
1784 doc.startTransaction();
1785 doc.root.smart('2');
1786 doc.root.unaware('2');
1787 doc.root.smart('3');
1788 doc.root.unaware('3');
1789 doc.endTransaction();
1793 expect(doc.root.getAttr('smart')).to.equal('1');
1794 expect(doc.root.getAttr('unaware')).to.equal('1');
1797 it('can have associated metadata', function() {
1798 var doc = getDocumentFromXML('<div></div>'),
1799 metadata = Object.create({});
1801 doc.registerExtension({document: {transformations: {
1803 this.trigger('change');
1807 doc.startTransaction(metadata);
1809 doc.endTransaction();
1811 var transaction = doc.undoStack[0];
1812 expect(transaction.metadata).to.equal(metadata);
1815 it('can be rolled back', function() {
1816 var doc = getDocumentFromXML('<root></root>');
1818 doc.startTransaction();
1819 doc.root.append({tagName: 'div'});
1820 doc.rollbackTransaction();
1822 expect(doc.undoStack.length).to.equal(0, 'nothing to undo');
1823 expect(doc.root.contents().length).to.equal(0);
1826 it('rollbacks and calls error handleor if error gets thrown', function() {
1827 var doc = getDocumentFromXML('<root></root>'),
1831 doc.transaction(function() {
1832 doc.root.append({tagName: 'div'});
1836 expect(spy.args[0][0]).to.equal(err);
1837 expect(doc.root.contents().length).to.equal(0);
1838 expect(doc.undoStack.length).to.equal(0);
1842 describe('Regression tests', function() {
1843 it('redos correctly after running its own undo followed by unaware transformation undo', function() {
1844 var doc = getDocumentFromXML('<section t="0"></section>');
1846 doc.registerExtension({elementNode: {transformations: {
1847 unaware: function() {
1848 this.triggerChangeEvent();
1852 this._$.attr('t', 1);
1853 this.triggerChangeEvent();
1856 this._$.attr('t', 0);
1866 expect(doc.root.getAttr('t')).to.equal('1');
1868 it('can perform undo of an operation performed after automatic transaction rollback', function() {
1869 var doc = getDocumentFromXML('<section></section>'),
1870 extension = {document: {transformations: {
1871 throwingTransformation: function() { throw new Error(); }
1874 doc.registerExtension(extension);
1876 doc.throwingTransformation();
1878 doc.transaction(function() {
1879 doc.root.setAttr('x', '2');
1882 expect(doc.undoStack.length).to.equal(1);
1883 expect(doc.root.getAttr('x')).to.equal('2');
1887 expect(doc.undoStack.length).to.equal(0);
1888 expect(doc.root.getAttr('x')).to.be.undefined;