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