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 at the beginning', function() {
319 var c = canvas.fromXML('<section><div></div></section>'),
320 prepended = c.doc().prepend({tag: 'header', klass: 'some.class'}),
321 children = c.doc().children();
323 expect(children).to.have.length(2);
324 expect(children[0].sameNode(prepended)).to.be.true;
327 it('can put new TextElement at the beginning', function() {
328 var c = canvas.fromXML('<section><div></div></section>'),
329 prepended = c.doc().prepend({text: 'Alice'}),
330 children = c.doc().children();
332 expect(children).to.have.length(2)
333 expect(children[0].sameNode(prepended)).to.be.true;
334 expect(children[0].getText()).to.equal('Alice');
337 it('can put new NodeElement after another NodeElement', function() {
338 var c = canvas.fromXML('<section><div></div></section>'),
339 div = c.doc().children()[0],
340 added = div.after({tag: 'header', klass: 'some.class'}),
341 children = c.doc().children();
342 expect(children.length).to.equal(2);
343 expect(children[1].sameNode(added)).to.be.true;
346 it('can put new Nodeelement before another element', function() {
347 var c = canvas.fromXML('<section><div></div></section>'),
348 div = c.doc().children()[0],
349 added = div.before({tag: 'header', klass: 'some.class'}),
350 children = c.doc().children();
351 expect(children.length).to.equal(2);
352 expect(children[0].sameNode(added)).to.be.true;
355 it('can put new DocumentNodeElement after DocumentTextElement', function() {
356 var c = canvas.fromXML('<section>Alice</section>'),
357 text = c.doc().children()[0],
358 added = text.after({tag: 'p'}),
359 children = c.doc().children();
361 expect(children.length).to.equal(2);
362 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
363 expect(children[0].getText()).to.equal('Alice');
364 expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
365 expect(children[1].sameNode(added)).to.be.true;
367 it('can put new DocumentNodeElement before DocumentTextElement', function() {
368 var c = canvas.fromXML('<section>Alice</section>'),
369 text = c.doc().children()[0],
370 added = text.before({tag: 'p'}),
371 children = c.doc().children();
373 expect(children.length).to.equal(2);
374 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
375 expect(children[0].sameNode(added)).to.be.true;
376 expect(children[1]).to.be.instanceOf(documentElement.DocumentTextElement);
377 expect(children[1].getText()).to.equal('Alice');
380 it('can divide DocumentTextElement with a new DocumentNodeElement', function() {
381 var c = canvas.fromXML('<section>Alice has a cat</section>'),
383 text = section.children()[0];
385 var returned = text.divide({tag: 'aside', klass: 'footnote', offset: 5}),
386 sectionChildren = section.children(),
387 lhsText = sectionChildren[0],
388 rhsText = sectionChildren[2];
390 expect(lhsText.getText()).to.equal('Alice');
391 expect(returned.sameNode(sectionChildren[1]));
392 expect(rhsText.getText()).to.equal(' has a cat');
395 it('treats dividing DocumentTextElement at the very end as appending after it', function() {
396 var c = canvas.fromXML('<section>Alice has a cat</section>'),
398 text = section.children()[0];
400 var returned = text.divide({tag: 'aside', klass: 'footnote', offset: 15}),
401 sectionChildren = section.children(),
402 textElement = sectionChildren[0],
403 nodeElement = sectionChildren[1];
405 expect(sectionChildren.length).to.equal(2);
406 expect(textElement.getText()).to.equal('Alice has a cat');
407 expect(returned.sameNode(nodeElement)).to.be.true;
408 expect(nodeElement.getWlxmlTag()).to.equal('aside');
411 it('treats dividing DocumentTextElement at the very beginning as appending before it', function() {
412 var c = canvas.fromXML('<section>Alice has a cat</section>'),
414 text = section.children()[0];
416 var returned = text.divide({tag: 'aside', klass: 'footnote', offset: 0}),
417 sectionChildren = section.children(),
418 nodeElement = sectionChildren[0],
419 textElement = sectionChildren[1];
421 expect(sectionChildren.length).to.equal(2);
422 expect(textElement.getText()).to.equal('Alice has a cat');
423 expect(returned.sameNode(nodeElement)).to.be.true;
424 expect(nodeElement.getWlxmlTag()).to.equal('aside');
428 describe('Splitting text', function() {
430 it('splits DocumentTextElement\'s parent into two DocumentNodeElements of the same type', function() {
431 var c = canvas.fromXML('<section><header>Some header</header></section>'),
433 text = section.children()[0].children()[0];
435 var returnedValue = text.split({offset: 5});
436 expect(section.children().length).to.equal(2, 'section has two children');
438 var header1 = section.children()[0];
439 var header2 = section.children()[1];
441 expect(header1.getWlxmlTag()).to.equal('header', 'first section child represents wlxml header');
442 expect(header1.children().length).to.equal(1, 'first header has one text child');
443 expect(header1.children()[0].getText()).to.equal('Some ', 'first header has correct content');
444 expect(header2.getWlxmlTag()).to.equal('header', 'second section child represents wlxml header');
445 expect(header2.children().length).to.equal(1, 'second header has one text child');
446 expect(header2.children()[0].getText()).to.equal('header', 'second header has correct content');
448 expect(returnedValue.first.sameNode(header1)).to.equal(true, 'first node returnde');
449 expect(returnedValue.second.sameNode(header2)).to.equal(true, 'second node returned');
452 it('leaves empty copy of DocumentNodeElement if splitting at the very beginning', function() {
453 var c = canvas.fromXML('<section><header>Some header</header></section>'),
455 text = section.children()[0].children()[0];
457 text.split({offset: 0});
459 var header1 = section.children()[0];
460 var header2 = section.children()[1];
462 expect(header1.children().length).to.equal(0);
463 expect(header2.children()[0].getText()).to.equal('Some header');
466 it('leaves empty copy of DocumentNodeElement if splitting at the very end', function() {
467 var c = canvas.fromXML('<section><header>Some header</header></section>'),
469 text = section.children()[0].children()[0];
471 text.split({offset: 11});
473 var header1 = section.children()[0];
474 var header2 = section.children()[1];
476 expect(header1.children()[0].getText()).to.equal('Some header');
477 expect(header2.children().length).to.equal(0);
480 it('keeps DocumentTextElement\'s parent\'s children elements intact', function() {
481 var c = canvas.fromXML('\
484 A <span>fancy</span> and <span>nice</span> header\
488 header = section.children()[0],
489 textAnd = header.children()[2];
491 textAnd.split({offset: 2});
493 var sectionChildren = section.children();
494 expect(sectionChildren.length).to.equal(2, 'Section has two children');
495 expect(sectionChildren[0].getWlxmlTag()).to.equal('header', 'First section element is a wlxml header');
496 expect(sectionChildren[1].getWlxmlTag()).to.equal('header', 'Second section element is a wlxml header');
498 var firstHeaderChildren = sectionChildren[0].children();
499 expect(firstHeaderChildren.length).to.equal(3, 'First header has three children');
500 expect(firstHeaderChildren[0].getText()).to.equal('A ', 'First header starts with a text');
501 expect(firstHeaderChildren[1].getWlxmlTag()).to.equal('span', 'First header has span in the middle');
502 expect(firstHeaderChildren[2].getText()).to.equal(' a', 'First header ends with text');
504 var secondHeaderChildren = sectionChildren[1].children();
505 expect(secondHeaderChildren.length).to.equal(3, 'Second header has three children');
506 expect(secondHeaderChildren[0].getText()).to.equal('nd ', 'Second header starts with text');
507 expect(secondHeaderChildren[1].getWlxmlTag()).to.equal('span', 'Second header has span in the middle');
508 expect(secondHeaderChildren[2].getText()).to.equal(' header', 'Second header ends with text');
512 describe('wrapping', function() {
513 it('wraps DocumentNodeElement', function() {
514 var c = canvas.fromXML('<section><div></div></section>'),
515 div = c.doc().children()[0];
517 var returned = div.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
518 parent = div.parent(),
519 parent2 = c.doc().children()[0];
521 expect(returned.sameNode(parent)).to.be.true;
522 expect(returned.sameNode(parent2)).to.be.true;
523 expect(returned.getWlxmlTag()).to.equal('header');
524 expect(returned.getWlxmlClass()).to.equal('some.class');
526 it('wraps DocumentTextElement', function() {
527 var c = canvas.fromXML('<section>Alice</section>'),
528 text = c.doc().children()[0];
530 var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
531 parent = text.parent(),
532 parent2 = c.doc().children()[0];
534 expect(returned.sameNode(parent)).to.be.true;
535 expect(returned.sameNode(parent2)).to.be.true;
536 expect(returned.getWlxmlTag()).to.equal('header');
537 expect(returned.getWlxmlClass()).to.equal('some.class');
540 describe('wrapping part of DocumentTextElement', function() {
541 [{start: 5, end: 12}, {start: 12, end: 5}].forEach(function(offsets) {
542 it('wraps in the middle ' + offsets.start + '/' + offsets.end, function() {
543 var c = canvas.fromXML('<section>Alice has a cat</section>'),
544 text = c.doc().children()[0];
546 var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class', start: offsets.start, end: offsets.end}),
547 children = c.doc().children();
549 expect(children.length).to.equal(3);
551 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
552 expect(children[0].getText()).to.equal('Alice');
554 expect(children[1].sameNode(returned)).to.be.true;
555 expect(returned.getWlxmlTag()).to.equal('header');
556 expect(returned.getWlxmlClass()).to.equal('some.class');
557 expect(children[1].children().length).to.equal(1);
558 expect(children[1].children()[0].getText()).to.equal(' has a ');
560 expect(children[2]).to.be.instanceOf(documentElement.DocumentTextElement);
561 expect(children[2].getText()).to.equal('cat');
565 it('wraps whole text inside DocumentTextElement if offsets span entire content', function() {
566 var c = canvas.fromXML('<section>Alice has a cat</section>'),
567 text = c.doc().children()[0];
569 var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class', start: 0, end: 15}),
570 children = c.doc().children();
572 expect(children.length).to.equal(1);
573 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
574 expect(children[0].children()[0].getText()).to.equal('Alice has a cat');
578 it('wraps text spanning multiple sibling DocumentTextNodes', function() {
579 var c = canvas.fromXML('<section>Alice has a <span>small</span> cat</section>'),
581 wrapper = c.wrapText({
583 _with: {tag: 'span', klass: 'some.class'},
589 expect(section.children().length).to.equal(2);
590 expect(section.children()[0]).to.be.instanceOf(documentElement.DocumentTextElement);
591 expect(section.children()[0].getText()).to.equal('Alice ');
593 var wrapper2 = section.children()[1];
594 expect(wrapper2.sameNode(wrapper)).to.be.true;
596 var wrapperChildren = wrapper.children();
597 expect(wrapperChildren.length).to.equal(3);
598 expect(wrapperChildren[0].getText()).to.equal('has a ');
600 expect(wrapperChildren[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
601 expect(wrapperChildren[1].children().length).to.equal(1);
602 expect(wrapperChildren[1].children()[0].getText()).to.equal('small');
604 expect(wrapperChildren[2].getText()).to.equal(' cat');
608 describe('unwrapping', function() {
609 it('unwraps DocumentTextElement from its parent DocumentNodeElement if it\'s its only child', function() {
610 var c = canvas.fromXML('<section>Alice <span>has a</span> cat</section>'),
612 text = section.children()[1].children()[0];
614 var newTextContainer = text.unwrap();
616 expect(section.children().length).to.equal(1, 'section has one child');
617 expect(section.children()[0].getText()).to.equal('Alice has a cat');
618 expect(newTextContainer.sameNode(c.doc())).to.equal(true, 'unwrap returns new text parent DocumentNodeElement');
623 describe('Lists api', function() {
624 describe('creating lists', function() {
625 it('allows creation of a list from existing sibling DocumentElements', function() {
626 var c = canvas.fromXML('\
634 textHas = section.children()[1],
635 divA = section.children()[2]
637 c.list.create({element1: textHas, element2: divA});
639 expect(section.children().length).to.equal(3, 'section has three child elements');
641 var child1 = section.children()[0],
642 list = section.children()[1],
643 child3 = section.children()[2];
645 expect(child1.getText()).to.equal('Alice');
646 expect(list.is('list')).to.equal(true, 'second child is a list');
647 expect(list.children().length).to.equal(2, 'list contains two elements');
648 list.children().forEach(function(child) {
649 expect(child.getWlxmlClass()).to.equal('item', 'list childs have wlxml class of item');
651 expect(child3.children()[0].getText()).to.equal('cat');
654 it('allows creating nested list from existing sibling list items', function() {
655 var c = canvas.fromXML('\
657 <div class="list-items">\
658 <div class="item">A</div>\
659 <div class="item">B</div>\
660 <div class="item">C</div>\
661 <div class="item">D</div>\
664 outerList = c.doc().children()[0],
665 itemB = outerList.children()[1],
666 itemC = outerList.children()[2];
669 c.list.create({element1: itemB, element2: itemC});
671 var outerListItems = outerList.children(),
672 innerList = outerListItems[1].children()[0],
673 innerListItems = innerList.children();
675 expect(outerListItems.length).to.equal(3, 'outer list has three items');
676 expect(outerListItems[0].children()[0].getText()).to.equal('A', 'first outer item ok');
677 expect(outerListItems[1].getWlxmlClass()).to.equal('item', 'inner list is wrapped by item element');
679 expect(innerList.is('list')).to.equal(true, 'inner list created');
680 expect(innerListItems.length).to.equal(2, 'inner list has two items');
681 expect(innerListItems[0].children()[0].getText()).to.equal('B', 'first inner item ok');
682 expect(innerListItems[1].children()[0].getText()).to.equal('C', 'second inner item ok');
684 expect(outerListItems[2].children()[0].getText()).to.equal('D', 'last outer item ok');
690 describe('extracting list items', function() {
691 it('creates two lists with extracted items in the middle if extracting from the middle of the list', function() {
692 var c = canvas.fromXML('\
694 <div class="list.items">\
695 <div class="item">0</div>\
696 <div class="item">1</div>\
697 <div class="item">2</div>\
698 <div class="item">3</div>\
701 list = c.doc().children()[0],
702 item1 = list.children()[1],
703 item2 = list.children()[2];
705 c.list.extractItems({element1: item1, element2: item2});
707 var section = c.doc(),
708 list1 = section.children()[0],
709 oldItem1 = section.children()[1],
710 oldItem2 = section.children()[2],
711 list2 = section.children()[3];
713 expect(section.children().length).to.equal(4, 'section contains four children');
715 expect(list1.is('list')).to.equal(true, 'first section child is a list');
716 expect(list1.children().length).to.equal(1, 'first list has one child');
717 expect(list1.children()[0].children()[0].getText()).to.equal('0', 'first item of the first list is a first item of the original list');
719 expect(oldItem1.children()[0].getText()).to.equal('1', 'first item got extracted');
720 expect(oldItem1.getWlxmlClass() === undefined).to.equal(true, 'first extracted element has no wlxml class');
722 expect(oldItem2.children()[0].getText()).to.equal('2', 'second item got extracted');
723 expect(oldItem2.getWlxmlClass() === undefined).to.equal(true, 'second extracted element has no wlxml class');
725 expect(list2.is('list')).to.equal(true, 'last section child is a list');
726 expect(list2.children().length).to.equal(1, 'second list has one child');
727 expect(list2.children()[0].children()[0].getText()).to.equal('3', 'first item of the second list is a last item of the original list');
730 it('puts extracted items above the list if starting item is the first one', function() {
731 var c = canvas.fromXML('\
733 <div class="list.items">\
734 <div class="item">0</div>\
735 <div class="item">1</div>\
736 <div class="item">2</div>\
739 list = c.doc().children()[0],
740 item1 = list.children()[0],
741 item2 = list.children()[1],
742 item3 = list.children()[2];
744 c.list.extractItems({element1: item1, element2: item2});
746 var section = c.doc(),
747 oldItem1 = section.children()[0],
748 oldItem2 = section.children()[1],
749 newList = section.children()[2];
751 expect(section.children().length).to.equal(3, 'section has three children');
752 expect(oldItem1.children()[0].getText()).to.equal('0', 'first item extracted');
753 expect(oldItem2.children()[0].getText()).to.equal('1', 'second item extracted');
754 expect(newList.is('list')).to.equal(true, 'list lies below extracted item');
755 expect(newList.children().length).to.equal(1, 'list has now one child');
758 it('puts extracted items below the list if ending item is the last one', function() {
759 var c = canvas.fromXML('\
761 <div class="list.items">\
762 <div class="item">0</div>\
763 <div class="item">1</div>\
764 <div class="item">2</div>\
767 list = c.doc().children()[0],
768 item1 = list.children()[0],
769 item2 = list.children()[1],
770 item3 = list.children()[2];
772 c.list.extractItems({element1: item2, element2: item3});
774 var section = c.doc(),
775 oldItem1 = section.children()[1],
776 oldItem2 = section.children()[2],
777 newList = section.children()[0];
779 expect(section.children().length).to.equal(3, 'section has three children');
780 expect(oldItem1.children()[0].getText()).to.equal('1', 'first item extracted');
781 expect(oldItem2.children()[0].getText()).to.equal('2', 'second item extracted');
782 expect(newList.is('list')).to.equal(true, 'list lies above extracted item');
783 expect(newList.children().length).to.equal(1, 'list has now one child');
786 it('removes list if all its items are extracted', function() {
787 var c = canvas.fromXML('\
789 <div class="list.items">\
790 <div class="item">some item</div>\
791 <div class="item">some item 2</div>\
794 list = c.doc().children()[0],
795 item1 = list.children()[0],
796 item2 = list.children()[1];
798 c.list.extractItems({element1: item1, element2: item2});
800 var section = c.doc(),
801 list1 = section.children()[0],
802 oldItem1 = section.children()[0],
803 oldItem2 = section.children()[1];
805 expect(section.children().length).to.equal(2, 'section contains two children');
806 expect(oldItem1.children()[0].getText()).to.equal('some item');
807 expect(oldItem2.children()[0].getText()).to.equal('some item 2');
810 it('creates two lists with extracted items in the middle if extracting from the middle of the list - nested case' , function() {
811 var c = canvas.fromXML('\
813 <div class="list.items">\
814 <div class="item">0</div>\
816 <div class="list.items">\
817 <div class="item">1.1</div>\
818 <div class="item">1.2</div>\
819 <div class="item">1.3</div>\
822 <div class="item">2</div>\
825 list = c.doc().children()[0],
826 nestedList = list.children()[1].children()[0],
827 nestedListItem = nestedList.children()[1];
829 c.list.extractItems({element1: nestedListItem, element2: nestedListItem});
831 var section = c.doc(),
832 list = section.children()[0],
833 item1 = list.children()[0],
834 item2 = list.children()[1], //
835 item3 = list.children()[2],
836 item4 = list.children()[3], //
837 item5 = list.children()[4],
838 nestedList1 = item2.children()[0],
839 nestedList2 = item4.children()[0];
841 expect(list.children().length).to.equal(5, 'top list has five items');
843 expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
845 expect(item2.getWlxmlClass()).to.equal('item', 'first nested list is still wrapped in item element');
846 expect(nestedList1.children().length).to.equal(1, 'first nested list is left with one child');
847 expect(nestedList1.children()[0].children()[0].getText()).to.equal('1.1', 'first nested list item left alone');
849 expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
851 expect(item4.getWlxmlClass()).to.equal('item', 'second nested list is still wrapped in item element');
852 expect(nestedList2.children().length).to.equal(1, 'second nested list is left with one child');
853 expect(nestedList2.children()[0].children()[0].getText()).to.equal('1.3', 'second nested list item left alone');
855 expect(item5.children()[0].getText()).to.equal('2', 'last item ok');
858 it('puts extracted items below the list if ending item is the last one - nested case' , function() {
859 var c = canvas.fromXML('\
861 <div class="list.items">\
862 <div class="item">0</div>\
864 <div class="list.items">\
865 <div class="item">1.1</div>\
866 <div class="item">1.2</div>\
867 <div class="item">1.3</div>\
870 <div class="item">2</div>\
873 list = c.doc().children()[0],
874 nestedList = list.children()[1].children()[0],
875 nestedListItem1 = nestedList.children()[1],
876 nestedListItem2 = nestedList.children()[2];
878 c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
880 var section = c.doc(),
881 list = section.children()[0],
882 item1 = list.children()[0],
883 item2 = list.children()[1],
884 item3 = list.children()[2],
885 item4 = list.children()[3],
886 item5 = list.children()[4];
887 nestedList = item2.children()[0];
889 expect(list.children().length).to.equal(5, 'top list has five items');
890 expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
891 expect(item2.getWlxmlClass()).to.equal('item', 'nested list is still wrapped in item element');
892 expect(nestedList.children().length).to.equal(1, 'nested list is left with one child');
893 expect(nestedList.children()[0].children()[0].getText()).to.equal('1.1', 'nested list item left alone');
894 expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
895 expect(item4.children()[0].getText()).to.equal('1.3', 'fourth item ok');
896 expect(item5.children()[0].getText()).to.equal('2', 'fifth item ok');
899 it('puts extracted items above the list if starting item is the first one - nested case' , function() {
900 var c = canvas.fromXML('\
902 <div class="list.items">\
903 <div class="item">0</div>\
905 <div class="list.items">\
906 <div class="item">1.1</div>\
907 <div class="item">1.2</div>\
908 <div class="item">1.3</div>\
911 <div class="item">2</div>\
914 list = c.doc().children()[0],
915 nestedList = list.children()[1].children()[0],
916 nestedListItem1 = nestedList.children()[0],
917 nestedListItem2 = nestedList.children()[1];
919 c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
921 var section = c.doc(),
922 list = section.children()[0],
923 item1 = list.children()[0],
924 item2 = list.children()[1],
925 item3 = list.children()[2],
926 item4 = list.children()[3],
927 item5 = list.children()[4];
928 nestedList = item4.children()[0];
930 expect(list.children().length).to.equal(5, 'top list has five items');
931 expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
932 expect(item2.children()[0].getText()).to.equal('1.1', 'second item ok');
933 expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
935 expect(item4.getWlxmlClass()).to.equal('item', 'nested list is still wrapped in item element');
936 expect(nestedList.children().length).to.equal(1, 'nested list is left with one child');
937 expect(nestedList.children()[0].children()[0].getText()).to.equal('1.3', 'nested list item left alone');
938 expect(item5.children()[0].getText()).to.equal('2', 'fifth item ok');
941 it('removes list if all its items are extracted - nested case', function() {
942 var c = canvas.fromXML('\
944 <div class="list.items">\
945 <div class="item">0</div>\
947 <div class="list.items">\
948 <div class="item">1.1</div>\
949 <div class="item">1.2</div>\
952 <div class="item">2</div>\
955 list = c.doc().children()[0],
956 nestedList = list.children()[1].children()[0],
957 nestedListItem1 = nestedList.children()[0],
958 nestedListItem2 = nestedList.children()[1];
960 c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
962 var section = c.doc(),
963 list = section.children()[0],
964 item1 = list.children()[0],
965 item2 = list.children()[1],
966 item3 = list.children()[2],
967 item4 = list.children()[3];
969 expect(list.children().length).to.equal(4, 'top list has four items');
970 expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
971 expect(item2.children()[0].getText()).to.equal('1.1', 'second item ok');
972 expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
973 expect(item4.children()[0].getText()).to.equal('2', 'fourth item ok');
976 it('extracts items out of outer most list when merge flag is set to false', function() {
977 var c = canvas.fromXML('\
979 <div class="list.items">\
980 <div class="item">0</div>\
982 <div class="list.items">\
983 <div class="item">1.1</div>\
984 <div class="item">1.2</div>\
987 <div class="item">2</div>\
991 list = section.children()[0],
992 nestedList = list.children()[1].children()[0],
993 nestedListItem = nestedList.children()[0];
995 var test = c.list.extractItems({element1: nestedListItem, element2: nestedListItem, merge: false});
997 expect(test).to.equal(true, 'extraction status ok');
999 var sectionChildren = section.children(),
1000 extractedItem = sectionChildren[1];
1002 expect(sectionChildren.length).to.equal(3, 'section has three children');
1003 expect(sectionChildren[0].is('list')).to.equal(true, 'first child is a list');
1005 expect(extractedItem.getWlxmlTag()).to.equal('div', 'extracted item is a wlxml div');
1006 expect(extractedItem.getWlxmlClass()).to.equal(undefined, 'extracted item has no wlxml class');
1007 expect(extractedItem.children()[0].getText()).to.equal('1.1', 'extracted item ok');
1008 expect(sectionChildren[2].is('list')).to.equal(true, 'second child is a list');
1015 describe('Cursor', function() {
1019 var findTextNode = function(inside, text) {
1020 var nodes = inside.find(':not(iframe)').addBack().contents().filter(function() {
1021 return this.nodeType === Node.TEXT_NODE && this.data === text;
1028 beforeEach(function() {
1029 getSelection = sinon.stub(window, 'getSelection');
1032 afterEach(function() {
1033 getSelection.restore();
1036 it('returns position when browser selection collapsed', function() {
1037 var c = canvas.fromXML('<section>Alice has a cat</section>'),
1038 dom = c.doc().dom(),
1039 text = findTextNode(dom, 'Alice has a cat');
1041 expect(text.nodeType).to.equal(Node.TEXT_NODE, 'correct node selected');
1042 expect($(text).text()).to.equal('Alice has a cat');
1044 getSelection.returns({
1051 var cursor = c.getCursor(),
1052 position = cursor.getPosition();
1054 expect(cursor.isSelecting()).to.equal(false, 'cursor is not selecting anything');
1055 expect(position.element.getText()).to.equal('Alice has a cat');
1056 expect(position.offset).to.equal(5);
1057 expect(position.offsetAtEnd).to.equal(false, 'offset is not at end');
1059 getSelection.returns({
1067 expect(cursor.getPosition().offsetAtEnd).to.equal(true, 'offset at end');
1070 it('returns boundries of selection when browser selection not collapsed', function() {
1071 var c = canvas.fromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
1072 dom = c.doc().dom(),
1074 alice: findTextNode(dom, 'Alice '),
1075 has: findTextNode(dom, 'has'),
1076 cat: findTextNode(dom, ' cat')
1078 cursor = c.getCursor(),
1079 aliceElement = c.getDocumentElement(text.alice),
1080 catElement = c.getDocumentElement(text.cat);
1084 {focus: text.alice, focusOffset: 1, anchor: text.cat, anchorOffset: 2, selectionAnchor: catElement},
1085 {focus: text.cat, focusOffset: 2, anchor: text.alice, anchorOffset: 1, selectionAnchor: aliceElement}
1086 ].forEach(function(s, idx) {
1087 getSelection.returns({isColapsed: false, anchorNode: s.anchor, anchorOffset: s.anchorOffset, focusNode: s.focus, focusOffset: s.focusOffset});
1089 var selectionStart = cursor.getSelectionStart(),
1090 selectionEnd = cursor.getSelectionEnd(),
1091 selectionAnchor = cursor.getSelectionAnchor();
1093 expect(cursor.isSelecting()).to.equal(true, 'cursor is selecting');
1094 expect(selectionStart.element.sameNode(aliceElement)).to.equal(true, '"Alice" is the start of the selection ' + idx);
1095 expect(selectionStart.offset).to.equal(1, '"Alice" offset ok' + idx);
1096 expect(selectionEnd.element.sameNode(catElement)).to.equal(true, '"Cat" is the start of the selection ' + idx);
1097 expect(selectionEnd.offset).to.equal(2, '"Cat" offset ok' + idx);
1098 expect(selectionAnchor.element.sameNode(s.selectionAnchor)).to.equal(true, 'anchor ok');
1099 expect(selectionAnchor.offset).to.equal(s.anchorOffset, 'anchor offset ok');
1103 it('recognizes when browser selection boundries lies in sibling DocumentTextElements', function() {
1104 var c = canvas.fromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
1105 dom = c.doc().dom(),
1107 alice: findTextNode(dom, 'Alice '),
1108 has: findTextNode(dom, 'has'),
1109 a: findTextNode(dom, ' a '),
1110 big: findTextNode(dom, 'big'),
1111 cat: findTextNode(dom, ' cat'),
1113 cursor = c.getCursor();
1115 expect($(text.alice).text()).to.equal('Alice ');
1116 expect($(text.has).text()).to.equal('has');
1117 expect($(text.a).text()).to.equal(' a ');
1118 expect($(text.big).text()).to.equal('big');
1119 expect($(text.cat).text()).to.equal(' cat');
1121 getSelection.returns({anchorNode: text.alice, focusNode: text.a});
1122 expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "a" are children');
1124 getSelection.returns({anchorNode: text.alice, focusNode: text.cat});
1125 expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "cat" are children');
1127 getSelection.returns({anchorNode: text.alice, focusNode: text.has});
1128 expect(cursor.isSelectingSiblings()).to.equal(false, '"Alice" and "has" are not children');
1130 getSelection.returns({anchorNode: text.has, focusNode: text.big});
1131 expect(cursor.isSelectingSiblings()).to.equal(false, '"has" and "big" are not children');
1135 describe('zero width space handling', function() {
1136 it('position range includes ZWS at the boundries of text in case when native selection api doesn\'t', function() {
1137 var c = canvas.fromXML("<section>Alice</section>"),
1138 dom = c.doc().dom(),
1139 textNode = findTextNode(dom, 'Alice'),
1140 cursor = c.getCursor();
1142 textNode.data = utils.unicode.ZWS + 'Alice';
1143 getSelection.returns({anchorNode: textNode, anchorOffset: 1, focusNode: textNode, focusOffset: 1});
1144 expect(cursor.getPosition().offset).to.equal(0);
1145 expect(cursor.getPosition().offsetAtBeginning).to.equal(true, 'offset at beginning');
1147 textNode.data = 'Alice' + utils.unicode.ZWS;
1148 getSelection.returns({anchorNode: textNode, anchorOffset: 5, focusNode: textNode, focusOffset: 5});
1149 expect(cursor.getPosition().offset).to.equal(6);
1150 expect(cursor.getPosition().offsetAtEnd).to.equal(true, 'offset at end');
1155 describe('Serializing document to WLXML', function() {
1156 it('keeps document intact when no changes have been made', function() {
1157 var xmlIn = '<section>Alice<div>has</div>a <span class="uri" meta-uri="http://cat.com">cat</span>!</section>',
1158 c = canvas.fromXML(xmlIn),
1161 var parser = new DOMParser(),
1162 input = parser.parseFromString(xmlIn, "application/xml").childNodes[0],
1163 output = parser.parseFromString(xmlOut, "application/xml").childNodes[0];
1165 expect(input.isEqualNode(output)).to.be.true;
1168 it('keeps arbitrary node attributes intact', function() {
1169 var xmlIn = '<section a="1" xmlns:dcterms="http://purl.org/dc/terms/"></section>',
1170 $xmlOut = $(canvas.fromXML(xmlIn).toXML());
1172 expect($xmlOut.attr('a')).to.equal('1');
1173 expect($xmlOut.attr('xmlns:dcterms')).to.equal('http://purl.org/dc/terms/');
1176 it('doesn\' serialize meta attribute if its empty', function() {
1179 c = canvas.fromXML('<section class="uri" meta-uri="some.uri"></section>');
1180 c.doc().setWlxmlMetaAttr('uri', '');
1181 expect($(c.toXML()).attr('meta-uri')).to.equal(undefined, 'overriding attribute with zero length string');
1183 c = canvas.fromXML('<section class="uri"></section>');
1184 c.doc().setWlxmlMetaAttr('uri', '');
1185 expect($(c.toXML()).attr('meta-uri')).to.equal(undefined, 'setting attribute to zero length string');
1188 describe('output xml', function() {
1189 it('keeps entities intact', function() {
1190 var xmlIn = '<section>< ></section>',
1191 c = canvas.fromXML(xmlIn),
1193 expect(xmlOut).to.equal(xmlIn);
1195 it('keeps entities intact when they form html/xml', function() {
1196 var xmlIn = '<section><abc></section>',
1197 c = canvas.fromXML(xmlIn),
1199 expect(xmlOut).to.equal(xmlIn);
1203 describe('formatting output xml', function() {
1204 /*it('keeps white spaces at the edges of input xml', function() {
1205 var xmlIn = ' <section></section> ',
1206 c = canvas.fromXML(xmlIn),
1209 expect(xmlOut.substr(4)).to.equal(' <', 'start');
1210 expect(xmlOut.substr(-2)).to.equal('> ', 'end');
1212 it('keeps white space between XML nodes', function() {
1213 var xmlIn = '<section>\n\n\n<div></div>\n\n\n<div></div>\n\n\n</section>',
1214 c = canvas.fromXML(xmlIn),
1217 var partsIn = xmlIn.split('\n\n\n'),
1218 partsOut = xmlOut.split('\n\n\n');
1220 expect(partsIn).to.deep.equal(partsOut);
1223 it('keeps white space between XML nodes - inline case', function() {
1224 var xmlIn = '<section>\n\n\n<span></span>\n\n\n<span></span>\n\n\n</section>',
1225 c = canvas.fromXML(xmlIn),
1228 var partsIn = xmlIn.split('\n\n\n'),
1229 partsOut = xmlOut.split('\n\n\n');
1231 expect(partsIn).to.deep.equal(partsOut);
1234 it('keeps white space at the beginning of text', function() {
1235 var xmlIn = '<section> abc<div>some div</div> abc</section>',
1236 c = canvas.fromXML(xmlIn),
1239 expect(xmlOut).to.equal(xmlIn);
1242 it('nests new children block elements', function() {
1243 var c = canvas.fromXML('<section></section>');
1245 c.doc().append({tag: 'header'});
1247 var xmlOut = c.toXML();
1248 expect(xmlOut.split('\n ')[0]).to.equal('<section>', 'nesting start ok');
1249 expect(xmlOut.split('\n').slice(-1)[0]).to.equal('</section>', 'nesting end ok');
1253 it('doesn\'t nest new children inline elements', function() {
1254 var c = canvas.fromXML('<section></section>');
1256 c.doc().append({tag: 'span'});
1258 var xmlOut = c.toXML();
1259 expect(xmlOut).to.equal('<section><span></span></section>');
1262 it('keeps original white space at the end of text', function() {
1264 var xmlIn = '<header> Some text ended with white space \
1266 <span class="uri">Some text</span> some text\
1269 c = canvas.fromXML(xmlIn);
1271 var xmlOut = c.toXML();
1272 console.log(xmlOut);
1273 expect(xmlOut).to.equal(xmlIn);
1276 it('keeps white space around text node', function() {
1277 var xmlIn = '<section>\
1278 <header>header1</header>\
1279 Some text surrounded by white space\
1280 <header>header2</header>\
1282 c = canvas.fromXML(xmlIn);
1284 var xmlOut = c.toXML();
1285 expect(xmlOut).to.equal(xmlIn);
1288 it('keeps white space around text node - last node case', function() {
1289 var xmlIn = '<section>\
1290 <header>header</header>\
1292 Some text surrounded by white space\
1295 c = canvas.fromXML(xmlIn);
1297 var xmlOut = c.toXML();
1298 expect(xmlOut).to.equal(xmlIn);
1301 it('keeps white space after detaching text element', function() {
1302 var xmlIn = '<section><header>header</header>\n\
1307 expectedXmlOut = '<section><header>header</header>\n\
1312 c = canvas.fromXML(xmlIn),
1313 children = c.doc().children(),
1314 text = children[children.length-1];
1316 expect(text.getText()).to.equal('text1');
1320 var xmlOut = c.toXML();
1321 expect(xmlOut).to.equal(expectedXmlOut);