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');
34 it('can resets its content entirely', function() {
35 var doc = getDocumentFromXML('<div></div>');
37 expect(doc.root.getTagName()).to.equal('div');
39 doc.loadXML('<header></header>');
40 expect(doc.root.getTagName()).to.equal('header');
44 describe('Basic ElementNode properties', function() {
45 it('exposes node contents', function() {
46 var node = elementNodeFromXML('<node>Some<node>text</node>is here</node>'),
47 contents = node.contents();
49 expect(contents).to.have.length(3);
50 expect(contents[0].nodeType).to.equal(Node.TEXT_NODE, 'text node 1');
51 expect(contents[1].nodeType).to.equal(Node.ELEMENT_NODE, 'element node 1');
52 expect(contents[2].nodeType).to.equal(Node.TEXT_NODE, 'text node 2');
55 describe('Storing custom data', function() {
58 beforeEach(function() {
59 node = elementNodeFromXML('<div></div>');
62 it('can append single value', function() {
63 node.setData('key', 'value');
64 expect(node.getData('key')).to.equal('value');
67 it('can overwrite the whole data', function() {
68 node.setData('key1', 'value1');
69 node.setData({key2: 'value2'});
70 expect(node.getData('key2')).to.equal('value2');
73 it('can fetch the whole data at once', function() {
74 node.setData({key1: 'value1', key2: 'value2'});
75 expect(node.getData()).to.eql({key1: 'value1', key2: 'value2'});
79 describe('Changing node tag', function() {
80 it('keeps custom data', function() {
81 var node = elementNodeFromXML('<div></div>');
83 node.setData('key', 'value');
84 node.setTag('header');
86 expect(node.getTagName()).to.equal('header');
87 expect(node.getData()).to.eql({key: 'value'});
92 describe('Manipulations', function() {
94 it('appends element node to another element node', function() {
95 var node1 = elementNodeFromParams({tag: 'div'}),
96 node2 = elementNodeFromParams({tag: 'a'});
98 expect(node1.contents()[0].sameNode(node2)).to.be.true;
101 it('wraps element node with another element node', function() {
102 var node = elementNodeFromXML('<div></div>'),
103 wrapper = elementNodeFromXML('<wrapper></wrapper>');
105 node.wrapWith(wrapper);
106 expect(node.parent().sameNode(wrapper)).to.be.true;
109 it('wraps text node with element node', function() {
110 var node = elementNodeFromXML('<div>Alice</div>'),
111 textNode = node.contents()[0],
112 wrapper = elementNodeFromXML('<wrapper></wrapper>');
114 textNode.wrapWith(wrapper);
115 expect(textNode.parent().sameNode(wrapper)).to.be.true;
116 expect(node.contents()).to.have.length(1);
119 it('unwraps element node contents', function() {
120 var node = elementNodeFromXML('<div>Alice <div>has <span>propably</span> a cat</div>!</div>'),
121 outerDiv = node.contents()[1];
123 outerDiv.unwrapContent();
125 expect(node.contents().length).to.equal(3);
126 expect(node.contents()[0].getText()).to.equal('Alice has ');
127 expect(node.contents()[1].getTagName()).to.equal('span');
128 expect(node.contents()[2].getText()).to.equal(' a cat!');
133 describe('Serializing document to WLXML', function() {
134 it('keeps document intact when no changes have been made', function() {
135 var xmlIn = '<section>Alice<div>has</div>a <span class="uri" meta-uri="http://cat.com">cat</span>!</section>',
136 doc = getDocumentFromXML(xmlIn),
137 xmlOut = doc.toXML();
139 var parser = new DOMParser(),
140 input = parser.parseFromString(xmlIn, 'application/xml').childNodes[0],
141 output = parser.parseFromString(xmlOut, 'application/xml').childNodes[0];
143 expect(input.isEqualNode(output)).to.be.true;