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 DocumentNodeElement', function() {
16 it('is always a div tag', function() {
17 ['section', 'header', 'span', 'aside', 'figure'].forEach(function(tagName) {
18 var dom = canvas.fromXML('<' + tagName + '></' + tagName + '>').doc().dom();
19 expect(dom.prop('tagName')).to.equal('DIV', tagName + ' is represented as div');
22 it('has wlxml tag put into wlxml-tag attribute of its internal container', function() {
23 var dom = canvas.fromXML('<section></section>').doc().dom();
24 expect(dom.children('[document-element-content]').attr('wlxml-tag')).to.equal('section');
26 it('has wlxml class put into wlxml-class attribute of its internal containr, dots replaced with dashes', function() {
27 var dom = canvas.fromXML('<section class="some.class"></section>').doc().dom();
28 expect(dom.children('[document-element-content]').attr('wlxml-class')).to.equal('some-class');
32 describe('Internal HTML representation of a DocumentTextElement', function() {
33 it('is text node wrapped in a div with document-text-element attribute set', function() {
34 var dom = canvas.fromXML('<section>Alice</section>').doc().children()[0].dom();
35 expect(dom.prop('tagName')).to.equal('DIV');
36 expect(dom.attr('document-text-element')).to.equal('');
37 expect(dom.contents().length).to.equal(1);
38 expect(dom.contents()[0].nodeType).to.equal(Node.TEXT_NODE);
39 expect($(dom.contents()[0]).text()).to.equal('Alice');
43 describe('basic properties', function() {
44 it('renders empty document when canvas created from empty XML', function() {
45 var c = canvas.fromXML('');
46 expect(c.doc()).to.equal(null);
49 it('gives access to its document root node', function() {
50 var c = canvas.fromXML('<section></section>');
51 expect(c.doc().getWlxmlTag()).to.equal('section');
54 describe('root element', function() {
55 it('has no parent', function() {
56 var c = canvas.fromXML('<section></section>');
57 expect(c.doc().parent()).to.be.null;
61 describe('DocumentTextElement', function() {
62 it('can have its content set', function() {
63 var c = canvas.fromXML('<section>Alice</section>'),
65 text = root.children()[0];
67 text.setText('a cat');
68 expect(root.children()[0].getText()).to.equal('a cat');
72 describe('DocumentNodeElement', function() {
73 it('knows index of its child', function() {
74 var c = canvas.fromXML('<section><div></div><header></header><span></span></section>'),
76 child = root.children()[1];
77 expect(root.childIndex(child)).to.equal(1);
80 it('knows WLXML tag it renders', function(){
81 var c = canvas.fromXML('<section></section>'),
83 expect(section.getWlxmlTag()).to.equal('section', 'initial tag is section');
84 section.setWlxmlTag('header');
85 expect(section.getWlxmlTag()).to.equal('header', 'tag is changed to header');
88 it('knows WLXML class of a WLXML tag it renders', function(){
89 var c = canvas.fromXML('<section class="some.class.A"></section>'),
91 expect(section.getWlxmlClass()).to.equal('some.class.A');
92 section.setWlxmlClass('some.class.B');
93 expect(section.getWlxmlClass()).to.equal('some.class.B');
94 section.setWlxmlClass(null);
95 expect(section.getWlxmlClass()).to.be.undefined;
100 describe('element has meta attributes', function() {
101 it('can change its meta attributes', function() {
102 var c = canvas.fromXML('<section><span class="uri" meta-uri="someuri"></span></section>'),
103 span = c.doc().children()[0];
105 expect(span.getWlxmlMetaAttr('uri')).to.equal('someuri');
106 span.setWlxmlMetaAttr('uri', 'otheruri');
107 expect(span.getWlxmlMetaAttr('uri')).to.equal('otheruri');
110 it('changes its meta attributes with class change', function() {
111 var c = canvas.fromXML('<section><span class="uri" meta-uri="someuri"></span></section>'),
112 span = c.doc().children()[0];
114 expect(span.getWlxmlMetaAttr('uri')).to.equal('someuri');
115 span.setWlxmlClass('author');
116 expect(span.getWlxmlMetaAttr('uri')).to.be.undefined;
119 it('keeps meta attribute value on class change if a new class has this attribute', function() {
120 var c = canvas.fromXML('<section><span class="uri" meta-uri="someuri"></span></section>'),
121 span = c.doc().children()[0];
122 span.setWlxmlClass('uri.some.subclass');
123 expect(span.getWlxmlMetaAttr('uri')).to.equal('someuri');
128 it('returns DocumentNodeElement instance from HTMLElement', function() {
129 var c = canvas.fromXML('<section></section>'),
130 htmlElement = c.doc().dom().get(0),
131 element = c.getDocumentElement(htmlElement);
132 expect(element).to.be.instanceOf(documentElement.DocumentNodeElement);
133 expect(element.sameNode(c.doc()));
136 it('returns DocumentTextElement instance from Text Node', function() {
137 var c = canvas.fromXML('<section>Alice</section>'),
138 aliceElement = c.doc().children()[0],
139 textNode = aliceElement.dom().contents()[0],
140 element = c.getDocumentElement(textNode);
142 expect(textNode.nodeType).to.equal(Node.TEXT_NODE, 'text node selected');
143 expect($(textNode).text()).to.equal('Alice');
145 expect(element).to.be.instanceOf(documentElement.DocumentTextElement);
146 expect(element.sameNode(c.doc().children()[0]));
152 describe('document representation api', function() {
153 describe('document root element', function() {
154 var c = canvas.fromXML('<section></section>');
155 it('exists', function() {
156 expect(c.doc()).to.be.instanceOf(documentElement.DocumentElement);
158 it('is of type DocumentNodeElement', function() {
159 expect(c.doc()).to.be.instanceOf(documentElement.DocumentNodeElement);
163 describe('DocumentElements comparison', function() {
164 it('reports dwo DocumentElements to be the same when they represent the same wlxml document element', function() {
165 var c = canvas.fromXML('<section><div></div><div></div></section>'),
166 first_div1 = c.doc().children()[0],
167 first_div2 = c.doc().children()[0],
168 second_div = c.doc().children()[1];
169 expect(first_div1.sameNode(first_div1)).to.be.true;
170 expect(first_div1.sameNode(first_div2)).to.be.true;
171 expect(first_div1.sameNode(second_div)).to.be.false;
175 describe('traversing', function() {
176 it('reports element nodes', function() {
177 var c = canvas.fromXML('<section><div></div></section>'),
178 children = c.doc().children();
179 expect(children.length).to.equal(1);
180 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
182 c = canvas.fromXML('<section><div></div><div></div></section>'),
183 children = c.doc().children();
184 expect(children.length).to.equal(2);
185 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
186 expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
189 it('reports text nodes', function() {
190 var c = canvas.fromXML('<section>Alice</section>'),
191 children = c.doc().children();
192 expect(children.length).to.equal(1);
193 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
196 describe('accessing parents', function() {
197 it('returns DocumentNodeElement representing parent in wlxml document as DocumentNodeElement parent', function() {
198 var c = canvas.fromXML('<section><div></div></section>'),
199 div = c.doc().children()[0];
200 expect(div.parent().sameNode(c.doc())).to.be.true;
202 it('returns DocumentNodeElement representing parent in wlxml document as DocumentTextElement parent', function() {
203 var c = canvas.fromXML('<section>Alice</section>'),
204 text = c.doc().children()[0];
205 expect(text.parent().sameNode(c.doc())).to.be.true;
209 describe('free text handling', function() {
210 it('sees free text', function() {
211 var c = canvas.fromXML('<section>Alice <span>has</span> a cat</section>'),
212 children = c.doc().children();
213 expect(children.length).to.equal(3);
214 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
215 expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
216 expect(children[2]).to.be.instanceOf(documentElement.DocumentTextElement);
220 describe('white characters handling', function() {
221 it('says empty element node has no children', function() {
222 var c = canvas.fromXML('<section></section>');
223 expect(c.doc().children().length).to.equal(0);
225 it('says element node with one space has one DocumentTextElement', function() {
226 var c = canvas.fromXML('<section> </section>');
227 expect(c.doc().children().length).to.equal(1);
228 expect(c.doc().children()[0]).to.be.instanceOf(documentElement.DocumentTextElement);
229 expect(c.doc().children()[0].getText()).to.equal(' ');
231 it('ignores white space surrounding block elements', function() {
232 var c = canvas.fromXML('<section> <div></div> </section>');
233 var children = c.doc().children();
234 expect(children.length).to.equal(1);
235 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
237 it('ignores white space between block elements', function() {
238 var c = canvas.fromXML('<section><div></div> <div></div></section>');
239 var children = c.doc().children();
240 expect(children.length === 2);
241 [0,1].forEach(function(idx) {
242 expect(children[idx]).to.be.instanceOf(documentElement.DocumentNodeElement);
246 it('trims white space from the beginning and the end of the block elements', function() {
247 var c = canvas.fromXML('<section> Alice <span>has</span> a cat </section>');
248 expect(c.doc().children()[0].getText()).to.equal('Alice ');
249 expect(c.doc().children()[2].getText()).to.equal(' a cat');
252 it('normalizes string of white characters to one space at the inline element boundries', function() {
253 var c = canvas.fromXML('<span> Alice has a cat </span>');
254 expect(c.doc().children()[0].getText()).to.equal(' Alice has a cat ');
257 it('normalizes string of white characters to one space before inline element', function() {
258 var c = canvas.fromXML('<div>Alice has <span>a cat</span></div>');
259 expect(c.doc().children()[0].getText()).to.equal('Alice has ');
262 it('normalizes string of white characters to one space after inline element', function() {
263 var c = canvas.fromXML('<div>Alice has <span>a</span> cat</div>');
264 expect(c.doc().children()[2].getText()).to.equal(' cat');
268 describe('getting vertically first text element', function() {
269 it('returns the first child if it\'s text element, ignores metadata', function() {
270 var c = canvas.fromXML('<section><metadata><dc:author>author</dc:author></metadata>Alice<div>has</div>a cat</section>'),
271 first = c.doc().getVerticallyFirstTextElement();
273 expect(first.sameNode(c.doc().children()[1])).to.be.true;
276 it('looks recursively inside node elements if they precede text element', function() {
277 var c = canvas.fromXML('\
286 textAlice = c.doc().children()[0].children()[0].children()[0],
287 first = c.doc().getVerticallyFirstTextElement();
289 expect(textAlice).to.be.instanceOf(documentElement.DocumentTextElement);
290 expect(first.sameNode(textAlice)).to.be.true;
295 describe('manipulation api', function() {
297 describe('Basic Element inserting', function() {
298 it('can put new NodeElement at the end', function() {
299 var c = canvas.fromXML('<section></section>'),
300 appended = c.doc().append({tag: 'header', klass: 'some.class'}),
301 children = c.doc().children();
303 expect(children.length).to.equal(1);
304 expect(children[0].sameNode(appended)).to.be.true;
307 it('can put new TextElement at the end', function() {
308 var c = canvas.fromXML('<section></section>'),
309 appended = c.doc().append({text: 'Alice'}),
310 children = c.doc().children();
312 expect(children.length).to.equal(1);
313 expect(children[0].sameNode(appended)).to.be.true;
314 expect(children[0].getText()).to.equal('Alice');
317 it('can put new NodeElement after another NodeElement', function() {
318 var c = canvas.fromXML('<section><div></div></section>'),
319 div = c.doc().children()[0],
320 added = div.after({tag: 'header', klass: 'some.class'}),
321 children = c.doc().children();
322 expect(children.length).to.equal(2);
323 expect(children[1].sameNode(added)).to.be.true;
326 it('can put new Nodeelement before another element', function() {
327 var c = canvas.fromXML('<section><div></div></section>'),
328 div = c.doc().children()[0],
329 added = div.before({tag: 'header', klass: 'some.class'}),
330 children = c.doc().children();
331 expect(children.length).to.equal(2);
332 expect(children[0].sameNode(added)).to.be.true;
335 it('can put new DocumentNodeElement after DocumentTextElement', function() {
336 var c = canvas.fromXML('<section>Alice</section>'),
337 text = c.doc().children()[0],
338 added = text.after({tag: 'p'}),
339 children = c.doc().children();
341 expect(children.length).to.equal(2);
342 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
343 expect(children[0].getText()).to.equal('Alice');
344 expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
345 expect(children[1].sameNode(added)).to.be.true;
347 it('can put new DocumentNodeElement before DocumentTextElement', function() {
348 var c = canvas.fromXML('<section>Alice</section>'),
349 text = c.doc().children()[0],
350 added = text.before({tag: 'p'}),
351 children = c.doc().children();
353 expect(children.length).to.equal(2);
354 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
355 expect(children[0].sameNode(added)).to.be.true;
356 expect(children[1]).to.be.instanceOf(documentElement.DocumentTextElement);
357 expect(children[1].getText()).to.equal('Alice');
360 it('can divide DocumentTextElement with a new DocumentNodeElement', function() {
361 var c = canvas.fromXML('<section>Alice has a cat</section>'),
363 text = section.children()[0];
365 var returned = text.divide({tag: 'aside', klass: 'footnote', offset: 5}),
366 sectionChildren = section.children(),
367 lhsText = sectionChildren[0],
368 rhsText = sectionChildren[2];
370 expect(lhsText.getText()).to.equal('Alice');
371 expect(returned.sameNode(sectionChildren[1]));
372 expect(rhsText.getText()).to.equal(' has a cat');
375 it('treats dividing DocumentTextElement at the very end as appending after it', function() {
376 var c = canvas.fromXML('<section>Alice has a cat</section>'),
378 text = section.children()[0];
380 var returned = text.divide({tag: 'aside', klass: 'footnote', offset: 15}),
381 sectionChildren = section.children(),
382 textElement = sectionChildren[0],
383 nodeElement = sectionChildren[1];
385 expect(sectionChildren.length).to.equal(2);
386 expect(textElement.getText()).to.equal('Alice has a cat');
387 expect(returned.sameNode(nodeElement)).to.be.true;
388 expect(nodeElement.getWlxmlTag()).to.equal('aside');
391 it('treats dividing DocumentTextElement at the very beginning as appending before it', function() {
392 var c = canvas.fromXML('<section>Alice has a cat</section>'),
394 text = section.children()[0];
396 var returned = text.divide({tag: 'aside', klass: 'footnote', offset: 0}),
397 sectionChildren = section.children(),
398 nodeElement = sectionChildren[0],
399 textElement = sectionChildren[1];
401 expect(sectionChildren.length).to.equal(2);
402 expect(textElement.getText()).to.equal('Alice has a cat');
403 expect(returned.sameNode(nodeElement)).to.be.true;
404 expect(nodeElement.getWlxmlTag()).to.equal('aside');
408 describe('Splitting text', function() {
410 it('splits DocumentTextElement\'s parent into two DocumentNodeElements of the same type', function() {
411 var c = canvas.fromXML('<section><header>Some header</header></section>'),
413 text = section.children()[0].children()[0];
415 var returnedValue = text.split({offset: 5});
416 expect(section.children().length).to.equal(2, 'section has two children');
418 var header1 = section.children()[0];
419 var header2 = section.children()[1];
421 expect(header1.getWlxmlTag()).to.equal('header', 'first section child represents wlxml header');
422 expect(header1.children().length).to.equal(1, 'first header has one text child');
423 expect(header1.children()[0].getText()).to.equal('Some ', 'first header has correct content');
424 expect(header2.getWlxmlTag()).to.equal('header', 'second section child represents wlxml header');
425 expect(header2.children().length).to.equal(1, 'second header has one text child');
426 expect(header2.children()[0].getText()).to.equal('header', 'second header has correct content');
428 expect(returnedValue.first.sameNode(header1)).to.equal(true, 'first node returnde');
429 expect(returnedValue.second.sameNode(header2)).to.equal(true, 'second node returned');
432 it('leaves empty copy of DocumentNodeElement if splitting at the very beginning', function() {
433 var c = canvas.fromXML('<section><header>Some header</header></section>'),
435 text = section.children()[0].children()[0];
437 text.split({offset: 0});
439 var header1 = section.children()[0];
440 var header2 = section.children()[1];
442 expect(header1.children().length).to.equal(0);
443 expect(header2.children()[0].getText()).to.equal('Some header');
446 it('leaves empty copy of DocumentNodeElement if splitting at the very end', function() {
447 var c = canvas.fromXML('<section><header>Some header</header></section>'),
449 text = section.children()[0].children()[0];
451 text.split({offset: 11});
453 var header1 = section.children()[0];
454 var header2 = section.children()[1];
456 expect(header1.children()[0].getText()).to.equal('Some header');
457 expect(header2.children().length).to.equal(0);
460 it('keeps DocumentTextElement\'s parent\'s children elements intact', function() {
461 var c = canvas.fromXML('\
464 A <span>fancy</span> and <span>nice</span> header\
468 header = section.children()[0],
469 textAnd = header.children()[2];
471 textAnd.split({offset: 2});
473 var sectionChildren = section.children();
474 expect(sectionChildren.length).to.equal(2, 'Section has two children');
475 expect(sectionChildren[0].getWlxmlTag()).to.equal('header', 'First section element is a wlxml header');
476 expect(sectionChildren[1].getWlxmlTag()).to.equal('header', 'Second section element is a wlxml header');
478 var firstHeaderChildren = sectionChildren[0].children();
479 expect(firstHeaderChildren.length).to.equal(3, 'First header has three children');
480 expect(firstHeaderChildren[0].getText()).to.equal('A ', 'First header starts with a text');
481 expect(firstHeaderChildren[1].getWlxmlTag()).to.equal('span', 'First header has span in the middle');
482 expect(firstHeaderChildren[2].getText()).to.equal(' a', 'First header ends with text');
484 var secondHeaderChildren = sectionChildren[1].children();
485 expect(secondHeaderChildren.length).to.equal(3, 'Second header has three children');
486 expect(secondHeaderChildren[0].getText()).to.equal('nd ', 'Second header starts with text');
487 expect(secondHeaderChildren[1].getWlxmlTag()).to.equal('span', 'Second header has span in the middle');
488 expect(secondHeaderChildren[2].getText()).to.equal(' header', 'Second header ends with text');
492 describe('wrapping', function() {
493 it('wraps DocumentNodeElement', function() {
494 var c = canvas.fromXML('<section><div></div></section>'),
495 div = c.doc().children()[0];
497 var returned = div.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
498 parent = div.parent(),
499 parent2 = c.doc().children()[0];
501 expect(returned.sameNode(parent)).to.be.true;
502 expect(returned.sameNode(parent2)).to.be.true;
503 expect(returned.getWlxmlTag()).to.equal('header');
504 expect(returned.getWlxmlClass()).to.equal('some.class');
506 it('wraps DocumentTextElement', function() {
507 var c = canvas.fromXML('<section>Alice</section>'),
508 text = c.doc().children()[0];
510 var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
511 parent = text.parent(),
512 parent2 = c.doc().children()[0];
514 expect(returned.sameNode(parent)).to.be.true;
515 expect(returned.sameNode(parent2)).to.be.true;
516 expect(returned.getWlxmlTag()).to.equal('header');
517 expect(returned.getWlxmlClass()).to.equal('some.class');
520 describe('wrapping part of DocumentTextElement', function() {
521 [{start: 5, end: 12}, {start: 12, end: 5}].forEach(function(offsets) {
522 it('wraps in the middle ' + offsets.start + '/' + offsets.end, function() {
523 var c = canvas.fromXML('<section>Alice has a cat</section>'),
524 text = c.doc().children()[0];
526 var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class', start: offsets.start, end: offsets.end}),
527 children = c.doc().children();
529 expect(children.length).to.equal(3);
531 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
532 expect(children[0].getText()).to.equal('Alice');
534 expect(children[1].sameNode(returned)).to.be.true;
535 expect(returned.getWlxmlTag()).to.equal('header');
536 expect(returned.getWlxmlClass()).to.equal('some.class');
537 expect(children[1].children().length).to.equal(1);
538 expect(children[1].children()[0].getText()).to.equal(' has a ');
540 expect(children[2]).to.be.instanceOf(documentElement.DocumentTextElement);
541 expect(children[2].getText()).to.equal('cat');
545 it('wraps whole text inside DocumentTextElement if offsets span entire content', function() {
546 var c = canvas.fromXML('<section>Alice has a cat</section>'),
547 text = c.doc().children()[0];
549 var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class', start: 0, end: 15}),
550 children = c.doc().children();
552 expect(children.length).to.equal(1);
553 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
554 expect(children[0].children()[0].getText()).to.equal('Alice has a cat');
558 it('wraps text spanning multiple sibling DocumentTextNodes', function() {
559 var c = canvas.fromXML('<section>Alice has a <span>small</span> cat</section>'),
561 wrapper = c.wrapText({
563 _with: {tag: 'span', klass: 'some.class'},
569 expect(section.children().length).to.equal(2);
570 expect(section.children()[0]).to.be.instanceOf(documentElement.DocumentTextElement);
571 expect(section.children()[0].getText()).to.equal('Alice ');
573 var wrapper2 = section.children()[1];
574 expect(wrapper2.sameNode(wrapper)).to.be.true;
576 var wrapperChildren = wrapper.children();
577 expect(wrapperChildren.length).to.equal(3);
578 expect(wrapperChildren[0].getText()).to.equal('has a ');
580 expect(wrapperChildren[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
581 expect(wrapperChildren[1].children().length).to.equal(1);
582 expect(wrapperChildren[1].children()[0].getText()).to.equal('small');
584 expect(wrapperChildren[2].getText()).to.equal(' cat');
588 describe('unwrapping', function() {
589 it('unwraps DocumentTextElement from its parent DocumentNodeElement if it\'s its only child', function() {
590 var c = canvas.fromXML('<section>Alice <span>has a</span> cat</section>'),
592 text = section.children()[1].children()[0];
594 var newTextContainer = text.unwrap();
596 expect(section.children().length).to.equal(1, 'section has one child');
597 expect(section.children()[0].getText()).to.equal('Alice has a cat');
598 expect(newTextContainer.sameNode(c.doc())).to.equal(true, 'unwrap returns new text parent DocumentNodeElement');
603 describe('Lists api', function() {
604 describe('creating lists', function() {
605 it('allows creation of a list from existing sibling DocumentElements', function() {
606 var c = canvas.fromXML('\
614 textHas = section.children()[1],
615 divA = section.children()[2]
617 c.list.create({element1: textHas, element2: divA});
619 expect(section.children().length).to.equal(3, 'section has three child elements');
621 var child1 = section.children()[0],
622 list = section.children()[1],
623 child3 = section.children()[2];
625 expect(child1.getText()).to.equal('Alice');
626 expect(list.is('list')).to.equal(true, 'second child is a list');
627 expect(list.children().length).to.equal(2, 'list contains two elements');
628 list.children().forEach(function(child) {
629 expect(child.getWlxmlClass()).to.equal('item', 'list childs have wlxml class of item');
631 expect(child3.children()[0].getText()).to.equal('cat');
634 it('allows creating nested list from existing sibling list items', function() {
635 var c = canvas.fromXML('\
637 <div class="list-items">\
638 <div class="item">A</div>\
639 <div class="item">B</div>\
640 <div class="item">C</div>\
641 <div class="item">D</div>\
644 outerList = c.doc().children()[0],
645 itemB = outerList.children()[1],
646 itemC = outerList.children()[2];
649 c.list.create({element1: itemB, element2: itemC});
651 var outerListItems = outerList.children(),
652 innerList = outerListItems[1].children()[0],
653 innerListItems = innerList.children();
655 expect(outerListItems.length).to.equal(3, 'outer list has three items');
656 expect(outerListItems[0].children()[0].getText()).to.equal('A', 'first outer item ok');
657 expect(outerListItems[1].getWlxmlClass()).to.equal('item', 'inner list is wrapped by item element');
659 expect(innerList.is('list')).to.equal(true, 'inner list created');
660 expect(innerListItems.length).to.equal(2, 'inner list has two items');
661 expect(innerListItems[0].children()[0].getText()).to.equal('B', 'first inner item ok');
662 expect(innerListItems[1].children()[0].getText()).to.equal('C', 'second inner item ok');
664 expect(outerListItems[2].children()[0].getText()).to.equal('D', 'last outer item ok');
670 describe('extracting list items', function() {
671 it('creates two lists with extracted items in the middle if extracting from the middle of the list', function() {
672 var c = canvas.fromXML('\
674 <div class="list.items">\
675 <div class="item">0</div>\
676 <div class="item">1</div>\
677 <div class="item">2</div>\
678 <div class="item">3</div>\
681 list = c.doc().children()[0],
682 item1 = list.children()[1],
683 item2 = list.children()[2];
685 c.list.extractItems({element1: item1, element2: item2});
687 var section = c.doc(),
688 list1 = section.children()[0],
689 oldItem1 = section.children()[1],
690 oldItem2 = section.children()[2],
691 list2 = section.children()[3];
693 expect(section.children().length).to.equal(4, 'section contains four children');
695 expect(list1.is('list')).to.equal(true, 'first section child is a list');
696 expect(list1.children().length).to.equal(1, 'first list has one child');
697 expect(list1.children()[0].children()[0].getText()).to.equal('0', 'first item of the first list is a first item of the original list');
699 expect(oldItem1.children()[0].getText()).to.equal('1', 'first item got extracted');
700 expect(oldItem1.getWlxmlClass() === undefined).to.equal(true, 'first extracted element has no wlxml class');
702 expect(oldItem2.children()[0].getText()).to.equal('2', 'second item got extracted');
703 expect(oldItem2.getWlxmlClass() === undefined).to.equal(true, 'second extracted element has no wlxml class');
705 expect(list2.is('list')).to.equal(true, 'last section child is a list');
706 expect(list2.children().length).to.equal(1, 'second list has one child');
707 expect(list2.children()[0].children()[0].getText()).to.equal('3', 'first item of the second list is a last item of the original list');
710 it('puts extracted items above the list if starting item is the first one', function() {
711 var c = canvas.fromXML('\
713 <div class="list.items">\
714 <div class="item">0</div>\
715 <div class="item">1</div>\
716 <div class="item">2</div>\
719 list = c.doc().children()[0],
720 item1 = list.children()[0],
721 item2 = list.children()[1],
722 item3 = list.children()[2];
724 c.list.extractItems({element1: item1, element2: item2});
726 var section = c.doc(),
727 oldItem1 = section.children()[0],
728 oldItem2 = section.children()[1],
729 newList = section.children()[2];
731 expect(section.children().length).to.equal(3, 'section has three children');
732 expect(oldItem1.children()[0].getText()).to.equal('0', 'first item extracted');
733 expect(oldItem2.children()[0].getText()).to.equal('1', 'second item extracted');
734 expect(newList.is('list')).to.equal(true, 'list lies below extracted item');
735 expect(newList.children().length).to.equal(1, 'list has now one child');
738 it('puts extracted items below the list if ending item is the last one', function() {
739 var c = canvas.fromXML('\
741 <div class="list.items">\
742 <div class="item">0</div>\
743 <div class="item">1</div>\
744 <div class="item">2</div>\
747 list = c.doc().children()[0],
748 item1 = list.children()[0],
749 item2 = list.children()[1],
750 item3 = list.children()[2];
752 c.list.extractItems({element1: item2, element2: item3});
754 var section = c.doc(),
755 oldItem1 = section.children()[1],
756 oldItem2 = section.children()[2],
757 newList = section.children()[0];
759 expect(section.children().length).to.equal(3, 'section has three children');
760 expect(oldItem1.children()[0].getText()).to.equal('1', 'first item extracted');
761 expect(oldItem2.children()[0].getText()).to.equal('2', 'second item extracted');
762 expect(newList.is('list')).to.equal(true, 'list lies above extracted item');
763 expect(newList.children().length).to.equal(1, 'list has now one child');
766 it('removes list if all its items are extracted', function() {
767 var c = canvas.fromXML('\
769 <div class="list.items">\
770 <div class="item">some item</div>\
771 <div class="item">some item 2</div>\
774 list = c.doc().children()[0],
775 item1 = list.children()[0],
776 item2 = list.children()[1];
778 c.list.extractItems({element1: item1, element2: item2});
780 var section = c.doc(),
781 list1 = section.children()[0],
782 oldItem1 = section.children()[0],
783 oldItem2 = section.children()[1];
785 expect(section.children().length).to.equal(2, 'section contains two children');
786 expect(oldItem1.children()[0].getText()).to.equal('some item');
787 expect(oldItem2.children()[0].getText()).to.equal('some item 2');
790 it('creates two lists with extracted items in the middle if extracting from the middle of the list - nested case' , function() {
791 var c = canvas.fromXML('\
793 <div class="list.items">\
794 <div class="item">0</div>\
796 <div class="list.items">\
797 <div class="item">1.1</div>\
798 <div class="item">1.2</div>\
799 <div class="item">1.3</div>\
802 <div class="item">2</div>\
805 list = c.doc().children()[0],
806 nestedList = list.children()[1].children()[0],
807 nestedListItem = nestedList.children()[1];
809 c.list.extractItems({element1: nestedListItem, element2: nestedListItem});
811 var section = c.doc(),
812 list = section.children()[0],
813 item1 = list.children()[0],
814 item2 = list.children()[1], //
815 item3 = list.children()[2],
816 item4 = list.children()[3], //
817 item5 = list.children()[4],
818 nestedList1 = item2.children()[0],
819 nestedList2 = item4.children()[0];
821 expect(list.children().length).to.equal(5, 'top list has five items');
823 expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
825 expect(item2.getWlxmlClass()).to.equal('item', 'first nested list is still wrapped in item element');
826 expect(nestedList1.children().length).to.equal(1, 'first nested list is left with one child');
827 expect(nestedList1.children()[0].children()[0].getText()).to.equal('1.1', 'first nested list item left alone');
829 expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
831 expect(item4.getWlxmlClass()).to.equal('item', 'second nested list is still wrapped in item element');
832 expect(nestedList2.children().length).to.equal(1, 'second nested list is left with one child');
833 expect(nestedList2.children()[0].children()[0].getText()).to.equal('1.3', 'second nested list item left alone');
835 expect(item5.children()[0].getText()).to.equal('2', 'last item ok');
838 it('puts extracted items below the list if ending item is the last one - nested case' , function() {
839 var c = canvas.fromXML('\
841 <div class="list.items">\
842 <div class="item">0</div>\
844 <div class="list.items">\
845 <div class="item">1.1</div>\
846 <div class="item">1.2</div>\
847 <div class="item">1.3</div>\
850 <div class="item">2</div>\
853 list = c.doc().children()[0],
854 nestedList = list.children()[1].children()[0],
855 nestedListItem1 = nestedList.children()[1],
856 nestedListItem2 = nestedList.children()[2];
858 c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
860 var section = c.doc(),
861 list = section.children()[0],
862 item1 = list.children()[0],
863 item2 = list.children()[1],
864 item3 = list.children()[2],
865 item4 = list.children()[3],
866 item5 = list.children()[4];
867 nestedList = item2.children()[0];
869 expect(list.children().length).to.equal(5, 'top list has five items');
870 expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
871 expect(item2.getWlxmlClass()).to.equal('item', 'nested list is still wrapped in item element');
872 expect(nestedList.children().length).to.equal(1, 'nested list is left with one child');
873 expect(nestedList.children()[0].children()[0].getText()).to.equal('1.1', 'nested list item left alone');
874 expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
875 expect(item4.children()[0].getText()).to.equal('1.3', 'fourth item ok');
876 expect(item5.children()[0].getText()).to.equal('2', 'fifth item ok');
879 it('puts extracted items above the list if starting item is the first one - nested case' , function() {
880 var c = canvas.fromXML('\
882 <div class="list.items">\
883 <div class="item">0</div>\
885 <div class="list.items">\
886 <div class="item">1.1</div>\
887 <div class="item">1.2</div>\
888 <div class="item">1.3</div>\
891 <div class="item">2</div>\
894 list = c.doc().children()[0],
895 nestedList = list.children()[1].children()[0],
896 nestedListItem1 = nestedList.children()[0],
897 nestedListItem2 = nestedList.children()[1];
899 c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
901 var section = c.doc(),
902 list = section.children()[0],
903 item1 = list.children()[0],
904 item2 = list.children()[1],
905 item3 = list.children()[2],
906 item4 = list.children()[3],
907 item5 = list.children()[4];
908 nestedList = item4.children()[0];
910 expect(list.children().length).to.equal(5, 'top list has five items');
911 expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
912 expect(item2.children()[0].getText()).to.equal('1.1', 'second item ok');
913 expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
915 expect(item4.getWlxmlClass()).to.equal('item', 'nested list is still wrapped in item element');
916 expect(nestedList.children().length).to.equal(1, 'nested list is left with one child');
917 expect(nestedList.children()[0].children()[0].getText()).to.equal('1.3', 'nested list item left alone');
918 expect(item5.children()[0].getText()).to.equal('2', 'fifth item ok');
921 it('removes list if all its items are extracted - nested case', function() {
922 var c = canvas.fromXML('\
924 <div class="list.items">\
925 <div class="item">0</div>\
927 <div class="list.items">\
928 <div class="item">1.1</div>\
929 <div class="item">1.2</div>\
932 <div class="item">2</div>\
935 list = c.doc().children()[0],
936 nestedList = list.children()[1].children()[0],
937 nestedListItem1 = nestedList.children()[0],
938 nestedListItem2 = nestedList.children()[1];
940 c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
942 var section = c.doc(),
943 list = section.children()[0],
944 item1 = list.children()[0],
945 item2 = list.children()[1],
946 item3 = list.children()[2],
947 item4 = list.children()[3];
949 expect(list.children().length).to.equal(4, 'top list has four items');
950 expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
951 expect(item2.children()[0].getText()).to.equal('1.1', 'second item ok');
952 expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
953 expect(item4.children()[0].getText()).to.equal('2', 'fourth item ok');
956 it('extracts items out of outer most list when merge flag is set to false', function() {
957 var c = canvas.fromXML('\
959 <div class="list.items">\
960 <div class="item">0</div>\
962 <div class="list.items">\
963 <div class="item">1.1</div>\
964 <div class="item">1.2</div>\
967 <div class="item">2</div>\
971 list = section.children()[0],
972 nestedList = list.children()[1].children()[0],
973 nestedListItem = nestedList.children()[0];
975 var test = c.list.extractItems({element1: nestedListItem, element2: nestedListItem, merge: false});
977 expect(test).to.equal(true, 'extraction status ok');
979 var sectionChildren = section.children(),
980 extractedItem = sectionChildren[1];
982 expect(sectionChildren.length).to.equal(3, 'section has three children');
983 expect(sectionChildren[0].is('list')).to.equal(true, 'first child is a list');
985 expect(extractedItem.getWlxmlTag()).to.equal('div', 'extracted item is a wlxml div');
986 expect(extractedItem.getWlxmlClass()).to.equal(undefined, 'extracted item has no wlxml class');
987 expect(extractedItem.children()[0].getText()).to.equal('1.1', 'extracted item ok');
988 expect(sectionChildren[2].is('list')).to.equal(true, 'second child is a list');
995 describe('Cursor', function() {
999 var findTextNode = function(inside, text) {
1000 var nodes = inside.find(':not(iframe)').addBack().contents().filter(function() {
1001 return this.nodeType === Node.TEXT_NODE && this.data === text;
1008 beforeEach(function() {
1009 getSelection = sinon.stub(window, 'getSelection');
1012 afterEach(function() {
1013 getSelection.restore();
1016 it('returns position when browser selection collapsed', function() {
1017 var c = canvas.fromXML('<section>Alice has a cat</section>'),
1018 dom = c.doc().dom(),
1019 text = findTextNode(dom, 'Alice has a cat');
1021 expect(text.nodeType).to.equal(Node.TEXT_NODE, 'correct node selected');
1022 expect($(text).text()).to.equal('Alice has a cat');
1024 getSelection.returns({
1031 var cursor = c.getCursor(),
1032 position = cursor.getPosition();
1034 expect(cursor.isSelecting()).to.equal(false, 'cursor is not selecting anything');
1035 expect(position.element.getText()).to.equal('Alice has a cat');
1036 expect(position.offset).to.equal(5);
1037 expect(position.offsetAtEnd).to.equal(false, 'offset is not at end');
1039 getSelection.returns({
1047 expect(cursor.getPosition().offsetAtEnd).to.equal(true, 'offset at end');
1050 it('returns boundries of selection when browser selection not collapsed', function() {
1051 var c = canvas.fromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
1052 dom = c.doc().dom(),
1054 alice: findTextNode(dom, 'Alice '),
1055 has: findTextNode(dom, 'has'),
1056 cat: findTextNode(dom, ' cat')
1058 cursor = c.getCursor(),
1059 aliceElement = c.getDocumentElement(text.alice),
1060 catElement = c.getDocumentElement(text.cat);
1064 {focus: text.alice, focusOffset: 1, anchor: text.cat, anchorOffset: 2, selectionAnchor: catElement},
1065 {focus: text.cat, focusOffset: 2, anchor: text.alice, anchorOffset: 1, selectionAnchor: aliceElement}
1066 ].forEach(function(s, idx) {
1067 getSelection.returns({isColapsed: false, anchorNode: s.anchor, anchorOffset: s.anchorOffset, focusNode: s.focus, focusOffset: s.focusOffset});
1069 var selectionStart = cursor.getSelectionStart(),
1070 selectionEnd = cursor.getSelectionEnd(),
1071 selectionAnchor = cursor.getSelectionAnchor();
1073 expect(cursor.isSelecting()).to.equal(true, 'cursor is selecting');
1074 expect(selectionStart.element.sameNode(aliceElement)).to.equal(true, '"Alice" is the start of the selection ' + idx);
1075 expect(selectionStart.offset).to.equal(1, '"Alice" offset ok' + idx);
1076 expect(selectionEnd.element.sameNode(catElement)).to.equal(true, '"Cat" is the start of the selection ' + idx);
1077 expect(selectionEnd.offset).to.equal(2, '"Cat" offset ok' + idx);
1078 expect(selectionAnchor.element.sameNode(s.selectionAnchor)).to.equal(true, 'anchor ok');
1079 expect(selectionAnchor.offset).to.equal(s.anchorOffset, 'anchor offset ok');
1083 it('recognizes when browser selection boundries lies in sibling DocumentTextElements', function() {
1084 var c = canvas.fromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
1085 dom = c.doc().dom(),
1087 alice: findTextNode(dom, 'Alice '),
1088 has: findTextNode(dom, 'has'),
1089 a: findTextNode(dom, ' a '),
1090 big: findTextNode(dom, 'big'),
1091 cat: findTextNode(dom, ' cat'),
1093 cursor = c.getCursor();
1095 expect($(text.alice).text()).to.equal('Alice ');
1096 expect($(text.has).text()).to.equal('has');
1097 expect($(text.a).text()).to.equal(' a ');
1098 expect($(text.big).text()).to.equal('big');
1099 expect($(text.cat).text()).to.equal(' cat');
1101 getSelection.returns({anchorNode: text.alice, focusNode: text.a});
1102 expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "a" are children');
1104 getSelection.returns({anchorNode: text.alice, focusNode: text.cat});
1105 expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "cat" are children');
1107 getSelection.returns({anchorNode: text.alice, focusNode: text.has});
1108 expect(cursor.isSelectingSiblings()).to.equal(false, '"Alice" and "has" are not children');
1110 getSelection.returns({anchorNode: text.has, focusNode: text.big});
1111 expect(cursor.isSelectingSiblings()).to.equal(false, '"has" and "big" are not children');
1116 describe('Serializing document to WLXML', function() {
1117 it('keeps document intact when no changes have been made', function() {
1118 var xmlIn = '<section>Alice<div>has</div>a <span class="uri" meta-uri="http://cat.com">cat</span>!</section>',
1119 c = canvas.fromXML(xmlIn),
1122 var parser = new DOMParser(),
1123 input = parser.parseFromString(xmlIn, "application/xml").childNodes[0],
1124 output = parser.parseFromString(xmlOut, "application/xml").childNodes[0];
1126 expect(input.isEqualNode(output)).to.be.true;
1129 it('keeps arbitrary node attributes intact', function() {
1130 var xmlIn = '<section a="1" xmlns:dcterms="http://purl.org/dc/terms/"></section>',
1131 $xmlOut = $(canvas.fromXML(xmlIn).toXML());
1133 expect($xmlOut.attr('a')).to.equal('1');
1134 expect($xmlOut.attr('xmlns:dcterms')).to.equal('http://purl.org/dc/terms/');
1137 it('doesn\' serialize meta attribute if its empty', function() {
1140 c = canvas.fromXML('<section class="uri" meta-uri="some.uri"></section>');
1141 c.doc().setWlxmlMetaAttr('uri', '');
1142 expect($(c.toXML()).attr('meta-uri')).to.equal(undefined, 'overriding attribute with zero length string');
1144 c = canvas.fromXML('<section class="uri"></section>');
1145 c.doc().setWlxmlMetaAttr('uri', '');
1146 expect($(c.toXML()).attr('meta-uri')).to.equal(undefined, 'setting attribute to zero length string');
1149 describe('output xml', function() {
1150 it('keeps entities intact', function() {
1151 var xmlIn = '<section>< ></section>',
1152 c = canvas.fromXML(xmlIn),
1154 expect(xmlOut).to.equal(xmlIn);
1156 it('keeps entities intact when they form html/xml', function() {
1157 var xmlIn = '<section><abc></section>',
1158 c = canvas.fromXML(xmlIn),
1160 expect(xmlOut).to.equal(xmlIn);
1164 describe('formatting output xml', function() {
1165 /*it('keeps white spaces at the edges of input xml', function() {
1166 var xmlIn = ' <section></section> ',
1167 c = canvas.fromXML(xmlIn),
1170 expect(xmlOut.substr(4)).to.equal(' <', 'start');
1171 expect(xmlOut.substr(-2)).to.equal('> ', 'end');
1173 it('keeps white space between XML nodes', function() {
1174 var xmlIn = '<section>\n\n\n<div></div>\n\n\n<div></div>\n\n\n</section>',
1175 c = canvas.fromXML(xmlIn),
1178 var partsIn = xmlIn.split('\n\n\n'),
1179 partsOut = xmlOut.split('\n\n\n');
1181 expect(partsIn).to.deep.equal(partsOut);
1184 it('keeps white space between XML nodes - inline case', function() {
1185 var xmlIn = '<section>\n\n\n<span></span>\n\n\n<span></span>\n\n\n</section>',
1186 c = canvas.fromXML(xmlIn),
1189 var partsIn = xmlIn.split('\n\n\n'),
1190 partsOut = xmlOut.split('\n\n\n');
1192 expect(partsIn).to.deep.equal(partsOut);
1195 it('keeps white space at the beginning of text', function() {
1196 var xmlIn = '<section> abc<div>some div</div> abc</section>',
1197 c = canvas.fromXML(xmlIn),
1200 expect(xmlOut).to.equal(xmlIn);
1203 it('nests new children block elements', function() {
1204 var c = canvas.fromXML('<section></section>');
1206 c.doc().append({tag: 'header'});
1208 var xmlOut = c.toXML();
1209 expect(xmlOut.split('\n ')[0]).to.equal('<section>', 'nesting start ok');
1210 expect(xmlOut.split('\n').slice(-1)[0]).to.equal('</section>', 'nesting end ok');
1214 it('doesn\'t nest new children inline elements', function() {
1215 var c = canvas.fromXML('<section></section>');
1217 c.doc().append({tag: 'span'});
1219 var xmlOut = c.toXML();
1220 expect(xmlOut).to.equal('<section><span></span></section>');
1223 it('keeps original white space at the end of text', function() {
1225 var xmlIn = '<header> Some text ended with white space \
1227 <span class="uri">Some text</span> some text\
1230 c = canvas.fromXML(xmlIn);
1232 var xmlOut = c.toXML();
1233 console.log(xmlOut);
1234 expect(xmlOut).to.equal(xmlIn);
1237 it('keeps white space around text node', function() {
1238 var xmlIn = '<section>\
1239 <header>header1</header>\
1240 Some text surrounded by white space\
1241 <header>header2</header>\
1243 c = canvas.fromXML(xmlIn);
1245 var xmlOut = c.toXML();
1246 expect(xmlOut).to.equal(xmlIn);
1249 it('keeps white space around text node - last node case', function() {
1250 var xmlIn = '<section>\
1251 <header>header</header>\
1253 Some text surrounded by white space\
1256 c = canvas.fromXML(xmlIn);
1258 var xmlOut = c.toXML();
1259 expect(xmlOut).to.equal(xmlIn);
1262 it('keeps white space after detaching text element', function() {
1263 var xmlIn = '<section><header>header</header>\n\
1268 expectedXmlOut = '<section><header>header</header>\n\
1273 c = canvas.fromXML(xmlIn),
1274 children = c.doc().children(),
1275 text = children[children.length-1];
1277 expect(text.getText()).to.equal('text1');
1281 var xmlOut = c.toXML();
1282 expect(xmlOut).to.equal(expectedXmlOut);