some additional assertions for list test
[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                 });
166                 it('ignores white space surrounding block elements', function() {
167                     var c = canvas.fromXML('<section> <div></div> </section>');
168                     var children = c.doc().children();
169                     expect(children.length).to.equal(1);
170                     expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
171                 });
172                 it('ignores white space between block elements', function() {
173                     var c = canvas.fromXML('<section><div></div> <div></div></section>');
174                     var children = c.doc().children();
175                     expect(children.length === 2);
176                     [0,1].forEach(function(idx) {
177                         expect(children[idx]).to.be.instanceOf(documentElement.DocumentNodeElement);
178                     });
179                 });
180
181                 it('trims white space from the beginning and the end of the block elements', function() {
182                     var c = canvas.fromXML('<section> Alice <span>has</span> a cat </section>');
183                     expect(c.doc().children()[0].getText()).to.equal('Alice ');
184                     expect(c.doc().children()[2].getText()).to.equal(' a cat');
185                 });
186
187                 it('normalizes string of white characters to one space at the inline element boundries', function() {
188                     var c = canvas.fromXML('<span>   Alice has a cat   </span>');
189                     expect(c.doc().children()[0].getText()).to.equal(' Alice has a cat ');
190                 });
191
192                 it('normalizes string of white characters to one space before inline element', function() {
193                     var c = canvas.fromXML('<div>Alice has  <span>a cat</span></div>');
194                     expect(c.doc().children()[0].getText()).to.equal('Alice has ');
195                 });
196                 
197                 it('normalizes string of white characters to one space after inline element', function() {
198                     var c = canvas.fromXML('<div>Alice has <span>a</span>  cat</div>');
199                     expect(c.doc().children()[2].getText()).to.equal(' cat');
200                 });
201             });
202         });
203
204         describe('manipulation api', function() {
205
206             describe('Basic Element inserting', function() {
207                 it('can put new NodeElement at the end', function() {
208                     var c = canvas.fromXML('<section></section>'),
209                         appended = c.doc().append({tag: 'header', klass: 'some.class'}),
210                         children = c.doc().children();
211
212                     expect(children.length).to.equal(1);
213                     expect(children[0].sameNode(appended));
214                 });
215
216                 it('can put new TextElement at the end', function() {
217                     var c = canvas.fromXML('<section></section>'),
218                         appended = c.doc().append({text: 'Alice'}),
219                         children = c.doc().children();
220
221                     expect(children.length).to.equal(1);
222                     expect(children[0].sameNode(appended));
223                     expect(children[0].getText()).to.equal('Alice');
224                 });
225
226                 it('can put new NodeElement after another NodeElement', function() {
227                     var c = canvas.fromXML('<section><div></div></section>'),
228                         div = c.doc().children()[0],
229                         added = div.after({tag: 'header', klass: 'some.class'}),
230                         children = c.doc().children();
231                     expect(children.length).to.equal(2);
232                     expect(children[1].sameNode(added));
233                 });
234
235                 it('can put new Nodeelement before another element', function() {
236                     var c = canvas.fromXML('<section><div></div></section>'),
237                         div = c.doc().children()[0],
238                         added = div.before({tag: 'header', klass: 'some.class'}),
239                         children = c.doc().children();
240                     expect(children.length).to.equal(2);
241                     expect(children[0].sameNode(added));
242                 });
243
244                 it('can put new DocumentNodeElement after DocumentTextElement', function() {
245                     var c = canvas.fromXML('<section>Alice</section>'),
246                         text = c.doc().children()[0],
247                         added = text.after({tag: 'p'}),
248                         children = c.doc().children();
249
250                     expect(children.length).to.equal(2);
251                     expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
252                     expect(children[0].getText()).to.equal('Alice');
253                     expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
254                     expect(children[1].sameNode(added)).to.be.true;
255                 });
256                 it('can put new DocumentNodeElement before DocumentTextElement', function() {
257                     var c = canvas.fromXML('<section>Alice</section>'),
258                         text = c.doc().children()[0],
259                         added = text.before({tag: 'p'}),
260                         children = c.doc().children();
261
262                     expect(children.length).to.equal(2);
263                     expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
264                     expect(children[0].sameNode(added)).to.be.true;
265                     expect(children[1]).to.be.instanceOf(documentElement.DocumentTextElement);
266                     expect(children[1].getText()).to.equal('Alice');
267                 });
268             });
269
270             describe('Splitting text', function() {
271                 
272                 it('splits DocumentTextElement\'s parent into two DocumentNodeElements of the same type', function() {
273                     var c = canvas.fromXML('<section><header>Some header</header></section>'),
274                         section = c.doc(),
275                         text = section.children()[0].children()[0];
276
277                     text.split({offset: 5});
278                     expect(section.children().length).to.equal(2, 'section has two children');
279                     
280                     var header1 = section.children()[0];
281                     var header2 = section.children()[1];
282
283                     expect(header1.wlxmlTag).to.equal('header', 'first section child represents wlxml header');
284                     expect(header1.children().length).to.equal(1, 'first header has one text child');
285                     expect(header1.children()[0].getText()).to.equal('Some ', 'first header has correct content');
286                     expect(header2.wlxmlTag).to.equal('header', 'second section child represents wlxml header');
287                     expect(header2.children().length).to.equal(1, 'second header has one text child');
288                     expect(header2.children()[0].getText()).to.equal('header', 'second header has correct content');
289                 });
290
291                 it('keeps DocumentTextElement\'s parent\'s children elements intact', function() {
292                     var c = canvas.fromXML('\
293                             <section>\
294                                 <header>\
295                                     A <span>fancy</span> and <span>nice</span> header\
296                                 </header>\
297                             </section>'),
298                         section = c.doc(),
299                         header = section.children()[0],
300                         textAnd = header.children()[2];
301
302                     textAnd.split({offset: 2});
303                     
304                     var sectionChildren = section.children();
305                     expect(sectionChildren.length).to.equal(2, 'Section has two children');
306                     expect(sectionChildren[0].wlxmlTag).to.equal('header', 'First section element is a wlxml header');
307                     expect(sectionChildren[1].wlxmlTag).to.equal('header', 'Second section element is a wlxml header');
308
309                     var firstHeaderChildren = sectionChildren[0].children();
310                     expect(firstHeaderChildren.length).to.equal(3, 'First header has three children');
311                     expect(firstHeaderChildren[0].getText()).to.equal('A ', 'First header starts with a text');
312                     expect(firstHeaderChildren[1].wlxmlTag).to.equal('span', 'First header has span in the middle');
313                     expect(firstHeaderChildren[2].getText()).to.equal(' a', 'First header ends with text');
314
315                     var secondHeaderChildren = sectionChildren[1].children();
316                     expect(secondHeaderChildren.length).to.equal(3, 'Second header has three children');
317                     expect(secondHeaderChildren[0].getText()).to.equal('nd ', 'Second header starts with text');
318                     expect(secondHeaderChildren[1].wlxmlTag).to.equal('span', 'Second header has span in the middle');
319                     expect(secondHeaderChildren[2].getText()).to.equal(' header', 'Second header ends with text');
320                 });
321             });
322
323             describe('wrapping', function() {
324                 it('wraps DocumentNodeElement', function() {
325                     var c = canvas.fromXML('<section><div></div></section>'),
326                         div = c.doc().children()[0];
327                     
328                     var returned = div.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
329                         parent = div.parent(),
330                         parent2 = c.doc().children()[0];
331
332                     expect(returned.sameNode(parent)).to.be.true;
333                     expect(returned.sameNode(parent2)).to.be.true;
334                 });
335                 it('wraps DocumentTextElement', function() {
336                     var c = canvas.fromXML('<section>Alice</section>'),
337                         text = c.doc().children()[0];
338                     
339                     var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
340                         parent = text.parent(),
341                         parent2 = c.doc().children()[0];
342
343                     expect(returned.sameNode(parent)).to.be.true;
344                     expect(returned.sameNode(parent2)).to.be.true;
345                 });
346                 
347                 it('wraps part of DocumentTextElement', function() {
348                     var c = canvas.fromXML('<section>Alice has a cat</section>'),
349                         text = c.doc().children()[0];
350                     
351                     var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class', start: 5, end: 12}),
352                         children = c.doc().children();
353
354                     expect(children.length).to.equal(3);
355                     
356                     expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
357                     expect(children[0].getText()).to.equal('Alice');
358
359                     expect(children[1].sameNode(returned)).to.be.true;
360                     expect(children[1].children().length).to.equal(1);
361                     expect(children[1].children()[0].getText()).to.equal(' has a ');
362
363                     expect(children[2]).to.be.instanceOf(documentElement.DocumentTextElement);
364                     expect(children[2].getText()).to.equal('cat');
365                 });
366
367                 it('wraps text spanning multiple sibling DocumentTextNodes', function() {
368                     var c = canvas.fromXML('<section>Alice has a <span>small</span> cat</section>'),
369                         section = c.doc(),
370                         wrapper = c.wrapText({
371                             inside: section, 
372                             _with: {tag: 'span', klass: 'some.class'},
373                             offsetStart: 6,
374                             offsetEnd: 4,
375                             textNodeIdx: [0,2]
376                         });
377
378                     expect(section.children().length).to.equal(2);
379                     expect(section.children()[0]).to.be.instanceOf(documentElement.DocumentTextElement);
380                     expect(section.children()[0].getText()).to.equal('Alice ');
381
382                     var wrapper2 = section.children()[1];
383                     expect(wrapper2.sameNode(wrapper)).to.be.true;
384
385                     var wrapperChildren = wrapper.children();
386                     expect(wrapperChildren.length).to.equal(3);
387                     expect(wrapperChildren[0].getText()).to.equal('has a ');
388
389                     expect(wrapperChildren[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
390                     expect(wrapperChildren[1].children().length).to.equal(1);
391                     expect(wrapperChildren[1].children()[0].getText()).to.equal('small');
392
393                     expect(wrapperChildren[2].getText()).to.equal(' cat');
394                 });
395             });
396         });
397
398         describe('Lists api', function() {
399             it('allows creation of a list from existing sibling DocumentElements', function() {
400                 var c = canvas.fromXML('\
401                     <section>\
402                         Alice\
403                         <div>has</div>\
404                         a\
405                         <div>cat</div>\
406                     </section>'),
407                     section = c.doc(),
408                     textAlice = section.children()[0],
409                     divCat = section.children()[3]
410                 
411                 c.list.create({element1: textAlice, element2: divCat});
412
413                 expect(section.children().length).to.equal(1, 'section has one child element');
414
415                 var list = section.children()[0];
416                 expect(list.is('list')).to.equal(true, 'section\'s only child is a list');
417                 expect(list.children().length).to.equal(4, 'list contains four elements');
418                 list.children().forEach(function(child) {
419                     expect(child.getWlxmlClass()).to.equal('item', 'list childs have wlxml class of item');
420                 });
421             });
422         });
423
424     });
425 });
426
427
428 });