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)).to.be.true;
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)).to.be.true;
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)).to.be.true;
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)).to.be.true;
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 var returnedValue = 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');
384 expect(returnedValue.first.sameNode(header1)).to.equal(true, 'first node returnde');
385 expect(returnedValue.second.sameNode(header2)).to.equal(true, 'second node returned');
388 it('leaves empty copy of DocumentNodeElement if splitting at the very beginning', function() {
389 var c = canvas.fromXML('<section><header>Some header</header></section>'),
391 text = section.children()[0].children()[0];
393 text.split({offset: 0});
395 var header1 = section.children()[0];
396 var header2 = section.children()[1];
398 expect(header1.children().length).to.equal(0);
399 expect(header2.children()[0].getText()).to.equal('Some header');
402 it('leaves empty copy of DocumentNodeElement if splitting at the very end', function() {
403 var c = canvas.fromXML('<section><header>Some header</header></section>'),
405 text = section.children()[0].children()[0];
407 text.split({offset: 11});
409 var header1 = section.children()[0];
410 var header2 = section.children()[1];
412 expect(header1.children()[0].getText()).to.equal('Some header');
413 expect(header2.children().length).to.equal(0);
416 it('keeps DocumentTextElement\'s parent\'s children elements intact', function() {
417 var c = canvas.fromXML('\
420 A <span>fancy</span> and <span>nice</span> header\
424 header = section.children()[0],
425 textAnd = header.children()[2];
427 textAnd.split({offset: 2});
429 var sectionChildren = section.children();
430 expect(sectionChildren.length).to.equal(2, 'Section has two children');
431 expect(sectionChildren[0].getWlxmlTag()).to.equal('header', 'First section element is a wlxml header');
432 expect(sectionChildren[1].getWlxmlTag()).to.equal('header', 'Second section element is a wlxml header');
434 var firstHeaderChildren = sectionChildren[0].children();
435 expect(firstHeaderChildren.length).to.equal(3, 'First header has three children');
436 expect(firstHeaderChildren[0].getText()).to.equal('A ', 'First header starts with a text');
437 expect(firstHeaderChildren[1].getWlxmlTag()).to.equal('span', 'First header has span in the middle');
438 expect(firstHeaderChildren[2].getText()).to.equal(' a', 'First header ends with text');
440 var secondHeaderChildren = sectionChildren[1].children();
441 expect(secondHeaderChildren.length).to.equal(3, 'Second header has three children');
442 expect(secondHeaderChildren[0].getText()).to.equal('nd ', 'Second header starts with text');
443 expect(secondHeaderChildren[1].getWlxmlTag()).to.equal('span', 'Second header has span in the middle');
444 expect(secondHeaderChildren[2].getText()).to.equal(' header', 'Second header ends with text');
448 describe('wrapping', function() {
449 it('wraps DocumentNodeElement', function() {
450 var c = canvas.fromXML('<section><div></div></section>'),
451 div = c.doc().children()[0];
453 var returned = div.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
454 parent = div.parent(),
455 parent2 = c.doc().children()[0];
457 expect(returned.sameNode(parent)).to.be.true;
458 expect(returned.sameNode(parent2)).to.be.true;
459 expect(returned.getWlxmlTag()).to.equal('header');
460 expect(returned.getWlxmlClass()).to.equal('some.class');
462 it('wraps DocumentTextElement', function() {
463 var c = canvas.fromXML('<section>Alice</section>'),
464 text = c.doc().children()[0];
466 var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
467 parent = text.parent(),
468 parent2 = c.doc().children()[0];
470 expect(returned.sameNode(parent)).to.be.true;
471 expect(returned.sameNode(parent2)).to.be.true;
472 expect(returned.getWlxmlTag()).to.equal('header');
473 expect(returned.getWlxmlClass()).to.equal('some.class');
476 describe('wrapping part of DocumentTextElement', function() {
477 [{start: 5, end: 12}, {start: 12, end: 5}].forEach(function(offsets) {
478 it('wraps in the middle ' + offsets.start + '/' + offsets.end, function() {
479 var c = canvas.fromXML('<section>Alice has a cat</section>'),
480 text = c.doc().children()[0];
482 var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class', start: offsets.start, end: offsets.end}),
483 children = c.doc().children();
485 expect(children.length).to.equal(3);
487 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
488 expect(children[0].getText()).to.equal('Alice');
490 expect(children[1].sameNode(returned)).to.be.true;
491 expect(returned.getWlxmlTag()).to.equal('header');
492 expect(returned.getWlxmlClass()).to.equal('some.class');
493 expect(children[1].children().length).to.equal(1);
494 expect(children[1].children()[0].getText()).to.equal(' has a ');
496 expect(children[2]).to.be.instanceOf(documentElement.DocumentTextElement);
497 expect(children[2].getText()).to.equal('cat');
501 it('wraps whole text inside DocumentTextElement if offsets span entire content', function() {
502 var c = canvas.fromXML('<section>Alice has a cat</section>'),
503 text = c.doc().children()[0];
505 var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class', start: 0, end: 15}),
506 children = c.doc().children();
508 expect(children.length).to.equal(1);
509 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
510 expect(children[0].children()[0].getText()).to.equal('Alice has a cat');
514 it('wraps text spanning multiple sibling DocumentTextNodes', function() {
515 var c = canvas.fromXML('<section>Alice has a <span>small</span> cat</section>'),
517 wrapper = c.wrapText({
519 _with: {tag: 'span', klass: 'some.class'},
525 expect(section.children().length).to.equal(2);
526 expect(section.children()[0]).to.be.instanceOf(documentElement.DocumentTextElement);
527 expect(section.children()[0].getText()).to.equal('Alice ');
529 var wrapper2 = section.children()[1];
530 expect(wrapper2.sameNode(wrapper)).to.be.true;
532 var wrapperChildren = wrapper.children();
533 expect(wrapperChildren.length).to.equal(3);
534 expect(wrapperChildren[0].getText()).to.equal('has a ');
536 expect(wrapperChildren[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
537 expect(wrapperChildren[1].children().length).to.equal(1);
538 expect(wrapperChildren[1].children()[0].getText()).to.equal('small');
540 expect(wrapperChildren[2].getText()).to.equal(' cat');
544 describe('unwrapping', function() {
545 it('unwraps DocumentTextElement from its parent DocumentNodeElement if it\'s its only child', function() {
546 var c = canvas.fromXML('<section>Alice <span>has a</span> cat</section>'),
548 text = section.children()[1].children()[0];
552 expect(section.children().length).to.equal(1, 'section has one child');
553 expect(section.children()[0].getText()).to.equal('Alice has a cat');
558 describe('Lists api', function() {
559 describe('creating lists', function() {
560 it('allows creation of a list from existing sibling DocumentElements', function() {
561 var c = canvas.fromXML('\
569 textHas = section.children()[1],
570 divA = section.children()[2]
572 c.list.create({element1: textHas, element2: divA});
574 expect(section.children().length).to.equal(3, 'section has three child elements');
576 var child1 = section.children()[0],
577 list = section.children()[1],
578 child3 = section.children()[2];
580 expect(child1.getText()).to.equal('Alice');
581 expect(list.is('list')).to.equal(true, 'second child is a list');
582 expect(list.children().length).to.equal(2, 'list contains two elements');
583 list.children().forEach(function(child) {
584 expect(child.getWlxmlClass()).to.equal('item', 'list childs have wlxml class of item');
586 expect(child3.children()[0].getText()).to.equal('cat');
589 it('allows creating nested list from existing sibling list items', function() {
590 var c = canvas.fromXML('\
592 <div class="list-items">\
593 <div class="item">A</div>\
594 <div class="item">B</div>\
595 <div class="item">C</div>\
596 <div class="item">D</div>\
599 outerList = c.doc().children()[0],
600 itemB = outerList.children()[1],
601 itemC = outerList.children()[2];
604 c.list.create({element1: itemB, element2: itemC});
606 var outerListItems = outerList.children(),
607 innerList = outerListItems[1].children()[0],
608 innerListItems = innerList.children();
610 expect(outerListItems.length).to.equal(3, 'outer list has three items');
611 expect(outerListItems[0].children()[0].getText()).to.equal('A', 'first outer item ok');
612 expect(outerListItems[1].getWlxmlClass()).to.equal('item', 'inner list is wrapped by item element');
614 expect(innerList.is('list')).to.equal(true, 'inner list created');
615 expect(innerListItems.length).to.equal(2, 'inner list has two items');
616 expect(innerListItems[0].children()[0].getText()).to.equal('B', 'first inner item ok');
617 expect(innerListItems[1].children()[0].getText()).to.equal('C', 'second inner item ok');
619 expect(outerListItems[2].children()[0].getText()).to.equal('D', 'last outer item ok');
625 describe('extracting list items', function() {
626 it('creates two lists with extracted items in the middle if extracting from the middle of the list', function() {
627 var c = canvas.fromXML('\
629 <div class="list.items">\
630 <div class="item">0</div>\
631 <div class="item">1</div>\
632 <div class="item">2</div>\
633 <div class="item">3</div>\
636 list = c.doc().children()[0],
637 item1 = list.children()[1],
638 item2 = list.children()[2];
640 c.list.extractItems({element1: item1, element2: item2});
642 var section = c.doc(),
643 list1 = section.children()[0],
644 oldItem1 = section.children()[1],
645 oldItem2 = section.children()[2],
646 list2 = section.children()[3];
648 expect(section.children().length).to.equal(4, 'section contains four children');
650 expect(list1.is('list')).to.equal(true, 'first section child is a list');
651 expect(list1.children().length).to.equal(1, 'first list has one child');
652 expect(list1.children()[0].children()[0].getText()).to.equal('0', 'first item of the first list is a first item of the original list');
654 expect(oldItem1.children()[0].getText()).to.equal('1', 'first item got extracted');
655 expect(oldItem1.getWlxmlClass() === undefined).to.equal(true, 'first extracted element has no wlxml class');
657 expect(oldItem2.children()[0].getText()).to.equal('2', 'second item got extracted');
658 expect(oldItem2.getWlxmlClass() === undefined).to.equal(true, 'second extracted element has no wlxml class');
660 expect(list2.is('list')).to.equal(true, 'last section child is a list');
661 expect(list2.children().length).to.equal(1, 'second list has one child');
662 expect(list2.children()[0].children()[0].getText()).to.equal('3', 'first item of the second list is a last item of the original list');
665 it('puts extracted items above the list if starting item is the first one', function() {
666 var c = canvas.fromXML('\
668 <div class="list.items">\
669 <div class="item">0</div>\
670 <div class="item">1</div>\
671 <div class="item">2</div>\
674 list = c.doc().children()[0],
675 item1 = list.children()[0],
676 item2 = list.children()[1],
677 item3 = list.children()[2];
679 c.list.extractItems({element1: item1, element2: item2});
681 var section = c.doc(),
682 oldItem1 = section.children()[0],
683 oldItem2 = section.children()[1],
684 newList = section.children()[2];
686 expect(section.children().length).to.equal(3, 'section has three children');
687 expect(oldItem1.children()[0].getText()).to.equal('0', 'first item extracted');
688 expect(oldItem2.children()[0].getText()).to.equal('1', 'second item extracted');
689 expect(newList.is('list')).to.equal(true, 'list lies below extracted item');
690 expect(newList.children().length).to.equal(1, 'list has now one child');
693 it('puts extracted items below the list if ending item is the last one', function() {
694 var c = canvas.fromXML('\
696 <div class="list.items">\
697 <div class="item">0</div>\
698 <div class="item">1</div>\
699 <div class="item">2</div>\
702 list = c.doc().children()[0],
703 item1 = list.children()[0],
704 item2 = list.children()[1],
705 item3 = list.children()[2];
707 c.list.extractItems({element1: item2, element2: item3});
709 var section = c.doc(),
710 oldItem1 = section.children()[1],
711 oldItem2 = section.children()[2],
712 newList = section.children()[0];
714 expect(section.children().length).to.equal(3, 'section has three children');
715 expect(oldItem1.children()[0].getText()).to.equal('1', 'first item extracted');
716 expect(oldItem2.children()[0].getText()).to.equal('2', 'second item extracted');
717 expect(newList.is('list')).to.equal(true, 'list lies above extracted item');
718 expect(newList.children().length).to.equal(1, 'list has now one child');
721 it('removes list if all its items are extracted', function() {
722 var c = canvas.fromXML('\
724 <div class="list.items">\
725 <div class="item">some item</div>\
726 <div class="item">some item 2</div>\
729 list = c.doc().children()[0],
730 item1 = list.children()[0],
731 item2 = list.children()[1];
733 c.list.extractItems({element1: item1, element2: item2});
735 var section = c.doc(),
736 list1 = section.children()[0],
737 oldItem1 = section.children()[0],
738 oldItem2 = section.children()[1];
740 expect(section.children().length).to.equal(2, 'section contains two children');
741 expect(oldItem1.children()[0].getText()).to.equal('some item');
742 expect(oldItem2.children()[0].getText()).to.equal('some item 2');
745 it('creates two lists with extracted items in the middle if extracting from the middle of the list - nested case' , function() {
746 var c = canvas.fromXML('\
748 <div class="list.items">\
749 <div class="item">0</div>\
751 <div class="list.items">\
752 <div class="item">1.1</div>\
753 <div class="item">1.2</div>\
754 <div class="item">1.3</div>\
757 <div class="item">2</div>\
760 list = c.doc().children()[0],
761 nestedList = list.children()[1].children()[0],
762 nestedListItem = nestedList.children()[1];
764 c.list.extractItems({element1: nestedListItem, element2: nestedListItem});
766 var section = c.doc(),
767 list = section.children()[0],
768 item1 = list.children()[0],
769 item2 = list.children()[1], //
770 item3 = list.children()[2],
771 item4 = list.children()[3], //
772 item5 = list.children()[4],
773 nestedList1 = item2.children()[0],
774 nestedList2 = item4.children()[0];
776 expect(list.children().length).to.equal(5, 'top list has five items');
778 expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
780 expect(item2.getWlxmlClass()).to.equal('item', 'first nested list is still wrapped in item element');
781 expect(nestedList1.children().length).to.equal(1, 'first nested list is left with one child');
782 expect(nestedList1.children()[0].children()[0].getText()).to.equal('1.1', 'first nested list item left alone');
784 expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
786 expect(item4.getWlxmlClass()).to.equal('item', 'second nested list is still wrapped in item element');
787 expect(nestedList2.children().length).to.equal(1, 'second nested list is left with one child');
788 expect(nestedList2.children()[0].children()[0].getText()).to.equal('1.3', 'second nested list item left alone');
790 expect(item5.children()[0].getText()).to.equal('2', 'last item ok');
793 it('puts extracted items below the list if ending item is the last 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()[1],
811 nestedListItem2 = nestedList.children()[2];
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 = item2.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.getWlxmlClass()).to.equal('item', 'nested list is still wrapped in item element');
827 expect(nestedList.children().length).to.equal(1, 'nested list is left with one child');
828 expect(nestedList.children()[0].children()[0].getText()).to.equal('1.1', 'nested list item left alone');
829 expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
830 expect(item4.children()[0].getText()).to.equal('1.3', 'fourth item ok');
831 expect(item5.children()[0].getText()).to.equal('2', 'fifth item ok');
834 it('puts extracted items above the list if starting item is the first one - nested case' , function() {
835 var c = canvas.fromXML('\
837 <div class="list.items">\
838 <div class="item">0</div>\
840 <div class="list.items">\
841 <div class="item">1.1</div>\
842 <div class="item">1.2</div>\
843 <div class="item">1.3</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],
862 item5 = list.children()[4];
863 nestedList = item4.children()[0];
865 expect(list.children().length).to.equal(5, 'top list has five items');
866 expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
867 expect(item2.children()[0].getText()).to.equal('1.1', 'second item ok');
868 expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
870 expect(item4.getWlxmlClass()).to.equal('item', 'nested list is still wrapped in item element');
871 expect(nestedList.children().length).to.equal(1, 'nested list is left with one child');
872 expect(nestedList.children()[0].children()[0].getText()).to.equal('1.3', 'nested list item left alone');
873 expect(item5.children()[0].getText()).to.equal('2', 'fifth item ok');
876 it('removes list if all its items are extracted - nested case', function() {
877 var c = canvas.fromXML('\
879 <div class="list.items">\
880 <div class="item">0</div>\
882 <div class="list.items">\
883 <div class="item">1.1</div>\
884 <div class="item">1.2</div>\
887 <div class="item">2</div>\
890 list = c.doc().children()[0],
891 nestedList = list.children()[1].children()[0],
892 nestedListItem1 = nestedList.children()[0],
893 nestedListItem2 = nestedList.children()[1];
895 c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
897 var section = c.doc(),
898 list = section.children()[0],
899 item1 = list.children()[0],
900 item2 = list.children()[1],
901 item3 = list.children()[2],
902 item4 = list.children()[3];
904 expect(list.children().length).to.equal(4, 'top list has four items');
905 expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
906 expect(item2.children()[0].getText()).to.equal('1.1', 'second item ok');
907 expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
908 expect(item4.children()[0].getText()).to.equal('2', 'fourth item ok');
911 it('extracts items out of outer most list when merge flag is set to false', function() {
912 var c = canvas.fromXML('\
914 <div class="list.items">\
915 <div class="item">0</div>\
917 <div class="list.items">\
918 <div class="item">1.1</div>\
919 <div class="item">1.2</div>\
922 <div class="item">2</div>\
926 list = section.children()[0],
927 nestedList = list.children()[1].children()[0],
928 nestedListItem = nestedList.children()[0];
930 var test = c.list.extractItems({element1: nestedListItem, element2: nestedListItem, merge: false});
932 expect(test).to.equal(true, 'extraction status ok');
934 var sectionChildren = section.children(),
935 extractedItem = sectionChildren[1];
937 expect(sectionChildren.length).to.equal(3, 'section has three children');
938 expect(sectionChildren[0].is('list')).to.equal(true, 'first child is a list');
940 expect(extractedItem.getWlxmlTag()).to.equal('div', 'extracted item is a wlxml div');
941 expect(extractedItem.getWlxmlClass()).to.equal(undefined, 'extracted item has no wlxml class');
942 expect(extractedItem.children()[0].getText()).to.equal('1.1', 'extracted item ok');
943 expect(sectionChildren[2].is('list')).to.equal(true, 'second child is a list');
950 describe('Cursor', function() {
954 beforeEach(function() {
955 getSelection = sinon.stub(window, 'getSelection');
958 afterEach(function() {
959 getSelection.restore();
962 it('returns position when browser selection collapsed', function() {
963 var c = canvas.fromXML('<section>Alice has a cat</section>'),
965 text = $(dom.contents()[0]).contents()[0];
967 expect(text.nodeType).to.equal(Node.TEXT_NODE, 'correct node selected');
968 expect($(text).text()).to.equal('Alice has a cat');
970 getSelection.returns({
977 var cursor = c.getCursor(),
978 position = cursor.getPosition();
980 expect(cursor.isSelecting()).to.equal(false, 'cursor is not selecting anything');
981 expect(position.element.getText()).to.equal('Alice has a cat');
982 expect(position.offset).to.equal(5);
983 expect(position.offsetAtEnd).to.equal(false, 'offset is not at end');
985 getSelection.returns({
993 expect(cursor.getPosition().offsetAtEnd).to.equal(true, 'offset at end');
996 it('returns boundries of selection when browser selection not collapsed', function() {
997 var c = canvas.fromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
1000 alice: dom.contents()[0],
1001 has: $(dom.contents()[1]).contents()[0],
1002 cat: dom.contents()[4]
1004 cursor = c.getCursor(),
1005 aliceElement = c.getDocumentElement(text.alice),
1006 catElement = c.getDocumentElement(text.cat);
1010 {focus: text.alice, focusOffset: 1, anchor: text.cat, anchorOffset: 2, selectionAnchor: catElement},
1011 {focus: text.cat, focusOffset: 2, anchor: text.alice, anchorOffset: 1, selectionAnchor: aliceElement}
1012 ].forEach(function(s, idx) {
1013 getSelection.returns({isColapsed: false, anchorNode: s.anchor, anchorOffset: s.anchorOffset, focusNode: s.focus, focusOffset: s.focusOffset});
1015 var selectionStart = cursor.getSelectionStart(),
1016 selectionEnd = cursor.getSelectionEnd(),
1017 selectionAnchor = cursor.getSelectionAnchor();
1019 expect(cursor.isSelecting()).to.equal(true, 'cursor is selecting');
1020 expect(selectionStart.element.sameNode(aliceElement)).to.equal(true, '"Alice" is the start of the selection ' + idx);
1021 expect(selectionStart.offset).to.equal(1, '"Alice" offset ok' + idx);
1022 expect(selectionEnd.element.sameNode(catElement)).to.equal(true, '"Cat" is the start of the selection ' + idx);
1023 expect(selectionEnd.offset).to.equal(2, '"Cat" offset ok' + idx);
1024 expect(selectionAnchor.element.sameNode(s.selectionAnchor)).to.equal(true, 'anchor ok');
1025 expect(selectionAnchor.offset).to.equal(s.anchorOffset, 'anchor offset ok');
1029 it('recognizes when browser selection boundries lies in sibling DocumentTextElements', function() {
1030 var c = canvas.fromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
1031 dom = c.doc().dom(),
1033 alice: dom.contents()[0],
1034 has: $(dom.contents()[1]).contents()[0],
1035 a: dom.contents()[2],
1036 big: $(dom.contents()[3]).contents()[0],
1037 cat: dom.contents()[4]
1039 cursor = c.getCursor();
1041 expect($(text.alice).text()).to.equal('Alice ');
1042 expect($(text.has).text()).to.equal('has');
1043 expect($(text.a).text()).to.equal(' a ');
1044 expect($(text.big).text()).to.equal('big');
1045 expect($(text.cat).text()).to.equal(' cat');
1047 getSelection.returns({anchorNode: text.alice, focusNode: text.a});
1048 expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "a" are children');
1050 getSelection.returns({anchorNode: text.alice, focusNode: text.cat});
1051 expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "cat" are children');
1053 getSelection.returns({anchorNode: text.alice, focusNode: text.has});
1054 expect(cursor.isSelectingSiblings()).to.equal(false, '"Alice" and "has" are not children');
1056 getSelection.returns({anchorNode: text.has, focusNode: text.big});
1057 expect(cursor.isSelectingSiblings()).to.equal(false, '"has" and "big" are not children');