4 ], function(chai, wlxml) {
8 /* global it, describe */
10 var expect = chai.expect;
12 var nodeFromXML = function(xml) {
13 return wlxml.WLXMLElementNodeFromXML(xml);
16 var getDocumentFromXML = function(xml) {
17 return wlxml.WLXMLDocumentFromXML(xml);
21 describe('WLXMLDocument', function() {
23 describe('Basic wlxml element node properties', function() {
24 it('returns its class', function() {
25 var node = nodeFromXML('<header class="class.subclass"></header>');
26 expect(node.getClass()).to.equal('class.subclass');
29 it('returns its attributes as dict', function() {
30 var node = nodeFromXML('<span meta-attr1="val1" meta-attr2="val2"></span>');
31 expect(node.getMetaAttributes()).to.eql([{name: 'attr1', value: 'val1'}, {name: 'attr2', value: 'val2'}]);
34 it('returns attributes other than class and meta-* as other attributes', function() {
35 var node = nodeFromXML('<span class="uri" meta-attr="val" attr1="val1" attr2="val2"></span>');
36 expect(node.getOtherAttributes()).to.eql({attr1: 'val1', attr2: 'val2'});
40 describe('White space handling', function() {
41 it('ignores white space surrounding block elements', function() {
42 var node = nodeFromXML('<section> <div></div> </section>'),
43 contents = node.contents();
44 expect(contents).to.have.length(1);
45 expect(contents[0].nodeType).to.equal(Node.ELEMENT_NODE);
47 it('ignores white space between block elements', function() {
48 var node = nodeFromXML('<section><div></div> <div></div></section>'),
49 contents = node.contents();
50 expect(contents).to.have.length(2);
51 [0,1].forEach(function(idx) {
52 expect(contents[idx].nodeType).to.equal(Node.ELEMENT_NODE);
55 it('trims white space from the beginning and the end of the block elements', function() {
56 var node = nodeFromXML('<section> Alice <span>has</span> a cat </section>');
57 expect(node.contents()[0].getText()).to.equal('Alice ');
58 expect(node.contents()[2].getText()).to.equal(' a cat');
60 it('normalizes string of white characters to one space at the inline element boundries', function() {
61 var node = nodeFromXML('<span> Alice has a cat </span>');
62 expect(node.contents()[0].getText()).to.equal(' Alice has a cat ');
64 it('normalizes string of white characters to one space before inline element', function() {
65 var node = nodeFromXML('<div>Alice has <span>a cat</span></div>');
66 expect(node.contents()[0].getText()).to.equal('Alice has ');
68 it('normalizes string of white characters to one space after inline element', function() {
69 var node = nodeFromXML('<div>Alice has <span>a</span> cat</div>');
70 expect(node.contents()[2].getText()).to.equal(' cat');
74 describe('formatting output xml', function() {
76 /*jshint multistr: true */
78 it('keeps white space between XML nodes', function() {
79 var xmlIn = '<section>\n\n\n<div></div>\n\n\n<div></div>\n\n\n</section>',
80 doc = getDocumentFromXML(xmlIn),
83 var partsIn = xmlIn.split('\n\n\n'),
84 partsOut = xmlOut.split('\n\n\n');
86 expect(partsIn).to.deep.equal(partsOut);
89 it('keeps white space between XML nodes - inline case', function() {
90 var xmlIn = '<section>\n\n\n<span></span>\n\n\n<span></span>\n\n\n</section>',
91 doc = getDocumentFromXML(xmlIn),
94 var partsIn = xmlIn.split('\n\n\n'),
95 partsOut = xmlOut.split('\n\n\n');
96 expect(partsIn).to.deep.equal(partsOut);
99 it('keeps white space at the beginning of text', function() {
100 var xmlIn = '<section> abc<div>some div</div> abc</section>',
101 doc = getDocumentFromXML(xmlIn),
102 xmlOut = doc.toXML();
104 expect(xmlOut).to.equal(xmlIn);
107 // it('nests new children block elements', function() {
108 // var doc = getDocumentFromXML('<section></section>');
110 // doc.root.append({tag: 'header'});
112 // var xmlOut = doc.toXML();
113 // expect(xmlOut.split('\n ')[0]).to.equal('<section>', 'nesting start ok');
114 // expect(xmlOut.split('\n').slice(-1)[0]).to.equal('</section>', 'nesting end ok');
118 // it('doesn\'t nest new children inline elements', function() {
119 // var doc = getDocumentFromXML('<section></section>');
121 // doc.root.append({tag: 'span'});
123 // var xmlOut = doc.toXML();
124 // expect(xmlOut).to.equal('<section><span></span></section>');
127 it('keeps original white space at the end of text', function() {
129 var xmlIn = '<header> Some text ended with white space \
131 <span class="uri">Some text</span> some text\
134 doc = getDocumentFromXML(xmlIn),
135 xmlOut = doc.toXML();
137 expect(xmlOut).to.equal(xmlIn);
140 it('keeps white space around text node', function() {
141 var xmlIn = '<section>\
142 <header>header1</header>\
143 Some text surrounded by white space\
144 <header>header2</header>\
146 doc = getDocumentFromXML(xmlIn),
147 xmlOut = doc.toXML();
148 expect(xmlOut).to.equal(xmlIn);
151 it('keeps white space around text node - last node case', function() {
152 var xmlIn = '<section>\
153 <header>header</header>\
155 Some text surrounded by white space\
158 doc = getDocumentFromXML(xmlIn),
159 xmlOut = doc.toXML();
161 expect(xmlOut).to.equal(xmlIn);
164 it('keeps white space after detaching text element', function() {
165 var xmlIn = '<section><header>header</header>\n\
170 expectedXmlOut = '<section><header>header</header>\n\
175 doc = getDocumentFromXML(xmlIn),
176 contents = doc.root.contents(),
177 text = contents[contents.length-1];
179 expect(text.getText()).to.equal('text1');
183 var xmlOut = doc.toXML();
184 expect(xmlOut).to.equal(expectedXmlOut);