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 sectionContents = section.contents(),
392 header = sectionContents[0],
393 headerContents = header.contents();
395 expect(sectionContents).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 sectionContents = section.contents(),
416 header = sectionContents[1],
417 headerChildren = header.contents();
419 expect(sectionContents).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('Events', function() {
429 it('emits nodeDetached event on node detach', function() {
430 var node = elementNodeFromXML('<div><div></div></div>'),
431 innerNode = node.contents()[0],
433 node.document.on('change', spy);
435 var detached = innerNode.detach(),
436 event = spy.args[0][0];
438 expect(event.type).to.equal('nodeDetached');
439 expect(event.meta.node.sameNode(detached, 'detached node in event meta'));
440 expect(event.meta.parent.sameNode(node), 'original parent node in event meta');
443 it('emits nodeAdded event when appending new node', function() {
444 var node = elementNodeFromXML('<div></div>'),
446 node.document.on('change', spy);
448 var appended = node.append({tagName:'div'}),
449 event = spy.args[0][0];
450 expect(event.type).to.equal('nodeAdded');
451 expect(event.meta.node.sameNode(appended)).to.be.true;
454 it('emits nodeMoved when appending aready existing node', function() {
455 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
456 a = node.contents()[0],
457 b = node.contents()[1],
459 node.document.on('change', spy);
461 var appended = a.append(b),
462 event = spy.args[0][0];
464 expect(spy.callCount).to.equal(1);
465 expect(event.type).to.equal('nodeMoved');
466 expect(event.meta.node.sameNode(appended)).to.be.true;
469 it('emits nodeAdded event when prepending new node', function() {
470 var node = elementNodeFromXML('<div></div>'),
472 node.document.on('change', spy);
474 var prepended = node.prepend({tagName:'div'}),
475 event = spy.args[0][0];
476 expect(event.type).to.equal('nodeAdded');
477 expect(event.meta.node.sameNode(prepended)).to.be.true;
480 it('emits nodeMoved when prepending aready existing node', function() {
481 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
482 a = node.contents()[0],
483 b = node.contents()[1],
485 node.document.on('change', spy);
487 var prepended = a.prepend(b),
488 event = spy.args[0][0];
489 expect(spy.callCount).to.equal(1);
490 expect(event.type).to.equal('nodeMoved');
491 expect(event.meta.node.sameNode(prepended)).to.be.true;
494 it('emits nodeAdded event when inserting node after another', function() {
495 var node = elementNodeFromXML('<div><a></a></div>').contents()[0],
497 node.document.on('change', spy);
499 var inserted = node.after({tagName:'div'}),
500 event = spy.args[0][0];
501 expect(event.type).to.equal('nodeAdded');
502 expect(event.meta.node.sameNode(inserted)).to.be.true;
505 it('emits nodeMoved when inserting aready existing node after another', function() {
506 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
507 a = node.contents()[0],
508 b = node.contents()[1],
510 node.document.on('change', spy);
511 var inserted = b.after(a),
512 event = spy.args[0][0];
514 expect(spy.callCount).to.equal(1);
515 expect(event.type).to.equal('nodeMoved');
516 expect(event.meta.node.sameNode(inserted)).to.be.true;
519 it('emits nodeAdded event when inserting node before another', function() {
520 var node = elementNodeFromXML('<div><a></a></div>').contents()[0],
522 node.document.on('change', spy);
524 var inserted = node.before({tagName:'div'}),
525 event = spy.args[0][0];
526 expect(event.type).to.equal('nodeAdded');
527 expect(event.meta.node.sameNode(inserted)).to.be.true;
530 it('emits nodeAdded when inserting aready existing node before another', function() {
531 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
532 a = node.contents()[0],
533 b = node.contents()[1],
535 node.document.on('change', spy);
536 var inserted = a.before(b),
537 event = spy.args[0][0];
539 expect(spy.callCount).to.equal(1);
540 expect(event.type).to.equal('nodeMoved');
541 expect(event.meta.node.sameNode(inserted)).to.be.true;
545 describe('Traversing', function() {
546 describe('Basic', function() {
547 it('can access node parent', function() {
548 var doc = getDocumentFromXML('<a><b></b></a>'),
552 expect(a.parent()).to.equal(null, 'parent of a root is null');
553 expect(b.parent().sameNode(a)).to.be.true;
555 it('can access node parents', function() {
556 var doc = getDocumentFromXML('<a><b><c></c></b></a>'),
561 var parents = c.parents();
562 expect(parents).to.eql([b,a]);
566 describe('finding sibling parents of two elements', function() {
567 it('returns elements themself if they have direct common parent', function() {
568 var doc = getDocumentFromXML('<section><div><div>A</div><div>B</div></div></section>'),
569 wrappingDiv = doc.root.contents()[0],
570 divA = wrappingDiv.contents()[0],
571 divB = wrappingDiv.contents()[1];
573 var siblingParents = doc.getSiblingParents({node1: divA, node2: divB});
575 expect(siblingParents.node1.sameNode(divA)).to.equal(true, 'divA');
576 expect(siblingParents.node2.sameNode(divB)).to.equal(true, 'divB');
579 it('returns sibling parents - example 1', function() {
580 var doc = getDocumentFromXML('<section>Alice <span>has a cat</span></section>'),
581 aliceText = doc.root.contents()[0],
582 span = doc.root.contents()[1],
583 spanText = span.contents()[0];
585 var siblingParents = doc.getSiblingParents({node1: aliceText, node2: spanText});
587 expect(siblingParents.node1.sameNode(aliceText)).to.equal(true, 'aliceText');
588 expect(siblingParents.node2.sameNode(span)).to.equal(true, 'span');
593 describe('Serializing document to WLXML', function() {
594 it('keeps document intact when no changes have been made', function() {
595 var xmlIn = '<section>Alice<div>has</div>a <span class="uri" meta-uri="http://cat.com">cat</span>!</section>',
596 doc = getDocumentFromXML(xmlIn),
597 xmlOut = doc.toXML();
599 var parser = new DOMParser(),
600 input = parser.parseFromString(xmlIn, 'application/xml').childNodes[0],
601 output = parser.parseFromString(xmlOut, 'application/xml').childNodes[0];
603 expect(input.isEqualNode(output)).to.be.true;
606 it('keeps entities intact', function() {
607 var xmlIn = '<section>< ></section>',
608 doc = getDocumentFromXML(xmlIn),
609 xmlOut = doc.toXML();
610 expect(xmlOut).to.equal(xmlIn);
612 it('keeps entities intact when they form html/xml', function() {
613 var xmlIn = '<section><abc></section>',
614 doc = getDocumentFromXML(xmlIn),
615 xmlOut = doc.toXML();
616 expect(xmlOut).to.equal(xmlIn);