5 ], function(chai, sinon, smartxml) {
9 /* global describe, it, beforeEach */
11 var expect = chai.expect;
14 var getDocumentFromXML = function(xml) {
15 return smartxml.documentFromXML(xml);
18 var elementNodeFromParams = function(params) {
19 return smartxml.elementNodeFromXML('<' + params.tag + '></' + params.tag + '>');
22 var elementNodeFromXML = function(xml) {
23 return smartxml.elementNodeFromXML(xml);
27 describe('smartxml', function() {
29 describe('Basic Document properties', function() {
30 it('exposes its root element', function() {
31 var doc = getDocumentFromXML('<div></div>');
32 expect(doc.root.getTagName()).to.equal('div');
35 it('can resets its content entirely', function() {
36 var doc = getDocumentFromXML('<div></div>');
38 expect(doc.root.getTagName()).to.equal('div');
40 doc.loadXML('<header></header>');
41 expect(doc.root.getTagName()).to.equal('header');
44 it('knows if it contains an ElementNode in its tree', function() {
45 var doc = getDocumentFromXML('<root><a></a>text</root>'),
47 a = root.contents()[0],
48 text = root.contents()[1];
50 expect(doc.containsNode(root)).to.equal(true, 'contains its root');
51 expect(doc.containsNode(a)).to.equal(true, 'contains Element Node');
52 expect(doc.containsNode(text)).to.equal(true, 'contains Text Node');
55 it('creates text nodes', function() {
56 var doc = getDocumentFromXML('<div></div>'),
57 emptyTextNode = doc.createDocumentNode({text:''}),
58 nonEmptyTextNode = doc.createDocumentNode({text: 'alice'});
59 expect(emptyTextNode.getText()).to.equal('', 'empty ok');
60 expect(nonEmptyTextNode.getText()).to.equal('alice', 'non empty ok');
64 describe('DocumentNode', function() {
65 it('can be cloned', function() {
66 var doc = getDocumentFromXML('<div>Alice</div>'),
67 text = doc.root.contents()[0],
70 [doc.root, text].forEach(function(node) {
71 suffix = ' (' + (node.nodeType === Node.TEXT_NODE ? 'text' : 'element') + ')';
73 expect(doc.containsNode(clone)).to.equal(false, 'clone is not contained in a document' + suffix);
74 expect(node.sameNode(clone)).to.equal(false, 'clone is not same node as its originator' + suffix);
75 expect(node.nativeNode.isEqualNode(clone.nativeNode)).to.equal(true, 'clone is identical as its originator' + suffix);
79 it('knows its path in the document tree', function() {
80 var doc = getDocumentFromXML('<root><a><b><c></c>text</b></a></root>'),
82 a = root.contents()[0],
84 text = b.contents()[1];
86 expect(root.getPath()).to.eql([], 'path of the root element is empty');
87 expect(a.getPath()).to.eql([0]);
88 expect(b.getPath()).to.eql([0, 0]);
89 expect(text.getPath()).to.eql([0,0,1]);
91 /* Paths relative to a given ancestor */
92 expect(text.getPath(root)).to.eql([0,0,1]);
93 expect(text.getPath(a)).to.eql([0,1]);
94 expect(text.getPath(b)).to.eql([1]);
98 describe('Basic ElementNode properties', function() {
99 it('exposes node contents', function() {
100 var node = elementNodeFromXML('<node>Some<node>text</node>is here</node>'),
101 contents = node.contents();
103 expect(contents).to.have.length(3);
104 expect(contents[0].nodeType).to.equal(Node.TEXT_NODE, 'text node 1');
105 expect(contents[1].nodeType).to.equal(Node.ELEMENT_NODE, 'element node 1');
106 expect(contents[2].nodeType).to.equal(Node.TEXT_NODE, 'text node 2');
109 describe('Storing custom data', function() {
112 beforeEach(function() {
113 node = elementNodeFromXML('<div></div>');
116 it('can append single value', function() {
117 node.setData('key', 'value');
118 expect(node.getData('key')).to.equal('value');
121 it('can overwrite the whole data', function() {
122 node.setData('key1', 'value1');
123 node.setData({key2: 'value2'});
124 expect(node.getData('key2')).to.equal('value2');
127 it('can fetch the whole data at once', function() {
128 node.setData({key1: 'value1', key2: 'value2'});
129 expect(node.getData()).to.eql({key1: 'value1', key2: 'value2'});
133 describe('Changing node tag', function() {
135 it('can change tag name', function() {
136 var node = elementNodeFromXML('<div></div>');
138 expect(node.getTagName()).to.equal('span');
141 it('emits nodeTagChange event', function() {
142 var node = elementNodeFromXML('<div></div>'),
145 node.document.on('change', spy);
147 var event = spy.args[0][0];
149 expect(event.type).to.equal('nodeTagChange');
150 expect(event.meta.node.sameNode(node)).to.be.true;
151 expect(event.meta.oldTagName).to.equal('div');
154 describe('Implementation specific expectations', function() {
155 // DOM specifies ElementNode tag as a read-only property, so
156 // changing it in a seamless way is a little bit tricky. For this reason
157 // the folowing expectations are required, despite the fact that they actually are
158 // motivated by implemetation details.
160 it('keeps node in the document', function() {
161 var doc = getDocumentFromXML('<div><header></header></div>'),
162 header = doc.root.contents()[0];
163 header.setTag('span');
164 expect(header.parent().sameNode(doc.root)).to.be.true;
166 it('keeps custom data', function() {
167 var node = elementNodeFromXML('<div></div>');
169 node.setData('key', 'value');
170 node.setTag('header');
172 expect(node.getTagName()).to.equal('header');
173 expect(node.getData()).to.eql({key: 'value'});
176 it('can change document root tag name', function() {
177 var doc = getDocumentFromXML('<div></div>');
178 doc.root.setTag('span');
179 expect(doc.root.getTagName()).to.equal('span');
182 it('keeps contents', function() {
183 var node = elementNodeFromXML('<div><div></div></div>');
184 node.setTag('header');
185 expect(node.contents()).to.have.length(1);
189 describe('Setting node attributes', function() {
190 it('can set node attribute', function() {
191 var node = elementNodeFromXML('<div></div>');
193 node.setAttr('key', 'value');
194 expect(node.getAttr('key')).to.equal('value');
196 it('emits nodeAttrChange event', function() {
197 var node = elementNodeFromXML('<div key="value1"></div>'),
200 node.document.on('change', spy);
201 node.setAttr('key', 'value2');
202 var event = spy.args[0][0];
204 expect(event.type).to.equal('nodeAttrChange');
205 expect(event.meta.node.sameNode(node)).to.be.true;
206 expect(event.meta.attr).to.equal('key');
207 expect(event.meta.oldVal).to.equal('value1');
214 describe('Basic TextNode properties', function() {
215 it('can have its text set', function() {
216 var node = elementNodeFromXML('<div>Alice</div>'),
217 textNode = node.contents()[0];
219 textNode.setText('Cat');
220 expect(textNode.getText()).to.equal('Cat');
223 it('emits nodeTextChange', function() {
224 var node = elementNodeFromXML('<div>Alice</div>'),
225 textNode = node.contents()[0],
228 textNode.document.on('change', spy);
229 textNode.setText('Cat');
231 var event = spy.args[0][0];
232 expect(event.type).to.equal('nodeTextChange');
235 it('puts NodeElement after itself', function() {
236 var node = elementNodeFromXML('<div>Alice</div>'),
237 textNode = node.contents()[0],
238 returned = textNode.after({tagName:'div'});
239 expect(returned.sameNode(node.contents()[1])).to.be.true;
242 it('puts NodeElement before itself', function() {
243 var node = elementNodeFromXML('<div>Alice</div>'),
244 textNode = node.contents()[0],
245 returned = textNode.before({tagName:'div'});
246 expect(returned.sameNode(node.contents()[0])).to.be.true;
249 describe('Wrapping TextNode contents', function() {
251 it('wraps DocumentTextElement', function() {
252 var node = elementNodeFromXML('<section>Alice</section>'),
253 textNode = node.contents()[0];
255 var returned = textNode.wrapWith({tagName: 'header'}),
256 parent = textNode.parent(),
257 parent2 = node.contents()[0];
259 expect(returned.sameNode(parent)).to.be.equal(true, 'wrapper is a parent');
260 expect(returned.sameNode(parent2)).to.be.equal(true, 'wrapper has a correct parent');
261 expect(returned.getTagName()).to.equal('header');
264 describe('wrapping part of DocumentTextElement', function() {
265 [{start: 5, end: 12}, {start: 12, end: 5}].forEach(function(offsets) {
266 it('wraps in the middle ' + offsets.start + '/' + offsets.end, function() {
267 var node = elementNodeFromXML('<section>Alice has a cat</section>'),
268 textNode = node.contents()[0];
270 var returned = textNode.wrapWith({tagName: 'header', attrs: {'attr1': 'value1'}, start: offsets.start, end: offsets.end}),
271 contents = node.contents();
273 expect(contents.length).to.equal(3);
275 expect(contents[0].nodeType).to.be.equal(Node.TEXT_NODE, 'first node is text node');
276 expect(contents[0].getText()).to.equal('Alice');
278 expect(contents[1].sameNode(returned)).to.be.true;
279 expect(returned.getTagName()).to.equal('header');
280 expect(returned.getAttr('attr1')).to.equal('value1');
281 expect(contents[1].contents().length).to.equal(1, 'wrapper has one node inside');
282 expect(contents[1].contents()[0].getText()).to.equal(' has a ');
284 expect(contents[2].nodeType).to.be.equal(Node.TEXT_NODE, 'third node is text node');
285 expect(contents[2].getText()).to.equal('cat');
289 it('wraps whole text inside DocumentTextElement if offsets span entire content', function() {
290 var node = elementNodeFromXML('<section>Alice has a cat</section>'),
291 textNode = node.contents()[0];
293 textNode.wrapWith({tagName: 'header', start: 0, end: 15});
295 var contents = node.contents();
296 expect(contents.length).to.equal(1);
297 expect(contents[0].getTagName()).to.equal('header');
298 expect(contents[0].contents()[0].getText()).to.equal('Alice has a cat');
305 describe('Manipulations', function() {
307 describe('replacing node with another one', function() {
308 it('replaces node with another one', function() {
309 var doc = getDocumentFromXML('<div><a></a></div>'),
310 a = doc.root.contents()[0];
312 var c = a.replaceWith({tagName: 'b', attrs: {b:'1'}});
314 expect(doc.root.contents()[0].sameNode(c));
315 expect(c.getTagName()).to.equal('b');
316 expect(c.getAttr('b')).to.equal('1');
318 it('can replace document root', function() {
319 var doc = getDocumentFromXML('<div></div>');
321 var header = doc.root.replaceWith({tagName: 'header'});
323 expect(doc.root.sameNode(header)).to.be.true;
324 expect(doc.containsNode(header)).to.be.true;
328 it('merges adjacent text nodes resulting from detaching an element node in between', function() {
329 var doc = getDocumentFromXML('<div>Alice <span>has</span>a cat</div>'),
330 span = doc.root.contents()[1];
334 var rootContents = doc.root.contents();
335 expect(rootContents).to.have.length(1, 'one child left');
336 expect(rootContents[0].getText()).to.equal('Alice a cat');
339 it('appends element node to another element node', function() {
340 var node1 = elementNodeFromParams({tag: 'div'}),
341 node2 = elementNodeFromParams({tag: 'a'}),
342 node3 = elementNodeFromParams({tag: 'p'});
345 expect(node1.contents()[0].sameNode(node2)).to.be.true;
346 expect(node1.contents()[1].sameNode(node3)).to.be.true;
349 it('prepends element node to another element node', function() {
350 var node1 = elementNodeFromParams({tag: 'div'}),
351 node2 = elementNodeFromParams({tag: 'a'}),
352 node3 = elementNodeFromParams({tag: 'p'});
353 node1.prepend(node2);
354 node1.prepend(node3);
355 expect(node1.contents()[0].sameNode(node3)).to.be.true;
356 expect(node1.contents()[1].sameNode(node2)).to.be.true;
359 it('wraps element node with another element node', function() {
360 var node = elementNodeFromXML('<div></div>'),
361 wrapper = elementNodeFromXML('<wrapper></wrapper>');
363 node.wrapWith(wrapper);
364 expect(node.parent().sameNode(wrapper)).to.be.true;
367 it('unwraps element node contents', function() {
368 var node = elementNodeFromXML('<div>Alice <div>has <span>propably</span> a cat</div>!</div>'),
369 outerDiv = node.contents()[1];
371 outerDiv.unwrapContent();
373 expect(node.contents().length).to.equal(3);
374 expect(node.contents()[0].getText()).to.equal('Alice has ');
375 expect(node.contents()[1].getTagName()).to.equal('span');
376 expect(node.contents()[2].getText()).to.equal(' a cat!');
379 it('unwrap single element node from its parent', function() {
380 var doc = getDocumentFromXML('<div><a><b></b></a></div>'),
382 a = div.contents()[0],
385 var parent = b.unwrap();
387 expect(parent.sameNode(div)).to.equal(true, 'returns new parent');
388 expect(div.contents()).to.have.length(1, 'root contains only one node');
389 expect(div.contents()[0].sameNode(b)).to.equal(true, 'node got unwrapped');
392 it('unwrap single text node from its parent', function() {
393 var doc = getDocumentFromXML('<div>Some <span>text</span>!</div>'),
395 span = div.contents()[1],
396 text = span.contents()[0];
398 var parent = text.unwrap();
400 expect(parent.sameNode(div)).to.equal(true, 'returns new parent');
401 expect(div.contents()).to.have.length(1, 'root contains only one node');
402 expect(div.contents()[0].getText()).to.equal('Some text!');
405 describe('Wrapping text', function() {
406 it('wraps text spanning multiple sibling TextNodes', function() {
407 var section = elementNodeFromXML('<section>Alice has a <span>small</span> cat</section>'),
408 wrapper = section.wrapText({
409 _with: {tagName: 'span', attrs: {'attr1': 'value1'}},
415 expect(section.contents().length).to.equal(2);
416 expect(section.contents()[0].nodeType).to.equal(Node.TEXT_NODE);
417 expect(section.contents()[0].getText()).to.equal('Alice ');
419 var wrapper2 = section.contents()[1];
420 expect(wrapper2.sameNode(wrapper)).to.be.true;
421 expect(wrapper.getTagName()).to.equal('span');
423 var wrapperContents = wrapper.contents();
424 expect(wrapperContents.length).to.equal(3);
425 expect(wrapperContents[0].getText()).to.equal('has a ');
427 expect(wrapperContents[1].nodeType).to.equal(Node.ELEMENT_NODE);
428 expect(wrapperContents[1].contents().length).to.equal(1);
429 expect(wrapperContents[1].contents()[0].getText()).to.equal('small');
433 describe('Wrapping Nodes', function() {
434 it('wraps multiple sibling nodes', function() {
435 var section = elementNodeFromXML('<section>Alice<div>has</div><div>a cat</div></section>'),
436 aliceText = section.contents()[0],
437 firstDiv = section.contents()[1],
438 lastDiv = section.contents()[section.contents().length -1];
440 var returned = section.document.wrapNodes({
443 _with: {tagName: 'header'}
446 var sectionContentss = section.contents(),
447 header = sectionContentss[0],
448 headerContents = header.contents();
450 expect(sectionContentss).to.have.length(1);
451 expect(header.sameNode(returned)).to.equal(true, 'wrapper returned');
452 expect(header.parent().sameNode(section)).to.be.true;
453 expect(headerContents).to.have.length(3);
454 expect(headerContents[0].sameNode(aliceText)).to.equal(true, 'first node wrapped');
455 expect(headerContents[1].sameNode(firstDiv)).to.equal(true, 'second node wrapped');
456 expect(headerContents[2].sameNode(lastDiv)).to.equal(true, 'third node wrapped');
459 it('wraps multiple sibling Elements - middle case', function() {
460 var section = elementNodeFromXML('<section><div></div><div></div><div></div><div></div></section>'),
461 div2 = section.contents()[1],
462 div3 = section.contents()[2];
464 section.document.wrapNodes({
467 _with: {tagName: 'header'}
470 var sectionContentss = section.contents(),
471 header = sectionContentss[1],
472 headerChildren = header.contents();
474 expect(sectionContentss).to.have.length(3);
475 expect(headerChildren).to.have.length(2);
476 expect(headerChildren[0].sameNode(div2)).to.equal(true, 'first node wrapped');
477 expect(headerChildren[1].sameNode(div3)).to.equal(true, 'second node wrapped');
483 describe('Splitting text', function() {
485 it('splits TextNode\'s parent into two ElementNodes', function() {
486 var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
488 text = section.contents()[0].contents()[0];
490 var returnedValue = text.split({offset: 5});
491 expect(section.contents().length).to.equal(2, 'section has two children');
493 var header1 = section.contents()[0];
494 var header2 = section.contents()[1];
496 expect(header1.getTagName()).to.equal('header', 'first section child ok');
497 expect(header1.contents().length).to.equal(1, 'first header has one child');
498 expect(header1.contents()[0].getText()).to.equal('Some ', 'first header has correct content');
499 expect(header2.getTagName()).to.equal('header', 'second section child ok');
500 expect(header2.contents().length).to.equal(1, 'second header has one child');
501 expect(header2.contents()[0].getText()).to.equal('header', 'second header has correct content');
503 expect(returnedValue.first.sameNode(header1)).to.equal(true, 'first node returned');
504 expect(returnedValue.second.sameNode(header2)).to.equal(true, 'second node returned');
507 it('leaves empty copy of ElementNode if splitting at the very beginning', function() {
508 var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
510 text = section.contents()[0].contents()[0];
512 text.split({offset: 0});
514 var header1 = section.contents()[0];
515 var header2 = section.contents()[1];
517 expect(header1.contents().length).to.equal(0);
518 expect(header2.contents()[0].getText()).to.equal('Some header');
521 it('leaves empty copy of ElementNode if splitting at the very end', function() {
522 var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
524 text = section.contents()[0].contents()[0];
526 text.split({offset: 11});
528 var header1 = section.contents()[0];
529 var header2 = section.contents()[1];
531 expect(header1.contents()[0].getText()).to.equal('Some header');
532 expect(header2.contents().length).to.equal(0);
535 it('keeps TextNodes\'s parent\'s children elements intact', function() {
536 var doc = getDocumentFromXML('<section><header>A <span>fancy</span> and <span>nice</span> header</header></section>'),
538 header = section.contents()[0],
539 textAnd = header.contents()[2];
541 textAnd.split({offset: 2});
543 var sectionContents = section.contents();
544 expect(sectionContents.length).to.equal(2, 'Section has two children');
545 expect(sectionContents[0].getTagName()).to.equal('header', 'First section node is a header');
546 expect(sectionContents[1].getTagName()).to.equal('header', 'Second section node is a header');
548 var firstHeaderContents = sectionContents[0].contents();
549 expect(firstHeaderContents.length).to.equal(3, 'First header has three children');
550 expect(firstHeaderContents[0].getText()).to.equal('A ', 'First header starts with a text');
551 expect(firstHeaderContents[1].getTagName()).to.equal('span', 'First header has span in the middle');
552 expect(firstHeaderContents[2].getText()).to.equal(' a', 'First header ends with text');
554 var secondHeaderContents = sectionContents[1].contents();
555 expect(secondHeaderContents.length).to.equal(3, 'Second header has three children');
556 expect(secondHeaderContents[0].getText()).to.equal('nd ', 'Second header starts with text');
557 expect(secondHeaderContents[1].getTagName()).to.equal('span', 'Second header has span in the middle');
558 expect(secondHeaderContents[2].getText()).to.equal(' header', 'Second header ends with text');
562 describe('Events', function() {
563 it('emits nodeDetached event on node detach', function() {
564 var node = elementNodeFromXML('<div><div></div></div>'),
565 innerNode = node.contents()[0],
567 node.document.on('change', spy);
569 var detached = innerNode.detach(),
570 event = spy.args[0][0];
572 expect(event.type).to.equal('nodeDetached');
573 expect(event.meta.node.sameNode(detached, 'detached node in event meta'));
574 expect(event.meta.parent.sameNode(node), 'original parent node in event meta');
577 it('emits nodeAdded event when appending new node', function() {
578 var node = elementNodeFromXML('<div></div>'),
580 node.document.on('change', spy);
582 var appended = node.append({tagName:'div'}),
583 event = spy.args[0][0];
584 expect(event.type).to.equal('nodeAdded');
585 expect(event.meta.node.sameNode(appended)).to.be.true;
588 it('emits nodeMoved when appending aready existing node', function() {
589 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
590 a = node.contents()[0],
591 b = node.contents()[1],
593 node.document.on('change', spy);
595 var appended = a.append(b),
596 event = spy.args[0][0];
598 expect(spy.callCount).to.equal(1);
599 expect(event.type).to.equal('nodeMoved');
600 expect(event.meta.node.sameNode(appended)).to.be.true;
603 it('emits nodeAdded event when prepending new node', function() {
604 var node = elementNodeFromXML('<div></div>'),
606 node.document.on('change', spy);
608 var prepended = node.prepend({tagName:'div'}),
609 event = spy.args[0][0];
610 expect(event.type).to.equal('nodeAdded');
611 expect(event.meta.node.sameNode(prepended)).to.be.true;
614 it('emits nodeMoved when prepending aready existing node', function() {
615 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
616 a = node.contents()[0],
617 b = node.contents()[1],
619 node.document.on('change', spy);
621 var prepended = a.prepend(b),
622 event = spy.args[0][0];
623 expect(spy.callCount).to.equal(1);
624 expect(event.type).to.equal('nodeMoved');
625 expect(event.meta.node.sameNode(prepended)).to.be.true;
628 it('emits nodeAdded event when inserting node after another', function() {
629 var node = elementNodeFromXML('<div><a></a></div>').contents()[0],
631 node.document.on('change', spy);
633 var inserted = node.after({tagName:'div'}),
634 event = spy.args[0][0];
635 expect(event.type).to.equal('nodeAdded');
636 expect(event.meta.node.sameNode(inserted)).to.be.true;
639 it('emits nodeMoved when inserting aready existing node after another', function() {
640 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
641 a = node.contents()[0],
642 b = node.contents()[1],
644 node.document.on('change', spy);
645 var inserted = b.after(a),
646 event = spy.args[0][0];
648 expect(spy.callCount).to.equal(1);
649 expect(event.type).to.equal('nodeMoved');
650 expect(event.meta.node.sameNode(inserted)).to.be.true;
653 it('emits nodeAdded event when inserting node before another', function() {
654 var node = elementNodeFromXML('<div><a></a></div>').contents()[0],
656 node.document.on('change', spy);
658 var inserted = node.before({tagName:'div'}),
659 event = spy.args[0][0];
660 expect(event.type).to.equal('nodeAdded');
661 expect(event.meta.node.sameNode(inserted)).to.be.true;
664 it('emits nodeAdded when inserting aready existing node before another', function() {
665 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
666 a = node.contents()[0],
667 b = node.contents()[1],
669 node.document.on('change', spy);
670 var inserted = a.before(b),
671 event = spy.args[0][0];
673 expect(spy.callCount).to.equal(1);
674 expect(event.type).to.equal('nodeMoved');
675 expect(event.meta.node.sameNode(inserted)).to.be.true;
678 it('emits nodeDetached and nodeAdded when replacing root node with another', function() {
679 var doc = getDocumentFromXML('<a></a>'),
683 doc.on('change', spy);
685 doc.root.replaceWith({tagName: 'b'});
687 expect(spy.callCount).to.equal(2);
689 var event1 = spy.args[0][0],
690 event2 = spy.args[1][0];
692 expect(event1.type).to.equal('nodeDetached');
693 expect(event1.meta.node.sameNode(oldRoot)).to.equal(true, 'root node in nodeDetached event metadata');
694 expect(event2.type).to.equal('nodeAdded');
695 expect(event2.meta.node.sameNode(doc.root)).to.equal(true, 'new root node in nodelAdded event meta');
699 describe('Traversing', function() {
700 describe('Basic', function() {
701 it('can access node parent', function() {
702 var doc = getDocumentFromXML('<a><b></b></a>'),
706 expect(a.parent()).to.equal(null, 'parent of a root is null');
707 expect(b.parent().sameNode(a)).to.be.true;
709 it('can access node parents', function() {
710 var doc = getDocumentFromXML('<a><b><c></c></b></a>'),
715 var parents = c.parents();
716 expect(parents).to.eql([b,a]);
720 describe('finding sibling parents of two elements', function() {
721 it('returns elements themself if they have direct common parent', function() {
722 var doc = getDocumentFromXML('<section><div><div>A</div><div>B</div></div></section>'),
723 wrappingDiv = doc.root.contents()[0],
724 divA = wrappingDiv.contents()[0],
725 divB = wrappingDiv.contents()[1];
727 var siblingParents = doc.getSiblingParents({node1: divA, node2: divB});
729 expect(siblingParents.node1.sameNode(divA)).to.equal(true, 'divA');
730 expect(siblingParents.node2.sameNode(divB)).to.equal(true, 'divB');
733 it('returns sibling parents - example 1', function() {
734 var doc = getDocumentFromXML('<section>Alice <span>has a cat</span></section>'),
735 aliceText = doc.root.contents()[0],
736 span = doc.root.contents()[1],
737 spanText = span.contents()[0];
739 var siblingParents = doc.getSiblingParents({node1: aliceText, node2: spanText});
741 expect(siblingParents.node1.sameNode(aliceText)).to.equal(true, 'aliceText');
742 expect(siblingParents.node2.sameNode(span)).to.equal(true, 'span');
747 describe('Serializing document to WLXML', function() {
748 it('keeps document intact when no changes have been made', function() {
749 var xmlIn = '<section>Alice<div>has</div>a <span class="uri" meta-uri="http://cat.com">cat</span>!</section>',
750 doc = getDocumentFromXML(xmlIn),
751 xmlOut = doc.toXML();
753 var parser = new DOMParser(),
754 input = parser.parseFromString(xmlIn, 'application/xml').childNodes[0],
755 output = parser.parseFromString(xmlOut, 'application/xml').childNodes[0];
757 expect(input.isEqualNode(output)).to.be.true;
760 it('keeps entities intact', function() {
761 var xmlIn = '<section>< ></section>',
762 doc = getDocumentFromXML(xmlIn),
763 xmlOut = doc.toXML();
764 expect(xmlOut).to.equal(xmlIn);
766 it('keeps entities intact when they form html/xml', function() {
767 var xmlIn = '<section><abc></section>',
768 doc = getDocumentFromXML(xmlIn),
769 xmlOut = doc.toXML();
770 expect(xmlOut).to.equal(xmlIn);