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];
579 var newTextContainer = text.unwrap();
581 expect(section.children().length).to.equal(1, 'section has one child');
582 expect(section.children()[0].getText()).to.equal('Alice has a cat');
583 expect(newTextContainer.sameNode(c.doc())).to.equal(true, 'unwrap returns new text parent DocumentNodeElement');
588 describe('Lists api', function() {
589 describe('creating lists', function() {
590 it('allows creation of a list from existing sibling DocumentElements', function() {
591 var c = canvas.fromXML('\
599 textHas = section.children()[1],
600 divA = section.children()[2]
602 c.list.create({element1: textHas, element2: divA});
604 expect(section.children().length).to.equal(3, 'section has three child elements');
606 var child1 = section.children()[0],
607 list = section.children()[1],
608 child3 = section.children()[2];
610 expect(child1.getText()).to.equal('Alice');
611 expect(list.is('list')).to.equal(true, 'second child is a list');
612 expect(list.children().length).to.equal(2, 'list contains two elements');
613 list.children().forEach(function(child) {
614 expect(child.getWlxmlClass()).to.equal('item', 'list childs have wlxml class of item');
616 expect(child3.children()[0].getText()).to.equal('cat');
619 it('allows creating nested list from existing sibling list items', function() {
620 var c = canvas.fromXML('\
622 <div class="list-items">\
623 <div class="item">A</div>\
624 <div class="item">B</div>\
625 <div class="item">C</div>\
626 <div class="item">D</div>\
629 outerList = c.doc().children()[0],
630 itemB = outerList.children()[1],
631 itemC = outerList.children()[2];
634 c.list.create({element1: itemB, element2: itemC});
636 var outerListItems = outerList.children(),
637 innerList = outerListItems[1].children()[0],
638 innerListItems = innerList.children();
640 expect(outerListItems.length).to.equal(3, 'outer list has three items');
641 expect(outerListItems[0].children()[0].getText()).to.equal('A', 'first outer item ok');
642 expect(outerListItems[1].getWlxmlClass()).to.equal('item', 'inner list is wrapped by item element');
644 expect(innerList.is('list')).to.equal(true, 'inner list created');
645 expect(innerListItems.length).to.equal(2, 'inner list has two items');
646 expect(innerListItems[0].children()[0].getText()).to.equal('B', 'first inner item ok');
647 expect(innerListItems[1].children()[0].getText()).to.equal('C', 'second inner item ok');
649 expect(outerListItems[2].children()[0].getText()).to.equal('D', 'last outer item ok');
655 describe('extracting list items', function() {
656 it('creates two lists with extracted items in the middle if extracting from the middle of the list', function() {
657 var c = canvas.fromXML('\
659 <div class="list.items">\
660 <div class="item">0</div>\
661 <div class="item">1</div>\
662 <div class="item">2</div>\
663 <div class="item">3</div>\
666 list = c.doc().children()[0],
667 item1 = list.children()[1],
668 item2 = list.children()[2];
670 c.list.extractItems({element1: item1, element2: item2});
672 var section = c.doc(),
673 list1 = section.children()[0],
674 oldItem1 = section.children()[1],
675 oldItem2 = section.children()[2],
676 list2 = section.children()[3];
678 expect(section.children().length).to.equal(4, 'section contains four children');
680 expect(list1.is('list')).to.equal(true, 'first section child is a list');
681 expect(list1.children().length).to.equal(1, 'first list has one child');
682 expect(list1.children()[0].children()[0].getText()).to.equal('0', 'first item of the first list is a first item of the original list');
684 expect(oldItem1.children()[0].getText()).to.equal('1', 'first item got extracted');
685 expect(oldItem1.getWlxmlClass() === undefined).to.equal(true, 'first extracted element has no wlxml class');
687 expect(oldItem2.children()[0].getText()).to.equal('2', 'second item got extracted');
688 expect(oldItem2.getWlxmlClass() === undefined).to.equal(true, 'second extracted element has no wlxml class');
690 expect(list2.is('list')).to.equal(true, 'last section child is a list');
691 expect(list2.children().length).to.equal(1, 'second list has one child');
692 expect(list2.children()[0].children()[0].getText()).to.equal('3', 'first item of the second list is a last item of the original list');
695 it('puts extracted items above the list if starting item is the first one', function() {
696 var c = canvas.fromXML('\
698 <div class="list.items">\
699 <div class="item">0</div>\
700 <div class="item">1</div>\
701 <div class="item">2</div>\
704 list = c.doc().children()[0],
705 item1 = list.children()[0],
706 item2 = list.children()[1],
707 item3 = list.children()[2];
709 c.list.extractItems({element1: item1, element2: item2});
711 var section = c.doc(),
712 oldItem1 = section.children()[0],
713 oldItem2 = section.children()[1],
714 newList = section.children()[2];
716 expect(section.children().length).to.equal(3, 'section has three children');
717 expect(oldItem1.children()[0].getText()).to.equal('0', 'first item extracted');
718 expect(oldItem2.children()[0].getText()).to.equal('1', 'second item extracted');
719 expect(newList.is('list')).to.equal(true, 'list lies below extracted item');
720 expect(newList.children().length).to.equal(1, 'list has now one child');
723 it('puts extracted items below the list if ending item is the last one', function() {
724 var c = canvas.fromXML('\
726 <div class="list.items">\
727 <div class="item">0</div>\
728 <div class="item">1</div>\
729 <div class="item">2</div>\
732 list = c.doc().children()[0],
733 item1 = list.children()[0],
734 item2 = list.children()[1],
735 item3 = list.children()[2];
737 c.list.extractItems({element1: item2, element2: item3});
739 var section = c.doc(),
740 oldItem1 = section.children()[1],
741 oldItem2 = section.children()[2],
742 newList = section.children()[0];
744 expect(section.children().length).to.equal(3, 'section has three children');
745 expect(oldItem1.children()[0].getText()).to.equal('1', 'first item extracted');
746 expect(oldItem2.children()[0].getText()).to.equal('2', 'second item extracted');
747 expect(newList.is('list')).to.equal(true, 'list lies above extracted item');
748 expect(newList.children().length).to.equal(1, 'list has now one child');
751 it('removes list if all its items are extracted', function() {
752 var c = canvas.fromXML('\
754 <div class="list.items">\
755 <div class="item">some item</div>\
756 <div class="item">some item 2</div>\
759 list = c.doc().children()[0],
760 item1 = list.children()[0],
761 item2 = list.children()[1];
763 c.list.extractItems({element1: item1, element2: item2});
765 var section = c.doc(),
766 list1 = section.children()[0],
767 oldItem1 = section.children()[0],
768 oldItem2 = section.children()[1];
770 expect(section.children().length).to.equal(2, 'section contains two children');
771 expect(oldItem1.children()[0].getText()).to.equal('some item');
772 expect(oldItem2.children()[0].getText()).to.equal('some item 2');
775 it('creates two lists with extracted items in the middle if extracting from the middle of the list - nested case' , function() {
776 var c = canvas.fromXML('\
778 <div class="list.items">\
779 <div class="item">0</div>\
781 <div class="list.items">\
782 <div class="item">1.1</div>\
783 <div class="item">1.2</div>\
784 <div class="item">1.3</div>\
787 <div class="item">2</div>\
790 list = c.doc().children()[0],
791 nestedList = list.children()[1].children()[0],
792 nestedListItem = nestedList.children()[1];
794 c.list.extractItems({element1: nestedListItem, element2: nestedListItem});
796 var section = c.doc(),
797 list = section.children()[0],
798 item1 = list.children()[0],
799 item2 = list.children()[1], //
800 item3 = list.children()[2],
801 item4 = list.children()[3], //
802 item5 = list.children()[4],
803 nestedList1 = item2.children()[0],
804 nestedList2 = item4.children()[0];
806 expect(list.children().length).to.equal(5, 'top list has five items');
808 expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
810 expect(item2.getWlxmlClass()).to.equal('item', 'first nested list is still wrapped in item element');
811 expect(nestedList1.children().length).to.equal(1, 'first nested list is left with one child');
812 expect(nestedList1.children()[0].children()[0].getText()).to.equal('1.1', 'first nested list item left alone');
814 expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
816 expect(item4.getWlxmlClass()).to.equal('item', 'second nested list is still wrapped in item element');
817 expect(nestedList2.children().length).to.equal(1, 'second nested list is left with one child');
818 expect(nestedList2.children()[0].children()[0].getText()).to.equal('1.3', 'second nested list item left alone');
820 expect(item5.children()[0].getText()).to.equal('2', 'last item ok');
823 it('puts extracted items below the list if ending item is the last one - nested case' , function() {
824 var c = canvas.fromXML('\
826 <div class="list.items">\
827 <div class="item">0</div>\
829 <div class="list.items">\
830 <div class="item">1.1</div>\
831 <div class="item">1.2</div>\
832 <div class="item">1.3</div>\
835 <div class="item">2</div>\
838 list = c.doc().children()[0],
839 nestedList = list.children()[1].children()[0],
840 nestedListItem1 = nestedList.children()[1],
841 nestedListItem2 = nestedList.children()[2];
843 c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
845 var section = c.doc(),
846 list = section.children()[0],
847 item1 = list.children()[0],
848 item2 = list.children()[1],
849 item3 = list.children()[2],
850 item4 = list.children()[3],
851 item5 = list.children()[4];
852 nestedList = item2.children()[0];
854 expect(list.children().length).to.equal(5, 'top list has five items');
855 expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
856 expect(item2.getWlxmlClass()).to.equal('item', 'nested list is still wrapped in item element');
857 expect(nestedList.children().length).to.equal(1, 'nested list is left with one child');
858 expect(nestedList.children()[0].children()[0].getText()).to.equal('1.1', 'nested list item left alone');
859 expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
860 expect(item4.children()[0].getText()).to.equal('1.3', 'fourth item ok');
861 expect(item5.children()[0].getText()).to.equal('2', 'fifth item ok');
864 it('puts extracted items above the list if starting item is the first one - nested case' , function() {
865 var c = canvas.fromXML('\
867 <div class="list.items">\
868 <div class="item">0</div>\
870 <div class="list.items">\
871 <div class="item">1.1</div>\
872 <div class="item">1.2</div>\
873 <div class="item">1.3</div>\
876 <div class="item">2</div>\
879 list = c.doc().children()[0],
880 nestedList = list.children()[1].children()[0],
881 nestedListItem1 = nestedList.children()[0],
882 nestedListItem2 = nestedList.children()[1];
884 c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
886 var section = c.doc(),
887 list = section.children()[0],
888 item1 = list.children()[0],
889 item2 = list.children()[1],
890 item3 = list.children()[2],
891 item4 = list.children()[3],
892 item5 = list.children()[4];
893 nestedList = item4.children()[0];
895 expect(list.children().length).to.equal(5, 'top list has five items');
896 expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
897 expect(item2.children()[0].getText()).to.equal('1.1', 'second item ok');
898 expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
900 expect(item4.getWlxmlClass()).to.equal('item', 'nested list is still wrapped in item element');
901 expect(nestedList.children().length).to.equal(1, 'nested list is left with one child');
902 expect(nestedList.children()[0].children()[0].getText()).to.equal('1.3', 'nested list item left alone');
903 expect(item5.children()[0].getText()).to.equal('2', 'fifth item ok');
906 it('removes list if all its items are extracted - nested case', function() {
907 var c = canvas.fromXML('\
909 <div class="list.items">\
910 <div class="item">0</div>\
912 <div class="list.items">\
913 <div class="item">1.1</div>\
914 <div class="item">1.2</div>\
917 <div class="item">2</div>\
920 list = c.doc().children()[0],
921 nestedList = list.children()[1].children()[0],
922 nestedListItem1 = nestedList.children()[0],
923 nestedListItem2 = nestedList.children()[1];
925 c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
927 var section = c.doc(),
928 list = section.children()[0],
929 item1 = list.children()[0],
930 item2 = list.children()[1],
931 item3 = list.children()[2],
932 item4 = list.children()[3];
934 expect(list.children().length).to.equal(4, 'top list has four items');
935 expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
936 expect(item2.children()[0].getText()).to.equal('1.1', 'second item ok');
937 expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
938 expect(item4.children()[0].getText()).to.equal('2', 'fourth item ok');
941 it('extracts items out of outer most list when merge flag is set to false', function() {
942 var c = canvas.fromXML('\
944 <div class="list.items">\
945 <div class="item">0</div>\
947 <div class="list.items">\
948 <div class="item">1.1</div>\
949 <div class="item">1.2</div>\
952 <div class="item">2</div>\
956 list = section.children()[0],
957 nestedList = list.children()[1].children()[0],
958 nestedListItem = nestedList.children()[0];
960 var test = c.list.extractItems({element1: nestedListItem, element2: nestedListItem, merge: false});
962 expect(test).to.equal(true, 'extraction status ok');
964 var sectionChildren = section.children(),
965 extractedItem = sectionChildren[1];
967 expect(sectionChildren.length).to.equal(3, 'section has three children');
968 expect(sectionChildren[0].is('list')).to.equal(true, 'first child is a list');
970 expect(extractedItem.getWlxmlTag()).to.equal('div', 'extracted item is a wlxml div');
971 expect(extractedItem.getWlxmlClass()).to.equal(undefined, 'extracted item has no wlxml class');
972 expect(extractedItem.children()[0].getText()).to.equal('1.1', 'extracted item ok');
973 expect(sectionChildren[2].is('list')).to.equal(true, 'second child is a list');
980 describe('Cursor', function() {
984 beforeEach(function() {
985 getSelection = sinon.stub(window, 'getSelection');
988 afterEach(function() {
989 getSelection.restore();
992 it('returns position when browser selection collapsed', function() {
993 var c = canvas.fromXML('<section>Alice has a cat</section>'),
995 text = $(dom.contents()[0]).contents()[0];
997 expect(text.nodeType).to.equal(Node.TEXT_NODE, 'correct node selected');
998 expect($(text).text()).to.equal('Alice has a cat');
1000 getSelection.returns({
1007 var cursor = c.getCursor(),
1008 position = cursor.getPosition();
1010 expect(cursor.isSelecting()).to.equal(false, 'cursor is not selecting anything');
1011 expect(position.element.getText()).to.equal('Alice has a cat');
1012 expect(position.offset).to.equal(5);
1013 expect(position.offsetAtEnd).to.equal(false, 'offset is not at end');
1015 getSelection.returns({
1023 expect(cursor.getPosition().offsetAtEnd).to.equal(true, 'offset at end');
1026 it('returns boundries of selection when browser selection not collapsed', function() {
1027 var c = canvas.fromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
1028 dom = c.doc().dom(),
1030 alice: dom.contents()[0],
1031 has: $(dom.contents()[1]).contents()[0],
1032 cat: dom.contents()[4]
1034 cursor = c.getCursor(),
1035 aliceElement = c.getDocumentElement(text.alice),
1036 catElement = c.getDocumentElement(text.cat);
1040 {focus: text.alice, focusOffset: 1, anchor: text.cat, anchorOffset: 2, selectionAnchor: catElement},
1041 {focus: text.cat, focusOffset: 2, anchor: text.alice, anchorOffset: 1, selectionAnchor: aliceElement}
1042 ].forEach(function(s, idx) {
1043 getSelection.returns({isColapsed: false, anchorNode: s.anchor, anchorOffset: s.anchorOffset, focusNode: s.focus, focusOffset: s.focusOffset});
1045 var selectionStart = cursor.getSelectionStart(),
1046 selectionEnd = cursor.getSelectionEnd(),
1047 selectionAnchor = cursor.getSelectionAnchor();
1049 expect(cursor.isSelecting()).to.equal(true, 'cursor is selecting');
1050 expect(selectionStart.element.sameNode(aliceElement)).to.equal(true, '"Alice" is the start of the selection ' + idx);
1051 expect(selectionStart.offset).to.equal(1, '"Alice" offset ok' + idx);
1052 expect(selectionEnd.element.sameNode(catElement)).to.equal(true, '"Cat" is the start of the selection ' + idx);
1053 expect(selectionEnd.offset).to.equal(2, '"Cat" offset ok' + idx);
1054 expect(selectionAnchor.element.sameNode(s.selectionAnchor)).to.equal(true, 'anchor ok');
1055 expect(selectionAnchor.offset).to.equal(s.anchorOffset, 'anchor offset ok');
1059 it('recognizes when browser selection boundries lies in sibling DocumentTextElements', function() {
1060 var c = canvas.fromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
1061 dom = c.doc().dom(),
1063 alice: dom.contents()[0],
1064 has: $(dom.contents()[1]).contents()[0],
1065 a: dom.contents()[2],
1066 big: $(dom.contents()[3]).contents()[0],
1067 cat: dom.contents()[4]
1069 cursor = c.getCursor();
1071 expect($(text.alice).text()).to.equal('Alice ');
1072 expect($(text.has).text()).to.equal('has');
1073 expect($(text.a).text()).to.equal(' a ');
1074 expect($(text.big).text()).to.equal('big');
1075 expect($(text.cat).text()).to.equal(' cat');
1077 getSelection.returns({anchorNode: text.alice, focusNode: text.a});
1078 expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "a" are children');
1080 getSelection.returns({anchorNode: text.alice, focusNode: text.cat});
1081 expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "cat" are children');
1083 getSelection.returns({anchorNode: text.alice, focusNode: text.has});
1084 expect(cursor.isSelectingSiblings()).to.equal(false, '"Alice" and "has" are not children');
1086 getSelection.returns({anchorNode: text.has, focusNode: text.big});
1087 expect(cursor.isSelectingSiblings()).to.equal(false, '"has" and "big" are not children');