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().getWlxmlTag()).to.equal('section');
74 describe('root element', function() {
75 it('has no parent', function() {
76 var c = canvas.fromXML('<section></section>');
77 expect(c.doc().parent()).to.be.null;
81 describe('DocumentTextElement', function() {
82 it('can have its content set', function() {
83 var c = canvas.fromXML('<section>Alice</section>'),
85 text = root.children()[0];
87 text.setText('a cat');
88 expect(root.children()[0].getText()).to.equal('a cat');
92 describe('DocumentNodeElement', function() {
93 it('knows index of its child', function() {
94 var c = canvas.fromXML('<section><div></div><header></header><span></span></section>'),
96 child = root.children()[1];
97 expect(root.childIndex(child)).to.equal(1);
100 it('knows WLXML tag it renders', function(){
101 var c = canvas.fromXML('<section></section>'),
103 expect(section.getWlxmlTag()).to.equal('section', 'initial tag is section');
104 section.setWlxmlTag('header');
105 expect(section.getWlxmlTag()).to.equal('header', 'tag is changed to header');
108 it('knows WLXML class of a WLXML tag it renders', function(){
109 var c = canvas.fromXML('<section class="some.class"></section>'),
111 expect(section.getWlxmlClass()).to.equal('some.class');
112 section.setWlxmlClass('some.other.class');
113 expect(section.getWlxmlClass()).to.equal('some.other.class');
114 section.setWlxmlClass(null);
115 expect(section.getWlxmlClass()).to.be.undefined;
119 it('returns DocumentNodeElement instance from HTMLElement', function() {
120 var c = canvas.fromXML('<section></section>'),
121 htmlElement = c.doc().dom().get(0),
122 element = c.getDocumentElement(htmlElement);
123 expect(element).to.be.instanceOf(documentElement.DocumentNodeElement);
124 expect(element.sameNode(c.doc()));
127 it('returns DocumentTextElement instance from Text Node', function() {
128 var c = canvas.fromXML('<section>Alice</section>'),
129 aliceElement = c.doc().children()[0],
130 textNode = aliceElement.dom()[0],
131 element = c.getDocumentElement(textNode);
133 expect(textNode.nodeType).to.equal(Node.TEXT_NODE, 'text node selected');
134 expect($(textNode).text()).to.equal('Alice');
136 expect(element).to.be.instanceOf(documentElement.DocumentTextElement);
137 expect(element.sameNode(c.doc().children()[0]));
143 describe('document representation api', function() {
144 describe('document root element', function() {
145 var c = canvas.fromXML('<section></section>');
146 it('exists', function() {
147 expect(c.doc()).to.be.instanceOf(documentElement.DocumentElement);
149 it('is of type DocumentNodeElement', function() {
150 expect(c.doc()).to.be.instanceOf(documentElement.DocumentNodeElement);
154 describe('DocumentElements comparison', function() {
155 it('reports dwo DocumentElements to be the same when they represent the same wlxml document element', function() {
156 var c = canvas.fromXML('<section><div></div><div></div></section>'),
157 first_div1 = c.doc().children()[0],
158 first_div2 = c.doc().children()[0],
159 second_div = c.doc().children()[1];
160 expect(first_div1.sameNode(first_div1)).to.be.true;
161 expect(first_div1.sameNode(first_div2)).to.be.true;
162 expect(first_div1.sameNode(second_div)).to.be.false;
166 describe('traversing', function() {
167 it('reports element nodes', function() {
168 var c = canvas.fromXML('<section><div></div></section>'),
169 children = c.doc().children();
170 expect(children.length).to.equal(1);
171 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
173 c = canvas.fromXML('<section><div></div><div></div></section>'),
174 children = c.doc().children();
175 expect(children.length).to.equal(2);
176 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
177 expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
180 it('reports text nodes', function() {
181 var c = canvas.fromXML('<section>Alice</section>'),
182 children = c.doc().children();
183 expect(children.length).to.equal(1);
184 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
187 describe('accessing parents', function() {
188 it('returns DocumentNodeElement representing parent in wlxml document as DocumentNodeElement parent', function() {
189 var c = canvas.fromXML('<section><div></div></section>'),
190 div = c.doc().children()[0];
191 expect(div.parent().sameNode(c.doc())).to.be.true;
193 it('returns DocumentNodeElement representing parent in wlxml document as DocumentTextElement parent', function() {
194 var c = canvas.fromXML('<section>Alice</section>'),
195 text = c.doc().children()[0];
196 expect(text.parent().sameNode(c.doc())).to.be.true;
200 describe('free text handling', function() {
201 it('sees free text', function() {
202 var c = canvas.fromXML('<section>Alice <span>has</span> a cat</section>'),
203 children = c.doc().children();
204 expect(children.length).to.equal(3);
205 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
206 expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
207 expect(children[2]).to.be.instanceOf(documentElement.DocumentTextElement);
211 describe('white characters handling', function() {
212 it('says empty element node has no children', function() {
213 var c = canvas.fromXML('<section></section>');
214 expect(c.doc().children().length).to.equal(0);
216 it('says element node with one space has one DocumentTextElement', function() {
217 var c = canvas.fromXML('<section> </section>');
218 expect(c.doc().children().length).to.equal(1);
219 expect(c.doc().children()[0]).to.be.instanceOf(documentElement.DocumentTextElement);
220 expect(c.doc().children()[0].getText()).to.equal(' ');
222 it('ignores white space surrounding block elements', function() {
223 var c = canvas.fromXML('<section> <div></div> </section>');
224 var children = c.doc().children();
225 expect(children.length).to.equal(1);
226 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
228 it('ignores white space between block elements', function() {
229 var c = canvas.fromXML('<section><div></div> <div></div></section>');
230 var children = c.doc().children();
231 expect(children.length === 2);
232 [0,1].forEach(function(idx) {
233 expect(children[idx]).to.be.instanceOf(documentElement.DocumentNodeElement);
237 it('trims white space from the beginning and the end of the block elements', function() {
238 var c = canvas.fromXML('<section> Alice <span>has</span> a cat </section>');
239 expect(c.doc().children()[0].getText()).to.equal('Alice ');
240 expect(c.doc().children()[2].getText()).to.equal(' a cat');
243 it('normalizes string of white characters to one space at the inline element boundries', function() {
244 var c = canvas.fromXML('<span> Alice has a cat </span>');
245 expect(c.doc().children()[0].getText()).to.equal(' Alice has a cat ');
248 it('normalizes string of white characters to one space before inline element', function() {
249 var c = canvas.fromXML('<div>Alice has <span>a cat</span></div>');
250 expect(c.doc().children()[0].getText()).to.equal('Alice has ');
253 it('normalizes string of white characters to one space after inline element', function() {
254 var c = canvas.fromXML('<div>Alice has <span>a</span> cat</div>');
255 expect(c.doc().children()[2].getText()).to.equal(' cat');
260 describe('manipulation api', function() {
262 describe('Basic Element inserting', function() {
263 it('can put new NodeElement at the end', function() {
264 var c = canvas.fromXML('<section></section>'),
265 appended = c.doc().append({tag: 'header', klass: 'some.class'}),
266 children = c.doc().children();
268 expect(children.length).to.equal(1);
269 expect(children[0].sameNode(appended));
272 it('can put new TextElement at the end', function() {
273 var c = canvas.fromXML('<section></section>'),
274 appended = c.doc().append({text: 'Alice'}),
275 children = c.doc().children();
277 expect(children.length).to.equal(1);
278 expect(children[0].sameNode(appended));
279 expect(children[0].getText()).to.equal('Alice');
282 it('can put new NodeElement after another NodeElement', function() {
283 var c = canvas.fromXML('<section><div></div></section>'),
284 div = c.doc().children()[0],
285 added = div.after({tag: 'header', klass: 'some.class'}),
286 children = c.doc().children();
287 expect(children.length).to.equal(2);
288 expect(children[1].sameNode(added));
291 it('can put new Nodeelement before another element', function() {
292 var c = canvas.fromXML('<section><div></div></section>'),
293 div = c.doc().children()[0],
294 added = div.before({tag: 'header', klass: 'some.class'}),
295 children = c.doc().children();
296 expect(children.length).to.equal(2);
297 expect(children[0].sameNode(added));
300 it('can put new DocumentNodeElement after DocumentTextElement', function() {
301 var c = canvas.fromXML('<section>Alice</section>'),
302 text = c.doc().children()[0],
303 added = text.after({tag: 'p'}),
304 children = c.doc().children();
306 expect(children.length).to.equal(2);
307 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
308 expect(children[0].getText()).to.equal('Alice');
309 expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
310 expect(children[1].sameNode(added)).to.be.true;
312 it('can put new DocumentNodeElement before DocumentTextElement', function() {
313 var c = canvas.fromXML('<section>Alice</section>'),
314 text = c.doc().children()[0],
315 added = text.before({tag: 'p'}),
316 children = c.doc().children();
318 expect(children.length).to.equal(2);
319 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
320 expect(children[0].sameNode(added)).to.be.true;
321 expect(children[1]).to.be.instanceOf(documentElement.DocumentTextElement);
322 expect(children[1].getText()).to.equal('Alice');
326 describe('Splitting text', function() {
328 it('splits DocumentTextElement\'s parent into two DocumentNodeElements of the same type', function() {
329 var c = canvas.fromXML('<section><header>Some header</header></section>'),
331 text = section.children()[0].children()[0];
333 text.split({offset: 5});
334 expect(section.children().length).to.equal(2, 'section has two children');
336 var header1 = section.children()[0];
337 var header2 = section.children()[1];
339 expect(header1.getWlxmlTag()).to.equal('header', 'first section child represents wlxml header');
340 expect(header1.children().length).to.equal(1, 'first header has one text child');
341 expect(header1.children()[0].getText()).to.equal('Some ', 'first header has correct content');
342 expect(header2.getWlxmlTag()).to.equal('header', 'second section child represents wlxml header');
343 expect(header2.children().length).to.equal(1, 'second header has one text child');
344 expect(header2.children()[0].getText()).to.equal('header', 'second header has correct content');
347 it('leaves empty copy of DocumentNodeElement if splitting at the very beginning', function() {
348 var c = canvas.fromXML('<section><header>Some header</header></section>'),
350 text = section.children()[0].children()[0];
352 text.split({offset: 0});
354 var header1 = section.children()[0];
355 var header2 = section.children()[1];
357 expect(header1.children().length).to.equal(0);
358 expect(header2.children()[0].getText()).to.equal('Some header');
361 it('leaves empty copy of DocumentNodeElement if splitting at the very end', function() {
362 var c = canvas.fromXML('<section><header>Some header</header></section>'),
364 text = section.children()[0].children()[0];
366 text.split({offset: 11});
368 var header1 = section.children()[0];
369 var header2 = section.children()[1];
371 expect(header1.children()[0].getText()).to.equal('Some header');
372 expect(header2.children().length).to.equal(0);
375 it('keeps DocumentTextElement\'s parent\'s children elements intact', function() {
376 var c = canvas.fromXML('\
379 A <span>fancy</span> and <span>nice</span> header\
383 header = section.children()[0],
384 textAnd = header.children()[2];
386 textAnd.split({offset: 2});
388 var sectionChildren = section.children();
389 expect(sectionChildren.length).to.equal(2, 'Section has two children');
390 expect(sectionChildren[0].getWlxmlTag()).to.equal('header', 'First section element is a wlxml header');
391 expect(sectionChildren[1].getWlxmlTag()).to.equal('header', 'Second section element is a wlxml header');
393 var firstHeaderChildren = sectionChildren[0].children();
394 expect(firstHeaderChildren.length).to.equal(3, 'First header has three children');
395 expect(firstHeaderChildren[0].getText()).to.equal('A ', 'First header starts with a text');
396 expect(firstHeaderChildren[1].getWlxmlTag()).to.equal('span', 'First header has span in the middle');
397 expect(firstHeaderChildren[2].getText()).to.equal(' a', 'First header ends with text');
399 var secondHeaderChildren = sectionChildren[1].children();
400 expect(secondHeaderChildren.length).to.equal(3, 'Second header has three children');
401 expect(secondHeaderChildren[0].getText()).to.equal('nd ', 'Second header starts with text');
402 expect(secondHeaderChildren[1].getWlxmlTag()).to.equal('span', 'Second header has span in the middle');
403 expect(secondHeaderChildren[2].getText()).to.equal(' header', 'Second header ends with text');
407 describe('wrapping', function() {
408 it('wraps DocumentNodeElement', function() {
409 var c = canvas.fromXML('<section><div></div></section>'),
410 div = c.doc().children()[0];
412 var returned = div.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
413 parent = div.parent(),
414 parent2 = c.doc().children()[0];
416 expect(returned.sameNode(parent)).to.be.true;
417 expect(returned.sameNode(parent2)).to.be.true;
418 expect(returned.getWlxmlTag()).to.equal('header');
419 expect(returned.getWlxmlClass()).to.equal('some.class');
421 it('wraps DocumentTextElement', function() {
422 var c = canvas.fromXML('<section>Alice</section>'),
423 text = c.doc().children()[0];
425 var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
426 parent = text.parent(),
427 parent2 = c.doc().children()[0];
429 expect(returned.sameNode(parent)).to.be.true;
430 expect(returned.sameNode(parent2)).to.be.true;
431 expect(returned.getWlxmlTag()).to.equal('header');
432 expect(returned.getWlxmlClass()).to.equal('some.class');
435 describe('wrapping part of DocumentTextElement', function() {
436 [{start: 5, end: 12}, {start: 12, end: 5}].forEach(function(offsets) {
437 it('wraps in the middle ' + offsets.start + '/' + offsets.end, function() {
438 var c = canvas.fromXML('<section>Alice has a cat</section>'),
439 text = c.doc().children()[0];
441 var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class', start: offsets.start, end: offsets.end}),
442 children = c.doc().children();
444 expect(children.length).to.equal(3);
446 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
447 expect(children[0].getText()).to.equal('Alice');
449 expect(children[1].sameNode(returned)).to.be.true;
450 expect(returned.getWlxmlTag()).to.equal('header');
451 expect(returned.getWlxmlClass()).to.equal('some.class');
452 expect(children[1].children().length).to.equal(1);
453 expect(children[1].children()[0].getText()).to.equal(' has a ');
455 expect(children[2]).to.be.instanceOf(documentElement.DocumentTextElement);
456 expect(children[2].getText()).to.equal('cat');
460 it('wraps whole text inside DocumentTextElement if offsets span entire content', function() {
461 var c = canvas.fromXML('<section>Alice has a cat</section>'),
462 text = c.doc().children()[0];
464 var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class', start: 0, end: 15}),
465 children = c.doc().children();
467 expect(children.length).to.equal(1);
468 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
469 expect(children[0].children()[0].getText()).to.equal('Alice has a cat');
473 it('wraps text spanning multiple sibling DocumentTextNodes', function() {
474 var c = canvas.fromXML('<section>Alice has a <span>small</span> cat</section>'),
476 wrapper = c.wrapText({
478 _with: {tag: 'span', klass: 'some.class'},
484 expect(section.children().length).to.equal(2);
485 expect(section.children()[0]).to.be.instanceOf(documentElement.DocumentTextElement);
486 expect(section.children()[0].getText()).to.equal('Alice ');
488 var wrapper2 = section.children()[1];
489 expect(wrapper2.sameNode(wrapper)).to.be.true;
491 var wrapperChildren = wrapper.children();
492 expect(wrapperChildren.length).to.equal(3);
493 expect(wrapperChildren[0].getText()).to.equal('has a ');
495 expect(wrapperChildren[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
496 expect(wrapperChildren[1].children().length).to.equal(1);
497 expect(wrapperChildren[1].children()[0].getText()).to.equal('small');
499 expect(wrapperChildren[2].getText()).to.equal(' cat');
503 describe('unwrapping', function() {
504 it('unwraps DocumentTextElement from its parent DocumentNodeElement if it\'s its only child', function() {
505 var c = canvas.fromXML('<section><div>Alice has a cat</div></section>'),
507 text = section.children()[0].children()[0];
511 expect(section.children().length).to.equal(1);
512 expect(section.children()[0].getText()).to.equal('Alice has a cat');
517 describe('Lists api', function() {
518 describe('creating lists', function() {
519 it('allows creation of a list from existing sibling DocumentElements', function() {
520 var c = canvas.fromXML('\
528 textHas = section.children()[1],
529 divA = section.children()[2]
531 c.list.create({element1: textHas, element2: divA});
533 expect(section.children().length).to.equal(3, 'section has three child elements');
535 var child1 = section.children()[0],
536 list = section.children()[1],
537 child3 = section.children()[2];
539 expect(child1.getText()).to.equal('Alice');
540 expect(list.is('list')).to.equal(true, 'second child is a list');
541 expect(list.children().length).to.equal(2, 'list contains two elements');
542 list.children().forEach(function(child) {
543 expect(child.getWlxmlClass()).to.equal('item', 'list childs have wlxml class of item');
545 expect(child3.children()[0].getText()).to.equal('cat');
548 it('allows creating nested list from existing sibling list items', function() {
549 var c = canvas.fromXML('\
551 <div class="list-items">\
552 <div class="item">A</div>\
553 <div class="item">B</div>\
554 <div class="item">C</div>\
555 <div class="item">D</div>\
558 outerList = c.doc().children()[0],
559 itemB = outerList.children()[1],
560 itemC = outerList.children()[2];
563 c.list.create({element1: itemB, element2: itemC});
565 var outerListItems = outerList.children(),
566 innerList = outerListItems[1].children()[0],
567 innerListItems = innerList.children();
569 expect(outerListItems.length).to.equal(3, 'outer list has three items');
570 expect(outerListItems[0].children()[0].getText()).to.equal('A', 'first outer item ok');
571 expect(outerListItems[1].getWlxmlClass()).to.equal('item', 'inner list is wrapped by item element');
573 expect(innerList.is('list')).to.equal(true, 'inner list created');
574 expect(innerListItems.length).to.equal(2, 'inner list has two items');
575 expect(innerListItems[0].children()[0].getText()).to.equal('B', 'first inner item ok');
576 expect(innerListItems[1].children()[0].getText()).to.equal('C', 'second inner item ok');
578 expect(outerListItems[2].children()[0].getText()).to.equal('D', 'last outer item ok');
584 describe('extracting list items', function() {
585 it('creates two lists with extracted items in the middle if extracting from the middle of the list', function() {
586 var c = canvas.fromXML('\
588 <div class="list.items">\
589 <div class="item">0</div>\
590 <div class="item">1</div>\
591 <div class="item">2</div>\
592 <div class="item">3</div>\
595 list = c.doc().children()[0],
596 item1 = list.children()[1],
597 item2 = list.children()[2];
599 c.list.extractItems({element1: item1, element2: item2});
601 var section = c.doc(),
602 list1 = section.children()[0],
603 oldItem1 = section.children()[1],
604 oldItem2 = section.children()[2],
605 list2 = section.children()[3];
607 expect(section.children().length).to.equal(4, 'section contains four children');
609 expect(list1.is('list')).to.equal(true, 'first section child is a list');
610 expect(list1.children().length).to.equal(1, 'first list has one child');
611 expect(list1.children()[0].children()[0].getText()).to.equal('0', 'first item of the first list is a first item of the original list');
613 expect(oldItem1.children()[0].getText()).to.equal('1', 'first item got extracted');
614 expect(oldItem1.getWlxmlClass() === undefined).to.equal(true, 'first extracted element has no wlxml class');
616 expect(oldItem2.children()[0].getText()).to.equal('2', 'second item got extracted');
617 expect(oldItem2.getWlxmlClass() === undefined).to.equal(true, 'second extracted element has no wlxml class');
619 expect(list2.is('list')).to.equal(true, 'last section child is a list');
620 expect(list2.children().length).to.equal(1, 'second list has one child');
621 expect(list2.children()[0].children()[0].getText()).to.equal('3', 'first item of the second list is a last item of the original list');
624 it('puts extracted items above the list if starting item is the first one', function() {
625 var c = canvas.fromXML('\
627 <div class="list.items">\
628 <div class="item">0</div>\
629 <div class="item">1</div>\
630 <div class="item">2</div>\
633 list = c.doc().children()[0],
634 item1 = list.children()[0],
635 item2 = list.children()[1],
636 item3 = list.children()[2];
638 c.list.extractItems({element1: item1, element2: item2});
640 var section = c.doc(),
641 oldItem1 = section.children()[0],
642 oldItem2 = section.children()[1],
643 newList = section.children()[2];
645 expect(section.children().length).to.equal(3, 'section has three children');
646 expect(oldItem1.children()[0].getText()).to.equal('0', 'first item extracted');
647 expect(oldItem2.children()[0].getText()).to.equal('1', 'second item extracted');
648 expect(newList.is('list')).to.equal(true, 'list lies below extracted item');
649 expect(newList.children().length).to.equal(1, 'list has now one child');
652 it('puts extracted items below the list if ending item is the last one', function() {
653 var c = canvas.fromXML('\
655 <div class="list.items">\
656 <div class="item">0</div>\
657 <div class="item">1</div>\
658 <div class="item">2</div>\
661 list = c.doc().children()[0],
662 item1 = list.children()[0],
663 item2 = list.children()[1],
664 item3 = list.children()[2];
666 c.list.extractItems({element1: item2, element2: item3});
668 var section = c.doc(),
669 oldItem1 = section.children()[1],
670 oldItem2 = section.children()[2],
671 newList = section.children()[0];
673 expect(section.children().length).to.equal(3, 'section has three children');
674 expect(oldItem1.children()[0].getText()).to.equal('1', 'first item extracted');
675 expect(oldItem2.children()[0].getText()).to.equal('2', 'second item extracted');
676 expect(newList.is('list')).to.equal(true, 'list lies above extracted item');
677 expect(newList.children().length).to.equal(1, 'list has now one child');
680 it('removes list if all its items are extracted', function() {
681 var c = canvas.fromXML('\
683 <div class="list.items">\
684 <div class="item">some item</div>\
685 <div class="item">some item 2</div>\
688 list = c.doc().children()[0],
689 item1 = list.children()[0],
690 item2 = list.children()[1];
692 c.list.extractItems({element1: item1, element2: item2});
694 var section = c.doc(),
695 list1 = section.children()[0],
696 oldItem1 = section.children()[0],
697 oldItem2 = section.children()[1];
699 expect(section.children().length).to.equal(2, 'section contains two children');
700 expect(oldItem1.children()[0].getText()).to.equal('some item');
701 expect(oldItem2.children()[0].getText()).to.equal('some item 2');
704 it('creates two lists with extracted items in the middle if extracting from the middle of the list - nested case' , function() {
705 var c = canvas.fromXML('\
707 <div class="list.items">\
708 <div class="item">0</div>\
710 <div class="list.items">\
711 <div class="item">1.1</div>\
712 <div class="item">1.2</div>\
713 <div class="item">1.3</div>\
716 <div class="item">2</div>\
719 list = c.doc().children()[0],
720 nestedList = list.children()[1].children()[0],
721 nestedListItem = nestedList.children()[1];
723 c.list.extractItems({element1: nestedListItem, element2: nestedListItem});
725 var section = c.doc(),
726 list = section.children()[0],
727 item1 = list.children()[0],
728 item2 = list.children()[1], //
729 item3 = list.children()[2],
730 item4 = list.children()[3], //
731 item5 = list.children()[4],
732 nestedList1 = item2.children()[0],
733 nestedList2 = item4.children()[0];
735 expect(list.children().length).to.equal(5, 'top list has five items');
737 expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
739 expect(item2.getWlxmlClass()).to.equal('item', 'first nested list is still wrapped in item element');
740 expect(nestedList1.children().length).to.equal(1, 'first nested list is left with one child');
741 expect(nestedList1.children()[0].children()[0].getText()).to.equal('1.1', 'first nested list item left alone');
743 expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
745 expect(item4.getWlxmlClass()).to.equal('item', 'second nested list is still wrapped in item element');
746 expect(nestedList2.children().length).to.equal(1, 'second nested list is left with one child');
747 expect(nestedList2.children()[0].children()[0].getText()).to.equal('1.3', 'second nested list item left alone');
749 expect(item5.children()[0].getText()).to.equal('2', 'last item ok');
752 it('puts extracted items below the list if ending item is the last 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()[1],
770 nestedListItem2 = nestedList.children()[2];
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 = item2.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.getWlxmlClass()).to.equal('item', 'nested list is still wrapped in item element');
786 expect(nestedList.children().length).to.equal(1, 'nested list is left with one child');
787 expect(nestedList.children()[0].children()[0].getText()).to.equal('1.1', 'nested list item left alone');
788 expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
789 expect(item4.children()[0].getText()).to.equal('1.3', 'fourth item ok');
790 expect(item5.children()[0].getText()).to.equal('2', 'fifth item ok');
793 it('puts extracted items above the list if starting item is the first one - nested case' , function() {
794 var c = canvas.fromXML('\
796 <div class="list.items">\
797 <div class="item">0</div>\
799 <div class="list.items">\
800 <div class="item">1.1</div>\
801 <div class="item">1.2</div>\
802 <div class="item">1.3</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],
821 item5 = list.children()[4];
822 nestedList = item4.children()[0];
824 expect(list.children().length).to.equal(5, 'top list has five items');
825 expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
826 expect(item2.children()[0].getText()).to.equal('1.1', 'second item ok');
827 expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
829 expect(item4.getWlxmlClass()).to.equal('item', 'nested list is still wrapped in item element');
830 expect(nestedList.children().length).to.equal(1, 'nested list is left with one child');
831 expect(nestedList.children()[0].children()[0].getText()).to.equal('1.3', 'nested list item left alone');
832 expect(item5.children()[0].getText()).to.equal('2', 'fifth item ok');
835 it('removes list if all its items are extracted - nested case', function() {
836 var c = canvas.fromXML('\
838 <div class="list.items">\
839 <div class="item">0</div>\
841 <div class="list.items">\
842 <div class="item">1.1</div>\
843 <div class="item">1.2</div>\
846 <div class="item">2</div>\
849 list = c.doc().children()[0],
850 nestedList = list.children()[1].children()[0],
851 nestedListItem1 = nestedList.children()[0],
852 nestedListItem2 = nestedList.children()[1];
854 c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
856 var section = c.doc(),
857 list = section.children()[0],
858 item1 = list.children()[0],
859 item2 = list.children()[1],
860 item3 = list.children()[2],
861 item4 = list.children()[3];
863 expect(list.children().length).to.equal(4, 'top list has four items');
864 expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
865 expect(item2.children()[0].getText()).to.equal('1.1', 'second item ok');
866 expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
867 expect(item4.children()[0].getText()).to.equal('2', 'fourth item ok');
870 it('extracts items out of outer most list when merge flag is set to false', function() {
871 var c = canvas.fromXML('\
873 <div class="list.items">\
874 <div class="item">0</div>\
876 <div class="list.items">\
877 <div class="item">1.1</div>\
878 <div class="item">1.2</div>\
881 <div class="item">2</div>\
885 list = section.children()[0],
886 nestedList = list.children()[1].children()[0],
887 nestedListItem = nestedList.children()[0];
889 var test = c.list.extractItems({element1: nestedListItem, element2: nestedListItem, merge: false});
891 expect(test).to.equal(true, 'extraction status ok');
893 var sectionChildren = section.children(),
894 extractedItem = sectionChildren[1];
896 expect(sectionChildren.length).to.equal(3, 'section has three children');
897 expect(sectionChildren[0].is('list')).to.equal(true, 'first child is a list');
899 expect(extractedItem.getWlxmlTag()).to.equal('div', 'extracted item is a wlxml div');
900 expect(extractedItem.getWlxmlClass()).to.equal(undefined, 'extracted item has no wlxml class');
901 expect(extractedItem.children()[0].getText()).to.equal('1.1', 'extracted item ok');
902 expect(sectionChildren[2].is('list')).to.equal(true, 'second child is a list');
909 describe('Cursor', function() {
913 beforeEach(function() {
914 getSelection = sinon.stub(window, 'getSelection');
917 afterEach(function() {
918 getSelection.restore();
921 it('returns position when browser selection collapsed', function() {
922 var c = canvas.fromXML('<section>Alice has a cat</section>'),
924 text = dom.contents()[0];
926 expect($(text).text()).to.equal('Alice has a cat');
928 getSelection.returns({
935 var cursor = c.getCursor(),
936 position = cursor.getPosition();
938 expect(cursor.isSelecting()).to.equal(false, 'cursor is not selecting anything');
939 expect(position.element.getText()).to.equal('Alice has a cat');
940 expect(position.offset).to.equal(5);
943 it('returns boundries of selection when browser selection not collapsed', function() {
944 var c = canvas.fromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
947 alice: dom.contents()[0],
948 has: $(dom.contents()[1]).contents()[0],
949 cat: dom.contents()[4]
951 cursor = c.getCursor(),
952 aliceElement = c.getDocumentElement(text.alice),
953 catElement = c.getDocumentElement(text.cat);
957 {focus: text.alice, focusOffset: 1, anchor: text.cat, anchorOffset: 2, selectionAnchor: catElement},
958 {focus: text.cat, focusOffset: 2, anchor: text.alice, anchorOffset: 1, selectionAnchor: aliceElement}
959 ].forEach(function(s, idx) {
960 getSelection.returns({isColapsed: false, anchorNode: s.anchor, anchorOffset: s.anchorOffset, focusNode: s.focus, focusOffset: s.focusOffset});
962 var selectionStart = cursor.getSelectionStart(),
963 selectionEnd = cursor.getSelectionEnd(),
964 selectionAnchor = cursor.getSelectionAnchor();
966 expect(cursor.isSelecting()).to.equal(true, 'cursor is selecting');
967 expect(selectionStart.element.sameNode(aliceElement)).to.equal(true, '"Alice" is the start of the selection ' + idx);
968 expect(selectionStart.offset).to.equal(1, '"Alice" offset ok' + idx);
969 expect(selectionEnd.element.sameNode(catElement)).to.equal(true, '"Cat" is the start of the selection ' + idx);
970 expect(selectionEnd.offset).to.equal(2, '"Cat" offset ok' + idx);
971 expect(selectionAnchor.element.sameNode(s.selectionAnchor)).to.equal(true, 'anchor ok');
972 expect(selectionAnchor.offset).to.equal(s.anchorOffset, 'anchor offset ok');
976 it('recognizes when browser selection boundries lies in sibling DocumentTextElements', function() {
977 var c = canvas.fromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
980 alice: dom.contents()[0],
981 has: $(dom.contents()[1]).contents()[0],
982 a: dom.contents()[2],
983 big: $(dom.contents()[3]).contents()[0],
984 cat: dom.contents()[4]
986 cursor = c.getCursor();
988 expect($(text.alice).text()).to.equal('Alice ');
989 expect($(text.has).text()).to.equal('has');
990 expect($(text.a).text()).to.equal(' a ');
991 expect($(text.big).text()).to.equal('big');
992 expect($(text.cat).text()).to.equal(' cat');
994 getSelection.returns({anchorNode: text.alice, focusNode: text.a});
995 expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "a" are children');
997 getSelection.returns({anchorNode: text.alice, focusNode: text.cat});
998 expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "cat" are children');
1000 getSelection.returns({anchorNode: text.alice, focusNode: text.has});
1001 expect(cursor.isSelectingSiblings()).to.equal(false, '"Alice" and "has" are not children');
1003 getSelection.returns({anchorNode: text.has, focusNode: text.big});
1004 expect(cursor.isSelectingSiblings()).to.equal(false, '"has" and "big" are not children');