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'});
168 describe('Changing node tag', function() {
170 it('can change tag name', function() {
171 var node = elementNodeFromXML('<div></div>');
173 expect(node.getTagName()).to.equal('span');
176 it('emits nodeTagChange event', function() {
177 var node = elementNodeFromXML('<div></div>'),
180 node.document.on('change', spy);
182 var event = spy.args[0][0];
184 expect(event.type).to.equal('nodeTagChange');
185 expect(event.meta.node.sameNode(node)).to.be.true;
186 expect(event.meta.oldTagName).to.equal('div');
189 describe('Implementation specific expectations', function() {
190 // DOM specifies ElementNode tag as a read-only property, so
191 // changing it in a seamless way is a little bit tricky. For this reason
192 // the folowing expectations are required, despite the fact that they actually are
193 // motivated by implemetation details.
195 it('keeps node in the document', function() {
196 var doc = getDocumentFromXML('<div><header></header></div>'),
197 header = doc.root.contents()[0];
198 header.setTag('span');
199 expect(header.parent().sameNode(doc.root)).to.be.true;
201 it('keeps custom data', function() {
202 var node = elementNodeFromXML('<div></div>');
204 node.setData('key', 'value');
205 node.setTag('header');
207 expect(node.getTagName()).to.equal('header');
208 expect(node.getData()).to.eql({key: 'value'});
211 it('can change document root tag name', function() {
212 var doc = getDocumentFromXML('<div></div>');
213 doc.root.setTag('span');
214 expect(doc.root.getTagName()).to.equal('span');
217 it('keeps contents', function() {
218 var node = elementNodeFromXML('<div><div></div></div>');
219 node.setTag('header');
220 expect(node.contents()).to.have.length(1);
224 describe('Setting node attributes', function() {
225 it('can set node attribute', function() {
226 var node = elementNodeFromXML('<div></div>');
228 node.setAttr('key', 'value');
229 expect(node.getAttr('key')).to.equal('value');
231 it('emits nodeAttrChange event', function() {
232 var node = elementNodeFromXML('<div key="value1"></div>'),
235 node.document.on('change', spy);
236 node.setAttr('key', 'value2');
237 var event = spy.args[0][0];
239 expect(event.type).to.equal('nodeAttrChange');
240 expect(event.meta.node.sameNode(node)).to.be.true;
241 expect(event.meta.attr).to.equal('key');
242 expect(event.meta.oldVal).to.equal('value1');
249 describe('Basic TextNode properties', function() {
250 it('can have its text set', function() {
251 var node = elementNodeFromXML('<div>Alice</div>'),
252 textNode = node.contents()[0];
254 textNode.setText('Cat');
255 expect(textNode.getText()).to.equal('Cat');
258 it('emits nodeTextChange', function() {
259 var node = elementNodeFromXML('<div>Alice</div>'),
260 textNode = node.contents()[0],
263 textNode.document.on('change', spy);
264 textNode.setText('Cat');
266 var event = spy.args[0][0];
267 expect(event.type).to.equal('nodeTextChange');
270 it('puts NodeElement after itself', function() {
271 var node = elementNodeFromXML('<div>Alice</div>'),
272 textNode = node.contents()[0],
273 returned = textNode.after({tagName:'div'});
274 expect(returned.sameNode(node.contents()[1])).to.be.true;
277 it('puts NodeElement before itself', function() {
278 var node = elementNodeFromXML('<div>Alice</div>'),
279 textNode = node.contents()[0],
280 returned = textNode.before({tagName:'div'});
281 expect(returned.sameNode(node.contents()[0])).to.be.true;
284 describe('Wrapping TextNode contents', function() {
286 it('wraps DocumentTextElement', function() {
287 var node = elementNodeFromXML('<section>Alice</section>'),
288 textNode = node.contents()[0];
290 var returned = textNode.wrapWith({tagName: 'header'}),
291 parent = textNode.parent(),
292 parent2 = node.contents()[0];
294 expect(returned.sameNode(parent)).to.be.equal(true, 'wrapper is a parent');
295 expect(returned.sameNode(parent2)).to.be.equal(true, 'wrapper has a correct parent');
296 expect(returned.getTagName()).to.equal('header');
299 describe('wrapping part of DocumentTextElement', function() {
300 [{start: 5, end: 12}, {start: 12, end: 5}].forEach(function(offsets) {
301 it('wraps in the middle ' + offsets.start + '/' + offsets.end, function() {
302 var node = elementNodeFromXML('<section>Alice has a cat</section>'),
303 textNode = node.contents()[0];
305 var returned = textNode.wrapWith({tagName: 'header', attrs: {'attr1': 'value1'}, start: offsets.start, end: offsets.end}),
306 contents = node.contents();
308 expect(contents.length).to.equal(3);
310 expect(contents[0].nodeType).to.be.equal(Node.TEXT_NODE, 'first node is text node');
311 expect(contents[0].getText()).to.equal('Alice');
313 expect(contents[1].sameNode(returned)).to.be.true;
314 expect(returned.getTagName()).to.equal('header');
315 expect(returned.getAttr('attr1')).to.equal('value1');
316 expect(contents[1].contents().length).to.equal(1, 'wrapper has one node inside');
317 expect(contents[1].contents()[0].getText()).to.equal(' has a ');
319 expect(contents[2].nodeType).to.be.equal(Node.TEXT_NODE, 'third node is text node');
320 expect(contents[2].getText()).to.equal('cat');
324 it('wraps whole text inside DocumentTextElement if offsets span entire content', function() {
325 var node = elementNodeFromXML('<section>Alice has a cat</section>'),
326 textNode = node.contents()[0];
328 textNode.wrapWith({tagName: 'header', start: 0, end: 15});
330 var contents = node.contents();
331 expect(contents.length).to.equal(1);
332 expect(contents[0].getTagName()).to.equal('header');
333 expect(contents[0].contents()[0].getText()).to.equal('Alice has a cat');
338 describe('Dividing text node into two with element node', function() {
339 it('can divide text node with element node, splitting text node into two', function() {
340 var doc = getDocumentFromXML('<div>Alice has a cat</div>'),
341 text = doc.root.contents()[0];
343 var returned = text.divideWithElementNode({tagName: 'aside'}, {offset: 5}),
344 contents = doc.root.contents(),
345 lhsText = contents[0],
346 rhsText = contents[2];
348 expect(lhsText.getText()).to.equal('Alice');
349 expect(returned.sameNode(contents[1]));
350 expect(rhsText.getText()).to.equal(' has a cat');
353 it('treats dividing at the very end as appending after it', function() {
354 var doc = getDocumentFromXML('<div>Alice has a cat</div>'),
355 text = doc.root.contents()[0];
358 var returned = text.divideWithElementNode({tagName: 'aside'}, {offset: 15}),
359 contents = doc.root.contents(),
360 textNode = contents[0],
361 elementNode = contents[1];
363 expect(contents.length).to.equal(2);
364 expect(textNode.getText()).to.equal('Alice has a cat');
365 expect(returned.sameNode(elementNode)).to.be.true;
366 expect(elementNode.getTagName()).to.equal('aside');
369 it('treats dividing at the very beginning as prepending before it', function() {
370 var doc = getDocumentFromXML('<div>Alice has a cat</div>'),
371 text = doc.root.contents()[0];
373 var returned = text.divideWithElementNode({tagName: 'aside'}, {offset: 0}),
374 contents = doc.root.contents(),
375 textNode = contents[1],
376 elementNode = contents[0];
378 expect(contents.length).to.equal(2);
379 expect(textNode.getText()).to.equal('Alice has a cat');
380 expect(returned.sameNode(elementNode)).to.be.true;
381 expect(elementNode.getTagName()).to.equal('aside');
386 describe('Manipulations', function() {
388 describe('replacing node with another one', function() {
389 it('replaces node with another one', function() {
390 var doc = getDocumentFromXML('<div><a></a></div>'),
391 a = doc.root.contents()[0];
393 var c = a.replaceWith({tagName: 'b', attrs: {b:'1'}});
395 expect(doc.root.contents()[0].sameNode(c));
396 expect(c.getTagName()).to.equal('b');
397 expect(c.getAttr('b')).to.equal('1');
399 it('can replace document root', function() {
400 var doc = getDocumentFromXML('<div></div>');
402 var header = doc.root.replaceWith({tagName: 'header'});
404 expect(doc.root.sameNode(header)).to.be.true;
405 expect(doc.containsNode(header)).to.be.true;
409 it('merges adjacent text nodes resulting from detaching an element node in between', function() {
410 var doc = getDocumentFromXML('<div>Alice <span>has</span>a cat</div>'),
411 span = doc.root.contents()[1];
415 var rootContents = doc.root.contents();
416 expect(rootContents).to.have.length(1, 'one child left');
417 expect(rootContents[0].getText()).to.equal('Alice a cat');
420 it('inserts node at index', function() {
421 var doc = getDocumentFromXML('<div><a></a><b></b><c></c></div>'),
422 b = doc.root.contents()[1];
424 var inserted = doc.root.insertAtIndex({tagName: 'test'}, 1);
426 expect(doc.root.contents()[1].sameNode(inserted)).to.equal(true, 'inserted node returned');
427 expect(b.getIndex()).to.equal(2, 'b node shifted right');
430 it('appends node when inserting node at index out of range', function() {
431 var doc = getDocumentFromXML('<div></div>');
433 var test1 = doc.root.insertAtIndex({tagName: 'test1'}, 0),
434 test2 = doc.root.insertAtIndex({tagName: 'test1'}, 10);
436 expect(doc.root.contents()[0].sameNode(test1)).to.equal(true, 'inserting at index 0 of empty nodes appends node');
437 expect(doc.root.contents().length).to.equal(1, 'inserting at index out of range does nothing');
438 expect(test2).to.equal(undefined, 'inserting at index out of range returns undefined');
441 it('appends element node to another element node', function() {
442 var node1 = elementNodeFromParams({tag: 'div'}),
443 node2 = elementNodeFromParams({tag: 'a'}),
444 node3 = elementNodeFromParams({tag: 'p'});
447 expect(node1.contents()[0].sameNode(node2)).to.be.true;
448 expect(node1.contents()[1].sameNode(node3)).to.be.true;
451 it('prepends element node to another element node', function() {
452 var node1 = elementNodeFromParams({tag: 'div'}),
453 node2 = elementNodeFromParams({tag: 'a'}),
454 node3 = elementNodeFromParams({tag: 'p'});
455 node1.prepend(node2);
456 node1.prepend(node3);
457 expect(node1.contents()[0].sameNode(node3)).to.be.true;
458 expect(node1.contents()[1].sameNode(node2)).to.be.true;
461 describe('adding text nodes', function() {
462 it('merges text nodes on append', function() {
463 var doc = getDocumentFromXML('<root>text1</root>'),
465 returned = doc.root.append({text: 'text2'});
466 expect(doc.root.contents().length).to.equal(1);
467 expect(returned.sameNode(doc.root.contents()[0])).to.equal(true, 'modified node returned');
468 expect(doc.root.contents()[0].getText()).to.equal('text1text2');
471 it('merges text nodes on prepend', function() {
472 var doc = getDocumentFromXML('<root>text1</root>'),
474 returned = doc.root.prepend({text: 'text2'});
475 expect(doc.root.contents().length).to.equal(1);
476 expect(returned.sameNode(doc.root.contents()[0])).to.equal(true, 'modified node returned');
477 expect(doc.root.contents()[0].getText()).to.equal('text2text1');
480 it('merges text nodes on before text node', function() {
481 var doc = getDocumentFromXML('<root>text1</root>'),
482 textNode = doc.root.contents()[0],
484 returned = textNode.before({text: 'text2'});
485 expect(doc.root.contents().length).to.equal(1);
486 expect(returned.sameNode(doc.root.contents()[0])).to.equal(true, 'modified node returned');
487 expect(doc.root.contents()[0].getText()).to.equal('text2text1');
490 it('merges text nodes on after text node', function() {
491 var doc = getDocumentFromXML('<root>text1</root>'),
492 textNode = doc.root.contents()[0],
494 returned = textNode.after({text: 'text2'});
495 expect(doc.root.contents().length).to.equal(1);
496 expect(returned.sameNode(doc.root.contents()[0])).to.equal(true, 'modified node returned');
497 expect(doc.root.contents()[0].getText()).to.equal('text1text2');
500 it('merges text nodes on before element node', function() {
501 var doc = getDocumentFromXML('<root>text1<div></div></root>'),
502 textNode = doc.root.contents()[0],
503 div = doc.root.contents()[1],
505 returned = div.before({text: 'text2'});
506 expect(doc.root.contents().length).to.equal(2);
507 expect(returned.sameNode(doc.root.contents()[0])).to.equal(true, 'modified node returned');
508 expect(textNode.getText()).to.equal('text1text2');
511 it('merges text nodes on after element node', function() {
512 var doc = getDocumentFromXML('<root><div></div>text1</root>'),
513 textNode = doc.root.contents()[1],
514 div = doc.root.contents()[0],
516 returned = div.after({text: 'text2'});
517 expect(doc.root.contents().length).to.equal(2);
518 expect(returned.sameNode(doc.root.contents()[1])).to.equal(true, 'modified node returned');
519 expect(textNode.getText()).to.equal('text2text1');
523 it('wraps element node with another element node', function() {
524 var node = elementNodeFromXML('<div></div>'),
525 wrapper = elementNodeFromXML('<wrapper></wrapper>');
527 node.wrapWith(wrapper);
528 expect(node.parent().sameNode(wrapper)).to.be.true;
531 it('unwraps element node contents', function() {
532 var node = elementNodeFromXML('<div>Alice <div>has <span>propably</span> a cat</div>!</div>'),
533 outerDiv = node.contents()[1];
535 outerDiv.unwrapContent();
537 expect(node.contents().length).to.equal(3);
538 expect(node.contents()[0].getText()).to.equal('Alice has ');
539 expect(node.contents()[1].getTagName()).to.equal('span');
540 expect(node.contents()[2].getText()).to.equal(' a cat!');
543 it('unwrap single element node from its parent', function() {
544 var doc = getDocumentFromXML('<div><a><b></b></a></div>'),
546 a = div.contents()[0],
549 var parent = b.unwrap();
551 expect(parent.sameNode(div)).to.equal(true, 'returns new parent');
552 expect(div.contents()).to.have.length(1, 'root contains only one node');
553 expect(div.contents()[0].sameNode(b)).to.equal(true, 'node got unwrapped');
556 it('unwrap single text node from its parent', function() {
557 var doc = getDocumentFromXML('<div>Some <span>text</span>!</div>'),
559 span = div.contents()[1],
560 text = span.contents()[0];
562 var parent = text.unwrap();
564 expect(parent.sameNode(div)).to.equal(true, 'returns new parent');
565 expect(div.contents()).to.have.length(1, 'root contains only one node');
566 expect(div.contents()[0].getText()).to.equal('Some text!');
569 describe('Wrapping text', function() {
570 it('wraps text spanning multiple sibling TextNodes', function() {
571 var section = elementNodeFromXML('<section>Alice has a <span>small</span> cat</section>'),
572 wrapper = section.wrapText({
573 _with: {tagName: 'span', attrs: {'attr1': 'value1'}},
579 expect(section.contents().length).to.equal(2);
580 expect(section.contents()[0].nodeType).to.equal(Node.TEXT_NODE);
581 expect(section.contents()[0].getText()).to.equal('Alice ');
583 var wrapper2 = section.contents()[1];
584 expect(wrapper2.sameNode(wrapper)).to.be.true;
585 expect(wrapper.getTagName()).to.equal('span');
587 var wrapperContents = wrapper.contents();
588 expect(wrapperContents.length).to.equal(3);
589 expect(wrapperContents[0].getText()).to.equal('has a ');
591 expect(wrapperContents[1].nodeType).to.equal(Node.ELEMENT_NODE);
592 expect(wrapperContents[1].contents().length).to.equal(1);
593 expect(wrapperContents[1].contents()[0].getText()).to.equal('small');
597 describe('Wrapping Nodes', function() {
598 it('wraps multiple sibling nodes', function() {
599 var section = elementNodeFromXML('<section>Alice<div>has</div><div>a cat</div></section>'),
600 aliceText = section.contents()[0],
601 firstDiv = section.contents()[1],
602 lastDiv = section.contents()[section.contents().length -1];
604 var returned = section.document.wrapNodes({
607 _with: {tagName: 'header'}
610 var sectionContentss = section.contents(),
611 header = sectionContentss[0],
612 headerContents = header.contents();
614 expect(sectionContentss).to.have.length(1);
615 expect(header.sameNode(returned)).to.equal(true, 'wrapper returned');
616 expect(header.parent().sameNode(section)).to.be.true;
617 expect(headerContents).to.have.length(3);
618 expect(headerContents[0].sameNode(aliceText)).to.equal(true, 'first node wrapped');
619 expect(headerContents[1].sameNode(firstDiv)).to.equal(true, 'second node wrapped');
620 expect(headerContents[2].sameNode(lastDiv)).to.equal(true, 'third node wrapped');
623 it('wraps multiple sibling Elements - middle case', function() {
624 var section = elementNodeFromXML('<section><div></div><div></div><div></div><div></div></section>'),
625 div2 = section.contents()[1],
626 div3 = section.contents()[2];
628 section.document.wrapNodes({
631 _with: {tagName: 'header'}
634 var sectionContentss = section.contents(),
635 header = sectionContentss[1],
636 headerChildren = header.contents();
638 expect(sectionContentss).to.have.length(3);
639 expect(headerChildren).to.have.length(2);
640 expect(headerChildren[0].sameNode(div2)).to.equal(true, 'first node wrapped');
641 expect(headerChildren[1].sameNode(div3)).to.equal(true, 'second node wrapped');
647 describe('Splitting text', function() {
649 it('splits TextNode\'s parent into two ElementNodes', function() {
650 var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
652 text = section.contents()[0].contents()[0];
654 var returnedValue = text.split({offset: 5});
655 expect(section.contents().length).to.equal(2, 'section has two children');
657 var header1 = section.contents()[0];
658 var header2 = section.contents()[1];
660 expect(header1.getTagName()).to.equal('header', 'first section child ok');
661 expect(header1.contents().length).to.equal(1, 'first header has one child');
662 expect(header1.contents()[0].getText()).to.equal('Some ', 'first header has correct content');
663 expect(header2.getTagName()).to.equal('header', 'second section child ok');
664 expect(header2.contents().length).to.equal(1, 'second header has one child');
665 expect(header2.contents()[0].getText()).to.equal('header', 'second header has correct content');
667 expect(returnedValue.first.sameNode(header1)).to.equal(true, 'first node returned');
668 expect(returnedValue.second.sameNode(header2)).to.equal(true, 'second node returned');
671 it('leaves empty copy of ElementNode if splitting at the very beginning', function() {
672 var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
674 text = section.contents()[0].contents()[0];
676 text.split({offset: 0});
678 var header1 = section.contents()[0];
679 var header2 = section.contents()[1];
681 expect(header1.contents().length).to.equal(0);
682 expect(header2.contents()[0].getText()).to.equal('Some header');
685 it('leaves empty copy of ElementNode if splitting at the very end', function() {
686 var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
688 text = section.contents()[0].contents()[0];
690 text.split({offset: 11});
692 var header1 = section.contents()[0];
693 var header2 = section.contents()[1];
695 expect(header1.contents()[0].getText()).to.equal('Some header');
696 expect(header2.contents().length).to.equal(0);
699 it('keeps TextNodes\'s parent\'s children elements intact', function() {
700 var doc = getDocumentFromXML('<section><header>A <span>fancy</span> and <span>nice</span> header</header></section>'),
702 header = section.contents()[0],
703 textAnd = header.contents()[2];
705 textAnd.split({offset: 2});
707 var sectionContents = section.contents();
708 expect(sectionContents.length).to.equal(2, 'Section has two children');
709 expect(sectionContents[0].getTagName()).to.equal('header', 'First section node is a header');
710 expect(sectionContents[1].getTagName()).to.equal('header', 'Second section node is a header');
712 var firstHeaderContents = sectionContents[0].contents();
713 expect(firstHeaderContents.length).to.equal(3, 'First header has three children');
714 expect(firstHeaderContents[0].getText()).to.equal('A ', 'First header starts with a text');
715 expect(firstHeaderContents[1].getTagName()).to.equal('span', 'First header has span in the middle');
716 expect(firstHeaderContents[2].getText()).to.equal(' a', 'First header ends with text');
718 var secondHeaderContents = sectionContents[1].contents();
719 expect(secondHeaderContents.length).to.equal(3, 'Second header has three children');
720 expect(secondHeaderContents[0].getText()).to.equal('nd ', 'Second header starts with text');
721 expect(secondHeaderContents[1].getTagName()).to.equal('span', 'Second header has span in the middle');
722 expect(secondHeaderContents[2].getText()).to.equal(' header', 'Second header ends with text');
726 describe('Events', function() {
727 it('emits nodeDetached event on node detach', function() {
728 var node = elementNodeFromXML('<div><div></div></div>'),
729 innerNode = node.contents()[0],
731 node.document.on('change', spy);
733 var detached = innerNode.detach(),
734 event = spy.args[0][0];
736 expect(event.type).to.equal('nodeDetached');
737 expect(event.meta.node.sameNode(detached, 'detached node in event meta'));
738 expect(event.meta.parent.sameNode(node), 'original parent node in event meta');
741 it('emits nodeAdded event when appending new node', function() {
742 var node = elementNodeFromXML('<div></div>'),
744 node.document.on('change', spy);
746 var appended = node.append({tagName:'div'}),
747 event = spy.args[0][0];
748 expect(event.type).to.equal('nodeAdded');
749 expect(event.meta.node.sameNode(appended)).to.be.true;
752 it('emits nodeMoved when appending aready existing node', function() {
753 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
754 a = node.contents()[0],
755 b = node.contents()[1],
757 node.document.on('change', spy);
759 var appended = a.append(b),
760 event = spy.args[0][0];
762 expect(spy.callCount).to.equal(1);
763 expect(event.type).to.equal('nodeMoved');
764 expect(event.meta.node.sameNode(appended)).to.be.true;
767 it('emits nodeAdded event when prepending new node', function() {
768 var node = elementNodeFromXML('<div></div>'),
770 node.document.on('change', spy);
772 var prepended = node.prepend({tagName:'div'}),
773 event = spy.args[0][0];
774 expect(event.type).to.equal('nodeAdded');
775 expect(event.meta.node.sameNode(prepended)).to.be.true;
778 it('emits nodeMoved when prepending aready existing node', function() {
779 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
780 a = node.contents()[0],
781 b = node.contents()[1],
783 node.document.on('change', spy);
785 var prepended = a.prepend(b),
786 event = spy.args[0][0];
787 expect(spy.callCount).to.equal(1);
788 expect(event.type).to.equal('nodeMoved');
789 expect(event.meta.node.sameNode(prepended)).to.be.true;
792 it('emits nodeAdded event when inserting node after another', function() {
793 var node = elementNodeFromXML('<div><a></a></div>').contents()[0],
795 node.document.on('change', spy);
797 var inserted = node.after({tagName:'div'}),
798 event = spy.args[0][0];
799 expect(event.type).to.equal('nodeAdded');
800 expect(event.meta.node.sameNode(inserted)).to.be.true;
803 it('emits nodeMoved when inserting aready existing node after another', function() {
804 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
805 a = node.contents()[0],
806 b = node.contents()[1],
808 node.document.on('change', spy);
809 var inserted = b.after(a),
810 event = spy.args[0][0];
812 expect(spy.callCount).to.equal(1);
813 expect(event.type).to.equal('nodeMoved');
814 expect(event.meta.node.sameNode(inserted)).to.be.true;
817 it('emits nodeAdded event when inserting node before another', function() {
818 var node = elementNodeFromXML('<div><a></a></div>').contents()[0],
820 node.document.on('change', spy);
822 var inserted = node.before({tagName:'div'}),
823 event = spy.args[0][0];
824 expect(event.type).to.equal('nodeAdded');
825 expect(event.meta.node.sameNode(inserted)).to.be.true;
828 it('emits nodeAdded when inserting aready existing node before another', function() {
829 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
830 a = node.contents()[0],
831 b = node.contents()[1],
833 node.document.on('change', spy);
834 var inserted = a.before(b),
835 event = spy.args[0][0];
837 expect(spy.callCount).to.equal(1);
838 expect(event.type).to.equal('nodeMoved');
839 expect(event.meta.node.sameNode(inserted)).to.be.true;
842 it('emits nodeDetached and nodeAdded when replacing root node with another', function() {
843 var doc = getDocumentFromXML('<a></a>'),
847 doc.on('change', spy);
849 doc.root.replaceWith({tagName: 'b'});
851 expect(spy.callCount).to.equal(2);
853 var event1 = spy.args[0][0],
854 event2 = spy.args[1][0];
856 expect(event1.type).to.equal('nodeDetached');
857 expect(event1.meta.node.sameNode(oldRoot)).to.equal(true, 'root node in nodeDetached event metadata');
858 expect(event2.type).to.equal('nodeAdded');
859 expect(event2.meta.node.sameNode(doc.root)).to.equal(true, 'new root node in nodelAdded event meta');
863 ['append', 'prepend', 'before', 'after'].forEach(function(insertionMethod) {
864 it('emits nodeDetached for node moved from a document tree to out of document node ' + insertionMethod, function() {
865 var doc = getDocumentFromXML('<div><a></a></div>'),
866 a = doc.root.contents()[0],
869 doc.on('change', spy);
871 var newNode = doc.createDocumentNode({tagName: 'b'}),
872 newNodeInner = newNode.append({tagName:'c'});
874 newNodeInner[insertionMethod](a);
876 var event = spy.args[0][0];
877 expect(event.type).to.equal('nodeDetached');
878 expect(event.meta.node.sameNode(a));
881 it('doesn\'t emit nodeDetached event for already out of document node moved to out of document node' + insertionMethod, function() {
882 var doc = getDocumentFromXML('<div><a></a></div>'),
885 doc.on('change', spy);
887 var newNode = doc.createDocumentNode({tagName: 'b'});
888 newNode.append({tagName:'c'});
890 expect(spy.callCount).to.equal(0);
897 describe('Traversing', function() {
898 describe('Basic', function() {
899 it('can access node parent', function() {
900 var doc = getDocumentFromXML('<a><b></b></a>'),
904 expect(a.parent()).to.equal(null, 'parent of a root is null');
905 expect(b.parent().sameNode(a)).to.be.true;
907 it('can access node parents', function() {
908 var doc = getDocumentFromXML('<a><b><c></c></b></a>'),
913 var parents = c.parents();
915 expect(parents[0].sameNode(b)).to.be.true;
916 expect(parents[1].sameNode(a)).to.be.true;
920 describe('finding sibling parents of two elements', function() {
921 it('returns elements themself if they have direct common parent', function() {
922 var doc = getDocumentFromXML('<section><div><div>A</div><div>B</div></div></section>'),
923 wrappingDiv = doc.root.contents()[0],
924 divA = wrappingDiv.contents()[0],
925 divB = wrappingDiv.contents()[1];
927 var siblingParents = doc.getSiblingParents({node1: divA, node2: divB});
929 expect(siblingParents.node1.sameNode(divA)).to.equal(true, 'divA');
930 expect(siblingParents.node2.sameNode(divB)).to.equal(true, 'divB');
933 it('returns sibling parents - example 1', function() {
934 var doc = getDocumentFromXML('<section>Alice <span>has a cat</span></section>'),
935 aliceText = doc.root.contents()[0],
936 span = doc.root.contents()[1],
937 spanText = span.contents()[0];
939 var siblingParents = doc.getSiblingParents({node1: aliceText, node2: spanText});
941 expect(siblingParents.node1.sameNode(aliceText)).to.equal(true, 'aliceText');
942 expect(siblingParents.node2.sameNode(span)).to.equal(true, 'span');
947 describe('Serializing document to WLXML', function() {
948 it('keeps document intact when no changes have been made', function() {
949 var xmlIn = '<section>Alice<div>has</div>a <span class="uri" meta-uri="http://cat.com">cat</span>!</section>',
950 doc = getDocumentFromXML(xmlIn),
951 xmlOut = doc.toXML();
953 var parser = new DOMParser(),
954 input = parser.parseFromString(xmlIn, 'application/xml').childNodes[0],
955 output = parser.parseFromString(xmlOut, 'application/xml').childNodes[0];
957 expect(input.isEqualNode(output)).to.be.true;
960 it('keeps entities intact', function() {
961 var xmlIn = '<section>< ></section>',
962 doc = getDocumentFromXML(xmlIn),
963 xmlOut = doc.toXML();
964 expect(xmlOut).to.equal(xmlIn);
966 it('keeps entities intact when they form html/xml', function() {
967 var xmlIn = '<section><abc></section>',
968 doc = getDocumentFromXML(xmlIn),
969 xmlOut = doc.toXML();
970 expect(xmlOut).to.equal(xmlIn);
974 describe('Extension API', function() {
975 var doc, extension, elementNode, textNode;
977 beforeEach(function() {
978 doc = getDocumentFromXML('<section>Alice<div class="test_class"></div></section>');
979 elementNode = doc.root;
980 textNode = doc.root.contents()[0];
983 expect(elementNode.testTransformation).to.be.undefined;
984 expect(textNode.testTransformation).to.be.undefined;
985 expect(doc.testTransformation).to.be.undefined;
987 expect(doc.testMethod).to.be.undefined;
988 expect(elementNode.testMethod).to.be.undefined;
989 expect(textNode.testMethod).to.be.undefined;
990 expect(elementNode.elementTestMethod).to.be.undefined;
991 expect(textNode.textTestMethod).to.be.undefined;
994 it('allows adding method to a document', function() {
995 extension = {document: {methods: {
996 testMethod: function() { return this; }
999 doc.registerExtension(extension);
1000 expect(doc.testMethod()).to.equal(doc, 'context is set to a document instance');
1003 it('allows adding transformation to a document', function() {
1004 extension = {document: {transformations: {
1005 testTransformation: function() { return this; },
1006 testTransformation2: {impl: function() { return this;}}
1009 doc.registerExtension(extension);
1010 expect(doc.testTransformation()).to.equal(doc, 'context is set to a document instance');
1011 expect(doc.testTransformation2()).to.equal(doc, 'context is set to a document instance');
1014 it('allows adding method to a DocumentNode instance', function() {
1018 testMethod: function() { return this; }
1023 textTestMethod: function() { return this; }
1028 elementTestMethod: function() { return this; }
1033 doc.registerExtension(extension);
1036 elementNode = doc.root;
1037 textNode = doc.root.contents()[0];
1039 expect(elementNode.testMethod().sameNode(elementNode)).to.equal(true, 'context is set to a node instance');
1040 expect(textNode.testMethod().sameNode(textNode)).to.equal(true, 'context is set to a node instance');
1042 expect(elementNode.elementTestMethod().sameNode(elementNode)).to.be.true;
1043 expect(elementNode.textTestMethod).to.be.undefined;
1045 expect(textNode.textTestMethod().sameNode(textNode)).to.be.true;
1046 expect(textNode.elementTestMethod).to.be.undefined;
1049 it('allows adding transformation to a DocumentNode', function() {
1053 testTransformation: function() { return this; },
1054 testTransformation2: {impl: function() { return this;}}
1059 textTestTransformation: function() { return this; }
1064 elementTestTransformation: function() { return this; }
1069 doc.registerExtension(extension);
1072 elementNode = doc.root;
1073 textNode = doc.root.contents()[0];
1075 expect(elementNode.testTransformation().sameNode(elementNode)).to.equal(true, '1');
1076 expect(elementNode.testTransformation2().sameNode(elementNode)).to.equal(true, '2');
1077 expect(textNode.testTransformation().sameNode(textNode)).to.equal(true, '3');
1078 expect(textNode.testTransformation2().sameNode(textNode)).to.equal(true, '4');
1080 expect(elementNode.elementTestTransformation().sameNode(elementNode)).to.be.true;
1081 expect(elementNode.textTestTransformation).to.be.undefined;
1083 expect(textNode.textTestTransformation().sameNode(textNode)).to.be.true;
1084 expect(textNode.elementTestTransfomation).to.be.undefined;
1087 it('allows text/element node methods and transformations to access node and transormations on document node', function() {
1089 var doc = getDocumentFromXML('<div>text</div>');
1091 doc.registerExtension({
1100 return 'super_trans';
1107 return 'element_sub_' + this.__super__.test();
1112 return 'element_trans_sub_' + this.__super__.testT();
1119 return 'text_sub_' + this.__super__.test();
1124 return 'text_trans_sub_' + this.__super__.testT();
1130 var textNode = doc.root.contents()[0];
1132 expect(doc.root.test()).to.equal('element_sub_super');
1133 expect(textNode.test()).to.equal('text_sub_super');
1134 expect(doc.root.testT()).to.equal('element_trans_sub_super_trans');
1135 expect(textNode.testT()).to.equal('text_trans_sub_super_trans');
1139 describe('Undo/redo', function() {
1141 it('smoke tests', function() {
1142 var doc = getDocumentFromXML('<div>Alice</div>'),
1143 textNode = doc.root.contents()[0];
1145 expect(doc.undoStack).to.have.length(0);
1147 textNode.wrapWith({tagName: 'span', start:1, end:2});
1148 expect(doc.undoStack).to.have.length(1, '1');
1149 expect(doc.toXML()).to.equal('<div>A<span>l</span>ice</div>');
1152 expect(doc.undoStack).to.have.length(0, '2');
1153 expect(doc.toXML()).to.equal('<div>Alice</div>');
1156 expect(doc.undoStack).to.have.length(1, '3');
1157 expect(doc.toXML()).to.equal('<div>A<span>l</span>ice</div>');
1160 expect(doc.undoStack).to.have.length(0, '4');
1161 expect(doc.toXML()).to.equal('<div>Alice</div>');
1164 expect(doc.undoStack).to.have.length(0, '5');
1165 expect(doc.toXML()).to.equal('<div>Alice</div>');
1168 it('smoke tests 2', function() {
1169 var doc = getDocumentFromXML('<div>Alice</div>'),
1170 textNode = doc.root.contents()[0],
1171 path = textNode.getPath();
1173 textNode.setText('Alice ');
1174 textNode.setText('Alice h');
1175 textNode.setText('Alice ha');
1176 textNode.setText('Alice has');
1178 expect(textNode.getText()).to.equal('Alice has');
1181 expect(doc.root.contents()[0].getText()).to.equal('Alice ha', '1');
1184 expect(doc.root.contents()[0].getText()).to.equal('Alice h', '2');
1187 expect(doc.root.contents()[0].getText()).to.equal('Alice ha', '3');
1190 expect(doc.root.contents()[0].getText()).to.equal('Alice has', '4');
1194 textNode = doc.getNodeByPath(path);
1195 textNode.setText('Cat');
1197 textNode = doc.getNodeByPath(path);
1198 expect(textNode.getText()).to.equal('Alice h');
1202 var sampleMethod = function(val) {
1203 this._$.attr('x', val);
1204 this.triggerChangeEvent();
1207 var transformations = {
1208 'unaware': sampleMethod,
1209 'returning change root': {
1211 getChangeRoot: function() {
1212 return this.context;
1215 'implementing undo operation': {
1216 impl: function(t, val) {
1217 t.oldVal = this.getAttr('x');
1218 sampleMethod.call(this, val);
1221 this.setAttr('x', t.oldVal);
1226 _.pairs(transformations).forEach(function(pair) {
1228 transformaton = pair[1];
1230 describe(name + ' transformation: ', function() {
1231 var doc, node, nodePath;
1233 beforeEach(function() {
1234 doc = getDocumentFromXML('<div><test x="old"></test></div>');
1236 doc.registerExtension({elementNode: {transformations: {
1240 node = doc.root.contents()[0];
1241 nodePath = node.getPath();
1244 it('transforms as expected', function() {
1246 expect(node.getAttr('x')).to.equal('new');
1249 it('can be undone', function() {
1252 node = doc.getNodeByPath(nodePath);
1253 expect(node.getAttr('x')).to.equal('old');
1256 it('can be undone and then redone', function() {
1260 node = doc.getNodeByPath(nodePath);
1261 expect(node.getAttr('x')).to.equal('new');
1264 it('handles a sample scenario', function() {
1265 doc.root.contents()[0].test('1');
1266 doc.root.contents()[0].test('2');
1267 doc.root.contents()[0].test('3');
1268 doc.root.contents()[0].test('4');
1269 doc.root.contents()[0].test('5');
1271 expect(doc.root.contents()[0].getAttr('x')).to.equal('5', 'after initial transformations');
1273 expect(doc.root.contents()[0].getAttr('x')).to.equal('4', 'undo 1.1');
1275 expect(doc.root.contents()[0].getAttr('x')).to.equal('3', 'undo 1.2');
1277 expect(doc.root.contents()[0].getAttr('x')).to.equal('4', 'redo 1.1');
1279 expect(doc.root.contents()[0].getAttr('x')).to.equal('5', 'redo 1.2');
1281 expect(doc.root.contents()[0].getAttr('x')).to.equal('4', 'undo 2.1');
1282 doc.root.contents()[0].test('10');
1283 expect(doc.root.contents()[0].getAttr('x')).to.equal('10', 'additional transformation');
1284 expect(doc.redoStack.length).to.equal(0, 'transformation cleared redo stack');
1286 expect(doc.root.contents()[0].getAttr('x')).to.equal('10', 'empty redoStack so redo was noop');
1288 expect(doc.root.contents()[0].getAttr('x')).to.equal('4', 'undoing additional transformation');
1290 expect(doc.root.contents()[0].getAttr('x')).to.equal('10', 'redoing additional transformation');
1295 it('smoke tests nested transformations', function() {
1296 var doc = getDocumentFromXML('<div></div>');
1298 doc.registerExtension({elementNode: {transformations: {
1299 nested: function(v) {
1300 this._$.attr('innerAttr', v);
1301 this.triggerChangeEvent();
1303 outer: function(v) {
1305 this._$.attr('outerAttr', v);
1306 this.triggerChangeEvent();
1310 doc.root.outer('test1');
1311 doc.root.outer('test2');
1313 expect(doc.root.getAttr('innerAttr')).to.equal('test2');
1314 expect(doc.root.getAttr('outerAttr')).to.equal('test2');
1318 expect(doc.root.getAttr('innerAttr')).to.equal('test1');
1319 expect(doc.root.getAttr('outerAttr')).to.equal('test1');
1323 expect(doc.root.getAttr('innerAttr')).to.equal(undefined);
1324 expect(doc.root.getAttr('outerAttr')).to.equal(undefined);
1328 expect(doc.root.getAttr('innerAttr')).to.equal('test1');
1329 expect(doc.root.getAttr('outerAttr')).to.equal('test1');
1333 expect(doc.root.getAttr('innerAttr')).to.equal('test2');
1334 expect(doc.root.getAttr('outerAttr')).to.equal('test2');
1338 it('ignores transformation if document didn\'t emit change event', function() {
1339 var doc = getDocumentFromXML('<div></div>');
1341 doc.registerExtension({elementNode: {transformations: {
1348 expect(doc.undoStack.length).to.equal(0);
1352 describe('Transactions', function() {
1353 it('allows to undo/redo series of transformations at once', function() {
1354 var doc = getDocumentFromXML('<div></div>');
1356 doc.registerExtension({
1357 elementNode: {transformations: {
1359 this.setAttr('test', v);
1364 doc.startTransaction();
1368 doc.endTransaction();
1371 expect(doc.root.getAttr('test'), '1');
1373 expect(doc.root.getAttr('test'), '3');
1375 expect(doc.root.getAttr('test'), '1');
1377 expect(doc.root.getAttr('test'), '3');
1380 it('ignores empty transactions', function() {
1381 var doc = getDocumentFromXML('<div></div>');
1382 doc.startTransaction();
1383 doc.endTransaction();
1384 expect(doc.undoStack).to.have.length(0, 'empty transaction doesn\'t get pushed into undo stack');
1387 it('doesn\'t break on optimizations', function() {
1388 // This is a smoke test checking if optimizations made to transaction undoing
1389 // doesnt't break anything.
1390 var doc = getDocumentFromXML('<div smart="1" unaware="1"></div>');
1392 doc.registerExtension({
1393 elementNode: {transformations: {
1394 unaware: function(v) {
1395 this.setAttr('unware', v);
1396 this.triggerChangeEvent();
1399 impl: function(t, v) {
1400 t.oldVal = this.getAttr('smart');
1401 this.setAttr('smart', v);
1402 this.triggerChangeEvent();
1405 this.setAttr('smart', t.oldVal);
1406 this.triggerChangeEvent();
1412 doc.startTransaction();
1413 doc.root.smart('2');
1414 doc.root.unaware('2');
1415 doc.root.smart('3');
1416 doc.root.unaware('3');
1417 doc.endTransaction();
1421 expect(doc.root.getAttr('smart')).to.equal('1');
1422 expect(doc.root.getAttr('unaware')).to.equal('1');