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.A"></section>'),
123 expect(section.getWlxmlClass()).to.equal('some.class.A');
124 section.setWlxmlClass('some.class.B');
125 expect(section.getWlxmlClass()).to.equal('some.class.B');
126 section.setWlxmlClass(null);
127 expect(section.getWlxmlClass()).to.be.undefined;
132 describe('element has meta attributes', function() {
133 it('can change its meta attributes', function() {
134 var c = canvas.fromXML('<section><span class="uri" meta-uri="someuri"></span></section>'),
135 span = c.doc().children()[0];
137 expect(span.getWlxmlMetaAttr('uri')).to.equal('someuri');
138 span.setWlxmlMetaAttr('uri', 'otheruri');
139 expect(span.getWlxmlMetaAttr('uri')).to.equal('otheruri');
142 it('changes its meta attributes with class change', function() {
143 var c = canvas.fromXML('<section><span class="uri" meta-uri="someuri"></span></section>'),
144 span = c.doc().children()[0];
146 expect(span.getWlxmlMetaAttr('uri')).to.equal('someuri');
147 span.setWlxmlClass('author');
148 expect(span.getWlxmlMetaAttr('uri')).to.be.undefined;
151 it('keeps meta attribute value on class change if a new class has this attribute', function() {
152 var c = canvas.fromXML('<section><span class="uri" meta-uri="someuri"></span></section>'),
153 span = c.doc().children()[0];
154 span.setWlxmlClass('uri.some.subclass');
155 expect(span.getWlxmlMetaAttr('uri')).to.equal('someuri');
160 it('returns DocumentNodeElement instance from HTMLElement', function() {
161 var c = canvas.fromXML('<section></section>'),
162 htmlElement = c.doc().dom().get(0),
163 element = c.getDocumentElement(htmlElement);
164 expect(element).to.be.instanceOf(documentElement.DocumentNodeElement);
165 expect(element.sameNode(c.doc()));
168 it('returns DocumentTextElement instance from Text Node', function() {
169 var c = canvas.fromXML('<section>Alice</section>'),
170 aliceElement = c.doc().children()[0],
171 textNode = aliceElement.dom().contents()[0],
172 element = c.getDocumentElement(textNode);
174 expect(textNode.nodeType).to.equal(Node.TEXT_NODE, 'text node selected');
175 expect($(textNode).text()).to.equal('Alice');
177 expect(element).to.be.instanceOf(documentElement.DocumentTextElement);
178 expect(element.sameNode(c.doc().children()[0]));
184 describe('document representation api', function() {
185 describe('document root element', function() {
186 var c = canvas.fromXML('<section></section>');
187 it('exists', function() {
188 expect(c.doc()).to.be.instanceOf(documentElement.DocumentElement);
190 it('is of type DocumentNodeElement', function() {
191 expect(c.doc()).to.be.instanceOf(documentElement.DocumentNodeElement);
195 describe('DocumentElements comparison', function() {
196 it('reports dwo DocumentElements to be the same when they represent the same wlxml document element', function() {
197 var c = canvas.fromXML('<section><div></div><div></div></section>'),
198 first_div1 = c.doc().children()[0],
199 first_div2 = c.doc().children()[0],
200 second_div = c.doc().children()[1];
201 expect(first_div1.sameNode(first_div1)).to.be.true;
202 expect(first_div1.sameNode(first_div2)).to.be.true;
203 expect(first_div1.sameNode(second_div)).to.be.false;
207 describe('traversing', function() {
208 it('reports element nodes', function() {
209 var c = canvas.fromXML('<section><div></div></section>'),
210 children = c.doc().children();
211 expect(children.length).to.equal(1);
212 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
214 c = canvas.fromXML('<section><div></div><div></div></section>'),
215 children = c.doc().children();
216 expect(children.length).to.equal(2);
217 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
218 expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
221 it('reports text nodes', function() {
222 var c = canvas.fromXML('<section>Alice</section>'),
223 children = c.doc().children();
224 expect(children.length).to.equal(1);
225 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
228 describe('accessing parents', function() {
229 it('returns DocumentNodeElement representing parent in wlxml document as DocumentNodeElement parent', function() {
230 var c = canvas.fromXML('<section><div></div></section>'),
231 div = c.doc().children()[0];
232 expect(div.parent().sameNode(c.doc())).to.be.true;
234 it('returns DocumentNodeElement representing parent in wlxml document as DocumentTextElement parent', function() {
235 var c = canvas.fromXML('<section>Alice</section>'),
236 text = c.doc().children()[0];
237 expect(text.parent().sameNode(c.doc())).to.be.true;
241 describe('free text handling', function() {
242 it('sees free text', function() {
243 var c = canvas.fromXML('<section>Alice <span>has</span> a cat</section>'),
244 children = c.doc().children();
245 expect(children.length).to.equal(3);
246 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
247 expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
248 expect(children[2]).to.be.instanceOf(documentElement.DocumentTextElement);
252 describe('white characters handling', function() {
253 it('says empty element node has no children', function() {
254 var c = canvas.fromXML('<section></section>');
255 expect(c.doc().children().length).to.equal(0);
257 it('says element node with one space has one DocumentTextElement', function() {
258 var c = canvas.fromXML('<section> </section>');
259 expect(c.doc().children().length).to.equal(1);
260 expect(c.doc().children()[0]).to.be.instanceOf(documentElement.DocumentTextElement);
261 expect(c.doc().children()[0].getText()).to.equal(' ');
263 it('ignores white space surrounding block elements', function() {
264 var c = canvas.fromXML('<section> <div></div> </section>');
265 var children = c.doc().children();
266 expect(children.length).to.equal(1);
267 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
269 it('ignores white space between block elements', function() {
270 var c = canvas.fromXML('<section><div></div> <div></div></section>');
271 var children = c.doc().children();
272 expect(children.length === 2);
273 [0,1].forEach(function(idx) {
274 expect(children[idx]).to.be.instanceOf(documentElement.DocumentNodeElement);
278 it('trims white space from the beginning and the end of the block elements', function() {
279 var c = canvas.fromXML('<section> Alice <span>has</span> a cat </section>');
280 expect(c.doc().children()[0].getText()).to.equal('Alice ');
281 expect(c.doc().children()[2].getText()).to.equal(' a cat');
284 it('normalizes string of white characters to one space at the inline element boundries', function() {
285 var c = canvas.fromXML('<span> Alice has a cat </span>');
286 expect(c.doc().children()[0].getText()).to.equal(' Alice has a cat ');
289 it('normalizes string of white characters to one space before inline element', function() {
290 var c = canvas.fromXML('<div>Alice has <span>a cat</span></div>');
291 expect(c.doc().children()[0].getText()).to.equal('Alice has ');
294 it('normalizes string of white characters to one space after inline element', function() {
295 var c = canvas.fromXML('<div>Alice has <span>a</span> cat</div>');
296 expect(c.doc().children()[2].getText()).to.equal(' cat');
300 describe('getting vertically first text element', function() {
301 it('returns the first child if it\'s text element, ignores metadata', function() {
302 var c = canvas.fromXML('<section><metadata><dc:author>author</dc:author></metadata>Alice<div>has</div>a cat</section>'),
303 first = c.doc().getVerticallyFirstTextElement();
305 expect(first.sameNode(c.doc().children()[1])).to.be.true;
308 it('looks recursively inside node elements if they precede text element', function() {
309 var c = canvas.fromXML('\
318 textAlice = c.doc().children()[0].children()[0].children()[0],
319 first = c.doc().getVerticallyFirstTextElement();
321 expect(textAlice).to.be.instanceOf(documentElement.DocumentTextElement);
322 expect(first.sameNode(textAlice)).to.be.true;
327 describe('manipulation api', function() {
329 describe('Basic Element inserting', function() {
330 it('can put new NodeElement at the end', function() {
331 var c = canvas.fromXML('<section></section>'),
332 appended = c.doc().append({tag: 'header', klass: 'some.class'}),
333 children = c.doc().children();
335 expect(children.length).to.equal(1);
336 expect(children[0].sameNode(appended)).to.be.true;
339 it('can put new TextElement at the end', function() {
340 var c = canvas.fromXML('<section></section>'),
341 appended = c.doc().append({text: 'Alice'}),
342 children = c.doc().children();
344 expect(children.length).to.equal(1);
345 expect(children[0].sameNode(appended)).to.be.true;
346 expect(children[0].getText()).to.equal('Alice');
349 it('can put new NodeElement after another NodeElement', function() {
350 var c = canvas.fromXML('<section><div></div></section>'),
351 div = c.doc().children()[0],
352 added = div.after({tag: 'header', klass: 'some.class'}),
353 children = c.doc().children();
354 expect(children.length).to.equal(2);
355 expect(children[1].sameNode(added)).to.be.true;
358 it('can put new Nodeelement before another element', function() {
359 var c = canvas.fromXML('<section><div></div></section>'),
360 div = c.doc().children()[0],
361 added = div.before({tag: 'header', klass: 'some.class'}),
362 children = c.doc().children();
363 expect(children.length).to.equal(2);
364 expect(children[0].sameNode(added)).to.be.true;
367 it('can put new DocumentNodeElement after DocumentTextElement', function() {
368 var c = canvas.fromXML('<section>Alice</section>'),
369 text = c.doc().children()[0],
370 added = text.after({tag: 'p'}),
371 children = c.doc().children();
373 expect(children.length).to.equal(2);
374 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
375 expect(children[0].getText()).to.equal('Alice');
376 expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
377 expect(children[1].sameNode(added)).to.be.true;
379 it('can put new DocumentNodeElement before DocumentTextElement', function() {
380 var c = canvas.fromXML('<section>Alice</section>'),
381 text = c.doc().children()[0],
382 added = text.before({tag: 'p'}),
383 children = c.doc().children();
385 expect(children.length).to.equal(2);
386 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
387 expect(children[0].sameNode(added)).to.be.true;
388 expect(children[1]).to.be.instanceOf(documentElement.DocumentTextElement);
389 expect(children[1].getText()).to.equal('Alice');
393 describe('Splitting text', function() {
395 it('splits DocumentTextElement\'s parent into two DocumentNodeElements of the same type', function() {
396 var c = canvas.fromXML('<section><header>Some header</header></section>'),
398 text = section.children()[0].children()[0];
400 var returnedValue = text.split({offset: 5});
401 expect(section.children().length).to.equal(2, 'section has two children');
403 var header1 = section.children()[0];
404 var header2 = section.children()[1];
406 expect(header1.getWlxmlTag()).to.equal('header', 'first section child represents wlxml header');
407 expect(header1.children().length).to.equal(1, 'first header has one text child');
408 expect(header1.children()[0].getText()).to.equal('Some ', 'first header has correct content');
409 expect(header2.getWlxmlTag()).to.equal('header', 'second section child represents wlxml header');
410 expect(header2.children().length).to.equal(1, 'second header has one text child');
411 expect(header2.children()[0].getText()).to.equal('header', 'second header has correct content');
413 expect(returnedValue.first.sameNode(header1)).to.equal(true, 'first node returnde');
414 expect(returnedValue.second.sameNode(header2)).to.equal(true, 'second node returned');
417 it('leaves empty copy of DocumentNodeElement if splitting at the very beginning', function() {
418 var c = canvas.fromXML('<section><header>Some header</header></section>'),
420 text = section.children()[0].children()[0];
422 text.split({offset: 0});
424 var header1 = section.children()[0];
425 var header2 = section.children()[1];
427 expect(header1.children().length).to.equal(0);
428 expect(header2.children()[0].getText()).to.equal('Some header');
431 it('leaves empty copy of DocumentNodeElement if splitting at the very end', function() {
432 var c = canvas.fromXML('<section><header>Some header</header></section>'),
434 text = section.children()[0].children()[0];
436 text.split({offset: 11});
438 var header1 = section.children()[0];
439 var header2 = section.children()[1];
441 expect(header1.children()[0].getText()).to.equal('Some header');
442 expect(header2.children().length).to.equal(0);
445 it('keeps DocumentTextElement\'s parent\'s children elements intact', function() {
446 var c = canvas.fromXML('\
449 A <span>fancy</span> and <span>nice</span> header\
453 header = section.children()[0],
454 textAnd = header.children()[2];
456 textAnd.split({offset: 2});
458 var sectionChildren = section.children();
459 expect(sectionChildren.length).to.equal(2, 'Section has two children');
460 expect(sectionChildren[0].getWlxmlTag()).to.equal('header', 'First section element is a wlxml header');
461 expect(sectionChildren[1].getWlxmlTag()).to.equal('header', 'Second section element is a wlxml header');
463 var firstHeaderChildren = sectionChildren[0].children();
464 expect(firstHeaderChildren.length).to.equal(3, 'First header has three children');
465 expect(firstHeaderChildren[0].getText()).to.equal('A ', 'First header starts with a text');
466 expect(firstHeaderChildren[1].getWlxmlTag()).to.equal('span', 'First header has span in the middle');
467 expect(firstHeaderChildren[2].getText()).to.equal(' a', 'First header ends with text');
469 var secondHeaderChildren = sectionChildren[1].children();
470 expect(secondHeaderChildren.length).to.equal(3, 'Second header has three children');
471 expect(secondHeaderChildren[0].getText()).to.equal('nd ', 'Second header starts with text');
472 expect(secondHeaderChildren[1].getWlxmlTag()).to.equal('span', 'Second header has span in the middle');
473 expect(secondHeaderChildren[2].getText()).to.equal(' header', 'Second header ends with text');
477 describe('wrapping', function() {
478 it('wraps DocumentNodeElement', function() {
479 var c = canvas.fromXML('<section><div></div></section>'),
480 div = c.doc().children()[0];
482 var returned = div.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
483 parent = div.parent(),
484 parent2 = c.doc().children()[0];
486 expect(returned.sameNode(parent)).to.be.true;
487 expect(returned.sameNode(parent2)).to.be.true;
488 expect(returned.getWlxmlTag()).to.equal('header');
489 expect(returned.getWlxmlClass()).to.equal('some.class');
491 it('wraps DocumentTextElement', function() {
492 var c = canvas.fromXML('<section>Alice</section>'),
493 text = c.doc().children()[0];
495 var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
496 parent = text.parent(),
497 parent2 = c.doc().children()[0];
499 expect(returned.sameNode(parent)).to.be.true;
500 expect(returned.sameNode(parent2)).to.be.true;
501 expect(returned.getWlxmlTag()).to.equal('header');
502 expect(returned.getWlxmlClass()).to.equal('some.class');
505 describe('wrapping part of DocumentTextElement', function() {
506 [{start: 5, end: 12}, {start: 12, end: 5}].forEach(function(offsets) {
507 it('wraps in the middle ' + offsets.start + '/' + offsets.end, function() {
508 var c = canvas.fromXML('<section>Alice has a cat</section>'),
509 text = c.doc().children()[0];
511 var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class', start: offsets.start, end: offsets.end}),
512 children = c.doc().children();
514 expect(children.length).to.equal(3);
516 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
517 expect(children[0].getText()).to.equal('Alice');
519 expect(children[1].sameNode(returned)).to.be.true;
520 expect(returned.getWlxmlTag()).to.equal('header');
521 expect(returned.getWlxmlClass()).to.equal('some.class');
522 expect(children[1].children().length).to.equal(1);
523 expect(children[1].children()[0].getText()).to.equal(' has a ');
525 expect(children[2]).to.be.instanceOf(documentElement.DocumentTextElement);
526 expect(children[2].getText()).to.equal('cat');
530 it('wraps whole text inside DocumentTextElement if offsets span entire content', function() {
531 var c = canvas.fromXML('<section>Alice has a cat</section>'),
532 text = c.doc().children()[0];
534 var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class', start: 0, end: 15}),
535 children = c.doc().children();
537 expect(children.length).to.equal(1);
538 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
539 expect(children[0].children()[0].getText()).to.equal('Alice has a cat');
543 it('wraps text spanning multiple sibling DocumentTextNodes', function() {
544 var c = canvas.fromXML('<section>Alice has a <span>small</span> cat</section>'),
546 wrapper = c.wrapText({
548 _with: {tag: 'span', klass: 'some.class'},
554 expect(section.children().length).to.equal(2);
555 expect(section.children()[0]).to.be.instanceOf(documentElement.DocumentTextElement);
556 expect(section.children()[0].getText()).to.equal('Alice ');
558 var wrapper2 = section.children()[1];
559 expect(wrapper2.sameNode(wrapper)).to.be.true;
561 var wrapperChildren = wrapper.children();
562 expect(wrapperChildren.length).to.equal(3);
563 expect(wrapperChildren[0].getText()).to.equal('has a ');
565 expect(wrapperChildren[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
566 expect(wrapperChildren[1].children().length).to.equal(1);
567 expect(wrapperChildren[1].children()[0].getText()).to.equal('small');
569 expect(wrapperChildren[2].getText()).to.equal(' cat');
573 describe('unwrapping', function() {
574 it('unwraps DocumentTextElement from its parent DocumentNodeElement if it\'s its only child', function() {
575 var c = canvas.fromXML('<section>Alice <span>has a</span> cat</section>'),
577 text = section.children()[1].children()[0];
581 expect(section.children().length).to.equal(1, 'section has one child');
582 expect(section.children()[0].getText()).to.equal('Alice has a cat');
587 describe('Lists api', function() {
588 describe('creating lists', function() {
589 it('allows creation of a list from existing sibling DocumentElements', function() {
590 var c = canvas.fromXML('\
598 textHas = section.children()[1],
599 divA = section.children()[2]
601 c.list.create({element1: textHas, element2: divA});
603 expect(section.children().length).to.equal(3, 'section has three child elements');
605 var child1 = section.children()[0],
606 list = section.children()[1],
607 child3 = section.children()[2];
609 expect(child1.getText()).to.equal('Alice');
610 expect(list.is('list')).to.equal(true, 'second child is a list');
611 expect(list.children().length).to.equal(2, 'list contains two elements');
612 list.children().forEach(function(child) {
613 expect(child.getWlxmlClass()).to.equal('item', 'list childs have wlxml class of item');
615 expect(child3.children()[0].getText()).to.equal('cat');
618 it('allows creating nested list from existing sibling list items', function() {
619 var c = canvas.fromXML('\
621 <div class="list-items">\
622 <div class="item">A</div>\
623 <div class="item">B</div>\
624 <div class="item">C</div>\
625 <div class="item">D</div>\
628 outerList = c.doc().children()[0],
629 itemB = outerList.children()[1],
630 itemC = outerList.children()[2];
633 c.list.create({element1: itemB, element2: itemC});
635 var outerListItems = outerList.children(),
636 innerList = outerListItems[1].children()[0],
637 innerListItems = innerList.children();
639 expect(outerListItems.length).to.equal(3, 'outer list has three items');
640 expect(outerListItems[0].children()[0].getText()).to.equal('A', 'first outer item ok');
641 expect(outerListItems[1].getWlxmlClass()).to.equal('item', 'inner list is wrapped by item element');
643 expect(innerList.is('list')).to.equal(true, 'inner list created');
644 expect(innerListItems.length).to.equal(2, 'inner list has two items');
645 expect(innerListItems[0].children()[0].getText()).to.equal('B', 'first inner item ok');
646 expect(innerListItems[1].children()[0].getText()).to.equal('C', 'second inner item ok');
648 expect(outerListItems[2].children()[0].getText()).to.equal('D', 'last outer item ok');
654 describe('extracting list items', function() {
655 it('creates two lists with extracted items in the middle if extracting from the middle of the list', function() {
656 var c = canvas.fromXML('\
658 <div class="list.items">\
659 <div class="item">0</div>\
660 <div class="item">1</div>\
661 <div class="item">2</div>\
662 <div class="item">3</div>\
665 list = c.doc().children()[0],
666 item1 = list.children()[1],
667 item2 = list.children()[2];
669 c.list.extractItems({element1: item1, element2: item2});
671 var section = c.doc(),
672 list1 = section.children()[0],
673 oldItem1 = section.children()[1],
674 oldItem2 = section.children()[2],
675 list2 = section.children()[3];
677 expect(section.children().length).to.equal(4, 'section contains four children');
679 expect(list1.is('list')).to.equal(true, 'first section child is a list');
680 expect(list1.children().length).to.equal(1, 'first list has one child');
681 expect(list1.children()[0].children()[0].getText()).to.equal('0', 'first item of the first list is a first item of the original list');
683 expect(oldItem1.children()[0].getText()).to.equal('1', 'first item got extracted');
684 expect(oldItem1.getWlxmlClass() === undefined).to.equal(true, 'first extracted element has no wlxml class');
686 expect(oldItem2.children()[0].getText()).to.equal('2', 'second item got extracted');
687 expect(oldItem2.getWlxmlClass() === undefined).to.equal(true, 'second extracted element has no wlxml class');
689 expect(list2.is('list')).to.equal(true, 'last section child is a list');
690 expect(list2.children().length).to.equal(1, 'second list has one child');
691 expect(list2.children()[0].children()[0].getText()).to.equal('3', 'first item of the second list is a last item of the original list');
694 it('puts extracted items above the list if starting item is the first one', function() {
695 var c = canvas.fromXML('\
697 <div class="list.items">\
698 <div class="item">0</div>\
699 <div class="item">1</div>\
700 <div class="item">2</div>\
703 list = c.doc().children()[0],
704 item1 = list.children()[0],
705 item2 = list.children()[1],
706 item3 = list.children()[2];
708 c.list.extractItems({element1: item1, element2: item2});
710 var section = c.doc(),
711 oldItem1 = section.children()[0],
712 oldItem2 = section.children()[1],
713 newList = section.children()[2];
715 expect(section.children().length).to.equal(3, 'section has three children');
716 expect(oldItem1.children()[0].getText()).to.equal('0', 'first item extracted');
717 expect(oldItem2.children()[0].getText()).to.equal('1', 'second item extracted');
718 expect(newList.is('list')).to.equal(true, 'list lies below extracted item');
719 expect(newList.children().length).to.equal(1, 'list has now one child');
722 it('puts extracted items below the list if ending item is the last one', function() {
723 var c = canvas.fromXML('\
725 <div class="list.items">\
726 <div class="item">0</div>\
727 <div class="item">1</div>\
728 <div class="item">2</div>\
731 list = c.doc().children()[0],
732 item1 = list.children()[0],
733 item2 = list.children()[1],
734 item3 = list.children()[2];
736 c.list.extractItems({element1: item2, element2: item3});
738 var section = c.doc(),
739 oldItem1 = section.children()[1],
740 oldItem2 = section.children()[2],
741 newList = section.children()[0];
743 expect(section.children().length).to.equal(3, 'section has three children');
744 expect(oldItem1.children()[0].getText()).to.equal('1', 'first item extracted');
745 expect(oldItem2.children()[0].getText()).to.equal('2', 'second item extracted');
746 expect(newList.is('list')).to.equal(true, 'list lies above extracted item');
747 expect(newList.children().length).to.equal(1, 'list has now one child');
750 it('removes list if all its items are extracted', function() {
751 var c = canvas.fromXML('\
753 <div class="list.items">\
754 <div class="item">some item</div>\
755 <div class="item">some item 2</div>\
758 list = c.doc().children()[0],
759 item1 = list.children()[0],
760 item2 = list.children()[1];
762 c.list.extractItems({element1: item1, element2: item2});
764 var section = c.doc(),
765 list1 = section.children()[0],
766 oldItem1 = section.children()[0],
767 oldItem2 = section.children()[1];
769 expect(section.children().length).to.equal(2, 'section contains two children');
770 expect(oldItem1.children()[0].getText()).to.equal('some item');
771 expect(oldItem2.children()[0].getText()).to.equal('some item 2');
774 it('creates two lists with extracted items in the middle if extracting from the middle of the list - nested case' , function() {
775 var c = canvas.fromXML('\
777 <div class="list.items">\
778 <div class="item">0</div>\
780 <div class="list.items">\
781 <div class="item">1.1</div>\
782 <div class="item">1.2</div>\
783 <div class="item">1.3</div>\
786 <div class="item">2</div>\
789 list = c.doc().children()[0],
790 nestedList = list.children()[1].children()[0],
791 nestedListItem = nestedList.children()[1];
793 c.list.extractItems({element1: nestedListItem, element2: nestedListItem});
795 var section = c.doc(),
796 list = section.children()[0],
797 item1 = list.children()[0],
798 item2 = list.children()[1], //
799 item3 = list.children()[2],
800 item4 = list.children()[3], //
801 item5 = list.children()[4],
802 nestedList1 = item2.children()[0],
803 nestedList2 = item4.children()[0];
805 expect(list.children().length).to.equal(5, 'top list has five items');
807 expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
809 expect(item2.getWlxmlClass()).to.equal('item', 'first nested list is still wrapped in item element');
810 expect(nestedList1.children().length).to.equal(1, 'first nested list is left with one child');
811 expect(nestedList1.children()[0].children()[0].getText()).to.equal('1.1', 'first nested list item left alone');
813 expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
815 expect(item4.getWlxmlClass()).to.equal('item', 'second nested list is still wrapped in item element');
816 expect(nestedList2.children().length).to.equal(1, 'second nested list is left with one child');
817 expect(nestedList2.children()[0].children()[0].getText()).to.equal('1.3', 'second nested list item left alone');
819 expect(item5.children()[0].getText()).to.equal('2', 'last item ok');
822 it('puts extracted items below the list if ending item is the last one - nested case' , function() {
823 var c = canvas.fromXML('\
825 <div class="list.items">\
826 <div class="item">0</div>\
828 <div class="list.items">\
829 <div class="item">1.1</div>\
830 <div class="item">1.2</div>\
831 <div class="item">1.3</div>\
834 <div class="item">2</div>\
837 list = c.doc().children()[0],
838 nestedList = list.children()[1].children()[0],
839 nestedListItem1 = nestedList.children()[1],
840 nestedListItem2 = nestedList.children()[2];
842 c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
844 var section = c.doc(),
845 list = section.children()[0],
846 item1 = list.children()[0],
847 item2 = list.children()[1],
848 item3 = list.children()[2],
849 item4 = list.children()[3],
850 item5 = list.children()[4];
851 nestedList = item2.children()[0];
853 expect(list.children().length).to.equal(5, 'top list has five items');
854 expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
855 expect(item2.getWlxmlClass()).to.equal('item', 'nested list is still wrapped in item element');
856 expect(nestedList.children().length).to.equal(1, 'nested list is left with one child');
857 expect(nestedList.children()[0].children()[0].getText()).to.equal('1.1', 'nested list item left alone');
858 expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
859 expect(item4.children()[0].getText()).to.equal('1.3', 'fourth item ok');
860 expect(item5.children()[0].getText()).to.equal('2', 'fifth item ok');
863 it('puts extracted items above the list if starting item is the first one - nested case' , function() {
864 var c = canvas.fromXML('\
866 <div class="list.items">\
867 <div class="item">0</div>\
869 <div class="list.items">\
870 <div class="item">1.1</div>\
871 <div class="item">1.2</div>\
872 <div class="item">1.3</div>\
875 <div class="item">2</div>\
878 list = c.doc().children()[0],
879 nestedList = list.children()[1].children()[0],
880 nestedListItem1 = nestedList.children()[0],
881 nestedListItem2 = nestedList.children()[1];
883 c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
885 var section = c.doc(),
886 list = section.children()[0],
887 item1 = list.children()[0],
888 item2 = list.children()[1],
889 item3 = list.children()[2],
890 item4 = list.children()[3],
891 item5 = list.children()[4];
892 nestedList = item4.children()[0];
894 expect(list.children().length).to.equal(5, 'top list has five items');
895 expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
896 expect(item2.children()[0].getText()).to.equal('1.1', 'second item ok');
897 expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
899 expect(item4.getWlxmlClass()).to.equal('item', 'nested list is still wrapped in item element');
900 expect(nestedList.children().length).to.equal(1, 'nested list is left with one child');
901 expect(nestedList.children()[0].children()[0].getText()).to.equal('1.3', 'nested list item left alone');
902 expect(item5.children()[0].getText()).to.equal('2', 'fifth item ok');
905 it('removes list if all its items are extracted - nested case', function() {
906 var c = canvas.fromXML('\
908 <div class="list.items">\
909 <div class="item">0</div>\
911 <div class="list.items">\
912 <div class="item">1.1</div>\
913 <div class="item">1.2</div>\
916 <div class="item">2</div>\
919 list = c.doc().children()[0],
920 nestedList = list.children()[1].children()[0],
921 nestedListItem1 = nestedList.children()[0],
922 nestedListItem2 = nestedList.children()[1];
924 c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
926 var section = c.doc(),
927 list = section.children()[0],
928 item1 = list.children()[0],
929 item2 = list.children()[1],
930 item3 = list.children()[2],
931 item4 = list.children()[3];
933 expect(list.children().length).to.equal(4, 'top list has four items');
934 expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
935 expect(item2.children()[0].getText()).to.equal('1.1', 'second item ok');
936 expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
937 expect(item4.children()[0].getText()).to.equal('2', 'fourth item ok');
940 it('extracts items out of outer most list when merge flag is set to false', function() {
941 var c = canvas.fromXML('\
943 <div class="list.items">\
944 <div class="item">0</div>\
946 <div class="list.items">\
947 <div class="item">1.1</div>\
948 <div class="item">1.2</div>\
951 <div class="item">2</div>\
955 list = section.children()[0],
956 nestedList = list.children()[1].children()[0],
957 nestedListItem = nestedList.children()[0];
959 var test = c.list.extractItems({element1: nestedListItem, element2: nestedListItem, merge: false});
961 expect(test).to.equal(true, 'extraction status ok');
963 var sectionChildren = section.children(),
964 extractedItem = sectionChildren[1];
966 expect(sectionChildren.length).to.equal(3, 'section has three children');
967 expect(sectionChildren[0].is('list')).to.equal(true, 'first child is a list');
969 expect(extractedItem.getWlxmlTag()).to.equal('div', 'extracted item is a wlxml div');
970 expect(extractedItem.getWlxmlClass()).to.equal(undefined, 'extracted item has no wlxml class');
971 expect(extractedItem.children()[0].getText()).to.equal('1.1', 'extracted item ok');
972 expect(sectionChildren[2].is('list')).to.equal(true, 'second child is a list');
979 describe('Cursor', function() {
983 beforeEach(function() {
984 getSelection = sinon.stub(window, 'getSelection');
987 afterEach(function() {
988 getSelection.restore();
991 it('returns position when browser selection collapsed', function() {
992 var c = canvas.fromXML('<section>Alice has a cat</section>'),
994 text = $(dom.contents()[0]).contents()[0];
996 expect(text.nodeType).to.equal(Node.TEXT_NODE, 'correct node selected');
997 expect($(text).text()).to.equal('Alice has a cat');
999 getSelection.returns({
1006 var cursor = c.getCursor(),
1007 position = cursor.getPosition();
1009 expect(cursor.isSelecting()).to.equal(false, 'cursor is not selecting anything');
1010 expect(position.element.getText()).to.equal('Alice has a cat');
1011 expect(position.offset).to.equal(5);
1012 expect(position.offsetAtEnd).to.equal(false, 'offset is not at end');
1014 getSelection.returns({
1022 expect(cursor.getPosition().offsetAtEnd).to.equal(true, 'offset at end');
1025 it('returns boundries of selection when browser selection not collapsed', function() {
1026 var c = canvas.fromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
1027 dom = c.doc().dom(),
1029 alice: dom.contents()[0],
1030 has: $(dom.contents()[1]).contents()[0],
1031 cat: dom.contents()[4]
1033 cursor = c.getCursor(),
1034 aliceElement = c.getDocumentElement(text.alice),
1035 catElement = c.getDocumentElement(text.cat);
1039 {focus: text.alice, focusOffset: 1, anchor: text.cat, anchorOffset: 2, selectionAnchor: catElement},
1040 {focus: text.cat, focusOffset: 2, anchor: text.alice, anchorOffset: 1, selectionAnchor: aliceElement}
1041 ].forEach(function(s, idx) {
1042 getSelection.returns({isColapsed: false, anchorNode: s.anchor, anchorOffset: s.anchorOffset, focusNode: s.focus, focusOffset: s.focusOffset});
1044 var selectionStart = cursor.getSelectionStart(),
1045 selectionEnd = cursor.getSelectionEnd(),
1046 selectionAnchor = cursor.getSelectionAnchor();
1048 expect(cursor.isSelecting()).to.equal(true, 'cursor is selecting');
1049 expect(selectionStart.element.sameNode(aliceElement)).to.equal(true, '"Alice" is the start of the selection ' + idx);
1050 expect(selectionStart.offset).to.equal(1, '"Alice" offset ok' + idx);
1051 expect(selectionEnd.element.sameNode(catElement)).to.equal(true, '"Cat" is the start of the selection ' + idx);
1052 expect(selectionEnd.offset).to.equal(2, '"Cat" offset ok' + idx);
1053 expect(selectionAnchor.element.sameNode(s.selectionAnchor)).to.equal(true, 'anchor ok');
1054 expect(selectionAnchor.offset).to.equal(s.anchorOffset, 'anchor offset ok');
1058 it('recognizes when browser selection boundries lies in sibling DocumentTextElements', function() {
1059 var c = canvas.fromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
1060 dom = c.doc().dom(),
1062 alice: dom.contents()[0],
1063 has: $(dom.contents()[1]).contents()[0],
1064 a: dom.contents()[2],
1065 big: $(dom.contents()[3]).contents()[0],
1066 cat: dom.contents()[4]
1068 cursor = c.getCursor();
1070 expect($(text.alice).text()).to.equal('Alice ');
1071 expect($(text.has).text()).to.equal('has');
1072 expect($(text.a).text()).to.equal(' a ');
1073 expect($(text.big).text()).to.equal('big');
1074 expect($(text.cat).text()).to.equal(' cat');
1076 getSelection.returns({anchorNode: text.alice, focusNode: text.a});
1077 expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "a" are children');
1079 getSelection.returns({anchorNode: text.alice, focusNode: text.cat});
1080 expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "cat" are children');
1082 getSelection.returns({anchorNode: text.alice, focusNode: text.has});
1083 expect(cursor.isSelectingSiblings()).to.equal(false, '"Alice" and "has" are not children');
1085 getSelection.returns({anchorNode: text.has, focusNode: text.big});
1086 expect(cursor.isSelectingSiblings()).to.equal(false, '"has" and "big" are not children');