4 ], function(chai, wlxml) {
9 /* global it, describe, beforeEach */
11 var expect = chai.expect;
13 var nodeFromXML = function(xml) {
14 return wlxml.WLXMLElementNodeFromXML(xml);
17 var getDocumentFromXML = function(xml, options) {
18 return wlxml.WLXMLDocumentFromXML(xml, options || {});
22 describe('WLXMLDocument', function() {
24 describe('Basic wlxml element node properties', function() {
25 it('returns its class', function() {
26 var node = nodeFromXML('<header class="class.subclass"></header>');
27 expect(node.getClass()).to.equal('class.subclass');
30 it('returns its class hierarchy', function() {
31 var node = nodeFromXML('<div class="a.b.c"></div>');
32 expect(node.getClassHierarchy()).to.eql(['', 'a', 'a.b', 'a.b.c']);
35 it('returns unregistered attributes', function() {
38 attrs: {'attr1': {type: 'string'}}
41 doc = getDocumentFromXML('<span class="testClass" attr="val" attr1="val1"></span>', {wlxmlClasses: testClasses});
42 expect(doc.root.getOtherAttributes()).to.eql({attr: {value:'val'}});
46 describe('WLXML node meta attributes', function() {
48 it('inherits keys from super classes', function() {
51 attrs: {'common': {type: 'string'}}
54 attrs: {'a_attr': {type: 'string'}}
57 attrs: {'a_b_attr': {type: 'string'}}
60 attrs: {'a_b_c_attr': {type: 'string'}}
63 doc = getDocumentFromXML('<section></section>', {wlxmlClasses: testClasses}),
66 expect(section.getMetaAttributes().keys()).to.eql(['common']);
68 section.setClass('a');
69 expect(section.getMetaAttributes().keys().sort()).to.eql(['common', 'a_attr'].sort());
71 section.setClass('a.b');
72 expect(section.getMetaAttributes().keys().sort()).to.eql(['common', 'a_attr', 'a_b_attr'].sort());
74 section.setClass('a.b.c');
75 expect(section.getMetaAttributes().keys().sort()).to.eql(['common', 'a_attr', 'a_b_attr', 'a_b_c_attr'].sort());
78 describe('api', function() {
79 it('returns meta attributes as a dict', function() {
83 attr1: {type: 'string'},
88 node = getDocumentFromXML(
89 '<span class="test" attr1="val1" attr2="2014-01-01"></span>',
90 {wlxmlClasses: testClasses}
92 attrs = node.getMetaAttributes();
94 expect(attrs.keys().sort()).to.eql(['attr1', 'attr2'].sort());
95 expect(attrs.attr1.value).to.equal('val1');
96 expect(attrs.attr1.type).to.equal('string');
97 expect(attrs.attr2.value).to.equal('2014-01-01');
98 expect(attrs.attr2.type).to.equal('date');
100 it('returns undefined value if attribute is missing', function() {
104 attr1: {type: 'string'},
108 node = getDocumentFromXML('<span class="test"></span>', {wlxmlClasses: testClasses}).root,
109 attrs = node.getMetaAttributes();
110 expect(attrs.attr1.value).to.be.undefined;
115 describe('White space handling', function() {
118 it('ignores white space surrounding block elements', function() {
119 var node = nodeFromXML('<section> <div></div> </section>'),
120 contents = node.contents();
121 expect(contents).to.have.length(1);
122 expect(contents[0].nodeType).to.equal(Node.ELEMENT_NODE);
124 it('ignores white space between block elements', function() {
125 var node = nodeFromXML('<section><div></div> <div></div></section>'),
126 contents = node.contents();
127 expect(contents).to.have.length(2);
128 [0,1].forEach(function(idx) {
129 expect(contents[idx].nodeType).to.equal(Node.ELEMENT_NODE);
132 it('trims white space from the beginning and the end of the block elements', function() {
133 var node = nodeFromXML('<section> Alice <span>has</span> a cat </section>');
134 expect(node.contents()[0].getText()).to.equal('Alice ');
135 expect(node.contents()[2].getText()).to.equal(' a cat');
137 it('normalizes string of white characters to one space at the inline element boundries', function() {
138 var node = nodeFromXML('<span> Alice has a cat </span>');
139 expect(node.contents()[0].getText()).to.equal(' Alice has a cat ');
141 it('normalizes string of white characters to one space before inline element', function() {
142 var node = nodeFromXML('<div>Alice has <span>a cat</span></div>');
143 expect(node.contents()[0].getText()).to.equal('Alice has ');
145 it('normalizes string of white characters to one space after inline element', function() {
146 var node = nodeFromXML('<div>Alice has <span>a</span> cat</div>');
147 expect(node.contents()[2].getText()).to.equal(' cat');
151 describe('formatting output xml', function() {
153 /*jshint multistr: true */
155 it('keeps white space between XML nodes', function() {
156 var xmlIn = '<section>\n\n\n<div></div>\n\n\n<div></div>\n\n\n</section>',
157 doc = getDocumentFromXML(xmlIn),
158 xmlOut = doc.toXML();
160 var partsIn = xmlIn.split('\n\n\n'),
161 partsOut = xmlOut.split('\n\n\n');
163 expect(partsIn).to.deep.equal(partsOut);
166 it('keeps white space between XML nodes - inline case', function() {
167 var xmlIn = '<section>\n\n\n<span></span>\n\n\n<span></span>\n\n\n</section>',
168 doc = getDocumentFromXML(xmlIn),
169 xmlOut = doc.toXML();
171 var partsIn = xmlIn.split('\n\n\n'),
172 partsOut = xmlOut.split('\n\n\n');
173 expect(partsIn).to.deep.equal(partsOut);
176 it('keeps white space at the beginning of text', function() {
177 var xmlIn = '<section> abc<div>some div</div> abc</section>',
178 doc = getDocumentFromXML(xmlIn),
179 xmlOut = doc.toXML();
181 expect(xmlOut).to.equal(xmlIn);
184 // it('nests new children block elements', function() {
185 // var doc = getDocumentFromXML('<section></section>');
187 // doc.root.append({tag: 'header'});
189 // var xmlOut = doc.toXML();
190 // expect(xmlOut.split('\n ')[0]).to.equal('<section>', 'nesting start ok');
191 // expect(xmlOut.split('\n').slice(-1)[0]).to.equal('</section>', 'nesting end ok');
195 // it('doesn\'t nest new children inline elements', function() {
196 // var doc = getDocumentFromXML('<section></section>');
198 // doc.root.append({tag: 'span'});
200 // var xmlOut = doc.toXML();
201 // expect(xmlOut).to.equal('<section><span></span></section>');
204 it('keeps original white space at the end of text', function() {
206 var xmlIn = '<header> Some text ended with white space \
208 <span class="uri">Some text</span> some text\
211 doc = getDocumentFromXML(xmlIn),
212 xmlOut = doc.toXML();
214 expect(xmlOut).to.equal(xmlIn);
217 it('keeps white space around text node', function() {
218 var xmlIn = '<section>\
219 <header>header1</header>\
220 Some text surrounded by white space\
221 <header>header2</header>\
223 doc = getDocumentFromXML(xmlIn),
224 xmlOut = doc.toXML();
225 expect(xmlOut).to.equal(xmlIn);
228 it('keeps white space around text node - last node case', function() {
229 var xmlIn = '<section>\
230 <header>header</header>\
232 Some text surrounded by white space\
235 doc = getDocumentFromXML(xmlIn),
236 xmlOut = doc.toXML();
238 expect(xmlOut).to.equal(xmlIn);
241 it('keeps white space after detaching text element', function() {
242 var xmlIn = '<section><header>header</header>\n\
247 expectedXmlOut = '<section><header>header</header>\n\
252 doc = getDocumentFromXML(xmlIn),
253 contents = doc.root.contents(),
254 text = contents[contents.length-1];
256 expect(text.getText()).to.equal('text1');
260 var xmlOut = doc.toXML();
261 expect(xmlOut).to.equal(expectedXmlOut);
266 describe('Extension', function() {
267 var doc, extension, elementNode, textNode, testClassNode;
269 beforeEach(function() {
270 doc = getDocumentFromXML('<section>Alice<div class="test_class"></div><div class="test_class.a"></div></section>');
271 elementNode = doc.root;
272 textNode = doc.root.contents()[0];
273 testClassNode = doc.root.contents('.test_class');
276 expect(testClassNode.object).to.be.undefined;
280 it('allows adding method to an ElementNode of specific class', function() {
281 extension = {wlxmlClass: {test_class: {methods: {
282 testMethod: function() { return this; }
284 doc.registerExtension(extension);
285 testClassNode = doc.root.contents()[1];
286 expect(testClassNode.object.testMethod().sameNode(testClassNode)).to.equal(true, '1');
289 it('allows adding transformation to an ElementNode of specific class', function() {
290 extension = {wlxmlClass: {test_class: {transformations: {
291 testTransformation: function() { return this; },
292 testTransformation2: {impl: function() { return this; }}
294 doc.registerExtension(extension);
295 testClassNode = doc.root.contents()[1];
296 expect(testClassNode.object.testTransformation().sameNode(testClassNode)).to.equal(true, '1');
297 expect(testClassNode.object.testTransformation2().sameNode(testClassNode)).to.equal(true, '1');
300 it('added methods are inherited by nodes with subclasses', function() {
301 extension = {wlxmlClass: {test_class: {methods: {
302 testMethod: function() { return this; }
304 doc.registerExtension(extension);
305 testClassNode = doc.root.contents()[2];
306 expect(testClassNode.object.testMethod().sameNode(testClassNode)).to.equal(true);
308 it('added transformations are inherited by nodes with subclasses', function() {
309 extension = {wlxmlClass: {test_class: {transformations: {
310 testTransformation: function() { return this; },
311 testTransformation2: {impl: function() { return this; }}
313 doc.registerExtension(extension);
314 testClassNode = doc.root.contents()[2];
315 expect(testClassNode.object.testTransformation().sameNode(testClassNode)).to.equal(true, '1');
316 expect(testClassNode.object.testTransformation2().sameNode(testClassNode)).to.equal(true, '2');