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('Basic ElementNode properties', function() {
65 it('exposes node contents', function() {
66 var node = elementNodeFromXML('<node>Some<node>text</node>is here</node>'),
67 contents = node.contents();
69 expect(contents).to.have.length(3);
70 expect(contents[0].nodeType).to.equal(Node.TEXT_NODE, 'text node 1');
71 expect(contents[1].nodeType).to.equal(Node.ELEMENT_NODE, 'element node 1');
72 expect(contents[2].nodeType).to.equal(Node.TEXT_NODE, 'text node 2');
75 describe('Storing custom data', function() {
78 beforeEach(function() {
79 node = elementNodeFromXML('<div></div>');
82 it('can append single value', function() {
83 node.setData('key', 'value');
84 expect(node.getData('key')).to.equal('value');
87 it('can overwrite the whole data', function() {
88 node.setData('key1', 'value1');
89 node.setData({key2: 'value2'});
90 expect(node.getData('key2')).to.equal('value2');
93 it('can fetch the whole data at once', function() {
94 node.setData({key1: 'value1', key2: 'value2'});
95 expect(node.getData()).to.eql({key1: 'value1', key2: 'value2'});
99 describe('Changing node tag', function() {
101 it('can change tag name', function() {
102 var node = elementNodeFromXML('<div></div>');
104 expect(node.getTagName()).to.equal('span');
107 it('emits nodeTagChange event', function() {
108 var node = elementNodeFromXML('<div></div>'),
111 node.document.on('change', spy);
113 var event = spy.args[0][0];
115 expect(event.type).to.equal('nodeTagChange');
116 expect(event.meta.node.sameNode(node)).to.be.true;
117 expect(event.meta.oldTagName).to.equal('div');
120 describe('Implementation specific expectations', function() {
121 // DOM specifies ElementNode tag as a read-only property, so
122 // changing it in a seamless way is a little bit tricky. For this reason
123 // the folowing expectations are required, despite the fact that they actually are
124 // motivated by implemetation details.
126 it('keeps node in the document', function() {
127 var doc = getDocumentFromXML('<div><header></header></div>'),
128 header = doc.root.contents()[0];
129 header.setTag('span');
130 expect(header.parent().sameNode(doc.root)).to.be.true;
132 it('keeps custom data', function() {
133 var node = elementNodeFromXML('<div></div>');
135 node.setData('key', 'value');
136 node.setTag('header');
138 expect(node.getTagName()).to.equal('header');
139 expect(node.getData()).to.eql({key: 'value'});
142 it('can change document root tag name', function() {
143 var doc = getDocumentFromXML('<div></div>');
144 doc.root.setTag('span');
145 expect(doc.root.getTagName()).to.equal('span');
148 it('keeps contents', function() {
149 var node = elementNodeFromXML('<div><div></div></div>');
150 node.setTag('header');
151 expect(node.contents()).to.have.length(1);
155 describe('Setting node attributes', function() {
156 it('can set node attribute', function() {
157 var node = elementNodeFromXML('<div></div>');
159 node.setAttr('key', 'value');
160 expect(node.getAttr('key')).to.equal('value');
162 it('emits nodeAttrChange event', function() {
163 var node = elementNodeFromXML('<div key="value1"></div>'),
166 node.document.on('change', spy);
167 node.setAttr('key', 'value2');
168 var event = spy.args[0][0];
170 expect(event.type).to.equal('nodeAttrChange');
171 expect(event.meta.node.sameNode(node)).to.be.true;
172 expect(event.meta.attr).to.equal('key');
173 expect(event.meta.oldVal).to.equal('value1');
180 describe('Basic TextNode properties', function() {
181 it('can have its text set', function() {
182 var node = elementNodeFromXML('<div>Alice</div>'),
183 textNode = node.contents()[0];
185 textNode.setText('Cat');
186 expect(textNode.getText()).to.equal('Cat');
189 it('emits nodeTextChange', function() {
190 var node = elementNodeFromXML('<div>Alice</div>'),
191 textNode = node.contents()[0],
194 textNode.document.on('change', spy);
195 textNode.setText('Cat');
197 var event = spy.args[0][0];
198 expect(event.type).to.equal('nodeTextChange');
201 it('puts NodeElement after itself', function() {
202 var node = elementNodeFromXML('<div>Alice</div>'),
203 textNode = node.contents()[0],
204 returned = textNode.after({tagName:'div'});
205 expect(returned.sameNode(node.contents()[1])).to.be.true;
208 it('puts NodeElement before itself', function() {
209 var node = elementNodeFromXML('<div>Alice</div>'),
210 textNode = node.contents()[0],
211 returned = textNode.before({tagName:'div'});
212 expect(returned.sameNode(node.contents()[0])).to.be.true;
215 describe('Wrapping TextNode contents', function() {
217 it('wraps DocumentTextElement', function() {
218 var node = elementNodeFromXML('<section>Alice</section>'),
219 textNode = node.contents()[0];
221 var returned = textNode.wrapWith({tagName: 'header'}),
222 parent = textNode.parent(),
223 parent2 = node.contents()[0];
225 expect(returned.sameNode(parent)).to.be.equal(true, 'wrapper is a parent');
226 expect(returned.sameNode(parent2)).to.be.equal(true, 'wrapper has a correct parent');
227 expect(returned.getTagName()).to.equal('header');
230 describe('wrapping part of DocumentTextElement', function() {
231 [{start: 5, end: 12}, {start: 12, end: 5}].forEach(function(offsets) {
232 it('wraps in the middle ' + offsets.start + '/' + offsets.end, function() {
233 var node = elementNodeFromXML('<section>Alice has a cat</section>'),
234 textNode = node.contents()[0];
236 var returned = textNode.wrapWith({tagName: 'header', attrs: {'attr1': 'value1'}, start: offsets.start, end: offsets.end}),
237 contents = node.contents();
239 expect(contents.length).to.equal(3);
241 expect(contents[0].nodeType).to.be.equal(Node.TEXT_NODE, 'first node is text node');
242 expect(contents[0].getText()).to.equal('Alice');
244 expect(contents[1].sameNode(returned)).to.be.true;
245 expect(returned.getTagName()).to.equal('header');
246 expect(returned.getAttr('attr1')).to.equal('value1');
247 expect(contents[1].contents().length).to.equal(1, 'wrapper has one node inside');
248 expect(contents[1].contents()[0].getText()).to.equal(' has a ');
250 expect(contents[2].nodeType).to.be.equal(Node.TEXT_NODE, 'third node is text node');
251 expect(contents[2].getText()).to.equal('cat');
255 it('wraps whole text inside DocumentTextElement if offsets span entire content', function() {
256 var node = elementNodeFromXML('<section>Alice has a cat</section>'),
257 textNode = node.contents()[0];
259 textNode.wrapWith({tagName: 'header', start: 0, end: 15});
261 var contents = node.contents();
262 expect(contents.length).to.equal(1);
263 expect(contents[0].getTagName()).to.equal('header');
264 expect(contents[0].contents()[0].getText()).to.equal('Alice has a cat');
271 describe('Manipulations', function() {
273 it('merges adjacent text nodes resulting from detaching an element node in between', function() {
274 var doc = getDocumentFromXML('<div>Alice <span>has</span>a cat</div>'),
275 span = doc.root.contents()[1];
279 var rootContents = doc.root.contents();
280 expect(rootContents).to.have.length(1, 'one child left');
281 expect(rootContents[0].getText()).to.equal('Alice a cat');
284 it('appends element node to another element node', function() {
285 var node1 = elementNodeFromParams({tag: 'div'}),
286 node2 = elementNodeFromParams({tag: 'a'}),
287 node3 = elementNodeFromParams({tag: 'p'});
290 expect(node1.contents()[0].sameNode(node2)).to.be.true;
291 expect(node1.contents()[1].sameNode(node3)).to.be.true;
294 it('prepends element node to another element node', function() {
295 var node1 = elementNodeFromParams({tag: 'div'}),
296 node2 = elementNodeFromParams({tag: 'a'}),
297 node3 = elementNodeFromParams({tag: 'p'});
298 node1.prepend(node2);
299 node1.prepend(node3);
300 expect(node1.contents()[0].sameNode(node3)).to.be.true;
301 expect(node1.contents()[1].sameNode(node2)).to.be.true;
304 it('wraps element node with another element node', function() {
305 var node = elementNodeFromXML('<div></div>'),
306 wrapper = elementNodeFromXML('<wrapper></wrapper>');
308 node.wrapWith(wrapper);
309 expect(node.parent().sameNode(wrapper)).to.be.true;
312 it('unwraps element node contents', function() {
313 var node = elementNodeFromXML('<div>Alice <div>has <span>propably</span> a cat</div>!</div>'),
314 outerDiv = node.contents()[1];
316 outerDiv.unwrapContent();
318 expect(node.contents().length).to.equal(3);
319 expect(node.contents()[0].getText()).to.equal('Alice has ');
320 expect(node.contents()[1].getTagName()).to.equal('span');
321 expect(node.contents()[2].getText()).to.equal(' a cat!');
324 it('unwrap single element node from its parent', function() {
325 var doc = getDocumentFromXML('<div><a><b></b></a></div>'),
327 a = div.contents()[0],
330 var parent = b.unwrap();
332 expect(parent.sameNode(div)).to.equal(true, 'returns new parent');
333 expect(div.contents()).to.have.length(1, 'root contains only one node');
334 expect(div.contents()[0].sameNode(b)).to.equal(true, 'node got unwrapped');
337 it('unwrap single text node from its parent', function() {
338 var doc = getDocumentFromXML('<div>Some <span>text</span>!</div>'),
340 span = div.contents()[1],
341 text = span.contents()[0];
343 var parent = text.unwrap();
345 expect(parent.sameNode(div)).to.equal(true, 'returns new parent');
346 expect(div.contents()).to.have.length(1, 'root contains only one node');
347 expect(div.contents()[0].getText()).to.equal('Some text!');
350 describe('Wrapping text', function() {
351 it('wraps text spanning multiple sibling TextNodes', function() {
352 var section = elementNodeFromXML('<section>Alice has a <span>small</span> cat</section>'),
353 wrapper = section.wrapText({
354 _with: {tagName: 'span', attrs: {'attr1': 'value1'}},
360 expect(section.contents().length).to.equal(2);
361 expect(section.contents()[0].nodeType).to.equal(Node.TEXT_NODE);
362 expect(section.contents()[0].getText()).to.equal('Alice ');
364 var wrapper2 = section.contents()[1];
365 expect(wrapper2.sameNode(wrapper)).to.be.true;
366 expect(wrapper.getTagName()).to.equal('span');
368 var wrapperContents = wrapper.contents();
369 expect(wrapperContents.length).to.equal(3);
370 expect(wrapperContents[0].getText()).to.equal('has a ');
372 expect(wrapperContents[1].nodeType).to.equal(Node.ELEMENT_NODE);
373 expect(wrapperContents[1].contents().length).to.equal(1);
374 expect(wrapperContents[1].contents()[0].getText()).to.equal('small');
378 describe('Wrapping Nodes', function() {
379 it('wraps multiple sibling nodes', function() {
380 var section = elementNodeFromXML('<section>Alice<div>has</div><div>a cat</div></section>'),
381 aliceText = section.contents()[0],
382 firstDiv = section.contents()[1],
383 lastDiv = section.contents()[section.contents().length -1];
385 var returned = section.document.wrapNodes({
388 _with: {tagName: 'header'}
391 var sectionContentss = section.contents(),
392 header = sectionContentss[0],
393 headerContents = header.contents();
395 expect(sectionContentss).to.have.length(1);
396 expect(header.sameNode(returned)).to.equal(true, 'wrapper returned');
397 expect(header.parent().sameNode(section)).to.be.true;
398 expect(headerContents).to.have.length(3);
399 expect(headerContents[0].sameNode(aliceText)).to.equal(true, 'first node wrapped');
400 expect(headerContents[1].sameNode(firstDiv)).to.equal(true, 'second node wrapped');
401 expect(headerContents[2].sameNode(lastDiv)).to.equal(true, 'third node wrapped');
404 it('wraps multiple sibling Elements - middle case', function() {
405 var section = elementNodeFromXML('<section><div></div><div></div><div></div><div></div></section>'),
406 div2 = section.contents()[1],
407 div3 = section.contents()[2];
409 section.document.wrapNodes({
412 _with: {tagName: 'header'}
415 var sectionContentss = section.contents(),
416 header = sectionContentss[1],
417 headerChildren = header.contents();
419 expect(sectionContentss).to.have.length(3);
420 expect(headerChildren).to.have.length(2);
421 expect(headerChildren[0].sameNode(div2)).to.equal(true, 'first node wrapped');
422 expect(headerChildren[1].sameNode(div3)).to.equal(true, 'second node wrapped');
428 describe('Splitting text', function() {
430 it('splits TextNode\'s parent into two ElementNodes', function() {
431 var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
433 text = section.contents()[0].contents()[0];
435 var returnedValue = text.split({offset: 5});
436 expect(section.contents().length).to.equal(2, 'section has two children');
438 var header1 = section.contents()[0];
439 var header2 = section.contents()[1];
441 expect(header1.getTagName()).to.equal('header', 'first section child ok');
442 expect(header1.contents().length).to.equal(1, 'first header has one child');
443 expect(header1.contents()[0].getText()).to.equal('Some ', 'first header has correct content');
444 expect(header2.getTagName()).to.equal('header', 'second section child ok');
445 expect(header2.contents().length).to.equal(1, 'second header has one child');
446 expect(header2.contents()[0].getText()).to.equal('header', 'second header has correct content');
448 expect(returnedValue.first.sameNode(header1)).to.equal(true, 'first node returned');
449 expect(returnedValue.second.sameNode(header2)).to.equal(true, 'second node returned');
452 it('leaves empty copy of ElementNode if splitting at the very beginning', function() {
453 var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
455 text = section.contents()[0].contents()[0];
457 text.split({offset: 0});
459 var header1 = section.contents()[0];
460 var header2 = section.contents()[1];
462 expect(header1.contents().length).to.equal(0);
463 expect(header2.contents()[0].getText()).to.equal('Some header');
466 it('leaves empty copy of ElementNode if splitting at the very end', function() {
467 var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
469 text = section.contents()[0].contents()[0];
471 text.split({offset: 11});
473 var header1 = section.contents()[0];
474 var header2 = section.contents()[1];
476 expect(header1.contents()[0].getText()).to.equal('Some header');
477 expect(header2.contents().length).to.equal(0);
480 it('keeps TextNodes\'s parent\'s children elements intact', function() {
481 var doc = getDocumentFromXML('<section><header>A <span>fancy</span> and <span>nice</span> header</header></section>'),
483 header = section.contents()[0],
484 textAnd = header.contents()[2];
486 textAnd.split({offset: 2});
488 var sectionContents = section.contents();
489 expect(sectionContents.length).to.equal(2, 'Section has two children');
490 expect(sectionContents[0].getTagName()).to.equal('header', 'First section node is a header');
491 expect(sectionContents[1].getTagName()).to.equal('header', 'Second section node is a header');
493 var firstHeaderContents = sectionContents[0].contents();
494 expect(firstHeaderContents.length).to.equal(3, 'First header has three children');
495 expect(firstHeaderContents[0].getText()).to.equal('A ', 'First header starts with a text');
496 expect(firstHeaderContents[1].getTagName()).to.equal('span', 'First header has span in the middle');
497 expect(firstHeaderContents[2].getText()).to.equal(' a', 'First header ends with text');
499 var secondHeaderContents = sectionContents[1].contents();
500 expect(secondHeaderContents.length).to.equal(3, 'Second header has three children');
501 expect(secondHeaderContents[0].getText()).to.equal('nd ', 'Second header starts with text');
502 expect(secondHeaderContents[1].getTagName()).to.equal('span', 'Second header has span in the middle');
503 expect(secondHeaderContents[2].getText()).to.equal(' header', 'Second header ends with text');
507 describe('Events', function() {
508 it('emits nodeDetached event on node detach', function() {
509 var node = elementNodeFromXML('<div><div></div></div>'),
510 innerNode = node.contents()[0],
512 node.document.on('change', spy);
514 var detached = innerNode.detach(),
515 event = spy.args[0][0];
517 expect(event.type).to.equal('nodeDetached');
518 expect(event.meta.node.sameNode(detached, 'detached node in event meta'));
519 expect(event.meta.parent.sameNode(node), 'original parent node in event meta');
522 it('emits nodeAdded event when appending new node', function() {
523 var node = elementNodeFromXML('<div></div>'),
525 node.document.on('change', spy);
527 var appended = node.append({tagName:'div'}),
528 event = spy.args[0][0];
529 expect(event.type).to.equal('nodeAdded');
530 expect(event.meta.node.sameNode(appended)).to.be.true;
533 it('emits nodeMoved when appending aready existing node', function() {
534 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
535 a = node.contents()[0],
536 b = node.contents()[1],
538 node.document.on('change', spy);
540 var appended = a.append(b),
541 event = spy.args[0][0];
543 expect(spy.callCount).to.equal(1);
544 expect(event.type).to.equal('nodeMoved');
545 expect(event.meta.node.sameNode(appended)).to.be.true;
548 it('emits nodeAdded event when prepending new node', function() {
549 var node = elementNodeFromXML('<div></div>'),
551 node.document.on('change', spy);
553 var prepended = node.prepend({tagName:'div'}),
554 event = spy.args[0][0];
555 expect(event.type).to.equal('nodeAdded');
556 expect(event.meta.node.sameNode(prepended)).to.be.true;
559 it('emits nodeMoved when prepending aready existing node', function() {
560 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
561 a = node.contents()[0],
562 b = node.contents()[1],
564 node.document.on('change', spy);
566 var prepended = a.prepend(b),
567 event = spy.args[0][0];
568 expect(spy.callCount).to.equal(1);
569 expect(event.type).to.equal('nodeMoved');
570 expect(event.meta.node.sameNode(prepended)).to.be.true;
573 it('emits nodeAdded event when inserting node after another', function() {
574 var node = elementNodeFromXML('<div><a></a></div>').contents()[0],
576 node.document.on('change', spy);
578 var inserted = node.after({tagName:'div'}),
579 event = spy.args[0][0];
580 expect(event.type).to.equal('nodeAdded');
581 expect(event.meta.node.sameNode(inserted)).to.be.true;
584 it('emits nodeMoved when inserting aready existing node after another', function() {
585 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
586 a = node.contents()[0],
587 b = node.contents()[1],
589 node.document.on('change', spy);
590 var inserted = b.after(a),
591 event = spy.args[0][0];
593 expect(spy.callCount).to.equal(1);
594 expect(event.type).to.equal('nodeMoved');
595 expect(event.meta.node.sameNode(inserted)).to.be.true;
598 it('emits nodeAdded event when inserting node before another', function() {
599 var node = elementNodeFromXML('<div><a></a></div>').contents()[0],
601 node.document.on('change', spy);
603 var inserted = node.before({tagName:'div'}),
604 event = spy.args[0][0];
605 expect(event.type).to.equal('nodeAdded');
606 expect(event.meta.node.sameNode(inserted)).to.be.true;
609 it('emits nodeAdded when inserting aready existing node before another', function() {
610 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
611 a = node.contents()[0],
612 b = node.contents()[1],
614 node.document.on('change', spy);
615 var inserted = a.before(b),
616 event = spy.args[0][0];
618 expect(spy.callCount).to.equal(1);
619 expect(event.type).to.equal('nodeMoved');
620 expect(event.meta.node.sameNode(inserted)).to.be.true;
624 describe('Traversing', function() {
625 describe('Basic', function() {
626 it('can access node parent', function() {
627 var doc = getDocumentFromXML('<a><b></b></a>'),
631 expect(a.parent()).to.equal(null, 'parent of a root is null');
632 expect(b.parent().sameNode(a)).to.be.true;
634 it('can access node parents', function() {
635 var doc = getDocumentFromXML('<a><b><c></c></b></a>'),
640 var parents = c.parents();
641 expect(parents).to.eql([b,a]);
645 describe('finding sibling parents of two elements', function() {
646 it('returns elements themself if they have direct common parent', function() {
647 var doc = getDocumentFromXML('<section><div><div>A</div><div>B</div></div></section>'),
648 wrappingDiv = doc.root.contents()[0],
649 divA = wrappingDiv.contents()[0],
650 divB = wrappingDiv.contents()[1];
652 var siblingParents = doc.getSiblingParents({node1: divA, node2: divB});
654 expect(siblingParents.node1.sameNode(divA)).to.equal(true, 'divA');
655 expect(siblingParents.node2.sameNode(divB)).to.equal(true, 'divB');
658 it('returns sibling parents - example 1', function() {
659 var doc = getDocumentFromXML('<section>Alice <span>has a cat</span></section>'),
660 aliceText = doc.root.contents()[0],
661 span = doc.root.contents()[1],
662 spanText = span.contents()[0];
664 var siblingParents = doc.getSiblingParents({node1: aliceText, node2: spanText});
666 expect(siblingParents.node1.sameNode(aliceText)).to.equal(true, 'aliceText');
667 expect(siblingParents.node2.sameNode(span)).to.equal(true, 'span');
672 describe('Serializing document to WLXML', function() {
673 it('keeps document intact when no changes have been made', function() {
674 var xmlIn = '<section>Alice<div>has</div>a <span class="uri" meta-uri="http://cat.com">cat</span>!</section>',
675 doc = getDocumentFromXML(xmlIn),
676 xmlOut = doc.toXML();
678 var parser = new DOMParser(),
679 input = parser.parseFromString(xmlIn, 'application/xml').childNodes[0],
680 output = parser.parseFromString(xmlOut, 'application/xml').childNodes[0];
682 expect(input.isEqualNode(output)).to.be.true;
685 it('keeps entities intact', function() {
686 var xmlIn = '<section>< ></section>',
687 doc = getDocumentFromXML(xmlIn),
688 xmlOut = doc.toXML();
689 expect(xmlOut).to.equal(xmlIn);
691 it('keeps entities intact when they form html/xml', function() {
692 var xmlIn = '<section><abc></section>',
693 doc = getDocumentFromXML(xmlIn),
694 xmlOut = doc.toXML();
695 expect(xmlOut).to.equal(xmlIn);