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');
65 describe('DocumentNode', function() {
66 it('can be cloned', function() {
67 var doc = getDocumentFromXML('<div>Alice</div>'),
68 text = doc.root.contents()[0],
71 [doc.root, text].forEach(function(node) {
72 suffix = ' (' + (node.nodeType === Node.TEXT_NODE ? 'text' : 'element') + ')';
74 expect(doc.containsNode(clone)).to.equal(false, 'clone is not contained in a document' + suffix);
75 expect(node.sameNode(clone)).to.equal(false, 'clone is not same node as its originator' + suffix);
76 expect(node.nativeNode.isEqualNode(clone.nativeNode)).to.equal(true, 'clone is identical as its originator' + suffix);
80 it('knows its path in the document tree', function() {
81 var doc = getDocumentFromXML('<root><a><b><c></c>text</b></a></root>'),
83 a = root.contents()[0],
85 text = b.contents()[1];
87 expect(root.getPath()).to.eql([], 'path of the root element is empty');
88 expect(a.getPath()).to.eql([0]);
89 expect(b.getPath()).to.eql([0, 0]);
90 expect(text.getPath()).to.eql([0,0,1]);
92 /* Paths relative to a given ancestor */
93 expect(text.getPath(root)).to.eql([0,0,1]);
94 expect(text.getPath(a)).to.eql([0,1]);
95 expect(text.getPath(b)).to.eql([1]);
99 describe('Basic ElementNode properties', function() {
100 it('exposes node contents', function() {
101 var node = elementNodeFromXML('<node>Some<node>text</node>is here</node>'),
102 contents = node.contents();
104 expect(contents).to.have.length(3);
105 expect(contents[0].nodeType).to.equal(Node.TEXT_NODE, 'text node 1');
106 expect(contents[1].nodeType).to.equal(Node.ELEMENT_NODE, 'element node 1');
107 expect(contents[2].nodeType).to.equal(Node.TEXT_NODE, 'text node 2');
110 describe('Storing custom data', function() {
113 beforeEach(function() {
114 node = elementNodeFromXML('<div></div>');
117 it('can append single value', function() {
118 node.setData('key', 'value');
119 expect(node.getData('key')).to.equal('value');
122 it('can overwrite the whole data', function() {
123 node.setData('key1', 'value1');
124 node.setData({key2: 'value2'});
125 expect(node.getData('key2')).to.equal('value2');
128 it('can fetch the whole data at once', function() {
129 node.setData({key1: 'value1', key2: 'value2'});
130 expect(node.getData()).to.eql({key1: 'value1', key2: 'value2'});
134 describe('Changing node tag', function() {
136 it('can change tag name', function() {
137 var node = elementNodeFromXML('<div></div>');
139 expect(node.getTagName()).to.equal('span');
142 it('emits nodeTagChange event', function() {
143 var node = elementNodeFromXML('<div></div>'),
146 node.document.on('change', spy);
148 var event = spy.args[0][0];
150 expect(event.type).to.equal('nodeTagChange');
151 expect(event.meta.node.sameNode(node)).to.be.true;
152 expect(event.meta.oldTagName).to.equal('div');
155 describe('Implementation specific expectations', function() {
156 // DOM specifies ElementNode tag as a read-only property, so
157 // changing it in a seamless way is a little bit tricky. For this reason
158 // the folowing expectations are required, despite the fact that they actually are
159 // motivated by implemetation details.
161 it('keeps node in the document', function() {
162 var doc = getDocumentFromXML('<div><header></header></div>'),
163 header = doc.root.contents()[0];
164 header.setTag('span');
165 expect(header.parent().sameNode(doc.root)).to.be.true;
167 it('keeps custom data', function() {
168 var node = elementNodeFromXML('<div></div>');
170 node.setData('key', 'value');
171 node.setTag('header');
173 expect(node.getTagName()).to.equal('header');
174 expect(node.getData()).to.eql({key: 'value'});
177 it('can change document root tag name', function() {
178 var doc = getDocumentFromXML('<div></div>');
179 doc.root.setTag('span');
180 expect(doc.root.getTagName()).to.equal('span');
183 it('keeps contents', function() {
184 var node = elementNodeFromXML('<div><div></div></div>');
185 node.setTag('header');
186 expect(node.contents()).to.have.length(1);
190 describe('Setting node attributes', function() {
191 it('can set node attribute', function() {
192 var node = elementNodeFromXML('<div></div>');
194 node.setAttr('key', 'value');
195 expect(node.getAttr('key')).to.equal('value');
197 it('emits nodeAttrChange event', function() {
198 var node = elementNodeFromXML('<div key="value1"></div>'),
201 node.document.on('change', spy);
202 node.setAttr('key', 'value2');
203 var event = spy.args[0][0];
205 expect(event.type).to.equal('nodeAttrChange');
206 expect(event.meta.node.sameNode(node)).to.be.true;
207 expect(event.meta.attr).to.equal('key');
208 expect(event.meta.oldVal).to.equal('value1');
215 describe('Basic TextNode properties', function() {
216 it('can have its text set', function() {
217 var node = elementNodeFromXML('<div>Alice</div>'),
218 textNode = node.contents()[0];
220 textNode.setText('Cat');
221 expect(textNode.getText()).to.equal('Cat');
224 it('emits nodeTextChange', function() {
225 var node = elementNodeFromXML('<div>Alice</div>'),
226 textNode = node.contents()[0],
229 textNode.document.on('change', spy);
230 textNode.setText('Cat');
232 var event = spy.args[0][0];
233 expect(event.type).to.equal('nodeTextChange');
236 it('puts NodeElement after itself', function() {
237 var node = elementNodeFromXML('<div>Alice</div>'),
238 textNode = node.contents()[0],
239 returned = textNode.after({tagName:'div'});
240 expect(returned.sameNode(node.contents()[1])).to.be.true;
243 it('puts NodeElement before itself', function() {
244 var node = elementNodeFromXML('<div>Alice</div>'),
245 textNode = node.contents()[0],
246 returned = textNode.before({tagName:'div'});
247 expect(returned.sameNode(node.contents()[0])).to.be.true;
250 describe('Wrapping TextNode contents', function() {
252 it('wraps DocumentTextElement', function() {
253 var node = elementNodeFromXML('<section>Alice</section>'),
254 textNode = node.contents()[0];
256 var returned = textNode.wrapWith({tagName: 'header'}),
257 parent = textNode.parent(),
258 parent2 = node.contents()[0];
260 expect(returned.sameNode(parent)).to.be.equal(true, 'wrapper is a parent');
261 expect(returned.sameNode(parent2)).to.be.equal(true, 'wrapper has a correct parent');
262 expect(returned.getTagName()).to.equal('header');
265 describe('wrapping part of DocumentTextElement', function() {
266 [{start: 5, end: 12}, {start: 12, end: 5}].forEach(function(offsets) {
267 it('wraps in the middle ' + offsets.start + '/' + offsets.end, function() {
268 var node = elementNodeFromXML('<section>Alice has a cat</section>'),
269 textNode = node.contents()[0];
271 var returned = textNode.wrapWith({tagName: 'header', attrs: {'attr1': 'value1'}, start: offsets.start, end: offsets.end}),
272 contents = node.contents();
274 expect(contents.length).to.equal(3);
276 expect(contents[0].nodeType).to.be.equal(Node.TEXT_NODE, 'first node is text node');
277 expect(contents[0].getText()).to.equal('Alice');
279 expect(contents[1].sameNode(returned)).to.be.true;
280 expect(returned.getTagName()).to.equal('header');
281 expect(returned.getAttr('attr1')).to.equal('value1');
282 expect(contents[1].contents().length).to.equal(1, 'wrapper has one node inside');
283 expect(contents[1].contents()[0].getText()).to.equal(' has a ');
285 expect(contents[2].nodeType).to.be.equal(Node.TEXT_NODE, 'third node is text node');
286 expect(contents[2].getText()).to.equal('cat');
290 it('wraps whole text inside DocumentTextElement if offsets span entire content', function() {
291 var node = elementNodeFromXML('<section>Alice has a cat</section>'),
292 textNode = node.contents()[0];
294 textNode.wrapWith({tagName: 'header', start: 0, end: 15});
296 var contents = node.contents();
297 expect(contents.length).to.equal(1);
298 expect(contents[0].getTagName()).to.equal('header');
299 expect(contents[0].contents()[0].getText()).to.equal('Alice has a cat');
306 describe('Manipulations', function() {
308 describe('replacing node with another one', function() {
309 it('replaces node with another one', function() {
310 var doc = getDocumentFromXML('<div><a></a></div>'),
311 a = doc.root.contents()[0];
313 var c = a.replaceWith({tagName: 'b', attrs: {b:'1'}});
315 expect(doc.root.contents()[0].sameNode(c));
316 expect(c.getTagName()).to.equal('b');
317 expect(c.getAttr('b')).to.equal('1');
319 it('can replace document root', function() {
320 var doc = getDocumentFromXML('<div></div>');
322 var header = doc.root.replaceWith({tagName: 'header'});
324 expect(doc.root.sameNode(header)).to.be.true;
325 expect(doc.containsNode(header)).to.be.true;
329 it('merges adjacent text nodes resulting from detaching an element node in between', function() {
330 var doc = getDocumentFromXML('<div>Alice <span>has</span>a cat</div>'),
331 span = doc.root.contents()[1];
335 var rootContents = doc.root.contents();
336 expect(rootContents).to.have.length(1, 'one child left');
337 expect(rootContents[0].getText()).to.equal('Alice a cat');
340 it('inserts node at index', function() {
341 var doc = getDocumentFromXML('<div><a></a><b></b><c></c></div>'),
342 b = doc.root.contents()[1];
344 var inserted = doc.root.insertAtIndex({tagName: 'test'}, 1);
346 expect(doc.root.contents()[1].sameNode(inserted)).to.equal(true, 'inserted node returned');
347 expect(b.getIndex()).to.equal(2, 'b node shifted right');
350 it('appends node when inserting node at index out of range', function() {
351 var doc = getDocumentFromXML('<div></div>');
353 var test1 = doc.root.insertAtIndex({tagName: 'test1'}, 0),
354 test2 = doc.root.insertAtIndex({tagName: 'test1'}, 10);
356 expect(doc.root.contents()[0].sameNode(test1)).to.equal(true, 'inserting at index 0 of empty nodes appends node');
357 expect(doc.root.contents().length).to.equal(1, 'inserting at index out of range does nothing');
358 expect(test2).to.equal(undefined, 'inserting at index out of range returns undefined');
361 it('appends element node to another element node', function() {
362 var node1 = elementNodeFromParams({tag: 'div'}),
363 node2 = elementNodeFromParams({tag: 'a'}),
364 node3 = elementNodeFromParams({tag: 'p'});
367 expect(node1.contents()[0].sameNode(node2)).to.be.true;
368 expect(node1.contents()[1].sameNode(node3)).to.be.true;
371 it('prepends element node to another element node', function() {
372 var node1 = elementNodeFromParams({tag: 'div'}),
373 node2 = elementNodeFromParams({tag: 'a'}),
374 node3 = elementNodeFromParams({tag: 'p'});
375 node1.prepend(node2);
376 node1.prepend(node3);
377 expect(node1.contents()[0].sameNode(node3)).to.be.true;
378 expect(node1.contents()[1].sameNode(node2)).to.be.true;
381 it('wraps element node with another element node', function() {
382 var node = elementNodeFromXML('<div></div>'),
383 wrapper = elementNodeFromXML('<wrapper></wrapper>');
385 node.wrapWith(wrapper);
386 expect(node.parent().sameNode(wrapper)).to.be.true;
389 it('unwraps element node contents', function() {
390 var node = elementNodeFromXML('<div>Alice <div>has <span>propably</span> a cat</div>!</div>'),
391 outerDiv = node.contents()[1];
393 outerDiv.unwrapContent();
395 expect(node.contents().length).to.equal(3);
396 expect(node.contents()[0].getText()).to.equal('Alice has ');
397 expect(node.contents()[1].getTagName()).to.equal('span');
398 expect(node.contents()[2].getText()).to.equal(' a cat!');
401 it('unwrap single element node from its parent', function() {
402 var doc = getDocumentFromXML('<div><a><b></b></a></div>'),
404 a = div.contents()[0],
407 var parent = b.unwrap();
409 expect(parent.sameNode(div)).to.equal(true, 'returns new parent');
410 expect(div.contents()).to.have.length(1, 'root contains only one node');
411 expect(div.contents()[0].sameNode(b)).to.equal(true, 'node got unwrapped');
414 it('unwrap single text node from its parent', function() {
415 var doc = getDocumentFromXML('<div>Some <span>text</span>!</div>'),
417 span = div.contents()[1],
418 text = span.contents()[0];
420 var parent = text.unwrap();
422 expect(parent.sameNode(div)).to.equal(true, 'returns new parent');
423 expect(div.contents()).to.have.length(1, 'root contains only one node');
424 expect(div.contents()[0].getText()).to.equal('Some text!');
427 describe('Wrapping text', function() {
428 it('wraps text spanning multiple sibling TextNodes', function() {
429 var section = elementNodeFromXML('<section>Alice has a <span>small</span> cat</section>'),
430 wrapper = section.wrapText({
431 _with: {tagName: 'span', attrs: {'attr1': 'value1'}},
437 expect(section.contents().length).to.equal(2);
438 expect(section.contents()[0].nodeType).to.equal(Node.TEXT_NODE);
439 expect(section.contents()[0].getText()).to.equal('Alice ');
441 var wrapper2 = section.contents()[1];
442 expect(wrapper2.sameNode(wrapper)).to.be.true;
443 expect(wrapper.getTagName()).to.equal('span');
445 var wrapperContents = wrapper.contents();
446 expect(wrapperContents.length).to.equal(3);
447 expect(wrapperContents[0].getText()).to.equal('has a ');
449 expect(wrapperContents[1].nodeType).to.equal(Node.ELEMENT_NODE);
450 expect(wrapperContents[1].contents().length).to.equal(1);
451 expect(wrapperContents[1].contents()[0].getText()).to.equal('small');
455 describe('Wrapping Nodes', function() {
456 it('wraps multiple sibling nodes', function() {
457 var section = elementNodeFromXML('<section>Alice<div>has</div><div>a cat</div></section>'),
458 aliceText = section.contents()[0],
459 firstDiv = section.contents()[1],
460 lastDiv = section.contents()[section.contents().length -1];
462 var returned = section.document.wrapNodes({
465 _with: {tagName: 'header'}
468 var sectionContentss = section.contents(),
469 header = sectionContentss[0],
470 headerContents = header.contents();
472 expect(sectionContentss).to.have.length(1);
473 expect(header.sameNode(returned)).to.equal(true, 'wrapper returned');
474 expect(header.parent().sameNode(section)).to.be.true;
475 expect(headerContents).to.have.length(3);
476 expect(headerContents[0].sameNode(aliceText)).to.equal(true, 'first node wrapped');
477 expect(headerContents[1].sameNode(firstDiv)).to.equal(true, 'second node wrapped');
478 expect(headerContents[2].sameNode(lastDiv)).to.equal(true, 'third node wrapped');
481 it('wraps multiple sibling Elements - middle case', function() {
482 var section = elementNodeFromXML('<section><div></div><div></div><div></div><div></div></section>'),
483 div2 = section.contents()[1],
484 div3 = section.contents()[2];
486 section.document.wrapNodes({
489 _with: {tagName: 'header'}
492 var sectionContentss = section.contents(),
493 header = sectionContentss[1],
494 headerChildren = header.contents();
496 expect(sectionContentss).to.have.length(3);
497 expect(headerChildren).to.have.length(2);
498 expect(headerChildren[0].sameNode(div2)).to.equal(true, 'first node wrapped');
499 expect(headerChildren[1].sameNode(div3)).to.equal(true, 'second node wrapped');
505 describe('Splitting text', function() {
507 it('splits TextNode\'s parent into two ElementNodes', function() {
508 var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
510 text = section.contents()[0].contents()[0];
512 var returnedValue = text.split({offset: 5});
513 expect(section.contents().length).to.equal(2, 'section has two children');
515 var header1 = section.contents()[0];
516 var header2 = section.contents()[1];
518 expect(header1.getTagName()).to.equal('header', 'first section child ok');
519 expect(header1.contents().length).to.equal(1, 'first header has one child');
520 expect(header1.contents()[0].getText()).to.equal('Some ', 'first header has correct content');
521 expect(header2.getTagName()).to.equal('header', 'second section child ok');
522 expect(header2.contents().length).to.equal(1, 'second header has one child');
523 expect(header2.contents()[0].getText()).to.equal('header', 'second header has correct content');
525 expect(returnedValue.first.sameNode(header1)).to.equal(true, 'first node returned');
526 expect(returnedValue.second.sameNode(header2)).to.equal(true, 'second node returned');
529 it('leaves empty copy of ElementNode if splitting at the very beginning', function() {
530 var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
532 text = section.contents()[0].contents()[0];
534 text.split({offset: 0});
536 var header1 = section.contents()[0];
537 var header2 = section.contents()[1];
539 expect(header1.contents().length).to.equal(0);
540 expect(header2.contents()[0].getText()).to.equal('Some header');
543 it('leaves empty copy of ElementNode if splitting at the very end', function() {
544 var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
546 text = section.contents()[0].contents()[0];
548 text.split({offset: 11});
550 var header1 = section.contents()[0];
551 var header2 = section.contents()[1];
553 expect(header1.contents()[0].getText()).to.equal('Some header');
554 expect(header2.contents().length).to.equal(0);
557 it('keeps TextNodes\'s parent\'s children elements intact', function() {
558 var doc = getDocumentFromXML('<section><header>A <span>fancy</span> and <span>nice</span> header</header></section>'),
560 header = section.contents()[0],
561 textAnd = header.contents()[2];
563 textAnd.split({offset: 2});
565 var sectionContents = section.contents();
566 expect(sectionContents.length).to.equal(2, 'Section has two children');
567 expect(sectionContents[0].getTagName()).to.equal('header', 'First section node is a header');
568 expect(sectionContents[1].getTagName()).to.equal('header', 'Second section node is a header');
570 var firstHeaderContents = sectionContents[0].contents();
571 expect(firstHeaderContents.length).to.equal(3, 'First header has three children');
572 expect(firstHeaderContents[0].getText()).to.equal('A ', 'First header starts with a text');
573 expect(firstHeaderContents[1].getTagName()).to.equal('span', 'First header has span in the middle');
574 expect(firstHeaderContents[2].getText()).to.equal(' a', 'First header ends with text');
576 var secondHeaderContents = sectionContents[1].contents();
577 expect(secondHeaderContents.length).to.equal(3, 'Second header has three children');
578 expect(secondHeaderContents[0].getText()).to.equal('nd ', 'Second header starts with text');
579 expect(secondHeaderContents[1].getTagName()).to.equal('span', 'Second header has span in the middle');
580 expect(secondHeaderContents[2].getText()).to.equal(' header', 'Second header ends with text');
584 describe('Events', function() {
585 it('emits nodeDetached event on node detach', function() {
586 var node = elementNodeFromXML('<div><div></div></div>'),
587 innerNode = node.contents()[0],
589 node.document.on('change', spy);
591 var detached = innerNode.detach(),
592 event = spy.args[0][0];
594 expect(event.type).to.equal('nodeDetached');
595 expect(event.meta.node.sameNode(detached, 'detached node in event meta'));
596 expect(event.meta.parent.sameNode(node), 'original parent node in event meta');
599 it('emits nodeAdded event when appending new node', function() {
600 var node = elementNodeFromXML('<div></div>'),
602 node.document.on('change', spy);
604 var appended = node.append({tagName:'div'}),
605 event = spy.args[0][0];
606 expect(event.type).to.equal('nodeAdded');
607 expect(event.meta.node.sameNode(appended)).to.be.true;
610 it('emits nodeMoved when appending aready existing node', function() {
611 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
612 a = node.contents()[0],
613 b = node.contents()[1],
615 node.document.on('change', spy);
617 var appended = a.append(b),
618 event = spy.args[0][0];
620 expect(spy.callCount).to.equal(1);
621 expect(event.type).to.equal('nodeMoved');
622 expect(event.meta.node.sameNode(appended)).to.be.true;
625 it('emits nodeAdded event when prepending new node', function() {
626 var node = elementNodeFromXML('<div></div>'),
628 node.document.on('change', spy);
630 var prepended = node.prepend({tagName:'div'}),
631 event = spy.args[0][0];
632 expect(event.type).to.equal('nodeAdded');
633 expect(event.meta.node.sameNode(prepended)).to.be.true;
636 it('emits nodeMoved when prepending aready existing node', function() {
637 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
638 a = node.contents()[0],
639 b = node.contents()[1],
641 node.document.on('change', spy);
643 var prepended = a.prepend(b),
644 event = spy.args[0][0];
645 expect(spy.callCount).to.equal(1);
646 expect(event.type).to.equal('nodeMoved');
647 expect(event.meta.node.sameNode(prepended)).to.be.true;
650 it('emits nodeAdded event when inserting node after another', function() {
651 var node = elementNodeFromXML('<div><a></a></div>').contents()[0],
653 node.document.on('change', spy);
655 var inserted = node.after({tagName:'div'}),
656 event = spy.args[0][0];
657 expect(event.type).to.equal('nodeAdded');
658 expect(event.meta.node.sameNode(inserted)).to.be.true;
661 it('emits nodeMoved when inserting aready existing node after another', function() {
662 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
663 a = node.contents()[0],
664 b = node.contents()[1],
666 node.document.on('change', spy);
667 var inserted = b.after(a),
668 event = spy.args[0][0];
670 expect(spy.callCount).to.equal(1);
671 expect(event.type).to.equal('nodeMoved');
672 expect(event.meta.node.sameNode(inserted)).to.be.true;
675 it('emits nodeAdded event when inserting node before another', function() {
676 var node = elementNodeFromXML('<div><a></a></div>').contents()[0],
678 node.document.on('change', spy);
680 var inserted = node.before({tagName:'div'}),
681 event = spy.args[0][0];
682 expect(event.type).to.equal('nodeAdded');
683 expect(event.meta.node.sameNode(inserted)).to.be.true;
686 it('emits nodeAdded when inserting aready existing node before another', function() {
687 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
688 a = node.contents()[0],
689 b = node.contents()[1],
691 node.document.on('change', spy);
692 var inserted = a.before(b),
693 event = spy.args[0][0];
695 expect(spy.callCount).to.equal(1);
696 expect(event.type).to.equal('nodeMoved');
697 expect(event.meta.node.sameNode(inserted)).to.be.true;
700 it('emits nodeDetached and nodeAdded when replacing root node with another', function() {
701 var doc = getDocumentFromXML('<a></a>'),
705 doc.on('change', spy);
707 doc.root.replaceWith({tagName: 'b'});
709 expect(spy.callCount).to.equal(2);
711 var event1 = spy.args[0][0],
712 event2 = spy.args[1][0];
714 expect(event1.type).to.equal('nodeDetached');
715 expect(event1.meta.node.sameNode(oldRoot)).to.equal(true, 'root node in nodeDetached event metadata');
716 expect(event2.type).to.equal('nodeAdded');
717 expect(event2.meta.node.sameNode(doc.root)).to.equal(true, 'new root node in nodelAdded event meta');
721 ['append', 'prepend', 'before', 'after'].forEach(function(insertionMethod) {
722 it('emits nodeDetached for node moved from a document tree to out of document node ' + insertionMethod, function() {
723 var doc = getDocumentFromXML('<div><a></a></div>'),
724 a = doc.root.contents()[0],
727 doc.on('change', spy);
729 var newNode = doc.createDocumentNode({tagName: 'b'}),
730 newNodeInner = newNode.append({tagName:'c'});
732 newNodeInner[insertionMethod](a);
734 var event = spy.args[0][0];
735 expect(event.type).to.equal('nodeDetached');
736 expect(event.meta.node.sameNode(a));
739 it('doesn\'t emit nodeDetached event for already out of document moved to out of document node: ' + insertionMethod, function() {
740 var doc = getDocumentFromXML('<div><a></a></div>'),
741 a = doc.root.contents()[0],
744 doc.on('change', spy);
746 var newNode = doc.createDocumentNode({tagName: 'b'});
747 var newNodeInner = newNode.append({tagName:'c'});
749 expect(spy.callCount).to.equal(0);
756 describe('Traversing', function() {
757 describe('Basic', function() {
758 it('can access node parent', function() {
759 var doc = getDocumentFromXML('<a><b></b></a>'),
763 expect(a.parent()).to.equal(null, 'parent of a root is null');
764 expect(b.parent().sameNode(a)).to.be.true;
766 it('can access node parents', function() {
767 var doc = getDocumentFromXML('<a><b><c></c></b></a>'),
772 var parents = c.parents();
774 expect(parents[0].sameNode(b)).to.be.true;
775 expect(parents[1].sameNode(a)).to.be.true;
779 describe('finding sibling parents of two elements', function() {
780 it('returns elements themself if they have direct common parent', function() {
781 var doc = getDocumentFromXML('<section><div><div>A</div><div>B</div></div></section>'),
782 wrappingDiv = doc.root.contents()[0],
783 divA = wrappingDiv.contents()[0],
784 divB = wrappingDiv.contents()[1];
786 var siblingParents = doc.getSiblingParents({node1: divA, node2: divB});
788 expect(siblingParents.node1.sameNode(divA)).to.equal(true, 'divA');
789 expect(siblingParents.node2.sameNode(divB)).to.equal(true, 'divB');
792 it('returns sibling parents - example 1', function() {
793 var doc = getDocumentFromXML('<section>Alice <span>has a cat</span></section>'),
794 aliceText = doc.root.contents()[0],
795 span = doc.root.contents()[1],
796 spanText = span.contents()[0];
798 var siblingParents = doc.getSiblingParents({node1: aliceText, node2: spanText});
800 expect(siblingParents.node1.sameNode(aliceText)).to.equal(true, 'aliceText');
801 expect(siblingParents.node2.sameNode(span)).to.equal(true, 'span');
806 describe('Serializing document to WLXML', function() {
807 it('keeps document intact when no changes have been made', function() {
808 var xmlIn = '<section>Alice<div>has</div>a <span class="uri" meta-uri="http://cat.com">cat</span>!</section>',
809 doc = getDocumentFromXML(xmlIn),
810 xmlOut = doc.toXML();
812 var parser = new DOMParser(),
813 input = parser.parseFromString(xmlIn, 'application/xml').childNodes[0],
814 output = parser.parseFromString(xmlOut, 'application/xml').childNodes[0];
816 expect(input.isEqualNode(output)).to.be.true;
819 it('keeps entities intact', function() {
820 var xmlIn = '<section>< ></section>',
821 doc = getDocumentFromXML(xmlIn),
822 xmlOut = doc.toXML();
823 expect(xmlOut).to.equal(xmlIn);
825 it('keeps entities intact when they form html/xml', function() {
826 var xmlIn = '<section><abc></section>',
827 doc = getDocumentFromXML(xmlIn),
828 xmlOut = doc.toXML();
829 expect(xmlOut).to.equal(xmlIn);
833 describe('Extension API', function() {
834 var doc, extension, elementNode, textNode, testClassNode;
836 beforeEach(function() {
837 doc = getDocumentFromXML('<section>Alice<div class="test_class"></div></section>');
838 elementNode = doc.root;
839 textNode = doc.root.contents()[0];
842 expect(elementNode.testTransformation).to.be.undefined;
843 expect(textNode.testTransformation).to.be.undefined;
844 expect(doc.testTransformation).to.be.undefined;
846 expect(doc.testMethod).to.be.undefined;
847 expect(elementNode.testMethod).to.be.undefined;
848 expect(textNode.testMethod).to.be.undefined;
849 expect(elementNode.elementTestMethod).to.be.undefined;
850 expect(textNode.textTestMethod).to.be.undefined;
853 it('allows adding method to a document', function() {
854 extension = {document: {methods: {
855 testMethod: function() { return this; }
858 doc.registerExtension(extension);
859 expect(doc.testMethod()).to.equal(doc, 'context is set to a document instance');
862 it('allows adding transformation to a document', function() {
863 extension = {document: {transformations: {
864 testTransformation: function() { return this; },
865 testTransformation2: {impl: function() { return this;}}
868 doc.registerExtension(extension);
869 expect(doc.testTransformation()).to.equal(doc, 'context is set to a document instance');
870 expect(doc.testTransformation2()).to.equal(doc, 'context is set to a document instance');
873 it('allows adding method to a DocumentNode instance', function() {
877 testMethod: function() { return this; }
882 textTestMethod: function() { return this; }
887 elementTestMethod: function() { return this; }
892 doc.registerExtension(extension);
895 elementNode = doc.root;
896 textNode = doc.root.contents()[0];
898 expect(elementNode.testMethod().sameNode(elementNode)).to.equal(true, 'context is set to a node instance');
899 expect(textNode.testMethod().sameNode(textNode)).to.equal(true, 'context is set to a node instance');
901 expect(elementNode.elementTestMethod().sameNode(elementNode)).to.be.true;
902 expect(elementNode.textTestMethod).to.be.undefined;
904 expect(textNode.textTestMethod().sameNode(textNode)).to.be.true;
905 expect(textNode.elementTestMethod).to.be.undefined;
908 it('allows adding transformation to a DocumentNode', function() {
912 testTransformation: function() { return this; },
913 testTransformation2: {impl: function() { return this;}}
918 textTestTransformation: function() { return this; }
923 elementTestTransformation: function() { return this; }
928 doc.registerExtension(extension);
931 elementNode = doc.root;
932 textNode = doc.root.contents()[0];
934 expect(elementNode.testTransformation().sameNode(elementNode)).to.equal(true, '1');
935 expect(elementNode.testTransformation2().sameNode(elementNode)).to.equal(true, '2');
936 expect(textNode.testTransformation().sameNode(textNode)).to.equal(true, '3');
937 expect(textNode.testTransformation2().sameNode(textNode)).to.equal(true, '4');
939 expect(elementNode.elementTestTransformation().sameNode(elementNode)).to.be.true;
940 expect(elementNode.textTestTransformation).to.be.undefined;
942 expect(textNode.textTestTransformation().sameNode(textNode)).to.be.true;
943 expect(textNode.elementTestTransfomation).to.be.undefined;
946 it('allows text/element node methods and transformations to access node and transormations on document node', function() {
948 var doc = getDocumentFromXML('<div>text</div>');
950 doc.registerExtension({
959 return 'super_trans';
966 return 'element_sub_' + this.__super__.test();
971 return 'element_trans_sub_' + this.__super__.testT();
978 return 'text_sub_' + this.__super__.test();
983 return 'text_trans_sub_' + this.__super__.testT();
989 var textNode = doc.root.contents()[0];
991 expect(doc.root.test()).to.equal('element_sub_super');
992 expect(textNode.test()).to.equal('text_sub_super');
993 expect(doc.root.testT()).to.equal('element_trans_sub_super_trans');
994 expect(textNode.testT()).to.equal('text_trans_sub_super_trans');
998 describe('Undo/redo', function() {
1000 it('smoke tests', function() {
1001 var doc = getDocumentFromXML('<div>Alice</div>'),
1002 textNode = doc.root.contents()[0];
1004 expect(doc.undoStack).to.have.length(0);
1006 textNode.wrapWith({tagName: 'span', start:1, end:2});
1007 expect(doc.undoStack).to.have.length(1, '1');
1008 expect(doc.toXML()).to.equal('<div>A<span>l</span>ice</div>');
1011 expect(doc.undoStack).to.have.length(0, '2');
1012 expect(doc.toXML()).to.equal('<div>Alice</div>');
1015 expect(doc.undoStack).to.have.length(1, '3');
1016 expect(doc.toXML()).to.equal('<div>A<span>l</span>ice</div>');
1019 expect(doc.undoStack).to.have.length(0, '4');
1020 expect(doc.toXML()).to.equal('<div>Alice</div>');
1023 expect(doc.undoStack).to.have.length(0, '5');
1024 expect(doc.toXML()).to.equal('<div>Alice</div>');
1027 it('smoke tests 2', function() {
1028 var doc = getDocumentFromXML('<div>Alice</div>'),
1029 textNode = doc.root.contents()[0],
1030 path = textNode.getPath();
1032 textNode.setText('Alice ');
1033 textNode.setText('Alice h');
1034 textNode.setText('Alice ha');
1035 textNode.setText('Alice has');
1037 expect(textNode.getText()).to.equal('Alice has');
1040 expect(doc.root.contents()[0].getText()).to.equal('Alice ha', '1');
1043 expect(doc.root.contents()[0].getText()).to.equal('Alice h', '2');
1046 expect(doc.root.contents()[0].getText()).to.equal('Alice ha', '3');
1049 expect(doc.root.contents()[0].getText()).to.equal('Alice has', '4');
1053 textNode = doc.getNodeByPath(path);
1054 textNode.setText('Cat');
1056 textNode = doc.getNodeByPath(path);
1057 expect(textNode.getText()).to.equal('Alice h');
1061 var sampleMethod = function(val) {
1062 this._$.attr('x', val);
1065 var transformations = {
1066 'unaware': sampleMethod,
1067 'returning change root': {
1069 getChangeRoot: function() {
1070 return this.context;
1073 'implementing undo operation': {
1074 impl: function(t, val) {
1075 t.oldVal = this.getAttr('x');
1076 sampleMethod.call(this, val);
1079 this.setAttr('x', t.oldVal);
1084 _.pairs(transformations).forEach(function(pair) {
1086 transformaton = pair[1];
1088 describe(name + ' transformation: ', function() {
1089 var doc, node, nodePath;
1091 beforeEach(function() {
1092 doc = getDocumentFromXML('<div><test x="old"></test></div>');
1094 doc.registerExtension({elementNode: {transformations: {
1098 node = doc.root.contents()[0];
1099 nodePath = node.getPath();
1102 it('transforms as expected', function() {
1104 expect(node.getAttr('x')).to.equal('new');
1107 it('can be undone', function() {
1110 node = doc.getNodeByPath(nodePath);
1111 expect(node.getAttr('x')).to.equal('old');
1114 it('can be undone and then redone', function() {
1118 node = doc.getNodeByPath(nodePath);
1119 expect(node.getAttr('x')).to.equal('new');
1122 it('handles a sample scenario', function() {
1123 doc.root.contents()[0].test('1');
1124 doc.root.contents()[0].test('2');
1125 doc.root.contents()[0].test('3');
1126 doc.root.contents()[0].test('4');
1127 doc.root.contents()[0].test('5');
1129 expect(doc.root.contents()[0].getAttr('x')).to.equal('5', 'after initial transformations');
1131 expect(doc.root.contents()[0].getAttr('x')).to.equal('4', 'undo 1.1');
1133 expect(doc.root.contents()[0].getAttr('x')).to.equal('3', 'undo 1.2');
1135 expect(doc.root.contents()[0].getAttr('x')).to.equal('4', 'redo 1.1');
1137 expect(doc.root.contents()[0].getAttr('x')).to.equal('5', 'redo 1.2');
1139 expect(doc.root.contents()[0].getAttr('x')).to.equal('4', 'undo 2.1');
1140 doc.root.contents()[0].test('10');
1141 expect(doc.root.contents()[0].getAttr('x')).to.equal('10', 'additional transformation');
1142 expect(doc.redoStack.length).to.equal(0, 'transformation cleared redo stack');
1144 expect(doc.root.contents()[0].getAttr('x')).to.equal('10', 'empty redoStack so redo was noop');
1146 expect(doc.root.contents()[0].getAttr('x')).to.equal('4', 'undoing additional transformation');
1148 expect(doc.root.contents()[0].getAttr('x')).to.equal('10', 'redoing additional transformation');
1153 it('smoke tests nested transformations', function() {
1154 var doc = getDocumentFromXML('<div></div>');
1156 doc.registerExtension({elementNode: {transformations: {
1157 nested: function(v) {
1158 this._$.attr('innerAttr', v);
1160 outer: function(v) {
1162 this._$.attr('outerAttr', v);
1166 doc.root.outer('test1');
1167 doc.root.outer('test2');
1169 expect(doc.root.getAttr('innerAttr')).to.equal('test2');
1170 expect(doc.root.getAttr('outerAttr')).to.equal('test2');
1174 expect(doc.root.getAttr('innerAttr')).to.equal('test1');
1175 expect(doc.root.getAttr('outerAttr')).to.equal('test1');
1179 expect(doc.root.getAttr('innerAttr')).to.equal(undefined);
1180 expect(doc.root.getAttr('outerAttr')).to.equal(undefined);
1184 expect(doc.root.getAttr('innerAttr')).to.equal('test1');
1185 expect(doc.root.getAttr('outerAttr')).to.equal('test1');
1189 expect(doc.root.getAttr('innerAttr')).to.equal('test2');
1190 expect(doc.root.getAttr('outerAttr')).to.equal('test2');
1193 // it('does work', function() {
1194 // var doc = getDocumentFromXML('<section><span>Alice</span></section>'),
1195 // span = doc.root.contents()[0];
1197 // span.transform('smartxml.detach');
1202 // expect(doc.root.contents()).to.have.length(1);
1203 // expect(doc.root.contents()[0].getTagName()).to.equal('span');
1204 // expect(doc.root.contents()[0].contents()[0].getText()).to.equal('Alice');
1207 // expect(doc.root.contents()).to.have.length(0);
1210 // expect(doc.root.contents()).to.have.length(1);
1211 // expect(doc.root.contents()[0].getTagName()).to.equal('span');
1212 // expect(doc.root.contents()[0].contents()[0].getText()).to.equal('Alice');
1215 // it('does work - merged text nodes case', function() {
1216 // var doc = getDocumentFromXML('<section>Alice <span>has</span> a cat.</section>'),
1217 // span = doc.root.contents()[1];
1219 // span.transform('smartxml.detach');
1224 // expect(doc.root.contents().length).to.equal(3);
1225 // //console.log(doc.toXML());
1226 // expect(doc.root.contents()[1].contents()[0].getText()).to.equal('has');
1229 // it('dbg - don not store nodes in tranformation state!', function() {
1230 // var doc = getDocumentFromXML('<section><a></a><b></b></section>'),
1231 // a = doc.root.contents()[0],
1232 // b = doc.root.contents()[1];
1234 // a.transform('smartxml.detach');
1235 // b.transform('smartxml.detach');
1238 // expect(doc.root.contents().length).to.equal(2);
1239 // expect(doc.root.contents()[0].getTagName()).to.equal('a');
1240 // expect(doc.root.contents()[1].getTagName()).to.equal('b');
1244 // expect(doc.root.contents().length).to.equal(0);