Fixing text unwrapping
[fnpeditor.git] / modules / documentCanvas / canvas / canvas.test3.js
1 define([
2 'libs/chai',
3 'libs/sinon',
4 'modules/documentCanvas/canvas/canvas',
5 'modules/documentCanvas/canvas/documentElement',
6 'modules/documentCanvas/canvas/utils'
7 ], function(chai, sinon, canvas, documentElement, utils) {
8     
9 'use strict';
10
11 var expect = chai.expect;
12
13
14 describe('Canvas', function() {
15
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');
21             });
22         });
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');
26         });
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');
30         });
31     });
32
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');
41         });
42     });
43
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);
48         });
49
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');
53         });
54
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;
59             });
60         });
61
62         describe('DocumentTextElement', function() {
63             it('can have its content set', function() {
64                 var c = canvas.fromXML('<section>Alice</section>'),
65                     root = c.doc(),
66                     text = root.children()[0];
67                 
68                 text.setText('a cat');
69                 expect(root.children()[0].getText()).to.equal('a cat');
70             });
71         });
72
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>'),
76                     root = c.doc(),
77                     child = root.children()[1];
78                 expect(root.childIndex(child)).to.equal(1);
79             });
80
81             it('knows WLXML tag it renders', function(){
82                 var c = canvas.fromXML('<section></section>'),
83                     section = c.doc();
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');
87             });
88
89             it('knows WLXML class of a WLXML tag it renders', function(){
90                 var c = canvas.fromXML('<section class="some.class.A"></section>'),
91                     section = c.doc();
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;
97             });
98
99
100
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];
105                     
106                     expect(span.getWlxmlMetaAttr('uri')).to.equal('someuri');
107                     span.setWlxmlMetaAttr('uri', 'otheruri');
108                     expect(span.getWlxmlMetaAttr('uri')).to.equal('otheruri');
109                 });
110
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];
114                     
115                     expect(span.getWlxmlMetaAttr('uri')).to.equal('someuri');
116                     span.setWlxmlClass('author');
117                     expect(span.getWlxmlMetaAttr('uri')).to.be.undefined;
118                 });
119
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');
125                 });
126             });
127         });
128
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()));
135         });
136         
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);
142
143             expect(textNode.nodeType).to.equal(Node.TEXT_NODE, 'text node selected');
144             expect($(textNode).text()).to.equal('Alice');
145
146             expect(element).to.be.instanceOf(documentElement.DocumentTextElement);
147             expect(element.sameNode(c.doc().children()[0]));
148         });
149     });
150
151
152
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);
158             });
159             it('is of type DocumentNodeElement', function() {
160                 expect(c.doc()).to.be.instanceOf(documentElement.DocumentNodeElement);
161             });
162         });
163
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;
173             });
174         });
175
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);
182
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);
188             });
189             
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);
195             });
196
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;
202                 });
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;
207                 });
208             });
209
210             describe('accessing sibling parents of two elements', function() {
211                 it('returns elements themself if they have direct common parent', function() {
212                     var c = canvas.fromXML('<section>\
213                         <div>\
214                             <div>A</div>\
215                             <div>B</div>\
216                         </div>\
217                     </section>'),
218                         section = c.doc(),
219                         wrappingDiv = c.doc().children()[0],
220                         divA = wrappingDiv.children()[0],
221                         divB = wrappingDiv.children()[1];
222
223                     var siblingParents = c.getSiblingParents({element1: divA, element2: divB});
224
225                     expect(siblingParents.element1.sameNode(divA)).to.equal(true, 'divA');
226                     expect(siblingParents.element2.sameNode(divB)).to.equal(true, 'divB');
227                 });
228
229                 it('returns sibling parents - example 1', function() {
230                     var c = canvas.fromXML('<section>Alice <span>has a cat</span></section>'),
231                         section = c.doc(),
232                         aliceText = section.children()[0],
233                         span = section.children()[1],
234                         spanText = span.children()[0];
235
236                     var siblingParents = c.getSiblingParents({element1: aliceText, element2: spanText});
237
238                     expect(siblingParents.element1.sameNode(aliceText)).to.equal(true, 'aliceText');
239                     expect(siblingParents.element2.sameNode(span)).to.equal(true, 'span');
240                 });
241             })
242
243             describe('free text handling', function() {
244                     it('sees free text', function() {
245                         var c = canvas.fromXML('<section>Alice <span>has</span> a cat</section>'),
246                             children = c.doc().children();
247                         expect(children.length).to.equal(3);
248                         expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
249                         expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
250                         expect(children[2]).to.be.instanceOf(documentElement.DocumentTextElement);
251                     });
252             });
253             
254             describe('white characters handling', function() {
255                 it('says empty element node has no children', function() {
256                     var c = canvas.fromXML('<section></section>');
257                     expect(c.doc().children().length).to.equal(0);
258                 });
259                 it('says element node with one space has one DocumentTextElement', function() {
260                     var c = canvas.fromXML('<section> </section>');
261                     expect(c.doc().children().length).to.equal(1);
262                     expect(c.doc().children()[0]).to.be.instanceOf(documentElement.DocumentTextElement);
263                     expect(c.doc().children()[0].getText()).to.equal(' ');
264                 });
265                 it('ignores white space surrounding block elements', function() {
266                     var c = canvas.fromXML('<section> <div></div> </section>');
267                     var children = c.doc().children();
268                     expect(children.length).to.equal(1);
269                     expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
270                 });
271                 it('ignores white space between block elements', function() {
272                     var c = canvas.fromXML('<section><div></div> <div></div></section>');
273                     var children = c.doc().children();
274                     expect(children.length === 2);
275                     [0,1].forEach(function(idx) {
276                         expect(children[idx]).to.be.instanceOf(documentElement.DocumentNodeElement);
277                     });
278                 });
279
280                 it('trims white space from the beginning and the end of the block elements', function() {
281                     var c = canvas.fromXML('<section> Alice <span>has</span> a cat </section>');
282                     expect(c.doc().children()[0].getText()).to.equal('Alice ');
283                     expect(c.doc().children()[2].getText()).to.equal(' a cat');
284                 });
285
286                 it('normalizes string of white characters to one space at the inline element boundries', function() {
287                     var c = canvas.fromXML('<span>   Alice has a cat   </span>');
288                     expect(c.doc().children()[0].getText()).to.equal(' Alice has a cat ');
289                 });
290
291                 it('normalizes string of white characters to one space before inline element', function() {
292                     var c = canvas.fromXML('<div>Alice has  <span>a cat</span></div>');
293                     expect(c.doc().children()[0].getText()).to.equal('Alice has ');
294                 });
295                 
296                 it('normalizes string of white characters to one space after inline element', function() {
297                     var c = canvas.fromXML('<div>Alice has <span>a</span>  cat</div>');
298                     expect(c.doc().children()[2].getText()).to.equal(' cat');
299                 });
300             });
301
302             describe('getting vertically first text element', function() {
303                 it('returns the first child if it\'s text element, ignores metadata', function() {
304                     var c = canvas.fromXML('<section><metadata><dc:author>author</dc:author></metadata>Alice<div>has</div>a cat</section>'),
305                         first = c.doc().getVerticallyFirstTextElement();
306
307                     expect(first.sameNode(c.doc().children()[1])).to.be.true;
308                 });
309
310                 it('looks recursively inside node elements if they precede text element', function() {
311                     var c = canvas.fromXML('\
312                             <section>\
313                                 <div>\
314                                     <div>\
315                                         Alice\
316                                     </div>\
317                                 </div>\
318                                 Some text\
319                             </section>'),
320                         textAlice = c.doc().children()[0].children()[0].children()[0],
321                         first = c.doc().getVerticallyFirstTextElement();
322
323                     expect(textAlice).to.be.instanceOf(documentElement.DocumentTextElement);
324                     expect(first.sameNode(textAlice)).to.be.true;
325                 });
326             });
327         });
328
329         describe('manipulation api', function() {
330
331             describe('Basic Element inserting', function() {
332                 it('can put new NodeElement at the end', function() {
333                     var c = canvas.fromXML('<section></section>'),
334                         appended = c.doc().append({tag: 'header', klass: 'some.class'}),
335                         children = c.doc().children();
336
337                     expect(children.length).to.equal(1);
338                     expect(children[0].sameNode(appended)).to.be.true;
339                 });
340
341                 it('can put new TextElement at the end', function() {
342                     var c = canvas.fromXML('<section></section>'),
343                         appended = c.doc().append({text: 'Alice'}),
344                         children = c.doc().children();
345
346                     expect(children.length).to.equal(1);
347                     expect(children[0].sameNode(appended)).to.be.true;
348                     expect(children[0].getText()).to.equal('Alice');
349                 });
350
351                 it('can put new NodeElement at the beginning', function() {
352                     var c = canvas.fromXML('<section><div></div></section>'),
353                         prepended = c.doc().prepend({tag: 'header', klass: 'some.class'}),
354                         children = c.doc().children();
355
356                     expect(children).to.have.length(2);
357                     expect(children[0].sameNode(prepended)).to.be.true;
358                 });
359
360                 it('can put new TextElement at the beginning', function() {
361                     var c = canvas.fromXML('<section><div></div></section>'),
362                         prepended = c.doc().prepend({text: 'Alice'}),
363                         children = c.doc().children();
364
365                     expect(children).to.have.length(2)
366                     expect(children[0].sameNode(prepended)).to.be.true;
367                     expect(children[0].getText()).to.equal('Alice');
368                 });
369
370                 it('can put new NodeElement after another NodeElement', function() {
371                     var c = canvas.fromXML('<section><div></div></section>'),
372                         div = c.doc().children()[0],
373                         added = div.after({tag: 'header', klass: 'some.class'}),
374                         children = c.doc().children();
375                     expect(children.length).to.equal(2);
376                     expect(children[1].sameNode(added)).to.be.true;
377                 });
378
379                 it('can put new Nodeelement before another element', function() {
380                     var c = canvas.fromXML('<section><div></div></section>'),
381                         div = c.doc().children()[0],
382                         added = div.before({tag: 'header', klass: 'some.class'}),
383                         children = c.doc().children();
384                     expect(children.length).to.equal(2);
385                     expect(children[0].sameNode(added)).to.be.true;
386                 });
387
388                 it('can put new DocumentNodeElement after DocumentTextElement', function() {
389                     var c = canvas.fromXML('<section>Alice</section>'),
390                         text = c.doc().children()[0],
391                         added = text.after({tag: 'p'}),
392                         children = c.doc().children();
393
394                     expect(children.length).to.equal(2);
395                     expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
396                     expect(children[0].getText()).to.equal('Alice');
397                     expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
398                     expect(children[1].sameNode(added)).to.be.true;
399                 });
400                 it('can put new DocumentNodeElement before DocumentTextElement', function() {
401                     var c = canvas.fromXML('<section>Alice</section>'),
402                         text = c.doc().children()[0],
403                         added = text.before({tag: 'p'}),
404                         children = c.doc().children();
405
406                     expect(children.length).to.equal(2);
407                     expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
408                     expect(children[0].sameNode(added)).to.be.true;
409                     expect(children[1]).to.be.instanceOf(documentElement.DocumentTextElement);
410                     expect(children[1].getText()).to.equal('Alice');
411                 });
412
413                 it('can divide DocumentTextElement with a new DocumentNodeElement', function() {
414                     var c = canvas.fromXML('<section>Alice has a cat</section>'),
415                         section = c.doc(),
416                         text = section.children()[0];
417
418                     var returned = text.divide({tag: 'aside', klass: 'footnote', offset: 5}),
419                         sectionChildren = section.children(),
420                         lhsText = sectionChildren[0],
421                         rhsText = sectionChildren[2];
422
423                     expect(lhsText.getText()).to.equal('Alice');
424                     expect(returned.sameNode(sectionChildren[1]));
425                     expect(rhsText.getText()).to.equal(' has a cat');
426                 });
427
428                 it('treats dividing DocumentTextElement at the very end as appending after it', function() {
429                     var c = canvas.fromXML('<section>Alice has a cat</section>'),
430                         section = c.doc(),
431                         text = section.children()[0];
432
433                     var returned = text.divide({tag: 'aside', klass: 'footnote', offset: 15}),
434                         sectionChildren = section.children(),
435                         textElement = sectionChildren[0],
436                         nodeElement = sectionChildren[1];
437
438                     expect(sectionChildren.length).to.equal(2);
439                     expect(textElement.getText()).to.equal('Alice has a cat');
440                     expect(returned.sameNode(nodeElement)).to.be.true;
441                     expect(nodeElement.getWlxmlTag()).to.equal('aside');
442                 });
443
444                 it('treats dividing DocumentTextElement at the very beginning as appending before it', function() {
445                     var c = canvas.fromXML('<section>Alice has a cat</section>'),
446                         section = c.doc(),
447                         text = section.children()[0];
448
449                     var returned = text.divide({tag: 'aside', klass: 'footnote', offset: 0}),
450                         sectionChildren = section.children(),
451                         nodeElement = sectionChildren[0],
452                         textElement = sectionChildren[1];
453                         
454                     expect(sectionChildren.length).to.equal(2);
455                     expect(textElement.getText()).to.equal('Alice has a cat');
456                     expect(returned.sameNode(nodeElement)).to.be.true;
457                     expect(nodeElement.getWlxmlTag()).to.equal('aside');
458                 });
459             });
460
461             describe('Removing elements', function() {
462                 it('merges left and right DocumentTextElement sibling of a detached DocumentNodeElement', function() {
463                     var c = canvas.fromXML('<section>Alice<div>has</div>a cat</section>'),
464                         section = c.doc(),
465                         div = section.children()[1];
466
467                     div.detach();
468
469                     var sectionChildren = section.children(),
470                         textElement = sectionChildren[0];
471
472                     expect(sectionChildren).to.have.length(1);
473                     expect(textElement.getText()).to.equal('Alicea cat');
474                 });
475             });
476
477             describe('Splitting text', function() {
478                 
479                 it('splits DocumentTextElement\'s parent into two DocumentNodeElements of the same type', function() {
480                     var c = canvas.fromXML('<section><header>Some header</header></section>'),
481                         section = c.doc(),
482                         text = section.children()[0].children()[0];
483
484                     var returnedValue = text.split({offset: 5});
485                     expect(section.children().length).to.equal(2, 'section has two children');
486                     
487                     var header1 = section.children()[0];
488                     var header2 = section.children()[1];
489
490                     expect(header1.getWlxmlTag()).to.equal('header', 'first section child represents wlxml header');
491                     expect(header1.children().length).to.equal(1, 'first header has one text child');
492                     expect(header1.children()[0].getText()).to.equal('Some ', 'first header has correct content');
493                     expect(header2.getWlxmlTag()).to.equal('header', 'second section child represents wlxml header');
494                     expect(header2.children().length).to.equal(1, 'second header has one text child');
495                     expect(header2.children()[0].getText()).to.equal('header', 'second header has correct content');
496
497                     expect(returnedValue.first.sameNode(header1)).to.equal(true, 'first node returnde');
498                     expect(returnedValue.second.sameNode(header2)).to.equal(true, 'second node returned');
499                 });
500
501                 it('leaves empty copy of DocumentNodeElement if splitting at the very beginning', function() {
502                         var c = canvas.fromXML('<section><header>Some header</header></section>'),
503                         section = c.doc(),
504                         text = section.children()[0].children()[0];
505
506                         text.split({offset: 0});
507                         
508                         var header1 = section.children()[0];
509                         var header2 = section.children()[1];
510
511                         expect(header1.children().length).to.equal(0);
512                         expect(header2.children()[0].getText()).to.equal('Some header');
513                 });
514
515                 it('leaves empty copy of DocumentNodeElement if splitting at the very end', function() {
516                         var c = canvas.fromXML('<section><header>Some header</header></section>'),
517                         section = c.doc(),
518                         text = section.children()[0].children()[0];
519
520                         text.split({offset: 11});
521                         
522                         var header1 = section.children()[0];
523                         var header2 = section.children()[1];
524
525                         expect(header1.children()[0].getText()).to.equal('Some header');
526                         expect(header2.children().length).to.equal(0);
527                 });
528
529                 it('keeps DocumentTextElement\'s parent\'s children elements intact', function() {
530                     var c = canvas.fromXML('\
531                             <section>\
532                                 <header>\
533                                     A <span>fancy</span> and <span>nice</span> header\
534                                 </header>\
535                             </section>'),
536                         section = c.doc(),
537                         header = section.children()[0],
538                         textAnd = header.children()[2];
539
540                     textAnd.split({offset: 2});
541                     
542                     var sectionChildren = section.children();
543                     expect(sectionChildren.length).to.equal(2, 'Section has two children');
544                     expect(sectionChildren[0].getWlxmlTag()).to.equal('header', 'First section element is a wlxml header');
545                     expect(sectionChildren[1].getWlxmlTag()).to.equal('header', 'Second section element is a wlxml header');
546
547                     var firstHeaderChildren = sectionChildren[0].children();
548                     expect(firstHeaderChildren.length).to.equal(3, 'First header has three children');
549                     expect(firstHeaderChildren[0].getText()).to.equal('A ', 'First header starts with a text');
550                     expect(firstHeaderChildren[1].getWlxmlTag()).to.equal('span', 'First header has span in the middle');
551                     expect(firstHeaderChildren[2].getText()).to.equal(' a', 'First header ends with text');
552
553                     var secondHeaderChildren = sectionChildren[1].children();
554                     expect(secondHeaderChildren.length).to.equal(3, 'Second header has three children');
555                     expect(secondHeaderChildren[0].getText()).to.equal('nd ', 'Second header starts with text');
556                     expect(secondHeaderChildren[1].getWlxmlTag()).to.equal('span', 'Second header has span in the middle');
557                     expect(secondHeaderChildren[2].getText()).to.equal(' header', 'Second header ends with text');
558                 });
559             });
560
561             describe('wrapping', function() {
562                 it('wraps DocumentNodeElement', function() {
563                     var c = canvas.fromXML('<section><div></div></section>'),
564                         div = c.doc().children()[0];
565                     
566                     var returned = div.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
567                         parent = div.parent(),
568                         parent2 = c.doc().children()[0];
569
570                     expect(returned.sameNode(parent)).to.be.true;
571                     expect(returned.sameNode(parent2)).to.be.true;
572                     expect(returned.getWlxmlTag()).to.equal('header');
573                     expect(returned.getWlxmlClass()).to.equal('some.class');
574                 });
575                 it('wraps DocumentTextElement', function() {
576                     var c = canvas.fromXML('<section>Alice</section>'),
577                         text = c.doc().children()[0];
578                     
579                     var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
580                         parent = text.parent(),
581                         parent2 = c.doc().children()[0];
582
583                     expect(returned.sameNode(parent)).to.be.true;
584                     expect(returned.sameNode(parent2)).to.be.true;
585                     expect(returned.getWlxmlTag()).to.equal('header');
586                     expect(returned.getWlxmlClass()).to.equal('some.class');
587                 });
588                 
589                 describe('wrapping part of DocumentTextElement', function() {
590                     [{start: 5, end: 12}, {start: 12, end: 5}].forEach(function(offsets) {
591                         it('wraps in the middle ' + offsets.start + '/' + offsets.end, function() {
592                             var c = canvas.fromXML('<section>Alice has a cat</section>'),
593                                 text = c.doc().children()[0];
594                             
595                             var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class', start: offsets.start, end: offsets.end}),
596                                 children = c.doc().children();
597
598                             expect(children.length).to.equal(3);
599                             
600                             expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
601                             expect(children[0].getText()).to.equal('Alice');
602
603                             expect(children[1].sameNode(returned)).to.be.true;
604                             expect(returned.getWlxmlTag()).to.equal('header');
605                             expect(returned.getWlxmlClass()).to.equal('some.class');
606                             expect(children[1].children().length).to.equal(1);
607                             expect(children[1].children()[0].getText()).to.equal(' has a ');
608
609                             expect(children[2]).to.be.instanceOf(documentElement.DocumentTextElement);
610                             expect(children[2].getText()).to.equal('cat');
611                         });
612                     });
613
614                     it('wraps whole text inside DocumentTextElement if offsets span entire content', function() {
615                          var c = canvas.fromXML('<section>Alice has a cat</section>'),
616                              text = c.doc().children()[0];
617                          
618                          var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class', start: 0, end: 15}),
619                              children = c.doc().children();
620
621                          expect(children.length).to.equal(1);
622                          expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
623                          expect(children[0].children()[0].getText()).to.equal('Alice has a cat');
624                     });
625                 });
626
627                 it('wraps text spanning multiple sibling DocumentTextNodes', function() {
628                     var c = canvas.fromXML('<section>Alice has a <span>small</span> cat</section>'),
629                         section = c.doc(),
630                         wrapper = c.wrapText({
631                             inside: section, 
632                             _with: {tag: 'span', klass: 'some.class'},
633                             offsetStart: 6,
634                             offsetEnd: 4,
635                             textNodeIdx: [0,2]
636                         });
637
638                     expect(section.children().length).to.equal(2);
639                     expect(section.children()[0]).to.be.instanceOf(documentElement.DocumentTextElement);
640                     expect(section.children()[0].getText()).to.equal('Alice ');
641
642                     var wrapper2 = section.children()[1];
643                     expect(wrapper2.sameNode(wrapper)).to.be.true;
644
645                     var wrapperChildren = wrapper.children();
646                     expect(wrapperChildren.length).to.equal(3);
647                     expect(wrapperChildren[0].getText()).to.equal('has a ');
648
649                     expect(wrapperChildren[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
650                     expect(wrapperChildren[1].children().length).to.equal(1);
651                     expect(wrapperChildren[1].children()[0].getText()).to.equal('small');
652
653                     expect(wrapperChildren[2].getText()).to.equal(' cat');
654                 });
655
656                 it('wraps multiple sibling Elements', function() {
657                     var c = canvas.fromXML('<section>Alice<div>has</div><div>a cat</div></section>'),
658                         section = c.doc(),
659                         aliceText = section.children()[0],
660                         firstDiv = section.children()[1],
661                         lastDiv = section.children()[section.children().length -1];
662
663                     var returned = c.wrapElements({
664                             element1: aliceText,
665                             element2: lastDiv,
666                             _with: {tag: 'header'}
667                         });
668
669                     var sectionChildren = section.children(),
670                         header = sectionChildren[0],
671                         headerChildren = header.children();
672
673                     expect(sectionChildren).to.have.length(1);
674                     expect(header.sameNode(returned)).to.equal(true, 'wrapper returned');
675                     expect(headerChildren).to.have.length(3);
676                     expect(headerChildren[0].sameNode(aliceText)).to.equal(true, 'first node wrapped');
677                     expect(headerChildren[1].sameNode(firstDiv)).to.equal(true, 'second node wrapped');
678                     expect(headerChildren[2].sameNode(lastDiv)).to.equal(true, 'third node wrapped');
679                 });
680                 it('wraps multiple sibling Elements - middle case', function() {
681                     var c = canvas.fromXML('<section><div></div>div></div><div></div><div></div></section>'),
682                         section = c.doc(),
683                         div1 = section.children()[0],
684                         div2 = section.children()[1],
685                         div3 = section.children()[2],
686                         div4 = section.children()[3];
687
688                     var returned = c.wrapElements({
689                             element1: div2,
690                             element2: div3,
691                             _with: {tag: 'header'}
692                         });
693
694                     var sectionChildren = section.children(),
695                         header = sectionChildren[1],
696                         headerChildren = header.children();
697
698                     expect(sectionChildren).to.have.length(3);
699                     expect(headerChildren).to.have.length(2);
700                     expect(headerChildren[0].sameNode(div2)).to.equal(true, 'first node wrapped');
701                     expect(headerChildren[1].sameNode(div3)).to.equal(true, 'second node wrapped');
702                 });
703             });
704
705             describe('unwrapping DocumentTextElement from its parent DocumentNodeElement if it\'s its only child', function() {
706                 it('unwraps text element from its parent and stays between its old parent siblings', function() {
707                     var c = canvas.fromXML('<section><div>Alice</div><div>has</div><div>a cat</div></section>'),
708                         section = c.doc(),
709                         sectionChildren = section.children(),
710                         divAlice = sectionChildren[0],
711                         divHas = sectionChildren[1],
712                         textHas = divHas.children()[0],
713                         divCat = sectionChildren[2];
714
715                     var newTextContainer = textHas.unwrap(),
716                         sectionChildren = section.children();
717
718                     expect(sectionChildren[0].sameNode(divAlice)).to.equal(true, 'divAlice ok');
719                     expect(newTextContainer.sameNode(section)).to.equal(true, 'unwrap returns new text parent DocumentNodeElement');
720                     expect(sectionChildren[1].getText()).to.equal('has');
721                     expect(sectionChildren[2].sameNode(divCat)).to.equal(true, 'divCat ok');
722
723                 });
724                 it('unwraps and join with its old parent adjacent text elements ', function() {
725                     var c = canvas.fromXML('<section>Alice <span>has a</span> cat</section>'),
726                     section = c.doc(),
727                     text = section.children()[1].children()[0];
728
729                     var newTextContainer = text.unwrap();
730
731                     expect(section.children().length).to.equal(1, 'section has one child');
732                     expect(section.children()[0].getText()).to.equal('Alice has a cat');
733                     expect(newTextContainer.sameNode(c.doc())).to.equal(true, 'unwrap returns new text parent DocumentNodeElement');
734                 });
735
736                 it('unwraps text element from its parent - first child case', function() {
737                     var c = canvas.fromXML('<section><span class="uri">Some</span>text</section>'),
738                         section = c.doc(),
739                         span = section.children()[0];
740
741                     span.children()[0].unwrap();
742
743                     var sectionChildren = section.children();
744
745                     expect(sectionChildren).to.have.length(1);
746                     expect(sectionChildren[0].getText()).to.equal('Sometext');
747                 });
748             });
749         });
750
751         describe('Lists api', function() {
752             describe('creating lists', function() {
753                 it('allows creation of a list from existing sibling DocumentElements', function() {
754                     var c = canvas.fromXML('\
755                         <section>\
756                             Alice\
757                             <div>has</div>\
758                             a\
759                             <div>cat</div>\
760                         </section>'),
761                         section = c.doc(),
762                         textHas = section.children()[1],
763                         divA = section.children()[2]
764                     
765                     c.list.create({element1: textHas, element2: divA});
766
767                     expect(section.children().length).to.equal(3, 'section has three child elements');
768
769                     var child1 = section.children()[0],
770                         list = section.children()[1],
771                         child3 = section.children()[2];
772
773                     expect(child1.getText()).to.equal('Alice');
774                     expect(list.is('list')).to.equal(true, 'second child is a list');
775                     expect(list.children().length).to.equal(2, 'list contains two elements');
776                     list.children().forEach(function(child) {
777                         expect(child.getWlxmlClass()).to.equal('item', 'list childs have wlxml class of item');
778                     });
779                     expect(child3.children()[0].getText()).to.equal('cat');
780                 });
781                 
782                 it('allows creating nested list from existing sibling list items', function() {
783                     var c = canvas.fromXML('\
784                         <section>\
785                             <div class="list-items">\
786                                 <div class="item">A</div>\
787                                 <div class="item">B</div>\
788                                 <div class="item">C</div>\
789                                 <div class="item">D</div>\
790                             </div>\
791                         </section>'),
792                         outerList = c.doc().children()[0],
793                         itemB = outerList.children()[1],
794                         itemC = outerList.children()[2];
795
796
797                         c.list.create({element1: itemB, element2: itemC});
798
799                     var outerListItems = outerList.children(),
800                         innerList = outerListItems[1].children()[0],
801                         innerListItems = innerList.children();
802
803                     expect(outerListItems.length).to.equal(3, 'outer list has three items');
804                     expect(outerListItems[0].children()[0].getText()).to.equal('A', 'first outer item ok');
805                     expect(outerListItems[1].getWlxmlClass()).to.equal('item', 'inner list is wrapped by item element');
806
807                     expect(innerList.is('list')).to.equal(true, 'inner list created');
808                     expect(innerListItems.length).to.equal(2, 'inner list has two items');
809                     expect(innerListItems[0].children()[0].getText()).to.equal('B', 'first inner item ok');
810                     expect(innerListItems[1].children()[0].getText()).to.equal('C', 'second inner item ok');
811
812                     expect(outerListItems[2].children()[0].getText()).to.equal('D', 'last outer item ok');
813
814                 });
815
816             });
817
818             describe('extracting list items', function() {
819                 it('creates two lists with extracted items in the middle if extracting from the middle of the list', function() {
820                     var c = canvas.fromXML('\
821                         <section>\
822                             <div class="list.items">\
823                                 <div class="item">0</div>\
824                                 <div class="item">1</div>\
825                                 <div class="item">2</div>\
826                                 <div class="item">3</div>\
827                             </div>\
828                         </section>'),
829                         list = c.doc().children()[0],
830                         item1 = list.children()[1],
831                         item2 = list.children()[2];
832
833                     c.list.extractItems({element1: item1, element2: item2});
834
835                     var section = c.doc(),
836                         list1 = section.children()[0],
837                         oldItem1 = section.children()[1],
838                         oldItem2 = section.children()[2],
839                         list2 = section.children()[3];
840
841                     expect(section.children().length).to.equal(4, 'section contains four children');
842                     
843                     expect(list1.is('list')).to.equal(true, 'first section child is a list');
844                     expect(list1.children().length).to.equal(1, 'first list has one child');
845                     expect(list1.children()[0].children()[0].getText()).to.equal('0', 'first item of the first list is a first item of the original list');
846
847                     expect(oldItem1.children()[0].getText()).to.equal('1', 'first item got extracted');
848                     expect(oldItem1.getWlxmlClass() === undefined).to.equal(true, 'first extracted element has no wlxml class');
849
850                     expect(oldItem2.children()[0].getText()).to.equal('2', 'second item got extracted');
851                     expect(oldItem2.getWlxmlClass() === undefined).to.equal(true, 'second extracted element has no wlxml class');
852
853                     expect(list2.is('list')).to.equal(true, 'last section child is a list');
854                     expect(list2.children().length).to.equal(1, 'second list has one child');
855                     expect(list2.children()[0].children()[0].getText()).to.equal('3', 'first item of the second list is a last item of the original list');
856                 });
857
858                 it('puts extracted items above the list if starting item is the first one', function() {
859                     var c = canvas.fromXML('\
860                         <section>\
861                             <div class="list.items">\
862                                 <div class="item">0</div>\
863                                 <div class="item">1</div>\
864                                 <div class="item">2</div>\
865                             </div>\
866                         </section>'),
867                         list = c.doc().children()[0],
868                         item1 = list.children()[0],
869                         item2 = list.children()[1],
870                         item3 = list.children()[2];
871
872                     c.list.extractItems({element1: item1, element2: item2});
873
874                     var section = c.doc(),
875                         oldItem1 = section.children()[0],
876                         oldItem2 = section.children()[1],
877                         newList = section.children()[2];
878
879                     expect(section.children().length).to.equal(3, 'section has three children');
880                     expect(oldItem1.children()[0].getText()).to.equal('0', 'first item extracted');
881                     expect(oldItem2.children()[0].getText()).to.equal('1', 'second item extracted');
882                     expect(newList.is('list')).to.equal(true, 'list lies below extracted item');
883                     expect(newList.children().length).to.equal(1, 'list has now one child');
884                 });
885
886                 it('puts extracted items below the list if ending item is the last one', function() {
887                     var c = canvas.fromXML('\
888                         <section>\
889                             <div class="list.items">\
890                                 <div class="item">0</div>\
891                                 <div class="item">1</div>\
892                                 <div class="item">2</div>\
893                             </div>\
894                         </section>'),
895                         list = c.doc().children()[0],
896                         item1 = list.children()[0],
897                         item2 = list.children()[1],
898                         item3 = list.children()[2];
899
900                     c.list.extractItems({element1: item2, element2: item3});
901
902                     var section = c.doc(),
903                         oldItem1 = section.children()[1],
904                         oldItem2 = section.children()[2],
905                         newList = section.children()[0];
906
907                     expect(section.children().length).to.equal(3, 'section has three children');
908                     expect(oldItem1.children()[0].getText()).to.equal('1', 'first item extracted');
909                     expect(oldItem2.children()[0].getText()).to.equal('2', 'second item extracted');
910                     expect(newList.is('list')).to.equal(true, 'list lies above extracted item');
911                     expect(newList.children().length).to.equal(1, 'list has now one child');
912                 });
913
914                 it('removes list if all its items are extracted', function() {
915                     var c = canvas.fromXML('\
916                         <section>\
917                             <div class="list.items">\
918                                 <div class="item">some item</div>\
919                                 <div class="item">some item 2</div>\
920                             </div>\
921                         </section>'),
922                         list = c.doc().children()[0],
923                         item1 = list.children()[0],
924                         item2 = list.children()[1];
925
926                     c.list.extractItems({element1: item1, element2: item2});
927
928                     var section = c.doc(),
929                         list1 = section.children()[0],
930                         oldItem1 = section.children()[0],
931                         oldItem2 = section.children()[1];
932
933                     expect(section.children().length).to.equal(2, 'section contains two children');
934                     expect(oldItem1.children()[0].getText()).to.equal('some item');
935                     expect(oldItem2.children()[0].getText()).to.equal('some item 2');
936                 });
937
938                 it('creates two lists with extracted items in the middle if extracting from the middle of the list - nested case' , function() {
939                     var c = canvas.fromXML('\
940                         <section>\
941                             <div class="list.items">\
942                                 <div class="item">0</div>\
943                                 <div class="item">\
944                                     <div class="list.items">\
945                                         <div class="item">1.1</div>\
946                                         <div class="item">1.2</div>\
947                                         <div class="item">1.3</div>\
948                                     </div>\
949                                 </div>\
950                                 <div class="item">2</div>\
951                             </div>\
952                         </section>'),
953                         list = c.doc().children()[0],
954                         nestedList = list.children()[1].children()[0],
955                         nestedListItem = nestedList.children()[1];
956
957                     c.list.extractItems({element1: nestedListItem, element2: nestedListItem});
958
959                     var section = c.doc(),
960                         list = section.children()[0],
961                         item1 = list.children()[0],
962                         item2 = list.children()[1], //
963                         item3 = list.children()[2],
964                         item4 = list.children()[3], //
965                         item5 = list.children()[4],
966                         nestedList1 = item2.children()[0],
967                         nestedList2 = item4.children()[0];
968
969                     expect(list.children().length).to.equal(5, 'top list has five items');
970                     
971                     expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
972
973                     expect(item2.getWlxmlClass()).to.equal('item', 'first nested list is still wrapped in item element');
974                     expect(nestedList1.children().length).to.equal(1, 'first nested list is left with one child');
975                     expect(nestedList1.children()[0].children()[0].getText()).to.equal('1.1', 'first nested list item left alone');
976                     
977                     expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
978
979                     expect(item4.getWlxmlClass()).to.equal('item', 'second nested list is still wrapped in item element');
980                     expect(nestedList2.children().length).to.equal(1, 'second nested list is left with one child');
981                     expect(nestedList2.children()[0].children()[0].getText()).to.equal('1.3', 'second nested list item left alone');
982
983                     expect(item5.children()[0].getText()).to.equal('2', 'last item ok');
984                 });
985
986                 it('puts extracted items below the list if ending item is the last one - nested case' , function() {
987                     var c = canvas.fromXML('\
988                         <section>\
989                             <div class="list.items">\
990                                 <div class="item">0</div>\
991                                 <div class="item">\
992                                     <div class="list.items">\
993                                         <div class="item">1.1</div>\
994                                         <div class="item">1.2</div>\
995                                         <div class="item">1.3</div>\
996                                     </div>\
997                                 </div>\
998                                 <div class="item">2</div>\
999                             </div>\
1000                         </section>'),
1001                         list = c.doc().children()[0],
1002                         nestedList = list.children()[1].children()[0],
1003                         nestedListItem1 = nestedList.children()[1],
1004                         nestedListItem2 = nestedList.children()[2];
1005
1006                     c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
1007
1008                     var section = c.doc(),
1009                         list = section.children()[0],
1010                         item1 = list.children()[0],
1011                         item2 = list.children()[1],
1012                         item3 = list.children()[2],
1013                         item4 = list.children()[3],
1014                         item5 = list.children()[4];
1015                     nestedList = item2.children()[0];
1016
1017                     expect(list.children().length).to.equal(5, 'top list has five items');
1018                     expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
1019                     expect(item2.getWlxmlClass()).to.equal('item', 'nested list is still wrapped in item element');
1020                     expect(nestedList.children().length).to.equal(1, 'nested list is left with one child');
1021                     expect(nestedList.children()[0].children()[0].getText()).to.equal('1.1', 'nested list item left alone');
1022                     expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
1023                     expect(item4.children()[0].getText()).to.equal('1.3', 'fourth item ok');
1024                     expect(item5.children()[0].getText()).to.equal('2', 'fifth item ok');
1025                 });
1026
1027                 it('puts extracted items above the list if starting item is the first one - nested case' , function() {
1028                     var c = canvas.fromXML('\
1029                         <section>\
1030                             <div class="list.items">\
1031                                 <div class="item">0</div>\
1032                                 <div class="item">\
1033                                     <div class="list.items">\
1034                                         <div class="item">1.1</div>\
1035                                         <div class="item">1.2</div>\
1036                                         <div class="item">1.3</div>\
1037                                     </div>\
1038                                 </div>\
1039                                 <div class="item">2</div>\
1040                             </div>\
1041                         </section>'),
1042                         list = c.doc().children()[0],
1043                         nestedList = list.children()[1].children()[0],
1044                         nestedListItem1 = nestedList.children()[0],
1045                         nestedListItem2 = nestedList.children()[1];
1046
1047                     c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
1048
1049                     var section = c.doc(),
1050                         list = section.children()[0],
1051                         item1 = list.children()[0],
1052                         item2 = list.children()[1],
1053                         item3 = list.children()[2],
1054                         item4 = list.children()[3],
1055                         item5 = list.children()[4];
1056                     nestedList = item4.children()[0];
1057
1058                     expect(list.children().length).to.equal(5, 'top list has five items');
1059                     expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
1060                     expect(item2.children()[0].getText()).to.equal('1.1', 'second item ok');
1061                     expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
1062                     
1063                     expect(item4.getWlxmlClass()).to.equal('item', 'nested list is still wrapped in item element');
1064                     expect(nestedList.children().length).to.equal(1, 'nested list is left with one child');
1065                     expect(nestedList.children()[0].children()[0].getText()).to.equal('1.3', 'nested list item left alone');
1066                     expect(item5.children()[0].getText()).to.equal('2', 'fifth item ok');
1067                 });
1068
1069                 it('removes list if all its items are extracted - nested case', function() {
1070                     var c = canvas.fromXML('\
1071                         <section>\
1072                             <div class="list.items">\
1073                                 <div class="item">0</div>\
1074                                 <div class="item">\
1075                                     <div class="list.items">\
1076                                         <div class="item">1.1</div>\
1077                                         <div class="item">1.2</div>\
1078                                     </div>\
1079                                 </div>\
1080                                 <div class="item">2</div>\
1081                             </div>\
1082                         </section>'),
1083                         list = c.doc().children()[0],
1084                         nestedList = list.children()[1].children()[0],
1085                         nestedListItem1 = nestedList.children()[0],
1086                         nestedListItem2 = nestedList.children()[1];
1087
1088                     c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
1089
1090                     var section = c.doc(),
1091                         list = section.children()[0],
1092                         item1 = list.children()[0],
1093                         item2 = list.children()[1],
1094                         item3 = list.children()[2],
1095                         item4 = list.children()[3];
1096
1097                     expect(list.children().length).to.equal(4, 'top list has four items');
1098                     expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
1099                     expect(item2.children()[0].getText()).to.equal('1.1', 'second item ok');
1100                     expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
1101                     expect(item4.children()[0].getText()).to.equal('2', 'fourth item ok');
1102                 });
1103
1104                 it('extracts items out of outer most list when merge flag is set to false', function() {
1105                     var c = canvas.fromXML('\
1106                         <section>\
1107                             <div class="list.items">\
1108                                 <div class="item">0</div>\
1109                                 <div class="item">\
1110                                     <div class="list.items">\
1111                                         <div class="item">1.1</div>\
1112                                         <div class="item">1.2</div>\
1113                                     </div>\
1114                                 </div>\
1115                                 <div class="item">2</div>\
1116                             </div>\
1117                         </section>'),
1118                         section = c.doc(),
1119                         list = section.children()[0],
1120                         nestedList = list.children()[1].children()[0],
1121                         nestedListItem = nestedList.children()[0];
1122
1123                     var test = c.list.extractItems({element1: nestedListItem, element2: nestedListItem, merge: false});
1124
1125                     expect(test).to.equal(true, 'extraction status ok');
1126
1127                     var sectionChildren = section.children(),
1128                         extractedItem = sectionChildren[1];
1129
1130                     expect(sectionChildren.length).to.equal(3, 'section has three children');
1131                     expect(sectionChildren[0].is('list')).to.equal(true, 'first child is a list');
1132
1133                     expect(extractedItem.getWlxmlTag()).to.equal('div', 'extracted item is a wlxml div');
1134                     expect(extractedItem.getWlxmlClass()).to.equal(undefined, 'extracted item has no wlxml class');
1135                     expect(extractedItem.children()[0].getText()).to.equal('1.1', 'extracted item ok');
1136                     expect(sectionChildren[2].is('list')).to.equal(true, 'second child is a list');
1137                 });
1138             });
1139         });
1140
1141     });
1142
1143     describe('Cursor', function() {
1144
1145         var getSelection;
1146
1147         var findTextNode = function(inside, text) {
1148             var nodes = inside.find(':not(iframe)').addBack().contents().filter(function() {
1149                 return this.nodeType === Node.TEXT_NODE && this.data === text;
1150             });
1151             if(nodes.length)
1152                 return nodes[0];
1153             return null;
1154         }
1155
1156         beforeEach(function() {
1157             getSelection = sinon.stub(window, 'getSelection');
1158         });
1159
1160         afterEach(function() {
1161             getSelection.restore();
1162         });
1163
1164         it('returns position when browser selection collapsed', function() {
1165             var c = canvas.fromXML('<section>Alice has a cat</section>'),
1166                 dom = c.doc().dom(),
1167                 text = findTextNode(dom, 'Alice has a cat');
1168
1169             expect(text.nodeType).to.equal(Node.TEXT_NODE, 'correct node selected');
1170             expect($(text).text()).to.equal('Alice has a cat');
1171
1172             getSelection.returns({
1173                 anchorNode: text,
1174                 focusNode: text,
1175                 anchorOffset: 5,
1176                 focusOffset: 5,
1177                 isCollapsed: true
1178             });
1179             var cursor = c.getCursor(),
1180                 position = cursor.getPosition();
1181
1182             expect(cursor.isSelecting()).to.equal(false, 'cursor is not selecting anything');
1183             expect(position.element.getText()).to.equal('Alice has a cat');
1184             expect(position.offset).to.equal(5);
1185             expect(position.offsetAtEnd).to.equal(false, 'offset is not at end');
1186
1187             getSelection.returns({
1188                 anchorNode: text,
1189                 focusNode: text,
1190                 anchorOffset: 15,
1191                 focusOffset: 15,
1192                 isCollapsed: true
1193             });
1194
1195             expect(cursor.getPosition().offsetAtEnd).to.equal(true, 'offset at end');
1196         });
1197
1198         it('returns boundries of selection when browser selection not collapsed', function() {
1199             var c = canvas.fromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
1200                 dom = c.doc().dom(),
1201                 text = {
1202                     alice: findTextNode(dom, 'Alice '),
1203                     has: findTextNode(dom, 'has'),
1204                     cat: findTextNode(dom, ' cat')
1205                 },
1206                 cursor = c.getCursor(),
1207                 aliceElement = c.getDocumentElement(text.alice),
1208                 catElement = c.getDocumentElement(text.cat);
1209
1210
1211                 [
1212                     {focus: text.alice, focusOffset: 1, anchor: text.cat,   anchorOffset: 2, selectionAnchor: catElement},
1213                     {focus: text.cat,   focusOffset: 2, anchor: text.alice, anchorOffset: 1, selectionAnchor: aliceElement}
1214                 ].forEach(function(s, idx) {
1215                     getSelection.returns({isColapsed: false, anchorNode: s.anchor, anchorOffset: s.anchorOffset, focusNode: s.focus, focusOffset: s.focusOffset});
1216
1217                     var selectionStart = cursor.getSelectionStart(),
1218                         selectionEnd = cursor.getSelectionEnd(),
1219                         selectionAnchor = cursor.getSelectionAnchor();
1220
1221                     expect(cursor.isSelecting()).to.equal(true, 'cursor is selecting');
1222                     expect(selectionStart.element.sameNode(aliceElement)).to.equal(true, '"Alice" is the start of the selection ' + idx);
1223                     expect(selectionStart.offset).to.equal(1, '"Alice" offset ok' + idx);
1224                     expect(selectionEnd.element.sameNode(catElement)).to.equal(true, '"Cat" is the start of the selection ' + idx);
1225                     expect(selectionEnd.offset).to.equal(2, '"Cat" offset ok' + idx);
1226                     expect(selectionAnchor.element.sameNode(s.selectionAnchor)).to.equal(true, 'anchor ok');
1227                     expect(selectionAnchor.offset).to.equal(s.anchorOffset, 'anchor offset ok');
1228                 });
1229         });
1230
1231         it('recognizes when browser selection boundries lies in sibling DocumentTextElements', function() {
1232             var c = canvas.fromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
1233                 dom = c.doc().dom(),
1234                 text = {
1235                     alice: findTextNode(dom, 'Alice '),
1236                     has: findTextNode(dom, 'has'),
1237                     a: findTextNode(dom, ' a '),
1238                     big: findTextNode(dom, 'big'),
1239                     cat: findTextNode(dom, ' cat'),
1240                 },
1241                 cursor = c.getCursor();
1242
1243             expect($(text.alice).text()).to.equal('Alice ');
1244             expect($(text.has).text()).to.equal('has');
1245             expect($(text.a).text()).to.equal(' a ');
1246             expect($(text.big).text()).to.equal('big');
1247             expect($(text.cat).text()).to.equal(' cat');
1248
1249             getSelection.returns({anchorNode: text.alice, focusNode: text.a});
1250             expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "a" are children');
1251
1252             getSelection.returns({anchorNode: text.alice, focusNode: text.cat});
1253             expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "cat" are children');
1254
1255             getSelection.returns({anchorNode: text.alice, focusNode: text.has});
1256             expect(cursor.isSelectingSiblings()).to.equal(false, '"Alice" and "has" are not children');
1257
1258             getSelection.returns({anchorNode: text.has, focusNode: text.big});
1259             expect(cursor.isSelectingSiblings()).to.equal(false, '"has" and "big" are not children');
1260             
1261         })
1262
1263         describe('zero width space handling', function() {
1264             it('position range includes ZWS at the boundries of text in case when native selection api doesn\'t', function() {
1265                 var c = canvas.fromXML("<section>Alice</section>"),
1266                     dom = c.doc().dom(),
1267                     textNode = findTextNode(dom, 'Alice'),
1268                     cursor = c.getCursor();
1269
1270                 textNode.data = utils.unicode.ZWS + 'Alice';
1271                 getSelection.returns({anchorNode: textNode, anchorOffset: 1, focusNode: textNode, focusOffset: 1});
1272                 expect(cursor.getPosition().offset).to.equal(0);
1273                 expect(cursor.getPosition().offsetAtBeginning).to.equal(true, 'offset at beginning');
1274                 
1275                 textNode.data = 'Alice' + utils.unicode.ZWS;
1276                 getSelection.returns({anchorNode: textNode, anchorOffset: 5, focusNode: textNode, focusOffset: 5});
1277                 expect(cursor.getPosition().offset).to.equal(6);
1278                 expect(cursor.getPosition().offsetAtEnd).to.equal(true, 'offset at end');
1279             });
1280         });
1281     });
1282
1283     describe('Serializing document to WLXML', function() {
1284         it('keeps document intact when no changes have been made', function() {
1285             var xmlIn = '<section>Alice<div>has</div>a <span class="uri" meta-uri="http://cat.com">cat</span>!</section>',
1286                 c = canvas.fromXML(xmlIn),
1287                 xmlOut = c.toXML();
1288
1289             var parser = new DOMParser(),
1290                 input = parser.parseFromString(xmlIn, "application/xml").childNodes[0],
1291                 output = parser.parseFromString(xmlOut, "application/xml").childNodes[0];
1292             
1293             expect(input.isEqualNode(output)).to.be.true;
1294         });
1295
1296         it('keeps arbitrary node attributes intact', function() {
1297             var xmlIn = '<section a="1" xmlns:dcterms="http://purl.org/dc/terms/"></section>',
1298                 $xmlOut = $(canvas.fromXML(xmlIn).toXML());
1299
1300             expect($xmlOut.attr('a')).to.equal('1');
1301             expect($xmlOut.attr('xmlns:dcterms')).to.equal('http://purl.org/dc/terms/');
1302         });
1303
1304         it('doesn\' serialize meta attribute if its empty', function() {
1305             var c;
1306
1307             c = canvas.fromXML('<section class="uri" meta-uri="some.uri"></section>');
1308             c.doc().setWlxmlMetaAttr('uri', '');
1309             expect($(c.toXML()).attr('meta-uri')).to.equal(undefined, 'overriding attribute with zero length string');
1310
1311             c = canvas.fromXML('<section class="uri"></section>');
1312             c.doc().setWlxmlMetaAttr('uri', '');
1313             expect($(c.toXML()).attr('meta-uri')).to.equal(undefined, 'setting attribute to zero length string');
1314         });
1315
1316         describe('output xml', function() {
1317             it('keeps entities intact', function() {
1318                 var xmlIn = '<section>&lt; &gt;</section>',
1319                     c = canvas.fromXML(xmlIn),
1320                     xmlOut = c.toXML();
1321                 expect(xmlOut).to.equal(xmlIn);
1322             });
1323             it('keeps entities intact when they form html/xml', function() {
1324                 var xmlIn = '<section>&lt;abc&gt;</section>',
1325                     c = canvas.fromXML(xmlIn),
1326                     xmlOut = c.toXML();
1327                 expect(xmlOut).to.equal(xmlIn);
1328             });
1329         });
1330
1331         describe('formatting output xml', function() {
1332             /*it('keeps white spaces at the edges of input xml', function() {
1333                 var xmlIn = '  <section></section>  ',
1334                 c = canvas.fromXML(xmlIn),
1335                 xmlOut = c.toXML();
1336
1337                 expect(xmlOut.substr(4)).to.equal('   <', 'start');
1338                 expect(xmlOut.substr(-2)).to.equal('>  ', 'end');
1339             });*/
1340             it('keeps white space between XML nodes', function() {
1341                 var xmlIn = '<section>\n\n\n<div></div>\n\n\n<div></div>\n\n\n</section>',
1342                 c = canvas.fromXML(xmlIn),
1343                 xmlOut = c.toXML();
1344
1345                 var partsIn = xmlIn.split('\n\n\n'),
1346                     partsOut = xmlOut.split('\n\n\n');
1347                 
1348                 expect(partsIn).to.deep.equal(partsOut);
1349             });
1350
1351             it('keeps white space between XML nodes - inline case', function() {
1352                 var xmlIn = '<section>\n\n\n<span></span>\n\n\n<span></span>\n\n\n</section>',
1353                 c = canvas.fromXML(xmlIn),
1354                 xmlOut = c.toXML();
1355
1356                 var partsIn = xmlIn.split('\n\n\n'),
1357                     partsOut = xmlOut.split('\n\n\n');
1358                 
1359                 expect(partsIn).to.deep.equal(partsOut);
1360             });
1361
1362             it('keeps white space at the beginning of text', function() {
1363                 var xmlIn = '<section>    abc<div>some div</div>    abc</section>',
1364                     c = canvas.fromXML(xmlIn),
1365                     xmlOut = c.toXML();
1366
1367                 expect(xmlOut).to.equal(xmlIn);
1368             });
1369
1370             it('nests new children block elements', function() {
1371                 var c = canvas.fromXML('<section></section>');
1372     
1373                 c.doc().append({tag: 'header'});
1374
1375                 var xmlOut = c.toXML();
1376                 expect(xmlOut.split('\n  ')[0]).to.equal('<section>', 'nesting start ok');
1377                 expect(xmlOut.split('\n').slice(-1)[0]).to.equal('</section>', 'nesting end ok');
1378
1379             });
1380
1381             it('doesn\'t nest new children inline elements', function() {
1382                 var c = canvas.fromXML('<section></section>');
1383     
1384                 c.doc().append({tag: 'span'});
1385
1386                 var xmlOut = c.toXML();
1387                 expect(xmlOut).to.equal('<section><span></span></section>');
1388             });
1389
1390             it('keeps original white space at the end of text', function() {
1391                 
1392                 var xmlIn = '<header>    Some text ended with white space \
1393                 \
1394                 <span class="uri">Some text</span> some text\
1395             \
1396             </header>',
1397                     c = canvas.fromXML(xmlIn);
1398
1399             var xmlOut = c.toXML();
1400             console.log(xmlOut);
1401             expect(xmlOut).to.equal(xmlIn);
1402             });
1403
1404             it('keeps white space around text node', function() {
1405                 var xmlIn = '<section>\
1406                 <header>header1</header>\
1407                 Some text surrounded by white space\
1408                 <header>header2</header>\
1409             </section>',
1410                     c = canvas.fromXML(xmlIn);
1411
1412                 var xmlOut = c.toXML();
1413                 expect(xmlOut).to.equal(xmlIn);
1414             });
1415
1416             it('keeps white space around text node - last node case', function() {
1417                 var xmlIn = '<section>\
1418                 <header>header</header>\
1419                     \
1420                 Some text surrounded by white space\
1421                     \
1422             </section>',
1423                     c = canvas.fromXML(xmlIn);
1424
1425                 var xmlOut = c.toXML();
1426                 expect(xmlOut).to.equal(xmlIn);
1427             });
1428
1429             it('keeps white space after detaching text element', function() {
1430                 var xmlIn = '<section><header>header</header>\n\
1431                     \n\
1432                 text1\n\
1433                     \n\
1434             </section>',
1435                     expectedXmlOut = '<section><header>header</header>\n\
1436                     \n\
1437                 \n\
1438                     \n\
1439             </section>',
1440                     c = canvas.fromXML(xmlIn),
1441                     children = c.doc().children(),
1442                     text = children[children.length-1];
1443                 
1444                 expect(text.getText()).to.equal('text1');
1445
1446                 text.detach();
1447
1448                 var xmlOut = c.toXML();
1449                 expect(xmlOut).to.equal(expectedXmlOut);
1450             });
1451
1452         })
1453     })
1454 });
1455
1456
1457 });