86fe4627061a977524d6ca60cc5449b1d9bad984
[fnpeditor.git] / src / wlxml / wlxml.test.js
1 define([
2     'libs/chai',
3     './wlxml.js'
4 ], function(chai, wlxml) {
5     
6 'use strict';
7
8 /* global it, describe */
9
10 var expect = chai.expect;
11
12 var nodeFromXML = function(xml) {
13     return wlxml.WLXMLElementNodeFromXML(xml);
14 };
15
16 var getDocumentFromXML = function(xml) {
17     return wlxml.WLXMLDocumentFromXML(xml);
18 };
19
20
21 describe('WLXMLDocument', function() {
22     
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');
27         });
28
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'}]);
32         });
33
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'});
37         });
38     });
39
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);
46         });
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);
53             });
54         });
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');
59         });
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 ');
63         });
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 ');
67         });
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');
71         });
72     });
73
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),
78             xmlOut = doc.toXML();
79
80             var partsIn = xmlIn.split('\n\n\n'),
81                 partsOut = xmlOut.split('\n\n\n');
82
83             expect(partsIn).to.deep.equal(partsOut);
84         });
85
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),
89                 xmlOut = doc.toXML();
90
91             var partsIn = xmlIn.split('\n\n\n'),
92                 partsOut = xmlOut.split('\n\n\n');
93             console.log(xmlIn);
94             console.log(xmlOut);
95             expect(partsIn).to.deep.equal(partsOut);
96         });
97
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();
102
103             expect(xmlOut).to.equal(xmlIn);
104         });
105
106         // it('nests new children block elements', function() {
107         //     var doc = getDocumentFromXML('<section></section>');
108     
109         //     doc.root.append({tag: 'header'});
110
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');
114
115         // });
116
117         // it('doesn\'t nest new children inline elements', function() {
118         //     var doc = getDocumentFromXML('<section></section>');
119     
120         //     doc.root.append({tag: 'span'});
121
122         //     var xmlOut = doc.toXML();
123         //     expect(xmlOut).to.equal('<section><span></span></section>');
124         // });
125
126         it('keeps original white space at the end of text', function() {
127             
128             var xmlIn = '<header>    Some text ended with white space \
129             \
130             <span class="uri">Some text</span> some text\
131         \
132         </header>',
133                 doc = getDocumentFromXML(xmlIn),
134                 xmlOut = doc.toXML();
135         
136             expect(xmlOut).to.equal(xmlIn);
137         });
138
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>\
144         </section>',
145                 doc = getDocumentFromXML(xmlIn),
146                 xmlOut = doc.toXML();
147             expect(xmlOut).to.equal(xmlIn);
148         });
149
150         it('keeps white space around text node - last node case', function() {
151             var xmlIn = '<section>\
152             <header>header</header>\
153                 \
154             Some text surrounded by white space\
155                 \
156         </section>',
157                 doc = getDocumentFromXML(xmlIn),
158                 xmlOut = doc.toXML();
159
160             expect(xmlOut).to.equal(xmlIn);
161         });
162
163         it('keeps white space after detaching text element', function() {
164             var xmlIn = '<section><header>header</header>\n\
165                 \n\
166             text1\n\
167                 \n\
168         </section>',
169                 expectedXmlOut = '<section><header>header</header>\n\
170                 \n\
171             \n\
172                 \n\
173         </section>',
174                 doc = getDocumentFromXML(xmlIn),
175                 contents = doc.root.contents(),
176                 text = contents[contents.length-1];
177             
178             expect(text.getText()).to.equal('text1');
179
180             text.detach();
181
182             var xmlOut = doc.toXML();
183             expect(xmlOut).to.equal(expectedXmlOut);
184         });
185
186     });
187
188 });
189
190 });