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() {
75 it('keeps white space between XML nodes', function() {
76 var xmlIn = '<section>\n\n\n<div></div>\n\n\n<div></div>\n\n\n</section>',
77 doc = getDocumentFromXML(xmlIn),
80 var partsIn = xmlIn.split('\n\n\n'),
81 partsOut = xmlOut.split('\n\n\n');
83 expect(partsIn).to.deep.equal(partsOut);
86 it('keeps white space between XML nodes - inline case', function() {
87 var xmlIn = '<section>\n\n\n<span></span>\n\n\n<span></span>\n\n\n</section>',
88 doc = getDocumentFromXML(xmlIn),
91 var partsIn = xmlIn.split('\n\n\n'),
92 partsOut = xmlOut.split('\n\n\n');
95 expect(partsIn).to.deep.equal(partsOut);
98 it('keeps white space at the beginning of text', function() {
99 var xmlIn = '<section> abc<div>some div</div> abc</section>',
100 doc = getDocumentFromXML(xmlIn),
101 xmlOut = doc.toXML();
103 expect(xmlOut).to.equal(xmlIn);
106 // it('nests new children block elements', function() {
107 // var doc = getDocumentFromXML('<section></section>');
109 // doc.root.append({tag: 'header'});
111 // var xmlOut = doc.toXML();
112 // expect(xmlOut.split('\n ')[0]).to.equal('<section>', 'nesting start ok');
113 // expect(xmlOut.split('\n').slice(-1)[0]).to.equal('</section>', 'nesting end ok');
117 // it('doesn\'t nest new children inline elements', function() {
118 // var doc = getDocumentFromXML('<section></section>');
120 // doc.root.append({tag: 'span'});
122 // var xmlOut = doc.toXML();
123 // expect(xmlOut).to.equal('<section><span></span></section>');
126 it('keeps original white space at the end of text', function() {
128 var xmlIn = '<header> Some text ended with white space \
130 <span class="uri">Some text</span> some text\
133 doc = getDocumentFromXML(xmlIn),
134 xmlOut = doc.toXML();
136 expect(xmlOut).to.equal(xmlIn);
139 it('keeps white space around text node', function() {
140 var xmlIn = '<section>\
141 <header>header1</header>\
142 Some text surrounded by white space\
143 <header>header2</header>\
145 doc = getDocumentFromXML(xmlIn),
146 xmlOut = doc.toXML();
147 expect(xmlOut).to.equal(xmlIn);
150 it('keeps white space around text node - last node case', function() {
151 var xmlIn = '<section>\
152 <header>header</header>\
154 Some text surrounded by white space\
157 doc = getDocumentFromXML(xmlIn),
158 xmlOut = doc.toXML();
160 expect(xmlOut).to.equal(xmlIn);
163 it('keeps white space after detaching text element', function() {
164 var xmlIn = '<section><header>header</header>\n\
169 expectedXmlOut = '<section><header>header</header>\n\
174 doc = getDocumentFromXML(xmlIn),
175 contents = doc.root.contents(),
176 text = contents[contents.length-1];
178 expect(text.getText()).to.equal('text1');
182 var xmlOut = doc.toXML();
183 expect(xmlOut).to.equal(expectedXmlOut);