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