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