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>');
625 doc.registerExtension({documentNode: {methods: {
627 describesParent: function() {
628 return this.getTagName() === 'x';
633 var div = doc.root.contents()[0],
634 x = div.contents()[1];
637 expect(doc.root.contents().length).to.equal(2);
638 expect(x.isInDocument()).to.be.false;
641 it('unwrap single element node from its parent', function() {
642 var doc = getDocumentFromXML('<div><a><b></b></a></div>'),
644 a = div.contents()[0],
647 var parent = b.unwrap();
649 expect(parent.sameNode(div)).to.equal(true, 'returns new parent');
650 expect(div.contents()).to.have.length(1, 'root contains only one node');
651 expect(div.contents()[0].sameNode(b)).to.equal(true, 'node got unwrapped');
654 it('unwrap single text node from its parent', function() {
655 var doc = getDocumentFromXML('<div>Some <span>text</span>!</div>'),
657 span = div.contents()[1],
658 text = span.contents()[0];
660 var parent = text.unwrap();
662 expect(parent.sameNode(div)).to.equal(true, 'returns new parent');
663 expect(div.contents()).to.have.length(1, 'root contains only one node');
664 expect(div.contents()[0].getText()).to.equal('Some text!');
667 describe('Wrapping text', function() {
668 it('wraps text spanning multiple sibling TextNodes', function() {
669 var section = elementNodeFromXML('<section>Alice has a <span>small</span> cat</section>'),
670 wrapper = section.wrapText({
671 _with: {tagName: 'span', attrs: {'attr1': 'value1'}},
677 expect(section.contents().length).to.equal(2);
678 expect(section.contents()[0].nodeType).to.equal(Node.TEXT_NODE);
679 expect(section.contents()[0].getText()).to.equal('Alice ');
681 var wrapper2 = section.contents()[1];
682 expect(wrapper2.sameNode(wrapper)).to.be.true;
683 expect(wrapper.getTagName()).to.equal('span');
685 var wrapperContents = wrapper.contents();
686 expect(wrapperContents.length).to.equal(3);
687 expect(wrapperContents[0].getText()).to.equal('has a ');
689 expect(wrapperContents[1].nodeType).to.equal(Node.ELEMENT_NODE);
690 expect(wrapperContents[1].contents().length).to.equal(1);
691 expect(wrapperContents[1].contents()[0].getText()).to.equal('small');
694 it('keeps parent-describing nodes in place', function() {
695 var doc = getDocumentFromXML('<root>Alice <x></x> probably <y></y> has a cat</root>');
697 doc.registerExtension({documentNode: {methods: {
699 describesParent: function() {
701 return this.nodeType === Node.ELEMENT_NODE && this.getTagName() === 'x';
707 x = root.contents()[1],
708 y = root.contents()[3];
711 _with: {tagName: 'span', attrs: {'attr1': 'value1'}},
717 expect(x.parent().sameNode(root)).to.be.true;
718 expect(y.parent().getTagName()).to.equal('span');
722 describe('Wrapping Nodes', function() {
723 it('wraps multiple sibling nodes', function() {
724 var section = elementNodeFromXML('<section>Alice<div>has</div><div>a cat</div></section>'),
725 aliceText = section.contents()[0],
726 firstDiv = section.contents()[1],
727 lastDiv = section.contents()[section.contents().length -1];
729 var returned = section.document.wrapNodes({
732 _with: {tagName: 'header'}
735 var sectionContentss = section.contents(),
736 header = sectionContentss[0],
737 headerContents = header.contents();
739 expect(sectionContentss).to.have.length(1);
740 expect(header.sameNode(returned)).to.equal(true, 'wrapper returned');
741 expect(header.parent().sameNode(section)).to.be.true;
742 expect(headerContents).to.have.length(3);
743 expect(headerContents[0].sameNode(aliceText)).to.equal(true, 'first node wrapped');
744 expect(headerContents[1].sameNode(firstDiv)).to.equal(true, 'second node wrapped');
745 expect(headerContents[2].sameNode(lastDiv)).to.equal(true, 'third node wrapped');
748 it('wraps multiple sibling Elements - middle case', function() {
749 var section = elementNodeFromXML('<section><div></div><div></div><div></div><div></div></section>'),
750 div2 = section.contents()[1],
751 div3 = section.contents()[2];
753 section.document.wrapNodes({
756 _with: {tagName: 'header'}
759 var sectionContentss = section.contents(),
760 header = sectionContentss[1],
761 headerChildren = header.contents();
763 expect(sectionContentss).to.have.length(3);
764 expect(headerChildren).to.have.length(2);
765 expect(headerChildren[0].sameNode(div2)).to.equal(true, 'first node wrapped');
766 expect(headerChildren[1].sameNode(div3)).to.equal(true, 'second node wrapped');
769 it('keeps parent-describing nodes in place', function() {
770 var section = elementNodeFromXML('<section>Alice<x></x><div>a cat</div></section>'),
771 aliceText = section.contents()[0],
772 x = section.contents()[1],
773 lastDiv = section.contents()[2];
775 section.document.registerExtension({documentNode: {methods: {
777 describesParent: function() {
778 return this.nodeType === Node.ELEMENT_NODE && this.getTagName() === 'x';
783 section.document.wrapNodes({
786 _with: {tagName: 'header'}
789 expect(x.parent().sameNode(section)).to.be.true;
790 expect(aliceText.parent().getTagName()).to.equal('header');
791 expect(lastDiv.parent().getTagName()).to.equal('header');
797 var getTextNodes = function(text, doc) {
800 var search = function(node) {
801 node.contents().forEach(function(node) {
802 if(node.nodeType === Node.TEXT_NODE) {
803 if(node.getText() === text) {
815 var getTextNode = function(text, doc) {
816 var nodes = getTextNodes(text, doc),
818 if(nodes.length === 0) {
819 error = 'Text not found';
820 } else if(nodes.length > 1) {
821 error = 'Text not unique';
822 } else if(nodes[0].getText() !== text) {
823 error = 'I was trying to cheat your test :(';
826 throw new Error(error);
831 describe('Removing arbitrary text', function() {
832 it('removes within single text element', function() {
833 var doc = getDocumentFromXML('<div>Alice</div>'),
834 text = getTextNode('Alice', doc);
845 expect(doc.root.contents().length).to.equal(1);
846 expect(doc.root.contents()[0].getText()).to.equal('Ae');
848 it('removes across elements - 1', function() {
849 var doc = getDocumentFromXML('<div><a>aaa</a><b>bbb</b></div>');
853 node: getTextNode('aaa', doc),
857 node: getTextNode('bbb', doc),
862 var contents = doc.root.contents();
863 expect(contents.length).to.equal(2);
864 expect(contents[0].contents()[0].getText()).to.equal('aa');
865 expect(contents[1].contents()[0].getText()).to.equal('b');
867 it('removes across elements - 2', function() {
868 var doc = getDocumentFromXML('<a><b><c>ccc</c></b>xxx</a>');
871 node: getTextNode('ccc', doc),
875 node: getTextNode('xxx', doc),
880 var contents = doc.root.contents();
881 expect(contents.length).to.equal(2);
882 expect(contents[0].getTagName()).to.equal('b');
883 expect(contents[1].getText()).to.equal('x');
885 var bContents = contents[0].contents();
886 expect(bContents.length).to.equal(1);
887 expect(bContents[0].getTagName()).to.equal('c');
888 expect(bContents[0].contents().length).to.equal(1);
889 expect(bContents[0].contents()[0].getText()).to.equal('cc');
891 it('remove across elements - 3 (merged text nodes)', function() {
892 var doc = getDocumentFromXML('<div>Alice <span>has</span> a cat</div>');
895 node: getTextNode('Alice ', doc),
899 node: getTextNode(' a cat', doc),
903 var contents = doc.root.contents();
904 expect(contents.length).to.equal(1);
905 expect(contents[0].getText()).to.equal('Acat');
907 it('remove across elements - 4', function() {
908 var doc = getDocumentFromXML('<div>Alice <div>has <span>a</span> cat</div></div>');
911 node: getTextNode('Alice ', doc),
915 node: getTextNode(' cat', doc),
919 var contents = doc.root.contents();
920 expect(contents.length).to.equal(2);
921 expect(contents[0].getText()).to.equal('A');
922 expect(contents[1].getTagName()).to.equal('div');
923 expect(contents[1].contents().length).to.equal(1);
924 expect(contents[1].contents()[0].getText()).to.equal('cat');
926 it('removes across elements - 5 (whole document)', function() {
927 var doc = getDocumentFromXML('<div>Alice <div>has <span>a</span> cat</div>!!!</div>');
930 node: getTextNode('Alice ', doc),
934 node: getTextNode('!!!', doc),
939 expect(doc.root.getTagName()).to.equal('div');
940 expect(doc.root.contents().length).to.equal(1);
941 expect(doc.root.contents()[0].getText()).to.equal('');
943 it('removes nodes in between', function() {
944 var doc = getDocumentFromXML('<div><a>aaa<x>!</x></a>xxx<x></x><b><x>!</x>bbb</b></div>');
947 node: getTextNode('aaa', doc),
951 node: getTextNode('bbb', doc),
956 var contents = doc.root.contents();
957 expect(contents.length).to.equal(2, 'two nodes survived');
958 expect(contents[0].getTagName()).to.equal('a');
959 expect(contents[1].getTagName()).to.equal('b');
960 expect(contents[0].contents().length).to.equal(1);
961 expect(contents[0].contents()[0].getText()).to.equal('aa');
962 expect(contents[1].contents().length).to.equal(1);
963 expect(contents[1].contents()[0].getText()).to.equal('b');
965 it('removes across elements - 6', function() {
966 var doc = getDocumentFromXML('<root><div>aaa<span>bbb</span>ccc</div><div>ddd</div></root>');
969 node: getTextNode('aaa', doc),
973 node: getTextNode('ddd', doc),
977 error: function(e) {throw e;}
980 var contents = doc.root.contents();
981 expect(contents.length).to.equal(2);
982 expect(contents[0].contents().length).to.equal(1);
983 expect(contents[0].contents()[0].getText()).to.equal('a');
984 expect(contents[1].contents().length).to.equal(1);
985 expect(contents[1].contents()[0].getText()).to.equal('dd');
989 describe('Splitting text', function() {
991 it('splits TextNode\'s parent into two ElementNodes', function() {
992 var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
994 text = section.contents()[0].contents()[0];
996 var returnedValue = text.split({offset: 5});
997 expect(section.contents().length).to.equal(2, 'section has two children');
999 var header1 = section.contents()[0];
1000 var header2 = section.contents()[1];
1002 expect(header1.getTagName()).to.equal('header', 'first section child ok');
1003 expect(header1.contents().length).to.equal(1, 'first header has one child');
1004 expect(header1.contents()[0].getText()).to.equal('Some ', 'first header has correct content');
1005 expect(header2.getTagName()).to.equal('header', 'second section child ok');
1006 expect(header2.contents().length).to.equal(1, 'second header has one child');
1007 expect(header2.contents()[0].getText()).to.equal('header', 'second header has correct content');
1009 expect(returnedValue.first.sameNode(header1)).to.equal(true, 'first node returned');
1010 expect(returnedValue.second.sameNode(header2)).to.equal(true, 'second node returned');
1013 it('leaves empty copy of ElementNode if splitting at the very beginning', function() {
1014 var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
1016 text = section.contents()[0].contents()[0];
1018 text.split({offset: 0});
1020 var header1 = section.contents()[0];
1021 var header2 = section.contents()[1];
1023 expect(header1.contents().length).to.equal(0);
1024 expect(header2.contents()[0].getText()).to.equal('Some header');
1027 it('leaves empty copy of ElementNode if splitting at the very end', function() {
1028 var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
1030 text = section.contents()[0].contents()[0];
1032 text.split({offset: 11});
1034 var header1 = section.contents()[0];
1035 var header2 = section.contents()[1];
1037 expect(header1.contents()[0].getText()).to.equal('Some header');
1038 expect(header2.contents().length).to.equal(0);
1041 it('keeps TextNodes\'s parent\'s children elements intact', function() {
1042 var doc = getDocumentFromXML('<section><header>A <span>fancy</span> and <span>nice</span> header</header></section>'),
1044 header = section.contents()[0],
1045 textAnd = header.contents()[2];
1047 textAnd.split({offset: 2});
1049 var sectionContents = section.contents();
1050 expect(sectionContents.length).to.equal(2, 'Section has two children');
1051 expect(sectionContents[0].getTagName()).to.equal('header', 'First section node is a header');
1052 expect(sectionContents[1].getTagName()).to.equal('header', 'Second section node is a header');
1054 var firstHeaderContents = sectionContents[0].contents();
1055 expect(firstHeaderContents.length).to.equal(3, 'First header has three children');
1056 expect(firstHeaderContents[0].getText()).to.equal('A ', 'First header starts with a text');
1057 expect(firstHeaderContents[1].getTagName()).to.equal('span', 'First header has span in the middle');
1058 expect(firstHeaderContents[2].getText()).to.equal(' a', 'First header ends with text');
1060 var secondHeaderContents = sectionContents[1].contents();
1061 expect(secondHeaderContents.length).to.equal(3, 'Second header has three children');
1062 expect(secondHeaderContents[0].getText()).to.equal('nd ', 'Second header starts with text');
1063 expect(secondHeaderContents[1].getTagName()).to.equal('span', 'Second header has span in the middle');
1064 expect(secondHeaderContents[2].getText()).to.equal(' header', 'Second header ends with text');
1068 describe('Events', function() {
1069 it('emits nodeDetached event on node detach', function() {
1070 var node = elementNodeFromXML('<div><div></div></div>'),
1071 innerNode = node.contents()[0],
1073 node.document.on('change', spy);
1075 var detached = innerNode.detach(),
1076 event = spy.args[0][0];
1078 expect(event.type).to.equal('nodeDetached');
1079 expect(event.meta.node.sameNode(detached, 'detached node in event meta'));
1080 expect(event.meta.parent.sameNode(node), 'original parent node in event meta');
1083 it('emits nodeAdded event when appending new node', function() {
1084 var node = elementNodeFromXML('<div></div>'),
1086 node.document.on('change', spy);
1088 var appended = node.append({tagName:'div'}),
1089 event = spy.args[0][0];
1090 expect(event.type).to.equal('nodeAdded');
1091 expect(event.meta.node.sameNode(appended)).to.be.true;
1094 it('emits nodeDetached/nodeAdded events with `move` flag when appending aready existing node', function() {
1095 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
1096 a = node.contents()[0],
1097 b = node.contents()[1],
1099 node.document.on('change', spy);
1101 var appended = a.append(b),
1102 detachedEvent = spy.args[0][0],
1103 addedEvent = spy.args[1][0];
1105 expect(spy.callCount).to.equal(2);
1106 expect(detachedEvent.type).to.equal('nodeDetached');
1107 expect(detachedEvent.meta.node.sameNode(appended)).to.be.true;
1108 expect(detachedEvent.meta.move).to.equal(true, 'move flag set to true for nodeDetachedEvent');
1109 expect(addedEvent.type).to.equal('nodeAdded');
1110 expect(addedEvent.meta.node.sameNode(appended)).to.be.true;
1111 expect(addedEvent.meta.move).to.equal(true, 'move flag set to true for nodeAddedEvent');
1115 it('emits nodeAdded event when prepending new node', function() {
1116 var node = elementNodeFromXML('<div></div>'),
1118 node.document.on('change', spy);
1120 var prepended = node.prepend({tagName:'div'}),
1121 event = spy.args[0][0];
1122 expect(event.type).to.equal('nodeAdded');
1123 expect(event.meta.node.sameNode(prepended)).to.be.true;
1126 it('emits nodeDetached/nodeAdded events with `move` flag when prepending aready existing node', function() {
1127 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
1128 a = node.contents()[0],
1129 b = node.contents()[1],
1131 node.document.on('change', spy);
1133 var prepended = a.prepend(b),
1134 detachedEvent = spy.args[0][0],
1135 addedEvent = spy.args[1][0];
1137 expect(spy.callCount).to.equal(2);
1138 expect(detachedEvent.type).to.equal('nodeDetached');
1139 expect(detachedEvent.meta.node.sameNode(prepended)).to.be.true;
1140 expect(detachedEvent.meta.move).to.equal(true, 'move flag set to true for nodeDetachedEvent');
1141 expect(addedEvent.type).to.equal('nodeAdded');
1142 expect(addedEvent.meta.node.sameNode(prepended)).to.be.true;
1143 expect(addedEvent.meta.move).to.equal(true, 'move flag set to true for nodeAddedEvent');
1146 it('emits nodeAdded event when inserting node after another', function() {
1147 var node = elementNodeFromXML('<div><a></a></div>').contents()[0],
1149 node.document.on('change', spy);
1151 var inserted = node.after({tagName:'div'}),
1152 event = spy.args[0][0];
1153 expect(event.type).to.equal('nodeAdded');
1154 expect(event.meta.node.sameNode(inserted)).to.be.true;
1157 it('emits nodeDetached/nodeAdded events with `move` flag when inserting aready existing node after another', function() {
1158 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
1159 a = node.contents()[0],
1160 b = node.contents()[1],
1162 node.document.on('change', spy);
1163 var inserted = b.after(a),
1164 detachedEvent = spy.args[0][0],
1165 addedEvent = spy.args[1][0];
1167 expect(spy.callCount).to.equal(2);
1168 expect(detachedEvent.type).to.equal('nodeDetached');
1169 expect(detachedEvent.meta.node.sameNode(inserted)).to.be.true;
1170 expect(detachedEvent.meta.move).to.equal(true, 'move flag set to true for nodeDetachedEvent');
1171 expect(addedEvent.type).to.equal('nodeAdded');
1172 expect(addedEvent.meta.node.sameNode(inserted)).to.be.true;
1173 expect(addedEvent.meta.move).to.equal(true, 'move flag set to true for nodeAddedEvent');
1176 it('emits nodeAdded event when inserting node before another', function() {
1177 var node = elementNodeFromXML('<div><a></a></div>').contents()[0],
1179 node.document.on('change', spy);
1181 var inserted = node.before({tagName:'div'}),
1182 event = spy.args[0][0];
1183 expect(event.type).to.equal('nodeAdded');
1184 expect(event.meta.node.sameNode(inserted)).to.be.true;
1187 it('emits nodeDetached/nodeAdded events with `move` flag when inserting aready existing node before another', function() {
1188 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
1189 a = node.contents()[0],
1190 b = node.contents()[1],
1192 node.document.on('change', spy);
1193 var inserted = a.before(b),
1194 detachedEvent = spy.args[0][0],
1195 addedEvent = spy.args[1][0];
1197 expect(spy.callCount).to.equal(2);
1198 expect(detachedEvent.type).to.equal('nodeDetached');
1199 expect(detachedEvent.meta.node.sameNode(inserted)).to.be.true;
1200 expect(detachedEvent.meta.move).to.equal(true, 'move flag set to true for nodeDetachedEvent');
1201 expect(addedEvent.type).to.equal('nodeAdded');
1202 expect(addedEvent.meta.node.sameNode(inserted)).to.be.true;
1203 expect(addedEvent.meta.move).to.equal(true, 'move flag set to true for nodeAddedEvent');
1206 it('emits nodeDetached and nodeAdded when replacing root node with another', function() {
1207 var doc = getDocumentFromXML('<a></a>'),
1211 doc.on('change', spy);
1213 doc.root.replaceWith({tagName: 'b'});
1215 expect(spy.callCount).to.equal(2);
1217 var event1 = spy.args[0][0],
1218 event2 = spy.args[1][0];
1220 expect(event1.type).to.equal('nodeDetached');
1221 expect(event1.meta.node.sameNode(oldRoot)).to.equal(true, 'root node in nodeDetached event metadata');
1222 expect(event2.type).to.equal('nodeAdded');
1223 expect(event2.meta.node.sameNode(doc.root)).to.equal(true, 'new root node in nodelAdded event meta');
1227 ['append', 'prepend', 'before', 'after'].forEach(function(insertionMethod) {
1228 it('emits nodeDetached for node moved from a document tree to out of document node ' + insertionMethod, function() {
1229 var doc = getDocumentFromXML('<div><a></a></div>'),
1230 a = doc.root.contents()[0],
1233 doc.on('change', spy);
1235 var newNode = doc.createDocumentNode({tagName: 'b'}),
1236 newNodeInner = newNode.append({tagName:'c'});
1238 newNodeInner[insertionMethod](a);
1240 var event = spy.args[0][0];
1241 expect(event.type).to.equal('nodeDetached');
1242 expect(event.meta.node.sameNode(a));
1245 it('doesn\'t emit nodeDetached event for already out of document node moved to out of document node' + insertionMethod, function() {
1246 var doc = getDocumentFromXML('<div><a></a></div>'),
1249 doc.on('change', spy);
1251 var newNode = doc.createDocumentNode({tagName: 'b'});
1252 newNode.append({tagName:'c'});
1254 expect(spy.callCount).to.equal(0);
1261 describe('Traversing', function() {
1262 describe('Basic', function() {
1263 it('can access node parent', function() {
1264 var doc = getDocumentFromXML('<a><b></b></a>'),
1266 b = a.contents()[0];
1268 expect(a.parent()).to.equal(null, 'parent of a root is null');
1269 expect(b.parent().sameNode(a)).to.be.true;
1271 it('can access node parents', function() {
1272 var doc = getDocumentFromXML('<a><b><c></c></b></a>'),
1274 b = a.contents()[0],
1275 c = b.contents()[0];
1277 var parents = c.parents();
1279 expect(parents[0].sameNode(b)).to.be.true;
1280 expect(parents[1].sameNode(a)).to.be.true;
1284 describe('finding sibling parents of two elements', function() {
1285 it('returns elements themself if they have direct common parent', function() {
1286 var doc = getDocumentFromXML('<section><div><div>A</div><div>B</div></div></section>'),
1287 wrappingDiv = doc.root.contents()[0],
1288 divA = wrappingDiv.contents()[0],
1289 divB = wrappingDiv.contents()[1];
1291 var siblingParents = doc.getSiblingParents({node1: divA, node2: divB});
1293 expect(siblingParents.node1.sameNode(divA)).to.equal(true, 'divA');
1294 expect(siblingParents.node2.sameNode(divB)).to.equal(true, 'divB');
1297 it('returns sibling parents - example 1', function() {
1298 var doc = getDocumentFromXML('<section>Alice <span>has a cat</span></section>'),
1299 aliceText = doc.root.contents()[0],
1300 span = doc.root.contents()[1],
1301 spanText = span.contents()[0];
1303 var siblingParents = doc.getSiblingParents({node1: aliceText, node2: spanText});
1305 expect(siblingParents.node1.sameNode(aliceText)).to.equal(true, 'aliceText');
1306 expect(siblingParents.node2.sameNode(span)).to.equal(true, 'span');
1309 it('returns node itself for two same nodes', function() {
1310 var doc = getDocumentFromXML('<section><div></div></section>'),
1311 div = doc.root.contents()[0];
1313 var siblingParents = doc.getSiblingParents({node1: div, node2: div});
1314 expect(!!siblingParents.node1 && !!siblingParents.node2).to.equal(true, 'nodes defined');
1315 expect(siblingParents.node1.sameNode(div)).to.be.equal(true, 'node1');
1316 expect(siblingParents.node2.sameNode(div)).to.be.equal(true, 'node2');
1321 describe('Serializing document to WLXML', function() {
1322 it('keeps document intact when no changes have been made', function() {
1323 var xmlIn = '<section>Alice<div>has</div>a <span class="uri" meta-uri="http://cat.com">cat</span>!</section>',
1324 doc = getDocumentFromXML(xmlIn),
1325 xmlOut = doc.toXML();
1327 var parser = new DOMParser(),
1328 input = parser.parseFromString(xmlIn, 'application/xml').childNodes[0],
1329 output = parser.parseFromString(xmlOut, 'application/xml').childNodes[0];
1331 expect(input.isEqualNode(output)).to.be.true;
1334 it('keeps entities intact', function() {
1335 var xmlIn = '<section>< ></section>',
1336 doc = getDocumentFromXML(xmlIn),
1337 xmlOut = doc.toXML();
1338 expect(xmlOut).to.equal(xmlIn);
1340 it('keeps entities intact when they form html/xml', function() {
1341 var xmlIn = '<section><abc></section>',
1342 doc = getDocumentFromXML(xmlIn),
1343 xmlOut = doc.toXML();
1344 expect(xmlOut).to.equal(xmlIn);
1348 describe('Extension API', function() {
1349 var doc, extension, elementNode, textNode;
1351 beforeEach(function() {
1352 doc = getDocumentFromXML('<section>Alice<div class="test_class"></div></section>');
1355 it('allows adding method to a document', function() {
1356 extension = {document: {methods: {
1357 testMethod: function() { return this; }
1360 doc.registerExtension(extension);
1361 expect(doc.testMethod()).to.equal(doc, 'context is set to a document instance');
1364 it('allows adding transformation to a document', function() {
1365 extension = {document: {transformations: {
1366 testTransformation: function() { return this; },
1367 testTransformation2: {impl: function() { return this;}}
1370 doc.registerExtension(extension);
1371 expect(doc.testTransformation()).to.equal(doc, 'context is set to a document instance');
1372 expect(doc.testTransformation2()).to.equal(doc, 'context is set to a document instance');
1375 it('allows adding method to a DocumentNode instance', function() {
1379 testMethod: function() { return this; }
1384 textTestMethod: function() { return this; }
1389 elementTestMethod: function() { return this; }
1394 doc.registerExtension(extension);
1396 elementNode = doc.root;
1397 textNode = doc.root.contents()[0];
1399 expect(elementNode.testMethod().sameNode(elementNode)).to.equal(true, 'context is set to a node instance');
1400 expect(textNode.testMethod().sameNode(textNode)).to.equal(true, 'context is set to a node instance');
1402 expect(elementNode.elementTestMethod().sameNode(elementNode)).to.be.true;
1403 expect(elementNode.textTestMethod).to.be.undefined;
1405 expect(textNode.textTestMethod().sameNode(textNode)).to.be.true;
1406 expect(textNode.elementTestMethod).to.be.undefined;
1409 it('allows adding transformation to a DocumentNode', function() {
1413 testTransformation: function() { return this; },
1414 testTransformation2: {impl: function() { return this;}}
1419 textTestTransformation: function() { return this; }
1424 elementTestTransformation: function() { return this; }
1429 doc.registerExtension(extension);
1431 elementNode = doc.root;
1432 textNode = doc.root.contents()[0];
1434 expect(elementNode.testTransformation().sameNode(elementNode)).to.equal(true, '1');
1435 expect(elementNode.testTransformation2().sameNode(elementNode)).to.equal(true, '2');
1436 expect(textNode.testTransformation().sameNode(textNode)).to.equal(true, '3');
1437 expect(textNode.testTransformation2().sameNode(textNode)).to.equal(true, '4');
1439 expect(elementNode.elementTestTransformation().sameNode(elementNode)).to.be.true;
1440 expect(elementNode.textTestTransformation).to.be.undefined;
1442 expect(textNode.textTestTransformation().sameNode(textNode)).to.be.true;
1443 expect(textNode.elementTestTransfomation).to.be.undefined;
1446 it('allows text/element node methods and transformations to access node and transormations on document node', function() {
1448 var doc = getDocumentFromXML('<div>text</div>');
1450 doc.registerExtension({
1459 return 'super_trans';
1466 return 'element_sub_' + this.__super__.test();
1471 return 'element_trans_sub_' + this.__super__.testT();
1478 return 'text_sub_' + this.__super__.test();
1483 return 'text_trans_sub_' + this.__super__.testT();
1489 var textNode = doc.root.contents()[0];
1491 expect(doc.root.test()).to.equal('element_sub_super');
1492 expect(textNode.test()).to.equal('text_sub_super');
1493 expect(doc.root.testT()).to.equal('element_trans_sub_super_trans');
1494 expect(textNode.testT()).to.equal('text_trans_sub_super_trans');
1498 describe('Undo/redo', function() {
1500 it('smoke tests', function() {
1501 var doc = getDocumentFromXML('<div>Alice</div>'),
1502 textNode = doc.root.contents()[0];
1504 expect(doc.undoStack).to.have.length(0);
1506 textNode.wrapWith({tagName: 'span', start:1, end:2});
1507 expect(doc.undoStack).to.have.length(1, '1');
1508 expect(doc.toXML()).to.equal('<div>A<span>l</span>ice</div>');
1511 expect(doc.undoStack).to.have.length(0, '2');
1512 expect(doc.toXML()).to.equal('<div>Alice</div>');
1515 expect(doc.undoStack).to.have.length(1, '3');
1516 expect(doc.toXML()).to.equal('<div>A<span>l</span>ice</div>');
1519 expect(doc.undoStack).to.have.length(0, '4');
1520 expect(doc.toXML()).to.equal('<div>Alice</div>');
1523 expect(doc.undoStack).to.have.length(0, '5');
1524 expect(doc.toXML()).to.equal('<div>Alice</div>');
1527 it('smoke tests 2', function() {
1528 var doc = getDocumentFromXML('<div>Alice</div>'),
1529 textNode = doc.root.contents()[0],
1530 path = textNode.getPath();
1532 textNode.setText('Alice ');
1533 textNode.setText('Alice h');
1534 textNode.setText('Alice ha');
1535 textNode.setText('Alice has');
1537 expect(textNode.getText()).to.equal('Alice has');
1540 expect(doc.root.contents()[0].getText()).to.equal('Alice ha', '1');
1543 expect(doc.root.contents()[0].getText()).to.equal('Alice h', '2');
1546 expect(doc.root.contents()[0].getText()).to.equal('Alice ha', '3');
1549 expect(doc.root.contents()[0].getText()).to.equal('Alice has', '4');
1553 textNode = doc.getNodeByPath(path);
1554 textNode.setText('Cat');
1556 textNode = doc.getNodeByPath(path);
1557 expect(textNode.getText()).to.equal('Alice h');
1561 var sampleMethod = function(val) {
1562 this._$.attr('x', val);
1563 this.triggerChangeEvent();
1566 var transformations = {
1567 'unaware': sampleMethod,
1568 'returning change root': {
1570 getChangeRoot: function() {
1571 return this.context;
1574 'implementing undo operation': {
1575 impl: function(t, val) {
1576 t.oldVal = this.getAttr('x');
1577 sampleMethod.call(this, val);
1580 this.setAttr('x', t.oldVal);
1585 _.pairs(transformations).forEach(function(pair) {
1587 transformaton = pair[1];
1589 describe(name + ' transformation: ', function() {
1590 var doc, node, nodePath;
1592 beforeEach(function() {
1593 doc = getDocumentFromXML('<div><test x="old"></test></div>');
1595 doc.registerExtension({elementNode: {transformations: {
1599 node = doc.root.contents()[0];
1600 nodePath = node.getPath();
1603 it('transforms as expected', function() {
1605 expect(node.getAttr('x')).to.equal('new');
1608 it('can be undone', function() {
1611 node = doc.getNodeByPath(nodePath);
1612 expect(node.getAttr('x')).to.equal('old');
1615 it('can be undone and then redone', function() {
1619 node = doc.getNodeByPath(nodePath);
1620 expect(node.getAttr('x')).to.equal('new');
1623 it('handles a sample scenario', function() {
1624 doc.root.contents()[0].test('1');
1625 doc.root.contents()[0].test('2');
1626 doc.root.contents()[0].test('3');
1627 doc.root.contents()[0].test('4');
1628 doc.root.contents()[0].test('5');
1630 expect(doc.root.contents()[0].getAttr('x')).to.equal('5', 'after initial transformations');
1632 expect(doc.root.contents()[0].getAttr('x')).to.equal('4', 'undo 1.1');
1634 expect(doc.root.contents()[0].getAttr('x')).to.equal('3', 'undo 1.2');
1636 expect(doc.root.contents()[0].getAttr('x')).to.equal('4', 'redo 1.1');
1638 expect(doc.root.contents()[0].getAttr('x')).to.equal('5', 'redo 1.2');
1640 expect(doc.root.contents()[0].getAttr('x')).to.equal('4', 'undo 2.1');
1641 doc.root.contents()[0].test('10');
1642 expect(doc.root.contents()[0].getAttr('x')).to.equal('10', 'additional transformation');
1643 expect(doc.redoStack.length).to.equal(0, 'transformation cleared redo stack');
1645 expect(doc.root.contents()[0].getAttr('x')).to.equal('10', 'empty redoStack so redo was noop');
1647 expect(doc.root.contents()[0].getAttr('x')).to.equal('4', 'undoing additional transformation');
1649 expect(doc.root.contents()[0].getAttr('x')).to.equal('10', 'redoing additional transformation');
1654 it('smoke tests nested transformations', function() {
1655 var doc = getDocumentFromXML('<div></div>');
1657 doc.registerExtension({elementNode: {transformations: {
1658 nested: function(v) {
1659 this._$.attr('innerAttr', v);
1660 this.triggerChangeEvent();
1662 outer: function(v) {
1664 this._$.attr('outerAttr', v);
1665 this.triggerChangeEvent();
1669 doc.root.outer('test1');
1670 doc.root.outer('test2');
1672 expect(doc.root.getAttr('innerAttr')).to.equal('test2');
1673 expect(doc.root.getAttr('outerAttr')).to.equal('test2');
1677 expect(doc.root.getAttr('innerAttr')).to.equal('test1');
1678 expect(doc.root.getAttr('outerAttr')).to.equal('test1');
1682 expect(doc.root.getAttr('innerAttr')).to.equal(undefined);
1683 expect(doc.root.getAttr('outerAttr')).to.equal(undefined);
1687 expect(doc.root.getAttr('innerAttr')).to.equal('test1');
1688 expect(doc.root.getAttr('outerAttr')).to.equal('test1');
1692 expect(doc.root.getAttr('innerAttr')).to.equal('test2');
1693 expect(doc.root.getAttr('outerAttr')).to.equal('test2');
1697 it('ignores transformation if document didn\'t emit change event', function() {
1698 var doc = getDocumentFromXML('<div></div>');
1700 doc.registerExtension({elementNode: {transformations: {
1707 expect(doc.undoStack.length).to.equal(0);
1711 describe('Transactions', function() {
1712 it('allows to undo/redo series of transformations at once', function() {
1713 var doc = getDocumentFromXML('<div></div>');
1715 doc.registerExtension({
1716 elementNode: {transformations: {
1718 this.setAttr('test', v);
1723 doc.startTransaction();
1727 doc.endTransaction();
1730 expect(doc.root.getAttr('test'), '1');
1732 expect(doc.root.getAttr('test'), '3');
1734 expect(doc.root.getAttr('test'), '1');
1736 expect(doc.root.getAttr('test'), '3');
1739 it('ignores empty transactions', function() {
1740 var doc = getDocumentFromXML('<div></div>');
1741 doc.startTransaction();
1742 doc.endTransaction();
1743 expect(doc.undoStack).to.have.length(0, 'empty transaction doesn\'t get pushed into undo stack');
1746 it('doesn\'t break on optimizations', function() {
1747 // This is a smoke test checking if optimizations made to transaction undoing
1748 // doesnt't break anything.
1749 var doc = getDocumentFromXML('<div smart="1" unaware="1"></div>');
1751 doc.registerExtension({
1752 elementNode: {transformations: {
1753 unaware: function(v) {
1754 this.setAttr('unware', v);
1755 this.triggerChangeEvent();
1758 impl: function(t, v) {
1759 t.oldVal = this.getAttr('smart');
1760 this.setAttr('smart', v);
1761 this.triggerChangeEvent();
1764 this.setAttr('smart', t.oldVal);
1765 this.triggerChangeEvent();
1771 doc.startTransaction();
1772 doc.root.smart('2');
1773 doc.root.unaware('2');
1774 doc.root.smart('3');
1775 doc.root.unaware('3');
1776 doc.endTransaction();
1780 expect(doc.root.getAttr('smart')).to.equal('1');
1781 expect(doc.root.getAttr('unaware')).to.equal('1');
1784 it('can have associated metadata', function() {
1785 var doc = getDocumentFromXML('<div></div>'),
1786 metadata = Object.create({});
1788 doc.registerExtension({document: {transformations: {
1790 this.trigger('change');
1794 doc.startTransaction(metadata);
1796 doc.endTransaction();
1798 var transaction = doc.undoStack[0];
1799 expect(transaction.metadata).to.equal(metadata);
1802 it('can be rolled back', function() {
1803 var doc = getDocumentFromXML('<root></root>');
1805 doc.startTransaction();
1806 doc.root.append({tagName: 'div'});
1807 doc.rollbackTransaction();
1809 expect(doc.undoStack.length).to.equal(0, 'nothing to undo');
1810 expect(doc.root.contents().length).to.equal(0);
1813 it('rollbacks and calls error handleor if error gets thrown', function() {
1814 var doc = getDocumentFromXML('<root></root>'),
1818 doc.transaction(function() {
1819 doc.root.append({tagName: 'div'});
1823 expect(spy.args[0][0]).to.equal(err);
1824 expect(doc.root.contents().length).to.equal(0);
1825 expect(doc.undoStack.length).to.equal(0);
1829 describe('Regression tests', function() {
1830 it('redos correctly after running its own undo followed by unaware transformation undo', function() {
1831 var doc = getDocumentFromXML('<section t="0"></section>');
1833 doc.registerExtension({elementNode: {transformations: {
1834 unaware: function() {
1835 this.triggerChangeEvent();
1839 this._$.attr('t', 1);
1840 this.triggerChangeEvent();
1843 this._$.attr('t', 0);
1853 expect(doc.root.getAttr('t')).to.equal('1');
1855 it('can perform undo of an operation performed after automatic transaction rollback', function() {
1856 var doc = getDocumentFromXML('<section></section>'),
1857 extension = {document: {transformations: {
1858 throwingTransformation: function() { throw new Error(); }
1861 doc.registerExtension(extension);
1863 doc.throwingTransformation();
1865 doc.transaction(function() {
1866 doc.root.setAttr('x', '2');
1869 expect(doc.undoStack.length).to.equal(1);
1870 expect(doc.root.getAttr('x')).to.equal('2');
1874 expect(doc.undoStack.length).to.equal(0);
1875 expect(doc.root.getAttr('x')).to.be.undefined;