51f4f0af31f26c65b5f782864ca6db44f6e66f99
[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 ], function(chai, sinon, canvas, documentElement) {
7     
8 'use strict';
9
10 var expect = chai.expect;
11
12
13 describe('Canvas', function() {
14
15     describe('Internal HTML representation of a sample document', function() {
16         it('works', function() {
17             var c = canvas.fromXML('\
18                 <section>\
19                     This is some text without its own wrapping tag.\
20                     <div class="p.subclass">\
21                         This is a paragraph.\
22                     </div>\
23                     <div>\
24                         This is text in a div <span>with some inline text</span>.\
25                     </div>\
26                     This is some text without its own wrapping tag.\
27                 </section>\
28             ');
29             var expected = '<div wlxml-tag="section">'
30                             + '<div wlxml-text>This is some text without its own wrapping tag.</div>'
31                             + '<div wlxml-tag="div" wlxml-class="p-subclass">'
32                             +   '<div wlxml-text>This is a paragraph.</div>'
33                             + '</div>'
34                             + '<div wlxml-tag="div">'
35                             +   '<div wlxml-text>This is text in a div </div>'
36                             +   '<div wlxml-tag="span">'
37                             +       '<div wlxml-text>with some inline text</div>'
38                             +   '</div>'
39                             +   '<div wlxml-text>.</div>'
40                             + '</div>'
41                             + '<div wlxml-text>This is some text without its own wrapping tag.</div>'
42                             + '</div>';
43             expect(c.doc().dom()[0].isEqualNode($(expected)[0])).to.be.true;
44         });
45     });
46
47     describe('Internal HTML representation of a DocumentNodeElement', function() {
48         it('is always a div tag', function() {
49             ['section', 'header', 'span', 'aside', 'figure'].forEach(function(tagName) {
50                 var dom = canvas.fromXML('<' + tagName + '></' + tagName + '>').doc().dom();
51                 expect(dom.prop('tagName')).to.equal('DIV', tagName + ' is represented as div');
52             });
53         });
54         it('has wlxml tag put into wlxml-tag attribute', function() {
55             var dom = canvas.fromXML('<section></section>').doc().dom();
56             expect(dom.attr('wlxml-tag')).to.equal('section');
57         });
58         it('has wlxml class put into wlxml-class, dots replaced with dashes', function() {
59             var dom = canvas.fromXML('<section class="some.class"></section>').doc().dom();
60             expect(dom.attr('wlxml-class')).to.equal('some-class');
61         });
62     });
63
64     describe('Internal HTML representation of a DocumentTextElement', function() {
65         it('is text node wrapped in a div with wlxml-text attribute set', function() {
66             var dom = canvas.fromXML('<section>Alice</section>').doc().children()[0].dom();
67             expect(dom.prop('tagName')).to.equal('DIV');
68             expect(dom.attr('wlxml-text')).to.equal('');
69             expect(dom.contents().length).to.equal(1);
70             expect(dom.contents()[0].nodeType).to.equal(Node.TEXT_NODE);
71             expect($(dom.contents()[0]).text()).to.equal('Alice');
72         });
73     });
74
75     describe('basic properties', function() {
76         it('renders empty document when canvas created from empty XML', function() {
77             var c = canvas.fromXML('');
78             expect(c.doc()).to.equal(null);
79         });
80
81         it('gives access to its document root node', function() {
82             var c = canvas.fromXML('<section></section>');
83             expect(c.doc().getWlxmlTag()).to.equal('section');
84         });
85
86         describe('root element', function() {
87             it('has no parent', function() {
88                 var c = canvas.fromXML('<section></section>');
89                 expect(c.doc().parent()).to.be.null;
90             });
91         });
92
93         describe('DocumentTextElement', function() {
94             it('can have its content set', function() {
95                 var c = canvas.fromXML('<section>Alice</section>'),
96                     root = c.doc(),
97                     text = root.children()[0];
98                 
99                 text.setText('a cat');
100                 expect(root.children()[0].getText()).to.equal('a cat');
101             });
102         });
103
104         describe('DocumentNodeElement', function() {
105             it('knows index of its child', function() {
106                 var c = canvas.fromXML('<section><div></div><header></header><span></span></section>'),
107                     root = c.doc(),
108                     child = root.children()[1];
109                 expect(root.childIndex(child)).to.equal(1);
110             });
111
112             it('knows WLXML tag it renders', function(){
113                 var c = canvas.fromXML('<section></section>'),
114                     section = c.doc();
115                 expect(section.getWlxmlTag()).to.equal('section', 'initial tag is section');
116                 section.setWlxmlTag('header');
117                 expect(section.getWlxmlTag()).to.equal('header', 'tag is changed to header');
118             });
119
120             it('knows WLXML class of a WLXML tag it renders', function(){
121                 var c = canvas.fromXML('<section class="some.class.A"></section>'),
122                     section = c.doc();
123                 expect(section.getWlxmlClass()).to.equal('some.class.A');
124                 section.setWlxmlClass('some.class.B');
125                 expect(section.getWlxmlClass()).to.equal('some.class.B');
126                 section.setWlxmlClass(null);
127                 expect(section.getWlxmlClass()).to.be.undefined;
128             });
129
130
131
132             describe('element has meta attributes', function() {
133                 it('can change its meta attributes', function() {
134                     var c = canvas.fromXML('<section><span class="uri" meta-uri="someuri"></span></section>'),
135                     span = c.doc().children()[0];
136                     
137                     expect(span.getWlxmlMetaAttr('uri')).to.equal('someuri');
138                     span.setWlxmlMetaAttr('uri', 'otheruri');
139                     expect(span.getWlxmlMetaAttr('uri')).to.equal('otheruri');
140                 });
141
142                 it('changes its meta attributes with class change', function() {
143                     var c = canvas.fromXML('<section><span class="uri" meta-uri="someuri"></span></section>'),
144                     span = c.doc().children()[0];
145                     
146                     expect(span.getWlxmlMetaAttr('uri')).to.equal('someuri');
147                     span.setWlxmlClass('author');
148                     expect(span.getWlxmlMetaAttr('uri')).to.be.undefined;
149                 });
150
151                 it('keeps meta attribute value on class change if a new class has this attribute', function() {
152                     var c = canvas.fromXML('<section><span class="uri" meta-uri="someuri"></span></section>'),
153                     span = c.doc().children()[0];
154                     span.setWlxmlClass('uri.some.subclass');
155                     expect(span.getWlxmlMetaAttr('uri')).to.equal('someuri');
156                 });
157             });
158         });
159
160         it('returns DocumentNodeElement instance from HTMLElement', function() {
161             var c = canvas.fromXML('<section></section>'),
162                 htmlElement = c.doc().dom().get(0),
163                 element = c.getDocumentElement(htmlElement);
164             expect(element).to.be.instanceOf(documentElement.DocumentNodeElement);
165             expect(element.sameNode(c.doc()));
166         });
167         
168         it('returns DocumentTextElement instance from Text Node', function() {
169             var c = canvas.fromXML('<section>Alice</section>'),
170                 aliceElement = c.doc().children()[0],
171                 textNode = aliceElement.dom().contents()[0],
172                 element = c.getDocumentElement(textNode);
173
174             expect(textNode.nodeType).to.equal(Node.TEXT_NODE, 'text node selected');
175             expect($(textNode).text()).to.equal('Alice');
176
177             expect(element).to.be.instanceOf(documentElement.DocumentTextElement);
178             expect(element.sameNode(c.doc().children()[0]));
179         });
180     });
181
182
183
184     describe('document representation api', function() {
185         describe('document root element', function() {
186             var c = canvas.fromXML('<section></section>');
187             it('exists', function() {
188                 expect(c.doc()).to.be.instanceOf(documentElement.DocumentElement);
189             });
190             it('is of type DocumentNodeElement', function() {
191                 expect(c.doc()).to.be.instanceOf(documentElement.DocumentNodeElement);
192             });
193         });
194
195         describe('DocumentElements comparison', function() {
196             it('reports dwo DocumentElements to be the same when they represent the same wlxml document element', function() {
197                 var c = canvas.fromXML('<section><div></div><div></div></section>'),
198                     first_div1 = c.doc().children()[0],
199                     first_div2 = c.doc().children()[0],
200                     second_div = c.doc().children()[1];
201                 expect(first_div1.sameNode(first_div1)).to.be.true;
202                 expect(first_div1.sameNode(first_div2)).to.be.true;
203                 expect(first_div1.sameNode(second_div)).to.be.false;
204             });
205         });
206
207         describe('traversing', function() {
208             it('reports element nodes', function() {
209                 var c = canvas.fromXML('<section><div></div></section>'),
210                     children = c.doc().children();
211                 expect(children.length).to.equal(1);
212                 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
213
214                 c = canvas.fromXML('<section><div></div><div></div></section>'),
215                     children = c.doc().children();
216                 expect(children.length).to.equal(2);
217                 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
218                 expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
219             });
220             
221             it('reports text nodes', function() {
222                 var c = canvas.fromXML('<section>Alice</section>'),
223                     children = c.doc().children();
224                 expect(children.length).to.equal(1);
225                 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
226             });
227
228             describe('accessing parents', function() {
229                 it('returns DocumentNodeElement representing parent in wlxml document as DocumentNodeElement parent', function() {
230                     var c = canvas.fromXML('<section><div></div></section>'),
231                         div = c.doc().children()[0];
232                     expect(div.parent().sameNode(c.doc())).to.be.true;
233                 });
234                 it('returns DocumentNodeElement representing parent in wlxml document as DocumentTextElement parent', function() {
235                     var c = canvas.fromXML('<section>Alice</section>'),
236                         text = c.doc().children()[0];
237                     expect(text.parent().sameNode(c.doc())).to.be.true;
238                 });
239             });
240
241             describe('free text handling', function() {
242                     it('sees free text', function() {
243                         var c = canvas.fromXML('<section>Alice <span>has</span> a cat</section>'),
244                             children = c.doc().children();
245                         expect(children.length).to.equal(3);
246                         expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
247                         expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
248                         expect(children[2]).to.be.instanceOf(documentElement.DocumentTextElement);
249                     });
250             });
251             
252             describe('white characters handling', function() {
253                 it('says empty element node has no children', function() {
254                     var c = canvas.fromXML('<section></section>');
255                     expect(c.doc().children().length).to.equal(0);
256                 });
257                 it('says element node with one space has one DocumentTextElement', function() {
258                     var c = canvas.fromXML('<section> </section>');
259                     expect(c.doc().children().length).to.equal(1);
260                     expect(c.doc().children()[0]).to.be.instanceOf(documentElement.DocumentTextElement);
261                     expect(c.doc().children()[0].getText()).to.equal(' ');
262                 });
263                 it('ignores white space surrounding block elements', function() {
264                     var c = canvas.fromXML('<section> <div></div> </section>');
265                     var children = c.doc().children();
266                     expect(children.length).to.equal(1);
267                     expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
268                 });
269                 it('ignores white space between block elements', function() {
270                     var c = canvas.fromXML('<section><div></div> <div></div></section>');
271                     var children = c.doc().children();
272                     expect(children.length === 2);
273                     [0,1].forEach(function(idx) {
274                         expect(children[idx]).to.be.instanceOf(documentElement.DocumentNodeElement);
275                     });
276                 });
277
278                 it('trims white space from the beginning and the end of the block elements', function() {
279                     var c = canvas.fromXML('<section> Alice <span>has</span> a cat </section>');
280                     expect(c.doc().children()[0].getText()).to.equal('Alice ');
281                     expect(c.doc().children()[2].getText()).to.equal(' a cat');
282                 });
283
284                 it('normalizes string of white characters to one space at the inline element boundries', function() {
285                     var c = canvas.fromXML('<span>   Alice has a cat   </span>');
286                     expect(c.doc().children()[0].getText()).to.equal(' Alice has a cat ');
287                 });
288
289                 it('normalizes string of white characters to one space before inline element', function() {
290                     var c = canvas.fromXML('<div>Alice has  <span>a cat</span></div>');
291                     expect(c.doc().children()[0].getText()).to.equal('Alice has ');
292                 });
293                 
294                 it('normalizes string of white characters to one space after inline element', function() {
295                     var c = canvas.fromXML('<div>Alice has <span>a</span>  cat</div>');
296                     expect(c.doc().children()[2].getText()).to.equal(' cat');
297                 });
298             });
299
300             describe('getting vertically first text element', function() {
301                 it('returns the first child if it\'s text element, ignores metadata', function() {
302                     var c = canvas.fromXML('<section><metadata><dc:author>author</dc:author></metadata>Alice<div>has</div>a cat</section>'),
303                         first = c.doc().getVerticallyFirstTextElement();
304
305                     expect(first.sameNode(c.doc().children()[1])).to.be.true;
306                 });
307
308                 it('looks recursively inside node elements if they precede text element', function() {
309                     var c = canvas.fromXML('\
310                             <section>\
311                                 <div>\
312                                     <div>\
313                                         Alice\
314                                     </div>\
315                                 </div>\
316                                 Some text\
317                             </section>'),
318                         textAlice = c.doc().children()[0].children()[0].children()[0],
319                         first = c.doc().getVerticallyFirstTextElement();
320
321                     expect(textAlice).to.be.instanceOf(documentElement.DocumentTextElement);
322                     expect(first.sameNode(textAlice)).to.be.true;
323                 });
324             });
325         });
326
327         describe('manipulation api', function() {
328
329             describe('Basic Element inserting', function() {
330                 it('can put new NodeElement at the end', function() {
331                     var c = canvas.fromXML('<section></section>'),
332                         appended = c.doc().append({tag: 'header', klass: 'some.class'}),
333                         children = c.doc().children();
334
335                     expect(children.length).to.equal(1);
336                     expect(children[0].sameNode(appended)).to.be.true;
337                 });
338
339                 it('can put new TextElement at the end', function() {
340                     var c = canvas.fromXML('<section></section>'),
341                         appended = c.doc().append({text: 'Alice'}),
342                         children = c.doc().children();
343
344                     expect(children.length).to.equal(1);
345                     expect(children[0].sameNode(appended)).to.be.true;
346                     expect(children[0].getText()).to.equal('Alice');
347                 });
348
349                 it('can put new NodeElement after another NodeElement', function() {
350                     var c = canvas.fromXML('<section><div></div></section>'),
351                         div = c.doc().children()[0],
352                         added = div.after({tag: 'header', klass: 'some.class'}),
353                         children = c.doc().children();
354                     expect(children.length).to.equal(2);
355                     expect(children[1].sameNode(added)).to.be.true;
356                 });
357
358                 it('can put new Nodeelement before another element', function() {
359                     var c = canvas.fromXML('<section><div></div></section>'),
360                         div = c.doc().children()[0],
361                         added = div.before({tag: 'header', klass: 'some.class'}),
362                         children = c.doc().children();
363                     expect(children.length).to.equal(2);
364                     expect(children[0].sameNode(added)).to.be.true;
365                 });
366
367                 it('can put new DocumentNodeElement after DocumentTextElement', function() {
368                     var c = canvas.fromXML('<section>Alice</section>'),
369                         text = c.doc().children()[0],
370                         added = text.after({tag: 'p'}),
371                         children = c.doc().children();
372
373                     expect(children.length).to.equal(2);
374                     expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
375                     expect(children[0].getText()).to.equal('Alice');
376                     expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
377                     expect(children[1].sameNode(added)).to.be.true;
378                 });
379                 it('can put new DocumentNodeElement before DocumentTextElement', function() {
380                     var c = canvas.fromXML('<section>Alice</section>'),
381                         text = c.doc().children()[0],
382                         added = text.before({tag: 'p'}),
383                         children = c.doc().children();
384
385                     expect(children.length).to.equal(2);
386                     expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
387                     expect(children[0].sameNode(added)).to.be.true;
388                     expect(children[1]).to.be.instanceOf(documentElement.DocumentTextElement);
389                     expect(children[1].getText()).to.equal('Alice');
390                 });
391             });
392
393             describe('Splitting text', function() {
394                 
395                 it('splits DocumentTextElement\'s parent into two DocumentNodeElements of the same type', function() {
396                     var c = canvas.fromXML('<section><header>Some header</header></section>'),
397                         section = c.doc(),
398                         text = section.children()[0].children()[0];
399
400                     var returnedValue = text.split({offset: 5});
401                     expect(section.children().length).to.equal(2, 'section has two children');
402                     
403                     var header1 = section.children()[0];
404                     var header2 = section.children()[1];
405
406                     expect(header1.getWlxmlTag()).to.equal('header', 'first section child represents wlxml header');
407                     expect(header1.children().length).to.equal(1, 'first header has one text child');
408                     expect(header1.children()[0].getText()).to.equal('Some ', 'first header has correct content');
409                     expect(header2.getWlxmlTag()).to.equal('header', 'second section child represents wlxml header');
410                     expect(header2.children().length).to.equal(1, 'second header has one text child');
411                     expect(header2.children()[0].getText()).to.equal('header', 'second header has correct content');
412
413                     expect(returnedValue.first.sameNode(header1)).to.equal(true, 'first node returnde');
414                     expect(returnedValue.second.sameNode(header2)).to.equal(true, 'second node returned');
415                 });
416
417                 it('leaves empty copy of DocumentNodeElement if splitting at the very beginning', function() {
418                         var c = canvas.fromXML('<section><header>Some header</header></section>'),
419                         section = c.doc(),
420                         text = section.children()[0].children()[0];
421
422                         text.split({offset: 0});
423                         
424                         var header1 = section.children()[0];
425                         var header2 = section.children()[1];
426
427                         expect(header1.children().length).to.equal(0);
428                         expect(header2.children()[0].getText()).to.equal('Some header');
429                 });
430
431                 it('leaves empty copy of DocumentNodeElement if splitting at the very end', function() {
432                         var c = canvas.fromXML('<section><header>Some header</header></section>'),
433                         section = c.doc(),
434                         text = section.children()[0].children()[0];
435
436                         text.split({offset: 11});
437                         
438                         var header1 = section.children()[0];
439                         var header2 = section.children()[1];
440
441                         expect(header1.children()[0].getText()).to.equal('Some header');
442                         expect(header2.children().length).to.equal(0);
443                 });
444
445                 it('keeps DocumentTextElement\'s parent\'s children elements intact', function() {
446                     var c = canvas.fromXML('\
447                             <section>\
448                                 <header>\
449                                     A <span>fancy</span> and <span>nice</span> header\
450                                 </header>\
451                             </section>'),
452                         section = c.doc(),
453                         header = section.children()[0],
454                         textAnd = header.children()[2];
455
456                     textAnd.split({offset: 2});
457                     
458                     var sectionChildren = section.children();
459                     expect(sectionChildren.length).to.equal(2, 'Section has two children');
460                     expect(sectionChildren[0].getWlxmlTag()).to.equal('header', 'First section element is a wlxml header');
461                     expect(sectionChildren[1].getWlxmlTag()).to.equal('header', 'Second section element is a wlxml header');
462
463                     var firstHeaderChildren = sectionChildren[0].children();
464                     expect(firstHeaderChildren.length).to.equal(3, 'First header has three children');
465                     expect(firstHeaderChildren[0].getText()).to.equal('A ', 'First header starts with a text');
466                     expect(firstHeaderChildren[1].getWlxmlTag()).to.equal('span', 'First header has span in the middle');
467                     expect(firstHeaderChildren[2].getText()).to.equal(' a', 'First header ends with text');
468
469                     var secondHeaderChildren = sectionChildren[1].children();
470                     expect(secondHeaderChildren.length).to.equal(3, 'Second header has three children');
471                     expect(secondHeaderChildren[0].getText()).to.equal('nd ', 'Second header starts with text');
472                     expect(secondHeaderChildren[1].getWlxmlTag()).to.equal('span', 'Second header has span in the middle');
473                     expect(secondHeaderChildren[2].getText()).to.equal(' header', 'Second header ends with text');
474                 });
475             });
476
477             describe('wrapping', function() {
478                 it('wraps DocumentNodeElement', function() {
479                     var c = canvas.fromXML('<section><div></div></section>'),
480                         div = c.doc().children()[0];
481                     
482                     var returned = div.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
483                         parent = div.parent(),
484                         parent2 = c.doc().children()[0];
485
486                     expect(returned.sameNode(parent)).to.be.true;
487                     expect(returned.sameNode(parent2)).to.be.true;
488                     expect(returned.getWlxmlTag()).to.equal('header');
489                     expect(returned.getWlxmlClass()).to.equal('some.class');
490                 });
491                 it('wraps DocumentTextElement', function() {
492                     var c = canvas.fromXML('<section>Alice</section>'),
493                         text = c.doc().children()[0];
494                     
495                     var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
496                         parent = text.parent(),
497                         parent2 = c.doc().children()[0];
498
499                     expect(returned.sameNode(parent)).to.be.true;
500                     expect(returned.sameNode(parent2)).to.be.true;
501                     expect(returned.getWlxmlTag()).to.equal('header');
502                     expect(returned.getWlxmlClass()).to.equal('some.class');
503                 });
504                 
505                 describe('wrapping part of DocumentTextElement', function() {
506                     [{start: 5, end: 12}, {start: 12, end: 5}].forEach(function(offsets) {
507                         it('wraps in the middle ' + offsets.start + '/' + offsets.end, function() {
508                             var c = canvas.fromXML('<section>Alice has a cat</section>'),
509                                 text = c.doc().children()[0];
510                             
511                             var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class', start: offsets.start, end: offsets.end}),
512                                 children = c.doc().children();
513
514                             expect(children.length).to.equal(3);
515                             
516                             expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
517                             expect(children[0].getText()).to.equal('Alice');
518
519                             expect(children[1].sameNode(returned)).to.be.true;
520                             expect(returned.getWlxmlTag()).to.equal('header');
521                             expect(returned.getWlxmlClass()).to.equal('some.class');
522                             expect(children[1].children().length).to.equal(1);
523                             expect(children[1].children()[0].getText()).to.equal(' has a ');
524
525                             expect(children[2]).to.be.instanceOf(documentElement.DocumentTextElement);
526                             expect(children[2].getText()).to.equal('cat');
527                         });
528                     });
529
530                     it('wraps whole text inside DocumentTextElement if offsets span entire content', function() {
531                          var c = canvas.fromXML('<section>Alice has a cat</section>'),
532                              text = c.doc().children()[0];
533                          
534                          var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class', start: 0, end: 15}),
535                              children = c.doc().children();
536
537                          expect(children.length).to.equal(1);
538                          expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
539                          expect(children[0].children()[0].getText()).to.equal('Alice has a cat');
540                     });
541                 });
542
543                 it('wraps text spanning multiple sibling DocumentTextNodes', function() {
544                     var c = canvas.fromXML('<section>Alice has a <span>small</span> cat</section>'),
545                         section = c.doc(),
546                         wrapper = c.wrapText({
547                             inside: section, 
548                             _with: {tag: 'span', klass: 'some.class'},
549                             offsetStart: 6,
550                             offsetEnd: 4,
551                             textNodeIdx: [0,2]
552                         });
553
554                     expect(section.children().length).to.equal(2);
555                     expect(section.children()[0]).to.be.instanceOf(documentElement.DocumentTextElement);
556                     expect(section.children()[0].getText()).to.equal('Alice ');
557
558                     var wrapper2 = section.children()[1];
559                     expect(wrapper2.sameNode(wrapper)).to.be.true;
560
561                     var wrapperChildren = wrapper.children();
562                     expect(wrapperChildren.length).to.equal(3);
563                     expect(wrapperChildren[0].getText()).to.equal('has a ');
564
565                     expect(wrapperChildren[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
566                     expect(wrapperChildren[1].children().length).to.equal(1);
567                     expect(wrapperChildren[1].children()[0].getText()).to.equal('small');
568
569                     expect(wrapperChildren[2].getText()).to.equal(' cat');
570                 });
571             });
572
573             describe('unwrapping', function() {
574                 it('unwraps DocumentTextElement from its parent DocumentNodeElement if it\'s its only child', function() {
575                     var c = canvas.fromXML('<section>Alice <span>has a</span> cat</section>'),
576                     section = c.doc(),
577                     text = section.children()[1].children()[0];
578
579                     var newTextContainer = text.unwrap();
580
581                     expect(section.children().length).to.equal(1, 'section has one child');
582                     expect(section.children()[0].getText()).to.equal('Alice has a cat');
583                     expect(newTextContainer.sameNode(c.doc())).to.equal(true, 'unwrap returns new text parent DocumentNodeElement');
584                 })
585             });
586         });
587
588         describe('Lists api', function() {
589             describe('creating lists', function() {
590                 it('allows creation of a list from existing sibling DocumentElements', function() {
591                     var c = canvas.fromXML('\
592                         <section>\
593                             Alice\
594                             <div>has</div>\
595                             a\
596                             <div>cat</div>\
597                         </section>'),
598                         section = c.doc(),
599                         textHas = section.children()[1],
600                         divA = section.children()[2]
601                     
602                     c.list.create({element1: textHas, element2: divA});
603
604                     expect(section.children().length).to.equal(3, 'section has three child elements');
605
606                     var child1 = section.children()[0],
607                         list = section.children()[1],
608                         child3 = section.children()[2];
609
610                     expect(child1.getText()).to.equal('Alice');
611                     expect(list.is('list')).to.equal(true, 'second child is a list');
612                     expect(list.children().length).to.equal(2, 'list contains two elements');
613                     list.children().forEach(function(child) {
614                         expect(child.getWlxmlClass()).to.equal('item', 'list childs have wlxml class of item');
615                     });
616                     expect(child3.children()[0].getText()).to.equal('cat');
617                 });
618                 
619                 it('allows creating nested list from existing sibling list items', function() {
620                     var c = canvas.fromXML('\
621                         <section>\
622                             <div class="list-items">\
623                                 <div class="item">A</div>\
624                                 <div class="item">B</div>\
625                                 <div class="item">C</div>\
626                                 <div class="item">D</div>\
627                             </div>\
628                         </section>'),
629                         outerList = c.doc().children()[0],
630                         itemB = outerList.children()[1],
631                         itemC = outerList.children()[2];
632
633
634                         c.list.create({element1: itemB, element2: itemC});
635
636                     var outerListItems = outerList.children(),
637                         innerList = outerListItems[1].children()[0],
638                         innerListItems = innerList.children();
639
640                     expect(outerListItems.length).to.equal(3, 'outer list has three items');
641                     expect(outerListItems[0].children()[0].getText()).to.equal('A', 'first outer item ok');
642                     expect(outerListItems[1].getWlxmlClass()).to.equal('item', 'inner list is wrapped by item element');
643
644                     expect(innerList.is('list')).to.equal(true, 'inner list created');
645                     expect(innerListItems.length).to.equal(2, 'inner list has two items');
646                     expect(innerListItems[0].children()[0].getText()).to.equal('B', 'first inner item ok');
647                     expect(innerListItems[1].children()[0].getText()).to.equal('C', 'second inner item ok');
648
649                     expect(outerListItems[2].children()[0].getText()).to.equal('D', 'last outer item ok');
650
651                 });
652
653             });
654
655             describe('extracting list items', function() {
656                 it('creates two lists with extracted items in the middle if extracting from the middle of the list', function() {
657                     var c = canvas.fromXML('\
658                         <section>\
659                             <div class="list.items">\
660                                 <div class="item">0</div>\
661                                 <div class="item">1</div>\
662                                 <div class="item">2</div>\
663                                 <div class="item">3</div>\
664                             </div>\
665                         </section>'),
666                         list = c.doc().children()[0],
667                         item1 = list.children()[1],
668                         item2 = list.children()[2];
669
670                     c.list.extractItems({element1: item1, element2: item2});
671
672                     var section = c.doc(),
673                         list1 = section.children()[0],
674                         oldItem1 = section.children()[1],
675                         oldItem2 = section.children()[2],
676                         list2 = section.children()[3];
677
678                     expect(section.children().length).to.equal(4, 'section contains four children');
679                     
680                     expect(list1.is('list')).to.equal(true, 'first section child is a list');
681                     expect(list1.children().length).to.equal(1, 'first list has one child');
682                     expect(list1.children()[0].children()[0].getText()).to.equal('0', 'first item of the first list is a first item of the original list');
683
684                     expect(oldItem1.children()[0].getText()).to.equal('1', 'first item got extracted');
685                     expect(oldItem1.getWlxmlClass() === undefined).to.equal(true, 'first extracted element has no wlxml class');
686
687                     expect(oldItem2.children()[0].getText()).to.equal('2', 'second item got extracted');
688                     expect(oldItem2.getWlxmlClass() === undefined).to.equal(true, 'second extracted element has no wlxml class');
689
690                     expect(list2.is('list')).to.equal(true, 'last section child is a list');
691                     expect(list2.children().length).to.equal(1, 'second list has one child');
692                     expect(list2.children()[0].children()[0].getText()).to.equal('3', 'first item of the second list is a last item of the original list');
693                 });
694
695                 it('puts extracted items above the list if starting item is the first one', function() {
696                     var c = canvas.fromXML('\
697                         <section>\
698                             <div class="list.items">\
699                                 <div class="item">0</div>\
700                                 <div class="item">1</div>\
701                                 <div class="item">2</div>\
702                             </div>\
703                         </section>'),
704                         list = c.doc().children()[0],
705                         item1 = list.children()[0],
706                         item2 = list.children()[1],
707                         item3 = list.children()[2];
708
709                     c.list.extractItems({element1: item1, element2: item2});
710
711                     var section = c.doc(),
712                         oldItem1 = section.children()[0],
713                         oldItem2 = section.children()[1],
714                         newList = section.children()[2];
715
716                     expect(section.children().length).to.equal(3, 'section has three children');
717                     expect(oldItem1.children()[0].getText()).to.equal('0', 'first item extracted');
718                     expect(oldItem2.children()[0].getText()).to.equal('1', 'second item extracted');
719                     expect(newList.is('list')).to.equal(true, 'list lies below extracted item');
720                     expect(newList.children().length).to.equal(1, 'list has now one child');
721                 });
722
723                 it('puts extracted items below the list if ending item is the last one', function() {
724                     var c = canvas.fromXML('\
725                         <section>\
726                             <div class="list.items">\
727                                 <div class="item">0</div>\
728                                 <div class="item">1</div>\
729                                 <div class="item">2</div>\
730                             </div>\
731                         </section>'),
732                         list = c.doc().children()[0],
733                         item1 = list.children()[0],
734                         item2 = list.children()[1],
735                         item3 = list.children()[2];
736
737                     c.list.extractItems({element1: item2, element2: item3});
738
739                     var section = c.doc(),
740                         oldItem1 = section.children()[1],
741                         oldItem2 = section.children()[2],
742                         newList = section.children()[0];
743
744                     expect(section.children().length).to.equal(3, 'section has three children');
745                     expect(oldItem1.children()[0].getText()).to.equal('1', 'first item extracted');
746                     expect(oldItem2.children()[0].getText()).to.equal('2', 'second item extracted');
747                     expect(newList.is('list')).to.equal(true, 'list lies above extracted item');
748                     expect(newList.children().length).to.equal(1, 'list has now one child');
749                 });
750
751                 it('removes list if all its items are extracted', function() {
752                     var c = canvas.fromXML('\
753                         <section>\
754                             <div class="list.items">\
755                                 <div class="item">some item</div>\
756                                 <div class="item">some item 2</div>\
757                             </div>\
758                         </section>'),
759                         list = c.doc().children()[0],
760                         item1 = list.children()[0],
761                         item2 = list.children()[1];
762
763                     c.list.extractItems({element1: item1, element2: item2});
764
765                     var section = c.doc(),
766                         list1 = section.children()[0],
767                         oldItem1 = section.children()[0],
768                         oldItem2 = section.children()[1];
769
770                     expect(section.children().length).to.equal(2, 'section contains two children');
771                     expect(oldItem1.children()[0].getText()).to.equal('some item');
772                     expect(oldItem2.children()[0].getText()).to.equal('some item 2');
773                 });
774
775                 it('creates two lists with extracted items in the middle if extracting from the middle of the list - nested case' , function() {
776                     var c = canvas.fromXML('\
777                         <section>\
778                             <div class="list.items">\
779                                 <div class="item">0</div>\
780                                 <div class="item">\
781                                     <div class="list.items">\
782                                         <div class="item">1.1</div>\
783                                         <div class="item">1.2</div>\
784                                         <div class="item">1.3</div>\
785                                     </div>\
786                                 </div>\
787                                 <div class="item">2</div>\
788                             </div>\
789                         </section>'),
790                         list = c.doc().children()[0],
791                         nestedList = list.children()[1].children()[0],
792                         nestedListItem = nestedList.children()[1];
793
794                     c.list.extractItems({element1: nestedListItem, element2: nestedListItem});
795
796                     var section = c.doc(),
797                         list = section.children()[0],
798                         item1 = list.children()[0],
799                         item2 = list.children()[1], //
800                         item3 = list.children()[2],
801                         item4 = list.children()[3], //
802                         item5 = list.children()[4],
803                         nestedList1 = item2.children()[0],
804                         nestedList2 = item4.children()[0];
805
806                     expect(list.children().length).to.equal(5, 'top list has five items');
807                     
808                     expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
809
810                     expect(item2.getWlxmlClass()).to.equal('item', 'first nested list is still wrapped in item element');
811                     expect(nestedList1.children().length).to.equal(1, 'first nested list is left with one child');
812                     expect(nestedList1.children()[0].children()[0].getText()).to.equal('1.1', 'first nested list item left alone');
813                     
814                     expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
815
816                     expect(item4.getWlxmlClass()).to.equal('item', 'second nested list is still wrapped in item element');
817                     expect(nestedList2.children().length).to.equal(1, 'second nested list is left with one child');
818                     expect(nestedList2.children()[0].children()[0].getText()).to.equal('1.3', 'second nested list item left alone');
819
820                     expect(item5.children()[0].getText()).to.equal('2', 'last item ok');
821                 });
822
823                 it('puts extracted items below the list if ending item is the last one - nested case' , function() {
824                     var c = canvas.fromXML('\
825                         <section>\
826                             <div class="list.items">\
827                                 <div class="item">0</div>\
828                                 <div class="item">\
829                                     <div class="list.items">\
830                                         <div class="item">1.1</div>\
831                                         <div class="item">1.2</div>\
832                                         <div class="item">1.3</div>\
833                                     </div>\
834                                 </div>\
835                                 <div class="item">2</div>\
836                             </div>\
837                         </section>'),
838                         list = c.doc().children()[0],
839                         nestedList = list.children()[1].children()[0],
840                         nestedListItem1 = nestedList.children()[1],
841                         nestedListItem2 = nestedList.children()[2];
842
843                     c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
844
845                     var section = c.doc(),
846                         list = section.children()[0],
847                         item1 = list.children()[0],
848                         item2 = list.children()[1],
849                         item3 = list.children()[2],
850                         item4 = list.children()[3],
851                         item5 = list.children()[4];
852                     nestedList = item2.children()[0];
853
854                     expect(list.children().length).to.equal(5, 'top list has five items');
855                     expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
856                     expect(item2.getWlxmlClass()).to.equal('item', 'nested list is still wrapped in item element');
857                     expect(nestedList.children().length).to.equal(1, 'nested list is left with one child');
858                     expect(nestedList.children()[0].children()[0].getText()).to.equal('1.1', 'nested list item left alone');
859                     expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
860                     expect(item4.children()[0].getText()).to.equal('1.3', 'fourth item ok');
861                     expect(item5.children()[0].getText()).to.equal('2', 'fifth item ok');
862                 });
863
864                 it('puts extracted items above the list if starting item is the first one - nested case' , function() {
865                     var c = canvas.fromXML('\
866                         <section>\
867                             <div class="list.items">\
868                                 <div class="item">0</div>\
869                                 <div class="item">\
870                                     <div class="list.items">\
871                                         <div class="item">1.1</div>\
872                                         <div class="item">1.2</div>\
873                                         <div class="item">1.3</div>\
874                                     </div>\
875                                 </div>\
876                                 <div class="item">2</div>\
877                             </div>\
878                         </section>'),
879                         list = c.doc().children()[0],
880                         nestedList = list.children()[1].children()[0],
881                         nestedListItem1 = nestedList.children()[0],
882                         nestedListItem2 = nestedList.children()[1];
883
884                     c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
885
886                     var section = c.doc(),
887                         list = section.children()[0],
888                         item1 = list.children()[0],
889                         item2 = list.children()[1],
890                         item3 = list.children()[2],
891                         item4 = list.children()[3],
892                         item5 = list.children()[4];
893                     nestedList = item4.children()[0];
894
895                     expect(list.children().length).to.equal(5, 'top list has five items');
896                     expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
897                     expect(item2.children()[0].getText()).to.equal('1.1', 'second item ok');
898                     expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
899                     
900                     expect(item4.getWlxmlClass()).to.equal('item', 'nested list is still wrapped in item element');
901                     expect(nestedList.children().length).to.equal(1, 'nested list is left with one child');
902                     expect(nestedList.children()[0].children()[0].getText()).to.equal('1.3', 'nested list item left alone');
903                     expect(item5.children()[0].getText()).to.equal('2', 'fifth item ok');
904                 });
905
906                 it('removes list if all its items are extracted - nested case', function() {
907                     var c = canvas.fromXML('\
908                         <section>\
909                             <div class="list.items">\
910                                 <div class="item">0</div>\
911                                 <div class="item">\
912                                     <div class="list.items">\
913                                         <div class="item">1.1</div>\
914                                         <div class="item">1.2</div>\
915                                     </div>\
916                                 </div>\
917                                 <div class="item">2</div>\
918                             </div>\
919                         </section>'),
920                         list = c.doc().children()[0],
921                         nestedList = list.children()[1].children()[0],
922                         nestedListItem1 = nestedList.children()[0],
923                         nestedListItem2 = nestedList.children()[1];
924
925                     c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
926
927                     var section = c.doc(),
928                         list = section.children()[0],
929                         item1 = list.children()[0],
930                         item2 = list.children()[1],
931                         item3 = list.children()[2],
932                         item4 = list.children()[3];
933
934                     expect(list.children().length).to.equal(4, 'top list has four items');
935                     expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
936                     expect(item2.children()[0].getText()).to.equal('1.1', 'second item ok');
937                     expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
938                     expect(item4.children()[0].getText()).to.equal('2', 'fourth item ok');
939                 });
940
941                 it('extracts items out of outer most list when merge flag is set to false', 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                         section = c.doc(),
956                         list = section.children()[0],
957                         nestedList = list.children()[1].children()[0],
958                         nestedListItem = nestedList.children()[0];
959
960                     var test = c.list.extractItems({element1: nestedListItem, element2: nestedListItem, merge: false});
961
962                     expect(test).to.equal(true, 'extraction status ok');
963
964                     var sectionChildren = section.children(),
965                         extractedItem = sectionChildren[1];
966
967                     expect(sectionChildren.length).to.equal(3, 'section has three children');
968                     expect(sectionChildren[0].is('list')).to.equal(true, 'first child is a list');
969
970                     expect(extractedItem.getWlxmlTag()).to.equal('div', 'extracted item is a wlxml div');
971                     expect(extractedItem.getWlxmlClass()).to.equal(undefined, 'extracted item has no wlxml class');
972                     expect(extractedItem.children()[0].getText()).to.equal('1.1', 'extracted item ok');
973                     expect(sectionChildren[2].is('list')).to.equal(true, 'second child is a list');
974                 });
975             });
976         });
977
978     });
979
980     describe('Cursor', function() {
981
982         var getSelection;
983
984         beforeEach(function() {
985             getSelection = sinon.stub(window, 'getSelection');
986         });
987
988         afterEach(function() {
989             getSelection.restore();
990         });
991
992         it('returns position when browser selection collapsed', function() {
993             var c = canvas.fromXML('<section>Alice has a cat</section>'),
994                 dom = c.doc().dom(),
995                 text = $(dom.contents()[0]).contents()[0];
996
997             expect(text.nodeType).to.equal(Node.TEXT_NODE, 'correct node selected');
998             expect($(text).text()).to.equal('Alice has a cat');
999
1000             getSelection.returns({
1001                 anchorNode: text,
1002                 focusNode: text,
1003                 anchorOffset: 5,
1004                 focusOffset: 5,
1005                 isCollapsed: true
1006             });
1007             var cursor = c.getCursor(),
1008                 position = cursor.getPosition();
1009
1010             expect(cursor.isSelecting()).to.equal(false, 'cursor is not selecting anything');
1011             expect(position.element.getText()).to.equal('Alice has a cat');
1012             expect(position.offset).to.equal(5);
1013             expect(position.offsetAtEnd).to.equal(false, 'offset is not at end');
1014
1015             getSelection.returns({
1016                 anchorNode: text,
1017                 focusNode: text,
1018                 anchorOffset: 15,
1019                 focusOffset: 15,
1020                 isCollapsed: true
1021             });
1022
1023             expect(cursor.getPosition().offsetAtEnd).to.equal(true, 'offset at end');
1024         });
1025
1026         it('returns boundries of selection when browser selection not collapsed', function() {
1027             var c = canvas.fromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
1028                 dom = c.doc().dom(),
1029                 text = {
1030                     alice: dom.contents()[0],
1031                     has: $(dom.contents()[1]).contents()[0],
1032                     cat: dom.contents()[4]
1033                 },
1034                 cursor = c.getCursor(),
1035                 aliceElement = c.getDocumentElement(text.alice),
1036                 catElement = c.getDocumentElement(text.cat);
1037
1038
1039                 [
1040                     {focus: text.alice, focusOffset: 1, anchor: text.cat,   anchorOffset: 2, selectionAnchor: catElement},
1041                     {focus: text.cat,   focusOffset: 2, anchor: text.alice, anchorOffset: 1, selectionAnchor: aliceElement}
1042                 ].forEach(function(s, idx) {
1043                     getSelection.returns({isColapsed: false, anchorNode: s.anchor, anchorOffset: s.anchorOffset, focusNode: s.focus, focusOffset: s.focusOffset});
1044
1045                     var selectionStart = cursor.getSelectionStart(),
1046                         selectionEnd = cursor.getSelectionEnd(),
1047                         selectionAnchor = cursor.getSelectionAnchor();
1048
1049                     expect(cursor.isSelecting()).to.equal(true, 'cursor is selecting');
1050                     expect(selectionStart.element.sameNode(aliceElement)).to.equal(true, '"Alice" is the start of the selection ' + idx);
1051                     expect(selectionStart.offset).to.equal(1, '"Alice" offset ok' + idx);
1052                     expect(selectionEnd.element.sameNode(catElement)).to.equal(true, '"Cat" is the start of the selection ' + idx);
1053                     expect(selectionEnd.offset).to.equal(2, '"Cat" offset ok' + idx);
1054                     expect(selectionAnchor.element.sameNode(s.selectionAnchor)).to.equal(true, 'anchor ok');
1055                     expect(selectionAnchor.offset).to.equal(s.anchorOffset, 'anchor offset ok');
1056                 });
1057         });
1058
1059         it('recognizes when browser selection boundries lies in sibling DocumentTextElements', function() {
1060             var c = canvas.fromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
1061                 dom = c.doc().dom(),
1062                 text = {
1063                     alice: dom.contents()[0],
1064                     has: $(dom.contents()[1]).contents()[0],
1065                     a: dom.contents()[2],
1066                     big: $(dom.contents()[3]).contents()[0],
1067                     cat: dom.contents()[4]
1068                 },
1069                 cursor = c.getCursor();
1070
1071             expect($(text.alice).text()).to.equal('Alice ');
1072             expect($(text.has).text()).to.equal('has');
1073             expect($(text.a).text()).to.equal(' a ');
1074             expect($(text.big).text()).to.equal('big');
1075             expect($(text.cat).text()).to.equal(' cat');
1076
1077             getSelection.returns({anchorNode: text.alice, focusNode: text.a});
1078             expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "a" are children');
1079
1080             getSelection.returns({anchorNode: text.alice, focusNode: text.cat});
1081             expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "cat" are children');
1082
1083             getSelection.returns({anchorNode: text.alice, focusNode: text.has});
1084             expect(cursor.isSelectingSiblings()).to.equal(false, '"Alice" and "has" are not children');
1085
1086             getSelection.returns({anchorNode: text.has, focusNode: text.big});
1087             expect(cursor.isSelectingSiblings()).to.equal(false, '"has" and "big" are not children');
1088             
1089         })
1090     });
1091
1092     describe('Serializing document to WLXML', function() {
1093         it('keeps document intact when no changes have been made', function() {
1094             var xmlIn = '<section>Alice<div>has</div>a <span class="uri" meta-uri="http://cat.com">cat</span>!</section>',
1095                 c = canvas.fromXML(xmlIn),
1096                 xmlOut = c.toXML();
1097
1098             var parser = new DOMParser(),
1099                 input = parser.parseFromString(xmlIn, "application/xml").childNodes[0],
1100                 output = parser.parseFromString(xmlOut, "application/xml").childNodes[0];
1101             
1102             expect(input.isEqualNode(output)).to.be.true;
1103         });
1104
1105         it('keeps arbitrary node attributes intact', function() {
1106             var xmlIn = '<section a="1" xmlns:dcterms="http://purl.org/dc/terms/"></section>',
1107                 $xmlOut = $(canvas.fromXML(xmlIn).toXML());
1108
1109             expect($xmlOut.attr('a')).to.equal('1');
1110             expect($xmlOut.attr('xmlns:dcterms')).to.equal('http://purl.org/dc/terms/');
1111         });
1112
1113         it('doesn\' serialize meta attribute if its empty', function() {
1114             var c;
1115
1116             c = canvas.fromXML('<section class="uri" meta-uri="some.uri"></section>');
1117             c.doc().setWlxmlMetaAttr('uri', '');
1118             expect($(c.toXML()).attr('meta-uri')).to.equal(undefined, 'overriding attribute with zero length string');
1119
1120             c = canvas.fromXML('<section class="uri"></section>');
1121             c.doc().setWlxmlMetaAttr('uri', '');
1122             expect($(c.toXML()).attr('meta-uri')).to.equal(undefined, 'setting attribute to zero length string');
1123         });
1124
1125         describe('formatting output xml', function() {
1126             /*it('keeps white spaces at the edges of input xml', function() {
1127                 var xmlIn = '  <section></section>  ',
1128                 c = canvas.fromXML(xmlIn),
1129                 xmlOut = c.toXML();
1130
1131                 expect(xmlOut.substr(4)).to.equal('   <', 'start');
1132                 expect(xmlOut.substr(-2)).to.equal('>  ', 'end');
1133             });*/
1134             it('keeps white space between XML nodes', function() {
1135                 var xmlIn = '<section>\n\n\n<div></div>\n\n\n<div></div>\n\n\n</section>',
1136                 c = canvas.fromXML(xmlIn),
1137                 xmlOut = c.toXML();
1138
1139                 var partsIn = xmlIn.split('\n\n\n'),
1140                     partsOut = xmlOut.split('\n\n\n');
1141                 
1142                 expect(partsIn).to.deep.equal(partsOut);
1143             });
1144
1145             it('keeps white space between XML nodes - inline case', function() {
1146                 var xmlIn = '<section>\n\n\n<span></span>\n\n\n<span></span>\n\n\n</section>',
1147                 c = canvas.fromXML(xmlIn),
1148                 xmlOut = c.toXML();
1149
1150                 var partsIn = xmlIn.split('\n\n\n'),
1151                     partsOut = xmlOut.split('\n\n\n');
1152                 
1153                 expect(partsIn).to.deep.equal(partsOut);
1154             });
1155
1156             it('nests new children block elements', function() {
1157                 var c = canvas.fromXML('<section></section>');
1158     
1159                 c.doc().append({tag: 'header'});
1160
1161                 var xmlOut = c.toXML();
1162                 expect(xmlOut.split('\n  ')[0]).to.equal('<section>', 'nesting start ok');
1163                 expect(xmlOut.split('\n').slice(-1)[0]).to.equal('</section>', 'nesting end ok');
1164
1165             });
1166
1167             it('doesn\'t nest new children inline elements', function() {
1168                 var c = canvas.fromXML('<section></section>');
1169     
1170                 c.doc().append({tag: 'span'});
1171
1172                 var xmlOut = c.toXML();
1173                 expect(xmlOut).to.equal('<section><span></span></section>');
1174             });
1175
1176             it('keeps original white space at the end of text', function() {
1177                 
1178                 var xmlIn = '<header>    Some text ended with white space \
1179                 \
1180                 <span class="uri">Some text</span> some text\
1181             \
1182             </header>',
1183                     c = canvas.fromXML(xmlIn);
1184
1185             var xmlOut = c.toXML();
1186             console.log(xmlOut);
1187             expect(xmlOut).to.equal(xmlIn);
1188
1189             });
1190
1191         })
1192     })
1193 });
1194
1195
1196 });