3 'modules/documentCanvas/canvas/canvas',
4 'modules/documentCanvas/canvas/documentElement'
5 ], function(chai, canvas, documentElement) {
9 var expect = chai.expect;
12 describe('Canvas', function() {
14 describe('Internal HTML representation of a DocumentNodeElement', function() {
15 it('is always a div tag', function() {
16 ['section', 'header', 'span', 'aside', 'figure'].forEach(function(tagName) {
17 var dom = canvas.fromXML('<' + tagName + '></' + tagName + '>').doc().dom();
18 expect(dom.prop('tagName')).to.equal('DIV', tagName + ' is represented as div');
21 it('has wlxml tag put into wlxml-tag attribute', function() {
22 var dom = canvas.fromXML('<section></section>').doc().dom();
23 expect(dom.attr('wlxml-tag')).to.equal('section');
25 it('has wlxml class put into wlxml-class, dots replaced with dashes', function() {
26 var dom = canvas.fromXML('<section class="some.class"></section>').doc().dom();
27 expect(dom.attr('wlxml-class')).to.equal('some-class');
31 describe('Internal HTML representation of a DocumentTextElement', function() {
32 it('is just a TextNode', function() {
33 var dom = canvas.fromXML('<section>Alice</section>').doc().children()[0].dom();
34 expect(dom[0].nodeType === Node.TEXT_NODE);
38 describe('basic properties', function() {
39 it('renders empty document when canvas created from empty XML', function() {
40 var c = canvas.fromXML('');
41 expect(c.doc()).to.equal(null);
44 it('gives access to its document root node', function() {
45 var c = canvas.fromXML('<section></section>');
46 expect(c.doc().wlxmlTag).to.equal('section');
49 describe('DocumentTextElement', function() {
50 it('can have its content set', function() {
51 var c = canvas.fromXML('<section>Alice</section>'),
53 text = root.children()[0];
55 text.setText('a cat');
56 expect(root.children()[0].getText()).to.equal('a cat');
60 describe('DocumentNodeElement', function() {
61 it('knows index of its child', function() {
62 var c = canvas.fromXML('<section><div></div><header></header><span></span></section>'),
64 child = root.children()[1];
65 expect(root.childIndex(child)).to.equal(1);
68 it('knows WLXML tag it renders', function(){
69 var c = canvas.fromXML('<section></section>'),
71 expect(section.getWlxmlTag()).to.equal('section', 'initial tag is section');
72 section.setWlxmlTag('header');
73 expect(section.getWlxmlTag()).to.equal('header', 'tag is changed to header');
76 it('knows WLXML class of a WLXML tag it renders', function(){
77 var c = canvas.fromXML('<section class="some.class"></section>'),
79 expect(section.getWlxmlClass()).to.equal('some.class');
80 section.setWlxmlClass('some.other.class');
81 expect(section.getWlxmlClass()).to.equal('some.other.class');
88 describe('document representation api', function() {
89 describe('document root element', function() {
90 var c = canvas.fromXML('<section></section>');
91 it('exists', function() {
92 expect(c.doc()).to.be.instanceOf(documentElement.DocumentElement);
94 it('is of type DocumentNodeElement', function() {
95 expect(c.doc()).to.be.instanceOf(documentElement.DocumentNodeElement);
99 describe('DocumentElements comparison', function() {
100 it('reports dwo DocumentElements to be the same when they represent the same wlxml document element', function() {
101 var c = canvas.fromXML('<section><div></div><div></div></section>'),
102 first_div1 = c.doc().children()[0],
103 first_div2 = c.doc().children()[0],
104 second_div = c.doc().children()[1];
105 expect(first_div1.sameNode(first_div1)).to.be.true;
106 expect(first_div1.sameNode(first_div2)).to.be.true;
107 expect(first_div1.sameNode(second_div)).to.be.false;
111 describe('traversing', function() {
112 it('reports element nodes', function() {
113 var c = canvas.fromXML('<section><div></div></section>'),
114 children = c.doc().children();
115 expect(children.length).to.equal(1);
116 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
118 c = canvas.fromXML('<section><div></div><div></div></section>'),
119 children = c.doc().children();
120 expect(children.length).to.equal(2);
121 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
122 expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
125 it('reports text nodes', function() {
126 var c = canvas.fromXML('<section>Alice</section>'),
127 children = c.doc().children();
128 expect(children.length).to.equal(1);
129 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
132 describe('accessing parents', function() {
133 it('returns DocumentNodeElement representing parent in wlxml document as DocumentNodeElement parent', function() {
134 var c = canvas.fromXML('<section><div></div></section>'),
135 div = c.doc().children()[0];
136 expect(div.parent().sameNode(c.doc())).to.be.true;
138 it('returns DocumentNodeElement representing parent in wlxml document as DocumentTextElement parent', function() {
139 var c = canvas.fromXML('<section>Alice</section>'),
140 text = c.doc().children()[0];
141 expect(text.parent().sameNode(c.doc())).to.be.true;
145 describe('free text handling', function() {
146 it('sees free text', function() {
147 var c = canvas.fromXML('<section>Alice <span>has</span> a cat</section>'),
148 children = c.doc().children();
149 expect(children.length).to.equal(3);
150 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
151 expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
152 expect(children[2]).to.be.instanceOf(documentElement.DocumentTextElement);
156 describe('white characters handling', function() {
157 it('says empty element node has no children', function() {
158 var c = canvas.fromXML('<section></section>');
159 expect(c.doc().children().length).to.equal(0);
161 it('says element node with one space has one DocumentTextElement', function() {
162 var c = canvas.fromXML('<section> </section>');
163 expect(c.doc().children().length).to.equal(1);
164 expect(c.doc().children()[0]).to.be.instanceOf(documentElement.DocumentTextElement);
165 expect(c.doc().children()[0].getText()).to.equal(' ');
167 it('ignores white space surrounding block elements', function() {
168 var c = canvas.fromXML('<section> <div></div> </section>');
169 var children = c.doc().children();
170 expect(children.length).to.equal(1);
171 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
173 it('ignores white space between block elements', function() {
174 var c = canvas.fromXML('<section><div></div> <div></div></section>');
175 var children = c.doc().children();
176 expect(children.length === 2);
177 [0,1].forEach(function(idx) {
178 expect(children[idx]).to.be.instanceOf(documentElement.DocumentNodeElement);
182 it('trims white space from the beginning and the end of the block elements', function() {
183 var c = canvas.fromXML('<section> Alice <span>has</span> a cat </section>');
184 expect(c.doc().children()[0].getText()).to.equal('Alice ');
185 expect(c.doc().children()[2].getText()).to.equal(' a cat');
188 it('normalizes string of white characters to one space at the inline element boundries', function() {
189 var c = canvas.fromXML('<span> Alice has a cat </span>');
190 expect(c.doc().children()[0].getText()).to.equal(' Alice has a cat ');
193 it('normalizes string of white characters to one space before inline element', function() {
194 var c = canvas.fromXML('<div>Alice has <span>a cat</span></div>');
195 expect(c.doc().children()[0].getText()).to.equal('Alice has ');
198 it('normalizes string of white characters to one space after inline element', function() {
199 var c = canvas.fromXML('<div>Alice has <span>a</span> cat</div>');
200 expect(c.doc().children()[2].getText()).to.equal(' cat');
205 describe('manipulation api', function() {
207 describe('Basic Element inserting', function() {
208 it('can put new NodeElement at the end', function() {
209 var c = canvas.fromXML('<section></section>'),
210 appended = c.doc().append({tag: 'header', klass: 'some.class'}),
211 children = c.doc().children();
213 expect(children.length).to.equal(1);
214 expect(children[0].sameNode(appended));
217 it('can put new TextElement at the end', function() {
218 var c = canvas.fromXML('<section></section>'),
219 appended = c.doc().append({text: 'Alice'}),
220 children = c.doc().children();
222 expect(children.length).to.equal(1);
223 expect(children[0].sameNode(appended));
224 expect(children[0].getText()).to.equal('Alice');
227 it('can put new NodeElement after another NodeElement', function() {
228 var c = canvas.fromXML('<section><div></div></section>'),
229 div = c.doc().children()[0],
230 added = div.after({tag: 'header', klass: 'some.class'}),
231 children = c.doc().children();
232 expect(children.length).to.equal(2);
233 expect(children[1].sameNode(added));
236 it('can put new Nodeelement before another element', function() {
237 var c = canvas.fromXML('<section><div></div></section>'),
238 div = c.doc().children()[0],
239 added = div.before({tag: 'header', klass: 'some.class'}),
240 children = c.doc().children();
241 expect(children.length).to.equal(2);
242 expect(children[0].sameNode(added));
245 it('can put new DocumentNodeElement after DocumentTextElement', function() {
246 var c = canvas.fromXML('<section>Alice</section>'),
247 text = c.doc().children()[0],
248 added = text.after({tag: 'p'}),
249 children = c.doc().children();
251 expect(children.length).to.equal(2);
252 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
253 expect(children[0].getText()).to.equal('Alice');
254 expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
255 expect(children[1].sameNode(added)).to.be.true;
257 it('can put new DocumentNodeElement before DocumentTextElement', function() {
258 var c = canvas.fromXML('<section>Alice</section>'),
259 text = c.doc().children()[0],
260 added = text.before({tag: 'p'}),
261 children = c.doc().children();
263 expect(children.length).to.equal(2);
264 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
265 expect(children[0].sameNode(added)).to.be.true;
266 expect(children[1]).to.be.instanceOf(documentElement.DocumentTextElement);
267 expect(children[1].getText()).to.equal('Alice');
271 describe('Splitting text', function() {
273 it('splits DocumentTextElement\'s parent into two DocumentNodeElements of the same type', function() {
274 var c = canvas.fromXML('<section><header>Some header</header></section>'),
276 text = section.children()[0].children()[0];
278 text.split({offset: 5});
279 expect(section.children().length).to.equal(2, 'section has two children');
281 var header1 = section.children()[0];
282 var header2 = section.children()[1];
284 expect(header1.wlxmlTag).to.equal('header', 'first section child represents wlxml header');
285 expect(header1.children().length).to.equal(1, 'first header has one text child');
286 expect(header1.children()[0].getText()).to.equal('Some ', 'first header has correct content');
287 expect(header2.wlxmlTag).to.equal('header', 'second section child represents wlxml header');
288 expect(header2.children().length).to.equal(1, 'second header has one text child');
289 expect(header2.children()[0].getText()).to.equal('header', 'second header has correct content');
292 it('keeps DocumentTextElement\'s parent\'s children elements intact', function() {
293 var c = canvas.fromXML('\
296 A <span>fancy</span> and <span>nice</span> header\
300 header = section.children()[0],
301 textAnd = header.children()[2];
303 textAnd.split({offset: 2});
305 var sectionChildren = section.children();
306 expect(sectionChildren.length).to.equal(2, 'Section has two children');
307 expect(sectionChildren[0].wlxmlTag).to.equal('header', 'First section element is a wlxml header');
308 expect(sectionChildren[1].wlxmlTag).to.equal('header', 'Second section element is a wlxml header');
310 var firstHeaderChildren = sectionChildren[0].children();
311 expect(firstHeaderChildren.length).to.equal(3, 'First header has three children');
312 expect(firstHeaderChildren[0].getText()).to.equal('A ', 'First header starts with a text');
313 expect(firstHeaderChildren[1].wlxmlTag).to.equal('span', 'First header has span in the middle');
314 expect(firstHeaderChildren[2].getText()).to.equal(' a', 'First header ends with text');
316 var secondHeaderChildren = sectionChildren[1].children();
317 expect(secondHeaderChildren.length).to.equal(3, 'Second header has three children');
318 expect(secondHeaderChildren[0].getText()).to.equal('nd ', 'Second header starts with text');
319 expect(secondHeaderChildren[1].wlxmlTag).to.equal('span', 'Second header has span in the middle');
320 expect(secondHeaderChildren[2].getText()).to.equal(' header', 'Second header ends with text');
324 describe('wrapping', function() {
325 it('wraps DocumentNodeElement', function() {
326 var c = canvas.fromXML('<section><div></div></section>'),
327 div = c.doc().children()[0];
329 var returned = div.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
330 parent = div.parent(),
331 parent2 = c.doc().children()[0];
333 expect(returned.sameNode(parent)).to.be.true;
334 expect(returned.sameNode(parent2)).to.be.true;
336 it('wraps DocumentTextElement', function() {
337 var c = canvas.fromXML('<section>Alice</section>'),
338 text = c.doc().children()[0];
340 var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
341 parent = text.parent(),
342 parent2 = c.doc().children()[0];
344 expect(returned.sameNode(parent)).to.be.true;
345 expect(returned.sameNode(parent2)).to.be.true;
348 it('wraps part of DocumentTextElement', function() {
349 var c = canvas.fromXML('<section>Alice has a cat</section>'),
350 text = c.doc().children()[0];
352 var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class', start: 5, end: 12}),
353 children = c.doc().children();
355 expect(children.length).to.equal(3);
357 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
358 expect(children[0].getText()).to.equal('Alice');
360 expect(children[1].sameNode(returned)).to.be.true;
361 expect(children[1].children().length).to.equal(1);
362 expect(children[1].children()[0].getText()).to.equal(' has a ');
364 expect(children[2]).to.be.instanceOf(documentElement.DocumentTextElement);
365 expect(children[2].getText()).to.equal('cat');
368 it('wraps text spanning multiple sibling DocumentTextNodes', function() {
369 var c = canvas.fromXML('<section>Alice has a <span>small</span> cat</section>'),
371 wrapper = c.wrapText({
373 _with: {tag: 'span', klass: 'some.class'},
379 expect(section.children().length).to.equal(2);
380 expect(section.children()[0]).to.be.instanceOf(documentElement.DocumentTextElement);
381 expect(section.children()[0].getText()).to.equal('Alice ');
383 var wrapper2 = section.children()[1];
384 expect(wrapper2.sameNode(wrapper)).to.be.true;
386 var wrapperChildren = wrapper.children();
387 expect(wrapperChildren.length).to.equal(3);
388 expect(wrapperChildren[0].getText()).to.equal('has a ');
390 expect(wrapperChildren[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
391 expect(wrapperChildren[1].children().length).to.equal(1);
392 expect(wrapperChildren[1].children()[0].getText()).to.equal('small');
394 expect(wrapperChildren[2].getText()).to.equal(' cat');
399 describe('Lists api', function() {
400 it('allows creation of a list from existing sibling DocumentElements', function() {
401 var c = canvas.fromXML('\
409 textAlice = section.children()[0],
410 divCat = section.children()[3]
412 c.list.create({element1: textAlice, element2: divCat});
414 expect(section.children().length).to.equal(1, 'section has one child element');
416 var list = section.children()[0];
417 expect(list.is('list')).to.equal(true, 'section\'s only child is a list');
418 expect(list.children().length).to.equal(4, 'list contains four elements');
419 list.children().forEach(function(child) {
420 expect(child.getWlxmlClass()).to.equal('item', 'list childs have wlxml class of item');