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