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 + '<div wlxml-text>This is some text without its own wrapping tag.</div>'
31 + '<div wlxml-tag="div" wlxml-class="p-subclass">'
32 + '<div wlxml-text>This is a paragraph.</div>'
34 + '<div wlxml-tag="div">'
35 + '<div wlxml-text>This is text in a div </div>'
36 + '<div wlxml-tag="span">'
37 + '<div wlxml-text>with some inline text</div>'
39 + '<div wlxml-text>.</div>'
41 + '<div wlxml-text>This is some text without its own wrapping tag.</div>'
43 expect(c.doc().dom()[0].isEqualNode($(expected)[0])).to.be.true;
47 describe('Internal HTML representation of a DocumentNodeElement', function() {
48 it('is always a div tag', function() {
49 ['section', 'header', 'span', 'aside', 'figure'].forEach(function(tagName) {
50 var dom = canvas.fromXML('<' + tagName + '></' + tagName + '>').doc().dom();
51 expect(dom.prop('tagName')).to.equal('DIV', tagName + ' is represented as div');
54 it('has wlxml tag put into wlxml-tag attribute', function() {
55 var dom = canvas.fromXML('<section></section>').doc().dom();
56 expect(dom.attr('wlxml-tag')).to.equal('section');
58 it('has wlxml class put into wlxml-class, dots replaced with dashes', function() {
59 var dom = canvas.fromXML('<section class="some.class"></section>').doc().dom();
60 expect(dom.attr('wlxml-class')).to.equal('some-class');
64 describe('Internal HTML representation of a DocumentTextElement', function() {
65 it('is text node wrapped in a div with wlxml-text attribute set', function() {
66 var dom = canvas.fromXML('<section>Alice</section>').doc().children()[0].dom();
67 expect(dom.prop('tagName')).to.equal('DIV');
68 expect(dom.attr('wlxml-text')).to.equal('');
69 expect(dom.contents().length).to.equal(1);
70 expect(dom.contents()[0].nodeType).to.equal(Node.TEXT_NODE);
71 expect($(dom.contents()[0]).text()).to.equal('Alice');
75 describe('basic properties', function() {
76 it('renders empty document when canvas created from empty XML', function() {
77 var c = canvas.fromXML('');
78 expect(c.doc()).to.equal(null);
81 it('gives access to its document root node', function() {
82 var c = canvas.fromXML('<section></section>');
83 expect(c.doc().getWlxmlTag()).to.equal('section');
86 describe('root element', function() {
87 it('has no parent', function() {
88 var c = canvas.fromXML('<section></section>');
89 expect(c.doc().parent()).to.be.null;
93 describe('DocumentTextElement', function() {
94 it('can have its content set', function() {
95 var c = canvas.fromXML('<section>Alice</section>'),
97 text = root.children()[0];
99 text.setText('a cat');
100 expect(root.children()[0].getText()).to.equal('a cat');
104 describe('DocumentNodeElement', function() {
105 it('knows index of its child', function() {
106 var c = canvas.fromXML('<section><div></div><header></header><span></span></section>'),
108 child = root.children()[1];
109 expect(root.childIndex(child)).to.equal(1);
112 it('knows WLXML tag it renders', function(){
113 var c = canvas.fromXML('<section></section>'),
115 expect(section.getWlxmlTag()).to.equal('section', 'initial tag is section');
116 section.setWlxmlTag('header');
117 expect(section.getWlxmlTag()).to.equal('header', 'tag is changed to header');
120 it('knows WLXML class of a WLXML tag it renders', function(){
121 var c = canvas.fromXML('<section class="some.class"></section>'),
123 expect(section.getWlxmlClass()).to.equal('some.class');
124 section.setWlxmlClass('some.other.class');
125 expect(section.getWlxmlClass()).to.equal('some.other.class');
126 section.setWlxmlClass(null);
127 expect(section.getWlxmlClass()).to.be.undefined;
131 it('returns DocumentNodeElement instance from HTMLElement', function() {
132 var c = canvas.fromXML('<section></section>'),
133 htmlElement = c.doc().dom().get(0),
134 element = c.getDocumentElement(htmlElement);
135 expect(element).to.be.instanceOf(documentElement.DocumentNodeElement);
136 expect(element.sameNode(c.doc()));
139 it('returns DocumentTextElement instance from Text Node', function() {
140 var c = canvas.fromXML('<section>Alice</section>'),
141 aliceElement = c.doc().children()[0],
142 textNode = aliceElement.dom().contents()[0],
143 element = c.getDocumentElement(textNode);
145 expect(textNode.nodeType).to.equal(Node.TEXT_NODE, 'text node selected');
146 expect($(textNode).text()).to.equal('Alice');
148 expect(element).to.be.instanceOf(documentElement.DocumentTextElement);
149 expect(element.sameNode(c.doc().children()[0]));
155 describe('document representation api', function() {
156 describe('document root element', function() {
157 var c = canvas.fromXML('<section></section>');
158 it('exists', function() {
159 expect(c.doc()).to.be.instanceOf(documentElement.DocumentElement);
161 it('is of type DocumentNodeElement', function() {
162 expect(c.doc()).to.be.instanceOf(documentElement.DocumentNodeElement);
166 describe('DocumentElements comparison', function() {
167 it('reports dwo DocumentElements to be the same when they represent the same wlxml document element', function() {
168 var c = canvas.fromXML('<section><div></div><div></div></section>'),
169 first_div1 = c.doc().children()[0],
170 first_div2 = c.doc().children()[0],
171 second_div = c.doc().children()[1];
172 expect(first_div1.sameNode(first_div1)).to.be.true;
173 expect(first_div1.sameNode(first_div2)).to.be.true;
174 expect(first_div1.sameNode(second_div)).to.be.false;
178 describe('traversing', function() {
179 it('reports element nodes', function() {
180 var c = canvas.fromXML('<section><div></div></section>'),
181 children = c.doc().children();
182 expect(children.length).to.equal(1);
183 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
185 c = canvas.fromXML('<section><div></div><div></div></section>'),
186 children = c.doc().children();
187 expect(children.length).to.equal(2);
188 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
189 expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
192 it('reports text nodes', function() {
193 var c = canvas.fromXML('<section>Alice</section>'),
194 children = c.doc().children();
195 expect(children.length).to.equal(1);
196 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
199 describe('accessing parents', function() {
200 it('returns DocumentNodeElement representing parent in wlxml document as DocumentNodeElement parent', function() {
201 var c = canvas.fromXML('<section><div></div></section>'),
202 div = c.doc().children()[0];
203 expect(div.parent().sameNode(c.doc())).to.be.true;
205 it('returns DocumentNodeElement representing parent in wlxml document as DocumentTextElement parent', function() {
206 var c = canvas.fromXML('<section>Alice</section>'),
207 text = c.doc().children()[0];
208 expect(text.parent().sameNode(c.doc())).to.be.true;
212 describe('free text handling', function() {
213 it('sees free text', function() {
214 var c = canvas.fromXML('<section>Alice <span>has</span> a cat</section>'),
215 children = c.doc().children();
216 expect(children.length).to.equal(3);
217 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
218 expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
219 expect(children[2]).to.be.instanceOf(documentElement.DocumentTextElement);
223 describe('white characters handling', function() {
224 it('says empty element node has no children', function() {
225 var c = canvas.fromXML('<section></section>');
226 expect(c.doc().children().length).to.equal(0);
228 it('says element node with one space has one DocumentTextElement', function() {
229 var c = canvas.fromXML('<section> </section>');
230 expect(c.doc().children().length).to.equal(1);
231 expect(c.doc().children()[0]).to.be.instanceOf(documentElement.DocumentTextElement);
232 expect(c.doc().children()[0].getText()).to.equal(' ');
234 it('ignores white space surrounding block elements', function() {
235 var c = canvas.fromXML('<section> <div></div> </section>');
236 var children = c.doc().children();
237 expect(children.length).to.equal(1);
238 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
240 it('ignores white space between block elements', function() {
241 var c = canvas.fromXML('<section><div></div> <div></div></section>');
242 var children = c.doc().children();
243 expect(children.length === 2);
244 [0,1].forEach(function(idx) {
245 expect(children[idx]).to.be.instanceOf(documentElement.DocumentNodeElement);
249 it('trims white space from the beginning and the end of the block elements', function() {
250 var c = canvas.fromXML('<section> Alice <span>has</span> a cat </section>');
251 expect(c.doc().children()[0].getText()).to.equal('Alice ');
252 expect(c.doc().children()[2].getText()).to.equal(' a cat');
255 it('normalizes string of white characters to one space at the inline element boundries', function() {
256 var c = canvas.fromXML('<span> Alice has a cat </span>');
257 expect(c.doc().children()[0].getText()).to.equal(' Alice has a cat ');
260 it('normalizes string of white characters to one space before inline element', function() {
261 var c = canvas.fromXML('<div>Alice has <span>a cat</span></div>');
262 expect(c.doc().children()[0].getText()).to.equal('Alice has ');
265 it('normalizes string of white characters to one space after inline element', function() {
266 var c = canvas.fromXML('<div>Alice has <span>a</span> cat</div>');
267 expect(c.doc().children()[2].getText()).to.equal(' cat');
272 describe('manipulation api', function() {
274 describe('Basic Element inserting', function() {
275 it('can put new NodeElement at the end', function() {
276 var c = canvas.fromXML('<section></section>'),
277 appended = c.doc().append({tag: 'header', klass: 'some.class'}),
278 children = c.doc().children();
280 expect(children.length).to.equal(1);
281 expect(children[0].sameNode(appended));
284 it('can put new TextElement at the end', function() {
285 var c = canvas.fromXML('<section></section>'),
286 appended = c.doc().append({text: 'Alice'}),
287 children = c.doc().children();
289 expect(children.length).to.equal(1);
290 expect(children[0].sameNode(appended));
291 expect(children[0].getText()).to.equal('Alice');
294 it('can put new NodeElement after another NodeElement', function() {
295 var c = canvas.fromXML('<section><div></div></section>'),
296 div = c.doc().children()[0],
297 added = div.after({tag: 'header', klass: 'some.class'}),
298 children = c.doc().children();
299 expect(children.length).to.equal(2);
300 expect(children[1].sameNode(added));
303 it('can put new Nodeelement before another element', function() {
304 var c = canvas.fromXML('<section><div></div></section>'),
305 div = c.doc().children()[0],
306 added = div.before({tag: 'header', klass: 'some.class'}),
307 children = c.doc().children();
308 expect(children.length).to.equal(2);
309 expect(children[0].sameNode(added));
312 it('can put new DocumentNodeElement after DocumentTextElement', function() {
313 var c = canvas.fromXML('<section>Alice</section>'),
314 text = c.doc().children()[0],
315 added = text.after({tag: 'p'}),
316 children = c.doc().children();
318 expect(children.length).to.equal(2);
319 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
320 expect(children[0].getText()).to.equal('Alice');
321 expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
322 expect(children[1].sameNode(added)).to.be.true;
324 it('can put new DocumentNodeElement before DocumentTextElement', function() {
325 var c = canvas.fromXML('<section>Alice</section>'),
326 text = c.doc().children()[0],
327 added = text.before({tag: 'p'}),
328 children = c.doc().children();
330 expect(children.length).to.equal(2);
331 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
332 expect(children[0].sameNode(added)).to.be.true;
333 expect(children[1]).to.be.instanceOf(documentElement.DocumentTextElement);
334 expect(children[1].getText()).to.equal('Alice');
338 describe('Splitting text', function() {
340 it('splits DocumentTextElement\'s parent into two DocumentNodeElements of the same type', function() {
341 var c = canvas.fromXML('<section><header>Some header</header></section>'),
343 text = section.children()[0].children()[0];
345 text.split({offset: 5});
346 expect(section.children().length).to.equal(2, 'section has two children');
348 var header1 = section.children()[0];
349 var header2 = section.children()[1];
351 expect(header1.getWlxmlTag()).to.equal('header', 'first section child represents wlxml header');
352 expect(header1.children().length).to.equal(1, 'first header has one text child');
353 expect(header1.children()[0].getText()).to.equal('Some ', 'first header has correct content');
354 expect(header2.getWlxmlTag()).to.equal('header', 'second section child represents wlxml header');
355 expect(header2.children().length).to.equal(1, 'second header has one text child');
356 expect(header2.children()[0].getText()).to.equal('header', 'second header has correct content');
359 it('leaves empty copy of DocumentNodeElement if splitting at the very beginning', function() {
360 var c = canvas.fromXML('<section><header>Some header</header></section>'),
362 text = section.children()[0].children()[0];
364 text.split({offset: 0});
366 var header1 = section.children()[0];
367 var header2 = section.children()[1];
369 expect(header1.children().length).to.equal(0);
370 expect(header2.children()[0].getText()).to.equal('Some header');
373 it('leaves empty copy of DocumentNodeElement if splitting at the very end', function() {
374 var c = canvas.fromXML('<section><header>Some header</header></section>'),
376 text = section.children()[0].children()[0];
378 text.split({offset: 11});
380 var header1 = section.children()[0];
381 var header2 = section.children()[1];
383 expect(header1.children()[0].getText()).to.equal('Some header');
384 expect(header2.children().length).to.equal(0);
387 it('keeps DocumentTextElement\'s parent\'s children elements intact', function() {
388 var c = canvas.fromXML('\
391 A <span>fancy</span> and <span>nice</span> header\
395 header = section.children()[0],
396 textAnd = header.children()[2];
398 textAnd.split({offset: 2});
400 var sectionChildren = section.children();
401 expect(sectionChildren.length).to.equal(2, 'Section has two children');
402 expect(sectionChildren[0].getWlxmlTag()).to.equal('header', 'First section element is a wlxml header');
403 expect(sectionChildren[1].getWlxmlTag()).to.equal('header', 'Second section element is a wlxml header');
405 var firstHeaderChildren = sectionChildren[0].children();
406 expect(firstHeaderChildren.length).to.equal(3, 'First header has three children');
407 expect(firstHeaderChildren[0].getText()).to.equal('A ', 'First header starts with a text');
408 expect(firstHeaderChildren[1].getWlxmlTag()).to.equal('span', 'First header has span in the middle');
409 expect(firstHeaderChildren[2].getText()).to.equal(' a', 'First header ends with text');
411 var secondHeaderChildren = sectionChildren[1].children();
412 expect(secondHeaderChildren.length).to.equal(3, 'Second header has three children');
413 expect(secondHeaderChildren[0].getText()).to.equal('nd ', 'Second header starts with text');
414 expect(secondHeaderChildren[1].getWlxmlTag()).to.equal('span', 'Second header has span in the middle');
415 expect(secondHeaderChildren[2].getText()).to.equal(' header', 'Second header ends with text');
419 describe('wrapping', function() {
420 it('wraps DocumentNodeElement', function() {
421 var c = canvas.fromXML('<section><div></div></section>'),
422 div = c.doc().children()[0];
424 var returned = div.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
425 parent = div.parent(),
426 parent2 = c.doc().children()[0];
428 expect(returned.sameNode(parent)).to.be.true;
429 expect(returned.sameNode(parent2)).to.be.true;
430 expect(returned.getWlxmlTag()).to.equal('header');
431 expect(returned.getWlxmlClass()).to.equal('some.class');
433 it('wraps DocumentTextElement', function() {
434 var c = canvas.fromXML('<section>Alice</section>'),
435 text = c.doc().children()[0];
437 var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
438 parent = text.parent(),
439 parent2 = c.doc().children()[0];
441 expect(returned.sameNode(parent)).to.be.true;
442 expect(returned.sameNode(parent2)).to.be.true;
443 expect(returned.getWlxmlTag()).to.equal('header');
444 expect(returned.getWlxmlClass()).to.equal('some.class');
447 describe('wrapping part of DocumentTextElement', function() {
448 [{start: 5, end: 12}, {start: 12, end: 5}].forEach(function(offsets) {
449 it('wraps in the middle ' + offsets.start + '/' + offsets.end, function() {
450 var c = canvas.fromXML('<section>Alice has a cat</section>'),
451 text = c.doc().children()[0];
453 var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class', start: offsets.start, end: offsets.end}),
454 children = c.doc().children();
456 expect(children.length).to.equal(3);
458 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
459 expect(children[0].getText()).to.equal('Alice');
461 expect(children[1].sameNode(returned)).to.be.true;
462 expect(returned.getWlxmlTag()).to.equal('header');
463 expect(returned.getWlxmlClass()).to.equal('some.class');
464 expect(children[1].children().length).to.equal(1);
465 expect(children[1].children()[0].getText()).to.equal(' has a ');
467 expect(children[2]).to.be.instanceOf(documentElement.DocumentTextElement);
468 expect(children[2].getText()).to.equal('cat');
472 it('wraps whole text inside DocumentTextElement if offsets span entire content', function() {
473 var c = canvas.fromXML('<section>Alice has a cat</section>'),
474 text = c.doc().children()[0];
476 var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class', start: 0, end: 15}),
477 children = c.doc().children();
479 expect(children.length).to.equal(1);
480 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
481 expect(children[0].children()[0].getText()).to.equal('Alice has a cat');
485 it('wraps text spanning multiple sibling DocumentTextNodes', function() {
486 var c = canvas.fromXML('<section>Alice has a <span>small</span> cat</section>'),
488 wrapper = c.wrapText({
490 _with: {tag: 'span', klass: 'some.class'},
496 expect(section.children().length).to.equal(2);
497 expect(section.children()[0]).to.be.instanceOf(documentElement.DocumentTextElement);
498 expect(section.children()[0].getText()).to.equal('Alice ');
500 var wrapper2 = section.children()[1];
501 expect(wrapper2.sameNode(wrapper)).to.be.true;
503 var wrapperChildren = wrapper.children();
504 expect(wrapperChildren.length).to.equal(3);
505 expect(wrapperChildren[0].getText()).to.equal('has a ');
507 expect(wrapperChildren[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
508 expect(wrapperChildren[1].children().length).to.equal(1);
509 expect(wrapperChildren[1].children()[0].getText()).to.equal('small');
511 expect(wrapperChildren[2].getText()).to.equal(' cat');
515 describe('unwrapping', function() {
516 it('unwraps DocumentTextElement from its parent DocumentNodeElement if it\'s its only child', function() {
517 var c = canvas.fromXML('<section>Alice <span>has a</span> cat</section>'),
519 text = section.children()[1].children()[0];
523 expect(section.children().length).to.equal(1, 'section has one child');
524 expect(section.children()[0].getText()).to.equal('Alice has a cat');
529 describe('Lists api', function() {
530 describe('creating lists', function() {
531 it('allows creation of a list from existing sibling DocumentElements', function() {
532 var c = canvas.fromXML('\
540 textHas = section.children()[1],
541 divA = section.children()[2]
543 c.list.create({element1: textHas, element2: divA});
545 expect(section.children().length).to.equal(3, 'section has three child elements');
547 var child1 = section.children()[0],
548 list = section.children()[1],
549 child3 = section.children()[2];
551 expect(child1.getText()).to.equal('Alice');
552 expect(list.is('list')).to.equal(true, 'second child is a list');
553 expect(list.children().length).to.equal(2, 'list contains two elements');
554 list.children().forEach(function(child) {
555 expect(child.getWlxmlClass()).to.equal('item', 'list childs have wlxml class of item');
557 expect(child3.children()[0].getText()).to.equal('cat');
560 it('allows creating nested list from existing sibling list items', function() {
561 var c = canvas.fromXML('\
563 <div class="list-items">\
564 <div class="item">A</div>\
565 <div class="item">B</div>\
566 <div class="item">C</div>\
567 <div class="item">D</div>\
570 outerList = c.doc().children()[0],
571 itemB = outerList.children()[1],
572 itemC = outerList.children()[2];
575 c.list.create({element1: itemB, element2: itemC});
577 var outerListItems = outerList.children(),
578 innerList = outerListItems[1].children()[0],
579 innerListItems = innerList.children();
581 expect(outerListItems.length).to.equal(3, 'outer list has three items');
582 expect(outerListItems[0].children()[0].getText()).to.equal('A', 'first outer item ok');
583 expect(outerListItems[1].getWlxmlClass()).to.equal('item', 'inner list is wrapped by item element');
585 expect(innerList.is('list')).to.equal(true, 'inner list created');
586 expect(innerListItems.length).to.equal(2, 'inner list has two items');
587 expect(innerListItems[0].children()[0].getText()).to.equal('B', 'first inner item ok');
588 expect(innerListItems[1].children()[0].getText()).to.equal('C', 'second inner item ok');
590 expect(outerListItems[2].children()[0].getText()).to.equal('D', 'last outer item ok');
596 describe('extracting list items', function() {
597 it('creates two lists with extracted items in the middle if extracting from the middle of the list', function() {
598 var c = canvas.fromXML('\
600 <div class="list.items">\
601 <div class="item">0</div>\
602 <div class="item">1</div>\
603 <div class="item">2</div>\
604 <div class="item">3</div>\
607 list = c.doc().children()[0],
608 item1 = list.children()[1],
609 item2 = list.children()[2];
611 c.list.extractItems({element1: item1, element2: item2});
613 var section = c.doc(),
614 list1 = section.children()[0],
615 oldItem1 = section.children()[1],
616 oldItem2 = section.children()[2],
617 list2 = section.children()[3];
619 expect(section.children().length).to.equal(4, 'section contains four children');
621 expect(list1.is('list')).to.equal(true, 'first section child is a list');
622 expect(list1.children().length).to.equal(1, 'first list has one child');
623 expect(list1.children()[0].children()[0].getText()).to.equal('0', 'first item of the first list is a first item of the original list');
625 expect(oldItem1.children()[0].getText()).to.equal('1', 'first item got extracted');
626 expect(oldItem1.getWlxmlClass() === undefined).to.equal(true, 'first extracted element has no wlxml class');
628 expect(oldItem2.children()[0].getText()).to.equal('2', 'second item got extracted');
629 expect(oldItem2.getWlxmlClass() === undefined).to.equal(true, 'second extracted element has no wlxml class');
631 expect(list2.is('list')).to.equal(true, 'last section child is a list');
632 expect(list2.children().length).to.equal(1, 'second list has one child');
633 expect(list2.children()[0].children()[0].getText()).to.equal('3', 'first item of the second list is a last item of the original list');
636 it('puts extracted items above the list if starting item is the first one', function() {
637 var c = canvas.fromXML('\
639 <div class="list.items">\
640 <div class="item">0</div>\
641 <div class="item">1</div>\
642 <div class="item">2</div>\
645 list = c.doc().children()[0],
646 item1 = list.children()[0],
647 item2 = list.children()[1],
648 item3 = list.children()[2];
650 c.list.extractItems({element1: item1, element2: item2});
652 var section = c.doc(),
653 oldItem1 = section.children()[0],
654 oldItem2 = section.children()[1],
655 newList = section.children()[2];
657 expect(section.children().length).to.equal(3, 'section has three children');
658 expect(oldItem1.children()[0].getText()).to.equal('0', 'first item extracted');
659 expect(oldItem2.children()[0].getText()).to.equal('1', 'second item extracted');
660 expect(newList.is('list')).to.equal(true, 'list lies below extracted item');
661 expect(newList.children().length).to.equal(1, 'list has now one child');
664 it('puts extracted items below the list if ending item is the last one', function() {
665 var c = canvas.fromXML('\
667 <div class="list.items">\
668 <div class="item">0</div>\
669 <div class="item">1</div>\
670 <div class="item">2</div>\
673 list = c.doc().children()[0],
674 item1 = list.children()[0],
675 item2 = list.children()[1],
676 item3 = list.children()[2];
678 c.list.extractItems({element1: item2, element2: item3});
680 var section = c.doc(),
681 oldItem1 = section.children()[1],
682 oldItem2 = section.children()[2],
683 newList = section.children()[0];
685 expect(section.children().length).to.equal(3, 'section has three children');
686 expect(oldItem1.children()[0].getText()).to.equal('1', 'first item extracted');
687 expect(oldItem2.children()[0].getText()).to.equal('2', 'second item extracted');
688 expect(newList.is('list')).to.equal(true, 'list lies above extracted item');
689 expect(newList.children().length).to.equal(1, 'list has now one child');
692 it('removes list if all its items are extracted', function() {
693 var c = canvas.fromXML('\
695 <div class="list.items">\
696 <div class="item">some item</div>\
697 <div class="item">some item 2</div>\
700 list = c.doc().children()[0],
701 item1 = list.children()[0],
702 item2 = list.children()[1];
704 c.list.extractItems({element1: item1, element2: item2});
706 var section = c.doc(),
707 list1 = section.children()[0],
708 oldItem1 = section.children()[0],
709 oldItem2 = section.children()[1];
711 expect(section.children().length).to.equal(2, 'section contains two children');
712 expect(oldItem1.children()[0].getText()).to.equal('some item');
713 expect(oldItem2.children()[0].getText()).to.equal('some item 2');
716 it('creates two lists with extracted items in the middle if extracting from the middle of the list - nested case' , function() {
717 var c = canvas.fromXML('\
719 <div class="list.items">\
720 <div class="item">0</div>\
722 <div class="list.items">\
723 <div class="item">1.1</div>\
724 <div class="item">1.2</div>\
725 <div class="item">1.3</div>\
728 <div class="item">2</div>\
731 list = c.doc().children()[0],
732 nestedList = list.children()[1].children()[0],
733 nestedListItem = nestedList.children()[1];
735 c.list.extractItems({element1: nestedListItem, element2: nestedListItem});
737 var section = c.doc(),
738 list = section.children()[0],
739 item1 = list.children()[0],
740 item2 = list.children()[1], //
741 item3 = list.children()[2],
742 item4 = list.children()[3], //
743 item5 = list.children()[4],
744 nestedList1 = item2.children()[0],
745 nestedList2 = item4.children()[0];
747 expect(list.children().length).to.equal(5, 'top list has five items');
749 expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
751 expect(item2.getWlxmlClass()).to.equal('item', 'first nested list is still wrapped in item element');
752 expect(nestedList1.children().length).to.equal(1, 'first nested list is left with one child');
753 expect(nestedList1.children()[0].children()[0].getText()).to.equal('1.1', 'first nested list item left alone');
755 expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
757 expect(item4.getWlxmlClass()).to.equal('item', 'second nested list is still wrapped in item element');
758 expect(nestedList2.children().length).to.equal(1, 'second nested list is left with one child');
759 expect(nestedList2.children()[0].children()[0].getText()).to.equal('1.3', 'second nested list item left alone');
761 expect(item5.children()[0].getText()).to.equal('2', 'last item ok');
764 it('puts extracted items below the list if ending item is the last one - nested case' , function() {
765 var c = canvas.fromXML('\
767 <div class="list.items">\
768 <div class="item">0</div>\
770 <div class="list.items">\
771 <div class="item">1.1</div>\
772 <div class="item">1.2</div>\
773 <div class="item">1.3</div>\
776 <div class="item">2</div>\
779 list = c.doc().children()[0],
780 nestedList = list.children()[1].children()[0],
781 nestedListItem1 = nestedList.children()[1],
782 nestedListItem2 = nestedList.children()[2];
784 c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
786 var section = c.doc(),
787 list = section.children()[0],
788 item1 = list.children()[0],
789 item2 = list.children()[1],
790 item3 = list.children()[2],
791 item4 = list.children()[3],
792 item5 = list.children()[4];
793 nestedList = item2.children()[0];
795 expect(list.children().length).to.equal(5, 'top list has five items');
796 expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
797 expect(item2.getWlxmlClass()).to.equal('item', 'nested list is still wrapped in item element');
798 expect(nestedList.children().length).to.equal(1, 'nested list is left with one child');
799 expect(nestedList.children()[0].children()[0].getText()).to.equal('1.1', 'nested list item left alone');
800 expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
801 expect(item4.children()[0].getText()).to.equal('1.3', 'fourth item ok');
802 expect(item5.children()[0].getText()).to.equal('2', 'fifth item ok');
805 it('puts extracted items above the list if starting item is the first one - nested case' , function() {
806 var c = canvas.fromXML('\
808 <div class="list.items">\
809 <div class="item">0</div>\
811 <div class="list.items">\
812 <div class="item">1.1</div>\
813 <div class="item">1.2</div>\
814 <div class="item">1.3</div>\
817 <div class="item">2</div>\
820 list = c.doc().children()[0],
821 nestedList = list.children()[1].children()[0],
822 nestedListItem1 = nestedList.children()[0],
823 nestedListItem2 = nestedList.children()[1];
825 c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
827 var section = c.doc(),
828 list = section.children()[0],
829 item1 = list.children()[0],
830 item2 = list.children()[1],
831 item3 = list.children()[2],
832 item4 = list.children()[3],
833 item5 = list.children()[4];
834 nestedList = item4.children()[0];
836 expect(list.children().length).to.equal(5, 'top list has five items');
837 expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
838 expect(item2.children()[0].getText()).to.equal('1.1', 'second item ok');
839 expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
841 expect(item4.getWlxmlClass()).to.equal('item', 'nested list is still wrapped in item element');
842 expect(nestedList.children().length).to.equal(1, 'nested list is left with one child');
843 expect(nestedList.children()[0].children()[0].getText()).to.equal('1.3', 'nested list item left alone');
844 expect(item5.children()[0].getText()).to.equal('2', 'fifth item ok');
847 it('removes list if all its items are extracted - nested case', function() {
848 var c = canvas.fromXML('\
850 <div class="list.items">\
851 <div class="item">0</div>\
853 <div class="list.items">\
854 <div class="item">1.1</div>\
855 <div class="item">1.2</div>\
858 <div class="item">2</div>\
861 list = c.doc().children()[0],
862 nestedList = list.children()[1].children()[0],
863 nestedListItem1 = nestedList.children()[0],
864 nestedListItem2 = nestedList.children()[1];
866 c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
868 var section = c.doc(),
869 list = section.children()[0],
870 item1 = list.children()[0],
871 item2 = list.children()[1],
872 item3 = list.children()[2],
873 item4 = list.children()[3];
875 expect(list.children().length).to.equal(4, 'top list has four items');
876 expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
877 expect(item2.children()[0].getText()).to.equal('1.1', 'second item ok');
878 expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
879 expect(item4.children()[0].getText()).to.equal('2', 'fourth item ok');
882 it('extracts items out of outer most list when merge flag is set to false', function() {
883 var c = canvas.fromXML('\
885 <div class="list.items">\
886 <div class="item">0</div>\
888 <div class="list.items">\
889 <div class="item">1.1</div>\
890 <div class="item">1.2</div>\
893 <div class="item">2</div>\
897 list = section.children()[0],
898 nestedList = list.children()[1].children()[0],
899 nestedListItem = nestedList.children()[0];
901 var test = c.list.extractItems({element1: nestedListItem, element2: nestedListItem, merge: false});
903 expect(test).to.equal(true, 'extraction status ok');
905 var sectionChildren = section.children(),
906 extractedItem = sectionChildren[1];
908 expect(sectionChildren.length).to.equal(3, 'section has three children');
909 expect(sectionChildren[0].is('list')).to.equal(true, 'first child is a list');
911 expect(extractedItem.getWlxmlTag()).to.equal('div', 'extracted item is a wlxml div');
912 expect(extractedItem.getWlxmlClass()).to.equal(undefined, 'extracted item has no wlxml class');
913 expect(extractedItem.children()[0].getText()).to.equal('1.1', 'extracted item ok');
914 expect(sectionChildren[2].is('list')).to.equal(true, 'second child is a list');
921 describe('Cursor', function() {
925 beforeEach(function() {
926 getSelection = sinon.stub(window, 'getSelection');
929 afterEach(function() {
930 getSelection.restore();
933 it('returns position when browser selection collapsed', function() {
934 var c = canvas.fromXML('<section>Alice has a cat</section>'),
936 text = $(dom.contents()[0]).contents()[0];
938 expect(text.nodeType).to.equal(Node.TEXT_NODE, 'correct node selected');
939 expect($(text).text()).to.equal('Alice has a cat');
941 getSelection.returns({
948 var cursor = c.getCursor(),
949 position = cursor.getPosition();
951 expect(cursor.isSelecting()).to.equal(false, 'cursor is not selecting anything');
952 expect(position.element.getText()).to.equal('Alice has a cat');
953 expect(position.offset).to.equal(5);
956 it('returns boundries of selection when browser selection not collapsed', function() {
957 var c = canvas.fromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
960 alice: dom.contents()[0],
961 has: $(dom.contents()[1]).contents()[0],
962 cat: dom.contents()[4]
964 cursor = c.getCursor(),
965 aliceElement = c.getDocumentElement(text.alice),
966 catElement = c.getDocumentElement(text.cat);
970 {focus: text.alice, focusOffset: 1, anchor: text.cat, anchorOffset: 2, selectionAnchor: catElement},
971 {focus: text.cat, focusOffset: 2, anchor: text.alice, anchorOffset: 1, selectionAnchor: aliceElement}
972 ].forEach(function(s, idx) {
973 getSelection.returns({isColapsed: false, anchorNode: s.anchor, anchorOffset: s.anchorOffset, focusNode: s.focus, focusOffset: s.focusOffset});
975 var selectionStart = cursor.getSelectionStart(),
976 selectionEnd = cursor.getSelectionEnd(),
977 selectionAnchor = cursor.getSelectionAnchor();
979 expect(cursor.isSelecting()).to.equal(true, 'cursor is selecting');
980 expect(selectionStart.element.sameNode(aliceElement)).to.equal(true, '"Alice" is the start of the selection ' + idx);
981 expect(selectionStart.offset).to.equal(1, '"Alice" offset ok' + idx);
982 expect(selectionEnd.element.sameNode(catElement)).to.equal(true, '"Cat" is the start of the selection ' + idx);
983 expect(selectionEnd.offset).to.equal(2, '"Cat" offset ok' + idx);
984 expect(selectionAnchor.element.sameNode(s.selectionAnchor)).to.equal(true, 'anchor ok');
985 expect(selectionAnchor.offset).to.equal(s.anchorOffset, 'anchor offset ok');
989 it('recognizes when browser selection boundries lies in sibling DocumentTextElements', function() {
990 var c = canvas.fromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
993 alice: dom.contents()[0],
994 has: $(dom.contents()[1]).contents()[0],
995 a: dom.contents()[2],
996 big: $(dom.contents()[3]).contents()[0],
997 cat: dom.contents()[4]
999 cursor = c.getCursor();
1001 expect($(text.alice).text()).to.equal('Alice ');
1002 expect($(text.has).text()).to.equal('has');
1003 expect($(text.a).text()).to.equal(' a ');
1004 expect($(text.big).text()).to.equal('big');
1005 expect($(text.cat).text()).to.equal(' cat');
1007 getSelection.returns({anchorNode: text.alice, focusNode: text.a});
1008 expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "a" are children');
1010 getSelection.returns({anchorNode: text.alice, focusNode: text.cat});
1011 expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "cat" are children');
1013 getSelection.returns({anchorNode: text.alice, focusNode: text.has});
1014 expect(cursor.isSelectingSiblings()).to.equal(false, '"Alice" and "has" are not children');
1016 getSelection.returns({anchorNode: text.has, focusNode: text.big});
1017 expect(cursor.isSelectingSiblings()).to.equal(false, '"has" and "big" are not children');