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');
56 describe('Basic ElementNode properties', function() {
57 it('exposes node contents', function() {
58 var node = elementNodeFromXML('<node>Some<node>text</node>is here</node>'),
59 contents = node.contents();
61 expect(contents).to.have.length(3);
62 expect(contents[0].nodeType).to.equal(Node.TEXT_NODE, 'text node 1');
63 expect(contents[1].nodeType).to.equal(Node.ELEMENT_NODE, 'element node 1');
64 expect(contents[2].nodeType).to.equal(Node.TEXT_NODE, 'text node 2');
67 describe('Storing custom data', function() {
70 beforeEach(function() {
71 node = elementNodeFromXML('<div></div>');
74 it('can append single value', function() {
75 node.setData('key', 'value');
76 expect(node.getData('key')).to.equal('value');
79 it('can overwrite the whole data', function() {
80 node.setData('key1', 'value1');
81 node.setData({key2: 'value2'});
82 expect(node.getData('key2')).to.equal('value2');
85 it('can fetch the whole data at once', function() {
86 node.setData({key1: 'value1', key2: 'value2'});
87 expect(node.getData()).to.eql({key1: 'value1', key2: 'value2'});
91 describe('Changing node tag', function() {
93 it('can change tag name', function() {
94 var node = elementNodeFromXML('<div></div>');
96 expect(node.getTagName()).to.equal('span');
99 it('emits nodeTagChange event', function() {
100 var node = elementNodeFromXML('<div></div>'),
103 node.document.on('change', spy);
105 var event = spy.args[0][0];
107 expect(event.type).to.equal('nodeTagChange');
108 expect(event.meta.node.sameNode(node)).to.be.true;
109 expect(event.meta.oldTagName).to.equal('div');
112 describe('Implementation specific expectations', function() {
113 // DOM specifies ElementNode tag as a read-only property, so
114 // changing it in a seamless way is a little bit tricky. For this reason
115 // the folowing expectations are required, despite the fact that they actually are
116 // motivated by implemetation details.
118 it('keeps node in the document', function() {
119 var doc = getDocumentFromXML('<div><header></header></div>'),
120 header = doc.root.contents()[0];
121 header.setTag('span');
122 expect(header.parent().sameNode(doc.root)).to.be.true;
124 it('keeps custom data', function() {
125 var node = elementNodeFromXML('<div></div>');
127 node.setData('key', 'value');
128 node.setTag('header');
130 expect(node.getTagName()).to.equal('header');
131 expect(node.getData()).to.eql({key: 'value'});
134 it('can change document root tag name', function() {
135 var doc = getDocumentFromXML('<div></div>');
136 doc.root.setTag('span');
137 expect(doc.root.getTagName()).to.equal('span');
140 it('keeps contents', function() {
141 var node = elementNodeFromXML('<div><div></div></div>');
142 node.setTag('header');
143 expect(node.contents()).to.have.length(1);
147 describe('Setting node attributes', function() {
148 it('can set node attribute', function() {
149 var node = elementNodeFromXML('<div></div>');
151 node.setAttr('key', 'value');
152 expect(node.getAttr('key')).to.equal('value');
154 it('emits nodeAttrChange event', function() {
155 var node = elementNodeFromXML('<div key="value1"></div>'),
158 node.document.on('change', spy);
159 node.setAttr('key', 'value2');
160 var event = spy.args[0][0];
162 expect(event.type).to.equal('nodeAttrChange');
163 expect(event.meta.node.sameNode(node)).to.be.true;
164 expect(event.meta.attr).to.equal('key');
165 expect(event.meta.oldVal).to.equal('value1');
172 describe('Basic TextNode properties', function() {
173 it('can have its text set', function() {
174 var node = elementNodeFromXML('<div>Alice</div>'),
175 textNode = node.contents()[0];
177 textNode.setText('Cat');
178 expect(textNode.getText()).to.equal('Cat');
181 it('emits nodeTextChange', function() {
182 var node = elementNodeFromXML('<div>Alice</div>'),
183 textNode = node.contents()[0],
186 textNode.document.on('change', spy);
187 textNode.setText('Cat');
189 var event = spy.args[0][0];
190 expect(event.type).to.equal('nodeTextChange');
193 it('puts NodeElement after itself', function() {
194 var node = elementNodeFromXML('<div>Alice</div>'),
195 textNode = node.contents()[0],
196 returned = textNode.after({tagName:'div'});
197 expect(returned.sameNode(node.contents()[1])).to.be.true;
200 it('puts NodeElement before itself', function() {
201 var node = elementNodeFromXML('<div>Alice</div>'),
202 textNode = node.contents()[0],
203 returned = textNode.before({tagName:'div'});
204 expect(returned.sameNode(node.contents()[0])).to.be.true;
207 describe('Wrapping TextNode contents', function() {
209 it('wraps DocumentTextElement', function() {
210 var node = elementNodeFromXML('<section>Alice</section>'),
211 textNode = node.contents()[0];
213 var returned = textNode.wrapWith({tagName: 'header'}),
214 parent = textNode.parent(),
215 parent2 = node.contents()[0];
217 expect(returned.sameNode(parent)).to.be.equal(true, 'wrapper is a parent');
218 expect(returned.sameNode(parent2)).to.be.equal(true, 'wrapper has a correct parent');
219 expect(returned.getTagName()).to.equal('header');
222 describe('wrapping part of DocumentTextElement', function() {
223 [{start: 5, end: 12}, {start: 12, end: 5}].forEach(function(offsets) {
224 it('wraps in the middle ' + offsets.start + '/' + offsets.end, function() {
225 var node = elementNodeFromXML('<section>Alice has a cat</section>'),
226 textNode = node.contents()[0];
228 var returned = textNode.wrapWith({tagName: 'header', attrs: {'attr1': 'value1'}, start: offsets.start, end: offsets.end}),
229 contents = node.contents();
231 expect(contents.length).to.equal(3);
233 expect(contents[0].nodeType).to.be.equal(Node.TEXT_NODE, 'first node is text node');
234 expect(contents[0].getText()).to.equal('Alice');
236 expect(contents[1].sameNode(returned)).to.be.true;
237 expect(returned.getTagName()).to.equal('header');
238 expect(returned.getAttr('attr1')).to.equal('value1');
239 expect(contents[1].contents().length).to.equal(1, 'wrapper has one node inside');
240 expect(contents[1].contents()[0].getText()).to.equal(' has a ');
242 expect(contents[2].nodeType).to.be.equal(Node.TEXT_NODE, 'third node is text node');
243 expect(contents[2].getText()).to.equal('cat');
247 it('wraps whole text inside DocumentTextElement if offsets span entire content', function() {
248 var node = elementNodeFromXML('<section>Alice has a cat</section>'),
249 textNode = node.contents()[0];
251 textNode.wrapWith({tagName: 'header', start: 0, end: 15});
253 var contents = node.contents();
254 expect(contents.length).to.equal(1);
255 expect(contents[0].getTagName()).to.equal('header');
256 expect(contents[0].contents()[0].getText()).to.equal('Alice has a cat');
263 describe('Manipulations', function() {
265 it('appends element node to another element node', function() {
266 var node1 = elementNodeFromParams({tag: 'div'}),
267 node2 = elementNodeFromParams({tag: 'a'}),
268 node3 = elementNodeFromParams({tag: 'p'});
271 expect(node1.contents()[0].sameNode(node2)).to.be.true;
272 expect(node1.contents()[1].sameNode(node3)).to.be.true;
275 it('prepends element node to another element node', function() {
276 var node1 = elementNodeFromParams({tag: 'div'}),
277 node2 = elementNodeFromParams({tag: 'a'}),
278 node3 = elementNodeFromParams({tag: 'p'});
279 node1.prepend(node2);
280 node1.prepend(node3);
281 expect(node1.contents()[0].sameNode(node3)).to.be.true;
282 expect(node1.contents()[1].sameNode(node2)).to.be.true;
285 it('wraps element node with another element node', function() {
286 var node = elementNodeFromXML('<div></div>'),
287 wrapper = elementNodeFromXML('<wrapper></wrapper>');
289 node.wrapWith(wrapper);
290 expect(node.parent().sameNode(wrapper)).to.be.true;
293 it('unwraps element node contents', function() {
294 var node = elementNodeFromXML('<div>Alice <div>has <span>propably</span> a cat</div>!</div>'),
295 outerDiv = node.contents()[1];
297 outerDiv.unwrapContent();
299 expect(node.contents().length).to.equal(3);
300 expect(node.contents()[0].getText()).to.equal('Alice has ');
301 expect(node.contents()[1].getTagName()).to.equal('span');
302 expect(node.contents()[2].getText()).to.equal(' a cat!');
305 it('unwrap single node from its parent', function() {
306 var doc = getDocumentFromXML('<div><a><b></b></a></div>'),
308 a = div.contents()[0],
311 var parent = b.unwrap();
313 expect(parent.sameNode(div)).to.equal(true, 'returns new parent');
314 expect(div.contents()).to.have.length(1, 'root contains only one node');
315 expect(div.contents()[0].sameNode(b)).to.equal(true, 'node got unwrapped');
318 describe('Wrapping text', function() {
319 it('wraps text spanning multiple sibling TextNodes', function() {
320 var section = elementNodeFromXML('<section>Alice has a <span>small</span> cat</section>'),
321 wrapper = section.wrapText({
322 _with: {tagName: 'span', attrs: {'attr1': 'value1'}},
328 expect(section.contents().length).to.equal(2);
329 expect(section.contents()[0].nodeType).to.equal(Node.TEXT_NODE);
330 expect(section.contents()[0].getText()).to.equal('Alice ');
332 var wrapper2 = section.contents()[1];
333 expect(wrapper2.sameNode(wrapper)).to.be.true;
334 expect(wrapper.getTagName()).to.equal('span');
336 var wrapperContents = wrapper.contents();
337 expect(wrapperContents.length).to.equal(3);
338 expect(wrapperContents[0].getText()).to.equal('has a ');
340 expect(wrapperContents[1].nodeType).to.equal(Node.ELEMENT_NODE);
341 expect(wrapperContents[1].contents().length).to.equal(1);
342 expect(wrapperContents[1].contents()[0].getText()).to.equal('small');
346 describe('Wrapping Nodes', function() {
347 it('wraps multiple sibling nodes', function() {
348 var section = elementNodeFromXML('<section>Alice<div>has</div><div>a cat</div></section>'),
349 aliceText = section.contents()[0],
350 firstDiv = section.contents()[1],
351 lastDiv = section.contents()[section.contents().length -1];
353 var returned = section.document.wrapNodes({
356 _with: {tagName: 'header'}
359 var sectionContents = section.contents(),
360 header = sectionContents[0],
361 headerContents = header.contents();
363 expect(sectionContents).to.have.length(1);
364 expect(header.sameNode(returned)).to.equal(true, 'wrapper returned');
365 expect(header.parent().sameNode(section)).to.be.true;
366 expect(headerContents).to.have.length(3);
367 expect(headerContents[0].sameNode(aliceText)).to.equal(true, 'first node wrapped');
368 expect(headerContents[1].sameNode(firstDiv)).to.equal(true, 'second node wrapped');
369 expect(headerContents[2].sameNode(lastDiv)).to.equal(true, 'third node wrapped');
372 it('wraps multiple sibling Elements - middle case', function() {
373 var section = elementNodeFromXML('<section><div></div><div></div><div></div><div></div></section>'),
374 div2 = section.contents()[1],
375 div3 = section.contents()[2];
377 section.document.wrapNodes({
380 _with: {tagName: 'header'}
383 var sectionContents = section.contents(),
384 header = sectionContents[1],
385 headerChildren = header.contents();
387 expect(sectionContents).to.have.length(3);
388 expect(headerChildren).to.have.length(2);
389 expect(headerChildren[0].sameNode(div2)).to.equal(true, 'first node wrapped');
390 expect(headerChildren[1].sameNode(div3)).to.equal(true, 'second node wrapped');
396 describe('Events', function() {
397 it('emits nodeDetached event on node detach', function() {
398 var node = elementNodeFromXML('<div><div></div></div>'),
399 innerNode = node.contents()[0],
401 node.document.on('change', spy);
403 var detached = innerNode.detach(),
404 event = spy.args[0][0];
406 expect(event.type).to.equal('nodeDetached');
407 expect(event.meta.node.sameNode(detached, 'detached node in event meta'));
408 expect(event.meta.parent.sameNode(node), 'original parent node in event meta');
411 it('emits nodeAdded event when appending new node', function() {
412 var node = elementNodeFromXML('<div></div>'),
414 node.document.on('change', spy);
416 var appended = node.append({tagName:'div'}),
417 event = spy.args[0][0];
418 expect(event.type).to.equal('nodeAdded');
419 expect(event.meta.node.sameNode(appended)).to.be.true;
422 it('emits nodeMoved when appending aready existing node', function() {
423 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
424 a = node.contents()[0],
425 b = node.contents()[1],
427 node.document.on('change', spy);
429 var appended = a.append(b),
430 event = spy.args[0][0];
432 expect(spy.callCount).to.equal(1);
433 expect(event.type).to.equal('nodeMoved');
434 expect(event.meta.node.sameNode(appended)).to.be.true;
437 it('emits nodeAdded event when prepending new node', function() {
438 var node = elementNodeFromXML('<div></div>'),
440 node.document.on('change', spy);
442 var prepended = node.prepend({tagName:'div'}),
443 event = spy.args[0][0];
444 expect(event.type).to.equal('nodeAdded');
445 expect(event.meta.node.sameNode(prepended)).to.be.true;
448 it('emits nodeMoved when prepending aready existing node', function() {
449 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
450 a = node.contents()[0],
451 b = node.contents()[1],
453 node.document.on('change', spy);
455 var prepended = a.prepend(b),
456 event = spy.args[0][0];
457 expect(spy.callCount).to.equal(1);
458 expect(event.type).to.equal('nodeMoved');
459 expect(event.meta.node.sameNode(prepended)).to.be.true;
462 it('emits nodeAdded event when inserting node after another', function() {
463 var node = elementNodeFromXML('<div><a></a></div>').contents()[0],
465 node.document.on('change', spy);
467 var inserted = node.after({tagName:'div'}),
468 event = spy.args[0][0];
469 expect(event.type).to.equal('nodeAdded');
470 expect(event.meta.node.sameNode(inserted)).to.be.true;
473 it('emits nodeMoved when inserting aready existing node after another', function() {
474 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
475 a = node.contents()[0],
476 b = node.contents()[1],
478 node.document.on('change', spy);
479 var inserted = b.after(a),
480 event = spy.args[0][0];
482 expect(spy.callCount).to.equal(1);
483 expect(event.type).to.equal('nodeMoved');
484 expect(event.meta.node.sameNode(inserted)).to.be.true;
487 it('emits nodeAdded event when inserting node before another', function() {
488 var node = elementNodeFromXML('<div><a></a></div>').contents()[0],
490 node.document.on('change', spy);
492 var inserted = node.before({tagName:'div'}),
493 event = spy.args[0][0];
494 expect(event.type).to.equal('nodeAdded');
495 expect(event.meta.node.sameNode(inserted)).to.be.true;
498 it('emits nodeAdded when inserting aready existing node before another', function() {
499 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
500 a = node.contents()[0],
501 b = node.contents()[1],
503 node.document.on('change', spy);
504 var inserted = a.before(b),
505 event = spy.args[0][0];
507 expect(spy.callCount).to.equal(1);
508 expect(event.type).to.equal('nodeMoved');
509 expect(event.meta.node.sameNode(inserted)).to.be.true;
513 describe('Traversing', function() {
514 describe('Basic', function() {
515 it('can access node parent', function() {
516 var doc = getDocumentFromXML('<a><b></b></a>'),
520 expect(a.parent()).to.equal(null, 'parent of a root is null');
521 expect(b.parent().sameNode(a)).to.be.true;
523 it('can access node parents', function() {
524 var doc = getDocumentFromXML('<a><b><c></c></b></a>'),
529 var parents = c.parents();
530 expect(parents).to.eql([b,a]);
534 describe('finding sibling parents of two elements', function() {
535 it('returns elements themself if they have direct common parent', function() {
536 var doc = getDocumentFromXML('<section><div><div>A</div><div>B</div></div></section>'),
537 wrappingDiv = doc.root.contents()[0],
538 divA = wrappingDiv.contents()[0],
539 divB = wrappingDiv.contents()[1];
541 var siblingParents = doc.getSiblingParents({node1: divA, node2: divB});
543 expect(siblingParents.node1.sameNode(divA)).to.equal(true, 'divA');
544 expect(siblingParents.node2.sameNode(divB)).to.equal(true, 'divB');
547 it('returns sibling parents - example 1', function() {
548 var doc = getDocumentFromXML('<section>Alice <span>has a cat</span></section>'),
549 aliceText = doc.root.contents()[0],
550 span = doc.root.contents()[1],
551 spanText = span.contents()[0];
553 var siblingParents = doc.getSiblingParents({node1: aliceText, node2: spanText});
555 expect(siblingParents.node1.sameNode(aliceText)).to.equal(true, 'aliceText');
556 expect(siblingParents.node2.sameNode(span)).to.equal(true, 'span');
561 describe('Serializing document to WLXML', function() {
562 it('keeps document intact when no changes have been made', function() {
563 var xmlIn = '<section>Alice<div>has</div>a <span class="uri" meta-uri="http://cat.com">cat</span>!</section>',
564 doc = getDocumentFromXML(xmlIn),
565 xmlOut = doc.toXML();
567 var parser = new DOMParser(),
568 input = parser.parseFromString(xmlIn, 'application/xml').childNodes[0],
569 output = parser.parseFromString(xmlOut, 'application/xml').childNodes[0];
571 expect(input.isEqualNode(output)).to.be.true;
574 it('keeps entities intact', function() {
575 var xmlIn = '<section>< ></section>',
576 doc = getDocumentFromXML(xmlIn),
577 xmlOut = doc.toXML();
578 expect(xmlOut).to.equal(xmlIn);
580 it('keeps entities intact when they form html/xml', function() {
581 var xmlIn = '<section><abc></section>',
582 doc = getDocumentFromXML(xmlIn),
583 xmlOut = doc.toXML();
584 expect(xmlOut).to.equal(xmlIn);