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('knows its path in the document tree', function() {
90 var doc = getDocumentFromXML('<root><a><b><c></c>text</b></a></root>'),
92 a = root.contents()[0],
94 text = b.contents()[1];
96 expect(root.getPath()).to.eql([], 'path of the root element is empty');
97 expect(a.getPath()).to.eql([0]);
98 expect(b.getPath()).to.eql([0, 0]);
99 expect(text.getPath()).to.eql([0,0,1]);
101 /* Paths relative to a given ancestor */
102 expect(text.getPath(root)).to.eql([0,0,1]);
103 expect(text.getPath(a)).to.eql([0,1]);
104 expect(text.getPath(b)).to.eql([1]);
108 describe('Basic ElementNode properties', function() {
109 it('exposes node contents', function() {
110 var node = elementNodeFromXML('<node>Some<node>text</node>is here</node>'),
111 contents = node.contents();
113 expect(contents).to.have.length(3);
114 expect(contents[0].nodeType).to.equal(Node.TEXT_NODE, 'text node 1');
115 expect(contents[1].nodeType).to.equal(Node.ELEMENT_NODE, 'element node 1');
116 expect(contents[2].nodeType).to.equal(Node.TEXT_NODE, 'text node 2');
119 describe('Storing custom data', function() {
122 beforeEach(function() {
123 node = elementNodeFromXML('<div></div>');
126 it('can append single value', function() {
127 node.setData('key', 'value');
128 expect(node.getData('key')).to.equal('value');
131 it('can overwrite the whole data', function() {
132 node.setData('key1', 'value1');
133 node.setData({key2: 'value2'});
134 expect(node.getData('key2')).to.equal('value2');
137 it('can fetch the whole data at once', function() {
138 node.setData({key1: 'value1', key2: 'value2'});
139 expect(node.getData()).to.eql({key1: 'value1', key2: 'value2'});
143 describe('Changing node tag', function() {
145 it('can change tag name', function() {
146 var node = elementNodeFromXML('<div></div>');
148 expect(node.getTagName()).to.equal('span');
151 it('emits nodeTagChange event', function() {
152 var node = elementNodeFromXML('<div></div>'),
155 node.document.on('change', spy);
157 var event = spy.args[0][0];
159 expect(event.type).to.equal('nodeTagChange');
160 expect(event.meta.node.sameNode(node)).to.be.true;
161 expect(event.meta.oldTagName).to.equal('div');
164 describe('Implementation specific expectations', function() {
165 // DOM specifies ElementNode tag as a read-only property, so
166 // changing it in a seamless way is a little bit tricky. For this reason
167 // the folowing expectations are required, despite the fact that they actually are
168 // motivated by implemetation details.
170 it('keeps node in the document', function() {
171 var doc = getDocumentFromXML('<div><header></header></div>'),
172 header = doc.root.contents()[0];
173 header.setTag('span');
174 expect(header.parent().sameNode(doc.root)).to.be.true;
176 it('keeps custom data', function() {
177 var node = elementNodeFromXML('<div></div>');
179 node.setData('key', 'value');
180 node.setTag('header');
182 expect(node.getTagName()).to.equal('header');
183 expect(node.getData()).to.eql({key: 'value'});
186 it('can change document root tag name', function() {
187 var doc = getDocumentFromXML('<div></div>');
188 doc.root.setTag('span');
189 expect(doc.root.getTagName()).to.equal('span');
192 it('keeps contents', function() {
193 var node = elementNodeFromXML('<div><div></div></div>');
194 node.setTag('header');
195 expect(node.contents()).to.have.length(1);
199 describe('Setting node attributes', function() {
200 it('can set node attribute', function() {
201 var node = elementNodeFromXML('<div></div>');
203 node.setAttr('key', 'value');
204 expect(node.getAttr('key')).to.equal('value');
206 it('emits nodeAttrChange event', function() {
207 var node = elementNodeFromXML('<div key="value1"></div>'),
210 node.document.on('change', spy);
211 node.setAttr('key', 'value2');
212 var event = spy.args[0][0];
214 expect(event.type).to.equal('nodeAttrChange');
215 expect(event.meta.node.sameNode(node)).to.be.true;
216 expect(event.meta.attr).to.equal('key');
217 expect(event.meta.oldVal).to.equal('value1');
224 describe('Basic TextNode properties', function() {
225 it('can have its text set', function() {
226 var node = elementNodeFromXML('<div>Alice</div>'),
227 textNode = node.contents()[0];
229 textNode.setText('Cat');
230 expect(textNode.getText()).to.equal('Cat');
233 it('emits nodeTextChange', function() {
234 var node = elementNodeFromXML('<div>Alice</div>'),
235 textNode = node.contents()[0],
238 textNode.document.on('change', spy);
239 textNode.setText('Cat');
241 var event = spy.args[0][0];
242 expect(event.type).to.equal('nodeTextChange');
245 it('puts NodeElement after itself', function() {
246 var node = elementNodeFromXML('<div>Alice</div>'),
247 textNode = node.contents()[0],
248 returned = textNode.after({tagName:'div'});
249 expect(returned.sameNode(node.contents()[1])).to.be.true;
252 it('puts NodeElement before itself', function() {
253 var node = elementNodeFromXML('<div>Alice</div>'),
254 textNode = node.contents()[0],
255 returned = textNode.before({tagName:'div'});
256 expect(returned.sameNode(node.contents()[0])).to.be.true;
259 describe('Wrapping TextNode contents', function() {
261 it('wraps DocumentTextElement', function() {
262 var node = elementNodeFromXML('<section>Alice</section>'),
263 textNode = node.contents()[0];
265 var returned = textNode.wrapWith({tagName: 'header'}),
266 parent = textNode.parent(),
267 parent2 = node.contents()[0];
269 expect(returned.sameNode(parent)).to.be.equal(true, 'wrapper is a parent');
270 expect(returned.sameNode(parent2)).to.be.equal(true, 'wrapper has a correct parent');
271 expect(returned.getTagName()).to.equal('header');
274 describe('wrapping part of DocumentTextElement', function() {
275 [{start: 5, end: 12}, {start: 12, end: 5}].forEach(function(offsets) {
276 it('wraps in the middle ' + offsets.start + '/' + offsets.end, function() {
277 var node = elementNodeFromXML('<section>Alice has a cat</section>'),
278 textNode = node.contents()[0];
280 var returned = textNode.wrapWith({tagName: 'header', attrs: {'attr1': 'value1'}, start: offsets.start, end: offsets.end}),
281 contents = node.contents();
283 expect(contents.length).to.equal(3);
285 expect(contents[0].nodeType).to.be.equal(Node.TEXT_NODE, 'first node is text node');
286 expect(contents[0].getText()).to.equal('Alice');
288 expect(contents[1].sameNode(returned)).to.be.true;
289 expect(returned.getTagName()).to.equal('header');
290 expect(returned.getAttr('attr1')).to.equal('value1');
291 expect(contents[1].contents().length).to.equal(1, 'wrapper has one node inside');
292 expect(contents[1].contents()[0].getText()).to.equal(' has a ');
294 expect(contents[2].nodeType).to.be.equal(Node.TEXT_NODE, 'third node is text node');
295 expect(contents[2].getText()).to.equal('cat');
299 it('wraps whole text inside DocumentTextElement if offsets span entire content', function() {
300 var node = elementNodeFromXML('<section>Alice has a cat</section>'),
301 textNode = node.contents()[0];
303 textNode.wrapWith({tagName: 'header', start: 0, end: 15});
305 var contents = node.contents();
306 expect(contents.length).to.equal(1);
307 expect(contents[0].getTagName()).to.equal('header');
308 expect(contents[0].contents()[0].getText()).to.equal('Alice has a cat');
313 describe('Dividing text node into two with element node', function() {
314 it('can divide text node with element node, splitting text node into two', function() {
315 var doc = getDocumentFromXML('<div>Alice has a cat</div>'),
316 text = doc.root.contents()[0];
318 var returned = text.divideWithElementNode({tagName: 'aside'}, {offset: 5}),
319 contents = doc.root.contents(),
320 lhsText = contents[0],
321 rhsText = contents[2];
323 expect(lhsText.getText()).to.equal('Alice');
324 expect(returned.sameNode(contents[1]));
325 expect(rhsText.getText()).to.equal(' has a cat');
328 it('treats dividing at the very end as appending after it', function() {
329 var doc = getDocumentFromXML('<div>Alice has a cat</div>'),
330 text = doc.root.contents()[0];
333 var returned = text.divideWithElementNode({tagName: 'aside'}, {offset: 15}),
334 contents = doc.root.contents(),
335 textNode = contents[0],
336 elementNode = contents[1];
338 expect(contents.length).to.equal(2);
339 expect(textNode.getText()).to.equal('Alice has a cat');
340 expect(returned.sameNode(elementNode)).to.be.true;
341 expect(elementNode.getTagName()).to.equal('aside');
344 it('treats dividing at the very beginning as prepending before it', function() {
345 var doc = getDocumentFromXML('<div>Alice has a cat</div>'),
346 text = doc.root.contents()[0];
348 var returned = text.divideWithElementNode({tagName: 'aside'}, {offset: 0}),
349 contents = doc.root.contents(),
350 textNode = contents[1],
351 elementNode = contents[0];
353 expect(contents.length).to.equal(2);
354 expect(textNode.getText()).to.equal('Alice has a cat');
355 expect(returned.sameNode(elementNode)).to.be.true;
356 expect(elementNode.getTagName()).to.equal('aside');
361 describe('Manipulations', function() {
363 describe('replacing node with another one', function() {
364 it('replaces node with another one', function() {
365 var doc = getDocumentFromXML('<div><a></a></div>'),
366 a = doc.root.contents()[0];
368 var c = a.replaceWith({tagName: 'b', attrs: {b:'1'}});
370 expect(doc.root.contents()[0].sameNode(c));
371 expect(c.getTagName()).to.equal('b');
372 expect(c.getAttr('b')).to.equal('1');
374 it('can replace document root', function() {
375 var doc = getDocumentFromXML('<div></div>');
377 var header = doc.root.replaceWith({tagName: 'header'});
379 expect(doc.root.sameNode(header)).to.be.true;
380 expect(doc.containsNode(header)).to.be.true;
384 it('merges adjacent text nodes resulting from detaching an element node in between', function() {
385 var doc = getDocumentFromXML('<div>Alice <span>has</span>a cat</div>'),
386 span = doc.root.contents()[1];
390 var rootContents = doc.root.contents();
391 expect(rootContents).to.have.length(1, 'one child left');
392 expect(rootContents[0].getText()).to.equal('Alice a cat');
395 it('inserts node at index', function() {
396 var doc = getDocumentFromXML('<div><a></a><b></b><c></c></div>'),
397 b = doc.root.contents()[1];
399 var inserted = doc.root.insertAtIndex({tagName: 'test'}, 1);
401 expect(doc.root.contents()[1].sameNode(inserted)).to.equal(true, 'inserted node returned');
402 expect(b.getIndex()).to.equal(2, 'b node shifted right');
405 it('appends node when inserting node at index out of range', function() {
406 var doc = getDocumentFromXML('<div></div>');
408 var test1 = doc.root.insertAtIndex({tagName: 'test1'}, 0),
409 test2 = doc.root.insertAtIndex({tagName: 'test1'}, 10);
411 expect(doc.root.contents()[0].sameNode(test1)).to.equal(true, 'inserting at index 0 of empty nodes appends node');
412 expect(doc.root.contents().length).to.equal(1, 'inserting at index out of range does nothing');
413 expect(test2).to.equal(undefined, 'inserting at index out of range returns undefined');
416 it('appends element node to another element node', function() {
417 var node1 = elementNodeFromParams({tag: 'div'}),
418 node2 = elementNodeFromParams({tag: 'a'}),
419 node3 = elementNodeFromParams({tag: 'p'});
422 expect(node1.contents()[0].sameNode(node2)).to.be.true;
423 expect(node1.contents()[1].sameNode(node3)).to.be.true;
426 it('prepends element node to another element node', function() {
427 var node1 = elementNodeFromParams({tag: 'div'}),
428 node2 = elementNodeFromParams({tag: 'a'}),
429 node3 = elementNodeFromParams({tag: 'p'});
430 node1.prepend(node2);
431 node1.prepend(node3);
432 expect(node1.contents()[0].sameNode(node3)).to.be.true;
433 expect(node1.contents()[1].sameNode(node2)).to.be.true;
436 it('wraps element node with another element node', function() {
437 var node = elementNodeFromXML('<div></div>'),
438 wrapper = elementNodeFromXML('<wrapper></wrapper>');
440 node.wrapWith(wrapper);
441 expect(node.parent().sameNode(wrapper)).to.be.true;
444 it('unwraps element node contents', function() {
445 var node = elementNodeFromXML('<div>Alice <div>has <span>propably</span> a cat</div>!</div>'),
446 outerDiv = node.contents()[1];
448 outerDiv.unwrapContent();
450 expect(node.contents().length).to.equal(3);
451 expect(node.contents()[0].getText()).to.equal('Alice has ');
452 expect(node.contents()[1].getTagName()).to.equal('span');
453 expect(node.contents()[2].getText()).to.equal(' a cat!');
456 it('unwrap single element node from its parent', function() {
457 var doc = getDocumentFromXML('<div><a><b></b></a></div>'),
459 a = div.contents()[0],
462 var parent = b.unwrap();
464 expect(parent.sameNode(div)).to.equal(true, 'returns new parent');
465 expect(div.contents()).to.have.length(1, 'root contains only one node');
466 expect(div.contents()[0].sameNode(b)).to.equal(true, 'node got unwrapped');
469 it('unwrap single text node from its parent', function() {
470 var doc = getDocumentFromXML('<div>Some <span>text</span>!</div>'),
472 span = div.contents()[1],
473 text = span.contents()[0];
475 var parent = text.unwrap();
477 expect(parent.sameNode(div)).to.equal(true, 'returns new parent');
478 expect(div.contents()).to.have.length(1, 'root contains only one node');
479 expect(div.contents()[0].getText()).to.equal('Some text!');
482 describe('Wrapping text', function() {
483 it('wraps text spanning multiple sibling TextNodes', function() {
484 var section = elementNodeFromXML('<section>Alice has a <span>small</span> cat</section>'),
485 wrapper = section.wrapText({
486 _with: {tagName: 'span', attrs: {'attr1': 'value1'}},
492 expect(section.contents().length).to.equal(2);
493 expect(section.contents()[0].nodeType).to.equal(Node.TEXT_NODE);
494 expect(section.contents()[0].getText()).to.equal('Alice ');
496 var wrapper2 = section.contents()[1];
497 expect(wrapper2.sameNode(wrapper)).to.be.true;
498 expect(wrapper.getTagName()).to.equal('span');
500 var wrapperContents = wrapper.contents();
501 expect(wrapperContents.length).to.equal(3);
502 expect(wrapperContents[0].getText()).to.equal('has a ');
504 expect(wrapperContents[1].nodeType).to.equal(Node.ELEMENT_NODE);
505 expect(wrapperContents[1].contents().length).to.equal(1);
506 expect(wrapperContents[1].contents()[0].getText()).to.equal('small');
510 describe('Wrapping Nodes', function() {
511 it('wraps multiple sibling nodes', function() {
512 var section = elementNodeFromXML('<section>Alice<div>has</div><div>a cat</div></section>'),
513 aliceText = section.contents()[0],
514 firstDiv = section.contents()[1],
515 lastDiv = section.contents()[section.contents().length -1];
517 var returned = section.document.wrapNodes({
520 _with: {tagName: 'header'}
523 var sectionContentss = section.contents(),
524 header = sectionContentss[0],
525 headerContents = header.contents();
527 expect(sectionContentss).to.have.length(1);
528 expect(header.sameNode(returned)).to.equal(true, 'wrapper returned');
529 expect(header.parent().sameNode(section)).to.be.true;
530 expect(headerContents).to.have.length(3);
531 expect(headerContents[0].sameNode(aliceText)).to.equal(true, 'first node wrapped');
532 expect(headerContents[1].sameNode(firstDiv)).to.equal(true, 'second node wrapped');
533 expect(headerContents[2].sameNode(lastDiv)).to.equal(true, 'third node wrapped');
536 it('wraps multiple sibling Elements - middle case', function() {
537 var section = elementNodeFromXML('<section><div></div><div></div><div></div><div></div></section>'),
538 div2 = section.contents()[1],
539 div3 = section.contents()[2];
541 section.document.wrapNodes({
544 _with: {tagName: 'header'}
547 var sectionContentss = section.contents(),
548 header = sectionContentss[1],
549 headerChildren = header.contents();
551 expect(sectionContentss).to.have.length(3);
552 expect(headerChildren).to.have.length(2);
553 expect(headerChildren[0].sameNode(div2)).to.equal(true, 'first node wrapped');
554 expect(headerChildren[1].sameNode(div3)).to.equal(true, 'second node wrapped');
560 describe('Splitting text', function() {
562 it('splits TextNode\'s parent into two ElementNodes', function() {
563 var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
565 text = section.contents()[0].contents()[0];
567 var returnedValue = text.split({offset: 5});
568 expect(section.contents().length).to.equal(2, 'section has two children');
570 var header1 = section.contents()[0];
571 var header2 = section.contents()[1];
573 expect(header1.getTagName()).to.equal('header', 'first section child ok');
574 expect(header1.contents().length).to.equal(1, 'first header has one child');
575 expect(header1.contents()[0].getText()).to.equal('Some ', 'first header has correct content');
576 expect(header2.getTagName()).to.equal('header', 'second section child ok');
577 expect(header2.contents().length).to.equal(1, 'second header has one child');
578 expect(header2.contents()[0].getText()).to.equal('header', 'second header has correct content');
580 expect(returnedValue.first.sameNode(header1)).to.equal(true, 'first node returned');
581 expect(returnedValue.second.sameNode(header2)).to.equal(true, 'second node returned');
584 it('leaves empty copy of ElementNode if splitting at the very beginning', function() {
585 var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
587 text = section.contents()[0].contents()[0];
589 text.split({offset: 0});
591 var header1 = section.contents()[0];
592 var header2 = section.contents()[1];
594 expect(header1.contents().length).to.equal(0);
595 expect(header2.contents()[0].getText()).to.equal('Some header');
598 it('leaves empty copy of ElementNode if splitting at the very end', function() {
599 var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
601 text = section.contents()[0].contents()[0];
603 text.split({offset: 11});
605 var header1 = section.contents()[0];
606 var header2 = section.contents()[1];
608 expect(header1.contents()[0].getText()).to.equal('Some header');
609 expect(header2.contents().length).to.equal(0);
612 it('keeps TextNodes\'s parent\'s children elements intact', function() {
613 var doc = getDocumentFromXML('<section><header>A <span>fancy</span> and <span>nice</span> header</header></section>'),
615 header = section.contents()[0],
616 textAnd = header.contents()[2];
618 textAnd.split({offset: 2});
620 var sectionContents = section.contents();
621 expect(sectionContents.length).to.equal(2, 'Section has two children');
622 expect(sectionContents[0].getTagName()).to.equal('header', 'First section node is a header');
623 expect(sectionContents[1].getTagName()).to.equal('header', 'Second section node is a header');
625 var firstHeaderContents = sectionContents[0].contents();
626 expect(firstHeaderContents.length).to.equal(3, 'First header has three children');
627 expect(firstHeaderContents[0].getText()).to.equal('A ', 'First header starts with a text');
628 expect(firstHeaderContents[1].getTagName()).to.equal('span', 'First header has span in the middle');
629 expect(firstHeaderContents[2].getText()).to.equal(' a', 'First header ends with text');
631 var secondHeaderContents = sectionContents[1].contents();
632 expect(secondHeaderContents.length).to.equal(3, 'Second header has three children');
633 expect(secondHeaderContents[0].getText()).to.equal('nd ', 'Second header starts with text');
634 expect(secondHeaderContents[1].getTagName()).to.equal('span', 'Second header has span in the middle');
635 expect(secondHeaderContents[2].getText()).to.equal(' header', 'Second header ends with text');
639 describe('Events', function() {
640 it('emits nodeDetached event on node detach', function() {
641 var node = elementNodeFromXML('<div><div></div></div>'),
642 innerNode = node.contents()[0],
644 node.document.on('change', spy);
646 var detached = innerNode.detach(),
647 event = spy.args[0][0];
649 expect(event.type).to.equal('nodeDetached');
650 expect(event.meta.node.sameNode(detached, 'detached node in event meta'));
651 expect(event.meta.parent.sameNode(node), 'original parent node in event meta');
654 it('emits nodeAdded event when appending new node', function() {
655 var node = elementNodeFromXML('<div></div>'),
657 node.document.on('change', spy);
659 var appended = node.append({tagName:'div'}),
660 event = spy.args[0][0];
661 expect(event.type).to.equal('nodeAdded');
662 expect(event.meta.node.sameNode(appended)).to.be.true;
665 it('emits nodeMoved when appending aready existing node', function() {
666 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
667 a = node.contents()[0],
668 b = node.contents()[1],
670 node.document.on('change', spy);
672 var appended = a.append(b),
673 event = spy.args[0][0];
675 expect(spy.callCount).to.equal(1);
676 expect(event.type).to.equal('nodeMoved');
677 expect(event.meta.node.sameNode(appended)).to.be.true;
680 it('emits nodeAdded event when prepending new node', function() {
681 var node = elementNodeFromXML('<div></div>'),
683 node.document.on('change', spy);
685 var prepended = node.prepend({tagName:'div'}),
686 event = spy.args[0][0];
687 expect(event.type).to.equal('nodeAdded');
688 expect(event.meta.node.sameNode(prepended)).to.be.true;
691 it('emits nodeMoved when prepending aready existing node', function() {
692 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
693 a = node.contents()[0],
694 b = node.contents()[1],
696 node.document.on('change', spy);
698 var prepended = a.prepend(b),
699 event = spy.args[0][0];
700 expect(spy.callCount).to.equal(1);
701 expect(event.type).to.equal('nodeMoved');
702 expect(event.meta.node.sameNode(prepended)).to.be.true;
705 it('emits nodeAdded event when inserting node after another', function() {
706 var node = elementNodeFromXML('<div><a></a></div>').contents()[0],
708 node.document.on('change', spy);
710 var inserted = node.after({tagName:'div'}),
711 event = spy.args[0][0];
712 expect(event.type).to.equal('nodeAdded');
713 expect(event.meta.node.sameNode(inserted)).to.be.true;
716 it('emits nodeMoved when inserting aready existing node after another', function() {
717 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
718 a = node.contents()[0],
719 b = node.contents()[1],
721 node.document.on('change', spy);
722 var inserted = b.after(a),
723 event = spy.args[0][0];
725 expect(spy.callCount).to.equal(1);
726 expect(event.type).to.equal('nodeMoved');
727 expect(event.meta.node.sameNode(inserted)).to.be.true;
730 it('emits nodeAdded event when inserting node before another', function() {
731 var node = elementNodeFromXML('<div><a></a></div>').contents()[0],
733 node.document.on('change', spy);
735 var inserted = node.before({tagName:'div'}),
736 event = spy.args[0][0];
737 expect(event.type).to.equal('nodeAdded');
738 expect(event.meta.node.sameNode(inserted)).to.be.true;
741 it('emits nodeAdded when inserting aready existing node before another', function() {
742 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
743 a = node.contents()[0],
744 b = node.contents()[1],
746 node.document.on('change', spy);
747 var inserted = a.before(b),
748 event = spy.args[0][0];
750 expect(spy.callCount).to.equal(1);
751 expect(event.type).to.equal('nodeMoved');
752 expect(event.meta.node.sameNode(inserted)).to.be.true;
755 it('emits nodeDetached and nodeAdded when replacing root node with another', function() {
756 var doc = getDocumentFromXML('<a></a>'),
760 doc.on('change', spy);
762 doc.root.replaceWith({tagName: 'b'});
764 expect(spy.callCount).to.equal(2);
766 var event1 = spy.args[0][0],
767 event2 = spy.args[1][0];
769 expect(event1.type).to.equal('nodeDetached');
770 expect(event1.meta.node.sameNode(oldRoot)).to.equal(true, 'root node in nodeDetached event metadata');
771 expect(event2.type).to.equal('nodeAdded');
772 expect(event2.meta.node.sameNode(doc.root)).to.equal(true, 'new root node in nodelAdded event meta');
776 ['append', 'prepend', 'before', 'after'].forEach(function(insertionMethod) {
777 it('emits nodeDetached for node moved from a document tree to out of document node ' + insertionMethod, function() {
778 var doc = getDocumentFromXML('<div><a></a></div>'),
779 a = doc.root.contents()[0],
782 doc.on('change', spy);
784 var newNode = doc.createDocumentNode({tagName: 'b'}),
785 newNodeInner = newNode.append({tagName:'c'});
787 newNodeInner[insertionMethod](a);
789 var event = spy.args[0][0];
790 expect(event.type).to.equal('nodeDetached');
791 expect(event.meta.node.sameNode(a));
794 it('doesn\'t emit nodeDetached event for already out of document node moved to out of document node' + insertionMethod, function() {
795 var doc = getDocumentFromXML('<div><a></a></div>'),
798 doc.on('change', spy);
800 var newNode = doc.createDocumentNode({tagName: 'b'});
801 newNode.append({tagName:'c'});
803 expect(spy.callCount).to.equal(0);
810 describe('Traversing', function() {
811 describe('Basic', function() {
812 it('can access node parent', function() {
813 var doc = getDocumentFromXML('<a><b></b></a>'),
817 expect(a.parent()).to.equal(null, 'parent of a root is null');
818 expect(b.parent().sameNode(a)).to.be.true;
820 it('can access node parents', function() {
821 var doc = getDocumentFromXML('<a><b><c></c></b></a>'),
826 var parents = c.parents();
828 expect(parents[0].sameNode(b)).to.be.true;
829 expect(parents[1].sameNode(a)).to.be.true;
833 describe('finding sibling parents of two elements', function() {
834 it('returns elements themself if they have direct common parent', function() {
835 var doc = getDocumentFromXML('<section><div><div>A</div><div>B</div></div></section>'),
836 wrappingDiv = doc.root.contents()[0],
837 divA = wrappingDiv.contents()[0],
838 divB = wrappingDiv.contents()[1];
840 var siblingParents = doc.getSiblingParents({node1: divA, node2: divB});
842 expect(siblingParents.node1.sameNode(divA)).to.equal(true, 'divA');
843 expect(siblingParents.node2.sameNode(divB)).to.equal(true, 'divB');
846 it('returns sibling parents - example 1', function() {
847 var doc = getDocumentFromXML('<section>Alice <span>has a cat</span></section>'),
848 aliceText = doc.root.contents()[0],
849 span = doc.root.contents()[1],
850 spanText = span.contents()[0];
852 var siblingParents = doc.getSiblingParents({node1: aliceText, node2: spanText});
854 expect(siblingParents.node1.sameNode(aliceText)).to.equal(true, 'aliceText');
855 expect(siblingParents.node2.sameNode(span)).to.equal(true, 'span');
860 describe('Serializing document to WLXML', function() {
861 it('keeps document intact when no changes have been made', function() {
862 var xmlIn = '<section>Alice<div>has</div>a <span class="uri" meta-uri="http://cat.com">cat</span>!</section>',
863 doc = getDocumentFromXML(xmlIn),
864 xmlOut = doc.toXML();
866 var parser = new DOMParser(),
867 input = parser.parseFromString(xmlIn, 'application/xml').childNodes[0],
868 output = parser.parseFromString(xmlOut, 'application/xml').childNodes[0];
870 expect(input.isEqualNode(output)).to.be.true;
873 it('keeps entities intact', function() {
874 var xmlIn = '<section>< ></section>',
875 doc = getDocumentFromXML(xmlIn),
876 xmlOut = doc.toXML();
877 expect(xmlOut).to.equal(xmlIn);
879 it('keeps entities intact when they form html/xml', function() {
880 var xmlIn = '<section><abc></section>',
881 doc = getDocumentFromXML(xmlIn),
882 xmlOut = doc.toXML();
883 expect(xmlOut).to.equal(xmlIn);
887 describe('Extension API', function() {
888 var doc, extension, elementNode, textNode;
890 beforeEach(function() {
891 doc = getDocumentFromXML('<section>Alice<div class="test_class"></div></section>');
892 elementNode = doc.root;
893 textNode = doc.root.contents()[0];
896 expect(elementNode.testTransformation).to.be.undefined;
897 expect(textNode.testTransformation).to.be.undefined;
898 expect(doc.testTransformation).to.be.undefined;
900 expect(doc.testMethod).to.be.undefined;
901 expect(elementNode.testMethod).to.be.undefined;
902 expect(textNode.testMethod).to.be.undefined;
903 expect(elementNode.elementTestMethod).to.be.undefined;
904 expect(textNode.textTestMethod).to.be.undefined;
907 it('allows adding method to a document', function() {
908 extension = {document: {methods: {
909 testMethod: function() { return this; }
912 doc.registerExtension(extension);
913 expect(doc.testMethod()).to.equal(doc, 'context is set to a document instance');
916 it('allows adding transformation to a document', function() {
917 extension = {document: {transformations: {
918 testTransformation: function() { return this; },
919 testTransformation2: {impl: function() { return this;}}
922 doc.registerExtension(extension);
923 expect(doc.testTransformation()).to.equal(doc, 'context is set to a document instance');
924 expect(doc.testTransformation2()).to.equal(doc, 'context is set to a document instance');
927 it('allows adding method to a DocumentNode instance', function() {
931 testMethod: function() { return this; }
936 textTestMethod: function() { return this; }
941 elementTestMethod: function() { return this; }
946 doc.registerExtension(extension);
949 elementNode = doc.root;
950 textNode = doc.root.contents()[0];
952 expect(elementNode.testMethod().sameNode(elementNode)).to.equal(true, 'context is set to a node instance');
953 expect(textNode.testMethod().sameNode(textNode)).to.equal(true, 'context is set to a node instance');
955 expect(elementNode.elementTestMethod().sameNode(elementNode)).to.be.true;
956 expect(elementNode.textTestMethod).to.be.undefined;
958 expect(textNode.textTestMethod().sameNode(textNode)).to.be.true;
959 expect(textNode.elementTestMethod).to.be.undefined;
962 it('allows adding transformation to a DocumentNode', function() {
966 testTransformation: function() { return this; },
967 testTransformation2: {impl: function() { return this;}}
972 textTestTransformation: function() { return this; }
977 elementTestTransformation: function() { return this; }
982 doc.registerExtension(extension);
985 elementNode = doc.root;
986 textNode = doc.root.contents()[0];
988 expect(elementNode.testTransformation().sameNode(elementNode)).to.equal(true, '1');
989 expect(elementNode.testTransformation2().sameNode(elementNode)).to.equal(true, '2');
990 expect(textNode.testTransformation().sameNode(textNode)).to.equal(true, '3');
991 expect(textNode.testTransformation2().sameNode(textNode)).to.equal(true, '4');
993 expect(elementNode.elementTestTransformation().sameNode(elementNode)).to.be.true;
994 expect(elementNode.textTestTransformation).to.be.undefined;
996 expect(textNode.textTestTransformation().sameNode(textNode)).to.be.true;
997 expect(textNode.elementTestTransfomation).to.be.undefined;
1000 it('allows text/element node methods and transformations to access node and transormations on document node', function() {
1002 var doc = getDocumentFromXML('<div>text</div>');
1004 doc.registerExtension({
1013 return 'super_trans';
1020 return 'element_sub_' + this.__super__.test();
1025 return 'element_trans_sub_' + this.__super__.testT();
1032 return 'text_sub_' + this.__super__.test();
1037 return 'text_trans_sub_' + this.__super__.testT();
1043 var textNode = doc.root.contents()[0];
1045 expect(doc.root.test()).to.equal('element_sub_super');
1046 expect(textNode.test()).to.equal('text_sub_super');
1047 expect(doc.root.testT()).to.equal('element_trans_sub_super_trans');
1048 expect(textNode.testT()).to.equal('text_trans_sub_super_trans');
1052 describe('Undo/redo', function() {
1054 it('smoke tests', function() {
1055 var doc = getDocumentFromXML('<div>Alice</div>'),
1056 textNode = doc.root.contents()[0];
1058 expect(doc.undoStack).to.have.length(0);
1060 textNode.wrapWith({tagName: 'span', start:1, end:2});
1061 expect(doc.undoStack).to.have.length(1, '1');
1062 expect(doc.toXML()).to.equal('<div>A<span>l</span>ice</div>');
1065 expect(doc.undoStack).to.have.length(0, '2');
1066 expect(doc.toXML()).to.equal('<div>Alice</div>');
1069 expect(doc.undoStack).to.have.length(1, '3');
1070 expect(doc.toXML()).to.equal('<div>A<span>l</span>ice</div>');
1073 expect(doc.undoStack).to.have.length(0, '4');
1074 expect(doc.toXML()).to.equal('<div>Alice</div>');
1077 expect(doc.undoStack).to.have.length(0, '5');
1078 expect(doc.toXML()).to.equal('<div>Alice</div>');
1081 it('smoke tests 2', function() {
1082 var doc = getDocumentFromXML('<div>Alice</div>'),
1083 textNode = doc.root.contents()[0],
1084 path = textNode.getPath();
1086 textNode.setText('Alice ');
1087 textNode.setText('Alice h');
1088 textNode.setText('Alice ha');
1089 textNode.setText('Alice has');
1091 expect(textNode.getText()).to.equal('Alice has');
1094 expect(doc.root.contents()[0].getText()).to.equal('Alice ha', '1');
1097 expect(doc.root.contents()[0].getText()).to.equal('Alice h', '2');
1100 expect(doc.root.contents()[0].getText()).to.equal('Alice ha', '3');
1103 expect(doc.root.contents()[0].getText()).to.equal('Alice has', '4');
1107 textNode = doc.getNodeByPath(path);
1108 textNode.setText('Cat');
1110 textNode = doc.getNodeByPath(path);
1111 expect(textNode.getText()).to.equal('Alice h');
1115 var sampleMethod = function(val) {
1116 this._$.attr('x', val);
1119 var transformations = {
1120 'unaware': sampleMethod,
1121 'returning change root': {
1123 getChangeRoot: function() {
1124 return this.context;
1127 'implementing undo operation': {
1128 impl: function(t, val) {
1129 t.oldVal = this.getAttr('x');
1130 sampleMethod.call(this, val);
1133 this.setAttr('x', t.oldVal);
1138 _.pairs(transformations).forEach(function(pair) {
1140 transformaton = pair[1];
1142 describe(name + ' transformation: ', function() {
1143 var doc, node, nodePath;
1145 beforeEach(function() {
1146 doc = getDocumentFromXML('<div><test x="old"></test></div>');
1148 doc.registerExtension({elementNode: {transformations: {
1152 node = doc.root.contents()[0];
1153 nodePath = node.getPath();
1156 it('transforms as expected', function() {
1158 expect(node.getAttr('x')).to.equal('new');
1161 it('can be undone', function() {
1164 node = doc.getNodeByPath(nodePath);
1165 expect(node.getAttr('x')).to.equal('old');
1168 it('can be undone and then redone', function() {
1172 node = doc.getNodeByPath(nodePath);
1173 expect(node.getAttr('x')).to.equal('new');
1176 it('handles a sample scenario', function() {
1177 doc.root.contents()[0].test('1');
1178 doc.root.contents()[0].test('2');
1179 doc.root.contents()[0].test('3');
1180 doc.root.contents()[0].test('4');
1181 doc.root.contents()[0].test('5');
1183 expect(doc.root.contents()[0].getAttr('x')).to.equal('5', 'after initial transformations');
1185 expect(doc.root.contents()[0].getAttr('x')).to.equal('4', 'undo 1.1');
1187 expect(doc.root.contents()[0].getAttr('x')).to.equal('3', 'undo 1.2');
1189 expect(doc.root.contents()[0].getAttr('x')).to.equal('4', 'redo 1.1');
1191 expect(doc.root.contents()[0].getAttr('x')).to.equal('5', 'redo 1.2');
1193 expect(doc.root.contents()[0].getAttr('x')).to.equal('4', 'undo 2.1');
1194 doc.root.contents()[0].test('10');
1195 expect(doc.root.contents()[0].getAttr('x')).to.equal('10', 'additional transformation');
1196 expect(doc.redoStack.length).to.equal(0, 'transformation cleared redo stack');
1198 expect(doc.root.contents()[0].getAttr('x')).to.equal('10', 'empty redoStack so redo was noop');
1200 expect(doc.root.contents()[0].getAttr('x')).to.equal('4', 'undoing additional transformation');
1202 expect(doc.root.contents()[0].getAttr('x')).to.equal('10', 'redoing additional transformation');
1207 it('smoke tests nested transformations', function() {
1208 var doc = getDocumentFromXML('<div></div>');
1210 doc.registerExtension({elementNode: {transformations: {
1211 nested: function(v) {
1212 this._$.attr('innerAttr', v);
1214 outer: function(v) {
1216 this._$.attr('outerAttr', v);
1220 doc.root.outer('test1');
1221 doc.root.outer('test2');
1223 expect(doc.root.getAttr('innerAttr')).to.equal('test2');
1224 expect(doc.root.getAttr('outerAttr')).to.equal('test2');
1228 expect(doc.root.getAttr('innerAttr')).to.equal('test1');
1229 expect(doc.root.getAttr('outerAttr')).to.equal('test1');
1233 expect(doc.root.getAttr('innerAttr')).to.equal(undefined);
1234 expect(doc.root.getAttr('outerAttr')).to.equal(undefined);
1238 expect(doc.root.getAttr('innerAttr')).to.equal('test1');
1239 expect(doc.root.getAttr('outerAttr')).to.equal('test1');
1243 expect(doc.root.getAttr('innerAttr')).to.equal('test2');
1244 expect(doc.root.getAttr('outerAttr')).to.equal('test2');
1248 describe('Transactions', function() {
1249 it('allows to undo/redo series of transformations at once', function() {
1250 var doc = getDocumentFromXML('<div></div>');
1252 doc.registerExtension({
1253 elementNode: {transformations: {
1255 this.setAttr('test', v);
1260 doc.startTransaction();
1264 doc.endTransaction();
1267 expect(doc.root.getAttr('test'), '1');
1269 expect(doc.root.getAttr('test'), '3');
1271 expect(doc.root.getAttr('test'), '1');
1273 expect(doc.root.getAttr('test'), '3');
1276 it('doesn\'t break on optimizations', function() {
1277 // This is a smoke test checking if optimizations made to transaction undoing
1278 // doesnt't break anything.
1279 var doc = getDocumentFromXML('<div smart="1" unaware="1"></div>');
1281 doc.registerExtension({
1282 elementNode: {transformations: {
1283 unaware: function(v) {
1284 this.setAttr('unware', v);
1287 impl: function(t, v) {
1288 t.oldVal = this.getAttr('smart');
1289 this.setAttr('smart', v);
1292 this.setAttr('smart', t.oldVal);
1298 doc.startTransaction();
1299 doc.root.smart('2');
1300 doc.root.unaware('2');
1301 doc.root.smart('3');
1302 doc.root.unaware('3');
1303 doc.endTransaction();
1307 expect(doc.root.getAttr('smart')).to.equal('1');
1308 expect(doc.root.getAttr('unaware')).to.equal('1');