Fixing unwrapping text element from its parent
[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('Splitting text', function() {
462                 
463                 it('splits DocumentTextElement\'s parent into two DocumentNodeElements of the same type', function() {
464                     var c = canvas.fromXML('<section><header>Some header</header></section>'),
465                         section = c.doc(),
466                         text = section.children()[0].children()[0];
467
468                     var returnedValue = text.split({offset: 5});
469                     expect(section.children().length).to.equal(2, 'section has two children');
470                     
471                     var header1 = section.children()[0];
472                     var header2 = section.children()[1];
473
474                     expect(header1.getWlxmlTag()).to.equal('header', 'first section child represents wlxml header');
475                     expect(header1.children().length).to.equal(1, 'first header has one text child');
476                     expect(header1.children()[0].getText()).to.equal('Some ', 'first header has correct content');
477                     expect(header2.getWlxmlTag()).to.equal('header', 'second section child represents wlxml header');
478                     expect(header2.children().length).to.equal(1, 'second header has one text child');
479                     expect(header2.children()[0].getText()).to.equal('header', 'second header has correct content');
480
481                     expect(returnedValue.first.sameNode(header1)).to.equal(true, 'first node returnde');
482                     expect(returnedValue.second.sameNode(header2)).to.equal(true, 'second node returned');
483                 });
484
485                 it('leaves empty copy of DocumentNodeElement if splitting at the very beginning', function() {
486                         var c = canvas.fromXML('<section><header>Some header</header></section>'),
487                         section = c.doc(),
488                         text = section.children()[0].children()[0];
489
490                         text.split({offset: 0});
491                         
492                         var header1 = section.children()[0];
493                         var header2 = section.children()[1];
494
495                         expect(header1.children().length).to.equal(0);
496                         expect(header2.children()[0].getText()).to.equal('Some header');
497                 });
498
499                 it('leaves empty copy of DocumentNodeElement if splitting at the very end', function() {
500                         var c = canvas.fromXML('<section><header>Some header</header></section>'),
501                         section = c.doc(),
502                         text = section.children()[0].children()[0];
503
504                         text.split({offset: 11});
505                         
506                         var header1 = section.children()[0];
507                         var header2 = section.children()[1];
508
509                         expect(header1.children()[0].getText()).to.equal('Some header');
510                         expect(header2.children().length).to.equal(0);
511                 });
512
513                 it('keeps DocumentTextElement\'s parent\'s children elements intact', function() {
514                     var c = canvas.fromXML('\
515                             <section>\
516                                 <header>\
517                                     A <span>fancy</span> and <span>nice</span> header\
518                                 </header>\
519                             </section>'),
520                         section = c.doc(),
521                         header = section.children()[0],
522                         textAnd = header.children()[2];
523
524                     textAnd.split({offset: 2});
525                     
526                     var sectionChildren = section.children();
527                     expect(sectionChildren.length).to.equal(2, 'Section has two children');
528                     expect(sectionChildren[0].getWlxmlTag()).to.equal('header', 'First section element is a wlxml header');
529                     expect(sectionChildren[1].getWlxmlTag()).to.equal('header', 'Second section element is a wlxml header');
530
531                     var firstHeaderChildren = sectionChildren[0].children();
532                     expect(firstHeaderChildren.length).to.equal(3, 'First header has three children');
533                     expect(firstHeaderChildren[0].getText()).to.equal('A ', 'First header starts with a text');
534                     expect(firstHeaderChildren[1].getWlxmlTag()).to.equal('span', 'First header has span in the middle');
535                     expect(firstHeaderChildren[2].getText()).to.equal(' a', 'First header ends with text');
536
537                     var secondHeaderChildren = sectionChildren[1].children();
538                     expect(secondHeaderChildren.length).to.equal(3, 'Second header has three children');
539                     expect(secondHeaderChildren[0].getText()).to.equal('nd ', 'Second header starts with text');
540                     expect(secondHeaderChildren[1].getWlxmlTag()).to.equal('span', 'Second header has span in the middle');
541                     expect(secondHeaderChildren[2].getText()).to.equal(' header', 'Second header ends with text');
542                 });
543             });
544
545             describe('wrapping', function() {
546                 it('wraps DocumentNodeElement', function() {
547                     var c = canvas.fromXML('<section><div></div></section>'),
548                         div = c.doc().children()[0];
549                     
550                     var returned = div.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
551                         parent = div.parent(),
552                         parent2 = c.doc().children()[0];
553
554                     expect(returned.sameNode(parent)).to.be.true;
555                     expect(returned.sameNode(parent2)).to.be.true;
556                     expect(returned.getWlxmlTag()).to.equal('header');
557                     expect(returned.getWlxmlClass()).to.equal('some.class');
558                 });
559                 it('wraps DocumentTextElement', function() {
560                     var c = canvas.fromXML('<section>Alice</section>'),
561                         text = c.doc().children()[0];
562                     
563                     var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
564                         parent = text.parent(),
565                         parent2 = c.doc().children()[0];
566
567                     expect(returned.sameNode(parent)).to.be.true;
568                     expect(returned.sameNode(parent2)).to.be.true;
569                     expect(returned.getWlxmlTag()).to.equal('header');
570                     expect(returned.getWlxmlClass()).to.equal('some.class');
571                 });
572                 
573                 describe('wrapping part of DocumentTextElement', function() {
574                     [{start: 5, end: 12}, {start: 12, end: 5}].forEach(function(offsets) {
575                         it('wraps in the middle ' + offsets.start + '/' + offsets.end, function() {
576                             var c = canvas.fromXML('<section>Alice has a cat</section>'),
577                                 text = c.doc().children()[0];
578                             
579                             var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class', start: offsets.start, end: offsets.end}),
580                                 children = c.doc().children();
581
582                             expect(children.length).to.equal(3);
583                             
584                             expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
585                             expect(children[0].getText()).to.equal('Alice');
586
587                             expect(children[1].sameNode(returned)).to.be.true;
588                             expect(returned.getWlxmlTag()).to.equal('header');
589                             expect(returned.getWlxmlClass()).to.equal('some.class');
590                             expect(children[1].children().length).to.equal(1);
591                             expect(children[1].children()[0].getText()).to.equal(' has a ');
592
593                             expect(children[2]).to.be.instanceOf(documentElement.DocumentTextElement);
594                             expect(children[2].getText()).to.equal('cat');
595                         });
596                     });
597
598                     it('wraps whole text inside DocumentTextElement if offsets span entire content', function() {
599                          var c = canvas.fromXML('<section>Alice has a cat</section>'),
600                              text = c.doc().children()[0];
601                          
602                          var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class', start: 0, end: 15}),
603                              children = c.doc().children();
604
605                          expect(children.length).to.equal(1);
606                          expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
607                          expect(children[0].children()[0].getText()).to.equal('Alice has a cat');
608                     });
609                 });
610
611                 it('wraps text spanning multiple sibling DocumentTextNodes', function() {
612                     var c = canvas.fromXML('<section>Alice has a <span>small</span> cat</section>'),
613                         section = c.doc(),
614                         wrapper = c.wrapText({
615                             inside: section, 
616                             _with: {tag: 'span', klass: 'some.class'},
617                             offsetStart: 6,
618                             offsetEnd: 4,
619                             textNodeIdx: [0,2]
620                         });
621
622                     expect(section.children().length).to.equal(2);
623                     expect(section.children()[0]).to.be.instanceOf(documentElement.DocumentTextElement);
624                     expect(section.children()[0].getText()).to.equal('Alice ');
625
626                     var wrapper2 = section.children()[1];
627                     expect(wrapper2.sameNode(wrapper)).to.be.true;
628
629                     var wrapperChildren = wrapper.children();
630                     expect(wrapperChildren.length).to.equal(3);
631                     expect(wrapperChildren[0].getText()).to.equal('has a ');
632
633                     expect(wrapperChildren[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
634                     expect(wrapperChildren[1].children().length).to.equal(1);
635                     expect(wrapperChildren[1].children()[0].getText()).to.equal('small');
636
637                     expect(wrapperChildren[2].getText()).to.equal(' cat');
638                 });
639
640                 it('wraps multiple sibling Elements', function() {
641                     var c = canvas.fromXML('<section>Alice<div>has</div><div>a cat</div></section>'),
642                         section = c.doc(),
643                         aliceText = section.children()[0],
644                         firstDiv = section.children()[1],
645                         lastDiv = section.children()[section.children().length -1];
646
647                     var returned = c.wrapElements({
648                             element1: aliceText,
649                             element2: lastDiv,
650                             _with: {tag: 'header'}
651                         });
652
653                     var sectionChildren = section.children(),
654                         header = sectionChildren[0],
655                         headerChildren = header.children();
656
657                     expect(sectionChildren).to.have.length(1);
658                     expect(header.sameNode(returned)).to.equal(true, 'wrapper returned');
659                     expect(headerChildren).to.have.length(3);
660                     expect(headerChildren[0].sameNode(aliceText)).to.equal(true, 'first node wrapped');
661                     expect(headerChildren[1].sameNode(firstDiv)).to.equal(true, 'second node wrapped');
662                     expect(headerChildren[2].sameNode(lastDiv)).to.equal(true, 'third node wrapped');
663                 });
664                 it('wraps multiple sibling Elements - middle case', function() {
665                     var c = canvas.fromXML('<section><div></div>div></div><div></div><div></div></section>'),
666                         section = c.doc(),
667                         div1 = section.children()[0],
668                         div2 = section.children()[1],
669                         div3 = section.children()[2],
670                         div4 = section.children()[3];
671
672                     var returned = c.wrapElements({
673                             element1: div2,
674                             element2: div3,
675                             _with: {tag: 'header'}
676                         });
677
678                     var sectionChildren = section.children(),
679                         header = sectionChildren[1],
680                         headerChildren = header.children();
681
682                     expect(sectionChildren).to.have.length(3);
683                     expect(headerChildren).to.have.length(2);
684                     expect(headerChildren[0].sameNode(div2)).to.equal(true, 'first node wrapped');
685                     expect(headerChildren[1].sameNode(div3)).to.equal(true, 'second node wrapped');
686                 });
687             });
688
689             describe('unwrapping DocumentTextElement from its parent DocumentNodeElement if it\'s its only child', function() {
690                 it('unwraps text element from its parent and stays between its old parent siblings', function() {
691                     var c = canvas.fromXML('<section><div>Alice</div><div>has</div><div>a cat</div></section>'),
692                         section = c.doc(),
693                         sectionChildren = section.children(),
694                         divAlice = sectionChildren[0],
695                         divHas = sectionChildren[1],
696                         textHas = divHas.children()[0],
697                         divCat = sectionChildren[2];
698
699                     var newTextContainer = textHas.unwrap(),
700                         sectionChildren = section.children();
701
702                     expect(sectionChildren[0].sameNode(divAlice)).to.equal(true, 'divAlice ok');
703                     expect(newTextContainer.sameNode(section)).to.equal(true, 'unwrap returns new text parent DocumentNodeElement');
704                     expect(sectionChildren[1].getText()).to.equal('has');
705                     expect(sectionChildren[2].sameNode(divCat)).to.equal(true, 'divCat ok');
706
707                 });
708                 it('unwraps and join with its old parent adjacent text elements ', function() {
709                     var c = canvas.fromXML('<section>Alice <span>has a</span> cat</section>'),
710                     section = c.doc(),
711                     text = section.children()[1].children()[0];
712
713                     var newTextContainer = text.unwrap();
714
715                     expect(section.children().length).to.equal(1, 'section has one child');
716                     expect(section.children()[0].getText()).to.equal('Alice has a cat');
717                     expect(newTextContainer.sameNode(c.doc())).to.equal(true, 'unwrap returns new text parent DocumentNodeElement');
718                 });
719             });
720         });
721
722         describe('Lists api', function() {
723             describe('creating lists', function() {
724                 it('allows creation of a list from existing sibling DocumentElements', function() {
725                     var c = canvas.fromXML('\
726                         <section>\
727                             Alice\
728                             <div>has</div>\
729                             a\
730                             <div>cat</div>\
731                         </section>'),
732                         section = c.doc(),
733                         textHas = section.children()[1],
734                         divA = section.children()[2]
735                     
736                     c.list.create({element1: textHas, element2: divA});
737
738                     expect(section.children().length).to.equal(3, 'section has three child elements');
739
740                     var child1 = section.children()[0],
741                         list = section.children()[1],
742                         child3 = section.children()[2];
743
744                     expect(child1.getText()).to.equal('Alice');
745                     expect(list.is('list')).to.equal(true, 'second child is a list');
746                     expect(list.children().length).to.equal(2, 'list contains two elements');
747                     list.children().forEach(function(child) {
748                         expect(child.getWlxmlClass()).to.equal('item', 'list childs have wlxml class of item');
749                     });
750                     expect(child3.children()[0].getText()).to.equal('cat');
751                 });
752                 
753                 it('allows creating nested list from existing sibling list items', function() {
754                     var c = canvas.fromXML('\
755                         <section>\
756                             <div class="list-items">\
757                                 <div class="item">A</div>\
758                                 <div class="item">B</div>\
759                                 <div class="item">C</div>\
760                                 <div class="item">D</div>\
761                             </div>\
762                         </section>'),
763                         outerList = c.doc().children()[0],
764                         itemB = outerList.children()[1],
765                         itemC = outerList.children()[2];
766
767
768                         c.list.create({element1: itemB, element2: itemC});
769
770                     var outerListItems = outerList.children(),
771                         innerList = outerListItems[1].children()[0],
772                         innerListItems = innerList.children();
773
774                     expect(outerListItems.length).to.equal(3, 'outer list has three items');
775                     expect(outerListItems[0].children()[0].getText()).to.equal('A', 'first outer item ok');
776                     expect(outerListItems[1].getWlxmlClass()).to.equal('item', 'inner list is wrapped by item element');
777
778                     expect(innerList.is('list')).to.equal(true, 'inner list created');
779                     expect(innerListItems.length).to.equal(2, 'inner list has two items');
780                     expect(innerListItems[0].children()[0].getText()).to.equal('B', 'first inner item ok');
781                     expect(innerListItems[1].children()[0].getText()).to.equal('C', 'second inner item ok');
782
783                     expect(outerListItems[2].children()[0].getText()).to.equal('D', 'last outer item ok');
784
785                 });
786
787             });
788
789             describe('extracting list items', function() {
790                 it('creates two lists with extracted items in the middle if extracting from the middle of the list', function() {
791                     var c = canvas.fromXML('\
792                         <section>\
793                             <div class="list.items">\
794                                 <div class="item">0</div>\
795                                 <div class="item">1</div>\
796                                 <div class="item">2</div>\
797                                 <div class="item">3</div>\
798                             </div>\
799                         </section>'),
800                         list = c.doc().children()[0],
801                         item1 = list.children()[1],
802                         item2 = list.children()[2];
803
804                     c.list.extractItems({element1: item1, element2: item2});
805
806                     var section = c.doc(),
807                         list1 = section.children()[0],
808                         oldItem1 = section.children()[1],
809                         oldItem2 = section.children()[2],
810                         list2 = section.children()[3];
811
812                     expect(section.children().length).to.equal(4, 'section contains four children');
813                     
814                     expect(list1.is('list')).to.equal(true, 'first section child is a list');
815                     expect(list1.children().length).to.equal(1, 'first list has one child');
816                     expect(list1.children()[0].children()[0].getText()).to.equal('0', 'first item of the first list is a first item of the original list');
817
818                     expect(oldItem1.children()[0].getText()).to.equal('1', 'first item got extracted');
819                     expect(oldItem1.getWlxmlClass() === undefined).to.equal(true, 'first extracted element has no wlxml class');
820
821                     expect(oldItem2.children()[0].getText()).to.equal('2', 'second item got extracted');
822                     expect(oldItem2.getWlxmlClass() === undefined).to.equal(true, 'second extracted element has no wlxml class');
823
824                     expect(list2.is('list')).to.equal(true, 'last section child is a list');
825                     expect(list2.children().length).to.equal(1, 'second list has one child');
826                     expect(list2.children()[0].children()[0].getText()).to.equal('3', 'first item of the second list is a last item of the original list');
827                 });
828
829                 it('puts extracted items above the list if starting item is the first one', function() {
830                     var c = canvas.fromXML('\
831                         <section>\
832                             <div class="list.items">\
833                                 <div class="item">0</div>\
834                                 <div class="item">1</div>\
835                                 <div class="item">2</div>\
836                             </div>\
837                         </section>'),
838                         list = c.doc().children()[0],
839                         item1 = list.children()[0],
840                         item2 = list.children()[1],
841                         item3 = list.children()[2];
842
843                     c.list.extractItems({element1: item1, element2: item2});
844
845                     var section = c.doc(),
846                         oldItem1 = section.children()[0],
847                         oldItem2 = section.children()[1],
848                         newList = section.children()[2];
849
850                     expect(section.children().length).to.equal(3, 'section has three children');
851                     expect(oldItem1.children()[0].getText()).to.equal('0', 'first item extracted');
852                     expect(oldItem2.children()[0].getText()).to.equal('1', 'second item extracted');
853                     expect(newList.is('list')).to.equal(true, 'list lies below extracted item');
854                     expect(newList.children().length).to.equal(1, 'list has now one child');
855                 });
856
857                 it('puts extracted items below the list if ending item is the last one', function() {
858                     var c = canvas.fromXML('\
859                         <section>\
860                             <div class="list.items">\
861                                 <div class="item">0</div>\
862                                 <div class="item">1</div>\
863                                 <div class="item">2</div>\
864                             </div>\
865                         </section>'),
866                         list = c.doc().children()[0],
867                         item1 = list.children()[0],
868                         item2 = list.children()[1],
869                         item3 = list.children()[2];
870
871                     c.list.extractItems({element1: item2, element2: item3});
872
873                     var section = c.doc(),
874                         oldItem1 = section.children()[1],
875                         oldItem2 = section.children()[2],
876                         newList = section.children()[0];
877
878                     expect(section.children().length).to.equal(3, 'section has three children');
879                     expect(oldItem1.children()[0].getText()).to.equal('1', 'first item extracted');
880                     expect(oldItem2.children()[0].getText()).to.equal('2', 'second item extracted');
881                     expect(newList.is('list')).to.equal(true, 'list lies above extracted item');
882                     expect(newList.children().length).to.equal(1, 'list has now one child');
883                 });
884
885                 it('removes list if all its items are extracted', function() {
886                     var c = canvas.fromXML('\
887                         <section>\
888                             <div class="list.items">\
889                                 <div class="item">some item</div>\
890                                 <div class="item">some item 2</div>\
891                             </div>\
892                         </section>'),
893                         list = c.doc().children()[0],
894                         item1 = list.children()[0],
895                         item2 = list.children()[1];
896
897                     c.list.extractItems({element1: item1, element2: item2});
898
899                     var section = c.doc(),
900                         list1 = section.children()[0],
901                         oldItem1 = section.children()[0],
902                         oldItem2 = section.children()[1];
903
904                     expect(section.children().length).to.equal(2, 'section contains two children');
905                     expect(oldItem1.children()[0].getText()).to.equal('some item');
906                     expect(oldItem2.children()[0].getText()).to.equal('some item 2');
907                 });
908
909                 it('creates two lists with extracted items in the middle if extracting from the middle of the list - nested case' , function() {
910                     var c = canvas.fromXML('\
911                         <section>\
912                             <div class="list.items">\
913                                 <div class="item">0</div>\
914                                 <div class="item">\
915                                     <div class="list.items">\
916                                         <div class="item">1.1</div>\
917                                         <div class="item">1.2</div>\
918                                         <div class="item">1.3</div>\
919                                     </div>\
920                                 </div>\
921                                 <div class="item">2</div>\
922                             </div>\
923                         </section>'),
924                         list = c.doc().children()[0],
925                         nestedList = list.children()[1].children()[0],
926                         nestedListItem = nestedList.children()[1];
927
928                     c.list.extractItems({element1: nestedListItem, element2: nestedListItem});
929
930                     var section = c.doc(),
931                         list = section.children()[0],
932                         item1 = list.children()[0],
933                         item2 = list.children()[1], //
934                         item3 = list.children()[2],
935                         item4 = list.children()[3], //
936                         item5 = list.children()[4],
937                         nestedList1 = item2.children()[0],
938                         nestedList2 = item4.children()[0];
939
940                     expect(list.children().length).to.equal(5, 'top list has five items');
941                     
942                     expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
943
944                     expect(item2.getWlxmlClass()).to.equal('item', 'first nested list is still wrapped in item element');
945                     expect(nestedList1.children().length).to.equal(1, 'first nested list is left with one child');
946                     expect(nestedList1.children()[0].children()[0].getText()).to.equal('1.1', 'first nested list item left alone');
947                     
948                     expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
949
950                     expect(item4.getWlxmlClass()).to.equal('item', 'second nested list is still wrapped in item element');
951                     expect(nestedList2.children().length).to.equal(1, 'second nested list is left with one child');
952                     expect(nestedList2.children()[0].children()[0].getText()).to.equal('1.3', 'second nested list item left alone');
953
954                     expect(item5.children()[0].getText()).to.equal('2', 'last item ok');
955                 });
956
957                 it('puts extracted items below the list if ending item is the last one - nested case' , function() {
958                     var c = canvas.fromXML('\
959                         <section>\
960                             <div class="list.items">\
961                                 <div class="item">0</div>\
962                                 <div class="item">\
963                                     <div class="list.items">\
964                                         <div class="item">1.1</div>\
965                                         <div class="item">1.2</div>\
966                                         <div class="item">1.3</div>\
967                                     </div>\
968                                 </div>\
969                                 <div class="item">2</div>\
970                             </div>\
971                         </section>'),
972                         list = c.doc().children()[0],
973                         nestedList = list.children()[1].children()[0],
974                         nestedListItem1 = nestedList.children()[1],
975                         nestedListItem2 = nestedList.children()[2];
976
977                     c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
978
979                     var section = c.doc(),
980                         list = section.children()[0],
981                         item1 = list.children()[0],
982                         item2 = list.children()[1],
983                         item3 = list.children()[2],
984                         item4 = list.children()[3],
985                         item5 = list.children()[4];
986                     nestedList = item2.children()[0];
987
988                     expect(list.children().length).to.equal(5, 'top list has five items');
989                     expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
990                     expect(item2.getWlxmlClass()).to.equal('item', 'nested list is still wrapped in item element');
991                     expect(nestedList.children().length).to.equal(1, 'nested list is left with one child');
992                     expect(nestedList.children()[0].children()[0].getText()).to.equal('1.1', 'nested list item left alone');
993                     expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
994                     expect(item4.children()[0].getText()).to.equal('1.3', 'fourth item ok');
995                     expect(item5.children()[0].getText()).to.equal('2', 'fifth item ok');
996                 });
997
998                 it('puts extracted items above the list if starting item is the first one - nested case' , function() {
999                     var c = canvas.fromXML('\
1000                         <section>\
1001                             <div class="list.items">\
1002                                 <div class="item">0</div>\
1003                                 <div class="item">\
1004                                     <div class="list.items">\
1005                                         <div class="item">1.1</div>\
1006                                         <div class="item">1.2</div>\
1007                                         <div class="item">1.3</div>\
1008                                     </div>\
1009                                 </div>\
1010                                 <div class="item">2</div>\
1011                             </div>\
1012                         </section>'),
1013                         list = c.doc().children()[0],
1014                         nestedList = list.children()[1].children()[0],
1015                         nestedListItem1 = nestedList.children()[0],
1016                         nestedListItem2 = nestedList.children()[1];
1017
1018                     c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
1019
1020                     var section = c.doc(),
1021                         list = section.children()[0],
1022                         item1 = list.children()[0],
1023                         item2 = list.children()[1],
1024                         item3 = list.children()[2],
1025                         item4 = list.children()[3],
1026                         item5 = list.children()[4];
1027                     nestedList = item4.children()[0];
1028
1029                     expect(list.children().length).to.equal(5, 'top list has five items');
1030                     expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
1031                     expect(item2.children()[0].getText()).to.equal('1.1', 'second item ok');
1032                     expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
1033                     
1034                     expect(item4.getWlxmlClass()).to.equal('item', 'nested list is still wrapped in item element');
1035                     expect(nestedList.children().length).to.equal(1, 'nested list is left with one child');
1036                     expect(nestedList.children()[0].children()[0].getText()).to.equal('1.3', 'nested list item left alone');
1037                     expect(item5.children()[0].getText()).to.equal('2', 'fifth item ok');
1038                 });
1039
1040                 it('removes list if all its items are extracted - nested case', function() {
1041                     var c = canvas.fromXML('\
1042                         <section>\
1043                             <div class="list.items">\
1044                                 <div class="item">0</div>\
1045                                 <div class="item">\
1046                                     <div class="list.items">\
1047                                         <div class="item">1.1</div>\
1048                                         <div class="item">1.2</div>\
1049                                     </div>\
1050                                 </div>\
1051                                 <div class="item">2</div>\
1052                             </div>\
1053                         </section>'),
1054                         list = c.doc().children()[0],
1055                         nestedList = list.children()[1].children()[0],
1056                         nestedListItem1 = nestedList.children()[0],
1057                         nestedListItem2 = nestedList.children()[1];
1058
1059                     c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
1060
1061                     var section = c.doc(),
1062                         list = section.children()[0],
1063                         item1 = list.children()[0],
1064                         item2 = list.children()[1],
1065                         item3 = list.children()[2],
1066                         item4 = list.children()[3];
1067
1068                     expect(list.children().length).to.equal(4, 'top list has four items');
1069                     expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
1070                     expect(item2.children()[0].getText()).to.equal('1.1', 'second item ok');
1071                     expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
1072                     expect(item4.children()[0].getText()).to.equal('2', 'fourth item ok');
1073                 });
1074
1075                 it('extracts items out of outer most list when merge flag is set to false', function() {
1076                     var c = canvas.fromXML('\
1077                         <section>\
1078                             <div class="list.items">\
1079                                 <div class="item">0</div>\
1080                                 <div class="item">\
1081                                     <div class="list.items">\
1082                                         <div class="item">1.1</div>\
1083                                         <div class="item">1.2</div>\
1084                                     </div>\
1085                                 </div>\
1086                                 <div class="item">2</div>\
1087                             </div>\
1088                         </section>'),
1089                         section = c.doc(),
1090                         list = section.children()[0],
1091                         nestedList = list.children()[1].children()[0],
1092                         nestedListItem = nestedList.children()[0];
1093
1094                     var test = c.list.extractItems({element1: nestedListItem, element2: nestedListItem, merge: false});
1095
1096                     expect(test).to.equal(true, 'extraction status ok');
1097
1098                     var sectionChildren = section.children(),
1099                         extractedItem = sectionChildren[1];
1100
1101                     expect(sectionChildren.length).to.equal(3, 'section has three children');
1102                     expect(sectionChildren[0].is('list')).to.equal(true, 'first child is a list');
1103
1104                     expect(extractedItem.getWlxmlTag()).to.equal('div', 'extracted item is a wlxml div');
1105                     expect(extractedItem.getWlxmlClass()).to.equal(undefined, 'extracted item has no wlxml class');
1106                     expect(extractedItem.children()[0].getText()).to.equal('1.1', 'extracted item ok');
1107                     expect(sectionChildren[2].is('list')).to.equal(true, 'second child is a list');
1108                 });
1109             });
1110         });
1111
1112     });
1113
1114     describe('Cursor', function() {
1115
1116         var getSelection;
1117
1118         var findTextNode = function(inside, text) {
1119             var nodes = inside.find(':not(iframe)').addBack().contents().filter(function() {
1120                 return this.nodeType === Node.TEXT_NODE && this.data === text;
1121             });
1122             if(nodes.length)
1123                 return nodes[0];
1124             return null;
1125         }
1126
1127         beforeEach(function() {
1128             getSelection = sinon.stub(window, 'getSelection');
1129         });
1130
1131         afterEach(function() {
1132             getSelection.restore();
1133         });
1134
1135         it('returns position when browser selection collapsed', function() {
1136             var c = canvas.fromXML('<section>Alice has a cat</section>'),
1137                 dom = c.doc().dom(),
1138                 text = findTextNode(dom, 'Alice has a cat');
1139
1140             expect(text.nodeType).to.equal(Node.TEXT_NODE, 'correct node selected');
1141             expect($(text).text()).to.equal('Alice has a cat');
1142
1143             getSelection.returns({
1144                 anchorNode: text,
1145                 focusNode: text,
1146                 anchorOffset: 5,
1147                 focusOffset: 5,
1148                 isCollapsed: true
1149             });
1150             var cursor = c.getCursor(),
1151                 position = cursor.getPosition();
1152
1153             expect(cursor.isSelecting()).to.equal(false, 'cursor is not selecting anything');
1154             expect(position.element.getText()).to.equal('Alice has a cat');
1155             expect(position.offset).to.equal(5);
1156             expect(position.offsetAtEnd).to.equal(false, 'offset is not at end');
1157
1158             getSelection.returns({
1159                 anchorNode: text,
1160                 focusNode: text,
1161                 anchorOffset: 15,
1162                 focusOffset: 15,
1163                 isCollapsed: true
1164             });
1165
1166             expect(cursor.getPosition().offsetAtEnd).to.equal(true, 'offset at end');
1167         });
1168
1169         it('returns boundries of selection when browser selection not collapsed', function() {
1170             var c = canvas.fromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
1171                 dom = c.doc().dom(),
1172                 text = {
1173                     alice: findTextNode(dom, 'Alice '),
1174                     has: findTextNode(dom, 'has'),
1175                     cat: findTextNode(dom, ' cat')
1176                 },
1177                 cursor = c.getCursor(),
1178                 aliceElement = c.getDocumentElement(text.alice),
1179                 catElement = c.getDocumentElement(text.cat);
1180
1181
1182                 [
1183                     {focus: text.alice, focusOffset: 1, anchor: text.cat,   anchorOffset: 2, selectionAnchor: catElement},
1184                     {focus: text.cat,   focusOffset: 2, anchor: text.alice, anchorOffset: 1, selectionAnchor: aliceElement}
1185                 ].forEach(function(s, idx) {
1186                     getSelection.returns({isColapsed: false, anchorNode: s.anchor, anchorOffset: s.anchorOffset, focusNode: s.focus, focusOffset: s.focusOffset});
1187
1188                     var selectionStart = cursor.getSelectionStart(),
1189                         selectionEnd = cursor.getSelectionEnd(),
1190                         selectionAnchor = cursor.getSelectionAnchor();
1191
1192                     expect(cursor.isSelecting()).to.equal(true, 'cursor is selecting');
1193                     expect(selectionStart.element.sameNode(aliceElement)).to.equal(true, '"Alice" is the start of the selection ' + idx);
1194                     expect(selectionStart.offset).to.equal(1, '"Alice" offset ok' + idx);
1195                     expect(selectionEnd.element.sameNode(catElement)).to.equal(true, '"Cat" is the start of the selection ' + idx);
1196                     expect(selectionEnd.offset).to.equal(2, '"Cat" offset ok' + idx);
1197                     expect(selectionAnchor.element.sameNode(s.selectionAnchor)).to.equal(true, 'anchor ok');
1198                     expect(selectionAnchor.offset).to.equal(s.anchorOffset, 'anchor offset ok');
1199                 });
1200         });
1201
1202         it('recognizes when browser selection boundries lies in sibling DocumentTextElements', function() {
1203             var c = canvas.fromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
1204                 dom = c.doc().dom(),
1205                 text = {
1206                     alice: findTextNode(dom, 'Alice '),
1207                     has: findTextNode(dom, 'has'),
1208                     a: findTextNode(dom, ' a '),
1209                     big: findTextNode(dom, 'big'),
1210                     cat: findTextNode(dom, ' cat'),
1211                 },
1212                 cursor = c.getCursor();
1213
1214             expect($(text.alice).text()).to.equal('Alice ');
1215             expect($(text.has).text()).to.equal('has');
1216             expect($(text.a).text()).to.equal(' a ');
1217             expect($(text.big).text()).to.equal('big');
1218             expect($(text.cat).text()).to.equal(' cat');
1219
1220             getSelection.returns({anchorNode: text.alice, focusNode: text.a});
1221             expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "a" are children');
1222
1223             getSelection.returns({anchorNode: text.alice, focusNode: text.cat});
1224             expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "cat" are children');
1225
1226             getSelection.returns({anchorNode: text.alice, focusNode: text.has});
1227             expect(cursor.isSelectingSiblings()).to.equal(false, '"Alice" and "has" are not children');
1228
1229             getSelection.returns({anchorNode: text.has, focusNode: text.big});
1230             expect(cursor.isSelectingSiblings()).to.equal(false, '"has" and "big" are not children');
1231             
1232         })
1233
1234         describe('zero width space handling', function() {
1235             it('position range includes ZWS at the boundries of text in case when native selection api doesn\'t', function() {
1236                 var c = canvas.fromXML("<section>Alice</section>"),
1237                     dom = c.doc().dom(),
1238                     textNode = findTextNode(dom, 'Alice'),
1239                     cursor = c.getCursor();
1240
1241                 textNode.data = utils.unicode.ZWS + 'Alice';
1242                 getSelection.returns({anchorNode: textNode, anchorOffset: 1, focusNode: textNode, focusOffset: 1});
1243                 expect(cursor.getPosition().offset).to.equal(0);
1244                 expect(cursor.getPosition().offsetAtBeginning).to.equal(true, 'offset at beginning');
1245                 
1246                 textNode.data = 'Alice' + utils.unicode.ZWS;
1247                 getSelection.returns({anchorNode: textNode, anchorOffset: 5, focusNode: textNode, focusOffset: 5});
1248                 expect(cursor.getPosition().offset).to.equal(6);
1249                 expect(cursor.getPosition().offsetAtEnd).to.equal(true, 'offset at end');
1250             });
1251         });
1252     });
1253
1254     describe('Serializing document to WLXML', function() {
1255         it('keeps document intact when no changes have been made', function() {
1256             var xmlIn = '<section>Alice<div>has</div>a <span class="uri" meta-uri="http://cat.com">cat</span>!</section>',
1257                 c = canvas.fromXML(xmlIn),
1258                 xmlOut = c.toXML();
1259
1260             var parser = new DOMParser(),
1261                 input = parser.parseFromString(xmlIn, "application/xml").childNodes[0],
1262                 output = parser.parseFromString(xmlOut, "application/xml").childNodes[0];
1263             
1264             expect(input.isEqualNode(output)).to.be.true;
1265         });
1266
1267         it('keeps arbitrary node attributes intact', function() {
1268             var xmlIn = '<section a="1" xmlns:dcterms="http://purl.org/dc/terms/"></section>',
1269                 $xmlOut = $(canvas.fromXML(xmlIn).toXML());
1270
1271             expect($xmlOut.attr('a')).to.equal('1');
1272             expect($xmlOut.attr('xmlns:dcterms')).to.equal('http://purl.org/dc/terms/');
1273         });
1274
1275         it('doesn\' serialize meta attribute if its empty', function() {
1276             var c;
1277
1278             c = canvas.fromXML('<section class="uri" meta-uri="some.uri"></section>');
1279             c.doc().setWlxmlMetaAttr('uri', '');
1280             expect($(c.toXML()).attr('meta-uri')).to.equal(undefined, 'overriding attribute with zero length string');
1281
1282             c = canvas.fromXML('<section class="uri"></section>');
1283             c.doc().setWlxmlMetaAttr('uri', '');
1284             expect($(c.toXML()).attr('meta-uri')).to.equal(undefined, 'setting attribute to zero length string');
1285         });
1286
1287         describe('output xml', function() {
1288             it('keeps entities intact', function() {
1289                 var xmlIn = '<section>&lt; &gt;</section>',
1290                     c = canvas.fromXML(xmlIn),
1291                     xmlOut = c.toXML();
1292                 expect(xmlOut).to.equal(xmlIn);
1293             });
1294             it('keeps entities intact when they form html/xml', function() {
1295                 var xmlIn = '<section>&lt;abc&gt;</section>',
1296                     c = canvas.fromXML(xmlIn),
1297                     xmlOut = c.toXML();
1298                 expect(xmlOut).to.equal(xmlIn);
1299             });
1300         });
1301
1302         describe('formatting output xml', function() {
1303             /*it('keeps white spaces at the edges of input xml', function() {
1304                 var xmlIn = '  <section></section>  ',
1305                 c = canvas.fromXML(xmlIn),
1306                 xmlOut = c.toXML();
1307
1308                 expect(xmlOut.substr(4)).to.equal('   <', 'start');
1309                 expect(xmlOut.substr(-2)).to.equal('>  ', 'end');
1310             });*/
1311             it('keeps white space between XML nodes', function() {
1312                 var xmlIn = '<section>\n\n\n<div></div>\n\n\n<div></div>\n\n\n</section>',
1313                 c = canvas.fromXML(xmlIn),
1314                 xmlOut = c.toXML();
1315
1316                 var partsIn = xmlIn.split('\n\n\n'),
1317                     partsOut = xmlOut.split('\n\n\n');
1318                 
1319                 expect(partsIn).to.deep.equal(partsOut);
1320             });
1321
1322             it('keeps white space between XML nodes - inline case', function() {
1323                 var xmlIn = '<section>\n\n\n<span></span>\n\n\n<span></span>\n\n\n</section>',
1324                 c = canvas.fromXML(xmlIn),
1325                 xmlOut = c.toXML();
1326
1327                 var partsIn = xmlIn.split('\n\n\n'),
1328                     partsOut = xmlOut.split('\n\n\n');
1329                 
1330                 expect(partsIn).to.deep.equal(partsOut);
1331             });
1332
1333             it('keeps white space at the beginning of text', function() {
1334                 var xmlIn = '<section>    abc<div>some div</div>    abc</section>',
1335                     c = canvas.fromXML(xmlIn),
1336                     xmlOut = c.toXML();
1337
1338                 expect(xmlOut).to.equal(xmlIn);
1339             });
1340
1341             it('nests new children block elements', function() {
1342                 var c = canvas.fromXML('<section></section>');
1343     
1344                 c.doc().append({tag: 'header'});
1345
1346                 var xmlOut = c.toXML();
1347                 expect(xmlOut.split('\n  ')[0]).to.equal('<section>', 'nesting start ok');
1348                 expect(xmlOut.split('\n').slice(-1)[0]).to.equal('</section>', 'nesting end ok');
1349
1350             });
1351
1352             it('doesn\'t nest new children inline elements', function() {
1353                 var c = canvas.fromXML('<section></section>');
1354     
1355                 c.doc().append({tag: 'span'});
1356
1357                 var xmlOut = c.toXML();
1358                 expect(xmlOut).to.equal('<section><span></span></section>');
1359             });
1360
1361             it('keeps original white space at the end of text', function() {
1362                 
1363                 var xmlIn = '<header>    Some text ended with white space \
1364                 \
1365                 <span class="uri">Some text</span> some text\
1366             \
1367             </header>',
1368                     c = canvas.fromXML(xmlIn);
1369
1370             var xmlOut = c.toXML();
1371             console.log(xmlOut);
1372             expect(xmlOut).to.equal(xmlIn);
1373             });
1374
1375             it('keeps white space around text node', function() {
1376                 var xmlIn = '<section>\
1377                 <header>header1</header>\
1378                 Some text surrounded by white space\
1379                 <header>header2</header>\
1380             </section>',
1381                     c = canvas.fromXML(xmlIn);
1382
1383                 var xmlOut = c.toXML();
1384                 expect(xmlOut).to.equal(xmlIn);
1385             });
1386
1387             it('keeps white space around text node - last node case', function() {
1388                 var xmlIn = '<section>\
1389                 <header>header</header>\
1390                     \
1391                 Some text surrounded by white space\
1392                     \
1393             </section>',
1394                     c = canvas.fromXML(xmlIn);
1395
1396                 var xmlOut = c.toXML();
1397                 expect(xmlOut).to.equal(xmlIn);
1398             });
1399
1400             it('keeps white space after detaching text element', function() {
1401                 var xmlIn = '<section><header>header</header>\n\
1402                     \n\
1403                 text1\n\
1404                     \n\
1405             </section>',
1406                     expectedXmlOut = '<section><header>header</header>\n\
1407                     \n\
1408                 \n\
1409                     \n\
1410             </section>',
1411                     c = canvas.fromXML(xmlIn),
1412                     children = c.doc().children(),
1413                     text = children[children.length-1];
1414                 
1415                 expect(text.getText()).to.equal('text1');
1416
1417                 text.detach();
1418
1419                 var xmlOut = c.toXML();
1420                 expect(xmlOut).to.equal(expectedXmlOut);
1421             });
1422
1423         })
1424     })
1425 });
1426
1427
1428 });