0b71ee0bbdc45bcf6e227aef39a0a0312207a805
[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                     var returnedValue = 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                     expect(returnedValue.first.sameNode(header1)).to.equal(true, 'first node returnde');
385                     expect(returnedValue.second.sameNode(header2)).to.equal(true, 'second node returned');
386                 });
387
388                 it('leaves empty copy of DocumentNodeElement if splitting at the very beginning', function() {
389                         var c = canvas.fromXML('<section><header>Some header</header></section>'),
390                         section = c.doc(),
391                         text = section.children()[0].children()[0];
392
393                         text.split({offset: 0});
394                         
395                         var header1 = section.children()[0];
396                         var header2 = section.children()[1];
397
398                         expect(header1.children().length).to.equal(0);
399                         expect(header2.children()[0].getText()).to.equal('Some header');
400                 });
401
402                 it('leaves empty copy of DocumentNodeElement if splitting at the very end', function() {
403                         var c = canvas.fromXML('<section><header>Some header</header></section>'),
404                         section = c.doc(),
405                         text = section.children()[0].children()[0];
406
407                         text.split({offset: 11});
408                         
409                         var header1 = section.children()[0];
410                         var header2 = section.children()[1];
411
412                         expect(header1.children()[0].getText()).to.equal('Some header');
413                         expect(header2.children().length).to.equal(0);
414                 });
415
416                 it('keeps DocumentTextElement\'s parent\'s children elements intact', function() {
417                     var c = canvas.fromXML('\
418                             <section>\
419                                 <header>\
420                                     A <span>fancy</span> and <span>nice</span> header\
421                                 </header>\
422                             </section>'),
423                         section = c.doc(),
424                         header = section.children()[0],
425                         textAnd = header.children()[2];
426
427                     textAnd.split({offset: 2});
428                     
429                     var sectionChildren = section.children();
430                     expect(sectionChildren.length).to.equal(2, 'Section has two children');
431                     expect(sectionChildren[0].getWlxmlTag()).to.equal('header', 'First section element is a wlxml header');
432                     expect(sectionChildren[1].getWlxmlTag()).to.equal('header', 'Second section element is a wlxml header');
433
434                     var firstHeaderChildren = sectionChildren[0].children();
435                     expect(firstHeaderChildren.length).to.equal(3, 'First header has three children');
436                     expect(firstHeaderChildren[0].getText()).to.equal('A ', 'First header starts with a text');
437                     expect(firstHeaderChildren[1].getWlxmlTag()).to.equal('span', 'First header has span in the middle');
438                     expect(firstHeaderChildren[2].getText()).to.equal(' a', 'First header ends with text');
439
440                     var secondHeaderChildren = sectionChildren[1].children();
441                     expect(secondHeaderChildren.length).to.equal(3, 'Second header has three children');
442                     expect(secondHeaderChildren[0].getText()).to.equal('nd ', 'Second header starts with text');
443                     expect(secondHeaderChildren[1].getWlxmlTag()).to.equal('span', 'Second header has span in the middle');
444                     expect(secondHeaderChildren[2].getText()).to.equal(' header', 'Second header ends with text');
445                 });
446             });
447
448             describe('wrapping', function() {
449                 it('wraps DocumentNodeElement', function() {
450                     var c = canvas.fromXML('<section><div></div></section>'),
451                         div = c.doc().children()[0];
452                     
453                     var returned = div.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
454                         parent = div.parent(),
455                         parent2 = c.doc().children()[0];
456
457                     expect(returned.sameNode(parent)).to.be.true;
458                     expect(returned.sameNode(parent2)).to.be.true;
459                     expect(returned.getWlxmlTag()).to.equal('header');
460                     expect(returned.getWlxmlClass()).to.equal('some.class');
461                 });
462                 it('wraps DocumentTextElement', function() {
463                     var c = canvas.fromXML('<section>Alice</section>'),
464                         text = c.doc().children()[0];
465                     
466                     var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
467                         parent = text.parent(),
468                         parent2 = c.doc().children()[0];
469
470                     expect(returned.sameNode(parent)).to.be.true;
471                     expect(returned.sameNode(parent2)).to.be.true;
472                     expect(returned.getWlxmlTag()).to.equal('header');
473                     expect(returned.getWlxmlClass()).to.equal('some.class');
474                 });
475                 
476                 describe('wrapping part of DocumentTextElement', function() {
477                     [{start: 5, end: 12}, {start: 12, end: 5}].forEach(function(offsets) {
478                         it('wraps in the middle ' + offsets.start + '/' + offsets.end, function() {
479                             var c = canvas.fromXML('<section>Alice has a cat</section>'),
480                                 text = c.doc().children()[0];
481                             
482                             var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class', start: offsets.start, end: offsets.end}),
483                                 children = c.doc().children();
484
485                             expect(children.length).to.equal(3);
486                             
487                             expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
488                             expect(children[0].getText()).to.equal('Alice');
489
490                             expect(children[1].sameNode(returned)).to.be.true;
491                             expect(returned.getWlxmlTag()).to.equal('header');
492                             expect(returned.getWlxmlClass()).to.equal('some.class');
493                             expect(children[1].children().length).to.equal(1);
494                             expect(children[1].children()[0].getText()).to.equal(' has a ');
495
496                             expect(children[2]).to.be.instanceOf(documentElement.DocumentTextElement);
497                             expect(children[2].getText()).to.equal('cat');
498                         });
499                     });
500
501                     it('wraps whole text inside DocumentTextElement if offsets span entire content', function() {
502                          var c = canvas.fromXML('<section>Alice has a cat</section>'),
503                              text = c.doc().children()[0];
504                          
505                          var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class', start: 0, end: 15}),
506                              children = c.doc().children();
507
508                          expect(children.length).to.equal(1);
509                          expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
510                          expect(children[0].children()[0].getText()).to.equal('Alice has a cat');
511                     });
512                 });
513
514                 it('wraps text spanning multiple sibling DocumentTextNodes', function() {
515                     var c = canvas.fromXML('<section>Alice has a <span>small</span> cat</section>'),
516                         section = c.doc(),
517                         wrapper = c.wrapText({
518                             inside: section, 
519                             _with: {tag: 'span', klass: 'some.class'},
520                             offsetStart: 6,
521                             offsetEnd: 4,
522                             textNodeIdx: [0,2]
523                         });
524
525                     expect(section.children().length).to.equal(2);
526                     expect(section.children()[0]).to.be.instanceOf(documentElement.DocumentTextElement);
527                     expect(section.children()[0].getText()).to.equal('Alice ');
528
529                     var wrapper2 = section.children()[1];
530                     expect(wrapper2.sameNode(wrapper)).to.be.true;
531
532                     var wrapperChildren = wrapper.children();
533                     expect(wrapperChildren.length).to.equal(3);
534                     expect(wrapperChildren[0].getText()).to.equal('has a ');
535
536                     expect(wrapperChildren[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
537                     expect(wrapperChildren[1].children().length).to.equal(1);
538                     expect(wrapperChildren[1].children()[0].getText()).to.equal('small');
539
540                     expect(wrapperChildren[2].getText()).to.equal(' cat');
541                 });
542             });
543
544             describe('unwrapping', function() {
545                 it('unwraps DocumentTextElement from its parent DocumentNodeElement if it\'s its only child', function() {
546                     var c = canvas.fromXML('<section>Alice <span>has a</span> cat</section>'),
547                     section = c.doc(),
548                     text = section.children()[1].children()[0];
549
550                     text.unwrap();
551
552                     expect(section.children().length).to.equal(1, 'section has one child');
553                     expect(section.children()[0].getText()).to.equal('Alice has a cat');
554                 })
555             });
556         });
557
558         describe('Lists api', function() {
559             describe('creating lists', function() {
560                 it('allows creation of a list from existing sibling DocumentElements', function() {
561                     var c = canvas.fromXML('\
562                         <section>\
563                             Alice\
564                             <div>has</div>\
565                             a\
566                             <div>cat</div>\
567                         </section>'),
568                         section = c.doc(),
569                         textHas = section.children()[1],
570                         divA = section.children()[2]
571                     
572                     c.list.create({element1: textHas, element2: divA});
573
574                     expect(section.children().length).to.equal(3, 'section has three child elements');
575
576                     var child1 = section.children()[0],
577                         list = section.children()[1],
578                         child3 = section.children()[2];
579
580                     expect(child1.getText()).to.equal('Alice');
581                     expect(list.is('list')).to.equal(true, 'second child is a list');
582                     expect(list.children().length).to.equal(2, 'list contains two elements');
583                     list.children().forEach(function(child) {
584                         expect(child.getWlxmlClass()).to.equal('item', 'list childs have wlxml class of item');
585                     });
586                     expect(child3.children()[0].getText()).to.equal('cat');
587                 });
588                 
589                 it('allows creating nested list from existing sibling list items', function() {
590                     var c = canvas.fromXML('\
591                         <section>\
592                             <div class="list-items">\
593                                 <div class="item">A</div>\
594                                 <div class="item">B</div>\
595                                 <div class="item">C</div>\
596                                 <div class="item">D</div>\
597                             </div>\
598                         </section>'),
599                         outerList = c.doc().children()[0],
600                         itemB = outerList.children()[1],
601                         itemC = outerList.children()[2];
602
603
604                         c.list.create({element1: itemB, element2: itemC});
605
606                     var outerListItems = outerList.children(),
607                         innerList = outerListItems[1].children()[0],
608                         innerListItems = innerList.children();
609
610                     expect(outerListItems.length).to.equal(3, 'outer list has three items');
611                     expect(outerListItems[0].children()[0].getText()).to.equal('A', 'first outer item ok');
612                     expect(outerListItems[1].getWlxmlClass()).to.equal('item', 'inner list is wrapped by item element');
613
614                     expect(innerList.is('list')).to.equal(true, 'inner list created');
615                     expect(innerListItems.length).to.equal(2, 'inner list has two items');
616                     expect(innerListItems[0].children()[0].getText()).to.equal('B', 'first inner item ok');
617                     expect(innerListItems[1].children()[0].getText()).to.equal('C', 'second inner item ok');
618
619                     expect(outerListItems[2].children()[0].getText()).to.equal('D', 'last outer item ok');
620
621                 });
622
623             });
624
625             describe('extracting list items', function() {
626                 it('creates two lists with extracted items in the middle if extracting from the middle of the list', function() {
627                     var c = canvas.fromXML('\
628                         <section>\
629                             <div class="list.items">\
630                                 <div class="item">0</div>\
631                                 <div class="item">1</div>\
632                                 <div class="item">2</div>\
633                                 <div class="item">3</div>\
634                             </div>\
635                         </section>'),
636                         list = c.doc().children()[0],
637                         item1 = list.children()[1],
638                         item2 = list.children()[2];
639
640                     c.list.extractItems({element1: item1, element2: item2});
641
642                     var section = c.doc(),
643                         list1 = section.children()[0],
644                         oldItem1 = section.children()[1],
645                         oldItem2 = section.children()[2],
646                         list2 = section.children()[3];
647
648                     expect(section.children().length).to.equal(4, 'section contains four children');
649                     
650                     expect(list1.is('list')).to.equal(true, 'first section child is a list');
651                     expect(list1.children().length).to.equal(1, 'first list has one child');
652                     expect(list1.children()[0].children()[0].getText()).to.equal('0', 'first item of the first list is a first item of the original list');
653
654                     expect(oldItem1.children()[0].getText()).to.equal('1', 'first item got extracted');
655                     expect(oldItem1.getWlxmlClass() === undefined).to.equal(true, 'first extracted element has no wlxml class');
656
657                     expect(oldItem2.children()[0].getText()).to.equal('2', 'second item got extracted');
658                     expect(oldItem2.getWlxmlClass() === undefined).to.equal(true, 'second extracted element has no wlxml class');
659
660                     expect(list2.is('list')).to.equal(true, 'last section child is a list');
661                     expect(list2.children().length).to.equal(1, 'second list has one child');
662                     expect(list2.children()[0].children()[0].getText()).to.equal('3', 'first item of the second list is a last item of the original list');
663                 });
664
665                 it('puts extracted items above the list if starting item is the first one', function() {
666                     var c = canvas.fromXML('\
667                         <section>\
668                             <div class="list.items">\
669                                 <div class="item">0</div>\
670                                 <div class="item">1</div>\
671                                 <div class="item">2</div>\
672                             </div>\
673                         </section>'),
674                         list = c.doc().children()[0],
675                         item1 = list.children()[0],
676                         item2 = list.children()[1],
677                         item3 = list.children()[2];
678
679                     c.list.extractItems({element1: item1, element2: item2});
680
681                     var section = c.doc(),
682                         oldItem1 = section.children()[0],
683                         oldItem2 = section.children()[1],
684                         newList = section.children()[2];
685
686                     expect(section.children().length).to.equal(3, 'section has three children');
687                     expect(oldItem1.children()[0].getText()).to.equal('0', 'first item extracted');
688                     expect(oldItem2.children()[0].getText()).to.equal('1', 'second item extracted');
689                     expect(newList.is('list')).to.equal(true, 'list lies below extracted item');
690                     expect(newList.children().length).to.equal(1, 'list has now one child');
691                 });
692
693                 it('puts extracted items below the list if ending item is the last one', function() {
694                     var c = canvas.fromXML('\
695                         <section>\
696                             <div class="list.items">\
697                                 <div class="item">0</div>\
698                                 <div class="item">1</div>\
699                                 <div class="item">2</div>\
700                             </div>\
701                         </section>'),
702                         list = c.doc().children()[0],
703                         item1 = list.children()[0],
704                         item2 = list.children()[1],
705                         item3 = list.children()[2];
706
707                     c.list.extractItems({element1: item2, element2: item3});
708
709                     var section = c.doc(),
710                         oldItem1 = section.children()[1],
711                         oldItem2 = section.children()[2],
712                         newList = section.children()[0];
713
714                     expect(section.children().length).to.equal(3, 'section has three children');
715                     expect(oldItem1.children()[0].getText()).to.equal('1', 'first item extracted');
716                     expect(oldItem2.children()[0].getText()).to.equal('2', 'second item extracted');
717                     expect(newList.is('list')).to.equal(true, 'list lies above extracted item');
718                     expect(newList.children().length).to.equal(1, 'list has now one child');
719                 });
720
721                 it('removes list if all its items are extracted', function() {
722                     var c = canvas.fromXML('\
723                         <section>\
724                             <div class="list.items">\
725                                 <div class="item">some item</div>\
726                                 <div class="item">some item 2</div>\
727                             </div>\
728                         </section>'),
729                         list = c.doc().children()[0],
730                         item1 = list.children()[0],
731                         item2 = list.children()[1];
732
733                     c.list.extractItems({element1: item1, element2: item2});
734
735                     var section = c.doc(),
736                         list1 = section.children()[0],
737                         oldItem1 = section.children()[0],
738                         oldItem2 = section.children()[1];
739
740                     expect(section.children().length).to.equal(2, 'section contains two children');
741                     expect(oldItem1.children()[0].getText()).to.equal('some item');
742                     expect(oldItem2.children()[0].getText()).to.equal('some item 2');
743                 });
744
745                 it('creates two lists with extracted items in the middle if extracting from the middle of the list - nested case' , function() {
746                     var c = canvas.fromXML('\
747                         <section>\
748                             <div class="list.items">\
749                                 <div class="item">0</div>\
750                                 <div class="item">\
751                                     <div class="list.items">\
752                                         <div class="item">1.1</div>\
753                                         <div class="item">1.2</div>\
754                                         <div class="item">1.3</div>\
755                                     </div>\
756                                 </div>\
757                                 <div class="item">2</div>\
758                             </div>\
759                         </section>'),
760                         list = c.doc().children()[0],
761                         nestedList = list.children()[1].children()[0],
762                         nestedListItem = nestedList.children()[1];
763
764                     c.list.extractItems({element1: nestedListItem, element2: nestedListItem});
765
766                     var section = c.doc(),
767                         list = section.children()[0],
768                         item1 = list.children()[0],
769                         item2 = list.children()[1], //
770                         item3 = list.children()[2],
771                         item4 = list.children()[3], //
772                         item5 = list.children()[4],
773                         nestedList1 = item2.children()[0],
774                         nestedList2 = item4.children()[0];
775
776                     expect(list.children().length).to.equal(5, 'top list has five items');
777                     
778                     expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
779
780                     expect(item2.getWlxmlClass()).to.equal('item', 'first nested list is still wrapped in item element');
781                     expect(nestedList1.children().length).to.equal(1, 'first nested list is left with one child');
782                     expect(nestedList1.children()[0].children()[0].getText()).to.equal('1.1', 'first nested list item left alone');
783                     
784                     expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
785
786                     expect(item4.getWlxmlClass()).to.equal('item', 'second nested list is still wrapped in item element');
787                     expect(nestedList2.children().length).to.equal(1, 'second nested list is left with one child');
788                     expect(nestedList2.children()[0].children()[0].getText()).to.equal('1.3', 'second nested list item left alone');
789
790                     expect(item5.children()[0].getText()).to.equal('2', 'last item ok');
791                 });
792
793                 it('puts extracted items below the list if ending item is the last one - nested case' , function() {
794                     var c = canvas.fromXML('\
795                         <section>\
796                             <div class="list.items">\
797                                 <div class="item">0</div>\
798                                 <div class="item">\
799                                     <div class="list.items">\
800                                         <div class="item">1.1</div>\
801                                         <div class="item">1.2</div>\
802                                         <div class="item">1.3</div>\
803                                     </div>\
804                                 </div>\
805                                 <div class="item">2</div>\
806                             </div>\
807                         </section>'),
808                         list = c.doc().children()[0],
809                         nestedList = list.children()[1].children()[0],
810                         nestedListItem1 = nestedList.children()[1],
811                         nestedListItem2 = nestedList.children()[2];
812
813                     c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
814
815                     var section = c.doc(),
816                         list = section.children()[0],
817                         item1 = list.children()[0],
818                         item2 = list.children()[1],
819                         item3 = list.children()[2],
820                         item4 = list.children()[3],
821                         item5 = list.children()[4];
822                     nestedList = item2.children()[0];
823
824                     expect(list.children().length).to.equal(5, 'top list has five items');
825                     expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
826                     expect(item2.getWlxmlClass()).to.equal('item', 'nested list is still wrapped in item element');
827                     expect(nestedList.children().length).to.equal(1, 'nested list is left with one child');
828                     expect(nestedList.children()[0].children()[0].getText()).to.equal('1.1', 'nested list item left alone');
829                     expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
830                     expect(item4.children()[0].getText()).to.equal('1.3', 'fourth item ok');
831                     expect(item5.children()[0].getText()).to.equal('2', 'fifth item ok');
832                 });
833
834                 it('puts extracted items above the list if starting item is the first one - nested case' , function() {
835                     var c = canvas.fromXML('\
836                         <section>\
837                             <div class="list.items">\
838                                 <div class="item">0</div>\
839                                 <div class="item">\
840                                     <div class="list.items">\
841                                         <div class="item">1.1</div>\
842                                         <div class="item">1.2</div>\
843                                         <div class="item">1.3</div>\
844                                     </div>\
845                                 </div>\
846                                 <div class="item">2</div>\
847                             </div>\
848                         </section>'),
849                         list = c.doc().children()[0],
850                         nestedList = list.children()[1].children()[0],
851                         nestedListItem1 = nestedList.children()[0],
852                         nestedListItem2 = nestedList.children()[1];
853
854                     c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
855
856                     var section = c.doc(),
857                         list = section.children()[0],
858                         item1 = list.children()[0],
859                         item2 = list.children()[1],
860                         item3 = list.children()[2],
861                         item4 = list.children()[3],
862                         item5 = list.children()[4];
863                     nestedList = item4.children()[0];
864
865                     expect(list.children().length).to.equal(5, 'top list has five items');
866                     expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
867                     expect(item2.children()[0].getText()).to.equal('1.1', 'second item ok');
868                     expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
869                     
870                     expect(item4.getWlxmlClass()).to.equal('item', 'nested list is still wrapped in item element');
871                     expect(nestedList.children().length).to.equal(1, 'nested list is left with one child');
872                     expect(nestedList.children()[0].children()[0].getText()).to.equal('1.3', 'nested list item left alone');
873                     expect(item5.children()[0].getText()).to.equal('2', 'fifth item ok');
874                 });
875
876                 it('removes list if all its items are extracted - nested case', function() {
877                     var c = canvas.fromXML('\
878                         <section>\
879                             <div class="list.items">\
880                                 <div class="item">0</div>\
881                                 <div class="item">\
882                                     <div class="list.items">\
883                                         <div class="item">1.1</div>\
884                                         <div class="item">1.2</div>\
885                                     </div>\
886                                 </div>\
887                                 <div class="item">2</div>\
888                             </div>\
889                         </section>'),
890                         list = c.doc().children()[0],
891                         nestedList = list.children()[1].children()[0],
892                         nestedListItem1 = nestedList.children()[0],
893                         nestedListItem2 = nestedList.children()[1];
894
895                     c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
896
897                     var section = c.doc(),
898                         list = section.children()[0],
899                         item1 = list.children()[0],
900                         item2 = list.children()[1],
901                         item3 = list.children()[2],
902                         item4 = list.children()[3];
903
904                     expect(list.children().length).to.equal(4, 'top list has four items');
905                     expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
906                     expect(item2.children()[0].getText()).to.equal('1.1', 'second item ok');
907                     expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
908                     expect(item4.children()[0].getText()).to.equal('2', 'fourth item ok');
909                 });
910
911                 it('extracts items out of outer most list when merge flag is set to false', function() {
912                     var c = canvas.fromXML('\
913                         <section>\
914                             <div class="list.items">\
915                                 <div class="item">0</div>\
916                                 <div class="item">\
917                                     <div class="list.items">\
918                                         <div class="item">1.1</div>\
919                                         <div class="item">1.2</div>\
920                                     </div>\
921                                 </div>\
922                                 <div class="item">2</div>\
923                             </div>\
924                         </section>'),
925                         section = c.doc(),
926                         list = section.children()[0],
927                         nestedList = list.children()[1].children()[0],
928                         nestedListItem = nestedList.children()[0];
929
930                     var test = c.list.extractItems({element1: nestedListItem, element2: nestedListItem, merge: false});
931
932                     expect(test).to.equal(true, 'extraction status ok');
933
934                     var sectionChildren = section.children(),
935                         extractedItem = sectionChildren[1];
936
937                     expect(sectionChildren.length).to.equal(3, 'section has three children');
938                     expect(sectionChildren[0].is('list')).to.equal(true, 'first child is a list');
939
940                     expect(extractedItem.getWlxmlTag()).to.equal('div', 'extracted item is a wlxml div');
941                     expect(extractedItem.getWlxmlClass()).to.equal(undefined, 'extracted item has no wlxml class');
942                     expect(extractedItem.children()[0].getText()).to.equal('1.1', 'extracted item ok');
943                     expect(sectionChildren[2].is('list')).to.equal(true, 'second child is a list');
944                 });
945             });
946         });
947
948     });
949
950     describe('Cursor', function() {
951
952         var getSelection;
953
954         beforeEach(function() {
955             getSelection = sinon.stub(window, 'getSelection');
956         });
957
958         afterEach(function() {
959             getSelection.restore();
960         });
961
962         it('returns position when browser selection collapsed', function() {
963             var c = canvas.fromXML('<section>Alice has a cat</section>'),
964                 dom = c.doc().dom(),
965                 text = $(dom.contents()[0]).contents()[0];
966
967             expect(text.nodeType).to.equal(Node.TEXT_NODE, 'correct node selected');
968             expect($(text).text()).to.equal('Alice has a cat');
969
970             getSelection.returns({
971                 anchorNode: text,
972                 focusNode: text,
973                 anchorOffset: 5,
974                 focusOffset: 5,
975                 isCollapsed: true
976             });
977             var cursor = c.getCursor(),
978                 position = cursor.getPosition();
979
980             expect(cursor.isSelecting()).to.equal(false, 'cursor is not selecting anything');
981             expect(position.element.getText()).to.equal('Alice has a cat');
982             expect(position.offset).to.equal(5);
983         });
984
985         it('returns boundries of selection when browser selection not collapsed', function() {
986             var c = canvas.fromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
987                 dom = c.doc().dom(),
988                 text = {
989                     alice: dom.contents()[0],
990                     has: $(dom.contents()[1]).contents()[0],
991                     cat: dom.contents()[4]
992                 },
993                 cursor = c.getCursor(),
994                 aliceElement = c.getDocumentElement(text.alice),
995                 catElement = c.getDocumentElement(text.cat);
996
997
998                 [
999                     {focus: text.alice, focusOffset: 1, anchor: text.cat,   anchorOffset: 2, selectionAnchor: catElement},
1000                     {focus: text.cat,   focusOffset: 2, anchor: text.alice, anchorOffset: 1, selectionAnchor: aliceElement}
1001                 ].forEach(function(s, idx) {
1002                     getSelection.returns({isColapsed: false, anchorNode: s.anchor, anchorOffset: s.anchorOffset, focusNode: s.focus, focusOffset: s.focusOffset});
1003
1004                     var selectionStart = cursor.getSelectionStart(),
1005                         selectionEnd = cursor.getSelectionEnd(),
1006                         selectionAnchor = cursor.getSelectionAnchor();
1007
1008                     expect(cursor.isSelecting()).to.equal(true, 'cursor is selecting');
1009                     expect(selectionStart.element.sameNode(aliceElement)).to.equal(true, '"Alice" is the start of the selection ' + idx);
1010                     expect(selectionStart.offset).to.equal(1, '"Alice" offset ok' + idx);
1011                     expect(selectionEnd.element.sameNode(catElement)).to.equal(true, '"Cat" is the start of the selection ' + idx);
1012                     expect(selectionEnd.offset).to.equal(2, '"Cat" offset ok' + idx);
1013                     expect(selectionAnchor.element.sameNode(s.selectionAnchor)).to.equal(true, 'anchor ok');
1014                     expect(selectionAnchor.offset).to.equal(s.anchorOffset, 'anchor offset ok');
1015                 });
1016         });
1017
1018         it('recognizes when browser selection boundries lies in sibling DocumentTextElements', function() {
1019             var c = canvas.fromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
1020                 dom = c.doc().dom(),
1021                 text = {
1022                     alice: dom.contents()[0],
1023                     has: $(dom.contents()[1]).contents()[0],
1024                     a: dom.contents()[2],
1025                     big: $(dom.contents()[3]).contents()[0],
1026                     cat: dom.contents()[4]
1027                 },
1028                 cursor = c.getCursor();
1029
1030             expect($(text.alice).text()).to.equal('Alice ');
1031             expect($(text.has).text()).to.equal('has');
1032             expect($(text.a).text()).to.equal(' a ');
1033             expect($(text.big).text()).to.equal('big');
1034             expect($(text.cat).text()).to.equal(' cat');
1035
1036             getSelection.returns({anchorNode: text.alice, focusNode: text.a});
1037             expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "a" are children');
1038
1039             getSelection.returns({anchorNode: text.alice, focusNode: text.cat});
1040             expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "cat" are children');
1041
1042             getSelection.returns({anchorNode: text.alice, focusNode: text.has});
1043             expect(cursor.isSelectingSiblings()).to.equal(false, '"Alice" and "has" are not children');
1044
1045             getSelection.returns({anchorNode: text.has, focusNode: text.big});
1046             expect(cursor.isSelectingSiblings()).to.equal(false, '"has" and "big" are not children');
1047             
1048         })
1049     });
1050 });
1051
1052
1053 });