4 'modules/documentCanvas/canvas/canvas',
5 'modules/documentCanvas/canvas/documentElement'
6 ], function(chai, sinon, canvas, documentElement) {
10 var expect = chai.expect;
13 describe('Canvas', function() {
15 describe('Internal HTML representation of a sample document', function() {
16 it('works', function() {
17 var c = canvas.fromXML('\
19 This is some text without its own wrapping tag.\
20 <div class="p.subclass">\
24 This is text in a div <span>with some inline text</span>.\
26 This is some text without its own wrapping tag.\
29 var expected = '<div wlxml-tag="section">'
30 + 'This is some text without its own wrapping tag.'
31 + '<div wlxml-tag="div" wlxml-class="p-subclass">This is a paragraph.</div>'
32 + '<div wlxml-tag="div">This is text in a div <div wlxml-tag="span">with some inline text</div>.</div>'
33 + 'This is some text without its own wrapping tag.'
35 expect(c.doc().dom()[0].isEqualNode($(expected)[0])).to.be.true;
39 describe('Internal HTML representation of a DocumentNodeElement', function() {
40 it('is always a div tag', function() {
41 ['section', 'header', 'span', 'aside', 'figure'].forEach(function(tagName) {
42 var dom = canvas.fromXML('<' + tagName + '></' + tagName + '>').doc().dom();
43 expect(dom.prop('tagName')).to.equal('DIV', tagName + ' is represented as div');
46 it('has wlxml tag put into wlxml-tag attribute', function() {
47 var dom = canvas.fromXML('<section></section>').doc().dom();
48 expect(dom.attr('wlxml-tag')).to.equal('section');
50 it('has wlxml class put into wlxml-class, dots replaced with dashes', function() {
51 var dom = canvas.fromXML('<section class="some.class"></section>').doc().dom();
52 expect(dom.attr('wlxml-class')).to.equal('some-class');
56 describe('Internal HTML representation of a DocumentTextElement', function() {
57 it('is just a TextNode', function() {
58 var dom = canvas.fromXML('<section>Alice</section>').doc().children()[0].dom();
59 expect(dom[0].nodeType === Node.TEXT_NODE);
63 describe('basic properties', function() {
64 it('renders empty document when canvas created from empty XML', function() {
65 var c = canvas.fromXML('');
66 expect(c.doc()).to.equal(null);
69 it('gives access to its document root node', function() {
70 var c = canvas.fromXML('<section></section>');
71 expect(c.doc().wlxmlTag).to.equal('section');
74 describe('DocumentTextElement', function() {
75 it('can have its content set', function() {
76 var c = canvas.fromXML('<section>Alice</section>'),
78 text = root.children()[0];
80 text.setText('a cat');
81 expect(root.children()[0].getText()).to.equal('a cat');
85 describe('DocumentNodeElement', function() {
86 it('knows index of its child', function() {
87 var c = canvas.fromXML('<section><div></div><header></header><span></span></section>'),
89 child = root.children()[1];
90 expect(root.childIndex(child)).to.equal(1);
93 it('knows WLXML tag it renders', function(){
94 var c = canvas.fromXML('<section></section>'),
96 expect(section.getWlxmlTag()).to.equal('section', 'initial tag is section');
97 section.setWlxmlTag('header');
98 expect(section.getWlxmlTag()).to.equal('header', 'tag is changed to header');
101 it('knows WLXML class of a WLXML tag it renders', function(){
102 var c = canvas.fromXML('<section class="some.class"></section>'),
104 expect(section.getWlxmlClass()).to.equal('some.class');
105 section.setWlxmlClass('some.other.class');
106 expect(section.getWlxmlClass()).to.equal('some.other.class');
107 section.setWlxmlClass(null);
108 expect(section.getWlxmlClass()).to.be.undefined;
112 it('returns DocumentNodeElement instance from HTMLElement', function() {
113 var c = canvas.fromXML('<section></section>'),
114 htmlElement = c.doc().dom().get(0),
115 element = c.getDocumentElement(htmlElement);
116 expect(element).to.be.instanceOf(documentElement.DocumentNodeElement);
117 expect(element.sameNode(c.doc()));
120 it('returns DocumentTextElement instance from Text Node', function() {
121 var c = canvas.fromXML('<section>Alice</section>'),
122 textNode = c.doc().children(0)[0].dom().get(0),
123 element = c.getDocumentElement(textNode);
124 expect(element).to.be.instanceOf(documentElement.DocumentTextElement);
125 expect(element.sameNode(c.doc().children()[0]));
131 describe('document representation api', function() {
132 describe('document root element', function() {
133 var c = canvas.fromXML('<section></section>');
134 it('exists', function() {
135 expect(c.doc()).to.be.instanceOf(documentElement.DocumentElement);
137 it('is of type DocumentNodeElement', function() {
138 expect(c.doc()).to.be.instanceOf(documentElement.DocumentNodeElement);
142 describe('DocumentElements comparison', function() {
143 it('reports dwo DocumentElements to be the same when they represent the same wlxml document element', function() {
144 var c = canvas.fromXML('<section><div></div><div></div></section>'),
145 first_div1 = c.doc().children()[0],
146 first_div2 = c.doc().children()[0],
147 second_div = c.doc().children()[1];
148 expect(first_div1.sameNode(first_div1)).to.be.true;
149 expect(first_div1.sameNode(first_div2)).to.be.true;
150 expect(first_div1.sameNode(second_div)).to.be.false;
154 describe('traversing', function() {
155 it('reports element nodes', function() {
156 var c = canvas.fromXML('<section><div></div></section>'),
157 children = c.doc().children();
158 expect(children.length).to.equal(1);
159 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
161 c = canvas.fromXML('<section><div></div><div></div></section>'),
162 children = c.doc().children();
163 expect(children.length).to.equal(2);
164 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
165 expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
168 it('reports text nodes', function() {
169 var c = canvas.fromXML('<section>Alice</section>'),
170 children = c.doc().children();
171 expect(children.length).to.equal(1);
172 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
175 describe('accessing parents', function() {
176 it('returns DocumentNodeElement representing parent in wlxml document as DocumentNodeElement parent', function() {
177 var c = canvas.fromXML('<section><div></div></section>'),
178 div = c.doc().children()[0];
179 expect(div.parent().sameNode(c.doc())).to.be.true;
181 it('returns DocumentNodeElement representing parent in wlxml document as DocumentTextElement parent', function() {
182 var c = canvas.fromXML('<section>Alice</section>'),
183 text = c.doc().children()[0];
184 expect(text.parent().sameNode(c.doc())).to.be.true;
188 describe('free text handling', function() {
189 it('sees free text', function() {
190 var c = canvas.fromXML('<section>Alice <span>has</span> a cat</section>'),
191 children = c.doc().children();
192 expect(children.length).to.equal(3);
193 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
194 expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
195 expect(children[2]).to.be.instanceOf(documentElement.DocumentTextElement);
199 describe('white characters handling', function() {
200 it('says empty element node has no children', function() {
201 var c = canvas.fromXML('<section></section>');
202 expect(c.doc().children().length).to.equal(0);
204 it('says element node with one space has one DocumentTextElement', function() {
205 var c = canvas.fromXML('<section> </section>');
206 expect(c.doc().children().length).to.equal(1);
207 expect(c.doc().children()[0]).to.be.instanceOf(documentElement.DocumentTextElement);
208 expect(c.doc().children()[0].getText()).to.equal(' ');
210 it('ignores white space surrounding block elements', function() {
211 var c = canvas.fromXML('<section> <div></div> </section>');
212 var children = c.doc().children();
213 expect(children.length).to.equal(1);
214 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
216 it('ignores white space between block elements', function() {
217 var c = canvas.fromXML('<section><div></div> <div></div></section>');
218 var children = c.doc().children();
219 expect(children.length === 2);
220 [0,1].forEach(function(idx) {
221 expect(children[idx]).to.be.instanceOf(documentElement.DocumentNodeElement);
225 it('trims white space from the beginning and the end of the block elements', function() {
226 var c = canvas.fromXML('<section> Alice <span>has</span> a cat </section>');
227 expect(c.doc().children()[0].getText()).to.equal('Alice ');
228 expect(c.doc().children()[2].getText()).to.equal(' a cat');
231 it('normalizes string of white characters to one space at the inline element boundries', function() {
232 var c = canvas.fromXML('<span> Alice has a cat </span>');
233 expect(c.doc().children()[0].getText()).to.equal(' Alice has a cat ');
236 it('normalizes string of white characters to one space before inline element', function() {
237 var c = canvas.fromXML('<div>Alice has <span>a cat</span></div>');
238 expect(c.doc().children()[0].getText()).to.equal('Alice has ');
241 it('normalizes string of white characters to one space after inline element', function() {
242 var c = canvas.fromXML('<div>Alice has <span>a</span> cat</div>');
243 expect(c.doc().children()[2].getText()).to.equal(' cat');
248 describe('manipulation api', function() {
250 describe('Basic Element inserting', function() {
251 it('can put new NodeElement at the end', function() {
252 var c = canvas.fromXML('<section></section>'),
253 appended = c.doc().append({tag: 'header', klass: 'some.class'}),
254 children = c.doc().children();
256 expect(children.length).to.equal(1);
257 expect(children[0].sameNode(appended));
260 it('can put new TextElement at the end', function() {
261 var c = canvas.fromXML('<section></section>'),
262 appended = c.doc().append({text: 'Alice'}),
263 children = c.doc().children();
265 expect(children.length).to.equal(1);
266 expect(children[0].sameNode(appended));
267 expect(children[0].getText()).to.equal('Alice');
270 it('can put new NodeElement after another NodeElement', function() {
271 var c = canvas.fromXML('<section><div></div></section>'),
272 div = c.doc().children()[0],
273 added = div.after({tag: 'header', klass: 'some.class'}),
274 children = c.doc().children();
275 expect(children.length).to.equal(2);
276 expect(children[1].sameNode(added));
279 it('can put new Nodeelement before another element', function() {
280 var c = canvas.fromXML('<section><div></div></section>'),
281 div = c.doc().children()[0],
282 added = div.before({tag: 'header', klass: 'some.class'}),
283 children = c.doc().children();
284 expect(children.length).to.equal(2);
285 expect(children[0].sameNode(added));
288 it('can put new DocumentNodeElement after DocumentTextElement', function() {
289 var c = canvas.fromXML('<section>Alice</section>'),
290 text = c.doc().children()[0],
291 added = text.after({tag: 'p'}),
292 children = c.doc().children();
294 expect(children.length).to.equal(2);
295 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
296 expect(children[0].getText()).to.equal('Alice');
297 expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
298 expect(children[1].sameNode(added)).to.be.true;
300 it('can put new DocumentNodeElement before DocumentTextElement', function() {
301 var c = canvas.fromXML('<section>Alice</section>'),
302 text = c.doc().children()[0],
303 added = text.before({tag: 'p'}),
304 children = c.doc().children();
306 expect(children.length).to.equal(2);
307 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
308 expect(children[0].sameNode(added)).to.be.true;
309 expect(children[1]).to.be.instanceOf(documentElement.DocumentTextElement);
310 expect(children[1].getText()).to.equal('Alice');
314 describe('Splitting text', function() {
316 it('splits DocumentTextElement\'s parent into two DocumentNodeElements of the same type', function() {
317 var c = canvas.fromXML('<section><header>Some header</header></section>'),
319 text = section.children()[0].children()[0];
321 text.split({offset: 5});
322 expect(section.children().length).to.equal(2, 'section has two children');
324 var header1 = section.children()[0];
325 var header2 = section.children()[1];
327 expect(header1.wlxmlTag).to.equal('header', 'first section child represents wlxml header');
328 expect(header1.children().length).to.equal(1, 'first header has one text child');
329 expect(header1.children()[0].getText()).to.equal('Some ', 'first header has correct content');
330 expect(header2.wlxmlTag).to.equal('header', 'second section child represents wlxml header');
331 expect(header2.children().length).to.equal(1, 'second header has one text child');
332 expect(header2.children()[0].getText()).to.equal('header', 'second header has correct content');
335 it('keeps DocumentTextElement\'s parent\'s children elements intact', function() {
336 var c = canvas.fromXML('\
339 A <span>fancy</span> and <span>nice</span> header\
343 header = section.children()[0],
344 textAnd = header.children()[2];
346 textAnd.split({offset: 2});
348 var sectionChildren = section.children();
349 expect(sectionChildren.length).to.equal(2, 'Section has two children');
350 expect(sectionChildren[0].wlxmlTag).to.equal('header', 'First section element is a wlxml header');
351 expect(sectionChildren[1].wlxmlTag).to.equal('header', 'Second section element is a wlxml header');
353 var firstHeaderChildren = sectionChildren[0].children();
354 expect(firstHeaderChildren.length).to.equal(3, 'First header has three children');
355 expect(firstHeaderChildren[0].getText()).to.equal('A ', 'First header starts with a text');
356 expect(firstHeaderChildren[1].wlxmlTag).to.equal('span', 'First header has span in the middle');
357 expect(firstHeaderChildren[2].getText()).to.equal(' a', 'First header ends with text');
359 var secondHeaderChildren = sectionChildren[1].children();
360 expect(secondHeaderChildren.length).to.equal(3, 'Second header has three children');
361 expect(secondHeaderChildren[0].getText()).to.equal('nd ', 'Second header starts with text');
362 expect(secondHeaderChildren[1].wlxmlTag).to.equal('span', 'Second header has span in the middle');
363 expect(secondHeaderChildren[2].getText()).to.equal(' header', 'Second header ends with text');
367 describe('wrapping', function() {
368 it('wraps DocumentNodeElement', function() {
369 var c = canvas.fromXML('<section><div></div></section>'),
370 div = c.doc().children()[0];
372 var returned = div.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
373 parent = div.parent(),
374 parent2 = c.doc().children()[0];
376 expect(returned.sameNode(parent)).to.be.true;
377 expect(returned.sameNode(parent2)).to.be.true;
378 expect(returned.getWlxmlTag()).to.equal('header');
379 expect(returned.getWlxmlClass()).to.equal('some.class');
381 it('wraps DocumentTextElement', function() {
382 var c = canvas.fromXML('<section>Alice</section>'),
383 text = c.doc().children()[0];
385 var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
386 parent = text.parent(),
387 parent2 = c.doc().children()[0];
389 expect(returned.sameNode(parent)).to.be.true;
390 expect(returned.sameNode(parent2)).to.be.true;
391 expect(returned.getWlxmlTag()).to.equal('header');
392 expect(returned.getWlxmlClass()).to.equal('some.class');
395 describe('wrapping part of DocumentTextElement', function() {
396 [{start: 5, end: 12}, {start: 12, end: 5}].forEach(function(offsets) {
397 it('wraps in the middle ' + offsets.start + '/' + offsets.end, function() {
398 var c = canvas.fromXML('<section>Alice has a cat</section>'),
399 text = c.doc().children()[0];
401 var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class', start: offsets.start, end: offsets.end}),
402 children = c.doc().children();
404 expect(children.length).to.equal(3);
406 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
407 expect(children[0].getText()).to.equal('Alice');
409 expect(children[1].sameNode(returned)).to.be.true;
410 expect(returned.getWlxmlTag()).to.equal('header');
411 expect(returned.getWlxmlClass()).to.equal('some.class');
412 expect(children[1].children().length).to.equal(1);
413 expect(children[1].children()[0].getText()).to.equal(' has a ');
415 expect(children[2]).to.be.instanceOf(documentElement.DocumentTextElement);
416 expect(children[2].getText()).to.equal('cat');
420 it('wraps whole text inside DocumentTextElement if offsets span entire content', function() {
421 var c = canvas.fromXML('<section>Alice has a cat</section>'),
422 text = c.doc().children()[0];
424 var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class', start: 0, end: 15}),
425 children = c.doc().children();
427 expect(children.length).to.equal(1);
428 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
429 expect(children[0].children()[0].getText()).to.equal('Alice has a cat');
433 it('wraps text spanning multiple sibling DocumentTextNodes', function() {
434 var c = canvas.fromXML('<section>Alice has a <span>small</span> cat</section>'),
436 wrapper = c.wrapText({
438 _with: {tag: 'span', klass: 'some.class'},
444 expect(section.children().length).to.equal(2);
445 expect(section.children()[0]).to.be.instanceOf(documentElement.DocumentTextElement);
446 expect(section.children()[0].getText()).to.equal('Alice ');
448 var wrapper2 = section.children()[1];
449 expect(wrapper2.sameNode(wrapper)).to.be.true;
451 var wrapperChildren = wrapper.children();
452 expect(wrapperChildren.length).to.equal(3);
453 expect(wrapperChildren[0].getText()).to.equal('has a ');
455 expect(wrapperChildren[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
456 expect(wrapperChildren[1].children().length).to.equal(1);
457 expect(wrapperChildren[1].children()[0].getText()).to.equal('small');
459 expect(wrapperChildren[2].getText()).to.equal(' cat');
463 describe('unwrapping', function() {
464 it('unwraps DocumentTextElement from its parent DocumentNodeElement if it\'s its only child', function() {
465 var c = canvas.fromXML('<section><div>Alice has a cat</div></section>'),
467 text = section.children()[0].children()[0];
471 expect(section.children().length).to.equal(1);
472 expect(section.children()[0].getText()).to.equal('Alice has a cat');
477 describe('Lists api', function() {
478 describe('creating lists', function() {
479 it('allows creation of a list from existing sibling DocumentElements', function() {
480 var c = canvas.fromXML('\
488 textHas = section.children()[1],
489 divA = section.children()[2]
491 c.list.create({element1: textHas, element2: divA});
493 expect(section.children().length).to.equal(3, 'section has three child elements');
495 var child1 = section.children()[0],
496 list = section.children()[1],
497 child3 = section.children()[2];
499 expect(child1.getText()).to.equal('Alice');
500 expect(list.is('list')).to.equal(true, 'second child is a list');
501 expect(list.children().length).to.equal(2, 'list contains two elements');
502 list.children().forEach(function(child) {
503 expect(child.getWlxmlClass()).to.equal('item', 'list childs have wlxml class of item');
505 expect(child3.children()[0].getText()).to.equal('cat');
508 it('allows creating nested list from existing sibling list items', function() {
509 var c = canvas.fromXML('\
511 <div class="list-items">\
512 <div class="item">A</div>\
513 <div class="item">B</div>\
514 <div class="item">C</div>\
515 <div class="item">D</div>\
518 outerList = c.doc().children()[0],
519 itemB = outerList.children()[1],
520 itemC = outerList.children()[2];
523 c.list.create({element1: itemB, element2: itemC});
525 var outerListItems = outerList.children(),
526 innerList = outerListItems[1].children()[0],
527 innerListItems = innerList.children();
529 expect(outerListItems.length).to.equal(3, 'outer list has three items');
530 expect(outerListItems[0].children()[0].getText()).to.equal('A', 'first outer item ok');
531 expect(outerListItems[1].getWlxmlClass()).to.equal('item', 'inner list is wrapped by item element');
533 expect(innerList.is('list')).to.equal(true, 'inner list created');
534 expect(innerListItems.length).to.equal(2, 'inner list has two items');
535 expect(innerListItems[0].children()[0].getText()).to.equal('B', 'first inner item ok');
536 expect(innerListItems[1].children()[0].getText()).to.equal('C', 'second inner item ok');
538 expect(outerListItems[2].children()[0].getText()).to.equal('D', 'last outer item ok');
544 describe('extracting list items', function() {
545 it('creates two lists with extracted items in the middle if extracting from the middle of the list', function() {
546 var c = canvas.fromXML('\
548 <div class="list.items">\
549 <div class="item">0</div>\
550 <div class="item">1</div>\
551 <div class="item">2</div>\
552 <div class="item">3</div>\
555 list = c.doc().children()[0],
556 item1 = list.children()[1],
557 item2 = list.children()[2];
559 c.list.extractItems({element1: item1, element2: item2});
561 var section = c.doc(),
562 list1 = section.children()[0],
563 oldItem1 = section.children()[1],
564 oldItem2 = section.children()[2],
565 list2 = section.children()[3];
567 expect(section.children().length).to.equal(4, 'section contains four children');
569 expect(list1.is('list')).to.equal(true, 'first section child is a list');
570 expect(list1.children().length).to.equal(1, 'first list has one child');
571 expect(list1.children()[0].children()[0].getText()).to.equal('0', 'first item of the first list is a first item of the original list');
573 expect(oldItem1.children()[0].getText()).to.equal('1', 'first item got extracted');
574 expect(oldItem1.getWlxmlClass() === undefined).to.equal(true, 'first extracted element has no wlxml class');
576 expect(oldItem2.children()[0].getText()).to.equal('2', 'second item got extracted');
577 expect(oldItem2.getWlxmlClass() === undefined).to.equal(true, 'second extracted element has no wlxml class');
579 expect(list2.is('list')).to.equal(true, 'last section child is a list');
580 expect(list2.children().length).to.equal(1, 'second list has one child');
581 expect(list2.children()[0].children()[0].getText()).to.equal('3', 'first item of the second list is a last item of the original list');
584 it('puts extracted items above the list if starting item is the first one', function() {
585 var c = canvas.fromXML('\
587 <div class="list.items">\
588 <div class="item">0</div>\
589 <div class="item">1</div>\
590 <div class="item">2</div>\
593 list = c.doc().children()[0],
594 item1 = list.children()[0],
595 item2 = list.children()[1],
596 item3 = list.children()[2];
598 c.list.extractItems({element1: item1, element2: item2});
600 var section = c.doc(),
601 oldItem1 = section.children()[0],
602 oldItem2 = section.children()[1],
603 newList = section.children()[2];
605 expect(section.children().length).to.equal(3, 'section has three children');
606 expect(oldItem1.children()[0].getText()).to.equal('0', 'first item extracted');
607 expect(oldItem2.children()[0].getText()).to.equal('1', 'second item extracted');
608 expect(newList.is('list')).to.equal(true, 'list lies below extracted item');
609 expect(newList.children().length).to.equal(1, 'list has now one child');
612 it('puts extracted items below the list if ending item is the last one', function() {
613 var c = canvas.fromXML('\
615 <div class="list.items">\
616 <div class="item">0</div>\
617 <div class="item">1</div>\
618 <div class="item">2</div>\
621 list = c.doc().children()[0],
622 item1 = list.children()[0],
623 item2 = list.children()[1],
624 item3 = list.children()[2];
626 c.list.extractItems({element1: item2, element2: item3});
628 var section = c.doc(),
629 oldItem1 = section.children()[1],
630 oldItem2 = section.children()[2],
631 newList = section.children()[0];
633 expect(section.children().length).to.equal(3, 'section has three children');
634 expect(oldItem1.children()[0].getText()).to.equal('1', 'first item extracted');
635 expect(oldItem2.children()[0].getText()).to.equal('2', 'second item extracted');
636 expect(newList.is('list')).to.equal(true, 'list lies above extracted item');
637 expect(newList.children().length).to.equal(1, 'list has now one child');
640 it('removes list if all its items are extracted', function() {
641 var c = canvas.fromXML('\
643 <div class="list.items">\
644 <div class="item">some item</div>\
645 <div class="item">some item 2</div>\
648 list = c.doc().children()[0],
649 item1 = list.children()[0],
650 item2 = list.children()[1];
652 c.list.extractItems({element1: item1, element2: item2});
654 var section = c.doc(),
655 list1 = section.children()[0],
656 oldItem1 = section.children()[0],
657 oldItem2 = section.children()[1];
659 expect(section.children().length).to.equal(2, 'section contains two children');
660 expect(oldItem1.children()[0].getText()).to.equal('some item');
661 expect(oldItem2.children()[0].getText()).to.equal('some item 2');
664 it('creates two lists with extracted items in the middle if extracting from the middle of the list - nested case' , function() {
665 var c = canvas.fromXML('\
667 <div class="list.items">\
668 <div class="item">0</div>\
670 <div class="list.items">\
671 <div class="item">1.1</div>\
672 <div class="item">1.2</div>\
673 <div class="item">1.3</div>\
676 <div class="item">2</div>\
679 list = c.doc().children()[0],
680 nestedList = list.children()[1].children()[0],
681 nestedListItem = nestedList.children()[1];
683 c.list.extractItems({element1: nestedListItem, element2: nestedListItem});
685 var section = c.doc(),
686 list = section.children()[0],
687 item1 = list.children()[0],
688 item2 = list.children()[1], //
689 item3 = list.children()[2],
690 item4 = list.children()[3], //
691 item5 = list.children()[4],
692 nestedList1 = item2.children()[0],
693 nestedList2 = item4.children()[0];
695 expect(list.children().length).to.equal(5, 'top list has five items');
697 expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
699 expect(item2.getWlxmlClass()).to.equal('item', 'first nested list is still wrapped in item element');
700 expect(nestedList1.children().length).to.equal(1, 'first nested list is left with one child');
701 expect(nestedList1.children()[0].children()[0].getText()).to.equal('1.1', 'first nested list item left alone');
703 expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
705 expect(item4.getWlxmlClass()).to.equal('item', 'second nested list is still wrapped in item element');
706 expect(nestedList2.children().length).to.equal(1, 'second nested list is left with one child');
707 expect(nestedList2.children()[0].children()[0].getText()).to.equal('1.3', 'second nested list item left alone');
709 expect(item5.children()[0].getText()).to.equal('2', 'last item ok');
712 it('puts extracted items below the list if ending item is the last one - nested case' , function() {
713 var c = canvas.fromXML('\
715 <div class="list.items">\
716 <div class="item">0</div>\
718 <div class="list.items">\
719 <div class="item">1.1</div>\
720 <div class="item">1.2</div>\
721 <div class="item">1.3</div>\
724 <div class="item">2</div>\
727 list = c.doc().children()[0],
728 nestedList = list.children()[1].children()[0],
729 nestedListItem1 = nestedList.children()[1],
730 nestedListItem2 = nestedList.children()[2];
732 c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
734 var section = c.doc(),
735 list = section.children()[0],
736 item1 = list.children()[0],
737 item2 = list.children()[1],
738 item3 = list.children()[2],
739 item4 = list.children()[3],
740 item5 = list.children()[4];
741 nestedList = item2.children()[0];
743 expect(list.children().length).to.equal(5, 'top list has five items');
744 expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
745 expect(item2.getWlxmlClass()).to.equal('item', 'nested list is still wrapped in item element');
746 expect(nestedList.children().length).to.equal(1, 'nested list is left with one child');
747 expect(nestedList.children()[0].children()[0].getText()).to.equal('1.1', 'nested list item left alone');
748 expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
749 expect(item4.children()[0].getText()).to.equal('1.3', 'fourth item ok');
750 expect(item5.children()[0].getText()).to.equal('2', 'fifth item ok');
753 it('puts extracted items above the list if starting item is the first one - nested case' , function() {
754 var c = canvas.fromXML('\
756 <div class="list.items">\
757 <div class="item">0</div>\
759 <div class="list.items">\
760 <div class="item">1.1</div>\
761 <div class="item">1.2</div>\
762 <div class="item">1.3</div>\
765 <div class="item">2</div>\
768 list = c.doc().children()[0],
769 nestedList = list.children()[1].children()[0],
770 nestedListItem1 = nestedList.children()[0],
771 nestedListItem2 = nestedList.children()[1];
773 c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
775 var section = c.doc(),
776 list = section.children()[0],
777 item1 = list.children()[0],
778 item2 = list.children()[1],
779 item3 = list.children()[2],
780 item4 = list.children()[3],
781 item5 = list.children()[4];
782 nestedList = item4.children()[0];
784 expect(list.children().length).to.equal(5, 'top list has five items');
785 expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
786 expect(item2.children()[0].getText()).to.equal('1.1', 'second item ok');
787 expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
789 expect(item4.getWlxmlClass()).to.equal('item', 'nested list is still wrapped in item element');
790 expect(nestedList.children().length).to.equal(1, 'nested list is left with one child');
791 expect(nestedList.children()[0].children()[0].getText()).to.equal('1.3', 'nested list item left alone');
792 expect(item5.children()[0].getText()).to.equal('2', 'fifth item ok');
795 it('removes list if all its items are extracted - nested case', function() {
796 var c = canvas.fromXML('\
798 <div class="list.items">\
799 <div class="item">0</div>\
801 <div class="list.items">\
802 <div class="item">1.1</div>\
803 <div class="item">1.2</div>\
806 <div class="item">2</div>\
809 list = c.doc().children()[0],
810 nestedList = list.children()[1].children()[0],
811 nestedListItem1 = nestedList.children()[0],
812 nestedListItem2 = nestedList.children()[1];
814 c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
816 var section = c.doc(),
817 list = section.children()[0],
818 item1 = list.children()[0],
819 item2 = list.children()[1],
820 item3 = list.children()[2],
821 item4 = list.children()[3];
823 expect(list.children().length).to.equal(4, 'top list has four items');
824 expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
825 expect(item2.children()[0].getText()).to.equal('1.1', 'second item ok');
826 expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
827 expect(item4.children()[0].getText()).to.equal('2', 'fourth item ok');
830 it('extracts items out of outer most list when merge flag is set to false', function() {
831 var c = canvas.fromXML('\
833 <div class="list.items">\
834 <div class="item">0</div>\
836 <div class="list.items">\
837 <div class="item">1.1</div>\
838 <div class="item">1.2</div>\
841 <div class="item">2</div>\
845 list = section.children()[0],
846 nestedList = list.children()[1].children()[0],
847 nestedListItem = nestedList.children()[0];
849 var test = c.list.extractItems({element1: nestedListItem, element2: nestedListItem, merge: false});
851 expect(test).to.equal(true, 'extraction status ok');
853 var sectionChildren = section.children(),
854 extractedItem = sectionChildren[1];
856 expect(sectionChildren.length).to.equal(3, 'section has three children');
857 expect(sectionChildren[0].is('list')).to.equal(true, 'first child is a list');
859 expect(extractedItem.getWlxmlTag()).to.equal('div', 'extracted item is a wlxml div');
860 expect(extractedItem.getWlxmlClass()).to.equal(undefined, 'extracted item has no wlxml class');
861 expect(extractedItem.children()[0].getText()).to.equal('1.1', 'extracted item ok');
862 expect(sectionChildren[2].is('list')).to.equal(true, 'second child is a list');
869 describe('Cursor', function() {
873 beforeEach(function() {
874 getSelection = sinon.stub(window, 'getSelection');
877 afterEach(function() {
878 getSelection.restore();
881 it('returns position when browser selection collapsed', function() {
882 var c = canvas.fromXML('<section>Alice has a cat</section>'),
884 text = dom.contents(0);
886 expect(text.text()).to.equal('Alice has a cat');
888 getSelection.returns({
895 var cursor = c.getCursor(),
896 position = cursor.getPosition();
898 expect(cursor.isSelecting()).to.equal(false, 'cursor is not selecting anything');
899 expect(position.element.getText()).to.equal('Alice has a cat');
900 expect(position.offset).to.equal(5);
903 it('returns boundries of selection when browser selection not collapsed', function() {
904 var c = canvas.fromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
907 alice: dom.contents()[0],
908 has: $(dom.contents()[1]).contents()[0],
909 cat: dom.contents()[4]
911 cursor = c.getCursor(),
912 aliceElement = c.getDocumentElement(text.alice),
913 catElement = c.getDocumentElement(text.cat);
917 {focus: text.alice, focusOffset: 1, anchor: text.cat, anchorOffset: 2, selectionAnchor: catElement},
918 {focus: text.cat, focusOffset: 2, anchor: text.alice, anchorOffset: 1, selectionAnchor: aliceElement}
919 ].forEach(function(s, idx) {
920 getSelection.returns({isColapsed: false, anchorNode: s.anchor, anchorOffset: s.anchorOffset, focusNode: s.focus, focusOffset: s.focusOffset});
922 var selectionStart = cursor.getSelectionStart(),
923 selectionEnd = cursor.getSelectionEnd(),
924 selectionAnchor = cursor.getSelectionAnchor();
926 expect(cursor.isSelecting()).to.equal(true, 'cursor is selecting');
927 expect(selectionStart.element.sameNode(aliceElement)).to.equal(true, '"Alice" is the start of the selection ' + idx);
928 expect(selectionStart.offset).to.equal(1, '"Alice" offset ok' + idx);
929 expect(selectionEnd.element.sameNode(catElement)).to.equal(true, '"Cat" is the start of the selection ' + idx);
930 expect(selectionEnd.offset).to.equal(2, '"Cat" offset ok' + idx);
931 expect(selectionAnchor.element.sameNode(s.selectionAnchor)).to.equal(true, 'anchor ok');
932 expect(selectionAnchor.offset).to.equal(s.anchorOffset, 'anchor offset ok');
936 it('recognizes when browser selection boundries lies in sibling DocumentTextElements', function() {
937 var c = canvas.fromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
940 alice: dom.contents()[0],
941 has: $(dom.contents()[1]).contents()[0],
942 a: dom.contents()[2],
943 big: $(dom.contents()[3]).contents()[0],
944 cat: dom.contents()[4]
946 cursor = c.getCursor();
948 expect($(text.alice).text()).to.equal('Alice ');
949 expect($(text.has).text()).to.equal('has');
950 expect($(text.a).text()).to.equal(' a ');
951 expect($(text.big).text()).to.equal('big');
952 expect($(text.cat).text()).to.equal(' cat');
954 getSelection.returns({anchorNode: text.alice, focusNode: text.a});
955 expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "a" are children');
957 getSelection.returns({anchorNode: text.alice, focusNode: text.cat});
958 expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "cat" are children');
960 getSelection.returns({anchorNode: text.alice, focusNode: text.has});
961 expect(cursor.isSelectingSiblings()).to.equal(false, '"Alice" and "has" are not children');
963 getSelection.returns({anchorNode: text.has, focusNode: text.big});
964 expect(cursor.isSelectingSiblings()).to.equal(false, '"has" and "big" are not children');