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