6 ], function(chai, sinon, _, smartxml) {
10 /* global describe, it, beforeEach, Node, DOMParser */
12 var expect = chai.expect;
15 var getDocumentFromXML = function(xml) {
16 return smartxml.documentFromXML(xml);
19 var elementNodeFromParams = function(params) {
20 return smartxml.elementNodeFromXML('<' + params.tag + '></' + params.tag + '>');
23 var elementNodeFromXML = function(xml) {
24 return smartxml.elementNodeFromXML(xml);
28 describe('smartxml', function() {
30 describe('Basic Document properties', function() {
31 it('exposes its root element', function() {
32 var doc = getDocumentFromXML('<div></div>');
33 expect(doc.root.getTagName()).to.equal('div');
36 it('can resets its content entirely', function() {
37 var doc = getDocumentFromXML('<div></div>');
39 expect(doc.root.getTagName()).to.equal('div');
41 doc.loadXML('<header></header>');
42 expect(doc.root.getTagName()).to.equal('header');
45 it('knows if it contains an ElementNode in its tree', function() {
46 var doc = getDocumentFromXML('<root><a></a>text</root>'),
48 a = root.contents()[0],
49 text = root.contents()[1];
51 expect(doc.containsNode(root)).to.equal(true, 'contains its root');
52 expect(doc.containsNode(a)).to.equal(true, 'contains Element Node');
53 expect(doc.containsNode(text)).to.equal(true, 'contains Text Node');
56 it('creates text nodes', function() {
57 var doc = getDocumentFromXML('<div></div>'),
58 emptyTextNode = doc.createDocumentNode({text:''}),
59 nonEmptyTextNode = doc.createDocumentNode({text: 'alice'});
60 expect(emptyTextNode.getText()).to.equal('', 'empty ok');
61 expect(nonEmptyTextNode.getText()).to.equal('alice', 'non empty ok');
64 it('creates nodes from xml strings', function() {
65 var doc = getDocumentFromXML('<div></div>'),
66 node = doc.createDocumentNode('<a>Alice<b></b></a>');
67 expect(node.getTagName()).to.equal('a');
68 expect(node.contents().length).to.equal(2);
69 expect(node.contents()[0].getText()).to.equal('Alice');
70 expect(node.contents()[1].getTagName()).to.equal('b');
74 describe('DocumentNode', function() {
75 it('can be cloned', function() {
76 var doc = getDocumentFromXML('<div>Alice</div>'),
77 text = doc.root.contents()[0],
80 [doc.root, text].forEach(function(node) {
81 suffix = ' (' + (node.nodeType === Node.TEXT_NODE ? 'text' : 'element') + ')';
83 expect(doc.containsNode(clone)).to.equal(false, 'clone is not contained in a document' + suffix);
84 expect(node.sameNode(clone)).to.equal(false, 'clone is not same node as its originator' + suffix);
85 expect(node.nativeNode.isEqualNode(clone.nativeNode)).to.equal(true, 'clone is identical as its originator' + suffix);
89 it('can be cloned with its contents and its contents data', function() {
90 var doc = getDocumentFromXML('<root><div></div></root>'),
92 div = root.contents()[0];
94 var ClonableObject = function(arg) {
97 ClonableObject.prototype.clone = function() {
98 return new ClonableObject(this.arg);
101 div.setData('key', 'value');
102 div.setData('clonableObject', new ClonableObject('test'));
104 var rootClone = root.clone(),
105 divClone = rootClone.contents()[0],
106 stringClone = divClone.getData('key'),
107 objClone = divClone.getData('clonableObject');
109 expect(stringClone).to.equal('value');
110 expect(objClone.arg).to.equal('test', 'clonable object got copied');
111 expect(objClone !== div.getData('clonableObject')).to.be.equal(true, 'copy of the clonable object is a new object');
114 it('knows its path in the document tree', function() {
115 var doc = getDocumentFromXML('<root><a><b><c></c>text</b></a></root>'),
117 a = root.contents()[0],
119 text = b.contents()[1];
121 expect(root.getPath()).to.eql([], 'path of the root element is empty');
122 expect(a.getPath()).to.eql([0]);
123 expect(b.getPath()).to.eql([0, 0]);
124 expect(text.getPath()).to.eql([0,0,1]);
126 /* Paths relative to a given ancestor */
127 expect(text.getPath(root)).to.eql([0,0,1]);
128 expect(text.getPath(a)).to.eql([0,1]);
129 expect(text.getPath(b)).to.eql([1]);
133 describe('Basic ElementNode properties', function() {
134 it('exposes node contents', function() {
135 var node = elementNodeFromXML('<node>Some<node>text</node>is here</node>'),
136 contents = node.contents();
138 expect(contents).to.have.length(3);
139 expect(contents[0].nodeType).to.equal(Node.TEXT_NODE, 'text node 1');
140 expect(contents[1].nodeType).to.equal(Node.ELEMENT_NODE, 'element node 1');
141 expect(contents[2].nodeType).to.equal(Node.TEXT_NODE, 'text node 2');
144 describe('Storing custom data', function() {
147 beforeEach(function() {
148 node = elementNodeFromXML('<div></div>');
151 it('can append single value', function() {
152 node.setData('key', 'value');
153 expect(node.getData('key')).to.equal('value');
156 it('can overwrite the whole data', function() {
157 node.setData('key1', 'value1');
158 node.setData({key2: 'value2'});
159 expect(node.getData('key2')).to.equal('value2');
162 it('can fetch the whole data at once', function() {
163 node.setData({key1: 'value1', key2: 'value2'});
164 expect(node.getData()).to.eql({key1: 'value1', key2: 'value2'});
167 it('can remove specific data', function() {
168 node.setData('key', 'value');
169 node.setData('key', undefined);
170 expect(node.getData('key')).to.be.undefined;
174 describe('Changing node tag', function() {
176 it('can change tag name', function() {
177 var node = elementNodeFromXML('<div></div>');
178 node = node.setTag('span');
179 expect(node.getTagName()).to.equal('span');
182 describe('Implementation specific expectations', function() {
183 it('keeps custom data', function() {
184 var node = elementNodeFromXML('<div></div>');
186 node.setData('key', 'value');
187 node = node.setTag('header');
189 expect(node.getTagName()).to.equal('header');
190 expect(node.getData()).to.eql({key: 'value'});
193 it('can change document root tag name', function() {
194 var doc = getDocumentFromXML('<div></div>');
195 doc.root.setTag('span');
196 expect(doc.root.getTagName()).to.equal('span');
199 it('keeps node contents', function() {
200 var node = elementNodeFromXML('<div><div></div></div>');
201 node = node.setTag('header');
202 expect(node.contents()).to.have.length(1);
207 describe('Setting node attributes', function() {
208 it('can set node attribute', function() {
209 var node = elementNodeFromXML('<div></div>');
211 node.setAttr('key', 'value');
212 expect(node.getAttr('key')).to.equal('value');
214 it('emits nodeAttrChange event', function() {
215 var node = elementNodeFromXML('<div key="value1"></div>'),
218 node.document.on('change', spy);
219 node.setAttr('key', 'value2');
220 var event = spy.args[0][0];
222 expect(event.type).to.equal('nodeAttrChange');
223 expect(event.meta.node.sameNode(node)).to.be.true;
224 expect(event.meta.attr).to.equal('key');
225 expect(event.meta.oldVal).to.equal('value1');
229 describe('Searching for the last child text node', function() {
231 '<div>xxx<div></div>last</div>',
232 '<div><div>last</div></div>',
233 '<div>xxx<div>last</div><div></div></div>'
234 ].forEach(function(xml, i) {
235 var example = 'example ' + i;
236 it('returns last child text node ' + example + ')', function() {
237 var doc = getDocumentFromXML(xml),
238 lastTextNode = doc.root.getLastTextNode();
239 expect(lastTextNode.getText()).to.equal('last', example);
244 describe('Putting nodes around', function() {
245 it('will not allow to put node before or after root node', function() {
246 var doc = getDocumentFromXML('<root></root>'),
251 doc.on('change', spy);
253 result = doc.root.before({tagName: 'test'});
255 expect(spy.callCount).to.equal(0);
256 expect(result).to.undefined;
258 result = doc.root.after({tagName: 'test'});
260 expect(spy.callCount).to.equal(0);
261 expect(result).to.undefined;
263 expect(doc.root.sameNode(root));
268 describe('Basic TextNode properties', function() {
269 it('can have its text set', function() {
270 var node = elementNodeFromXML('<div>Alice</div>'),
271 textNode = node.contents()[0];
273 textNode.setText('Cat');
274 expect(textNode.getText()).to.equal('Cat');
277 it('emits nodeTextChange', function() {
278 var node = elementNodeFromXML('<div>Alice</div>'),
279 textNode = node.contents()[0],
282 textNode.document.on('change', spy);
283 textNode.setText('Cat');
285 var event = spy.args[0][0];
286 expect(event.type).to.equal('nodeTextChange');
289 it('puts NodeElement after itself', function() {
290 var node = elementNodeFromXML('<div>Alice</div>'),
291 textNode = node.contents()[0],
292 returned = textNode.after({tagName:'div'});
293 expect(returned.sameNode(node.contents()[1])).to.be.true;
296 it('puts NodeElement before itself', function() {
297 var node = elementNodeFromXML('<div>Alice</div>'),
298 textNode = node.contents()[0],
299 returned = textNode.before({tagName:'div'});
300 expect(returned.sameNode(node.contents()[0])).to.be.true;
303 describe('Wrapping TextNode contents', function() {
305 it('wraps DocumentTextElement', function() {
306 var node = elementNodeFromXML('<section>Alice</section>'),
307 textNode = node.contents()[0];
309 var returned = textNode.wrapWith({tagName: 'header'}),
310 parent = textNode.parent(),
311 parent2 = node.contents()[0];
313 expect(returned.sameNode(parent)).to.be.equal(true, 'wrapper is a parent');
314 expect(returned.sameNode(parent2)).to.be.equal(true, 'wrapper has a correct parent');
315 expect(returned.getTagName()).to.equal('header');
318 describe('wrapping part of DocumentTextElement', function() {
319 [{start: 5, end: 12}, {start: 12, end: 5}].forEach(function(offsets) {
320 it('wraps in the middle ' + offsets.start + '/' + offsets.end, function() {
321 var node = elementNodeFromXML('<section>Alice has a cat</section>'),
322 textNode = node.contents()[0];
324 var returned = textNode.wrapWith({tagName: 'header', attrs: {'attr1': 'value1'}, start: offsets.start, end: offsets.end}),
325 contents = node.contents();
327 expect(contents.length).to.equal(3);
329 expect(contents[0].nodeType).to.be.equal(Node.TEXT_NODE, 'first node is text node');
330 expect(contents[0].getText()).to.equal('Alice');
332 expect(contents[1].sameNode(returned)).to.be.true;
333 expect(returned.getTagName()).to.equal('header');
334 expect(returned.getAttr('attr1')).to.equal('value1');
335 expect(contents[1].contents().length).to.equal(1, 'wrapper has one node inside');
336 expect(contents[1].contents()[0].getText()).to.equal(' has a ');
338 expect(contents[2].nodeType).to.be.equal(Node.TEXT_NODE, 'third node is text node');
339 expect(contents[2].getText()).to.equal('cat');
343 it('wraps whole text inside DocumentTextElement if offsets span entire content', function() {
344 var node = elementNodeFromXML('<section>Alice has a cat</section>'),
345 textNode = node.contents()[0];
347 textNode.wrapWith({tagName: 'header', start: 0, end: 15});
349 var contents = node.contents();
350 expect(contents.length).to.equal(1);
351 expect(contents[0].getTagName()).to.equal('header');
352 expect(contents[0].contents()[0].getText()).to.equal('Alice has a cat');
357 describe('Dividing text node into two with element node', function() {
358 it('can divide text node with element node, splitting text node into two', function() {
359 var doc = getDocumentFromXML('<div>Alice has a cat</div>'),
360 text = doc.root.contents()[0];
362 var returned = text.divideWithElementNode({tagName: 'aside'}, {offset: 5}),
363 contents = doc.root.contents(),
364 lhsText = contents[0],
365 rhsText = contents[2];
367 expect(lhsText.getText()).to.equal('Alice');
368 expect(returned.sameNode(contents[1]));
369 expect(rhsText.getText()).to.equal(' has a cat');
372 it('treats dividing at the very end as appending after it', function() {
373 var doc = getDocumentFromXML('<div>Alice has a cat</div>'),
374 text = doc.root.contents()[0];
377 var returned = text.divideWithElementNode({tagName: 'aside'}, {offset: 15}),
378 contents = doc.root.contents(),
379 textNode = contents[0],
380 elementNode = contents[1];
382 expect(contents.length).to.equal(2);
383 expect(textNode.getText()).to.equal('Alice has a cat');
384 expect(returned.sameNode(elementNode)).to.be.true;
385 expect(elementNode.getTagName()).to.equal('aside');
388 it('treats dividing at the very beginning as prepending before it', function() {
389 var doc = getDocumentFromXML('<div>Alice has a cat</div>'),
390 text = doc.root.contents()[0];
392 var returned = text.divideWithElementNode({tagName: 'aside'}, {offset: 0}),
393 contents = doc.root.contents(),
394 textNode = contents[1],
395 elementNode = contents[0];
397 expect(contents.length).to.equal(2);
398 expect(textNode.getText()).to.equal('Alice has a cat');
399 expect(returned.sameNode(elementNode)).to.be.true;
400 expect(elementNode.getTagName()).to.equal('aside');
405 describe('Manipulations', function() {
407 describe('detaching nodes', function() {
408 it('can detach document root node', function() {
409 var doc = getDocumentFromXML('<div></div>');
412 expect(doc.root).to.equal(null);
416 describe('replacing node with another one', function() {
417 it('replaces node with another one', function() {
418 var doc = getDocumentFromXML('<div><a></a></div>'),
419 a = doc.root.contents()[0];
421 var c = a.replaceWith({tagName: 'b', attrs: {b:'1'}});
423 expect(doc.root.contents()[0].sameNode(c));
424 expect(c.getTagName()).to.equal('b');
425 expect(c.getAttr('b')).to.equal('1');
427 it('can replace document root', function() {
428 var doc = getDocumentFromXML('<div></div>');
430 var header = doc.root.replaceWith({tagName: 'header'});
432 expect(doc.root.sameNode(header)).to.be.true;
433 expect(doc.containsNode(header)).to.be.true;
437 it('merges adjacent text nodes resulting from detaching an element node in between', function() {
438 var doc = getDocumentFromXML('<div>Alice <span>has</span>a cat</div>'),
439 span = doc.root.contents()[1];
443 var rootContents = doc.root.contents();
444 expect(rootContents).to.have.length(1, 'one child left');
445 expect(rootContents[0].getText()).to.equal('Alice a cat');
448 it('merges adjacent text nodes resulting from moving an element node in between', function() {
449 var doc = getDocumentFromXML('<div><a></a>Alice <span>has</span>a cat</div>'),
450 span = doc.root.contents()[2],
451 a = doc.root.contents()[0];
455 var rootContents = doc.root.contents();
456 expect(rootContents).to.have.length(2, 'one child left');
457 expect(rootContents[1].getText()).to.equal('Alice a cat');
460 it('inserts node at index', function() {
461 var doc = getDocumentFromXML('<div><a></a><b></b><c></c></div>'),
462 b = doc.root.contents()[1];
464 var inserted = doc.root.insertAtIndex({tagName: 'test'}, 1);
466 expect(doc.root.contents()[1].sameNode(inserted)).to.equal(true, 'inserted node returned');
467 expect(b.getIndex()).to.equal(2, 'b node shifted right');
470 it('appends node when inserting node at index out of range', function() {
471 var doc = getDocumentFromXML('<div></div>');
473 var test1 = doc.root.insertAtIndex({tagName: 'test1'}, 0),
474 test2 = doc.root.insertAtIndex({tagName: 'test1'}, 10);
476 expect(doc.root.contents()[0].sameNode(test1)).to.equal(true, 'inserting at index 0 of empty nodes appends node');
477 expect(doc.root.contents().length).to.equal(1, 'inserting at index out of range does nothing');
478 expect(test2).to.equal(undefined, 'inserting at index out of range returns undefined');
481 it('appends element node to another element node', function() {
482 var node1 = elementNodeFromParams({tag: 'div'}),
483 node2 = elementNodeFromParams({tag: 'a'}),
484 node3 = elementNodeFromParams({tag: 'p'});
487 expect(node1.contents()[0].sameNode(node2)).to.be.true;
488 expect(node1.contents()[1].sameNode(node3)).to.be.true;
491 it('prepends element node to another element node', function() {
492 var node1 = elementNodeFromParams({tag: 'div'}),
493 node2 = elementNodeFromParams({tag: 'a'}),
494 node3 = elementNodeFromParams({tag: 'p'});
495 node1.prepend(node2);
496 node1.prepend(node3);
497 expect(node1.contents()[0].sameNode(node3)).to.be.true;
498 expect(node1.contents()[1].sameNode(node2)).to.be.true;
501 describe('adding text nodes', function() {
502 it('merges text nodes on append', function() {
503 var doc = getDocumentFromXML('<root>text1</root>'),
505 returned = doc.root.append({text: 'text2'});
506 expect(doc.root.contents().length).to.equal(1);
507 expect(returned.sameNode(doc.root.contents()[0])).to.equal(true, 'modified node returned');
508 expect(doc.root.contents()[0].getText()).to.equal('text1text2');
511 it('merges text nodes on prepend', function() {
512 var doc = getDocumentFromXML('<root>text1</root>'),
514 returned = doc.root.prepend({text: 'text2'});
515 expect(doc.root.contents().length).to.equal(1);
516 expect(returned.sameNode(doc.root.contents()[0])).to.equal(true, 'modified node returned');
517 expect(doc.root.contents()[0].getText()).to.equal('text2text1');
520 it('merges text nodes on before text node', function() {
521 var doc = getDocumentFromXML('<root>text1</root>'),
522 textNode = doc.root.contents()[0],
524 returned = textNode.before({text: 'text2'});
525 expect(doc.root.contents().length).to.equal(1);
526 expect(returned.sameNode(doc.root.contents()[0])).to.equal(true, 'modified node returned');
527 expect(doc.root.contents()[0].getText()).to.equal('text2text1');
530 it('merges text nodes on after text node', function() {
531 var doc = getDocumentFromXML('<root>text1</root>'),
532 textNode = doc.root.contents()[0],
534 returned = textNode.after({text: 'text2'});
535 expect(doc.root.contents().length).to.equal(1);
536 expect(returned.sameNode(doc.root.contents()[0])).to.equal(true, 'modified node returned');
537 expect(doc.root.contents()[0].getText()).to.equal('text1text2');
540 it('merges text nodes on before element node', function() {
541 var doc = getDocumentFromXML('<root>text1<div></div></root>'),
542 textNode = doc.root.contents()[0],
543 div = doc.root.contents()[1],
545 returned = div.before({text: 'text2'});
546 expect(doc.root.contents().length).to.equal(2);
547 expect(returned.sameNode(doc.root.contents()[0])).to.equal(true, 'modified node returned');
548 expect(textNode.getText()).to.equal('text1text2');
551 it('merges text nodes on after element node', function() {
552 var doc = getDocumentFromXML('<root><div></div>text1</root>'),
553 textNode = doc.root.contents()[1],
554 div = doc.root.contents()[0],
556 returned = div.after({text: 'text2'});
557 expect(doc.root.contents().length).to.equal(2);
558 expect(returned.sameNode(doc.root.contents()[1])).to.equal(true, 'modified node returned');
559 expect(textNode.getText()).to.equal('text2text1');
563 it('wraps root element node with another element node', function() {
564 var node = elementNodeFromXML('<div></div>'),
565 wrapper = elementNodeFromXML('<wrapper></wrapper>');
567 node.wrapWith(wrapper);
568 expect(node.parent().sameNode(wrapper)).to.be.true;
569 expect(node.document.root.sameNode(wrapper)).to.be.true;
572 it('wraps element node with another element node', function() {
573 var doc = getDocumentFromXML('<section><div></div></section>'),
574 div = doc.root.contents()[0];
576 var wrapper = div.wrapWith({tagName: 'wrapper'});
577 expect(wrapper.sameNode(doc.root.contents()[0])).to.equal(true, '1');
578 expect(div.parent().sameNode(wrapper)).to.equal(true, '2');
579 expect(wrapper.contents()[0].sameNode(div)).to.equal(true, '3');
582 it('wraps element outside of document tree', function() {
583 var doc = getDocumentFromXML('<section><div></div></section>'),
584 node = doc.createDocumentNode({tagName: 'node'});
586 node.wrapWith({tagName: 'wrapper'});
587 expect(node.parent().getTagName()).to.equal('wrapper');
588 expect(node.parent().contents()[0].sameNode(node)).to.be.true;
589 expect(doc.root.getTagName()).to.equal('section');
592 it('unwraps element node contents', function() {
593 var node = elementNodeFromXML('<div>Alice <div>has <span>propably</span> a cat</div>!</div>'),
594 outerDiv = node.contents()[1];
596 outerDiv.unwrapContent();
598 expect(node.contents().length).to.equal(3);
599 expect(node.contents()[0].getText()).to.equal('Alice has ');
600 expect(node.contents()[1].getTagName()).to.equal('span');
601 expect(node.contents()[2].getText()).to.equal(' a cat!');
604 it('removes parent-describing sibling nodes of unwrapped node', function() {
605 var doc = getDocumentFromXML('<root><div><a></a><x></x><a></a></div></root>'),
606 div = doc.root.contents()[0],
607 x = div.contents()[1];
609 doc.registerExtension({documentNode: {methods: {
611 describesParent: function() {
612 return this.getTagName() === 'x';
618 expect(doc.root.contents().length).to.equal(2);
619 expect(x.isInDocument()).to.be.false;
622 it('unwrap single element node from its parent', function() {
623 var doc = getDocumentFromXML('<div><a><b></b></a></div>'),
625 a = div.contents()[0],
628 var parent = b.unwrap();
630 expect(parent.sameNode(div)).to.equal(true, 'returns new parent');
631 expect(div.contents()).to.have.length(1, 'root contains only one node');
632 expect(div.contents()[0].sameNode(b)).to.equal(true, 'node got unwrapped');
635 it('unwrap single text node from its parent', function() {
636 var doc = getDocumentFromXML('<div>Some <span>text</span>!</div>'),
638 span = div.contents()[1],
639 text = span.contents()[0];
641 var parent = text.unwrap();
643 expect(parent.sameNode(div)).to.equal(true, 'returns new parent');
644 expect(div.contents()).to.have.length(1, 'root contains only one node');
645 expect(div.contents()[0].getText()).to.equal('Some text!');
648 describe('Wrapping text', function() {
649 it('wraps text spanning multiple sibling TextNodes', function() {
650 var section = elementNodeFromXML('<section>Alice has a <span>small</span> cat</section>'),
651 wrapper = section.wrapText({
652 _with: {tagName: 'span', attrs: {'attr1': 'value1'}},
658 expect(section.contents().length).to.equal(2);
659 expect(section.contents()[0].nodeType).to.equal(Node.TEXT_NODE);
660 expect(section.contents()[0].getText()).to.equal('Alice ');
662 var wrapper2 = section.contents()[1];
663 expect(wrapper2.sameNode(wrapper)).to.be.true;
664 expect(wrapper.getTagName()).to.equal('span');
666 var wrapperContents = wrapper.contents();
667 expect(wrapperContents.length).to.equal(3);
668 expect(wrapperContents[0].getText()).to.equal('has a ');
670 expect(wrapperContents[1].nodeType).to.equal(Node.ELEMENT_NODE);
671 expect(wrapperContents[1].contents().length).to.equal(1);
672 expect(wrapperContents[1].contents()[0].getText()).to.equal('small');
675 it('keeps parent-describing nodes in place', function() {
676 var doc = getDocumentFromXML('<root>Alice <x></x> probably <y></y> has a cat</root>'),
678 x = root.contents()[1],
679 y = root.contents()[3];
681 doc.registerExtension({documentNode: {methods: {
683 describesParent: function() {
685 return this.nodeType === Node.ELEMENT_NODE && this.getTagName() === 'x';
691 _with: {tagName: 'span', attrs: {'attr1': 'value1'}},
697 expect(x.parent().sameNode(root)).to.be.true;
698 expect(y.parent().getTagName()).to.equal('span');
702 describe('Wrapping Nodes', function() {
703 it('wraps multiple sibling nodes', function() {
704 var section = elementNodeFromXML('<section>Alice<div>has</div><div>a cat</div></section>'),
705 aliceText = section.contents()[0],
706 firstDiv = section.contents()[1],
707 lastDiv = section.contents()[section.contents().length -1];
709 var returned = section.document.wrapNodes({
712 _with: {tagName: 'header'}
715 var sectionContentss = section.contents(),
716 header = sectionContentss[0],
717 headerContents = header.contents();
719 expect(sectionContentss).to.have.length(1);
720 expect(header.sameNode(returned)).to.equal(true, 'wrapper returned');
721 expect(header.parent().sameNode(section)).to.be.true;
722 expect(headerContents).to.have.length(3);
723 expect(headerContents[0].sameNode(aliceText)).to.equal(true, 'first node wrapped');
724 expect(headerContents[1].sameNode(firstDiv)).to.equal(true, 'second node wrapped');
725 expect(headerContents[2].sameNode(lastDiv)).to.equal(true, 'third node wrapped');
728 it('wraps multiple sibling Elements - middle case', function() {
729 var section = elementNodeFromXML('<section><div></div><div></div><div></div><div></div></section>'),
730 div2 = section.contents()[1],
731 div3 = section.contents()[2];
733 section.document.wrapNodes({
736 _with: {tagName: 'header'}
739 var sectionContentss = section.contents(),
740 header = sectionContentss[1],
741 headerChildren = header.contents();
743 expect(sectionContentss).to.have.length(3);
744 expect(headerChildren).to.have.length(2);
745 expect(headerChildren[0].sameNode(div2)).to.equal(true, 'first node wrapped');
746 expect(headerChildren[1].sameNode(div3)).to.equal(true, 'second node wrapped');
749 it('keeps parent-describing nodes in place', function() {
750 var section = elementNodeFromXML('<section>Alice<x></x><div>a cat</div></section>'),
751 aliceText = section.contents()[0],
752 x = section.contents()[1],
753 lastDiv = section.contents()[2];
755 section.document.registerExtension({documentNode: {methods: {
757 describesParent: function() {
758 return this.nodeType === Node.ELEMENT_NODE && this.getTagName() === 'x';
763 section.document.wrapNodes({
766 _with: {tagName: 'header'}
769 expect(x.parent().sameNode(section)).to.be.true;
770 expect(aliceText.parent().getTagName()).to.equal('header');
771 expect(lastDiv.parent().getTagName()).to.equal('header');
777 var getTextNodes = function(text, doc) {
780 var search = function(node) {
781 node.contents().forEach(function(node) {
782 if(node.nodeType === Node.TEXT_NODE) {
783 if(node.getText() === text) {
795 var getTextNode = function(text, doc) {
796 var nodes = getTextNodes(text, doc),
798 if(nodes.length === 0) {
799 error = 'Text not found';
800 } else if(nodes.length > 1) {
801 error = 'Text not unique';
802 } else if(nodes[0].getText() !== text) {
803 error = 'I was trying to cheat your test :(';
806 throw new Error(error);
811 describe('Removing arbitrary text', function() {
812 it('removes within single text element', function() {
813 var doc = getDocumentFromXML('<div>Alice</div>'),
814 text = getTextNode('Alice', doc);
825 expect(doc.root.contents().length).to.equal(1);
826 expect(doc.root.contents()[0].getText()).to.equal('Ae');
828 it('removes across elements - 1', function() {
829 var doc = getDocumentFromXML('<div><a>aaa</a><b>bbb</b></div>');
833 node: getTextNode('aaa', doc),
837 node: getTextNode('bbb', doc),
842 var contents = doc.root.contents();
843 expect(contents.length).to.equal(2);
844 expect(contents[0].contents()[0].getText()).to.equal('aa');
845 expect(contents[1].contents()[0].getText()).to.equal('b');
847 it('removes across elements - 2', function() {
848 var doc = getDocumentFromXML('<a><b><c>ccc</c></b>xxx</a>');
851 node: getTextNode('ccc', doc),
855 node: getTextNode('xxx', doc),
860 var contents = doc.root.contents();
861 expect(contents.length).to.equal(2);
862 expect(contents[0].getTagName()).to.equal('b');
863 expect(contents[1].getText()).to.equal('x');
865 var bContents = contents[0].contents();
866 expect(bContents.length).to.equal(1);
867 expect(bContents[0].getTagName()).to.equal('c');
868 expect(bContents[0].contents().length).to.equal(1);
869 expect(bContents[0].contents()[0].getText()).to.equal('cc');
871 it('remove across elements - 3 (merged text nodes)', function() {
872 var doc = getDocumentFromXML('<div>Alice <span>has</span> a cat</div>');
875 node: getTextNode('Alice ', doc),
879 node: getTextNode(' a cat', doc),
883 var contents = doc.root.contents();
884 expect(contents.length).to.equal(1);
885 expect(contents[0].getText()).to.equal('Acat');
887 it('remove across elements - 4', function() {
888 var doc = getDocumentFromXML('<div>Alice <div>has <span>a</span> cat</div></div>');
891 node: getTextNode('Alice ', doc),
895 node: getTextNode(' cat', doc),
899 var contents = doc.root.contents();
900 expect(contents.length).to.equal(2);
901 expect(contents[0].getText()).to.equal('A');
902 expect(contents[1].getTagName()).to.equal('div');
903 expect(contents[1].contents().length).to.equal(1);
904 expect(contents[1].contents()[0].getText()).to.equal('cat');
906 it('removes across elements - 5 (whole document)', function() {
907 var doc = getDocumentFromXML('<div>Alice <div>has <span>a</span> cat</div>!!!</div>');
910 node: getTextNode('Alice ', doc),
914 node: getTextNode('!!!', doc),
919 expect(doc.root.getTagName()).to.equal('div');
920 expect(doc.root.contents().length).to.equal(1);
921 expect(doc.root.contents()[0].getText()).to.equal('');
923 it('removes nodes in between', function() {
924 var doc = getDocumentFromXML('<div><a>aaa<x>!</x></a>xxx<x></x><b><x>!</x>bbb</b></div>');
927 node: getTextNode('aaa', doc),
931 node: getTextNode('bbb', doc),
936 var contents = doc.root.contents();
937 expect(contents.length).to.equal(2, 'two nodes survived');
938 expect(contents[0].getTagName()).to.equal('a');
939 expect(contents[1].getTagName()).to.equal('b');
940 expect(contents[0].contents().length).to.equal(1);
941 expect(contents[0].contents()[0].getText()).to.equal('aa');
942 expect(contents[1].contents().length).to.equal(1);
943 expect(contents[1].contents()[0].getText()).to.equal('b');
947 describe('Splitting text', function() {
949 it('splits TextNode\'s parent into two ElementNodes', function() {
950 var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
952 text = section.contents()[0].contents()[0];
954 var returnedValue = text.split({offset: 5});
955 expect(section.contents().length).to.equal(2, 'section has two children');
957 var header1 = section.contents()[0];
958 var header2 = section.contents()[1];
960 expect(header1.getTagName()).to.equal('header', 'first section child ok');
961 expect(header1.contents().length).to.equal(1, 'first header has one child');
962 expect(header1.contents()[0].getText()).to.equal('Some ', 'first header has correct content');
963 expect(header2.getTagName()).to.equal('header', 'second section child ok');
964 expect(header2.contents().length).to.equal(1, 'second header has one child');
965 expect(header2.contents()[0].getText()).to.equal('header', 'second header has correct content');
967 expect(returnedValue.first.sameNode(header1)).to.equal(true, 'first node returned');
968 expect(returnedValue.second.sameNode(header2)).to.equal(true, 'second node returned');
971 it('leaves empty copy of ElementNode if splitting at the very beginning', function() {
972 var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
974 text = section.contents()[0].contents()[0];
976 text.split({offset: 0});
978 var header1 = section.contents()[0];
979 var header2 = section.contents()[1];
981 expect(header1.contents().length).to.equal(0);
982 expect(header2.contents()[0].getText()).to.equal('Some header');
985 it('leaves empty copy of ElementNode if splitting at the very end', function() {
986 var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
988 text = section.contents()[0].contents()[0];
990 text.split({offset: 11});
992 var header1 = section.contents()[0];
993 var header2 = section.contents()[1];
995 expect(header1.contents()[0].getText()).to.equal('Some header');
996 expect(header2.contents().length).to.equal(0);
999 it('keeps TextNodes\'s parent\'s children elements intact', function() {
1000 var doc = getDocumentFromXML('<section><header>A <span>fancy</span> and <span>nice</span> header</header></section>'),
1002 header = section.contents()[0],
1003 textAnd = header.contents()[2];
1005 textAnd.split({offset: 2});
1007 var sectionContents = section.contents();
1008 expect(sectionContents.length).to.equal(2, 'Section has two children');
1009 expect(sectionContents[0].getTagName()).to.equal('header', 'First section node is a header');
1010 expect(sectionContents[1].getTagName()).to.equal('header', 'Second section node is a header');
1012 var firstHeaderContents = sectionContents[0].contents();
1013 expect(firstHeaderContents.length).to.equal(3, 'First header has three children');
1014 expect(firstHeaderContents[0].getText()).to.equal('A ', 'First header starts with a text');
1015 expect(firstHeaderContents[1].getTagName()).to.equal('span', 'First header has span in the middle');
1016 expect(firstHeaderContents[2].getText()).to.equal(' a', 'First header ends with text');
1018 var secondHeaderContents = sectionContents[1].contents();
1019 expect(secondHeaderContents.length).to.equal(3, 'Second header has three children');
1020 expect(secondHeaderContents[0].getText()).to.equal('nd ', 'Second header starts with text');
1021 expect(secondHeaderContents[1].getTagName()).to.equal('span', 'Second header has span in the middle');
1022 expect(secondHeaderContents[2].getText()).to.equal(' header', 'Second header ends with text');
1026 describe('Events', function() {
1027 it('emits nodeDetached event on node detach', function() {
1028 var node = elementNodeFromXML('<div><div></div></div>'),
1029 innerNode = node.contents()[0],
1031 node.document.on('change', spy);
1033 var detached = innerNode.detach(),
1034 event = spy.args[0][0];
1036 expect(event.type).to.equal('nodeDetached');
1037 expect(event.meta.node.sameNode(detached, 'detached node in event meta'));
1038 expect(event.meta.parent.sameNode(node), 'original parent node in event meta');
1041 it('emits nodeAdded event when appending new node', function() {
1042 var node = elementNodeFromXML('<div></div>'),
1044 node.document.on('change', spy);
1046 var appended = node.append({tagName:'div'}),
1047 event = spy.args[0][0];
1048 expect(event.type).to.equal('nodeAdded');
1049 expect(event.meta.node.sameNode(appended)).to.be.true;
1052 it('emits nodeDetached/nodeAdded events with `move` flag when appending aready existing node', function() {
1053 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
1054 a = node.contents()[0],
1055 b = node.contents()[1],
1057 node.document.on('change', spy);
1059 var appended = a.append(b),
1060 detachedEvent = spy.args[0][0],
1061 addedEvent = spy.args[1][0];
1063 expect(spy.callCount).to.equal(2);
1064 expect(detachedEvent.type).to.equal('nodeDetached');
1065 expect(detachedEvent.meta.node.sameNode(appended)).to.be.true;
1066 expect(detachedEvent.meta.move).to.equal(true, 'move flag set to true for nodeDetachedEvent');
1067 expect(addedEvent.type).to.equal('nodeAdded');
1068 expect(addedEvent.meta.node.sameNode(appended)).to.be.true;
1069 expect(addedEvent.meta.move).to.equal(true, 'move flag set to true for nodeAddedEvent');
1073 it('emits nodeAdded event when prepending new node', function() {
1074 var node = elementNodeFromXML('<div></div>'),
1076 node.document.on('change', spy);
1078 var prepended = node.prepend({tagName:'div'}),
1079 event = spy.args[0][0];
1080 expect(event.type).to.equal('nodeAdded');
1081 expect(event.meta.node.sameNode(prepended)).to.be.true;
1084 it('emits nodeDetached/nodeAdded events with `move` flag when prepending aready existing node', function() {
1085 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
1086 a = node.contents()[0],
1087 b = node.contents()[1],
1089 node.document.on('change', spy);
1091 var prepended = a.prepend(b),
1092 detachedEvent = spy.args[0][0],
1093 addedEvent = spy.args[1][0];
1095 expect(spy.callCount).to.equal(2);
1096 expect(detachedEvent.type).to.equal('nodeDetached');
1097 expect(detachedEvent.meta.node.sameNode(prepended)).to.be.true;
1098 expect(detachedEvent.meta.move).to.equal(true, 'move flag set to true for nodeDetachedEvent');
1099 expect(addedEvent.type).to.equal('nodeAdded');
1100 expect(addedEvent.meta.node.sameNode(prepended)).to.be.true;
1101 expect(addedEvent.meta.move).to.equal(true, 'move flag set to true for nodeAddedEvent');
1104 it('emits nodeAdded event when inserting node after another', function() {
1105 var node = elementNodeFromXML('<div><a></a></div>').contents()[0],
1107 node.document.on('change', spy);
1109 var inserted = node.after({tagName:'div'}),
1110 event = spy.args[0][0];
1111 expect(event.type).to.equal('nodeAdded');
1112 expect(event.meta.node.sameNode(inserted)).to.be.true;
1115 it('emits nodeDetached/nodeAdded events with `move` flag when inserting aready existing node after another', function() {
1116 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
1117 a = node.contents()[0],
1118 b = node.contents()[1],
1120 node.document.on('change', spy);
1121 var inserted = b.after(a),
1122 detachedEvent = spy.args[0][0],
1123 addedEvent = spy.args[1][0];
1125 expect(spy.callCount).to.equal(2);
1126 expect(detachedEvent.type).to.equal('nodeDetached');
1127 expect(detachedEvent.meta.node.sameNode(inserted)).to.be.true;
1128 expect(detachedEvent.meta.move).to.equal(true, 'move flag set to true for nodeDetachedEvent');
1129 expect(addedEvent.type).to.equal('nodeAdded');
1130 expect(addedEvent.meta.node.sameNode(inserted)).to.be.true;
1131 expect(addedEvent.meta.move).to.equal(true, 'move flag set to true for nodeAddedEvent');
1134 it('emits nodeAdded event when inserting node before another', function() {
1135 var node = elementNodeFromXML('<div><a></a></div>').contents()[0],
1137 node.document.on('change', spy);
1139 var inserted = node.before({tagName:'div'}),
1140 event = spy.args[0][0];
1141 expect(event.type).to.equal('nodeAdded');
1142 expect(event.meta.node.sameNode(inserted)).to.be.true;
1145 it('emits nodeDetached/nodeAdded events with `move` flag when inserting aready existing node before another', function() {
1146 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
1147 a = node.contents()[0],
1148 b = node.contents()[1],
1150 node.document.on('change', spy);
1151 var inserted = a.before(b),
1152 detachedEvent = spy.args[0][0],
1153 addedEvent = spy.args[1][0];
1155 expect(spy.callCount).to.equal(2);
1156 expect(detachedEvent.type).to.equal('nodeDetached');
1157 expect(detachedEvent.meta.node.sameNode(inserted)).to.be.true;
1158 expect(detachedEvent.meta.move).to.equal(true, 'move flag set to true for nodeDetachedEvent');
1159 expect(addedEvent.type).to.equal('nodeAdded');
1160 expect(addedEvent.meta.node.sameNode(inserted)).to.be.true;
1161 expect(addedEvent.meta.move).to.equal(true, 'move flag set to true for nodeAddedEvent');
1164 it('emits nodeDetached and nodeAdded when replacing root node with another', function() {
1165 var doc = getDocumentFromXML('<a></a>'),
1169 doc.on('change', spy);
1171 doc.root.replaceWith({tagName: 'b'});
1173 expect(spy.callCount).to.equal(2);
1175 var event1 = spy.args[0][0],
1176 event2 = spy.args[1][0];
1178 expect(event1.type).to.equal('nodeDetached');
1179 expect(event1.meta.node.sameNode(oldRoot)).to.equal(true, 'root node in nodeDetached event metadata');
1180 expect(event2.type).to.equal('nodeAdded');
1181 expect(event2.meta.node.sameNode(doc.root)).to.equal(true, 'new root node in nodelAdded event meta');
1185 ['append', 'prepend', 'before', 'after'].forEach(function(insertionMethod) {
1186 it('emits nodeDetached for node moved from a document tree to out of document node ' + insertionMethod, function() {
1187 var doc = getDocumentFromXML('<div><a></a></div>'),
1188 a = doc.root.contents()[0],
1191 doc.on('change', spy);
1193 var newNode = doc.createDocumentNode({tagName: 'b'}),
1194 newNodeInner = newNode.append({tagName:'c'});
1196 newNodeInner[insertionMethod](a);
1198 var event = spy.args[0][0];
1199 expect(event.type).to.equal('nodeDetached');
1200 expect(event.meta.node.sameNode(a));
1203 it('doesn\'t emit nodeDetached event for already out of document node moved to out of document node' + insertionMethod, function() {
1204 var doc = getDocumentFromXML('<div><a></a></div>'),
1207 doc.on('change', spy);
1209 var newNode = doc.createDocumentNode({tagName: 'b'});
1210 newNode.append({tagName:'c'});
1212 expect(spy.callCount).to.equal(0);
1219 describe('Traversing', function() {
1220 describe('Basic', function() {
1221 it('can access node parent', function() {
1222 var doc = getDocumentFromXML('<a><b></b></a>'),
1224 b = a.contents()[0];
1226 expect(a.parent()).to.equal(null, 'parent of a root is null');
1227 expect(b.parent().sameNode(a)).to.be.true;
1229 it('can access node parents', function() {
1230 var doc = getDocumentFromXML('<a><b><c></c></b></a>'),
1232 b = a.contents()[0],
1233 c = b.contents()[0];
1235 var parents = c.parents();
1237 expect(parents[0].sameNode(b)).to.be.true;
1238 expect(parents[1].sameNode(a)).to.be.true;
1242 describe('finding sibling parents of two elements', function() {
1243 it('returns elements themself if they have direct common parent', function() {
1244 var doc = getDocumentFromXML('<section><div><div>A</div><div>B</div></div></section>'),
1245 wrappingDiv = doc.root.contents()[0],
1246 divA = wrappingDiv.contents()[0],
1247 divB = wrappingDiv.contents()[1];
1249 var siblingParents = doc.getSiblingParents({node1: divA, node2: divB});
1251 expect(siblingParents.node1.sameNode(divA)).to.equal(true, 'divA');
1252 expect(siblingParents.node2.sameNode(divB)).to.equal(true, 'divB');
1255 it('returns sibling parents - example 1', function() {
1256 var doc = getDocumentFromXML('<section>Alice <span>has a cat</span></section>'),
1257 aliceText = doc.root.contents()[0],
1258 span = doc.root.contents()[1],
1259 spanText = span.contents()[0];
1261 var siblingParents = doc.getSiblingParents({node1: aliceText, node2: spanText});
1263 expect(siblingParents.node1.sameNode(aliceText)).to.equal(true, 'aliceText');
1264 expect(siblingParents.node2.sameNode(span)).to.equal(true, 'span');
1267 it('returns node itself for two same nodes', function() {
1268 var doc = getDocumentFromXML('<section><div></div></section>'),
1269 div = doc.root.contents()[0];
1271 var siblingParents = doc.getSiblingParents({node1: div, node2: div});
1272 expect(!!siblingParents.node1 && !!siblingParents.node2).to.equal(true, 'nodes defined');
1273 expect(siblingParents.node1.sameNode(div)).to.be.equal(true, 'node1');
1274 expect(siblingParents.node2.sameNode(div)).to.be.equal(true, 'node2');
1279 describe('Serializing document to WLXML', function() {
1280 it('keeps document intact when no changes have been made', function() {
1281 var xmlIn = '<section>Alice<div>has</div>a <span class="uri" meta-uri="http://cat.com">cat</span>!</section>',
1282 doc = getDocumentFromXML(xmlIn),
1283 xmlOut = doc.toXML();
1285 var parser = new DOMParser(),
1286 input = parser.parseFromString(xmlIn, 'application/xml').childNodes[0],
1287 output = parser.parseFromString(xmlOut, 'application/xml').childNodes[0];
1289 expect(input.isEqualNode(output)).to.be.true;
1292 it('keeps entities intact', function() {
1293 var xmlIn = '<section>< ></section>',
1294 doc = getDocumentFromXML(xmlIn),
1295 xmlOut = doc.toXML();
1296 expect(xmlOut).to.equal(xmlIn);
1298 it('keeps entities intact when they form html/xml', function() {
1299 var xmlIn = '<section><abc></section>',
1300 doc = getDocumentFromXML(xmlIn),
1301 xmlOut = doc.toXML();
1302 expect(xmlOut).to.equal(xmlIn);
1306 describe('Extension API', function() {
1307 var doc, extension, elementNode, textNode;
1309 beforeEach(function() {
1310 doc = getDocumentFromXML('<section>Alice<div class="test_class"></div></section>');
1311 elementNode = doc.root;
1312 textNode = doc.root.contents()[0];
1315 expect(elementNode.testTransformation).to.be.undefined;
1316 expect(textNode.testTransformation).to.be.undefined;
1317 expect(doc.testTransformation).to.be.undefined;
1319 expect(doc.testMethod).to.be.undefined;
1320 expect(elementNode.testMethod).to.be.undefined;
1321 expect(textNode.testMethod).to.be.undefined;
1322 expect(elementNode.elementTestMethod).to.be.undefined;
1323 expect(textNode.textTestMethod).to.be.undefined;
1326 it('allows adding method to a document', function() {
1327 extension = {document: {methods: {
1328 testMethod: function() { return this; }
1331 doc.registerExtension(extension);
1332 expect(doc.testMethod()).to.equal(doc, 'context is set to a document instance');
1335 it('allows adding transformation to a document', function() {
1336 extension = {document: {transformations: {
1337 testTransformation: function() { return this; },
1338 testTransformation2: {impl: function() { return this;}}
1341 doc.registerExtension(extension);
1342 expect(doc.testTransformation()).to.equal(doc, 'context is set to a document instance');
1343 expect(doc.testTransformation2()).to.equal(doc, 'context is set to a document instance');
1346 it('allows adding method to a DocumentNode instance', function() {
1350 testMethod: function() { return this; }
1355 textTestMethod: function() { return this; }
1360 elementTestMethod: function() { return this; }
1365 doc.registerExtension(extension);
1368 elementNode = doc.root;
1369 textNode = doc.root.contents()[0];
1371 expect(elementNode.testMethod().sameNode(elementNode)).to.equal(true, 'context is set to a node instance');
1372 expect(textNode.testMethod().sameNode(textNode)).to.equal(true, 'context is set to a node instance');
1374 expect(elementNode.elementTestMethod().sameNode(elementNode)).to.be.true;
1375 expect(elementNode.textTestMethod).to.be.undefined;
1377 expect(textNode.textTestMethod().sameNode(textNode)).to.be.true;
1378 expect(textNode.elementTestMethod).to.be.undefined;
1381 it('allows adding transformation to a DocumentNode', function() {
1385 testTransformation: function() { return this; },
1386 testTransformation2: {impl: function() { return this;}}
1391 textTestTransformation: function() { return this; }
1396 elementTestTransformation: function() { return this; }
1401 doc.registerExtension(extension);
1404 elementNode = doc.root;
1405 textNode = doc.root.contents()[0];
1407 expect(elementNode.testTransformation().sameNode(elementNode)).to.equal(true, '1');
1408 expect(elementNode.testTransformation2().sameNode(elementNode)).to.equal(true, '2');
1409 expect(textNode.testTransformation().sameNode(textNode)).to.equal(true, '3');
1410 expect(textNode.testTransformation2().sameNode(textNode)).to.equal(true, '4');
1412 expect(elementNode.elementTestTransformation().sameNode(elementNode)).to.be.true;
1413 expect(elementNode.textTestTransformation).to.be.undefined;
1415 expect(textNode.textTestTransformation().sameNode(textNode)).to.be.true;
1416 expect(textNode.elementTestTransfomation).to.be.undefined;
1419 it('allows text/element node methods and transformations to access node and transormations on document node', function() {
1421 var doc = getDocumentFromXML('<div>text</div>');
1423 doc.registerExtension({
1432 return 'super_trans';
1439 return 'element_sub_' + this.__super__.test();
1444 return 'element_trans_sub_' + this.__super__.testT();
1451 return 'text_sub_' + this.__super__.test();
1456 return 'text_trans_sub_' + this.__super__.testT();
1462 var textNode = doc.root.contents()[0];
1464 expect(doc.root.test()).to.equal('element_sub_super');
1465 expect(textNode.test()).to.equal('text_sub_super');
1466 expect(doc.root.testT()).to.equal('element_trans_sub_super_trans');
1467 expect(textNode.testT()).to.equal('text_trans_sub_super_trans');
1471 describe('Undo/redo', function() {
1473 it('smoke tests', function() {
1474 var doc = getDocumentFromXML('<div>Alice</div>'),
1475 textNode = doc.root.contents()[0];
1477 expect(doc.undoStack).to.have.length(0);
1479 textNode.wrapWith({tagName: 'span', start:1, end:2});
1480 expect(doc.undoStack).to.have.length(1, '1');
1481 expect(doc.toXML()).to.equal('<div>A<span>l</span>ice</div>');
1484 expect(doc.undoStack).to.have.length(0, '2');
1485 expect(doc.toXML()).to.equal('<div>Alice</div>');
1488 expect(doc.undoStack).to.have.length(1, '3');
1489 expect(doc.toXML()).to.equal('<div>A<span>l</span>ice</div>');
1492 expect(doc.undoStack).to.have.length(0, '4');
1493 expect(doc.toXML()).to.equal('<div>Alice</div>');
1496 expect(doc.undoStack).to.have.length(0, '5');
1497 expect(doc.toXML()).to.equal('<div>Alice</div>');
1500 it('smoke tests 2', function() {
1501 var doc = getDocumentFromXML('<div>Alice</div>'),
1502 textNode = doc.root.contents()[0],
1503 path = textNode.getPath();
1505 textNode.setText('Alice ');
1506 textNode.setText('Alice h');
1507 textNode.setText('Alice ha');
1508 textNode.setText('Alice has');
1510 expect(textNode.getText()).to.equal('Alice has');
1513 expect(doc.root.contents()[0].getText()).to.equal('Alice ha', '1');
1516 expect(doc.root.contents()[0].getText()).to.equal('Alice h', '2');
1519 expect(doc.root.contents()[0].getText()).to.equal('Alice ha', '3');
1522 expect(doc.root.contents()[0].getText()).to.equal('Alice has', '4');
1526 textNode = doc.getNodeByPath(path);
1527 textNode.setText('Cat');
1529 textNode = doc.getNodeByPath(path);
1530 expect(textNode.getText()).to.equal('Alice h');
1534 var sampleMethod = function(val) {
1535 this._$.attr('x', val);
1536 this.triggerChangeEvent();
1539 var transformations = {
1540 'unaware': sampleMethod,
1541 'returning change root': {
1543 getChangeRoot: function() {
1544 return this.context;
1547 'implementing undo operation': {
1548 impl: function(t, val) {
1549 t.oldVal = this.getAttr('x');
1550 sampleMethod.call(this, val);
1553 this.setAttr('x', t.oldVal);
1558 _.pairs(transformations).forEach(function(pair) {
1560 transformaton = pair[1];
1562 describe(name + ' transformation: ', function() {
1563 var doc, node, nodePath;
1565 beforeEach(function() {
1566 doc = getDocumentFromXML('<div><test x="old"></test></div>');
1568 doc.registerExtension({elementNode: {transformations: {
1572 node = doc.root.contents()[0];
1573 nodePath = node.getPath();
1576 it('transforms as expected', function() {
1578 expect(node.getAttr('x')).to.equal('new');
1581 it('can be undone', function() {
1584 node = doc.getNodeByPath(nodePath);
1585 expect(node.getAttr('x')).to.equal('old');
1588 it('can be undone and then redone', function() {
1592 node = doc.getNodeByPath(nodePath);
1593 expect(node.getAttr('x')).to.equal('new');
1596 it('handles a sample scenario', function() {
1597 doc.root.contents()[0].test('1');
1598 doc.root.contents()[0].test('2');
1599 doc.root.contents()[0].test('3');
1600 doc.root.contents()[0].test('4');
1601 doc.root.contents()[0].test('5');
1603 expect(doc.root.contents()[0].getAttr('x')).to.equal('5', 'after initial transformations');
1605 expect(doc.root.contents()[0].getAttr('x')).to.equal('4', 'undo 1.1');
1607 expect(doc.root.contents()[0].getAttr('x')).to.equal('3', 'undo 1.2');
1609 expect(doc.root.contents()[0].getAttr('x')).to.equal('4', 'redo 1.1');
1611 expect(doc.root.contents()[0].getAttr('x')).to.equal('5', 'redo 1.2');
1613 expect(doc.root.contents()[0].getAttr('x')).to.equal('4', 'undo 2.1');
1614 doc.root.contents()[0].test('10');
1615 expect(doc.root.contents()[0].getAttr('x')).to.equal('10', 'additional transformation');
1616 expect(doc.redoStack.length).to.equal(0, 'transformation cleared redo stack');
1618 expect(doc.root.contents()[0].getAttr('x')).to.equal('10', 'empty redoStack so redo was noop');
1620 expect(doc.root.contents()[0].getAttr('x')).to.equal('4', 'undoing additional transformation');
1622 expect(doc.root.contents()[0].getAttr('x')).to.equal('10', 'redoing additional transformation');
1627 it('smoke tests nested transformations', function() {
1628 var doc = getDocumentFromXML('<div></div>');
1630 doc.registerExtension({elementNode: {transformations: {
1631 nested: function(v) {
1632 this._$.attr('innerAttr', v);
1633 this.triggerChangeEvent();
1635 outer: function(v) {
1637 this._$.attr('outerAttr', v);
1638 this.triggerChangeEvent();
1642 doc.root.outer('test1');
1643 doc.root.outer('test2');
1645 expect(doc.root.getAttr('innerAttr')).to.equal('test2');
1646 expect(doc.root.getAttr('outerAttr')).to.equal('test2');
1650 expect(doc.root.getAttr('innerAttr')).to.equal('test1');
1651 expect(doc.root.getAttr('outerAttr')).to.equal('test1');
1655 expect(doc.root.getAttr('innerAttr')).to.equal(undefined);
1656 expect(doc.root.getAttr('outerAttr')).to.equal(undefined);
1660 expect(doc.root.getAttr('innerAttr')).to.equal('test1');
1661 expect(doc.root.getAttr('outerAttr')).to.equal('test1');
1665 expect(doc.root.getAttr('innerAttr')).to.equal('test2');
1666 expect(doc.root.getAttr('outerAttr')).to.equal('test2');
1670 it('ignores transformation if document didn\'t emit change event', function() {
1671 var doc = getDocumentFromXML('<div></div>');
1673 doc.registerExtension({elementNode: {transformations: {
1680 expect(doc.undoStack.length).to.equal(0);
1684 describe('Transactions', function() {
1685 it('allows to undo/redo series of transformations at once', function() {
1686 var doc = getDocumentFromXML('<div></div>');
1688 doc.registerExtension({
1689 elementNode: {transformations: {
1691 this.setAttr('test', v);
1696 doc.startTransaction();
1700 doc.endTransaction();
1703 expect(doc.root.getAttr('test'), '1');
1705 expect(doc.root.getAttr('test'), '3');
1707 expect(doc.root.getAttr('test'), '1');
1709 expect(doc.root.getAttr('test'), '3');
1712 it('ignores empty transactions', function() {
1713 var doc = getDocumentFromXML('<div></div>');
1714 doc.startTransaction();
1715 doc.endTransaction();
1716 expect(doc.undoStack).to.have.length(0, 'empty transaction doesn\'t get pushed into undo stack');
1719 it('doesn\'t break on optimizations', function() {
1720 // This is a smoke test checking if optimizations made to transaction undoing
1721 // doesnt't break anything.
1722 var doc = getDocumentFromXML('<div smart="1" unaware="1"></div>');
1724 doc.registerExtension({
1725 elementNode: {transformations: {
1726 unaware: function(v) {
1727 this.setAttr('unware', v);
1728 this.triggerChangeEvent();
1731 impl: function(t, v) {
1732 t.oldVal = this.getAttr('smart');
1733 this.setAttr('smart', v);
1734 this.triggerChangeEvent();
1737 this.setAttr('smart', t.oldVal);
1738 this.triggerChangeEvent();
1744 doc.startTransaction();
1745 doc.root.smart('2');
1746 doc.root.unaware('2');
1747 doc.root.smart('3');
1748 doc.root.unaware('3');
1749 doc.endTransaction();
1753 expect(doc.root.getAttr('smart')).to.equal('1');
1754 expect(doc.root.getAttr('unaware')).to.equal('1');
1757 it('can have associated metadata', function() {
1758 var doc = getDocumentFromXML('<div></div>'),
1759 metadata = Object.create({});
1761 doc.registerExtension({document: {transformations: {
1763 this.trigger('change');
1767 doc.startTransaction(metadata);
1769 doc.endTransaction();
1771 var transaction = doc.undoStack[0];
1772 expect(transaction.metadata).to.equal(metadata);
1775 it('can be rolled back', function() {
1776 var doc = getDocumentFromXML('<root></root>');
1778 doc.startTransaction();
1779 doc.root.append({tagName: 'div'});
1780 doc.rollbackTransaction();
1782 expect(doc.undoStack.length).to.equal(0, 'nothing to undo');
1783 expect(doc.root.contents().length).to.equal(0);
1786 it('rollbacks and calls error handleor if error gets thrown', function() {
1787 var doc = getDocumentFromXML('<root></root>'),
1791 doc.transaction(function() {
1792 doc.root.append({tagName: 'div'});
1796 expect(spy.args[0][0]).to.equal(err);
1797 expect(doc.root.contents().length).to.equal(0);
1798 expect(doc.undoStack.length).to.equal(0);
1802 describe('Regression tests', function() {
1803 it('redos correctly after running its own undo followed by unaware transformation undo', function() {
1804 var doc = getDocumentFromXML('<section t="0"></section>');
1806 doc.registerExtension({elementNode: {transformations: {
1807 unaware: function() {
1808 this.triggerChangeEvent();
1812 this._$.attr('t', 1);
1813 this.triggerChangeEvent();
1816 this._$.attr('t', 0);
1826 expect(doc.root.getAttr('t')).to.equal('1');
1828 it('can perform undo of an operation performed after automatic transaction rollback', function() {
1829 var doc = getDocumentFromXML('<section></section>'),
1830 extension = {document: {transformations: {
1831 throwingTransformation: function() { throw new Error(); }
1834 doc.registerExtension(extension);
1836 doc.throwingTransformation();
1838 doc.transaction(function() {
1839 doc.root.setAttr('x', '2');
1842 expect(doc.undoStack.length).to.equal(1);
1843 expect(doc.root.getAttr('x')).to.equal('2');
1847 expect(doc.undoStack.length).to.equal(0);
1848 expect(doc.root.getAttr('x')).to.be.undefined;