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