4 ], function(chai, smartxml) {
8 /* global describe, it, beforeEach */
10 var expect = chai.expect;
13 var getDocumentFromXML = function(xml) {
14 return smartxml.documentFromXML(xml);
17 var elementNodeFromParams = function(params) {
18 return smartxml.elementNodeFromXML('<' + params.tag + '></' + params.tag + '>');
21 var elementNodeFromXML = function(xml) {
22 return smartxml.elementNodeFromXML(xml);
26 describe('smartxml', function() {
28 describe('Basic Document properties', function() {
29 it('exposes its root element', function() {
30 var doc = getDocumentFromXML('<div></div>');
31 expect(doc.root.getTagName()).to.equal('div');
35 describe('Basic ElementNode properties', function() {
36 it('exposes node contents', function() {
37 var node = elementNodeFromXML('<node>Some<node>text</node>is here</node>'),
38 contents = node.contents();
40 expect(contents).to.have.length(3);
41 expect(contents[0].nodeType).to.equal(Node.TEXT_NODE, 'text node 1');
42 expect(contents[1].nodeType).to.equal(Node.ELEMENT_NODE, 'element node 1');
43 expect(contents[2].nodeType).to.equal(Node.TEXT_NODE, 'text node 2');
46 describe('Storing custom data', function() {
49 beforeEach(function() {
50 node = elementNodeFromXML('<div></div>');
53 it('can append single value', function() {
54 node.setData('key', 'value');
55 expect(node.getData('key')).to.equal('value');
58 it('can overwrite the whole data', function() {
59 node.setData('key1', 'value1');
60 node.setData({key2: 'value2'});
61 expect(node.getData('key2')).to.equal('value2');
64 it('can fetch the whole data at once', function() {
65 node.setData({key1: 'value1', key2: 'value2'});
66 expect(node.getData()).to.eql({key1: 'value1', key2: 'value2'});
70 describe('Changing node tag', function() {
71 it('keeps custom data', function() {
72 var node = elementNodeFromXML('<div></div>');
74 node.setData('key', 'value');
75 node.setTag('header');
77 expect(node.getTagName()).to.equal('header');
78 expect(node.getData()).to.eql({key: 'value'});
83 describe('Manipulations', function() {
85 it('appends element node to another element node', function() {
86 var node1 = elementNodeFromParams({tag: 'div'}),
87 node2 = elementNodeFromParams({tag: 'a'});
89 expect(node1.contents()[0].sameNode(node2)).to.be.true;
92 it('wraps element node with another element node', function() {
93 var node = elementNodeFromXML('<div></div>'),
94 wrapper = elementNodeFromXML('<wrapper></wrapper>');
96 node.wrapWith(wrapper);
97 expect(node.parent().sameNode(wrapper)).to.be.true;
100 it('wraps text node with element node', function() {
101 var node = elementNodeFromXML('<div>Alice</div>'),
102 textNode = node.contents()[0],
103 wrapper = elementNodeFromXML('<wrapper></wrapper>');
105 textNode.wrapWith(wrapper);
106 expect(textNode.parent().sameNode(wrapper)).to.be.true;
107 expect(node.contents()).to.have.length(1);
110 it('unwraps element node contents', function() {
111 var node = elementNodeFromXML('<div>Alice <div>has <span>propably</span> a cat</div>!</div>'),
112 outerDiv = node.contents()[1];
114 outerDiv.unwrapContent();
116 expect(node.contents().length).to.equal(3);
117 expect(node.contents()[0].getText()).to.equal('Alice has ');
118 expect(node.contents()[1].getTagName()).to.equal('span');
119 expect(node.contents()[2].getText()).to.equal(' a cat!');
124 describe('Serializing document to WLXML', function() {
125 it('keeps document intact when no changes have been made', function() {
126 var xmlIn = '<section>Alice<div>has</div>a <span class="uri" meta-uri="http://cat.com">cat</span>!</section>',
127 doc = getDocumentFromXML(xmlIn),
128 xmlOut = doc.toXML();
130 var parser = new DOMParser(),
131 input = parser.parseFromString(xmlIn, 'application/xml').childNodes[0],
132 output = parser.parseFromString(xmlOut, 'application/xml').childNodes[0];
134 expect(input.isEqualNode(output)).to.be.true;