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'});
71 describe('Manipulations', function() {
73 it('appends element node to another element node', function() {
74 var node1 = elementNodeFromParams({tag: 'div'}),
75 node2 = elementNodeFromParams({tag: 'a'});
77 expect(node1.contents()[0].sameNode(node2)).to.be.true;
80 it('wraps element node with another element node', function() {
81 var node = elementNodeFromXML('<div></div>'),
82 wrapper = elementNodeFromXML('<wrapper></wrapper>');
84 node.wrapWith(wrapper);
85 expect(node.parent().sameNode(wrapper)).to.be.true;
88 it('wraps text node with element node', function() {
89 var node = elementNodeFromXML('<div>Alice</div>'),
90 textNode = node.contents()[0],
91 wrapper = elementNodeFromXML('<wrapper></wrapper>');
93 textNode.wrapWith(wrapper);
94 expect(textNode.parent().sameNode(wrapper)).to.be.true;
95 expect(node.contents()).to.have.length(1);
98 it('unwraps element node contents', function() {
99 var node = elementNodeFromXML('<div>Alice <div>has <span>propably</span> a cat</div>!</div>'),
100 outerDiv = node.contents()[1];
102 outerDiv.unwrapContent();
104 expect(node.contents().length).to.equal(3);
105 expect(node.contents()[0].getText()).to.equal('Alice has ');
106 expect(node.contents()[1].getTagName()).to.equal('span');
107 expect(node.contents()[2].getText()).to.equal(' a cat!');
112 describe('Serializing document to WLXML', function() {
113 it('keeps document intact when no changes have been made', function() {
114 var xmlIn = '<section>Alice<div>has</div>a <span class="uri" meta-uri="http://cat.com">cat</span>!</section>',
115 doc = getDocumentFromXML(xmlIn),
116 xmlOut = doc.toXML();
118 var parser = new DOMParser(),
119 input = parser.parseFromString(xmlIn, 'application/xml').childNodes[0],
120 output = parser.parseFromString(xmlOut, 'application/xml').childNodes[0];
122 expect(input.isEqualNode(output)).to.be.true;