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']);
36 describe('White space handling', function() {
39 it('ignores white space surrounding block elements', function() {
40 var node = nodeFromXML('<section> <div></div> </section>'),
41 contents = node.contents();
42 expect(contents).to.have.length(1);
43 expect(contents[0].nodeType).to.equal(Node.ELEMENT_NODE);
45 it('ignores white space between block elements', function() {
46 var node = nodeFromXML('<section><div></div> <div></div></section>'),
47 contents = node.contents();
48 expect(contents).to.have.length(2);
49 [0,1].forEach(function(idx) {
50 expect(contents[idx].nodeType).to.equal(Node.ELEMENT_NODE);
53 it('trims white space from the beginning and the end of the block elements', function() {
54 var node = nodeFromXML('<section> Alice <span>has</span> a cat </section>');
55 expect(node.contents()[0].getText()).to.equal('Alice ');
56 expect(node.contents()[2].getText()).to.equal(' a cat');
58 it('normalizes string of white characters to one space at the inline element boundries', function() {
59 var node = nodeFromXML('<span> Alice has a cat </span>');
60 expect(node.contents()[0].getText()).to.equal(' Alice has a cat ');
62 it('normalizes string of white characters to one space before inline element', function() {
63 var node = nodeFromXML('<div>Alice has <span>a cat</span></div>');
64 expect(node.contents()[0].getText()).to.equal('Alice has ');
66 it('normalizes string of white characters to one space after inline element', function() {
67 var node = nodeFromXML('<div>Alice has <span>a</span> cat</div>');
68 expect(node.contents()[2].getText()).to.equal(' cat');
72 describe('formatting output xml', function() {
74 /*jshint multistr: true */
76 it('keeps white space between XML nodes', function() {
77 var xmlIn = '<section>\n\n\n<div></div>\n\n\n<div></div>\n\n\n</section>',
78 doc = getDocumentFromXML(xmlIn),
81 var partsIn = xmlIn.split('\n\n\n'),
82 partsOut = xmlOut.split('\n\n\n');
84 expect(partsIn).to.deep.equal(partsOut);
87 it('keeps white space between XML nodes - inline case', function() {
88 var xmlIn = '<section>\n\n\n<span></span>\n\n\n<span></span>\n\n\n</section>',
89 doc = getDocumentFromXML(xmlIn),
92 var partsIn = xmlIn.split('\n\n\n'),
93 partsOut = xmlOut.split('\n\n\n');
94 expect(partsIn).to.deep.equal(partsOut);
97 it('keeps white space at the beginning of text', function() {
98 var xmlIn = '<section> abc<div>some div</div> abc</section>',
99 doc = getDocumentFromXML(xmlIn),
100 xmlOut = doc.toXML();
102 expect(xmlOut).to.equal(xmlIn);
105 // it('nests new children block elements', function() {
106 // var doc = getDocumentFromXML('<section></section>');
108 // doc.root.append({tag: 'header'});
110 // var xmlOut = doc.toXML();
111 // expect(xmlOut.split('\n ')[0]).to.equal('<section>', 'nesting start ok');
112 // expect(xmlOut.split('\n').slice(-1)[0]).to.equal('</section>', 'nesting end ok');
116 // it('doesn\'t nest new children inline elements', function() {
117 // var doc = getDocumentFromXML('<section></section>');
119 // doc.root.append({tag: 'span'});
121 // var xmlOut = doc.toXML();
122 // expect(xmlOut).to.equal('<section><span></span></section>');
125 it('keeps original white space at the end of text', function() {
127 var xmlIn = '<header> Some text ended with white space \
129 <span class="uri">Some text</span> some text\
132 doc = getDocumentFromXML(xmlIn),
133 xmlOut = doc.toXML();
135 expect(xmlOut).to.equal(xmlIn);
138 it('keeps white space around text node', function() {
139 var xmlIn = '<section>\
140 <header>header1</header>\
141 Some text surrounded by white space\
142 <header>header2</header>\
144 doc = getDocumentFromXML(xmlIn),
145 xmlOut = doc.toXML();
146 expect(xmlOut).to.equal(xmlIn);
149 it('keeps white space around text node - last node case', function() {
150 var xmlIn = '<section>\
151 <header>header</header>\
153 Some text surrounded by white space\
156 doc = getDocumentFromXML(xmlIn),
157 xmlOut = doc.toXML();
159 expect(xmlOut).to.equal(xmlIn);
162 it('keeps white space after detaching text element', function() {
163 var xmlIn = '<section><header>header</header>\n\
168 expectedXmlOut = '<section><header>header</header>\n\
173 doc = getDocumentFromXML(xmlIn),
174 contents = doc.root.contents(),
175 text = contents[contents.length-1];
177 expect(text.getText()).to.equal('text1');
181 var xmlOut = doc.toXML();
182 expect(xmlOut).to.equal(expectedXmlOut);
187 describe('Extension', function() {
188 var doc, extension, testClassNode;
190 beforeEach(function() {
191 doc = getDocumentFromXML('<section>Alice<div class="test_class"></div><div class="test_class.a"></div></section>');
194 it('allows adding method to an ElementNode of specific class', function() {
195 extension = {wlxmlClass: {test_class: {methods: {
196 testMethod: function() { return this; }
198 doc.registerExtension(extension);
199 testClassNode = doc.root.contents()[1];
200 expect(testClassNode.object.testMethod().sameNode(testClassNode)).to.equal(true, '1');
203 it('allows adding non-function properties to an ElementNode of specific class', function() {
204 extension = {wlxmlClass: {test_class: {methods: {
207 doc.registerExtension(extension);
208 testClassNode = doc.root.contents()[1];
209 expect(testClassNode.object.testProp).to.equal(123);
212 it('allows adding transformation to an ElementNode of specific class', function() {
213 extension = {wlxmlClass: {test_class: {transformations: {
214 testTransformation: function() { return this; },
215 testTransformation2: {impl: function() { return this; }}
217 doc.registerExtension(extension);
218 testClassNode = doc.root.contents()[1];
219 expect(testClassNode.object.testTransformation().sameNode(testClassNode)).to.equal(true, '1');
220 expect(testClassNode.object.testTransformation2().sameNode(testClassNode)).to.equal(true, '1');
223 it('added methods are inherited by nodes with subclasses', function() {
224 extension = {wlxmlClass: {test_class: {methods: {
225 testMethod: function() { return this; }
227 doc.registerExtension(extension);
228 testClassNode = doc.root.contents()[2];
229 expect(testClassNode.object.testMethod().sameNode(testClassNode)).to.equal(true);
231 it('added transformations are inherited by nodes with subclasses', function() {
232 extension = {wlxmlClass: {test_class: {transformations: {
233 testTransformation: function() { return this; },
234 testTransformation2: {impl: function() { return this; }}
236 doc.registerExtension(extension);
237 testClassNode = doc.root.contents()[2];
238 expect(testClassNode.object.testTransformation().sameNode(testClassNode)).to.equal(true, '1');
239 expect(testClassNode.object.testTransformation2().sameNode(testClassNode)).to.equal(true, '2');
243 describe('Context roots', function() {
244 var doc = getDocumentFromXML('<section><div class="a"><div class="b"><div class="c"></div></div></div></section>');
245 doc.registerExtension({wlxmlClass: {a: {methods: {
246 isContextRoot: function(node) {
247 return node.getClass() === 'b';
251 var divA = doc.root.contents()[0],
252 divB = divA.contents()[0],
253 divC = divB.contents()[0];
255 it('allows extensions declaring a node as a context root', function() {
256 expect(divC.isContextRoot()).to.equal(false, 'c is not a context root');
257 expect(divB.isContextRoot()).to.equal(true, 'b is a context root');
258 expect(divA.isContextRoot()).to.equal(false, 'a is not a context root');
261 it('closes context for parent context quering methods', function() {
262 expect(divC.isInside('b')).to.equal(true, 'c inside b');
263 expect(divC.isInside('a')).to.equal(false, 'c not inside a');
264 expect(divC.isInside({tagName: 'section'})).to.equal(false, 'c not inside section');
266 expect(divB.isInside('a')).to.equal(true, 'b inside a');
267 expect(divB.isInside({tagName: 'section'})).to.equal(true, 'b inside section');
269 expect(divA.isInside({tagName: 'section'})).to.equal(true, 'a inside section');