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