4 'modules/documentCanvas/canvas/canvas',
5 'modules/documentCanvas/canvas/documentElement',
6 'modules/documentCanvas/canvas/utils'
7 ], function(chai, sinon, canvas, documentElement, utils) {
11 var expect = chai.expect;
14 describe('Canvas', function() {
16 describe('Internal HTML representation of a DocumentNodeElement', function() {
17 it('is always a div tag', function() {
18 ['section', 'header', 'span', 'aside', 'figure'].forEach(function(tagName) {
19 var dom = canvas.fromXML('<' + tagName + '></' + tagName + '>').doc().dom();
20 expect(dom.prop('tagName')).to.equal('DIV', tagName + ' is represented as div');
23 it('has wlxml tag put into wlxml-tag attribute of its internal container', function() {
24 var dom = canvas.fromXML('<section></section>').doc().dom();
25 expect(dom.children('[document-element-content]').attr('wlxml-tag')).to.equal('section');
27 it('has wlxml class put into wlxml-class attribute of its internal containr, dots replaced with dashes', function() {
28 var dom = canvas.fromXML('<section class="some.class"></section>').doc().dom();
29 expect(dom.children('[document-element-content]').attr('wlxml-class')).to.equal('some-class');
33 describe('Internal HTML representation of a DocumentTextElement', function() {
34 it('is text node wrapped in a div with document-text-element attribute set', function() {
35 var dom = canvas.fromXML('<section>Alice</section>').doc().children()[0].dom();
36 expect(dom.prop('tagName')).to.equal('DIV');
37 expect(dom.attr('document-text-element')).to.equal('');
38 expect(dom.contents().length).to.equal(1);
39 expect(dom.contents()[0].nodeType).to.equal(Node.TEXT_NODE);
40 expect($(dom.contents()[0]).text()).to.equal('Alice');
44 describe('basic properties', function() {
45 it('renders empty document when canvas created from empty XML', function() {
46 var c = canvas.fromXML('');
47 expect(c.doc()).to.equal(null);
50 it('gives access to its document root node', function() {
51 var c = canvas.fromXML('<section></section>');
52 expect(c.doc().getWlxmlTag()).to.equal('section');
55 describe('root element', function() {
56 it('has no parent', function() {
57 var c = canvas.fromXML('<section></section>');
58 expect(c.doc().parent()).to.be.null;
62 describe('DocumentTextElement', function() {
63 it('can have its content set', function() {
64 var c = canvas.fromXML('<section>Alice</section>'),
66 text = root.children()[0];
68 text.setText('a cat');
69 expect(root.children()[0].getText()).to.equal('a cat');
73 describe('DocumentNodeElement', function() {
74 it('knows index of its child', function() {
75 var c = canvas.fromXML('<section><div></div><header></header><span></span></section>'),
77 child = root.children()[1];
78 expect(root.childIndex(child)).to.equal(1);
81 it('knows WLXML tag it renders', function(){
82 var c = canvas.fromXML('<section></section>'),
84 expect(section.getWlxmlTag()).to.equal('section', 'initial tag is section');
85 section.setWlxmlTag('header');
86 expect(section.getWlxmlTag()).to.equal('header', 'tag is changed to header');
89 it('knows WLXML class of a WLXML tag it renders', function(){
90 var c = canvas.fromXML('<section class="some.class.A"></section>'),
92 expect(section.getWlxmlClass()).to.equal('some.class.A');
93 section.setWlxmlClass('some.class.B');
94 expect(section.getWlxmlClass()).to.equal('some.class.B');
95 section.setWlxmlClass(null);
96 expect(section.getWlxmlClass()).to.be.undefined;
101 describe('element has meta attributes', function() {
102 it('can change its meta attributes', function() {
103 var c = canvas.fromXML('<section><span class="uri" meta-uri="someuri"></span></section>'),
104 span = c.doc().children()[0];
106 expect(span.getWlxmlMetaAttr('uri')).to.equal('someuri');
107 span.setWlxmlMetaAttr('uri', 'otheruri');
108 expect(span.getWlxmlMetaAttr('uri')).to.equal('otheruri');
111 it('changes its meta attributes with class change', function() {
112 var c = canvas.fromXML('<section><span class="uri" meta-uri="someuri"></span></section>'),
113 span = c.doc().children()[0];
115 expect(span.getWlxmlMetaAttr('uri')).to.equal('someuri');
116 span.setWlxmlClass('author');
117 expect(span.getWlxmlMetaAttr('uri')).to.be.undefined;
120 it('keeps meta attribute value on class change if a new class has this attribute', function() {
121 var c = canvas.fromXML('<section><span class="uri" meta-uri="someuri"></span></section>'),
122 span = c.doc().children()[0];
123 span.setWlxmlClass('uri.some.subclass');
124 expect(span.getWlxmlMetaAttr('uri')).to.equal('someuri');
129 it('returns DocumentNodeElement instance from HTMLElement', function() {
130 var c = canvas.fromXML('<section></section>'),
131 htmlElement = c.doc().dom().get(0),
132 element = c.getDocumentElement(htmlElement);
133 expect(element).to.be.instanceOf(documentElement.DocumentNodeElement);
134 expect(element.sameNode(c.doc()));
137 it('returns DocumentTextElement instance from Text Node', function() {
138 var c = canvas.fromXML('<section>Alice</section>'),
139 aliceElement = c.doc().children()[0],
140 textNode = aliceElement.dom().contents()[0],
141 element = c.getDocumentElement(textNode);
143 expect(textNode.nodeType).to.equal(Node.TEXT_NODE, 'text node selected');
144 expect($(textNode).text()).to.equal('Alice');
146 expect(element).to.be.instanceOf(documentElement.DocumentTextElement);
147 expect(element.sameNode(c.doc().children()[0]));
153 describe('document representation api', function() {
154 describe('document root element', function() {
155 var c = canvas.fromXML('<section></section>');
156 it('exists', function() {
157 expect(c.doc()).to.be.instanceOf(documentElement.DocumentElement);
159 it('is of type DocumentNodeElement', function() {
160 expect(c.doc()).to.be.instanceOf(documentElement.DocumentNodeElement);
164 describe('DocumentElements comparison', function() {
165 it('reports dwo DocumentElements to be the same when they represent the same wlxml document element', function() {
166 var c = canvas.fromXML('<section><div></div><div></div></section>'),
167 first_div1 = c.doc().children()[0],
168 first_div2 = c.doc().children()[0],
169 second_div = c.doc().children()[1];
170 expect(first_div1.sameNode(first_div1)).to.be.true;
171 expect(first_div1.sameNode(first_div2)).to.be.true;
172 expect(first_div1.sameNode(second_div)).to.be.false;
176 describe('traversing', function() {
177 it('reports element nodes', function() {
178 var c = canvas.fromXML('<section><div></div></section>'),
179 children = c.doc().children();
180 expect(children.length).to.equal(1);
181 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
183 c = canvas.fromXML('<section><div></div><div></div></section>'),
184 children = c.doc().children();
185 expect(children.length).to.equal(2);
186 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
187 expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
190 it('reports text nodes', function() {
191 var c = canvas.fromXML('<section>Alice</section>'),
192 children = c.doc().children();
193 expect(children.length).to.equal(1);
194 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
197 describe('accessing parents', function() {
198 it('returns DocumentNodeElement representing parent in wlxml document as DocumentNodeElement parent', function() {
199 var c = canvas.fromXML('<section><div></div></section>'),
200 div = c.doc().children()[0];
201 expect(div.parent().sameNode(c.doc())).to.be.true;
203 it('returns DocumentNodeElement representing parent in wlxml document as DocumentTextElement parent', function() {
204 var c = canvas.fromXML('<section>Alice</section>'),
205 text = c.doc().children()[0];
206 expect(text.parent().sameNode(c.doc())).to.be.true;
210 describe('free text handling', function() {
211 it('sees free text', function() {
212 var c = canvas.fromXML('<section>Alice <span>has</span> a cat</section>'),
213 children = c.doc().children();
214 expect(children.length).to.equal(3);
215 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
216 expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
217 expect(children[2]).to.be.instanceOf(documentElement.DocumentTextElement);
221 describe('white characters handling', function() {
222 it('says empty element node has no children', function() {
223 var c = canvas.fromXML('<section></section>');
224 expect(c.doc().children().length).to.equal(0);
226 it('says element node with one space has one DocumentTextElement', function() {
227 var c = canvas.fromXML('<section> </section>');
228 expect(c.doc().children().length).to.equal(1);
229 expect(c.doc().children()[0]).to.be.instanceOf(documentElement.DocumentTextElement);
230 expect(c.doc().children()[0].getText()).to.equal(' ');
232 it('ignores white space surrounding block elements', function() {
233 var c = canvas.fromXML('<section> <div></div> </section>');
234 var children = c.doc().children();
235 expect(children.length).to.equal(1);
236 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
238 it('ignores white space between block elements', function() {
239 var c = canvas.fromXML('<section><div></div> <div></div></section>');
240 var children = c.doc().children();
241 expect(children.length === 2);
242 [0,1].forEach(function(idx) {
243 expect(children[idx]).to.be.instanceOf(documentElement.DocumentNodeElement);
247 it('trims white space from the beginning and the end of the block elements', function() {
248 var c = canvas.fromXML('<section> Alice <span>has</span> a cat </section>');
249 expect(c.doc().children()[0].getText()).to.equal('Alice ');
250 expect(c.doc().children()[2].getText()).to.equal(' a cat');
253 it('normalizes string of white characters to one space at the inline element boundries', function() {
254 var c = canvas.fromXML('<span> Alice has a cat </span>');
255 expect(c.doc().children()[0].getText()).to.equal(' Alice has a cat ');
258 it('normalizes string of white characters to one space before inline element', function() {
259 var c = canvas.fromXML('<div>Alice has <span>a cat</span></div>');
260 expect(c.doc().children()[0].getText()).to.equal('Alice has ');
263 it('normalizes string of white characters to one space after inline element', function() {
264 var c = canvas.fromXML('<div>Alice has <span>a</span> cat</div>');
265 expect(c.doc().children()[2].getText()).to.equal(' cat');
269 describe('getting vertically first text element', function() {
270 it('returns the first child if it\'s text element, ignores metadata', function() {
271 var c = canvas.fromXML('<section><metadata><dc:author>author</dc:author></metadata>Alice<div>has</div>a cat</section>'),
272 first = c.doc().getVerticallyFirstTextElement();
274 expect(first.sameNode(c.doc().children()[1])).to.be.true;
277 it('looks recursively inside node elements if they precede text element', function() {
278 var c = canvas.fromXML('\
287 textAlice = c.doc().children()[0].children()[0].children()[0],
288 first = c.doc().getVerticallyFirstTextElement();
290 expect(textAlice).to.be.instanceOf(documentElement.DocumentTextElement);
291 expect(first.sameNode(textAlice)).to.be.true;
296 describe('manipulation api', function() {
298 describe('Basic Element inserting', function() {
299 it('can put new NodeElement at the end', function() {
300 var c = canvas.fromXML('<section></section>'),
301 appended = c.doc().append({tag: 'header', klass: 'some.class'}),
302 children = c.doc().children();
304 expect(children.length).to.equal(1);
305 expect(children[0].sameNode(appended)).to.be.true;
308 it('can put new TextElement at the end', function() {
309 var c = canvas.fromXML('<section></section>'),
310 appended = c.doc().append({text: 'Alice'}),
311 children = c.doc().children();
313 expect(children.length).to.equal(1);
314 expect(children[0].sameNode(appended)).to.be.true;
315 expect(children[0].getText()).to.equal('Alice');
318 it('can put new NodeElement after another NodeElement', function() {
319 var c = canvas.fromXML('<section><div></div></section>'),
320 div = c.doc().children()[0],
321 added = div.after({tag: 'header', klass: 'some.class'}),
322 children = c.doc().children();
323 expect(children.length).to.equal(2);
324 expect(children[1].sameNode(added)).to.be.true;
327 it('can put new Nodeelement before another element', function() {
328 var c = canvas.fromXML('<section><div></div></section>'),
329 div = c.doc().children()[0],
330 added = div.before({tag: 'header', klass: 'some.class'}),
331 children = c.doc().children();
332 expect(children.length).to.equal(2);
333 expect(children[0].sameNode(added)).to.be.true;
336 it('can put new DocumentNodeElement after DocumentTextElement', function() {
337 var c = canvas.fromXML('<section>Alice</section>'),
338 text = c.doc().children()[0],
339 added = text.after({tag: 'p'}),
340 children = c.doc().children();
342 expect(children.length).to.equal(2);
343 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
344 expect(children[0].getText()).to.equal('Alice');
345 expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
346 expect(children[1].sameNode(added)).to.be.true;
348 it('can put new DocumentNodeElement before DocumentTextElement', function() {
349 var c = canvas.fromXML('<section>Alice</section>'),
350 text = c.doc().children()[0],
351 added = text.before({tag: 'p'}),
352 children = c.doc().children();
354 expect(children.length).to.equal(2);
355 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
356 expect(children[0].sameNode(added)).to.be.true;
357 expect(children[1]).to.be.instanceOf(documentElement.DocumentTextElement);
358 expect(children[1].getText()).to.equal('Alice');
361 it('can divide DocumentTextElement with a new DocumentNodeElement', function() {
362 var c = canvas.fromXML('<section>Alice has a cat</section>'),
364 text = section.children()[0];
366 var returned = text.divide({tag: 'aside', klass: 'footnote', offset: 5}),
367 sectionChildren = section.children(),
368 lhsText = sectionChildren[0],
369 rhsText = sectionChildren[2];
371 expect(lhsText.getText()).to.equal('Alice');
372 expect(returned.sameNode(sectionChildren[1]));
373 expect(rhsText.getText()).to.equal(' has a cat');
376 it('treats dividing DocumentTextElement at the very end as appending after it', function() {
377 var c = canvas.fromXML('<section>Alice has a cat</section>'),
379 text = section.children()[0];
381 var returned = text.divide({tag: 'aside', klass: 'footnote', offset: 15}),
382 sectionChildren = section.children(),
383 textElement = sectionChildren[0],
384 nodeElement = sectionChildren[1];
386 expect(sectionChildren.length).to.equal(2);
387 expect(textElement.getText()).to.equal('Alice has a cat');
388 expect(returned.sameNode(nodeElement)).to.be.true;
389 expect(nodeElement.getWlxmlTag()).to.equal('aside');
392 it('treats dividing DocumentTextElement at the very beginning as appending before it', function() {
393 var c = canvas.fromXML('<section>Alice has a cat</section>'),
395 text = section.children()[0];
397 var returned = text.divide({tag: 'aside', klass: 'footnote', offset: 0}),
398 sectionChildren = section.children(),
399 nodeElement = sectionChildren[0],
400 textElement = sectionChildren[1];
402 expect(sectionChildren.length).to.equal(2);
403 expect(textElement.getText()).to.equal('Alice has a cat');
404 expect(returned.sameNode(nodeElement)).to.be.true;
405 expect(nodeElement.getWlxmlTag()).to.equal('aside');
409 describe('Splitting text', function() {
411 it('splits DocumentTextElement\'s parent into two DocumentNodeElements of the same type', function() {
412 var c = canvas.fromXML('<section><header>Some header</header></section>'),
414 text = section.children()[0].children()[0];
416 var returnedValue = text.split({offset: 5});
417 expect(section.children().length).to.equal(2, 'section has two children');
419 var header1 = section.children()[0];
420 var header2 = section.children()[1];
422 expect(header1.getWlxmlTag()).to.equal('header', 'first section child represents wlxml header');
423 expect(header1.children().length).to.equal(1, 'first header has one text child');
424 expect(header1.children()[0].getText()).to.equal('Some ', 'first header has correct content');
425 expect(header2.getWlxmlTag()).to.equal('header', 'second section child represents wlxml header');
426 expect(header2.children().length).to.equal(1, 'second header has one text child');
427 expect(header2.children()[0].getText()).to.equal('header', 'second header has correct content');
429 expect(returnedValue.first.sameNode(header1)).to.equal(true, 'first node returnde');
430 expect(returnedValue.second.sameNode(header2)).to.equal(true, 'second node returned');
433 it('leaves empty copy of DocumentNodeElement if splitting at the very beginning', function() {
434 var c = canvas.fromXML('<section><header>Some header</header></section>'),
436 text = section.children()[0].children()[0];
438 text.split({offset: 0});
440 var header1 = section.children()[0];
441 var header2 = section.children()[1];
443 expect(header1.children().length).to.equal(0);
444 expect(header2.children()[0].getText()).to.equal('Some header');
447 it('leaves empty copy of DocumentNodeElement if splitting at the very end', function() {
448 var c = canvas.fromXML('<section><header>Some header</header></section>'),
450 text = section.children()[0].children()[0];
452 text.split({offset: 11});
454 var header1 = section.children()[0];
455 var header2 = section.children()[1];
457 expect(header1.children()[0].getText()).to.equal('Some header');
458 expect(header2.children().length).to.equal(0);
461 it('keeps DocumentTextElement\'s parent\'s children elements intact', function() {
462 var c = canvas.fromXML('\
465 A <span>fancy</span> and <span>nice</span> header\
469 header = section.children()[0],
470 textAnd = header.children()[2];
472 textAnd.split({offset: 2});
474 var sectionChildren = section.children();
475 expect(sectionChildren.length).to.equal(2, 'Section has two children');
476 expect(sectionChildren[0].getWlxmlTag()).to.equal('header', 'First section element is a wlxml header');
477 expect(sectionChildren[1].getWlxmlTag()).to.equal('header', 'Second section element is a wlxml header');
479 var firstHeaderChildren = sectionChildren[0].children();
480 expect(firstHeaderChildren.length).to.equal(3, 'First header has three children');
481 expect(firstHeaderChildren[0].getText()).to.equal('A ', 'First header starts with a text');
482 expect(firstHeaderChildren[1].getWlxmlTag()).to.equal('span', 'First header has span in the middle');
483 expect(firstHeaderChildren[2].getText()).to.equal(' a', 'First header ends with text');
485 var secondHeaderChildren = sectionChildren[1].children();
486 expect(secondHeaderChildren.length).to.equal(3, 'Second header has three children');
487 expect(secondHeaderChildren[0].getText()).to.equal('nd ', 'Second header starts with text');
488 expect(secondHeaderChildren[1].getWlxmlTag()).to.equal('span', 'Second header has span in the middle');
489 expect(secondHeaderChildren[2].getText()).to.equal(' header', 'Second header ends with text');
493 describe('wrapping', function() {
494 it('wraps DocumentNodeElement', function() {
495 var c = canvas.fromXML('<section><div></div></section>'),
496 div = c.doc().children()[0];
498 var returned = div.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
499 parent = div.parent(),
500 parent2 = c.doc().children()[0];
502 expect(returned.sameNode(parent)).to.be.true;
503 expect(returned.sameNode(parent2)).to.be.true;
504 expect(returned.getWlxmlTag()).to.equal('header');
505 expect(returned.getWlxmlClass()).to.equal('some.class');
507 it('wraps DocumentTextElement', function() {
508 var c = canvas.fromXML('<section>Alice</section>'),
509 text = c.doc().children()[0];
511 var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
512 parent = text.parent(),
513 parent2 = c.doc().children()[0];
515 expect(returned.sameNode(parent)).to.be.true;
516 expect(returned.sameNode(parent2)).to.be.true;
517 expect(returned.getWlxmlTag()).to.equal('header');
518 expect(returned.getWlxmlClass()).to.equal('some.class');
521 describe('wrapping part of DocumentTextElement', function() {
522 [{start: 5, end: 12}, {start: 12, end: 5}].forEach(function(offsets) {
523 it('wraps in the middle ' + offsets.start + '/' + offsets.end, function() {
524 var c = canvas.fromXML('<section>Alice has a cat</section>'),
525 text = c.doc().children()[0];
527 var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class', start: offsets.start, end: offsets.end}),
528 children = c.doc().children();
530 expect(children.length).to.equal(3);
532 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
533 expect(children[0].getText()).to.equal('Alice');
535 expect(children[1].sameNode(returned)).to.be.true;
536 expect(returned.getWlxmlTag()).to.equal('header');
537 expect(returned.getWlxmlClass()).to.equal('some.class');
538 expect(children[1].children().length).to.equal(1);
539 expect(children[1].children()[0].getText()).to.equal(' has a ');
541 expect(children[2]).to.be.instanceOf(documentElement.DocumentTextElement);
542 expect(children[2].getText()).to.equal('cat');
546 it('wraps whole text inside DocumentTextElement if offsets span entire content', function() {
547 var c = canvas.fromXML('<section>Alice has a cat</section>'),
548 text = c.doc().children()[0];
550 var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class', start: 0, end: 15}),
551 children = c.doc().children();
553 expect(children.length).to.equal(1);
554 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
555 expect(children[0].children()[0].getText()).to.equal('Alice has a cat');
559 it('wraps text spanning multiple sibling DocumentTextNodes', function() {
560 var c = canvas.fromXML('<section>Alice has a <span>small</span> cat</section>'),
562 wrapper = c.wrapText({
564 _with: {tag: 'span', klass: 'some.class'},
570 expect(section.children().length).to.equal(2);
571 expect(section.children()[0]).to.be.instanceOf(documentElement.DocumentTextElement);
572 expect(section.children()[0].getText()).to.equal('Alice ');
574 var wrapper2 = section.children()[1];
575 expect(wrapper2.sameNode(wrapper)).to.be.true;
577 var wrapperChildren = wrapper.children();
578 expect(wrapperChildren.length).to.equal(3);
579 expect(wrapperChildren[0].getText()).to.equal('has a ');
581 expect(wrapperChildren[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
582 expect(wrapperChildren[1].children().length).to.equal(1);
583 expect(wrapperChildren[1].children()[0].getText()).to.equal('small');
585 expect(wrapperChildren[2].getText()).to.equal(' cat');
589 describe('unwrapping', function() {
590 it('unwraps DocumentTextElement from its parent DocumentNodeElement if it\'s its only child', function() {
591 var c = canvas.fromXML('<section>Alice <span>has a</span> cat</section>'),
593 text = section.children()[1].children()[0];
595 var newTextContainer = text.unwrap();
597 expect(section.children().length).to.equal(1, 'section has one child');
598 expect(section.children()[0].getText()).to.equal('Alice has a cat');
599 expect(newTextContainer.sameNode(c.doc())).to.equal(true, 'unwrap returns new text parent DocumentNodeElement');
604 describe('Lists api', function() {
605 describe('creating lists', function() {
606 it('allows creation of a list from existing sibling DocumentElements', function() {
607 var c = canvas.fromXML('\
615 textHas = section.children()[1],
616 divA = section.children()[2]
618 c.list.create({element1: textHas, element2: divA});
620 expect(section.children().length).to.equal(3, 'section has three child elements');
622 var child1 = section.children()[0],
623 list = section.children()[1],
624 child3 = section.children()[2];
626 expect(child1.getText()).to.equal('Alice');
627 expect(list.is('list')).to.equal(true, 'second child is a list');
628 expect(list.children().length).to.equal(2, 'list contains two elements');
629 list.children().forEach(function(child) {
630 expect(child.getWlxmlClass()).to.equal('item', 'list childs have wlxml class of item');
632 expect(child3.children()[0].getText()).to.equal('cat');
635 it('allows creating nested list from existing sibling list items', function() {
636 var c = canvas.fromXML('\
638 <div class="list-items">\
639 <div class="item">A</div>\
640 <div class="item">B</div>\
641 <div class="item">C</div>\
642 <div class="item">D</div>\
645 outerList = c.doc().children()[0],
646 itemB = outerList.children()[1],
647 itemC = outerList.children()[2];
650 c.list.create({element1: itemB, element2: itemC});
652 var outerListItems = outerList.children(),
653 innerList = outerListItems[1].children()[0],
654 innerListItems = innerList.children();
656 expect(outerListItems.length).to.equal(3, 'outer list has three items');
657 expect(outerListItems[0].children()[0].getText()).to.equal('A', 'first outer item ok');
658 expect(outerListItems[1].getWlxmlClass()).to.equal('item', 'inner list is wrapped by item element');
660 expect(innerList.is('list')).to.equal(true, 'inner list created');
661 expect(innerListItems.length).to.equal(2, 'inner list has two items');
662 expect(innerListItems[0].children()[0].getText()).to.equal('B', 'first inner item ok');
663 expect(innerListItems[1].children()[0].getText()).to.equal('C', 'second inner item ok');
665 expect(outerListItems[2].children()[0].getText()).to.equal('D', 'last outer item ok');
671 describe('extracting list items', function() {
672 it('creates two lists with extracted items in the middle if extracting from the middle of the list', function() {
673 var c = canvas.fromXML('\
675 <div class="list.items">\
676 <div class="item">0</div>\
677 <div class="item">1</div>\
678 <div class="item">2</div>\
679 <div class="item">3</div>\
682 list = c.doc().children()[0],
683 item1 = list.children()[1],
684 item2 = list.children()[2];
686 c.list.extractItems({element1: item1, element2: item2});
688 var section = c.doc(),
689 list1 = section.children()[0],
690 oldItem1 = section.children()[1],
691 oldItem2 = section.children()[2],
692 list2 = section.children()[3];
694 expect(section.children().length).to.equal(4, 'section contains four children');
696 expect(list1.is('list')).to.equal(true, 'first section child is a list');
697 expect(list1.children().length).to.equal(1, 'first list has one child');
698 expect(list1.children()[0].children()[0].getText()).to.equal('0', 'first item of the first list is a first item of the original list');
700 expect(oldItem1.children()[0].getText()).to.equal('1', 'first item got extracted');
701 expect(oldItem1.getWlxmlClass() === undefined).to.equal(true, 'first extracted element has no wlxml class');
703 expect(oldItem2.children()[0].getText()).to.equal('2', 'second item got extracted');
704 expect(oldItem2.getWlxmlClass() === undefined).to.equal(true, 'second extracted element has no wlxml class');
706 expect(list2.is('list')).to.equal(true, 'last section child is a list');
707 expect(list2.children().length).to.equal(1, 'second list has one child');
708 expect(list2.children()[0].children()[0].getText()).to.equal('3', 'first item of the second list is a last item of the original list');
711 it('puts extracted items above the list if starting item is the first one', function() {
712 var c = canvas.fromXML('\
714 <div class="list.items">\
715 <div class="item">0</div>\
716 <div class="item">1</div>\
717 <div class="item">2</div>\
720 list = c.doc().children()[0],
721 item1 = list.children()[0],
722 item2 = list.children()[1],
723 item3 = list.children()[2];
725 c.list.extractItems({element1: item1, element2: item2});
727 var section = c.doc(),
728 oldItem1 = section.children()[0],
729 oldItem2 = section.children()[1],
730 newList = section.children()[2];
732 expect(section.children().length).to.equal(3, 'section has three children');
733 expect(oldItem1.children()[0].getText()).to.equal('0', 'first item extracted');
734 expect(oldItem2.children()[0].getText()).to.equal('1', 'second item extracted');
735 expect(newList.is('list')).to.equal(true, 'list lies below extracted item');
736 expect(newList.children().length).to.equal(1, 'list has now one child');
739 it('puts extracted items below the list if ending item is the last one', function() {
740 var c = canvas.fromXML('\
742 <div class="list.items">\
743 <div class="item">0</div>\
744 <div class="item">1</div>\
745 <div class="item">2</div>\
748 list = c.doc().children()[0],
749 item1 = list.children()[0],
750 item2 = list.children()[1],
751 item3 = list.children()[2];
753 c.list.extractItems({element1: item2, element2: item3});
755 var section = c.doc(),
756 oldItem1 = section.children()[1],
757 oldItem2 = section.children()[2],
758 newList = section.children()[0];
760 expect(section.children().length).to.equal(3, 'section has three children');
761 expect(oldItem1.children()[0].getText()).to.equal('1', 'first item extracted');
762 expect(oldItem2.children()[0].getText()).to.equal('2', 'second item extracted');
763 expect(newList.is('list')).to.equal(true, 'list lies above extracted item');
764 expect(newList.children().length).to.equal(1, 'list has now one child');
767 it('removes list if all its items are extracted', function() {
768 var c = canvas.fromXML('\
770 <div class="list.items">\
771 <div class="item">some item</div>\
772 <div class="item">some item 2</div>\
775 list = c.doc().children()[0],
776 item1 = list.children()[0],
777 item2 = list.children()[1];
779 c.list.extractItems({element1: item1, element2: item2});
781 var section = c.doc(),
782 list1 = section.children()[0],
783 oldItem1 = section.children()[0],
784 oldItem2 = section.children()[1];
786 expect(section.children().length).to.equal(2, 'section contains two children');
787 expect(oldItem1.children()[0].getText()).to.equal('some item');
788 expect(oldItem2.children()[0].getText()).to.equal('some item 2');
791 it('creates two lists with extracted items in the middle if extracting from the middle of the list - nested case' , function() {
792 var c = canvas.fromXML('\
794 <div class="list.items">\
795 <div class="item">0</div>\
797 <div class="list.items">\
798 <div class="item">1.1</div>\
799 <div class="item">1.2</div>\
800 <div class="item">1.3</div>\
803 <div class="item">2</div>\
806 list = c.doc().children()[0],
807 nestedList = list.children()[1].children()[0],
808 nestedListItem = nestedList.children()[1];
810 c.list.extractItems({element1: nestedListItem, element2: nestedListItem});
812 var section = c.doc(),
813 list = section.children()[0],
814 item1 = list.children()[0],
815 item2 = list.children()[1], //
816 item3 = list.children()[2],
817 item4 = list.children()[3], //
818 item5 = list.children()[4],
819 nestedList1 = item2.children()[0],
820 nestedList2 = item4.children()[0];
822 expect(list.children().length).to.equal(5, 'top list has five items');
824 expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
826 expect(item2.getWlxmlClass()).to.equal('item', 'first nested list is still wrapped in item element');
827 expect(nestedList1.children().length).to.equal(1, 'first nested list is left with one child');
828 expect(nestedList1.children()[0].children()[0].getText()).to.equal('1.1', 'first nested list item left alone');
830 expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
832 expect(item4.getWlxmlClass()).to.equal('item', 'second nested list is still wrapped in item element');
833 expect(nestedList2.children().length).to.equal(1, 'second nested list is left with one child');
834 expect(nestedList2.children()[0].children()[0].getText()).to.equal('1.3', 'second nested list item left alone');
836 expect(item5.children()[0].getText()).to.equal('2', 'last item ok');
839 it('puts extracted items below the list if ending item is the last one - nested case' , function() {
840 var c = canvas.fromXML('\
842 <div class="list.items">\
843 <div class="item">0</div>\
845 <div class="list.items">\
846 <div class="item">1.1</div>\
847 <div class="item">1.2</div>\
848 <div class="item">1.3</div>\
851 <div class="item">2</div>\
854 list = c.doc().children()[0],
855 nestedList = list.children()[1].children()[0],
856 nestedListItem1 = nestedList.children()[1],
857 nestedListItem2 = nestedList.children()[2];
859 c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
861 var section = c.doc(),
862 list = section.children()[0],
863 item1 = list.children()[0],
864 item2 = list.children()[1],
865 item3 = list.children()[2],
866 item4 = list.children()[3],
867 item5 = list.children()[4];
868 nestedList = item2.children()[0];
870 expect(list.children().length).to.equal(5, 'top list has five items');
871 expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
872 expect(item2.getWlxmlClass()).to.equal('item', 'nested list is still wrapped in item element');
873 expect(nestedList.children().length).to.equal(1, 'nested list is left with one child');
874 expect(nestedList.children()[0].children()[0].getText()).to.equal('1.1', 'nested list item left alone');
875 expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
876 expect(item4.children()[0].getText()).to.equal('1.3', 'fourth item ok');
877 expect(item5.children()[0].getText()).to.equal('2', 'fifth item ok');
880 it('puts extracted items above the list if starting item is the first one - nested case' , function() {
881 var c = canvas.fromXML('\
883 <div class="list.items">\
884 <div class="item">0</div>\
886 <div class="list.items">\
887 <div class="item">1.1</div>\
888 <div class="item">1.2</div>\
889 <div class="item">1.3</div>\
892 <div class="item">2</div>\
895 list = c.doc().children()[0],
896 nestedList = list.children()[1].children()[0],
897 nestedListItem1 = nestedList.children()[0],
898 nestedListItem2 = nestedList.children()[1];
900 c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
902 var section = c.doc(),
903 list = section.children()[0],
904 item1 = list.children()[0],
905 item2 = list.children()[1],
906 item3 = list.children()[2],
907 item4 = list.children()[3],
908 item5 = list.children()[4];
909 nestedList = item4.children()[0];
911 expect(list.children().length).to.equal(5, 'top list has five items');
912 expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
913 expect(item2.children()[0].getText()).to.equal('1.1', 'second item ok');
914 expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
916 expect(item4.getWlxmlClass()).to.equal('item', 'nested list is still wrapped in item element');
917 expect(nestedList.children().length).to.equal(1, 'nested list is left with one child');
918 expect(nestedList.children()[0].children()[0].getText()).to.equal('1.3', 'nested list item left alone');
919 expect(item5.children()[0].getText()).to.equal('2', 'fifth item ok');
922 it('removes list if all its items are extracted - nested case', function() {
923 var c = canvas.fromXML('\
925 <div class="list.items">\
926 <div class="item">0</div>\
928 <div class="list.items">\
929 <div class="item">1.1</div>\
930 <div class="item">1.2</div>\
933 <div class="item">2</div>\
936 list = c.doc().children()[0],
937 nestedList = list.children()[1].children()[0],
938 nestedListItem1 = nestedList.children()[0],
939 nestedListItem2 = nestedList.children()[1];
941 c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
943 var section = c.doc(),
944 list = section.children()[0],
945 item1 = list.children()[0],
946 item2 = list.children()[1],
947 item3 = list.children()[2],
948 item4 = list.children()[3];
950 expect(list.children().length).to.equal(4, 'top list has four items');
951 expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
952 expect(item2.children()[0].getText()).to.equal('1.1', 'second item ok');
953 expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
954 expect(item4.children()[0].getText()).to.equal('2', 'fourth item ok');
957 it('extracts items out of outer most list when merge flag is set to false', function() {
958 var c = canvas.fromXML('\
960 <div class="list.items">\
961 <div class="item">0</div>\
963 <div class="list.items">\
964 <div class="item">1.1</div>\
965 <div class="item">1.2</div>\
968 <div class="item">2</div>\
972 list = section.children()[0],
973 nestedList = list.children()[1].children()[0],
974 nestedListItem = nestedList.children()[0];
976 var test = c.list.extractItems({element1: nestedListItem, element2: nestedListItem, merge: false});
978 expect(test).to.equal(true, 'extraction status ok');
980 var sectionChildren = section.children(),
981 extractedItem = sectionChildren[1];
983 expect(sectionChildren.length).to.equal(3, 'section has three children');
984 expect(sectionChildren[0].is('list')).to.equal(true, 'first child is a list');
986 expect(extractedItem.getWlxmlTag()).to.equal('div', 'extracted item is a wlxml div');
987 expect(extractedItem.getWlxmlClass()).to.equal(undefined, 'extracted item has no wlxml class');
988 expect(extractedItem.children()[0].getText()).to.equal('1.1', 'extracted item ok');
989 expect(sectionChildren[2].is('list')).to.equal(true, 'second child is a list');
996 describe('Cursor', function() {
1000 var findTextNode = function(inside, text) {
1001 var nodes = inside.find(':not(iframe)').addBack().contents().filter(function() {
1002 return this.nodeType === Node.TEXT_NODE && this.data === text;
1009 beforeEach(function() {
1010 getSelection = sinon.stub(window, 'getSelection');
1013 afterEach(function() {
1014 getSelection.restore();
1017 it('returns position when browser selection collapsed', function() {
1018 var c = canvas.fromXML('<section>Alice has a cat</section>'),
1019 dom = c.doc().dom(),
1020 text = findTextNode(dom, 'Alice has a cat');
1022 expect(text.nodeType).to.equal(Node.TEXT_NODE, 'correct node selected');
1023 expect($(text).text()).to.equal('Alice has a cat');
1025 getSelection.returns({
1032 var cursor = c.getCursor(),
1033 position = cursor.getPosition();
1035 expect(cursor.isSelecting()).to.equal(false, 'cursor is not selecting anything');
1036 expect(position.element.getText()).to.equal('Alice has a cat');
1037 expect(position.offset).to.equal(5);
1038 expect(position.offsetAtEnd).to.equal(false, 'offset is not at end');
1040 getSelection.returns({
1048 expect(cursor.getPosition().offsetAtEnd).to.equal(true, 'offset at end');
1051 it('returns boundries of selection when browser selection not collapsed', function() {
1052 var c = canvas.fromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
1053 dom = c.doc().dom(),
1055 alice: findTextNode(dom, 'Alice '),
1056 has: findTextNode(dom, 'has'),
1057 cat: findTextNode(dom, ' cat')
1059 cursor = c.getCursor(),
1060 aliceElement = c.getDocumentElement(text.alice),
1061 catElement = c.getDocumentElement(text.cat);
1065 {focus: text.alice, focusOffset: 1, anchor: text.cat, anchorOffset: 2, selectionAnchor: catElement},
1066 {focus: text.cat, focusOffset: 2, anchor: text.alice, anchorOffset: 1, selectionAnchor: aliceElement}
1067 ].forEach(function(s, idx) {
1068 getSelection.returns({isColapsed: false, anchorNode: s.anchor, anchorOffset: s.anchorOffset, focusNode: s.focus, focusOffset: s.focusOffset});
1070 var selectionStart = cursor.getSelectionStart(),
1071 selectionEnd = cursor.getSelectionEnd(),
1072 selectionAnchor = cursor.getSelectionAnchor();
1074 expect(cursor.isSelecting()).to.equal(true, 'cursor is selecting');
1075 expect(selectionStart.element.sameNode(aliceElement)).to.equal(true, '"Alice" is the start of the selection ' + idx);
1076 expect(selectionStart.offset).to.equal(1, '"Alice" offset ok' + idx);
1077 expect(selectionEnd.element.sameNode(catElement)).to.equal(true, '"Cat" is the start of the selection ' + idx);
1078 expect(selectionEnd.offset).to.equal(2, '"Cat" offset ok' + idx);
1079 expect(selectionAnchor.element.sameNode(s.selectionAnchor)).to.equal(true, 'anchor ok');
1080 expect(selectionAnchor.offset).to.equal(s.anchorOffset, 'anchor offset ok');
1084 it('recognizes when browser selection boundries lies in sibling DocumentTextElements', function() {
1085 var c = canvas.fromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
1086 dom = c.doc().dom(),
1088 alice: findTextNode(dom, 'Alice '),
1089 has: findTextNode(dom, 'has'),
1090 a: findTextNode(dom, ' a '),
1091 big: findTextNode(dom, 'big'),
1092 cat: findTextNode(dom, ' cat'),
1094 cursor = c.getCursor();
1096 expect($(text.alice).text()).to.equal('Alice ');
1097 expect($(text.has).text()).to.equal('has');
1098 expect($(text.a).text()).to.equal(' a ');
1099 expect($(text.big).text()).to.equal('big');
1100 expect($(text.cat).text()).to.equal(' cat');
1102 getSelection.returns({anchorNode: text.alice, focusNode: text.a});
1103 expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "a" are children');
1105 getSelection.returns({anchorNode: text.alice, focusNode: text.cat});
1106 expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "cat" are children');
1108 getSelection.returns({anchorNode: text.alice, focusNode: text.has});
1109 expect(cursor.isSelectingSiblings()).to.equal(false, '"Alice" and "has" are not children');
1111 getSelection.returns({anchorNode: text.has, focusNode: text.big});
1112 expect(cursor.isSelectingSiblings()).to.equal(false, '"has" and "big" are not children');
1116 describe('zero width space handling', function() {
1117 it('position range includes ZWS at the boundries of text in case when native selection api doesn\'t', function() {
1118 var c = canvas.fromXML("<section>Alice</section>"),
1119 dom = c.doc().dom(),
1120 textNode = findTextNode(dom, 'Alice'),
1121 cursor = c.getCursor();
1123 textNode.data = utils.unicode.ZWS + 'Alice';
1124 getSelection.returns({anchorNode: textNode, anchorOffset: 1, focusNode: textNode, focusOffset: 1});
1125 expect(cursor.getPosition().offset).to.equal(0);
1126 expect(cursor.getPosition().offsetAtBeginning).to.equal(true, 'offset at beginning');
1128 textNode.data = 'Alice' + utils.unicode.ZWS;
1129 getSelection.returns({anchorNode: textNode, anchorOffset: 5, focusNode: textNode, focusOffset: 5});
1130 expect(cursor.getPosition().offset).to.equal(6);
1131 expect(cursor.getPosition().offsetAtEnd).to.equal(true, 'offset at end');
1136 describe('Serializing document to WLXML', function() {
1137 it('keeps document intact when no changes have been made', function() {
1138 var xmlIn = '<section>Alice<div>has</div>a <span class="uri" meta-uri="http://cat.com">cat</span>!</section>',
1139 c = canvas.fromXML(xmlIn),
1142 var parser = new DOMParser(),
1143 input = parser.parseFromString(xmlIn, "application/xml").childNodes[0],
1144 output = parser.parseFromString(xmlOut, "application/xml").childNodes[0];
1146 expect(input.isEqualNode(output)).to.be.true;
1149 it('keeps arbitrary node attributes intact', function() {
1150 var xmlIn = '<section a="1" xmlns:dcterms="http://purl.org/dc/terms/"></section>',
1151 $xmlOut = $(canvas.fromXML(xmlIn).toXML());
1153 expect($xmlOut.attr('a')).to.equal('1');
1154 expect($xmlOut.attr('xmlns:dcterms')).to.equal('http://purl.org/dc/terms/');
1157 it('doesn\' serialize meta attribute if its empty', function() {
1160 c = canvas.fromXML('<section class="uri" meta-uri="some.uri"></section>');
1161 c.doc().setWlxmlMetaAttr('uri', '');
1162 expect($(c.toXML()).attr('meta-uri')).to.equal(undefined, 'overriding attribute with zero length string');
1164 c = canvas.fromXML('<section class="uri"></section>');
1165 c.doc().setWlxmlMetaAttr('uri', '');
1166 expect($(c.toXML()).attr('meta-uri')).to.equal(undefined, 'setting attribute to zero length string');
1169 describe('output xml', function() {
1170 it('keeps entities intact', function() {
1171 var xmlIn = '<section>< ></section>',
1172 c = canvas.fromXML(xmlIn),
1174 expect(xmlOut).to.equal(xmlIn);
1176 it('keeps entities intact when they form html/xml', function() {
1177 var xmlIn = '<section><abc></section>',
1178 c = canvas.fromXML(xmlIn),
1180 expect(xmlOut).to.equal(xmlIn);
1184 describe('formatting output xml', function() {
1185 /*it('keeps white spaces at the edges of input xml', function() {
1186 var xmlIn = ' <section></section> ',
1187 c = canvas.fromXML(xmlIn),
1190 expect(xmlOut.substr(4)).to.equal(' <', 'start');
1191 expect(xmlOut.substr(-2)).to.equal('> ', 'end');
1193 it('keeps white space between XML nodes', function() {
1194 var xmlIn = '<section>\n\n\n<div></div>\n\n\n<div></div>\n\n\n</section>',
1195 c = canvas.fromXML(xmlIn),
1198 var partsIn = xmlIn.split('\n\n\n'),
1199 partsOut = xmlOut.split('\n\n\n');
1201 expect(partsIn).to.deep.equal(partsOut);
1204 it('keeps white space between XML nodes - inline case', function() {
1205 var xmlIn = '<section>\n\n\n<span></span>\n\n\n<span></span>\n\n\n</section>',
1206 c = canvas.fromXML(xmlIn),
1209 var partsIn = xmlIn.split('\n\n\n'),
1210 partsOut = xmlOut.split('\n\n\n');
1212 expect(partsIn).to.deep.equal(partsOut);
1215 it('keeps white space at the beginning of text', function() {
1216 var xmlIn = '<section> abc<div>some div</div> abc</section>',
1217 c = canvas.fromXML(xmlIn),
1220 expect(xmlOut).to.equal(xmlIn);
1223 it('nests new children block elements', function() {
1224 var c = canvas.fromXML('<section></section>');
1226 c.doc().append({tag: 'header'});
1228 var xmlOut = c.toXML();
1229 expect(xmlOut.split('\n ')[0]).to.equal('<section>', 'nesting start ok');
1230 expect(xmlOut.split('\n').slice(-1)[0]).to.equal('</section>', 'nesting end ok');
1234 it('doesn\'t nest new children inline elements', function() {
1235 var c = canvas.fromXML('<section></section>');
1237 c.doc().append({tag: 'span'});
1239 var xmlOut = c.toXML();
1240 expect(xmlOut).to.equal('<section><span></span></section>');
1243 it('keeps original white space at the end of text', function() {
1245 var xmlIn = '<header> Some text ended with white space \
1247 <span class="uri">Some text</span> some text\
1250 c = canvas.fromXML(xmlIn);
1252 var xmlOut = c.toXML();
1253 console.log(xmlOut);
1254 expect(xmlOut).to.equal(xmlIn);
1257 it('keeps white space around text node', function() {
1258 var xmlIn = '<section>\
1259 <header>header1</header>\
1260 Some text surrounded by white space\
1261 <header>header2</header>\
1263 c = canvas.fromXML(xmlIn);
1265 var xmlOut = c.toXML();
1266 expect(xmlOut).to.equal(xmlIn);
1269 it('keeps white space around text node - last node case', function() {
1270 var xmlIn = '<section>\
1271 <header>header</header>\
1273 Some text surrounded by white space\
1276 c = canvas.fromXML(xmlIn);
1278 var xmlOut = c.toXML();
1279 expect(xmlOut).to.equal(xmlIn);
1282 it('keeps white space after detaching text element', function() {
1283 var xmlIn = '<section><header>header</header>\n\
1288 expectedXmlOut = '<section><header>header</header>\n\
1293 c = canvas.fromXML(xmlIn),
1294 children = c.doc().children(),
1295 text = children[children.length-1];
1297 expect(text.getText()).to.equal('text1');
1301 var xmlOut = c.toXML();
1302 expect(xmlOut).to.equal(expectedXmlOut);