e122a1a50e5b3ed20c2f60117440c50754ee8d9c
[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 sample document', function() {
15         it('works', function() {
16             var c = canvas.fromXML('\
17                 <section>\
18                     This is some text without its own wrapping tag.\
19                     <div class="p.subclass">\
20                         This is a paragraph.\
21                     </div>\
22                     <div>\
23                         This is text in a div <span>with some inline text</span>.\
24                     </div>\
25                     This is some text without its own wrapping tag.\
26                 </section>\
27             ');
28             var expected = '<div wlxml-tag="section">'
29                             + 'This is some text without its own wrapping tag.'
30                             + '<div wlxml-tag="div" wlxml-class="p-subclass">This is a paragraph.</div>'
31                             + '<div wlxml-tag="div">This is text in a div <div wlxml-tag="span">with some inline text</div>.</div>'
32                             + 'This is some text without its own wrapping tag.'
33                             + '</div>';
34             expect(c.doc().dom()[0].isEqualNode($(expected)[0])).to.be.true;
35         });
36     });
37
38     describe('Internal HTML representation of a DocumentNodeElement', function() {
39         it('is always a div tag', function() {
40             ['section', 'header', 'span', 'aside', 'figure'].forEach(function(tagName) {
41                 var dom = canvas.fromXML('<' + tagName + '></' + tagName + '>').doc().dom();
42                 expect(dom.prop('tagName')).to.equal('DIV', tagName + ' is represented as div');
43             });
44         });
45         it('has wlxml tag put into wlxml-tag attribute', function() {
46             var dom = canvas.fromXML('<section></section>').doc().dom();
47             expect(dom.attr('wlxml-tag')).to.equal('section');
48         });
49         it('has wlxml class put into wlxml-class, dots replaced with dashes', function() {
50             var dom = canvas.fromXML('<section class="some.class"></section>').doc().dom();
51             expect(dom.attr('wlxml-class')).to.equal('some-class');
52         });
53     });
54
55     describe('Internal HTML representation of a DocumentTextElement', function() {
56         it('is just a TextNode', function() {
57             var dom = canvas.fromXML('<section>Alice</section>').doc().children()[0].dom();
58             expect(dom[0].nodeType === Node.TEXT_NODE);
59         });
60     });
61
62     describe('basic properties', function() {
63         it('renders empty document when canvas created from empty XML', function() {
64             var c = canvas.fromXML('');
65             expect(c.doc()).to.equal(null);
66         });
67
68         it('gives access to its document root node', function() {
69             var c = canvas.fromXML('<section></section>');
70             expect(c.doc().wlxmlTag).to.equal('section');
71         });
72
73         describe('DocumentTextElement', function() {
74             it('can have its content set', function() {
75                 var c = canvas.fromXML('<section>Alice</section>'),
76                     root = c.doc(),
77                     text = root.children()[0];
78                 
79                 text.setText('a cat');
80                 expect(root.children()[0].getText()).to.equal('a cat');
81             });
82         });
83
84         describe('DocumentNodeElement', function() {
85             it('knows index of its child', function() {
86                 var c = canvas.fromXML('<section><div></div><header></header><span></span></section>'),
87                     root = c.doc(),
88                     child = root.children()[1];
89                 expect(root.childIndex(child)).to.equal(1);
90             });
91
92             it('knows WLXML tag it renders', function(){
93                 var c = canvas.fromXML('<section></section>'),
94                     section = c.doc();
95                 expect(section.getWlxmlTag()).to.equal('section', 'initial tag is section');
96                 section.setWlxmlTag('header');
97                 expect(section.getWlxmlTag()).to.equal('header', 'tag is changed to header');
98             });
99
100             it('knows WLXML class of a WLXML tag it renders', function(){
101                 var c = canvas.fromXML('<section class="some.class"></section>'),
102                     section = c.doc();
103                 expect(section.getWlxmlClass()).to.equal('some.class');
104                 section.setWlxmlClass('some.other.class');
105                 expect(section.getWlxmlClass()).to.equal('some.other.class');
106             });
107         });
108     });
109
110
111
112     describe('document representation api', function() {
113         describe('document root element', function() {
114             var c = canvas.fromXML('<section></section>');
115             it('exists', function() {
116                 expect(c.doc()).to.be.instanceOf(documentElement.DocumentElement);
117             });
118             it('is of type DocumentNodeElement', function() {
119                 expect(c.doc()).to.be.instanceOf(documentElement.DocumentNodeElement);
120             });
121         });
122
123         describe('DocumentElements comparison', function() {
124             it('reports dwo DocumentElements to be the same when they represent the same wlxml document element', function() {
125                 var c = canvas.fromXML('<section><div></div><div></div></section>'),
126                     first_div1 = c.doc().children()[0],
127                     first_div2 = c.doc().children()[0],
128                     second_div = c.doc().children()[1];
129                 expect(first_div1.sameNode(first_div1)).to.be.true;
130                 expect(first_div1.sameNode(first_div2)).to.be.true;
131                 expect(first_div1.sameNode(second_div)).to.be.false;
132             });
133         });
134
135         describe('traversing', function() {
136             it('reports element nodes', function() {
137                 var c = canvas.fromXML('<section><div></div></section>'),
138                     children = c.doc().children();
139                 expect(children.length).to.equal(1);
140                 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
141
142                 c = canvas.fromXML('<section><div></div><div></div></section>'),
143                     children = c.doc().children();
144                 expect(children.length).to.equal(2);
145                 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
146                 expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
147             });
148             
149             it('reports text nodes', function() {
150                 var c = canvas.fromXML('<section>Alice</section>'),
151                     children = c.doc().children();
152                 expect(children.length).to.equal(1);
153                 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
154             });
155
156             describe('accessing parents', function() {
157                 it('returns DocumentNodeElement representing parent in wlxml document as DocumentNodeElement parent', function() {
158                     var c = canvas.fromXML('<section><div></div></section>'),
159                         div = c.doc().children()[0];
160                     expect(div.parent().sameNode(c.doc())).to.be.true;
161                 });
162                 it('returns DocumentNodeElement representing parent in wlxml document as DocumentTextElement parent', function() {
163                     var c = canvas.fromXML('<section>Alice</section>'),
164                         text = c.doc().children()[0];
165                     expect(text.parent().sameNode(c.doc())).to.be.true;
166                 });
167             });
168
169             describe('free text handling', function() {
170                     it('sees free text', function() {
171                         var c = canvas.fromXML('<section>Alice <span>has</span> a cat</section>'),
172                             children = c.doc().children();
173                         expect(children.length).to.equal(3);
174                         expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
175                         expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
176                         expect(children[2]).to.be.instanceOf(documentElement.DocumentTextElement);
177                     });
178             });
179             
180             describe('white characters handling', function() {
181                 it('says empty element node has no children', function() {
182                     var c = canvas.fromXML('<section></section>');
183                     expect(c.doc().children().length).to.equal(0);
184                 });
185                 it('says element node with one space has one DocumentTextElement', function() {
186                     var c = canvas.fromXML('<section> </section>');
187                     expect(c.doc().children().length).to.equal(1);
188                     expect(c.doc().children()[0]).to.be.instanceOf(documentElement.DocumentTextElement);
189                     expect(c.doc().children()[0].getText()).to.equal(' ');
190                 });
191                 it('ignores white space surrounding block elements', function() {
192                     var c = canvas.fromXML('<section> <div></div> </section>');
193                     var children = c.doc().children();
194                     expect(children.length).to.equal(1);
195                     expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
196                 });
197                 it('ignores white space between block elements', function() {
198                     var c = canvas.fromXML('<section><div></div> <div></div></section>');
199                     var children = c.doc().children();
200                     expect(children.length === 2);
201                     [0,1].forEach(function(idx) {
202                         expect(children[idx]).to.be.instanceOf(documentElement.DocumentNodeElement);
203                     });
204                 });
205
206                 it('trims white space from the beginning and the end of the block elements', function() {
207                     var c = canvas.fromXML('<section> Alice <span>has</span> a cat </section>');
208                     expect(c.doc().children()[0].getText()).to.equal('Alice ');
209                     expect(c.doc().children()[2].getText()).to.equal(' a cat');
210                 });
211
212                 it('normalizes string of white characters to one space at the inline element boundries', function() {
213                     var c = canvas.fromXML('<span>   Alice has a cat   </span>');
214                     expect(c.doc().children()[0].getText()).to.equal(' Alice has a cat ');
215                 });
216
217                 it('normalizes string of white characters to one space before inline element', function() {
218                     var c = canvas.fromXML('<div>Alice has  <span>a cat</span></div>');
219                     expect(c.doc().children()[0].getText()).to.equal('Alice has ');
220                 });
221                 
222                 it('normalizes string of white characters to one space after inline element', function() {
223                     var c = canvas.fromXML('<div>Alice has <span>a</span>  cat</div>');
224                     expect(c.doc().children()[2].getText()).to.equal(' cat');
225                 });
226             });
227         });
228
229         describe('manipulation api', function() {
230
231             describe('Basic Element inserting', function() {
232                 it('can put new NodeElement at the end', function() {
233                     var c = canvas.fromXML('<section></section>'),
234                         appended = c.doc().append({tag: 'header', klass: 'some.class'}),
235                         children = c.doc().children();
236
237                     expect(children.length).to.equal(1);
238                     expect(children[0].sameNode(appended));
239                 });
240
241                 it('can put new TextElement at the end', function() {
242                     var c = canvas.fromXML('<section></section>'),
243                         appended = c.doc().append({text: 'Alice'}),
244                         children = c.doc().children();
245
246                     expect(children.length).to.equal(1);
247                     expect(children[0].sameNode(appended));
248                     expect(children[0].getText()).to.equal('Alice');
249                 });
250
251                 it('can put new NodeElement after another NodeElement', function() {
252                     var c = canvas.fromXML('<section><div></div></section>'),
253                         div = c.doc().children()[0],
254                         added = div.after({tag: 'header', klass: 'some.class'}),
255                         children = c.doc().children();
256                     expect(children.length).to.equal(2);
257                     expect(children[1].sameNode(added));
258                 });
259
260                 it('can put new Nodeelement before another element', function() {
261                     var c = canvas.fromXML('<section><div></div></section>'),
262                         div = c.doc().children()[0],
263                         added = div.before({tag: 'header', klass: 'some.class'}),
264                         children = c.doc().children();
265                     expect(children.length).to.equal(2);
266                     expect(children[0].sameNode(added));
267                 });
268
269                 it('can put new DocumentNodeElement after DocumentTextElement', function() {
270                     var c = canvas.fromXML('<section>Alice</section>'),
271                         text = c.doc().children()[0],
272                         added = text.after({tag: 'p'}),
273                         children = c.doc().children();
274
275                     expect(children.length).to.equal(2);
276                     expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
277                     expect(children[0].getText()).to.equal('Alice');
278                     expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
279                     expect(children[1].sameNode(added)).to.be.true;
280                 });
281                 it('can put new DocumentNodeElement before DocumentTextElement', function() {
282                     var c = canvas.fromXML('<section>Alice</section>'),
283                         text = c.doc().children()[0],
284                         added = text.before({tag: 'p'}),
285                         children = c.doc().children();
286
287                     expect(children.length).to.equal(2);
288                     expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
289                     expect(children[0].sameNode(added)).to.be.true;
290                     expect(children[1]).to.be.instanceOf(documentElement.DocumentTextElement);
291                     expect(children[1].getText()).to.equal('Alice');
292                 });
293             });
294
295             describe('Splitting text', function() {
296                 
297                 it('splits DocumentTextElement\'s parent into two DocumentNodeElements of the same type', function() {
298                     var c = canvas.fromXML('<section><header>Some header</header></section>'),
299                         section = c.doc(),
300                         text = section.children()[0].children()[0];
301
302                     text.split({offset: 5});
303                     expect(section.children().length).to.equal(2, 'section has two children');
304                     
305                     var header1 = section.children()[0];
306                     var header2 = section.children()[1];
307
308                     expect(header1.wlxmlTag).to.equal('header', 'first section child represents wlxml header');
309                     expect(header1.children().length).to.equal(1, 'first header has one text child');
310                     expect(header1.children()[0].getText()).to.equal('Some ', 'first header has correct content');
311                     expect(header2.wlxmlTag).to.equal('header', 'second section child represents wlxml header');
312                     expect(header2.children().length).to.equal(1, 'second header has one text child');
313                     expect(header2.children()[0].getText()).to.equal('header', 'second header has correct content');
314                 });
315
316                 it('keeps DocumentTextElement\'s parent\'s children elements intact', function() {
317                     var c = canvas.fromXML('\
318                             <section>\
319                                 <header>\
320                                     A <span>fancy</span> and <span>nice</span> header\
321                                 </header>\
322                             </section>'),
323                         section = c.doc(),
324                         header = section.children()[0],
325                         textAnd = header.children()[2];
326
327                     textAnd.split({offset: 2});
328                     
329                     var sectionChildren = section.children();
330                     expect(sectionChildren.length).to.equal(2, 'Section has two children');
331                     expect(sectionChildren[0].wlxmlTag).to.equal('header', 'First section element is a wlxml header');
332                     expect(sectionChildren[1].wlxmlTag).to.equal('header', 'Second section element is a wlxml header');
333
334                     var firstHeaderChildren = sectionChildren[0].children();
335                     expect(firstHeaderChildren.length).to.equal(3, 'First header has three children');
336                     expect(firstHeaderChildren[0].getText()).to.equal('A ', 'First header starts with a text');
337                     expect(firstHeaderChildren[1].wlxmlTag).to.equal('span', 'First header has span in the middle');
338                     expect(firstHeaderChildren[2].getText()).to.equal(' a', 'First header ends with text');
339
340                     var secondHeaderChildren = sectionChildren[1].children();
341                     expect(secondHeaderChildren.length).to.equal(3, 'Second header has three children');
342                     expect(secondHeaderChildren[0].getText()).to.equal('nd ', 'Second header starts with text');
343                     expect(secondHeaderChildren[1].wlxmlTag).to.equal('span', 'Second header has span in the middle');
344                     expect(secondHeaderChildren[2].getText()).to.equal(' header', 'Second header ends with text');
345                 });
346             });
347
348             describe('wrapping', function() {
349                 it('wraps DocumentNodeElement', function() {
350                     var c = canvas.fromXML('<section><div></div></section>'),
351                         div = c.doc().children()[0];
352                     
353                     var returned = div.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
354                         parent = div.parent(),
355                         parent2 = c.doc().children()[0];
356
357                     expect(returned.sameNode(parent)).to.be.true;
358                     expect(returned.sameNode(parent2)).to.be.true;
359                 });
360                 it('wraps DocumentTextElement', function() {
361                     var c = canvas.fromXML('<section>Alice</section>'),
362                         text = c.doc().children()[0];
363                     
364                     var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
365                         parent = text.parent(),
366                         parent2 = c.doc().children()[0];
367
368                     expect(returned.sameNode(parent)).to.be.true;
369                     expect(returned.sameNode(parent2)).to.be.true;
370                 });
371                 
372                 it('wraps part of DocumentTextElement', function() {
373                     var c = canvas.fromXML('<section>Alice has a cat</section>'),
374                         text = c.doc().children()[0];
375                     
376                     var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class', start: 5, end: 12}),
377                         children = c.doc().children();
378
379                     expect(children.length).to.equal(3);
380                     
381                     expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
382                     expect(children[0].getText()).to.equal('Alice');
383
384                     expect(children[1].sameNode(returned)).to.be.true;
385                     expect(children[1].children().length).to.equal(1);
386                     expect(children[1].children()[0].getText()).to.equal(' has a ');
387
388                     expect(children[2]).to.be.instanceOf(documentElement.DocumentTextElement);
389                     expect(children[2].getText()).to.equal('cat');
390                 });
391
392                 it('wraps text spanning multiple sibling DocumentTextNodes', function() {
393                     var c = canvas.fromXML('<section>Alice has a <span>small</span> cat</section>'),
394                         section = c.doc(),
395                         wrapper = c.wrapText({
396                             inside: section, 
397                             _with: {tag: 'span', klass: 'some.class'},
398                             offsetStart: 6,
399                             offsetEnd: 4,
400                             textNodeIdx: [0,2]
401                         });
402
403                     expect(section.children().length).to.equal(2);
404                     expect(section.children()[0]).to.be.instanceOf(documentElement.DocumentTextElement);
405                     expect(section.children()[0].getText()).to.equal('Alice ');
406
407                     var wrapper2 = section.children()[1];
408                     expect(wrapper2.sameNode(wrapper)).to.be.true;
409
410                     var wrapperChildren = wrapper.children();
411                     expect(wrapperChildren.length).to.equal(3);
412                     expect(wrapperChildren[0].getText()).to.equal('has a ');
413
414                     expect(wrapperChildren[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
415                     expect(wrapperChildren[1].children().length).to.equal(1);
416                     expect(wrapperChildren[1].children()[0].getText()).to.equal('small');
417
418                     expect(wrapperChildren[2].getText()).to.equal(' cat');
419                 });
420             });
421         });
422
423         describe('Lists api', function() {
424             it('allows creation of a list from existing sibling DocumentElements', function() {
425                 var c = canvas.fromXML('\
426                     <section>\
427                         Alice\
428                         <div>has</div>\
429                         a\
430                         <div>cat</div>\
431                     </section>'),
432                     section = c.doc(),
433                     textAlice = section.children()[0],
434                     divCat = section.children()[3]
435                 
436                 c.list.create({element1: textAlice, element2: divCat});
437
438                 expect(section.children().length).to.equal(1, 'section has one child element');
439
440                 var list = section.children()[0];
441                 expect(list.is('list')).to.equal(true, 'section\'s only child is a list');
442                 expect(list.children().length).to.equal(4, 'list contains four elements');
443                 list.children().forEach(function(child) {
444                     expect(child.getWlxmlClass()).to.equal('item', 'list childs have wlxml class of item');
445                 });
446             });
447         });
448
449     });
450 });
451
452
453 });