77b3b19b7ae504649d70da0f43898815f9bfa952
[fnpeditor.git] / modules / documentCanvas / canvas / canvas.test3.js
1 define([
2 'libs/chai',
3 'modules/documentCanvas/canvas/canvas',
4 'modules/documentCanvas/canvas/documentElement'
5 ], function(chai, canvas, documentElement) {
6     
7 'use strict';
8
9 var expect = chai.expect;
10
11
12 describe('Canvas', function() {
13
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');
19             });
20         });
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');
24         });
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');
28         });
29     });
30
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);
35         });
36     });
37
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);
42         });
43
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');
47         });
48
49         describe('DocumentTextElement', function() {
50             it('can have its content set', function() {
51                 var c = canvas.fromXML('<section>Alice</section>'),
52                     root = c.doc(),
53                     text = root.children()[0];
54                 
55                 text.setText('a cat');
56                 expect(root.children()[0].getText()).to.equal('a cat');
57             });
58         });
59
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>'),
63                     root = c.doc(),
64                     child = root.children()[1];
65                 expect(root.childIndex(child)).to.equal(1);
66             });
67
68             it('knows WLXML tag it renders', function(){
69                 var c = canvas.fromXML('<section></section>'),
70                     section = c.doc();
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');
74             });
75
76             it('knows WLXML class of a WLXML tag it renders', function(){
77                 var c = canvas.fromXML('<section class="some.class"></section>'),
78                     section = c.doc();
79                 expect(section.getWlxmlClass()).to.equal('some.class');
80                 section.setWlxmlClass('some.other.class');
81                 expect(section.getWlxmlClass()).to.equal('some.other.class');
82             });
83         });
84     });
85
86
87
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);
93             });
94             it('is of type DocumentNodeElement', function() {
95                 expect(c.doc()).to.be.instanceOf(documentElement.DocumentNodeElement);
96             });
97         });
98
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;
108             });
109         });
110
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);
117
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);
123             });
124             
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);
130             });
131
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;
137                 });
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;
142                 });
143             });
144
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);
153                     });
154             });
155             
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);
160                 });
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(' ');
166                 });
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);
172                 });
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);
179                     });
180                 });
181
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');
186                 });
187
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 ');
191                 });
192
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 ');
196                 });
197                 
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');
201                 });
202             });
203         });
204
205         describe('manipulation api', function() {
206
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();
212
213                     expect(children.length).to.equal(1);
214                     expect(children[0].sameNode(appended));
215                 });
216
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();
221
222                     expect(children.length).to.equal(1);
223                     expect(children[0].sameNode(appended));
224                     expect(children[0].getText()).to.equal('Alice');
225                 });
226
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));
234                 });
235
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));
243                 });
244
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();
250
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;
256                 });
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();
262
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');
268                 });
269             });
270
271             describe('Splitting text', function() {
272                 
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>'),
275                         section = c.doc(),
276                         text = section.children()[0].children()[0];
277
278                     text.split({offset: 5});
279                     expect(section.children().length).to.equal(2, 'section has two children');
280                     
281                     var header1 = section.children()[0];
282                     var header2 = section.children()[1];
283
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');
290                 });
291
292                 it('keeps DocumentTextElement\'s parent\'s children elements intact', function() {
293                     var c = canvas.fromXML('\
294                             <section>\
295                                 <header>\
296                                     A <span>fancy</span> and <span>nice</span> header\
297                                 </header>\
298                             </section>'),
299                         section = c.doc(),
300                         header = section.children()[0],
301                         textAnd = header.children()[2];
302
303                     textAnd.split({offset: 2});
304                     
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');
309
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');
315
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');
321                 });
322             });
323
324             describe('wrapping', function() {
325                 it('wraps DocumentNodeElement', function() {
326                     var c = canvas.fromXML('<section><div></div></section>'),
327                         div = c.doc().children()[0];
328                     
329                     var returned = div.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
330                         parent = div.parent(),
331                         parent2 = c.doc().children()[0];
332
333                     expect(returned.sameNode(parent)).to.be.true;
334                     expect(returned.sameNode(parent2)).to.be.true;
335                 });
336                 it('wraps DocumentTextElement', function() {
337                     var c = canvas.fromXML('<section>Alice</section>'),
338                         text = c.doc().children()[0];
339                     
340                     var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
341                         parent = text.parent(),
342                         parent2 = c.doc().children()[0];
343
344                     expect(returned.sameNode(parent)).to.be.true;
345                     expect(returned.sameNode(parent2)).to.be.true;
346                 });
347                 
348                 it('wraps part of DocumentTextElement', function() {
349                     var c = canvas.fromXML('<section>Alice has a cat</section>'),
350                         text = c.doc().children()[0];
351                     
352                     var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class', start: 5, end: 12}),
353                         children = c.doc().children();
354
355                     expect(children.length).to.equal(3);
356                     
357                     expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
358                     expect(children[0].getText()).to.equal('Alice');
359
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 ');
363
364                     expect(children[2]).to.be.instanceOf(documentElement.DocumentTextElement);
365                     expect(children[2].getText()).to.equal('cat');
366                 });
367
368                 it('wraps text spanning multiple sibling DocumentTextNodes', function() {
369                     var c = canvas.fromXML('<section>Alice has a <span>small</span> cat</section>'),
370                         section = c.doc(),
371                         wrapper = c.wrapText({
372                             inside: section, 
373                             _with: {tag: 'span', klass: 'some.class'},
374                             offsetStart: 6,
375                             offsetEnd: 4,
376                             textNodeIdx: [0,2]
377                         });
378
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 ');
382
383                     var wrapper2 = section.children()[1];
384                     expect(wrapper2.sameNode(wrapper)).to.be.true;
385
386                     var wrapperChildren = wrapper.children();
387                     expect(wrapperChildren.length).to.equal(3);
388                     expect(wrapperChildren[0].getText()).to.equal('has a ');
389
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');
393
394                     expect(wrapperChildren[2].getText()).to.equal(' cat');
395                 });
396             });
397         });
398
399         describe('Lists api', function() {
400             it('allows creation of a list from existing sibling DocumentElements', function() {
401                 var c = canvas.fromXML('\
402                     <section>\
403                         Alice\
404                         <div>has</div>\
405                         a\
406                         <div>cat</div>\
407                     </section>'),
408                     section = c.doc(),
409                     textAlice = section.children()[0],
410                     divCat = section.children()[3]
411                 
412                 c.list.create({element1: textAlice, element2: divCat});
413
414                 expect(section.children().length).to.equal(1, 'section has one child element');
415
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');
421                 });
422             });
423         });
424
425     });
426 });
427
428
429 });