4 ], function(chai, smartxml) {
8 /* global describe, it */
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');
47 describe('Manipulations', function() {
49 it('appends element node to another element node', function() {
50 var node1 = elementNodeFromParams({tag: 'div'}),
51 node2 = elementNodeFromParams({tag: 'a'});
53 expect(node1.contents()[0].sameNode(node2)).to.be.true;
56 it('wraps element node with another element node', function() {
57 var node = elementNodeFromXML('<div></div>'),
58 wrapper = elementNodeFromXML('<wrapper></wrapper>');
60 node.wrapWith(wrapper);
61 expect(node.parent().sameNode(wrapper)).to.be.true;
64 it('wraps text node with element node', function() {
65 var node = elementNodeFromXML('<div>Alice</div>'),
66 textNode = node.contents()[0],
67 wrapper = elementNodeFromXML('<wrapper></wrapper>');
69 textNode.wrapWith(wrapper);
70 expect(textNode.parent().sameNode(wrapper)).to.be.true;
71 expect(node.contents()).to.have.length(1);
74 it('unwraps element node contents', function() {
75 var node = elementNodeFromXML('<div>Alice <div>has <span>propably</span> a cat</div>!</div>'),
76 outerDiv = node.contents()[1];
78 outerDiv.unwrapContent();
80 expect(node.contents().length).to.equal(3);
81 expect(node.contents()[0].getText()).to.equal('Alice has ');
82 expect(node.contents()[1].getTagName()).to.equal('span');
83 expect(node.contents()[2].getText()).to.equal(' a cat!');
88 describe('Serializing document to WLXML', function() {
89 it('keeps document intact when no changes have been made', function() {
90 var xmlIn = '<section>Alice<div>has</div>a <span class="uri" meta-uri="http://cat.com">cat</span>!</section>',
91 doc = getDocumentFromXML(xmlIn),
94 var parser = new DOMParser(),
95 input = parser.parseFromString(xmlIn, 'application/xml').childNodes[0],
96 output = parser.parseFromString(xmlOut, 'application/xml').childNodes[0];
98 expect(input.isEqualNode(output)).to.be.true;