705ef2ceed7810a3a03159a0c7315884e8cdfab0
[fnpeditor.git] / modules / documentCanvas / canvas / canvas.test3.js
1 define([
2 'libs/chai',
3 'libs/sinon',
4 'modules/documentCanvas/canvas/canvas',
5 'modules/documentCanvas/canvas/documentElement'
6 ], function(chai, sinon, canvas, documentElement) {
7     
8 'use strict';
9
10 var expect = chai.expect;
11
12
13 describe('Canvas', function() {
14
15     describe('Internal HTML representation of a sample document', function() {
16         it('works', function() {
17             var c = canvas.fromXML('\
18                 <section>\
19                     This is some text without its own wrapping tag.\
20                     <div class="p.subclass">\
21                         This is a paragraph.\
22                     </div>\
23                     <div>\
24                         This is text in a div <span>with some inline text</span>.\
25                     </div>\
26                     This is some text without its own wrapping tag.\
27                 </section>\
28             ');
29             var expected = '<div wlxml-tag="section">'
30                             + '<div wlxml-text>This is some text without its own wrapping tag.</div>'
31                             + '<div wlxml-tag="div" wlxml-class="p-subclass">'
32                             +   '<div wlxml-text>This is a paragraph.</div>'
33                             + '</div>'
34                             + '<div wlxml-tag="div">'
35                             +   '<div wlxml-text>This is text in a div </div>'
36                             +   '<div wlxml-tag="span">'
37                             +       '<div wlxml-text>with some inline text</div>'
38                             +   '</div>'
39                             +   '<div wlxml-text>.</div>'
40                             + '</div>'
41                             + '<div wlxml-text>This is some text without its own wrapping tag.</div>'
42                             + '</div>';
43             expect(c.doc().dom()[0].isEqualNode($(expected)[0])).to.be.true;
44         });
45     });
46
47     describe('Internal HTML representation of a DocumentNodeElement', function() {
48         it('is always a div tag', function() {
49             ['section', 'header', 'span', 'aside', 'figure'].forEach(function(tagName) {
50                 var dom = canvas.fromXML('<' + tagName + '></' + tagName + '>').doc().dom();
51                 expect(dom.prop('tagName')).to.equal('DIV', tagName + ' is represented as div');
52             });
53         });
54         it('has wlxml tag put into wlxml-tag attribute', function() {
55             var dom = canvas.fromXML('<section></section>').doc().dom();
56             expect(dom.attr('wlxml-tag')).to.equal('section');
57         });
58         it('has wlxml class put into wlxml-class, dots replaced with dashes', function() {
59             var dom = canvas.fromXML('<section class="some.class"></section>').doc().dom();
60             expect(dom.attr('wlxml-class')).to.equal('some-class');
61         });
62     });
63
64     describe('Internal HTML representation of a DocumentTextElement', function() {
65         it('is text node wrapped in a div with wlxml-text attribute set', function() {
66             var dom = canvas.fromXML('<section>Alice</section>').doc().children()[0].dom();
67             expect(dom.prop('tagName')).to.equal('DIV');
68             expect(dom.attr('wlxml-text')).to.equal('');
69             expect(dom.contents().length).to.equal(1);
70             expect(dom.contents()[0].nodeType).to.equal(Node.TEXT_NODE);
71             expect($(dom.contents()[0]).text()).to.equal('Alice');
72         });
73     });
74
75     describe('basic properties', function() {
76         it('renders empty document when canvas created from empty XML', function() {
77             var c = canvas.fromXML('');
78             expect(c.doc()).to.equal(null);
79         });
80
81         it('gives access to its document root node', function() {
82             var c = canvas.fromXML('<section></section>');
83             expect(c.doc().getWlxmlTag()).to.equal('section');
84         });
85
86         describe('root element', function() {
87             it('has no parent', function() {
88                 var c = canvas.fromXML('<section></section>');
89                 expect(c.doc().parent()).to.be.null;
90             });
91         });
92
93         describe('DocumentTextElement', function() {
94             it('can have its content set', function() {
95                 var c = canvas.fromXML('<section>Alice</section>'),
96                     root = c.doc(),
97                     text = root.children()[0];
98                 
99                 text.setText('a cat');
100                 expect(root.children()[0].getText()).to.equal('a cat');
101             });
102         });
103
104         describe('DocumentNodeElement', function() {
105             it('knows index of its child', function() {
106                 var c = canvas.fromXML('<section><div></div><header></header><span></span></section>'),
107                     root = c.doc(),
108                     child = root.children()[1];
109                 expect(root.childIndex(child)).to.equal(1);
110             });
111
112             it('knows WLXML tag it renders', function(){
113                 var c = canvas.fromXML('<section></section>'),
114                     section = c.doc();
115                 expect(section.getWlxmlTag()).to.equal('section', 'initial tag is section');
116                 section.setWlxmlTag('header');
117                 expect(section.getWlxmlTag()).to.equal('header', 'tag is changed to header');
118             });
119
120             it('knows WLXML class of a WLXML tag it renders', function(){
121                 var c = canvas.fromXML('<section class="some.class"></section>'),
122                     section = c.doc();
123                 expect(section.getWlxmlClass()).to.equal('some.class');
124                 section.setWlxmlClass('some.other.class');
125                 expect(section.getWlxmlClass()).to.equal('some.other.class');
126                 section.setWlxmlClass(null);
127                 expect(section.getWlxmlClass()).to.be.undefined;
128             });
129         });
130
131         it('returns DocumentNodeElement instance from HTMLElement', function() {
132             var c = canvas.fromXML('<section></section>'),
133                 htmlElement = c.doc().dom().get(0),
134                 element = c.getDocumentElement(htmlElement);
135             expect(element).to.be.instanceOf(documentElement.DocumentNodeElement);
136             expect(element.sameNode(c.doc()));
137         });
138         
139         it('returns DocumentTextElement instance from Text Node', function() {
140             var c = canvas.fromXML('<section>Alice</section>'),
141                 aliceElement = c.doc().children()[0],
142                 textNode = aliceElement.dom().contents()[0],
143                 element = c.getDocumentElement(textNode);
144
145             expect(textNode.nodeType).to.equal(Node.TEXT_NODE, 'text node selected');
146             expect($(textNode).text()).to.equal('Alice');
147
148             expect(element).to.be.instanceOf(documentElement.DocumentTextElement);
149             expect(element.sameNode(c.doc().children()[0]));
150         });
151     });
152
153
154
155     describe('document representation api', function() {
156         describe('document root element', function() {
157             var c = canvas.fromXML('<section></section>');
158             it('exists', function() {
159                 expect(c.doc()).to.be.instanceOf(documentElement.DocumentElement);
160             });
161             it('is of type DocumentNodeElement', function() {
162                 expect(c.doc()).to.be.instanceOf(documentElement.DocumentNodeElement);
163             });
164         });
165
166         describe('DocumentElements comparison', function() {
167             it('reports dwo DocumentElements to be the same when they represent the same wlxml document element', function() {
168                 var c = canvas.fromXML('<section><div></div><div></div></section>'),
169                     first_div1 = c.doc().children()[0],
170                     first_div2 = c.doc().children()[0],
171                     second_div = c.doc().children()[1];
172                 expect(first_div1.sameNode(first_div1)).to.be.true;
173                 expect(first_div1.sameNode(first_div2)).to.be.true;
174                 expect(first_div1.sameNode(second_div)).to.be.false;
175             });
176         });
177
178         describe('traversing', function() {
179             it('reports element nodes', function() {
180                 var c = canvas.fromXML('<section><div></div></section>'),
181                     children = c.doc().children();
182                 expect(children.length).to.equal(1);
183                 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
184
185                 c = canvas.fromXML('<section><div></div><div></div></section>'),
186                     children = c.doc().children();
187                 expect(children.length).to.equal(2);
188                 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
189                 expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
190             });
191             
192             it('reports text nodes', function() {
193                 var c = canvas.fromXML('<section>Alice</section>'),
194                     children = c.doc().children();
195                 expect(children.length).to.equal(1);
196                 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
197             });
198
199             describe('accessing parents', function() {
200                 it('returns DocumentNodeElement representing parent in wlxml document as DocumentNodeElement parent', function() {
201                     var c = canvas.fromXML('<section><div></div></section>'),
202                         div = c.doc().children()[0];
203                     expect(div.parent().sameNode(c.doc())).to.be.true;
204                 });
205                 it('returns DocumentNodeElement representing parent in wlxml document as DocumentTextElement parent', function() {
206                     var c = canvas.fromXML('<section>Alice</section>'),
207                         text = c.doc().children()[0];
208                     expect(text.parent().sameNode(c.doc())).to.be.true;
209                 });
210             });
211
212             describe('free text handling', function() {
213                     it('sees free text', function() {
214                         var c = canvas.fromXML('<section>Alice <span>has</span> a cat</section>'),
215                             children = c.doc().children();
216                         expect(children.length).to.equal(3);
217                         expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
218                         expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
219                         expect(children[2]).to.be.instanceOf(documentElement.DocumentTextElement);
220                     });
221             });
222             
223             describe('white characters handling', function() {
224                 it('says empty element node has no children', function() {
225                     var c = canvas.fromXML('<section></section>');
226                     expect(c.doc().children().length).to.equal(0);
227                 });
228                 it('says element node with one space has one DocumentTextElement', function() {
229                     var c = canvas.fromXML('<section> </section>');
230                     expect(c.doc().children().length).to.equal(1);
231                     expect(c.doc().children()[0]).to.be.instanceOf(documentElement.DocumentTextElement);
232                     expect(c.doc().children()[0].getText()).to.equal(' ');
233                 });
234                 it('ignores white space surrounding block elements', function() {
235                     var c = canvas.fromXML('<section> <div></div> </section>');
236                     var children = c.doc().children();
237                     expect(children.length).to.equal(1);
238                     expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
239                 });
240                 it('ignores white space between block elements', function() {
241                     var c = canvas.fromXML('<section><div></div> <div></div></section>');
242                     var children = c.doc().children();
243                     expect(children.length === 2);
244                     [0,1].forEach(function(idx) {
245                         expect(children[idx]).to.be.instanceOf(documentElement.DocumentNodeElement);
246                     });
247                 });
248
249                 it('trims white space from the beginning and the end of the block elements', function() {
250                     var c = canvas.fromXML('<section> Alice <span>has</span> a cat </section>');
251                     expect(c.doc().children()[0].getText()).to.equal('Alice ');
252                     expect(c.doc().children()[2].getText()).to.equal(' a cat');
253                 });
254
255                 it('normalizes string of white characters to one space at the inline element boundries', function() {
256                     var c = canvas.fromXML('<span>   Alice has a cat   </span>');
257                     expect(c.doc().children()[0].getText()).to.equal(' Alice has a cat ');
258                 });
259
260                 it('normalizes string of white characters to one space before inline element', function() {
261                     var c = canvas.fromXML('<div>Alice has  <span>a cat</span></div>');
262                     expect(c.doc().children()[0].getText()).to.equal('Alice has ');
263                 });
264                 
265                 it('normalizes string of white characters to one space after inline element', function() {
266                     var c = canvas.fromXML('<div>Alice has <span>a</span>  cat</div>');
267                     expect(c.doc().children()[2].getText()).to.equal(' cat');
268                 });
269             });
270
271             describe('getting vertically first text element', function() {
272                 it('returns the first child if it\'s text element, ignores metadata', function() {
273                     var c = canvas.fromXML('<section><metadata><dc:author>author</dc:author></metadata>Alice<div>has</div>a cat</section>'),
274                         first = c.doc().getVerticallyFirstTextElement();
275
276                     expect(first.sameNode(c.doc().children()[1])).to.be.true;
277                 });
278
279                 it('looks recursively inside node elements if they precede text element', function() {
280                     var c = canvas.fromXML('\
281                             <section>\
282                                 <div>\
283                                     <div>\
284                                         Alice\
285                                     </div>\
286                                 </div>\
287                                 Some text\
288                             </section>'),
289                         textAlice = c.doc().children()[0].children()[0].children()[0],
290                         first = c.doc().getVerticallyFirstTextElement();
291
292                     expect(textAlice).to.be.instanceOf(documentElement.DocumentTextElement);
293                     expect(first.sameNode(textAlice)).to.be.true;
294                 });
295             });
296         });
297
298         describe('manipulation api', function() {
299
300             describe('Basic Element inserting', function() {
301                 it('can put new NodeElement at the end', function() {
302                     var c = canvas.fromXML('<section></section>'),
303                         appended = c.doc().append({tag: 'header', klass: 'some.class'}),
304                         children = c.doc().children();
305
306                     expect(children.length).to.equal(1);
307                     expect(children[0].sameNode(appended));
308                 });
309
310                 it('can put new TextElement at the end', function() {
311                     var c = canvas.fromXML('<section></section>'),
312                         appended = c.doc().append({text: 'Alice'}),
313                         children = c.doc().children();
314
315                     expect(children.length).to.equal(1);
316                     expect(children[0].sameNode(appended));
317                     expect(children[0].getText()).to.equal('Alice');
318                 });
319
320                 it('can put new NodeElement after another NodeElement', function() {
321                     var c = canvas.fromXML('<section><div></div></section>'),
322                         div = c.doc().children()[0],
323                         added = div.after({tag: 'header', klass: 'some.class'}),
324                         children = c.doc().children();
325                     expect(children.length).to.equal(2);
326                     expect(children[1].sameNode(added));
327                 });
328
329                 it('can put new Nodeelement before another element', function() {
330                     var c = canvas.fromXML('<section><div></div></section>'),
331                         div = c.doc().children()[0],
332                         added = div.before({tag: 'header', klass: 'some.class'}),
333                         children = c.doc().children();
334                     expect(children.length).to.equal(2);
335                     expect(children[0].sameNode(added));
336                 });
337
338                 it('can put new DocumentNodeElement after DocumentTextElement', function() {
339                     var c = canvas.fromXML('<section>Alice</section>'),
340                         text = c.doc().children()[0],
341                         added = text.after({tag: 'p'}),
342                         children = c.doc().children();
343
344                     expect(children.length).to.equal(2);
345                     expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
346                     expect(children[0].getText()).to.equal('Alice');
347                     expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
348                     expect(children[1].sameNode(added)).to.be.true;
349                 });
350                 it('can put new DocumentNodeElement before DocumentTextElement', function() {
351                     var c = canvas.fromXML('<section>Alice</section>'),
352                         text = c.doc().children()[0],
353                         added = text.before({tag: 'p'}),
354                         children = c.doc().children();
355
356                     expect(children.length).to.equal(2);
357                     expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
358                     expect(children[0].sameNode(added)).to.be.true;
359                     expect(children[1]).to.be.instanceOf(documentElement.DocumentTextElement);
360                     expect(children[1].getText()).to.equal('Alice');
361                 });
362             });
363
364             describe('Splitting text', function() {
365                 
366                 it('splits DocumentTextElement\'s parent into two DocumentNodeElements of the same type', function() {
367                     var c = canvas.fromXML('<section><header>Some header</header></section>'),
368                         section = c.doc(),
369                         text = section.children()[0].children()[0];
370
371                     text.split({offset: 5});
372                     expect(section.children().length).to.equal(2, 'section has two children');
373                     
374                     var header1 = section.children()[0];
375                     var header2 = section.children()[1];
376
377                     expect(header1.getWlxmlTag()).to.equal('header', 'first section child represents wlxml header');
378                     expect(header1.children().length).to.equal(1, 'first header has one text child');
379                     expect(header1.children()[0].getText()).to.equal('Some ', 'first header has correct content');
380                     expect(header2.getWlxmlTag()).to.equal('header', 'second section child represents wlxml header');
381                     expect(header2.children().length).to.equal(1, 'second header has one text child');
382                     expect(header2.children()[0].getText()).to.equal('header', 'second header has correct content');
383                 });
384
385                 it('leaves empty copy of DocumentNodeElement if splitting at the very beginning', function() {
386                         var c = canvas.fromXML('<section><header>Some header</header></section>'),
387                         section = c.doc(),
388                         text = section.children()[0].children()[0];
389
390                         text.split({offset: 0});
391                         
392                         var header1 = section.children()[0];
393                         var header2 = section.children()[1];
394
395                         expect(header1.children().length).to.equal(0);
396                         expect(header2.children()[0].getText()).to.equal('Some header');
397                 });
398
399                 it('leaves empty copy of DocumentNodeElement if splitting at the very end', function() {
400                         var c = canvas.fromXML('<section><header>Some header</header></section>'),
401                         section = c.doc(),
402                         text = section.children()[0].children()[0];
403
404                         text.split({offset: 11});
405                         
406                         var header1 = section.children()[0];
407                         var header2 = section.children()[1];
408
409                         expect(header1.children()[0].getText()).to.equal('Some header');
410                         expect(header2.children().length).to.equal(0);
411                 });
412
413                 it('keeps DocumentTextElement\'s parent\'s children elements intact', function() {
414                     var c = canvas.fromXML('\
415                             <section>\
416                                 <header>\
417                                     A <span>fancy</span> and <span>nice</span> header\
418                                 </header>\
419                             </section>'),
420                         section = c.doc(),
421                         header = section.children()[0],
422                         textAnd = header.children()[2];
423
424                     textAnd.split({offset: 2});
425                     
426                     var sectionChildren = section.children();
427                     expect(sectionChildren.length).to.equal(2, 'Section has two children');
428                     expect(sectionChildren[0].getWlxmlTag()).to.equal('header', 'First section element is a wlxml header');
429                     expect(sectionChildren[1].getWlxmlTag()).to.equal('header', 'Second section element is a wlxml header');
430
431                     var firstHeaderChildren = sectionChildren[0].children();
432                     expect(firstHeaderChildren.length).to.equal(3, 'First header has three children');
433                     expect(firstHeaderChildren[0].getText()).to.equal('A ', 'First header starts with a text');
434                     expect(firstHeaderChildren[1].getWlxmlTag()).to.equal('span', 'First header has span in the middle');
435                     expect(firstHeaderChildren[2].getText()).to.equal(' a', 'First header ends with text');
436
437                     var secondHeaderChildren = sectionChildren[1].children();
438                     expect(secondHeaderChildren.length).to.equal(3, 'Second header has three children');
439                     expect(secondHeaderChildren[0].getText()).to.equal('nd ', 'Second header starts with text');
440                     expect(secondHeaderChildren[1].getWlxmlTag()).to.equal('span', 'Second header has span in the middle');
441                     expect(secondHeaderChildren[2].getText()).to.equal(' header', 'Second header ends with text');
442                 });
443             });
444
445             describe('wrapping', function() {
446                 it('wraps DocumentNodeElement', function() {
447                     var c = canvas.fromXML('<section><div></div></section>'),
448                         div = c.doc().children()[0];
449                     
450                     var returned = div.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
451                         parent = div.parent(),
452                         parent2 = c.doc().children()[0];
453
454                     expect(returned.sameNode(parent)).to.be.true;
455                     expect(returned.sameNode(parent2)).to.be.true;
456                     expect(returned.getWlxmlTag()).to.equal('header');
457                     expect(returned.getWlxmlClass()).to.equal('some.class');
458                 });
459                 it('wraps DocumentTextElement', function() {
460                     var c = canvas.fromXML('<section>Alice</section>'),
461                         text = c.doc().children()[0];
462                     
463                     var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
464                         parent = text.parent(),
465                         parent2 = c.doc().children()[0];
466
467                     expect(returned.sameNode(parent)).to.be.true;
468                     expect(returned.sameNode(parent2)).to.be.true;
469                     expect(returned.getWlxmlTag()).to.equal('header');
470                     expect(returned.getWlxmlClass()).to.equal('some.class');
471                 });
472                 
473                 describe('wrapping part of DocumentTextElement', function() {
474                     [{start: 5, end: 12}, {start: 12, end: 5}].forEach(function(offsets) {
475                         it('wraps in the middle ' + offsets.start + '/' + offsets.end, function() {
476                             var c = canvas.fromXML('<section>Alice has a cat</section>'),
477                                 text = c.doc().children()[0];
478                             
479                             var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class', start: offsets.start, end: offsets.end}),
480                                 children = c.doc().children();
481
482                             expect(children.length).to.equal(3);
483                             
484                             expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
485                             expect(children[0].getText()).to.equal('Alice');
486
487                             expect(children[1].sameNode(returned)).to.be.true;
488                             expect(returned.getWlxmlTag()).to.equal('header');
489                             expect(returned.getWlxmlClass()).to.equal('some.class');
490                             expect(children[1].children().length).to.equal(1);
491                             expect(children[1].children()[0].getText()).to.equal(' has a ');
492
493                             expect(children[2]).to.be.instanceOf(documentElement.DocumentTextElement);
494                             expect(children[2].getText()).to.equal('cat');
495                         });
496                     });
497
498                     it('wraps whole text inside DocumentTextElement if offsets span entire content', function() {
499                          var c = canvas.fromXML('<section>Alice has a cat</section>'),
500                              text = c.doc().children()[0];
501                          
502                          var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class', start: 0, end: 15}),
503                              children = c.doc().children();
504
505                          expect(children.length).to.equal(1);
506                          expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
507                          expect(children[0].children()[0].getText()).to.equal('Alice has a cat');
508                     });
509                 });
510
511                 it('wraps text spanning multiple sibling DocumentTextNodes', function() {
512                     var c = canvas.fromXML('<section>Alice has a <span>small</span> cat</section>'),
513                         section = c.doc(),
514                         wrapper = c.wrapText({
515                             inside: section, 
516                             _with: {tag: 'span', klass: 'some.class'},
517                             offsetStart: 6,
518                             offsetEnd: 4,
519                             textNodeIdx: [0,2]
520                         });
521
522                     expect(section.children().length).to.equal(2);
523                     expect(section.children()[0]).to.be.instanceOf(documentElement.DocumentTextElement);
524                     expect(section.children()[0].getText()).to.equal('Alice ');
525
526                     var wrapper2 = section.children()[1];
527                     expect(wrapper2.sameNode(wrapper)).to.be.true;
528
529                     var wrapperChildren = wrapper.children();
530                     expect(wrapperChildren.length).to.equal(3);
531                     expect(wrapperChildren[0].getText()).to.equal('has a ');
532
533                     expect(wrapperChildren[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
534                     expect(wrapperChildren[1].children().length).to.equal(1);
535                     expect(wrapperChildren[1].children()[0].getText()).to.equal('small');
536
537                     expect(wrapperChildren[2].getText()).to.equal(' cat');
538                 });
539             });
540
541             describe('unwrapping', function() {
542                 it('unwraps DocumentTextElement from its parent DocumentNodeElement if it\'s its only child', function() {
543                     var c = canvas.fromXML('<section>Alice <span>has a</span> cat</section>'),
544                     section = c.doc(),
545                     text = section.children()[1].children()[0];
546
547                     text.unwrap();
548
549                     expect(section.children().length).to.equal(1, 'section has one child');
550                     expect(section.children()[0].getText()).to.equal('Alice has a cat');
551                 })
552             });
553         });
554
555         describe('Lists api', function() {
556             describe('creating lists', function() {
557                 it('allows creation of a list from existing sibling DocumentElements', function() {
558                     var c = canvas.fromXML('\
559                         <section>\
560                             Alice\
561                             <div>has</div>\
562                             a\
563                             <div>cat</div>\
564                         </section>'),
565                         section = c.doc(),
566                         textHas = section.children()[1],
567                         divA = section.children()[2]
568                     
569                     c.list.create({element1: textHas, element2: divA});
570
571                     expect(section.children().length).to.equal(3, 'section has three child elements');
572
573                     var child1 = section.children()[0],
574                         list = section.children()[1],
575                         child3 = section.children()[2];
576
577                     expect(child1.getText()).to.equal('Alice');
578                     expect(list.is('list')).to.equal(true, 'second child is a list');
579                     expect(list.children().length).to.equal(2, 'list contains two elements');
580                     list.children().forEach(function(child) {
581                         expect(child.getWlxmlClass()).to.equal('item', 'list childs have wlxml class of item');
582                     });
583                     expect(child3.children()[0].getText()).to.equal('cat');
584                 });
585                 
586                 it('allows creating nested list from existing sibling list items', function() {
587                     var c = canvas.fromXML('\
588                         <section>\
589                             <div class="list-items">\
590                                 <div class="item">A</div>\
591                                 <div class="item">B</div>\
592                                 <div class="item">C</div>\
593                                 <div class="item">D</div>\
594                             </div>\
595                         </section>'),
596                         outerList = c.doc().children()[0],
597                         itemB = outerList.children()[1],
598                         itemC = outerList.children()[2];
599
600
601                         c.list.create({element1: itemB, element2: itemC});
602
603                     var outerListItems = outerList.children(),
604                         innerList = outerListItems[1].children()[0],
605                         innerListItems = innerList.children();
606
607                     expect(outerListItems.length).to.equal(3, 'outer list has three items');
608                     expect(outerListItems[0].children()[0].getText()).to.equal('A', 'first outer item ok');
609                     expect(outerListItems[1].getWlxmlClass()).to.equal('item', 'inner list is wrapped by item element');
610
611                     expect(innerList.is('list')).to.equal(true, 'inner list created');
612                     expect(innerListItems.length).to.equal(2, 'inner list has two items');
613                     expect(innerListItems[0].children()[0].getText()).to.equal('B', 'first inner item ok');
614                     expect(innerListItems[1].children()[0].getText()).to.equal('C', 'second inner item ok');
615
616                     expect(outerListItems[2].children()[0].getText()).to.equal('D', 'last outer item ok');
617
618                 });
619
620             });
621
622             describe('extracting list items', function() {
623                 it('creates two lists with extracted items in the middle if extracting from the middle of the list', function() {
624                     var c = canvas.fromXML('\
625                         <section>\
626                             <div class="list.items">\
627                                 <div class="item">0</div>\
628                                 <div class="item">1</div>\
629                                 <div class="item">2</div>\
630                                 <div class="item">3</div>\
631                             </div>\
632                         </section>'),
633                         list = c.doc().children()[0],
634                         item1 = list.children()[1],
635                         item2 = list.children()[2];
636
637                     c.list.extractItems({element1: item1, element2: item2});
638
639                     var section = c.doc(),
640                         list1 = section.children()[0],
641                         oldItem1 = section.children()[1],
642                         oldItem2 = section.children()[2],
643                         list2 = section.children()[3];
644
645                     expect(section.children().length).to.equal(4, 'section contains four children');
646                     
647                     expect(list1.is('list')).to.equal(true, 'first section child is a list');
648                     expect(list1.children().length).to.equal(1, 'first list has one child');
649                     expect(list1.children()[0].children()[0].getText()).to.equal('0', 'first item of the first list is a first item of the original list');
650
651                     expect(oldItem1.children()[0].getText()).to.equal('1', 'first item got extracted');
652                     expect(oldItem1.getWlxmlClass() === undefined).to.equal(true, 'first extracted element has no wlxml class');
653
654                     expect(oldItem2.children()[0].getText()).to.equal('2', 'second item got extracted');
655                     expect(oldItem2.getWlxmlClass() === undefined).to.equal(true, 'second extracted element has no wlxml class');
656
657                     expect(list2.is('list')).to.equal(true, 'last section child is a list');
658                     expect(list2.children().length).to.equal(1, 'second list has one child');
659                     expect(list2.children()[0].children()[0].getText()).to.equal('3', 'first item of the second list is a last item of the original list');
660                 });
661
662                 it('puts extracted items above the list if starting item is the first one', function() {
663                     var c = canvas.fromXML('\
664                         <section>\
665                             <div class="list.items">\
666                                 <div class="item">0</div>\
667                                 <div class="item">1</div>\
668                                 <div class="item">2</div>\
669                             </div>\
670                         </section>'),
671                         list = c.doc().children()[0],
672                         item1 = list.children()[0],
673                         item2 = list.children()[1],
674                         item3 = list.children()[2];
675
676                     c.list.extractItems({element1: item1, element2: item2});
677
678                     var section = c.doc(),
679                         oldItem1 = section.children()[0],
680                         oldItem2 = section.children()[1],
681                         newList = section.children()[2];
682
683                     expect(section.children().length).to.equal(3, 'section has three children');
684                     expect(oldItem1.children()[0].getText()).to.equal('0', 'first item extracted');
685                     expect(oldItem2.children()[0].getText()).to.equal('1', 'second item extracted');
686                     expect(newList.is('list')).to.equal(true, 'list lies below extracted item');
687                     expect(newList.children().length).to.equal(1, 'list has now one child');
688                 });
689
690                 it('puts extracted items below the list if ending item is the last one', function() {
691                     var c = canvas.fromXML('\
692                         <section>\
693                             <div class="list.items">\
694                                 <div class="item">0</div>\
695                                 <div class="item">1</div>\
696                                 <div class="item">2</div>\
697                             </div>\
698                         </section>'),
699                         list = c.doc().children()[0],
700                         item1 = list.children()[0],
701                         item2 = list.children()[1],
702                         item3 = list.children()[2];
703
704                     c.list.extractItems({element1: item2, element2: item3});
705
706                     var section = c.doc(),
707                         oldItem1 = section.children()[1],
708                         oldItem2 = section.children()[2],
709                         newList = section.children()[0];
710
711                     expect(section.children().length).to.equal(3, 'section has three children');
712                     expect(oldItem1.children()[0].getText()).to.equal('1', 'first item extracted');
713                     expect(oldItem2.children()[0].getText()).to.equal('2', 'second item extracted');
714                     expect(newList.is('list')).to.equal(true, 'list lies above extracted item');
715                     expect(newList.children().length).to.equal(1, 'list has now one child');
716                 });
717
718                 it('removes list if all its items are extracted', function() {
719                     var c = canvas.fromXML('\
720                         <section>\
721                             <div class="list.items">\
722                                 <div class="item">some item</div>\
723                                 <div class="item">some item 2</div>\
724                             </div>\
725                         </section>'),
726                         list = c.doc().children()[0],
727                         item1 = list.children()[0],
728                         item2 = list.children()[1];
729
730                     c.list.extractItems({element1: item1, element2: item2});
731
732                     var section = c.doc(),
733                         list1 = section.children()[0],
734                         oldItem1 = section.children()[0],
735                         oldItem2 = section.children()[1];
736
737                     expect(section.children().length).to.equal(2, 'section contains two children');
738                     expect(oldItem1.children()[0].getText()).to.equal('some item');
739                     expect(oldItem2.children()[0].getText()).to.equal('some item 2');
740                 });
741
742                 it('creates two lists with extracted items in the middle if extracting from the middle of the list - nested case' , function() {
743                     var c = canvas.fromXML('\
744                         <section>\
745                             <div class="list.items">\
746                                 <div class="item">0</div>\
747                                 <div class="item">\
748                                     <div class="list.items">\
749                                         <div class="item">1.1</div>\
750                                         <div class="item">1.2</div>\
751                                         <div class="item">1.3</div>\
752                                     </div>\
753                                 </div>\
754                                 <div class="item">2</div>\
755                             </div>\
756                         </section>'),
757                         list = c.doc().children()[0],
758                         nestedList = list.children()[1].children()[0],
759                         nestedListItem = nestedList.children()[1];
760
761                     c.list.extractItems({element1: nestedListItem, element2: nestedListItem});
762
763                     var section = c.doc(),
764                         list = section.children()[0],
765                         item1 = list.children()[0],
766                         item2 = list.children()[1], //
767                         item3 = list.children()[2],
768                         item4 = list.children()[3], //
769                         item5 = list.children()[4],
770                         nestedList1 = item2.children()[0],
771                         nestedList2 = item4.children()[0];
772
773                     expect(list.children().length).to.equal(5, 'top list has five items');
774                     
775                     expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
776
777                     expect(item2.getWlxmlClass()).to.equal('item', 'first nested list is still wrapped in item element');
778                     expect(nestedList1.children().length).to.equal(1, 'first nested list is left with one child');
779                     expect(nestedList1.children()[0].children()[0].getText()).to.equal('1.1', 'first nested list item left alone');
780                     
781                     expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
782
783                     expect(item4.getWlxmlClass()).to.equal('item', 'second nested list is still wrapped in item element');
784                     expect(nestedList2.children().length).to.equal(1, 'second nested list is left with one child');
785                     expect(nestedList2.children()[0].children()[0].getText()).to.equal('1.3', 'second nested list item left alone');
786
787                     expect(item5.children()[0].getText()).to.equal('2', 'last item ok');
788                 });
789
790                 it('puts extracted items below the list if ending item is the last one - nested case' , function() {
791                     var c = canvas.fromXML('\
792                         <section>\
793                             <div class="list.items">\
794                                 <div class="item">0</div>\
795                                 <div class="item">\
796                                     <div class="list.items">\
797                                         <div class="item">1.1</div>\
798                                         <div class="item">1.2</div>\
799                                         <div class="item">1.3</div>\
800                                     </div>\
801                                 </div>\
802                                 <div class="item">2</div>\
803                             </div>\
804                         </section>'),
805                         list = c.doc().children()[0],
806                         nestedList = list.children()[1].children()[0],
807                         nestedListItem1 = nestedList.children()[1],
808                         nestedListItem2 = nestedList.children()[2];
809
810                     c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
811
812                     var section = c.doc(),
813                         list = section.children()[0],
814                         item1 = list.children()[0],
815                         item2 = list.children()[1],
816                         item3 = list.children()[2],
817                         item4 = list.children()[3],
818                         item5 = list.children()[4];
819                     nestedList = item2.children()[0];
820
821                     expect(list.children().length).to.equal(5, 'top list has five items');
822                     expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
823                     expect(item2.getWlxmlClass()).to.equal('item', 'nested list is still wrapped in item element');
824                     expect(nestedList.children().length).to.equal(1, 'nested list is left with one child');
825                     expect(nestedList.children()[0].children()[0].getText()).to.equal('1.1', 'nested list item left alone');
826                     expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
827                     expect(item4.children()[0].getText()).to.equal('1.3', 'fourth item ok');
828                     expect(item5.children()[0].getText()).to.equal('2', 'fifth item ok');
829                 });
830
831                 it('puts extracted items above the list if starting item is the first one - nested case' , function() {
832                     var c = canvas.fromXML('\
833                         <section>\
834                             <div class="list.items">\
835                                 <div class="item">0</div>\
836                                 <div class="item">\
837                                     <div class="list.items">\
838                                         <div class="item">1.1</div>\
839                                         <div class="item">1.2</div>\
840                                         <div class="item">1.3</div>\
841                                     </div>\
842                                 </div>\
843                                 <div class="item">2</div>\
844                             </div>\
845                         </section>'),
846                         list = c.doc().children()[0],
847                         nestedList = list.children()[1].children()[0],
848                         nestedListItem1 = nestedList.children()[0],
849                         nestedListItem2 = nestedList.children()[1];
850
851                     c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
852
853                     var section = c.doc(),
854                         list = section.children()[0],
855                         item1 = list.children()[0],
856                         item2 = list.children()[1],
857                         item3 = list.children()[2],
858                         item4 = list.children()[3],
859                         item5 = list.children()[4];
860                     nestedList = item4.children()[0];
861
862                     expect(list.children().length).to.equal(5, 'top list has five items');
863                     expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
864                     expect(item2.children()[0].getText()).to.equal('1.1', 'second item ok');
865                     expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
866                     
867                     expect(item4.getWlxmlClass()).to.equal('item', 'nested list is still wrapped in item element');
868                     expect(nestedList.children().length).to.equal(1, 'nested list is left with one child');
869                     expect(nestedList.children()[0].children()[0].getText()).to.equal('1.3', 'nested list item left alone');
870                     expect(item5.children()[0].getText()).to.equal('2', 'fifth item ok');
871                 });
872
873                 it('removes list if all its items are extracted - nested case', function() {
874                     var c = canvas.fromXML('\
875                         <section>\
876                             <div class="list.items">\
877                                 <div class="item">0</div>\
878                                 <div class="item">\
879                                     <div class="list.items">\
880                                         <div class="item">1.1</div>\
881                                         <div class="item">1.2</div>\
882                                     </div>\
883                                 </div>\
884                                 <div class="item">2</div>\
885                             </div>\
886                         </section>'),
887                         list = c.doc().children()[0],
888                         nestedList = list.children()[1].children()[0],
889                         nestedListItem1 = nestedList.children()[0],
890                         nestedListItem2 = nestedList.children()[1];
891
892                     c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
893
894                     var section = c.doc(),
895                         list = section.children()[0],
896                         item1 = list.children()[0],
897                         item2 = list.children()[1],
898                         item3 = list.children()[2],
899                         item4 = list.children()[3];
900
901                     expect(list.children().length).to.equal(4, 'top list has four items');
902                     expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
903                     expect(item2.children()[0].getText()).to.equal('1.1', 'second item ok');
904                     expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
905                     expect(item4.children()[0].getText()).to.equal('2', 'fourth item ok');
906                 });
907
908                 it('extracts items out of outer most list when merge flag is set to false', function() {
909                     var c = canvas.fromXML('\
910                         <section>\
911                             <div class="list.items">\
912                                 <div class="item">0</div>\
913                                 <div class="item">\
914                                     <div class="list.items">\
915                                         <div class="item">1.1</div>\
916                                         <div class="item">1.2</div>\
917                                     </div>\
918                                 </div>\
919                                 <div class="item">2</div>\
920                             </div>\
921                         </section>'),
922                         section = c.doc(),
923                         list = section.children()[0],
924                         nestedList = list.children()[1].children()[0],
925                         nestedListItem = nestedList.children()[0];
926
927                     var test = c.list.extractItems({element1: nestedListItem, element2: nestedListItem, merge: false});
928
929                     expect(test).to.equal(true, 'extraction status ok');
930
931                     var sectionChildren = section.children(),
932                         extractedItem = sectionChildren[1];
933
934                     expect(sectionChildren.length).to.equal(3, 'section has three children');
935                     expect(sectionChildren[0].is('list')).to.equal(true, 'first child is a list');
936
937                     expect(extractedItem.getWlxmlTag()).to.equal('div', 'extracted item is a wlxml div');
938                     expect(extractedItem.getWlxmlClass()).to.equal(undefined, 'extracted item has no wlxml class');
939                     expect(extractedItem.children()[0].getText()).to.equal('1.1', 'extracted item ok');
940                     expect(sectionChildren[2].is('list')).to.equal(true, 'second child is a list');
941                 });
942             });
943         });
944
945     });
946
947     describe('Cursor', function() {
948
949         var getSelection;
950
951         beforeEach(function() {
952             getSelection = sinon.stub(window, 'getSelection');
953         });
954
955         afterEach(function() {
956             getSelection.restore();
957         });
958
959         it('returns position when browser selection collapsed', function() {
960             var c = canvas.fromXML('<section>Alice has a cat</section>'),
961                 dom = c.doc().dom(),
962                 text = $(dom.contents()[0]).contents()[0];
963
964             expect(text.nodeType).to.equal(Node.TEXT_NODE, 'correct node selected');
965             expect($(text).text()).to.equal('Alice has a cat');
966
967             getSelection.returns({
968                 anchorNode: text,
969                 focusNode: text,
970                 anchorOffset: 5,
971                 focusOffset: 5,
972                 isCollapsed: true
973             });
974             var cursor = c.getCursor(),
975                 position = cursor.getPosition();
976
977             expect(cursor.isSelecting()).to.equal(false, 'cursor is not selecting anything');
978             expect(position.element.getText()).to.equal('Alice has a cat');
979             expect(position.offset).to.equal(5);
980         });
981
982         it('returns boundries of selection when browser selection not collapsed', function() {
983             var c = canvas.fromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
984                 dom = c.doc().dom(),
985                 text = {
986                     alice: dom.contents()[0],
987                     has: $(dom.contents()[1]).contents()[0],
988                     cat: dom.contents()[4]
989                 },
990                 cursor = c.getCursor(),
991                 aliceElement = c.getDocumentElement(text.alice),
992                 catElement = c.getDocumentElement(text.cat);
993
994
995                 [
996                     {focus: text.alice, focusOffset: 1, anchor: text.cat,   anchorOffset: 2, selectionAnchor: catElement},
997                     {focus: text.cat,   focusOffset: 2, anchor: text.alice, anchorOffset: 1, selectionAnchor: aliceElement}
998                 ].forEach(function(s, idx) {
999                     getSelection.returns({isColapsed: false, anchorNode: s.anchor, anchorOffset: s.anchorOffset, focusNode: s.focus, focusOffset: s.focusOffset});
1000
1001                     var selectionStart = cursor.getSelectionStart(),
1002                         selectionEnd = cursor.getSelectionEnd(),
1003                         selectionAnchor = cursor.getSelectionAnchor();
1004
1005                     expect(cursor.isSelecting()).to.equal(true, 'cursor is selecting');
1006                     expect(selectionStart.element.sameNode(aliceElement)).to.equal(true, '"Alice" is the start of the selection ' + idx);
1007                     expect(selectionStart.offset).to.equal(1, '"Alice" offset ok' + idx);
1008                     expect(selectionEnd.element.sameNode(catElement)).to.equal(true, '"Cat" is the start of the selection ' + idx);
1009                     expect(selectionEnd.offset).to.equal(2, '"Cat" offset ok' + idx);
1010                     expect(selectionAnchor.element.sameNode(s.selectionAnchor)).to.equal(true, 'anchor ok');
1011                     expect(selectionAnchor.offset).to.equal(s.anchorOffset, 'anchor offset ok');
1012                 });
1013         });
1014
1015         it('recognizes when browser selection boundries lies in sibling DocumentTextElements', function() {
1016             var c = canvas.fromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
1017                 dom = c.doc().dom(),
1018                 text = {
1019                     alice: dom.contents()[0],
1020                     has: $(dom.contents()[1]).contents()[0],
1021                     a: dom.contents()[2],
1022                     big: $(dom.contents()[3]).contents()[0],
1023                     cat: dom.contents()[4]
1024                 },
1025                 cursor = c.getCursor();
1026
1027             expect($(text.alice).text()).to.equal('Alice ');
1028             expect($(text.has).text()).to.equal('has');
1029             expect($(text.a).text()).to.equal(' a ');
1030             expect($(text.big).text()).to.equal('big');
1031             expect($(text.cat).text()).to.equal(' cat');
1032
1033             getSelection.returns({anchorNode: text.alice, focusNode: text.a});
1034             expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "a" are children');
1035
1036             getSelection.returns({anchorNode: text.alice, focusNode: text.cat});
1037             expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "cat" are children');
1038
1039             getSelection.returns({anchorNode: text.alice, focusNode: text.has});
1040             expect(cursor.isSelectingSiblings()).to.equal(false, '"Alice" and "has" are not children');
1041
1042             getSelection.returns({anchorNode: text.has, focusNode: text.big});
1043             expect(cursor.isSelectingSiblings()).to.equal(false, '"has" and "big" are not children');
1044             
1045         })
1046     });
1047 });
1048
1049
1050 });