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');
45 describe('Basic ElementNode properties', function() {
46 it('exposes node contents', function() {
47 var node = elementNodeFromXML('<node>Some<node>text</node>is here</node>'),
48 contents = node.contents();
50 expect(contents).to.have.length(3);
51 expect(contents[0].nodeType).to.equal(Node.TEXT_NODE, 'text node 1');
52 expect(contents[1].nodeType).to.equal(Node.ELEMENT_NODE, 'element node 1');
53 expect(contents[2].nodeType).to.equal(Node.TEXT_NODE, 'text node 2');
56 describe('Storing custom data', function() {
59 beforeEach(function() {
60 node = elementNodeFromXML('<div></div>');
63 it('can append single value', function() {
64 node.setData('key', 'value');
65 expect(node.getData('key')).to.equal('value');
68 it('can overwrite the whole data', function() {
69 node.setData('key1', 'value1');
70 node.setData({key2: 'value2'});
71 expect(node.getData('key2')).to.equal('value2');
74 it('can fetch the whole data at once', function() {
75 node.setData({key1: 'value1', key2: 'value2'});
76 expect(node.getData()).to.eql({key1: 'value1', key2: 'value2'});
80 describe('Changing node tag', function() {
82 it('can change tag name', function() {
83 var node = elementNodeFromXML('<div></div>');
85 expect(node.getTagName()).to.equal('span');
88 it('emits nodeTagChange event', function() {
89 var node = elementNodeFromXML('<div></div>'),
92 node.document.on('change', spy);
94 var event = spy.args[0][0];
96 expect(event.type).to.equal('nodeTagChange');
97 expect(event.meta.node.sameNode(node)).to.be.true;
98 expect(event.meta.oldTagName).to.equal('div');
101 describe('Implementation specific expectations', function() {
102 // DOM specifies ElementNode tag as a read-only property, so
103 // changing it in a seamless way is a little bit tricky. For this reason
104 // the folowing expectations are required, despite the fact that they actually are
105 // motivated by implemetation details.
107 it('keeps node in the document', function() {
108 var doc = getDocumentFromXML('<div><header></header></div>'),
109 header = doc.root.contents()[0];
110 header.setTag('span');
111 expect(header.parent().sameNode(doc.root)).to.be.true;
113 it('keeps custom data', function() {
114 var node = elementNodeFromXML('<div></div>');
116 node.setData('key', 'value');
117 node.setTag('header');
119 expect(node.getTagName()).to.equal('header');
120 expect(node.getData()).to.eql({key: 'value'});
123 it('can change document root tag name', function() {
124 var doc = getDocumentFromXML('<div></div>');
125 doc.root.setTag('span');
126 expect(doc.root.getTagName()).to.equal('span');
129 it('keeps contents', function() {
130 var node = elementNodeFromXML('<div><div></div></div>');
131 node.setTag('header');
132 expect(node.contents()).to.have.length(1);
136 describe('Setting node attributes', function() {
137 it('can set node attribute', function() {
138 var node = elementNodeFromXML('<div></div>');
140 node.setAttr('key', 'value');
141 expect(node.getAttr('key')).to.equal('value');
143 it('emits nodeAttrChange event', function() {
144 var node = elementNodeFromXML('<div key="value1"></div>'),
147 node.document.on('change', spy);
148 node.setAttr('key', 'value2');
149 var event = spy.args[0][0];
151 expect(event.type).to.equal('nodeAttrChange');
152 expect(event.meta.node.sameNode(node)).to.be.true;
153 expect(event.meta.attr).to.equal('key');
154 expect(event.meta.oldVal).to.equal('value1');
161 describe('Basic TextNode properties', function() {
162 it('can have its text set', function() {
163 var node = elementNodeFromXML('<div>Alice</div>'),
164 textNode = node.contents()[0];
166 textNode.setText('Cat');
167 expect(textNode.getText()).to.equal('Cat');
170 it('emits nodeTextChange', function() {
171 var node = elementNodeFromXML('<div>Alice</div>'),
172 textNode = node.contents()[0],
175 textNode.document.on('change', spy);
176 textNode.setText('Cat');
178 var event = spy.args[0][0];
179 expect(event.type).to.equal('nodeTextChange');
182 it('puts NodeElement after itself', function() {
183 var node = elementNodeFromXML('<div>Alice</div>'),
184 textNode = node.contents()[0],
185 returned = textNode.after({tagName:'div'});
186 expect(returned.sameNode(node.contents()[1])).to.be.true;
189 it('puts NodeElement before itself', function() {
190 var node = elementNodeFromXML('<div>Alice</div>'),
191 textNode = node.contents()[0],
192 returned = textNode.before({tagName:'div'});
193 expect(returned.sameNode(node.contents()[0])).to.be.true;
196 describe('Wrapping TextNode contents', function() {
198 it('wraps DocumentTextElement', function() {
199 var node = elementNodeFromXML('<section>Alice</section>'),
200 textNode = node.contents()[0];
202 var returned = textNode.wrapWith({tagName: 'header'}),
203 parent = textNode.parent(),
204 parent2 = node.contents()[0];
206 expect(returned.sameNode(parent)).to.be.equal(true, 'wrapper is a parent');
207 expect(returned.sameNode(parent2)).to.be.equal(true, 'wrapper has a correct parent');
208 expect(returned.getTagName()).to.equal('header');
211 describe('wrapping part of DocumentTextElement', function() {
212 [{start: 5, end: 12}, {start: 12, end: 5}].forEach(function(offsets) {
213 it('wraps in the middle ' + offsets.start + '/' + offsets.end, function() {
214 var node = elementNodeFromXML('<section>Alice has a cat</section>'),
215 textNode = node.contents()[0];
217 var returned = textNode.wrapWith({tagName: 'header', attrs: {'attr1': 'value1'}, start: offsets.start, end: offsets.end}),
218 contents = node.contents();
220 expect(contents.length).to.equal(3);
222 expect(contents[0].nodeType).to.be.equal(Node.TEXT_NODE, 'first node is text node');
223 expect(contents[0].getText()).to.equal('Alice');
225 expect(contents[1].sameNode(returned)).to.be.true;
226 expect(returned.getTagName()).to.equal('header');
227 expect(returned.getAttr('attr1')).to.equal('value1');
228 expect(contents[1].contents().length).to.equal(1, 'wrapper has one node inside');
229 expect(contents[1].contents()[0].getText()).to.equal(' has a ');
231 expect(contents[2].nodeType).to.be.equal(Node.TEXT_NODE, 'third node is text node');
232 expect(contents[2].getText()).to.equal('cat');
236 it('wraps whole text inside DocumentTextElement if offsets span entire content', function() {
237 var node = elementNodeFromXML('<section>Alice has a cat</section>'),
238 textNode = node.contents()[0];
240 textNode.wrapWith({tagName: 'header', start: 0, end: 15});
242 var contents = node.contents();
243 expect(contents.length).to.equal(1);
244 expect(contents[0].getTagName()).to.equal('header');
245 expect(contents[0].contents()[0].getText()).to.equal('Alice has a cat');
252 describe('Manipulations', function() {
254 it('appends element node to another element node', function() {
255 var node1 = elementNodeFromParams({tag: 'div'}),
256 node2 = elementNodeFromParams({tag: 'a'}),
257 node3 = elementNodeFromParams({tag: 'p'});
260 expect(node1.contents()[0].sameNode(node2)).to.be.true;
261 expect(node1.contents()[1].sameNode(node3)).to.be.true;
264 it('prepends element node to another element node', function() {
265 var node1 = elementNodeFromParams({tag: 'div'}),
266 node2 = elementNodeFromParams({tag: 'a'}),
267 node3 = elementNodeFromParams({tag: 'p'});
268 node1.prepend(node2);
269 node1.prepend(node3);
270 expect(node1.contents()[0].sameNode(node3)).to.be.true;
271 expect(node1.contents()[1].sameNode(node2)).to.be.true;
274 it('wraps element node with another element node', function() {
275 var node = elementNodeFromXML('<div></div>'),
276 wrapper = elementNodeFromXML('<wrapper></wrapper>');
278 node.wrapWith(wrapper);
279 expect(node.parent().sameNode(wrapper)).to.be.true;
282 it('unwraps element node contents', function() {
283 var node = elementNodeFromXML('<div>Alice <div>has <span>propably</span> a cat</div>!</div>'),
284 outerDiv = node.contents()[1];
286 outerDiv.unwrapContent();
288 expect(node.contents().length).to.equal(3);
289 expect(node.contents()[0].getText()).to.equal('Alice has ');
290 expect(node.contents()[1].getTagName()).to.equal('span');
291 expect(node.contents()[2].getText()).to.equal(' a cat!');
294 describe('Wrapping text', function() {
295 it('wraps text spanning multiple sibling TextNodes', function() {
296 var section = elementNodeFromXML('<section>Alice has a <span>small</span> cat</section>'),
297 wrapper = section.wrapText({
298 _with: {tagName: 'span', attrs: {'attr1': 'value1'}},
304 expect(section.contents().length).to.equal(2);
305 expect(section.contents()[0].nodeType).to.equal(Node.TEXT_NODE);
306 expect(section.contents()[0].getText()).to.equal('Alice ');
308 var wrapper2 = section.contents()[1];
309 expect(wrapper2.sameNode(wrapper)).to.be.true;
310 expect(wrapper.getTagName()).to.equal('span');
312 var wrapperContents = wrapper.contents();
313 expect(wrapperContents.length).to.equal(3);
314 expect(wrapperContents[0].getText()).to.equal('has a ');
316 expect(wrapperContents[1].nodeType).to.equal(Node.ELEMENT_NODE);
317 expect(wrapperContents[1].contents().length).to.equal(1);
318 expect(wrapperContents[1].contents()[0].getText()).to.equal('small');
322 describe('Wrapping Nodes', function() {
323 it('wraps multiple sibling nodes', function() {
324 var section = elementNodeFromXML('<section>Alice<div>has</div><div>a cat</div></section>'),
325 aliceText = section.contents()[0],
326 firstDiv = section.contents()[1],
327 lastDiv = section.contents()[section.contents().length -1];
329 var returned = section.document.wrapNodes({
332 _with: {tagName: 'header'}
335 var sectionContents = section.contents(),
336 header = sectionContents[0],
337 headerContents = header.contents();
339 expect(sectionContents).to.have.length(1);
340 expect(header.sameNode(returned)).to.equal(true, 'wrapper returned');
341 expect(header.parent().sameNode(section)).to.be.true;
342 expect(headerContents).to.have.length(3);
343 expect(headerContents[0].sameNode(aliceText)).to.equal(true, 'first node wrapped');
344 expect(headerContents[1].sameNode(firstDiv)).to.equal(true, 'second node wrapped');
345 expect(headerContents[2].sameNode(lastDiv)).to.equal(true, 'third node wrapped');
348 it('wraps multiple sibling Elements - middle case', function() {
349 var section = elementNodeFromXML('<section><div></div><div></div><div></div><div></div></section>'),
350 div2 = section.contents()[1],
351 div3 = section.contents()[2];
353 section.document.wrapNodes({
356 _with: {tagName: 'header'}
359 var sectionContents = section.contents(),
360 header = sectionContents[1],
361 headerChildren = header.contents();
363 expect(sectionContents).to.have.length(3);
364 expect(headerChildren).to.have.length(2);
365 expect(headerChildren[0].sameNode(div2)).to.equal(true, 'first node wrapped');
366 expect(headerChildren[1].sameNode(div3)).to.equal(true, 'second node wrapped');
372 describe('Events', function() {
373 it('emits nodeAdded event when appending new node', function() {
374 var node = elementNodeFromXML('<div></div>'),
376 node.document.on('change', spy);
378 var appended = node.append({tagName:'div'}),
379 event = spy.args[0][0];
380 expect(event.type).to.equal('nodeAdded');
381 expect(event.meta.node.sameNode(appended)).to.be.true;
384 it('emits nodeMoved when appending aready existing node', function() {
385 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
386 a = node.contents()[0],
387 b = node.contents()[1],
389 node.document.on('change', spy);
391 var appended = a.append(b),
392 event = spy.args[0][0];
394 expect(spy.callCount).to.equal(1);
395 expect(event.type).to.equal('nodeMoved');
396 expect(event.meta.node.sameNode(appended)).to.be.true;
399 it('emits nodeAdded event when prepending new node', function() {
400 var node = elementNodeFromXML('<div></div>'),
402 node.document.on('change', spy);
404 var prepended = node.prepend({tagName:'div'}),
405 event = spy.args[0][0];
406 expect(event.type).to.equal('nodeAdded');
407 expect(event.meta.node.sameNode(prepended)).to.be.true;
410 it('emits nodeMoved when prepending aready existing node', function() {
411 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
412 a = node.contents()[0],
413 b = node.contents()[1],
415 node.document.on('change', spy);
417 var prepended = a.prepend(b),
418 event = spy.args[0][0];
419 expect(spy.callCount).to.equal(1);
420 expect(event.type).to.equal('nodeMoved');
421 expect(event.meta.node.sameNode(prepended)).to.be.true;
424 it('emits nodeAdded event when inserting node after another', function() {
425 var node = elementNodeFromXML('<div><a></a></div>').contents()[0],
427 node.document.on('change', spy);
429 var inserted = node.after({tagName:'div'}),
430 event = spy.args[0][0];
431 expect(event.type).to.equal('nodeAdded');
432 expect(event.meta.node.sameNode(inserted)).to.be.true;
435 it('emits nodeMoved when inserting aready existing node after another', function() {
436 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
437 a = node.contents()[0],
438 b = node.contents()[1],
440 node.document.on('change', spy);
441 var inserted = b.after(a),
442 event = spy.args[0][0];
444 expect(spy.callCount).to.equal(1);
445 expect(event.type).to.equal('nodeMoved');
446 expect(event.meta.node.sameNode(inserted)).to.be.true;
449 it('emits nodeAdded event when inserting node before another', function() {
450 var node = elementNodeFromXML('<div><a></a></div>').contents()[0],
452 node.document.on('change', spy);
454 var inserted = node.before({tagName:'div'}),
455 event = spy.args[0][0];
456 expect(event.type).to.equal('nodeAdded');
457 expect(event.meta.node.sameNode(inserted)).to.be.true;
460 it('emits nodeAdded when inserting aready existing node before another', function() {
461 var node = elementNodeFromXML('<div><a></a><b></b></div>'),
462 a = node.contents()[0],
463 b = node.contents()[1],
465 node.document.on('change', spy);
466 var inserted = a.before(b),
467 event = spy.args[0][0];
469 expect(spy.callCount).to.equal(1);
470 expect(event.type).to.equal('nodeMoved');
471 expect(event.meta.node.sameNode(inserted)).to.be.true;
475 describe('Serializing document to WLXML', function() {
476 it('keeps document intact when no changes have been made', function() {
477 var xmlIn = '<section>Alice<div>has</div>a <span class="uri" meta-uri="http://cat.com">cat</span>!</section>',
478 doc = getDocumentFromXML(xmlIn),
479 xmlOut = doc.toXML();
481 var parser = new DOMParser(),
482 input = parser.parseFromString(xmlIn, 'application/xml').childNodes[0],
483 output = parser.parseFromString(xmlOut, 'application/xml').childNodes[0];
485 expect(input.isEqualNode(output)).to.be.true;
488 it('keeps entities intact', function() {
489 var xmlIn = '<section>< ></section>',
490 doc = getDocumentFromXML(xmlIn),
491 xmlOut = doc.toXML();
492 expect(xmlOut).to.equal(xmlIn);
494 it('keeps entities intact when they form html/xml', function() {
495 var xmlIn = '<section><abc></section>',
496 doc = getDocumentFromXML(xmlIn),
497 xmlOut = doc.toXML();
498 expect(xmlOut).to.equal(xmlIn);