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');
271 describe('getting vertically first text element', function() {
272 it('returns the first child if it\'s text element, ignores metadata', function() {
273 var c = canvas.fromXML('<section><metadata><dc:author>author</dc:author></metadata>Alice<div>has</div>a cat</section>'),
274 first = c.doc().getVerticallyFirstTextElement();
276 expect(first.sameNode(c.doc().children()[1])).to.be.true;
279 it('looks recursively inside node elements if they precede text element', function() {
280 var c = canvas.fromXML('\
289 textAlice = c.doc().children()[0].children()[0].children()[0],
290 first = c.doc().getVerticallyFirstTextElement();
292 expect(textAlice).to.be.instanceOf(documentElement.DocumentTextElement);
293 expect(first.sameNode(textAlice)).to.be.true;
298 describe('manipulation api', function() {
300 describe('Basic Element inserting', function() {
301 it('can put new NodeElement at the end', function() {
302 var c = canvas.fromXML('<section></section>'),
303 appended = c.doc().append({tag: 'header', klass: 'some.class'}),
304 children = c.doc().children();
306 expect(children.length).to.equal(1);
307 expect(children[0].sameNode(appended));
310 it('can put new TextElement at the end', function() {
311 var c = canvas.fromXML('<section></section>'),
312 appended = c.doc().append({text: 'Alice'}),
313 children = c.doc().children();
315 expect(children.length).to.equal(1);
316 expect(children[0].sameNode(appended));
317 expect(children[0].getText()).to.equal('Alice');
320 it('can put new NodeElement after another NodeElement', function() {
321 var c = canvas.fromXML('<section><div></div></section>'),
322 div = c.doc().children()[0],
323 added = div.after({tag: 'header', klass: 'some.class'}),
324 children = c.doc().children();
325 expect(children.length).to.equal(2);
326 expect(children[1].sameNode(added));
329 it('can put new Nodeelement before another element', function() {
330 var c = canvas.fromXML('<section><div></div></section>'),
331 div = c.doc().children()[0],
332 added = div.before({tag: 'header', klass: 'some.class'}),
333 children = c.doc().children();
334 expect(children.length).to.equal(2);
335 expect(children[0].sameNode(added));
338 it('can put new DocumentNodeElement after DocumentTextElement', function() {
339 var c = canvas.fromXML('<section>Alice</section>'),
340 text = c.doc().children()[0],
341 added = text.after({tag: 'p'}),
342 children = c.doc().children();
344 expect(children.length).to.equal(2);
345 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
346 expect(children[0].getText()).to.equal('Alice');
347 expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
348 expect(children[1].sameNode(added)).to.be.true;
350 it('can put new DocumentNodeElement before DocumentTextElement', function() {
351 var c = canvas.fromXML('<section>Alice</section>'),
352 text = c.doc().children()[0],
353 added = text.before({tag: 'p'}),
354 children = c.doc().children();
356 expect(children.length).to.equal(2);
357 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
358 expect(children[0].sameNode(added)).to.be.true;
359 expect(children[1]).to.be.instanceOf(documentElement.DocumentTextElement);
360 expect(children[1].getText()).to.equal('Alice');
364 describe('Splitting text', function() {
366 it('splits DocumentTextElement\'s parent into two DocumentNodeElements of the same type', function() {
367 var c = canvas.fromXML('<section><header>Some header</header></section>'),
369 text = section.children()[0].children()[0];
371 text.split({offset: 5});
372 expect(section.children().length).to.equal(2, 'section has two children');
374 var header1 = section.children()[0];
375 var header2 = section.children()[1];
377 expect(header1.getWlxmlTag()).to.equal('header', 'first section child represents wlxml header');
378 expect(header1.children().length).to.equal(1, 'first header has one text child');
379 expect(header1.children()[0].getText()).to.equal('Some ', 'first header has correct content');
380 expect(header2.getWlxmlTag()).to.equal('header', 'second section child represents wlxml header');
381 expect(header2.children().length).to.equal(1, 'second header has one text child');
382 expect(header2.children()[0].getText()).to.equal('header', 'second header has correct content');
385 it('leaves empty copy of DocumentNodeElement if splitting at the very beginning', function() {
386 var c = canvas.fromXML('<section><header>Some header</header></section>'),
388 text = section.children()[0].children()[0];
390 text.split({offset: 0});
392 var header1 = section.children()[0];
393 var header2 = section.children()[1];
395 expect(header1.children().length).to.equal(0);
396 expect(header2.children()[0].getText()).to.equal('Some header');
399 it('leaves empty copy of DocumentNodeElement if splitting at the very end', function() {
400 var c = canvas.fromXML('<section><header>Some header</header></section>'),
402 text = section.children()[0].children()[0];
404 text.split({offset: 11});
406 var header1 = section.children()[0];
407 var header2 = section.children()[1];
409 expect(header1.children()[0].getText()).to.equal('Some header');
410 expect(header2.children().length).to.equal(0);
413 it('keeps DocumentTextElement\'s parent\'s children elements intact', function() {
414 var c = canvas.fromXML('\
417 A <span>fancy</span> and <span>nice</span> header\
421 header = section.children()[0],
422 textAnd = header.children()[2];
424 textAnd.split({offset: 2});
426 var sectionChildren = section.children();
427 expect(sectionChildren.length).to.equal(2, 'Section has two children');
428 expect(sectionChildren[0].getWlxmlTag()).to.equal('header', 'First section element is a wlxml header');
429 expect(sectionChildren[1].getWlxmlTag()).to.equal('header', 'Second section element is a wlxml header');
431 var firstHeaderChildren = sectionChildren[0].children();
432 expect(firstHeaderChildren.length).to.equal(3, 'First header has three children');
433 expect(firstHeaderChildren[0].getText()).to.equal('A ', 'First header starts with a text');
434 expect(firstHeaderChildren[1].getWlxmlTag()).to.equal('span', 'First header has span in the middle');
435 expect(firstHeaderChildren[2].getText()).to.equal(' a', 'First header ends with text');
437 var secondHeaderChildren = sectionChildren[1].children();
438 expect(secondHeaderChildren.length).to.equal(3, 'Second header has three children');
439 expect(secondHeaderChildren[0].getText()).to.equal('nd ', 'Second header starts with text');
440 expect(secondHeaderChildren[1].getWlxmlTag()).to.equal('span', 'Second header has span in the middle');
441 expect(secondHeaderChildren[2].getText()).to.equal(' header', 'Second header ends with text');
445 describe('wrapping', function() {
446 it('wraps DocumentNodeElement', function() {
447 var c = canvas.fromXML('<section><div></div></section>'),
448 div = c.doc().children()[0];
450 var returned = div.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
451 parent = div.parent(),
452 parent2 = c.doc().children()[0];
454 expect(returned.sameNode(parent)).to.be.true;
455 expect(returned.sameNode(parent2)).to.be.true;
456 expect(returned.getWlxmlTag()).to.equal('header');
457 expect(returned.getWlxmlClass()).to.equal('some.class');
459 it('wraps DocumentTextElement', function() {
460 var c = canvas.fromXML('<section>Alice</section>'),
461 text = c.doc().children()[0];
463 var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
464 parent = text.parent(),
465 parent2 = c.doc().children()[0];
467 expect(returned.sameNode(parent)).to.be.true;
468 expect(returned.sameNode(parent2)).to.be.true;
469 expect(returned.getWlxmlTag()).to.equal('header');
470 expect(returned.getWlxmlClass()).to.equal('some.class');
473 describe('wrapping part of DocumentTextElement', function() {
474 [{start: 5, end: 12}, {start: 12, end: 5}].forEach(function(offsets) {
475 it('wraps in the middle ' + offsets.start + '/' + offsets.end, function() {
476 var c = canvas.fromXML('<section>Alice has a cat</section>'),
477 text = c.doc().children()[0];
479 var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class', start: offsets.start, end: offsets.end}),
480 children = c.doc().children();
482 expect(children.length).to.equal(3);
484 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
485 expect(children[0].getText()).to.equal('Alice');
487 expect(children[1].sameNode(returned)).to.be.true;
488 expect(returned.getWlxmlTag()).to.equal('header');
489 expect(returned.getWlxmlClass()).to.equal('some.class');
490 expect(children[1].children().length).to.equal(1);
491 expect(children[1].children()[0].getText()).to.equal(' has a ');
493 expect(children[2]).to.be.instanceOf(documentElement.DocumentTextElement);
494 expect(children[2].getText()).to.equal('cat');
498 it('wraps whole text inside DocumentTextElement if offsets span entire content', function() {
499 var c = canvas.fromXML('<section>Alice has a cat</section>'),
500 text = c.doc().children()[0];
502 var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class', start: 0, end: 15}),
503 children = c.doc().children();
505 expect(children.length).to.equal(1);
506 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
507 expect(children[0].children()[0].getText()).to.equal('Alice has a cat');
511 it('wraps text spanning multiple sibling DocumentTextNodes', function() {
512 var c = canvas.fromXML('<section>Alice has a <span>small</span> cat</section>'),
514 wrapper = c.wrapText({
516 _with: {tag: 'span', klass: 'some.class'},
522 expect(section.children().length).to.equal(2);
523 expect(section.children()[0]).to.be.instanceOf(documentElement.DocumentTextElement);
524 expect(section.children()[0].getText()).to.equal('Alice ');
526 var wrapper2 = section.children()[1];
527 expect(wrapper2.sameNode(wrapper)).to.be.true;
529 var wrapperChildren = wrapper.children();
530 expect(wrapperChildren.length).to.equal(3);
531 expect(wrapperChildren[0].getText()).to.equal('has a ');
533 expect(wrapperChildren[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
534 expect(wrapperChildren[1].children().length).to.equal(1);
535 expect(wrapperChildren[1].children()[0].getText()).to.equal('small');
537 expect(wrapperChildren[2].getText()).to.equal(' cat');
541 describe('unwrapping', function() {
542 it('unwraps DocumentTextElement from its parent DocumentNodeElement if it\'s its only child', function() {
543 var c = canvas.fromXML('<section>Alice <span>has a</span> cat</section>'),
545 text = section.children()[1].children()[0];
549 expect(section.children().length).to.equal(1, 'section has one child');
550 expect(section.children()[0].getText()).to.equal('Alice has a cat');
555 describe('Lists api', function() {
556 describe('creating lists', function() {
557 it('allows creation of a list from existing sibling DocumentElements', function() {
558 var c = canvas.fromXML('\
566 textHas = section.children()[1],
567 divA = section.children()[2]
569 c.list.create({element1: textHas, element2: divA});
571 expect(section.children().length).to.equal(3, 'section has three child elements');
573 var child1 = section.children()[0],
574 list = section.children()[1],
575 child3 = section.children()[2];
577 expect(child1.getText()).to.equal('Alice');
578 expect(list.is('list')).to.equal(true, 'second child is a list');
579 expect(list.children().length).to.equal(2, 'list contains two elements');
580 list.children().forEach(function(child) {
581 expect(child.getWlxmlClass()).to.equal('item', 'list childs have wlxml class of item');
583 expect(child3.children()[0].getText()).to.equal('cat');
586 it('allows creating nested list from existing sibling list items', function() {
587 var c = canvas.fromXML('\
589 <div class="list-items">\
590 <div class="item">A</div>\
591 <div class="item">B</div>\
592 <div class="item">C</div>\
593 <div class="item">D</div>\
596 outerList = c.doc().children()[0],
597 itemB = outerList.children()[1],
598 itemC = outerList.children()[2];
601 c.list.create({element1: itemB, element2: itemC});
603 var outerListItems = outerList.children(),
604 innerList = outerListItems[1].children()[0],
605 innerListItems = innerList.children();
607 expect(outerListItems.length).to.equal(3, 'outer list has three items');
608 expect(outerListItems[0].children()[0].getText()).to.equal('A', 'first outer item ok');
609 expect(outerListItems[1].getWlxmlClass()).to.equal('item', 'inner list is wrapped by item element');
611 expect(innerList.is('list')).to.equal(true, 'inner list created');
612 expect(innerListItems.length).to.equal(2, 'inner list has two items');
613 expect(innerListItems[0].children()[0].getText()).to.equal('B', 'first inner item ok');
614 expect(innerListItems[1].children()[0].getText()).to.equal('C', 'second inner item ok');
616 expect(outerListItems[2].children()[0].getText()).to.equal('D', 'last outer item ok');
622 describe('extracting list items', function() {
623 it('creates two lists with extracted items in the middle if extracting from the middle of the list', function() {
624 var c = canvas.fromXML('\
626 <div class="list.items">\
627 <div class="item">0</div>\
628 <div class="item">1</div>\
629 <div class="item">2</div>\
630 <div class="item">3</div>\
633 list = c.doc().children()[0],
634 item1 = list.children()[1],
635 item2 = list.children()[2];
637 c.list.extractItems({element1: item1, element2: item2});
639 var section = c.doc(),
640 list1 = section.children()[0],
641 oldItem1 = section.children()[1],
642 oldItem2 = section.children()[2],
643 list2 = section.children()[3];
645 expect(section.children().length).to.equal(4, 'section contains four children');
647 expect(list1.is('list')).to.equal(true, 'first section child is a list');
648 expect(list1.children().length).to.equal(1, 'first list has one child');
649 expect(list1.children()[0].children()[0].getText()).to.equal('0', 'first item of the first list is a first item of the original list');
651 expect(oldItem1.children()[0].getText()).to.equal('1', 'first item got extracted');
652 expect(oldItem1.getWlxmlClass() === undefined).to.equal(true, 'first extracted element has no wlxml class');
654 expect(oldItem2.children()[0].getText()).to.equal('2', 'second item got extracted');
655 expect(oldItem2.getWlxmlClass() === undefined).to.equal(true, 'second extracted element has no wlxml class');
657 expect(list2.is('list')).to.equal(true, 'last section child is a list');
658 expect(list2.children().length).to.equal(1, 'second list has one child');
659 expect(list2.children()[0].children()[0].getText()).to.equal('3', 'first item of the second list is a last item of the original list');
662 it('puts extracted items above the list if starting item is the first one', function() {
663 var c = canvas.fromXML('\
665 <div class="list.items">\
666 <div class="item">0</div>\
667 <div class="item">1</div>\
668 <div class="item">2</div>\
671 list = c.doc().children()[0],
672 item1 = list.children()[0],
673 item2 = list.children()[1],
674 item3 = list.children()[2];
676 c.list.extractItems({element1: item1, element2: item2});
678 var section = c.doc(),
679 oldItem1 = section.children()[0],
680 oldItem2 = section.children()[1],
681 newList = section.children()[2];
683 expect(section.children().length).to.equal(3, 'section has three children');
684 expect(oldItem1.children()[0].getText()).to.equal('0', 'first item extracted');
685 expect(oldItem2.children()[0].getText()).to.equal('1', 'second item extracted');
686 expect(newList.is('list')).to.equal(true, 'list lies below extracted item');
687 expect(newList.children().length).to.equal(1, 'list has now one child');
690 it('puts extracted items below the list if ending item is the last one', function() {
691 var c = canvas.fromXML('\
693 <div class="list.items">\
694 <div class="item">0</div>\
695 <div class="item">1</div>\
696 <div class="item">2</div>\
699 list = c.doc().children()[0],
700 item1 = list.children()[0],
701 item2 = list.children()[1],
702 item3 = list.children()[2];
704 c.list.extractItems({element1: item2, element2: item3});
706 var section = c.doc(),
707 oldItem1 = section.children()[1],
708 oldItem2 = section.children()[2],
709 newList = section.children()[0];
711 expect(section.children().length).to.equal(3, 'section has three children');
712 expect(oldItem1.children()[0].getText()).to.equal('1', 'first item extracted');
713 expect(oldItem2.children()[0].getText()).to.equal('2', 'second item extracted');
714 expect(newList.is('list')).to.equal(true, 'list lies above extracted item');
715 expect(newList.children().length).to.equal(1, 'list has now one child');
718 it('removes list if all its items are extracted', function() {
719 var c = canvas.fromXML('\
721 <div class="list.items">\
722 <div class="item">some item</div>\
723 <div class="item">some item 2</div>\
726 list = c.doc().children()[0],
727 item1 = list.children()[0],
728 item2 = list.children()[1];
730 c.list.extractItems({element1: item1, element2: item2});
732 var section = c.doc(),
733 list1 = section.children()[0],
734 oldItem1 = section.children()[0],
735 oldItem2 = section.children()[1];
737 expect(section.children().length).to.equal(2, 'section contains two children');
738 expect(oldItem1.children()[0].getText()).to.equal('some item');
739 expect(oldItem2.children()[0].getText()).to.equal('some item 2');
742 it('creates two lists with extracted items in the middle if extracting from the middle of the list - nested case' , function() {
743 var c = canvas.fromXML('\
745 <div class="list.items">\
746 <div class="item">0</div>\
748 <div class="list.items">\
749 <div class="item">1.1</div>\
750 <div class="item">1.2</div>\
751 <div class="item">1.3</div>\
754 <div class="item">2</div>\
757 list = c.doc().children()[0],
758 nestedList = list.children()[1].children()[0],
759 nestedListItem = nestedList.children()[1];
761 c.list.extractItems({element1: nestedListItem, element2: nestedListItem});
763 var section = c.doc(),
764 list = section.children()[0],
765 item1 = list.children()[0],
766 item2 = list.children()[1], //
767 item3 = list.children()[2],
768 item4 = list.children()[3], //
769 item5 = list.children()[4],
770 nestedList1 = item2.children()[0],
771 nestedList2 = item4.children()[0];
773 expect(list.children().length).to.equal(5, 'top list has five items');
775 expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
777 expect(item2.getWlxmlClass()).to.equal('item', 'first nested list is still wrapped in item element');
778 expect(nestedList1.children().length).to.equal(1, 'first nested list is left with one child');
779 expect(nestedList1.children()[0].children()[0].getText()).to.equal('1.1', 'first nested list item left alone');
781 expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
783 expect(item4.getWlxmlClass()).to.equal('item', 'second nested list is still wrapped in item element');
784 expect(nestedList2.children().length).to.equal(1, 'second nested list is left with one child');
785 expect(nestedList2.children()[0].children()[0].getText()).to.equal('1.3', 'second nested list item left alone');
787 expect(item5.children()[0].getText()).to.equal('2', 'last item ok');
790 it('puts extracted items below the list if ending item is the last one - nested case' , function() {
791 var c = canvas.fromXML('\
793 <div class="list.items">\
794 <div class="item">0</div>\
796 <div class="list.items">\
797 <div class="item">1.1</div>\
798 <div class="item">1.2</div>\
799 <div class="item">1.3</div>\
802 <div class="item">2</div>\
805 list = c.doc().children()[0],
806 nestedList = list.children()[1].children()[0],
807 nestedListItem1 = nestedList.children()[1],
808 nestedListItem2 = nestedList.children()[2];
810 c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
812 var section = c.doc(),
813 list = section.children()[0],
814 item1 = list.children()[0],
815 item2 = list.children()[1],
816 item3 = list.children()[2],
817 item4 = list.children()[3],
818 item5 = list.children()[4];
819 nestedList = item2.children()[0];
821 expect(list.children().length).to.equal(5, 'top list has five items');
822 expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
823 expect(item2.getWlxmlClass()).to.equal('item', 'nested list is still wrapped in item element');
824 expect(nestedList.children().length).to.equal(1, 'nested list is left with one child');
825 expect(nestedList.children()[0].children()[0].getText()).to.equal('1.1', 'nested list item left alone');
826 expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
827 expect(item4.children()[0].getText()).to.equal('1.3', 'fourth item ok');
828 expect(item5.children()[0].getText()).to.equal('2', 'fifth item ok');
831 it('puts extracted items above the list if starting item is the first one - nested case' , function() {
832 var c = canvas.fromXML('\
834 <div class="list.items">\
835 <div class="item">0</div>\
837 <div class="list.items">\
838 <div class="item">1.1</div>\
839 <div class="item">1.2</div>\
840 <div class="item">1.3</div>\
843 <div class="item">2</div>\
846 list = c.doc().children()[0],
847 nestedList = list.children()[1].children()[0],
848 nestedListItem1 = nestedList.children()[0],
849 nestedListItem2 = nestedList.children()[1];
851 c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
853 var section = c.doc(),
854 list = section.children()[0],
855 item1 = list.children()[0],
856 item2 = list.children()[1],
857 item3 = list.children()[2],
858 item4 = list.children()[3],
859 item5 = list.children()[4];
860 nestedList = item4.children()[0];
862 expect(list.children().length).to.equal(5, 'top list has five items');
863 expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
864 expect(item2.children()[0].getText()).to.equal('1.1', 'second item ok');
865 expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
867 expect(item4.getWlxmlClass()).to.equal('item', 'nested list is still wrapped in item element');
868 expect(nestedList.children().length).to.equal(1, 'nested list is left with one child');
869 expect(nestedList.children()[0].children()[0].getText()).to.equal('1.3', 'nested list item left alone');
870 expect(item5.children()[0].getText()).to.equal('2', 'fifth item ok');
873 it('removes list if all its items are extracted - nested case', function() {
874 var c = canvas.fromXML('\
876 <div class="list.items">\
877 <div class="item">0</div>\
879 <div class="list.items">\
880 <div class="item">1.1</div>\
881 <div class="item">1.2</div>\
884 <div class="item">2</div>\
887 list = c.doc().children()[0],
888 nestedList = list.children()[1].children()[0],
889 nestedListItem1 = nestedList.children()[0],
890 nestedListItem2 = nestedList.children()[1];
892 c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
894 var section = c.doc(),
895 list = section.children()[0],
896 item1 = list.children()[0],
897 item2 = list.children()[1],
898 item3 = list.children()[2],
899 item4 = list.children()[3];
901 expect(list.children().length).to.equal(4, 'top list has four items');
902 expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
903 expect(item2.children()[0].getText()).to.equal('1.1', 'second item ok');
904 expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
905 expect(item4.children()[0].getText()).to.equal('2', 'fourth item ok');
908 it('extracts items out of outer most list when merge flag is set to false', function() {
909 var c = canvas.fromXML('\
911 <div class="list.items">\
912 <div class="item">0</div>\
914 <div class="list.items">\
915 <div class="item">1.1</div>\
916 <div class="item">1.2</div>\
919 <div class="item">2</div>\
923 list = section.children()[0],
924 nestedList = list.children()[1].children()[0],
925 nestedListItem = nestedList.children()[0];
927 var test = c.list.extractItems({element1: nestedListItem, element2: nestedListItem, merge: false});
929 expect(test).to.equal(true, 'extraction status ok');
931 var sectionChildren = section.children(),
932 extractedItem = sectionChildren[1];
934 expect(sectionChildren.length).to.equal(3, 'section has three children');
935 expect(sectionChildren[0].is('list')).to.equal(true, 'first child is a list');
937 expect(extractedItem.getWlxmlTag()).to.equal('div', 'extracted item is a wlxml div');
938 expect(extractedItem.getWlxmlClass()).to.equal(undefined, 'extracted item has no wlxml class');
939 expect(extractedItem.children()[0].getText()).to.equal('1.1', 'extracted item ok');
940 expect(sectionChildren[2].is('list')).to.equal(true, 'second child is a list');
947 describe('Cursor', function() {
951 beforeEach(function() {
952 getSelection = sinon.stub(window, 'getSelection');
955 afterEach(function() {
956 getSelection.restore();
959 it('returns position when browser selection collapsed', function() {
960 var c = canvas.fromXML('<section>Alice has a cat</section>'),
962 text = $(dom.contents()[0]).contents()[0];
964 expect(text.nodeType).to.equal(Node.TEXT_NODE, 'correct node selected');
965 expect($(text).text()).to.equal('Alice has a cat');
967 getSelection.returns({
974 var cursor = c.getCursor(),
975 position = cursor.getPosition();
977 expect(cursor.isSelecting()).to.equal(false, 'cursor is not selecting anything');
978 expect(position.element.getText()).to.equal('Alice has a cat');
979 expect(position.offset).to.equal(5);
982 it('returns boundries of selection when browser selection not collapsed', function() {
983 var c = canvas.fromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
986 alice: dom.contents()[0],
987 has: $(dom.contents()[1]).contents()[0],
988 cat: dom.contents()[4]
990 cursor = c.getCursor(),
991 aliceElement = c.getDocumentElement(text.alice),
992 catElement = c.getDocumentElement(text.cat);
996 {focus: text.alice, focusOffset: 1, anchor: text.cat, anchorOffset: 2, selectionAnchor: catElement},
997 {focus: text.cat, focusOffset: 2, anchor: text.alice, anchorOffset: 1, selectionAnchor: aliceElement}
998 ].forEach(function(s, idx) {
999 getSelection.returns({isColapsed: false, anchorNode: s.anchor, anchorOffset: s.anchorOffset, focusNode: s.focus, focusOffset: s.focusOffset});
1001 var selectionStart = cursor.getSelectionStart(),
1002 selectionEnd = cursor.getSelectionEnd(),
1003 selectionAnchor = cursor.getSelectionAnchor();
1005 expect(cursor.isSelecting()).to.equal(true, 'cursor is selecting');
1006 expect(selectionStart.element.sameNode(aliceElement)).to.equal(true, '"Alice" is the start of the selection ' + idx);
1007 expect(selectionStart.offset).to.equal(1, '"Alice" offset ok' + idx);
1008 expect(selectionEnd.element.sameNode(catElement)).to.equal(true, '"Cat" is the start of the selection ' + idx);
1009 expect(selectionEnd.offset).to.equal(2, '"Cat" offset ok' + idx);
1010 expect(selectionAnchor.element.sameNode(s.selectionAnchor)).to.equal(true, 'anchor ok');
1011 expect(selectionAnchor.offset).to.equal(s.anchorOffset, 'anchor offset ok');
1015 it('recognizes when browser selection boundries lies in sibling DocumentTextElements', function() {
1016 var c = canvas.fromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
1017 dom = c.doc().dom(),
1019 alice: dom.contents()[0],
1020 has: $(dom.contents()[1]).contents()[0],
1021 a: dom.contents()[2],
1022 big: $(dom.contents()[3]).contents()[0],
1023 cat: dom.contents()[4]
1025 cursor = c.getCursor();
1027 expect($(text.alice).text()).to.equal('Alice ');
1028 expect($(text.has).text()).to.equal('has');
1029 expect($(text.a).text()).to.equal(' a ');
1030 expect($(text.big).text()).to.equal('big');
1031 expect($(text.cat).text()).to.equal(' cat');
1033 getSelection.returns({anchorNode: text.alice, focusNode: text.a});
1034 expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "a" are children');
1036 getSelection.returns({anchorNode: text.alice, focusNode: text.cat});
1037 expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "cat" are children');
1039 getSelection.returns({anchorNode: text.alice, focusNode: text.has});
1040 expect(cursor.isSelectingSiblings()).to.equal(false, '"Alice" and "has" are not children');
1042 getSelection.returns({anchorNode: text.has, focusNode: text.big});
1043 expect(cursor.isSelectingSiblings()).to.equal(false, '"has" and "big" are not children');