4 'modules/documentCanvas/canvas/canvas',
5 'modules/documentCanvas/canvas/documentElement'
6 ], function(chai, sinon, canvas, documentElement) {
10 var expect = chai.expect;
13 describe('Canvas', function() {
15 describe('Internal HTML representation of a sample document', function() {
16 it('works', function() {
17 var c = canvas.fromXML('\
19 This is some text without its own wrapping tag.\
20 <div class="p.subclass">\
24 This is text in a div <span>with some inline text</span>.\
26 This is some text without its own wrapping tag.\
29 var expected = '<div wlxml-tag="section">'
30 + '<div wlxml-text>This is some text without its own wrapping tag.</div>'
31 + '<div wlxml-tag="div" wlxml-class="p-subclass">'
32 + '<div wlxml-text>This is a paragraph.</div>'
34 + '<div wlxml-tag="div">'
35 + '<div wlxml-text>This is text in a div </div>'
36 + '<div wlxml-tag="span">'
37 + '<div wlxml-text>with some inline text</div>'
39 + '<div wlxml-text>.</div>'
41 + '<div wlxml-text>This is some text without its own wrapping tag.</div>'
43 expect(c.doc().dom()[0].isEqualNode($(expected)[0])).to.be.true;
47 describe('Internal HTML representation of a DocumentNodeElement', function() {
48 it('is always a div tag', function() {
49 ['section', 'header', 'span', 'aside', 'figure'].forEach(function(tagName) {
50 var dom = canvas.fromXML('<' + tagName + '></' + tagName + '>').doc().dom();
51 expect(dom.prop('tagName')).to.equal('DIV', tagName + ' is represented as div');
54 it('has wlxml tag put into wlxml-tag attribute', function() {
55 var dom = canvas.fromXML('<section></section>').doc().dom();
56 expect(dom.attr('wlxml-tag')).to.equal('section');
58 it('has wlxml class put into wlxml-class, dots replaced with dashes', function() {
59 var dom = canvas.fromXML('<section class="some.class"></section>').doc().dom();
60 expect(dom.attr('wlxml-class')).to.equal('some-class');
64 describe('Internal HTML representation of a DocumentTextElement', function() {
65 it('is text node wrapped in a div with wlxml-text attribute set', function() {
66 var dom = canvas.fromXML('<section>Alice</section>').doc().children()[0].dom();
67 expect(dom.prop('tagName')).to.equal('DIV');
68 expect(dom.attr('wlxml-text')).to.equal('');
69 expect(dom.contents().length).to.equal(1);
70 expect(dom.contents()[0].nodeType).to.equal(Node.TEXT_NODE);
71 expect($(dom.contents()[0]).text()).to.equal('Alice');
75 describe('basic properties', function() {
76 it('renders empty document when canvas created from empty XML', function() {
77 var c = canvas.fromXML('');
78 expect(c.doc()).to.equal(null);
81 it('gives access to its document root node', function() {
82 var c = canvas.fromXML('<section></section>');
83 expect(c.doc().getWlxmlTag()).to.equal('section');
86 describe('root element', function() {
87 it('has no parent', function() {
88 var c = canvas.fromXML('<section></section>');
89 expect(c.doc().parent()).to.be.null;
93 describe('DocumentTextElement', function() {
94 it('can have its content set', function() {
95 var c = canvas.fromXML('<section>Alice</section>'),
97 text = root.children()[0];
99 text.setText('a cat');
100 expect(root.children()[0].getText()).to.equal('a cat');
104 describe('DocumentNodeElement', function() {
105 it('knows index of its child', function() {
106 var c = canvas.fromXML('<section><div></div><header></header><span></span></section>'),
108 child = root.children()[1];
109 expect(root.childIndex(child)).to.equal(1);
112 it('knows WLXML tag it renders', function(){
113 var c = canvas.fromXML('<section></section>'),
115 expect(section.getWlxmlTag()).to.equal('section', 'initial tag is section');
116 section.setWlxmlTag('header');
117 expect(section.getWlxmlTag()).to.equal('header', 'tag is changed to header');
120 it('knows WLXML class of a WLXML tag it renders', function(){
121 var c = canvas.fromXML('<section class="some.class.A"></section>'),
123 expect(section.getWlxmlClass()).to.equal('some.class.A');
124 section.setWlxmlClass('some.class.B');
125 expect(section.getWlxmlClass()).to.equal('some.class.B');
126 section.setWlxmlClass(null);
127 expect(section.getWlxmlClass()).to.be.undefined;
132 describe('element has meta attributes', function() {
133 it('can change its meta attributes', function() {
134 var c = canvas.fromXML('<section><span class="uri" meta-uri="someuri"></span></section>'),
135 span = c.doc().children()[0];
137 expect(span.getWlxmlMetaAttr('uri')).to.equal('someuri');
138 span.setWlxmlMetaAttr('uri', 'otheruri');
139 expect(span.getWlxmlMetaAttr('uri')).to.equal('otheruri');
142 it('changes its meta attributes with class change', function() {
143 var c = canvas.fromXML('<section><span class="uri" meta-uri="someuri"></span></section>'),
144 span = c.doc().children()[0];
146 expect(span.getWlxmlMetaAttr('uri')).to.equal('someuri');
147 span.setWlxmlClass('author');
148 expect(span.getWlxmlMetaAttr('uri')).to.be.undefined;
151 it('keeps meta attribute value on class change if a new class has this attribute', function() {
152 var c = canvas.fromXML('<section><span class="uri" meta-uri="someuri"></span></section>'),
153 span = c.doc().children()[0];
154 span.setWlxmlClass('uri.some.subclass');
155 expect(span.getWlxmlMetaAttr('uri')).to.equal('someuri');
160 it('returns DocumentNodeElement instance from HTMLElement', function() {
161 var c = canvas.fromXML('<section></section>'),
162 htmlElement = c.doc().dom().get(0),
163 element = c.getDocumentElement(htmlElement);
164 expect(element).to.be.instanceOf(documentElement.DocumentNodeElement);
165 expect(element.sameNode(c.doc()));
168 it('returns DocumentTextElement instance from Text Node', function() {
169 var c = canvas.fromXML('<section>Alice</section>'),
170 aliceElement = c.doc().children()[0],
171 textNode = aliceElement.dom().contents()[0],
172 element = c.getDocumentElement(textNode);
174 expect(textNode.nodeType).to.equal(Node.TEXT_NODE, 'text node selected');
175 expect($(textNode).text()).to.equal('Alice');
177 expect(element).to.be.instanceOf(documentElement.DocumentTextElement);
178 expect(element.sameNode(c.doc().children()[0]));
184 describe('document representation api', function() {
185 describe('document root element', function() {
186 var c = canvas.fromXML('<section></section>');
187 it('exists', function() {
188 expect(c.doc()).to.be.instanceOf(documentElement.DocumentElement);
190 it('is of type DocumentNodeElement', function() {
191 expect(c.doc()).to.be.instanceOf(documentElement.DocumentNodeElement);
195 describe('DocumentElements comparison', function() {
196 it('reports dwo DocumentElements to be the same when they represent the same wlxml document element', function() {
197 var c = canvas.fromXML('<section><div></div><div></div></section>'),
198 first_div1 = c.doc().children()[0],
199 first_div2 = c.doc().children()[0],
200 second_div = c.doc().children()[1];
201 expect(first_div1.sameNode(first_div1)).to.be.true;
202 expect(first_div1.sameNode(first_div2)).to.be.true;
203 expect(first_div1.sameNode(second_div)).to.be.false;
207 describe('traversing', function() {
208 it('reports element nodes', function() {
209 var c = canvas.fromXML('<section><div></div></section>'),
210 children = c.doc().children();
211 expect(children.length).to.equal(1);
212 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
214 c = canvas.fromXML('<section><div></div><div></div></section>'),
215 children = c.doc().children();
216 expect(children.length).to.equal(2);
217 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
218 expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
221 it('reports text nodes', function() {
222 var c = canvas.fromXML('<section>Alice</section>'),
223 children = c.doc().children();
224 expect(children.length).to.equal(1);
225 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
228 describe('accessing parents', function() {
229 it('returns DocumentNodeElement representing parent in wlxml document as DocumentNodeElement parent', function() {
230 var c = canvas.fromXML('<section><div></div></section>'),
231 div = c.doc().children()[0];
232 expect(div.parent().sameNode(c.doc())).to.be.true;
234 it('returns DocumentNodeElement representing parent in wlxml document as DocumentTextElement parent', function() {
235 var c = canvas.fromXML('<section>Alice</section>'),
236 text = c.doc().children()[0];
237 expect(text.parent().sameNode(c.doc())).to.be.true;
241 describe('free text handling', function() {
242 it('sees free text', function() {
243 var c = canvas.fromXML('<section>Alice <span>has</span> a cat</section>'),
244 children = c.doc().children();
245 expect(children.length).to.equal(3);
246 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
247 expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
248 expect(children[2]).to.be.instanceOf(documentElement.DocumentTextElement);
252 describe('white characters handling', function() {
253 it('says empty element node has no children', function() {
254 var c = canvas.fromXML('<section></section>');
255 expect(c.doc().children().length).to.equal(0);
257 it('says element node with one space has one DocumentTextElement', function() {
258 var c = canvas.fromXML('<section> </section>');
259 expect(c.doc().children().length).to.equal(1);
260 expect(c.doc().children()[0]).to.be.instanceOf(documentElement.DocumentTextElement);
261 expect(c.doc().children()[0].getText()).to.equal(' ');
263 it('ignores white space surrounding block elements', function() {
264 var c = canvas.fromXML('<section> <div></div> </section>');
265 var children = c.doc().children();
266 expect(children.length).to.equal(1);
267 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
269 it('ignores white space between block elements', function() {
270 var c = canvas.fromXML('<section><div></div> <div></div></section>');
271 var children = c.doc().children();
272 expect(children.length === 2);
273 [0,1].forEach(function(idx) {
274 expect(children[idx]).to.be.instanceOf(documentElement.DocumentNodeElement);
278 it('trims white space from the beginning and the end of the block elements', function() {
279 var c = canvas.fromXML('<section> Alice <span>has</span> a cat </section>');
280 expect(c.doc().children()[0].getText()).to.equal('Alice ');
281 expect(c.doc().children()[2].getText()).to.equal(' a cat');
284 it('normalizes string of white characters to one space at the inline element boundries', function() {
285 var c = canvas.fromXML('<span> Alice has a cat </span>');
286 expect(c.doc().children()[0].getText()).to.equal(' Alice has a cat ');
289 it('normalizes string of white characters to one space before inline element', function() {
290 var c = canvas.fromXML('<div>Alice has <span>a cat</span></div>');
291 expect(c.doc().children()[0].getText()).to.equal('Alice has ');
294 it('normalizes string of white characters to one space after inline element', function() {
295 var c = canvas.fromXML('<div>Alice has <span>a</span> cat</div>');
296 expect(c.doc().children()[2].getText()).to.equal(' cat');
300 describe('getting vertically first text element', function() {
301 it('returns the first child if it\'s text element, ignores metadata', function() {
302 var c = canvas.fromXML('<section><metadata><dc:author>author</dc:author></metadata>Alice<div>has</div>a cat</section>'),
303 first = c.doc().getVerticallyFirstTextElement();
305 expect(first.sameNode(c.doc().children()[1])).to.be.true;
308 it('looks recursively inside node elements if they precede text element', function() {
309 var c = canvas.fromXML('\
318 textAlice = c.doc().children()[0].children()[0].children()[0],
319 first = c.doc().getVerticallyFirstTextElement();
321 expect(textAlice).to.be.instanceOf(documentElement.DocumentTextElement);
322 expect(first.sameNode(textAlice)).to.be.true;
327 describe('manipulation api', function() {
329 describe('Basic Element inserting', function() {
330 it('can put new NodeElement at the end', function() {
331 var c = canvas.fromXML('<section></section>'),
332 appended = c.doc().append({tag: 'header', klass: 'some.class'}),
333 children = c.doc().children();
335 expect(children.length).to.equal(1);
336 expect(children[0].sameNode(appended)).to.be.true;
339 it('can put new TextElement at the end', function() {
340 var c = canvas.fromXML('<section></section>'),
341 appended = c.doc().append({text: 'Alice'}),
342 children = c.doc().children();
344 expect(children.length).to.equal(1);
345 expect(children[0].sameNode(appended)).to.be.true;
346 expect(children[0].getText()).to.equal('Alice');
349 it('can put new NodeElement after another NodeElement', function() {
350 var c = canvas.fromXML('<section><div></div></section>'),
351 div = c.doc().children()[0],
352 added = div.after({tag: 'header', klass: 'some.class'}),
353 children = c.doc().children();
354 expect(children.length).to.equal(2);
355 expect(children[1].sameNode(added)).to.be.true;
358 it('can put new Nodeelement before another element', function() {
359 var c = canvas.fromXML('<section><div></div></section>'),
360 div = c.doc().children()[0],
361 added = div.before({tag: 'header', klass: 'some.class'}),
362 children = c.doc().children();
363 expect(children.length).to.equal(2);
364 expect(children[0].sameNode(added)).to.be.true;
367 it('can put new DocumentNodeElement after DocumentTextElement', function() {
368 var c = canvas.fromXML('<section>Alice</section>'),
369 text = c.doc().children()[0],
370 added = text.after({tag: 'p'}),
371 children = c.doc().children();
373 expect(children.length).to.equal(2);
374 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
375 expect(children[0].getText()).to.equal('Alice');
376 expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
377 expect(children[1].sameNode(added)).to.be.true;
379 it('can put new DocumentNodeElement before DocumentTextElement', function() {
380 var c = canvas.fromXML('<section>Alice</section>'),
381 text = c.doc().children()[0],
382 added = text.before({tag: 'p'}),
383 children = c.doc().children();
385 expect(children.length).to.equal(2);
386 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
387 expect(children[0].sameNode(added)).to.be.true;
388 expect(children[1]).to.be.instanceOf(documentElement.DocumentTextElement);
389 expect(children[1].getText()).to.equal('Alice');
392 it('can divide DocumentTextElement with a new DocumentNodeElement', function() {
393 var c = canvas.fromXML('<section>Alice has a cat</section>'),
395 text = section.children()[0];
397 var returned = text.divide({tag: 'aside', klass: 'footnote', offset: 5}),
398 sectionChildren = section.children(),
399 lhsText = sectionChildren[0],
400 rhsText = sectionChildren[2];
402 expect(lhsText.getText()).to.equal('Alice');
403 expect(returned.sameNode(sectionChildren[1]));
404 expect(rhsText.getText()).to.equal(' has a cat');
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 beforeEach(function() {
1000 getSelection = sinon.stub(window, 'getSelection');
1003 afterEach(function() {
1004 getSelection.restore();
1007 it('returns position when browser selection collapsed', function() {
1008 var c = canvas.fromXML('<section>Alice has a cat</section>'),
1009 dom = c.doc().dom(),
1010 text = $(dom.contents()[0]).contents()[0];
1012 expect(text.nodeType).to.equal(Node.TEXT_NODE, 'correct node selected');
1013 expect($(text).text()).to.equal('Alice has a cat');
1015 getSelection.returns({
1022 var cursor = c.getCursor(),
1023 position = cursor.getPosition();
1025 expect(cursor.isSelecting()).to.equal(false, 'cursor is not selecting anything');
1026 expect(position.element.getText()).to.equal('Alice has a cat');
1027 expect(position.offset).to.equal(5);
1028 expect(position.offsetAtEnd).to.equal(false, 'offset is not at end');
1030 getSelection.returns({
1038 expect(cursor.getPosition().offsetAtEnd).to.equal(true, 'offset at end');
1041 it('returns boundries of selection when browser selection not collapsed', function() {
1042 var c = canvas.fromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
1043 dom = c.doc().dom(),
1045 alice: dom.contents()[0],
1046 has: $(dom.contents()[1]).contents()[0],
1047 cat: dom.contents()[4]
1049 cursor = c.getCursor(),
1050 aliceElement = c.getDocumentElement(text.alice),
1051 catElement = c.getDocumentElement(text.cat);
1055 {focus: text.alice, focusOffset: 1, anchor: text.cat, anchorOffset: 2, selectionAnchor: catElement},
1056 {focus: text.cat, focusOffset: 2, anchor: text.alice, anchorOffset: 1, selectionAnchor: aliceElement}
1057 ].forEach(function(s, idx) {
1058 getSelection.returns({isColapsed: false, anchorNode: s.anchor, anchorOffset: s.anchorOffset, focusNode: s.focus, focusOffset: s.focusOffset});
1060 var selectionStart = cursor.getSelectionStart(),
1061 selectionEnd = cursor.getSelectionEnd(),
1062 selectionAnchor = cursor.getSelectionAnchor();
1064 expect(cursor.isSelecting()).to.equal(true, 'cursor is selecting');
1065 expect(selectionStart.element.sameNode(aliceElement)).to.equal(true, '"Alice" is the start of the selection ' + idx);
1066 expect(selectionStart.offset).to.equal(1, '"Alice" offset ok' + idx);
1067 expect(selectionEnd.element.sameNode(catElement)).to.equal(true, '"Cat" is the start of the selection ' + idx);
1068 expect(selectionEnd.offset).to.equal(2, '"Cat" offset ok' + idx);
1069 expect(selectionAnchor.element.sameNode(s.selectionAnchor)).to.equal(true, 'anchor ok');
1070 expect(selectionAnchor.offset).to.equal(s.anchorOffset, 'anchor offset ok');
1074 it('recognizes when browser selection boundries lies in sibling DocumentTextElements', function() {
1075 var c = canvas.fromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
1076 dom = c.doc().dom(),
1078 alice: dom.contents()[0],
1079 has: $(dom.contents()[1]).contents()[0],
1080 a: dom.contents()[2],
1081 big: $(dom.contents()[3]).contents()[0],
1082 cat: dom.contents()[4]
1084 cursor = c.getCursor();
1086 expect($(text.alice).text()).to.equal('Alice ');
1087 expect($(text.has).text()).to.equal('has');
1088 expect($(text.a).text()).to.equal(' a ');
1089 expect($(text.big).text()).to.equal('big');
1090 expect($(text.cat).text()).to.equal(' cat');
1092 getSelection.returns({anchorNode: text.alice, focusNode: text.a});
1093 expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "a" are children');
1095 getSelection.returns({anchorNode: text.alice, focusNode: text.cat});
1096 expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "cat" are children');
1098 getSelection.returns({anchorNode: text.alice, focusNode: text.has});
1099 expect(cursor.isSelectingSiblings()).to.equal(false, '"Alice" and "has" are not children');
1101 getSelection.returns({anchorNode: text.has, focusNode: text.big});
1102 expect(cursor.isSelectingSiblings()).to.equal(false, '"has" and "big" are not children');
1107 describe('Serializing document to WLXML', function() {
1108 it('keeps document intact when no changes have been made', function() {
1109 var xmlIn = '<section>Alice<div>has</div>a <span class="uri" meta-uri="http://cat.com">cat</span>!</section>',
1110 c = canvas.fromXML(xmlIn),
1113 var parser = new DOMParser(),
1114 input = parser.parseFromString(xmlIn, "application/xml").childNodes[0],
1115 output = parser.parseFromString(xmlOut, "application/xml").childNodes[0];
1117 expect(input.isEqualNode(output)).to.be.true;
1120 it('keeps arbitrary node attributes intact', function() {
1121 var xmlIn = '<section a="1" xmlns:dcterms="http://purl.org/dc/terms/"></section>',
1122 $xmlOut = $(canvas.fromXML(xmlIn).toXML());
1124 expect($xmlOut.attr('a')).to.equal('1');
1125 expect($xmlOut.attr('xmlns:dcterms')).to.equal('http://purl.org/dc/terms/');
1128 it('doesn\' serialize meta attribute if its empty', function() {
1131 c = canvas.fromXML('<section class="uri" meta-uri="some.uri"></section>');
1132 c.doc().setWlxmlMetaAttr('uri', '');
1133 expect($(c.toXML()).attr('meta-uri')).to.equal(undefined, 'overriding attribute with zero length string');
1135 c = canvas.fromXML('<section class="uri"></section>');
1136 c.doc().setWlxmlMetaAttr('uri', '');
1137 expect($(c.toXML()).attr('meta-uri')).to.equal(undefined, 'setting attribute to zero length string');
1140 describe('output xml', function() {
1141 it('keeps entities intact', function() {
1142 var xmlIn = '<section>< ></section>',
1143 c = canvas.fromXML(xmlIn),
1145 expect(xmlOut).to.equal(xmlIn);
1147 it('keeps entities intact when they form html/xml', function() {
1148 var xmlIn = '<section><abc></section>',
1149 c = canvas.fromXML(xmlIn),
1151 expect(xmlOut).to.equal(xmlIn);
1155 describe('formatting output xml', function() {
1156 /*it('keeps white spaces at the edges of input xml', function() {
1157 var xmlIn = ' <section></section> ',
1158 c = canvas.fromXML(xmlIn),
1161 expect(xmlOut.substr(4)).to.equal(' <', 'start');
1162 expect(xmlOut.substr(-2)).to.equal('> ', 'end');
1164 it('keeps white space between XML nodes', function() {
1165 var xmlIn = '<section>\n\n\n<div></div>\n\n\n<div></div>\n\n\n</section>',
1166 c = canvas.fromXML(xmlIn),
1169 var partsIn = xmlIn.split('\n\n\n'),
1170 partsOut = xmlOut.split('\n\n\n');
1172 expect(partsIn).to.deep.equal(partsOut);
1175 it('keeps white space between XML nodes - inline case', function() {
1176 var xmlIn = '<section>\n\n\n<span></span>\n\n\n<span></span>\n\n\n</section>',
1177 c = canvas.fromXML(xmlIn),
1180 var partsIn = xmlIn.split('\n\n\n'),
1181 partsOut = xmlOut.split('\n\n\n');
1183 expect(partsIn).to.deep.equal(partsOut);
1186 it('keeps white space at the beginning of text', function() {
1187 var xmlIn = '<section> abc<div>some div</div> abc</section>',
1188 c = canvas.fromXML(xmlIn),
1191 expect(xmlOut).to.equal(xmlIn);
1194 it('nests new children block elements', function() {
1195 var c = canvas.fromXML('<section></section>');
1197 c.doc().append({tag: 'header'});
1199 var xmlOut = c.toXML();
1200 expect(xmlOut.split('\n ')[0]).to.equal('<section>', 'nesting start ok');
1201 expect(xmlOut.split('\n').slice(-1)[0]).to.equal('</section>', 'nesting end ok');
1205 it('doesn\'t nest new children inline elements', function() {
1206 var c = canvas.fromXML('<section></section>');
1208 c.doc().append({tag: 'span'});
1210 var xmlOut = c.toXML();
1211 expect(xmlOut).to.equal('<section><span></span></section>');
1214 it('keeps original white space at the end of text', function() {
1216 var xmlIn = '<header> Some text ended with white space \
1218 <span class="uri">Some text</span> some text\
1221 c = canvas.fromXML(xmlIn);
1223 var xmlOut = c.toXML();
1224 console.log(xmlOut);
1225 expect(xmlOut).to.equal(xmlIn);
1228 it('keeps white space around text node', function() {
1229 var xmlIn = '<section>\
1230 <header>header1</header>\
1231 Some text surrounded by white space\
1232 <header>header2</header>\
1234 c = canvas.fromXML(xmlIn);
1236 var xmlOut = c.toXML();
1237 expect(xmlOut).to.equal(xmlIn);
1240 it('keeps white space around text node - last node case', function() {
1241 var xmlIn = '<section>\
1242 <header>header</header>\
1244 Some text surrounded by white space\
1247 c = canvas.fromXML(xmlIn);
1249 var xmlOut = c.toXML();
1250 expect(xmlOut).to.equal(xmlIn);
1253 it('keeps white space after detaching text element', function() {
1254 var xmlIn = '<section><header>header</header>\n\
1259 expectedXmlOut = '<section><header>header</header>\n\
1264 c = canvas.fromXML(xmlIn),
1265 children = c.doc().children(),
1266 text = children[children.length-1];
1268 expect(text.getText()).to.equal('text1');
1272 var xmlOut = c.toXML();
1273 expect(xmlOut).to.equal(expectedXmlOut);