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