3 'modules/documentCanvas/canvas/canvas',
4 'modules/documentCanvas/canvas/documentElement'
5 ], function(chai, canvas, documentElement) {
9 var expect = chai.expect;
12 describe('Canvas', function() {
14 describe('Internal HTML representation of a sample document', function() {
15 it('works', function() {
16 var c = canvas.fromXML('\
18 This is some text without its own wrapping tag.\
19 <div class="p.subclass">\
23 This is text in a div <span>with some inline text</span>.\
25 This is some text without its own wrapping tag.\
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.'
34 expect(c.doc().dom()[0].isEqualNode($(expected)[0])).to.be.true;
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');
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');
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');
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);
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);
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');
73 describe('DocumentTextElement', function() {
74 it('can have its content set', function() {
75 var c = canvas.fromXML('<section>Alice</section>'),
77 text = root.children()[0];
79 text.setText('a cat');
80 expect(root.children()[0].getText()).to.equal('a cat');
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>'),
88 child = root.children()[1];
89 expect(root.childIndex(child)).to.equal(1);
92 it('knows WLXML tag it renders', function(){
93 var c = canvas.fromXML('<section></section>'),
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');
100 it('knows WLXML class of a WLXML tag it renders', function(){
101 var c = canvas.fromXML('<section class="some.class"></section>'),
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;
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()));
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]));
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);
136 it('is of type DocumentNodeElement', function() {
137 expect(c.doc()).to.be.instanceOf(documentElement.DocumentNodeElement);
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;
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);
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);
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);
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;
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;
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);
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);
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(' ');
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);
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);
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');
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 ');
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 ');
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');
247 describe('manipulation api', function() {
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();
255 expect(children.length).to.equal(1);
256 expect(children[0].sameNode(appended));
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();
264 expect(children.length).to.equal(1);
265 expect(children[0].sameNode(appended));
266 expect(children[0].getText()).to.equal('Alice');
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));
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));
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();
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;
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();
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');
313 describe('Splitting text', function() {
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>'),
318 text = section.children()[0].children()[0];
320 text.split({offset: 5});
321 expect(section.children().length).to.equal(2, 'section has two children');
323 var header1 = section.children()[0];
324 var header2 = section.children()[1];
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');
334 it('keeps DocumentTextElement\'s parent\'s children elements intact', function() {
335 var c = canvas.fromXML('\
338 A <span>fancy</span> and <span>nice</span> header\
342 header = section.children()[0],
343 textAnd = header.children()[2];
345 textAnd.split({offset: 2});
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');
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');
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');
366 describe('wrapping', function() {
367 it('wraps DocumentNodeElement', function() {
368 var c = canvas.fromXML('<section><div></div></section>'),
369 div = c.doc().children()[0];
371 var returned = div.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
372 parent = div.parent(),
373 parent2 = c.doc().children()[0];
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');
380 it('wraps DocumentTextElement', function() {
381 var c = canvas.fromXML('<section>Alice</section>'),
382 text = c.doc().children()[0];
384 var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
385 parent = text.parent(),
386 parent2 = c.doc().children()[0];
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');
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];
400 var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class', start: offsets.start, end: offsets.end}),
401 children = c.doc().children();
403 expect(children.length).to.equal(3);
405 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
406 expect(children[0].getText()).to.equal('Alice');
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 ');
414 expect(children[2]).to.be.instanceOf(documentElement.DocumentTextElement);
415 expect(children[2].getText()).to.equal('cat');
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];
423 var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class', start: 0, end: 15}),
424 children = c.doc().children();
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');
432 it('wraps text spanning multiple sibling DocumentTextNodes', function() {
433 var c = canvas.fromXML('<section>Alice has a <span>small</span> cat</section>'),
435 wrapper = c.wrapText({
437 _with: {tag: 'span', klass: 'some.class'},
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 ');
447 var wrapper2 = section.children()[1];
448 expect(wrapper2.sameNode(wrapper)).to.be.true;
450 var wrapperChildren = wrapper.children();
451 expect(wrapperChildren.length).to.equal(3);
452 expect(wrapperChildren[0].getText()).to.equal('has a ');
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');
458 expect(wrapperChildren[2].getText()).to.equal(' cat');
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>'),
466 text = section.children()[0].children()[0];
470 expect(section.children().length).to.equal(1);
471 expect(section.children()[0].getText()).to.equal('Alice has a cat');
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('\
487 textHas = section.children()[1],
488 divA = section.children()[2]
490 c.list.create({element1: textHas, element2: divA});
492 expect(section.children().length).to.equal(3, 'section has three child elements');
494 var child1 = section.children()[0],
495 list = section.children()[1],
496 child3 = section.children()[2];
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');
504 expect(child3.children()[0].getText()).to.equal('cat');
507 it('allows creating nested list from existing sibling list items', function() {
508 var c = canvas.fromXML('\
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>\
517 outerList = c.doc().children()[0],
518 itemB = outerList.children()[1],
519 itemC = outerList.children()[2];
522 c.list.create({element1: itemB, element2: itemC});
524 var outerListItems = outerList.children(),
525 innerList = outerListItems[1].children()[0],
526 innerListItems = innerList.children();
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');
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');
537 expect(outerListItems[2].children()[0].getText()).to.equal('D', 'last outer item ok');
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('\
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>\
554 list = c.doc().children()[0],
555 item1 = list.children()[1],
556 item2 = list.children()[2];
558 c.list.extractItems({element1: item1, element2: item2});
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];
566 expect(section.children().length).to.equal(4, 'section contains four children');
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');
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');
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');
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');
583 it('puts extracted items above the list if starting item is the first one', function() {
584 var c = canvas.fromXML('\
586 <div class="list.items">\
587 <div class="item">0</div>\
588 <div class="item">1</div>\
589 <div class="item">2</div>\
592 list = c.doc().children()[0],
593 item1 = list.children()[0],
594 item2 = list.children()[1],
595 item3 = list.children()[2];
597 c.list.extractItems({element1: item1, element2: item2});
599 var section = c.doc(),
600 oldItem1 = section.children()[0],
601 oldItem2 = section.children()[1],
602 newList = section.children()[2];
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');
611 it('puts extracted items below the list if ending item is the last one', function() {
612 var c = canvas.fromXML('\
614 <div class="list.items">\
615 <div class="item">0</div>\
616 <div class="item">1</div>\
617 <div class="item">2</div>\
620 list = c.doc().children()[0],
621 item1 = list.children()[0],
622 item2 = list.children()[1],
623 item3 = list.children()[2];
625 c.list.extractItems({element1: item2, element2: item3});
627 var section = c.doc(),
628 oldItem1 = section.children()[1],
629 oldItem2 = section.children()[2],
630 newList = section.children()[0];
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');
639 it('removes list if all its items are extracted', function() {
640 var c = canvas.fromXML('\
642 <div class="list.items">\
643 <div class="item">some item</div>\
644 <div class="item">some item 2</div>\
647 list = c.doc().children()[0],
648 item1 = list.children()[0],
649 item2 = list.children()[1];
651 c.list.extractItems({element1: item1, element2: item2});
653 var section = c.doc(),
654 list1 = section.children()[0],
655 oldItem1 = section.children()[0],
656 oldItem2 = section.children()[1];
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');
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('\
666 <div class="list.items">\
667 <div class="item">0</div>\
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>\
675 <div class="item">2</div>\
678 list = c.doc().children()[0],
679 nestedList = list.children()[1].children()[0],
680 nestedListItem = nestedList.children()[1];
682 c.list.extractItems({element1: nestedListItem, element2: nestedListItem});
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];
694 expect(list.children().length).to.equal(5, 'top list has five items');
696 expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
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');
702 expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
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');
708 expect(item5.children()[0].getText()).to.equal('2', 'last item ok');
711 it('puts extracted items below the list if ending item is the last one - nested case' , function() {
712 var c = canvas.fromXML('\
714 <div class="list.items">\
715 <div class="item">0</div>\
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>\
723 <div class="item">2</div>\
726 list = c.doc().children()[0],
727 nestedList = list.children()[1].children()[0],
728 nestedListItem1 = nestedList.children()[1],
729 nestedListItem2 = nestedList.children()[2];
731 c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
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];
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');
752 it('puts extracted items above the list if starting item is the first one - nested case' , function() {
753 var c = canvas.fromXML('\
755 <div class="list.items">\
756 <div class="item">0</div>\
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>\
764 <div class="item">2</div>\
767 list = c.doc().children()[0],
768 nestedList = list.children()[1].children()[0],
769 nestedListItem1 = nestedList.children()[0],
770 nestedListItem2 = nestedList.children()[1];
772 c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
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];
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');
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');
794 it('removes list if all its items are extracted - nested case', function() {
795 var c = canvas.fromXML('\
797 <div class="list.items">\
798 <div class="item">0</div>\
800 <div class="list.items">\
801 <div class="item">1.1</div>\
802 <div class="item">1.2</div>\
805 <div class="item">2</div>\
808 list = c.doc().children()[0],
809 nestedList = list.children()[1].children()[0],
810 nestedListItem1 = nestedList.children()[0],
811 nestedListItem2 = nestedList.children()[1];
813 c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
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];
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');
829 it('extracts items out of outer most list when merge flag is set to false', function() {
830 var c = canvas.fromXML('\
832 <div class="list.items">\
833 <div class="item">0</div>\
835 <div class="list.items">\
836 <div class="item">1.1</div>\
837 <div class="item">1.2</div>\
840 <div class="item">2</div>\
844 list = section.children()[0],
845 nestedList = list.children()[1].children()[0],
846 nestedListItem = nestedList.children()[0];
848 var test = c.list.extractItems({element1: nestedListItem, element2: nestedListItem, merge: false});
850 expect(test).to.equal(true, 'extraction status ok');
852 var sectionChildren = section.children(),
853 extractedItem = sectionChildren[1];
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');
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');