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 describe('Wrapping TextNode contents', function() {
184 it('wraps DocumentTextElement', function() {
185 var node = elementNodeFromXML('<section>Alice</section>'),
186 textNode = node.contents()[0];
188 var returned = textNode.wrapWith({tagName: 'header'}),
189 parent = textNode.parent(),
190 parent2 = node.contents()[0];
192 expect(returned.sameNode(parent)).to.be.equal(true, 'wrapper is a parent');
193 expect(returned.sameNode(parent2)).to.be.equal(true, 'wrapper has a correct parent');
194 expect(returned.getTagName()).to.equal('header');
200 describe('Manipulations', function() {
202 it('appends element node to another element node', function() {
203 var node1 = elementNodeFromParams({tag: 'div'}),
204 node2 = elementNodeFromParams({tag: 'a'});
206 expect(node1.contents()[0].sameNode(node2)).to.be.true;
209 it('wraps element node with another element node', function() {
210 var node = elementNodeFromXML('<div></div>'),
211 wrapper = elementNodeFromXML('<wrapper></wrapper>');
213 node.wrapWith(wrapper);
214 expect(node.parent().sameNode(wrapper)).to.be.true;
217 it('unwraps element node contents', function() {
218 var node = elementNodeFromXML('<div>Alice <div>has <span>propably</span> a cat</div>!</div>'),
219 outerDiv = node.contents()[1];
221 outerDiv.unwrapContent();
223 expect(node.contents().length).to.equal(3);
224 expect(node.contents()[0].getText()).to.equal('Alice has ');
225 expect(node.contents()[1].getTagName()).to.equal('span');
226 expect(node.contents()[2].getText()).to.equal(' a cat!');
231 describe('Serializing document to WLXML', function() {
232 it('keeps document intact when no changes have been made', function() {
233 var xmlIn = '<section>Alice<div>has</div>a <span class="uri" meta-uri="http://cat.com">cat</span>!</section>',
234 doc = getDocumentFromXML(xmlIn),
235 xmlOut = doc.toXML();
237 var parser = new DOMParser(),
238 input = parser.parseFromString(xmlIn, 'application/xml').childNodes[0],
239 output = parser.parseFromString(xmlOut, 'application/xml').childNodes[0];
241 expect(input.isEqualNode(output)).to.be.true;
244 it('keeps entities intact', function() {
245 var xmlIn = '<section>< ></section>',
246 doc = getDocumentFromXML(xmlIn),
247 xmlOut = doc.toXML();
248 expect(xmlOut).to.equal(xmlIn);
250 it('keeps entities intact when they form html/xml', function() {
251 var xmlIn = '<section><abc></section>',
252 doc = getDocumentFromXML(xmlIn),
253 xmlOut = doc.toXML();
254 expect(xmlOut).to.equal(xmlIn);