Refactoring
[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 DocumentNodeElement', function() {
16         it('is always a div tag', function() {
17             ['section', 'header', 'span', 'aside', 'figure'].forEach(function(tagName) {
18                 var dom = canvas.fromXML('<' + tagName + '></' + tagName + '>').doc().dom();
19                 expect(dom.prop('tagName')).to.equal('DIV', tagName + ' is represented as div');
20             });
21         });
22         it('has wlxml tag put into wlxml-tag attribute of its internal container', function() {
23             var dom = canvas.fromXML('<section></section>').doc().dom();
24             expect(dom.children('[document-element-content]').attr('wlxml-tag')).to.equal('section');
25         });
26         it('has wlxml class put into wlxml-class attribute of its internal containr, dots replaced with dashes', function() {
27             var dom = canvas.fromXML('<section class="some.class"></section>').doc().dom();
28             expect(dom.children('[document-element-content]').attr('wlxml-class')).to.equal('some-class');
29         });
30     });
31
32     describe('Internal HTML representation of a DocumentTextElement', function() {
33         it('is text node wrapped in a div with document-text-element attribute set', function() {
34             var dom = canvas.fromXML('<section>Alice</section>').doc().children()[0].dom();
35             expect(dom.prop('tagName')).to.equal('DIV');
36             expect(dom.attr('document-text-element')).to.equal('');
37             expect(dom.contents().length).to.equal(1);
38             expect(dom.contents()[0].nodeType).to.equal(Node.TEXT_NODE);
39             expect($(dom.contents()[0]).text()).to.equal('Alice');
40         });
41     });
42
43     describe('basic properties', function() {
44         it('renders empty document when canvas created from empty XML', function() {
45             var c = canvas.fromXML('');
46             expect(c.doc()).to.equal(null);
47         });
48
49         it('gives access to its document root node', function() {
50             var c = canvas.fromXML('<section></section>');
51             expect(c.doc().getWlxmlTag()).to.equal('section');
52         });
53
54         describe('root element', function() {
55             it('has no parent', function() {
56                 var c = canvas.fromXML('<section></section>');
57                 expect(c.doc().parent()).to.be.null;
58             });
59         });
60
61         describe('DocumentTextElement', function() {
62             it('can have its content set', function() {
63                 var c = canvas.fromXML('<section>Alice</section>'),
64                     root = c.doc(),
65                     text = root.children()[0];
66                 
67                 text.setText('a cat');
68                 expect(root.children()[0].getText()).to.equal('a cat');
69             });
70         });
71
72         describe('DocumentNodeElement', function() {
73             it('knows index of its child', function() {
74                 var c = canvas.fromXML('<section><div></div><header></header><span></span></section>'),
75                     root = c.doc(),
76                     child = root.children()[1];
77                 expect(root.childIndex(child)).to.equal(1);
78             });
79
80             it('knows WLXML tag it renders', function(){
81                 var c = canvas.fromXML('<section></section>'),
82                     section = c.doc();
83                 expect(section.getWlxmlTag()).to.equal('section', 'initial tag is section');
84                 section.setWlxmlTag('header');
85                 expect(section.getWlxmlTag()).to.equal('header', 'tag is changed to header');
86             });
87
88             it('knows WLXML class of a WLXML tag it renders', function(){
89                 var c = canvas.fromXML('<section class="some.class.A"></section>'),
90                     section = c.doc();
91                 expect(section.getWlxmlClass()).to.equal('some.class.A');
92                 section.setWlxmlClass('some.class.B');
93                 expect(section.getWlxmlClass()).to.equal('some.class.B');
94                 section.setWlxmlClass(null);
95                 expect(section.getWlxmlClass()).to.be.undefined;
96             });
97
98
99
100             describe('element has meta attributes', function() {
101                 it('can change its meta attributes', function() {
102                     var c = canvas.fromXML('<section><span class="uri" meta-uri="someuri"></span></section>'),
103                     span = c.doc().children()[0];
104                     
105                     expect(span.getWlxmlMetaAttr('uri')).to.equal('someuri');
106                     span.setWlxmlMetaAttr('uri', 'otheruri');
107                     expect(span.getWlxmlMetaAttr('uri')).to.equal('otheruri');
108                 });
109
110                 it('changes its meta attributes with class change', function() {
111                     var c = canvas.fromXML('<section><span class="uri" meta-uri="someuri"></span></section>'),
112                     span = c.doc().children()[0];
113                     
114                     expect(span.getWlxmlMetaAttr('uri')).to.equal('someuri');
115                     span.setWlxmlClass('author');
116                     expect(span.getWlxmlMetaAttr('uri')).to.be.undefined;
117                 });
118
119                 it('keeps meta attribute value on class change if a new class has this attribute', function() {
120                     var c = canvas.fromXML('<section><span class="uri" meta-uri="someuri"></span></section>'),
121                     span = c.doc().children()[0];
122                     span.setWlxmlClass('uri.some.subclass');
123                     expect(span.getWlxmlMetaAttr('uri')).to.equal('someuri');
124                 });
125             });
126         });
127
128         it('returns DocumentNodeElement instance from HTMLElement', function() {
129             var c = canvas.fromXML('<section></section>'),
130                 htmlElement = c.doc().dom().get(0),
131                 element = c.getDocumentElement(htmlElement);
132             expect(element).to.be.instanceOf(documentElement.DocumentNodeElement);
133             expect(element.sameNode(c.doc()));
134         });
135         
136         it('returns DocumentTextElement instance from Text Node', function() {
137             var c = canvas.fromXML('<section>Alice</section>'),
138                 aliceElement = c.doc().children()[0],
139                 textNode = aliceElement.dom().contents()[0],
140                 element = c.getDocumentElement(textNode);
141
142             expect(textNode.nodeType).to.equal(Node.TEXT_NODE, 'text node selected');
143             expect($(textNode).text()).to.equal('Alice');
144
145             expect(element).to.be.instanceOf(documentElement.DocumentTextElement);
146             expect(element.sameNode(c.doc().children()[0]));
147         });
148     });
149
150
151
152     describe('document representation api', function() {
153         describe('document root element', function() {
154             var c = canvas.fromXML('<section></section>');
155             it('exists', function() {
156                 expect(c.doc()).to.be.instanceOf(documentElement.DocumentElement);
157             });
158             it('is of type DocumentNodeElement', function() {
159                 expect(c.doc()).to.be.instanceOf(documentElement.DocumentNodeElement);
160             });
161         });
162
163         describe('DocumentElements comparison', function() {
164             it('reports dwo DocumentElements to be the same when they represent the same wlxml document element', function() {
165                 var c = canvas.fromXML('<section><div></div><div></div></section>'),
166                     first_div1 = c.doc().children()[0],
167                     first_div2 = c.doc().children()[0],
168                     second_div = c.doc().children()[1];
169                 expect(first_div1.sameNode(first_div1)).to.be.true;
170                 expect(first_div1.sameNode(first_div2)).to.be.true;
171                 expect(first_div1.sameNode(second_div)).to.be.false;
172             });
173         });
174
175         describe('traversing', function() {
176             it('reports element nodes', function() {
177                 var c = canvas.fromXML('<section><div></div></section>'),
178                     children = c.doc().children();
179                 expect(children.length).to.equal(1);
180                 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
181
182                 c = canvas.fromXML('<section><div></div><div></div></section>'),
183                     children = c.doc().children();
184                 expect(children.length).to.equal(2);
185                 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
186                 expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
187             });
188             
189             it('reports text nodes', function() {
190                 var c = canvas.fromXML('<section>Alice</section>'),
191                     children = c.doc().children();
192                 expect(children.length).to.equal(1);
193                 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
194             });
195
196             describe('accessing parents', function() {
197                 it('returns DocumentNodeElement representing parent in wlxml document as DocumentNodeElement parent', function() {
198                     var c = canvas.fromXML('<section><div></div></section>'),
199                         div = c.doc().children()[0];
200                     expect(div.parent().sameNode(c.doc())).to.be.true;
201                 });
202                 it('returns DocumentNodeElement representing parent in wlxml document as DocumentTextElement parent', function() {
203                     var c = canvas.fromXML('<section>Alice</section>'),
204                         text = c.doc().children()[0];
205                     expect(text.parent().sameNode(c.doc())).to.be.true;
206                 });
207             });
208
209             describe('free text handling', function() {
210                     it('sees free text', function() {
211                         var c = canvas.fromXML('<section>Alice <span>has</span> a cat</section>'),
212                             children = c.doc().children();
213                         expect(children.length).to.equal(3);
214                         expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
215                         expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
216                         expect(children[2]).to.be.instanceOf(documentElement.DocumentTextElement);
217                     });
218             });
219             
220             describe('white characters handling', function() {
221                 it('says empty element node has no children', function() {
222                     var c = canvas.fromXML('<section></section>');
223                     expect(c.doc().children().length).to.equal(0);
224                 });
225                 it('says element node with one space has one DocumentTextElement', function() {
226                     var c = canvas.fromXML('<section> </section>');
227                     expect(c.doc().children().length).to.equal(1);
228                     expect(c.doc().children()[0]).to.be.instanceOf(documentElement.DocumentTextElement);
229                     expect(c.doc().children()[0].getText()).to.equal(' ');
230                 });
231                 it('ignores white space surrounding block elements', function() {
232                     var c = canvas.fromXML('<section> <div></div> </section>');
233                     var children = c.doc().children();
234                     expect(children.length).to.equal(1);
235                     expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
236                 });
237                 it('ignores white space between block elements', function() {
238                     var c = canvas.fromXML('<section><div></div> <div></div></section>');
239                     var children = c.doc().children();
240                     expect(children.length === 2);
241                     [0,1].forEach(function(idx) {
242                         expect(children[idx]).to.be.instanceOf(documentElement.DocumentNodeElement);
243                     });
244                 });
245
246                 it('trims white space from the beginning and the end of the block elements', function() {
247                     var c = canvas.fromXML('<section> Alice <span>has</span> a cat </section>');
248                     expect(c.doc().children()[0].getText()).to.equal('Alice ');
249                     expect(c.doc().children()[2].getText()).to.equal(' a cat');
250                 });
251
252                 it('normalizes string of white characters to one space at the inline element boundries', function() {
253                     var c = canvas.fromXML('<span>   Alice has a cat   </span>');
254                     expect(c.doc().children()[0].getText()).to.equal(' Alice has a cat ');
255                 });
256
257                 it('normalizes string of white characters to one space before inline element', function() {
258                     var c = canvas.fromXML('<div>Alice has  <span>a cat</span></div>');
259                     expect(c.doc().children()[0].getText()).to.equal('Alice has ');
260                 });
261                 
262                 it('normalizes string of white characters to one space after inline element', function() {
263                     var c = canvas.fromXML('<div>Alice has <span>a</span>  cat</div>');
264                     expect(c.doc().children()[2].getText()).to.equal(' cat');
265                 });
266             });
267
268             describe('getting vertically first text element', function() {
269                 it('returns the first child if it\'s text element, ignores metadata', function() {
270                     var c = canvas.fromXML('<section><metadata><dc:author>author</dc:author></metadata>Alice<div>has</div>a cat</section>'),
271                         first = c.doc().getVerticallyFirstTextElement();
272
273                     expect(first.sameNode(c.doc().children()[1])).to.be.true;
274                 });
275
276                 it('looks recursively inside node elements if they precede text element', function() {
277                     var c = canvas.fromXML('\
278                             <section>\
279                                 <div>\
280                                     <div>\
281                                         Alice\
282                                     </div>\
283                                 </div>\
284                                 Some text\
285                             </section>'),
286                         textAlice = c.doc().children()[0].children()[0].children()[0],
287                         first = c.doc().getVerticallyFirstTextElement();
288
289                     expect(textAlice).to.be.instanceOf(documentElement.DocumentTextElement);
290                     expect(first.sameNode(textAlice)).to.be.true;
291                 });
292             });
293         });
294
295         describe('manipulation api', function() {
296
297             describe('Basic Element inserting', function() {
298                 it('can put new NodeElement at the end', function() {
299                     var c = canvas.fromXML('<section></section>'),
300                         appended = c.doc().append({tag: 'header', klass: 'some.class'}),
301                         children = c.doc().children();
302
303                     expect(children.length).to.equal(1);
304                     expect(children[0].sameNode(appended)).to.be.true;
305                 });
306
307                 it('can put new TextElement at the end', function() {
308                     var c = canvas.fromXML('<section></section>'),
309                         appended = c.doc().append({text: 'Alice'}),
310                         children = c.doc().children();
311
312                     expect(children.length).to.equal(1);
313                     expect(children[0].sameNode(appended)).to.be.true;
314                     expect(children[0].getText()).to.equal('Alice');
315                 });
316
317                 it('can put new NodeElement after another NodeElement', function() {
318                     var c = canvas.fromXML('<section><div></div></section>'),
319                         div = c.doc().children()[0],
320                         added = div.after({tag: 'header', klass: 'some.class'}),
321                         children = c.doc().children();
322                     expect(children.length).to.equal(2);
323                     expect(children[1].sameNode(added)).to.be.true;
324                 });
325
326                 it('can put new Nodeelement before another element', function() {
327                     var c = canvas.fromXML('<section><div></div></section>'),
328                         div = c.doc().children()[0],
329                         added = div.before({tag: 'header', klass: 'some.class'}),
330                         children = c.doc().children();
331                     expect(children.length).to.equal(2);
332                     expect(children[0].sameNode(added)).to.be.true;
333                 });
334
335                 it('can put new DocumentNodeElement after DocumentTextElement', function() {
336                     var c = canvas.fromXML('<section>Alice</section>'),
337                         text = c.doc().children()[0],
338                         added = text.after({tag: 'p'}),
339                         children = c.doc().children();
340
341                     expect(children.length).to.equal(2);
342                     expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
343                     expect(children[0].getText()).to.equal('Alice');
344                     expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
345                     expect(children[1].sameNode(added)).to.be.true;
346                 });
347                 it('can put new DocumentNodeElement before DocumentTextElement', function() {
348                     var c = canvas.fromXML('<section>Alice</section>'),
349                         text = c.doc().children()[0],
350                         added = text.before({tag: 'p'}),
351                         children = c.doc().children();
352
353                     expect(children.length).to.equal(2);
354                     expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
355                     expect(children[0].sameNode(added)).to.be.true;
356                     expect(children[1]).to.be.instanceOf(documentElement.DocumentTextElement);
357                     expect(children[1].getText()).to.equal('Alice');
358                 });
359
360                 it('can divide DocumentTextElement with a new DocumentNodeElement', function() {
361                     var c = canvas.fromXML('<section>Alice has a cat</section>'),
362                         section = c.doc(),
363                         text = section.children()[0];
364
365                     var returned = text.divide({tag: 'aside', klass: 'footnote', offset: 5}),
366                         sectionChildren = section.children(),
367                         lhsText = sectionChildren[0],
368                         rhsText = sectionChildren[2];
369
370                     expect(lhsText.getText()).to.equal('Alice');
371                     expect(returned.sameNode(sectionChildren[1]));
372                     expect(rhsText.getText()).to.equal(' has a cat');
373                 });
374
375                 it('treats dividing DocumentTextElement at the very end as appending after it', function() {
376                     var c = canvas.fromXML('<section>Alice has a cat</section>'),
377                         section = c.doc(),
378                         text = section.children()[0];
379
380                     var returned = text.divide({tag: 'aside', klass: 'footnote', offset: 15}),
381                         sectionChildren = section.children(),
382                         textElement = sectionChildren[0],
383                         nodeElement = sectionChildren[1];
384
385                     expect(sectionChildren.length).to.equal(2);
386                     expect(textElement.getText()).to.equal('Alice has a cat');
387                     expect(returned.sameNode(nodeElement)).to.be.true;
388                     expect(nodeElement.getWlxmlTag()).to.equal('aside');
389                 });
390
391                 it('treats dividing DocumentTextElement at the very beginning as appending before it', function() {
392                     var c = canvas.fromXML('<section>Alice has a cat</section>'),
393                         section = c.doc(),
394                         text = section.children()[0];
395
396                     var returned = text.divide({tag: 'aside', klass: 'footnote', offset: 0}),
397                         sectionChildren = section.children(),
398                         nodeElement = sectionChildren[0],
399                         textElement = sectionChildren[1];
400                         
401                     expect(sectionChildren.length).to.equal(2);
402                     expect(textElement.getText()).to.equal('Alice has a cat');
403                     expect(returned.sameNode(nodeElement)).to.be.true;
404                     expect(nodeElement.getWlxmlTag()).to.equal('aside');
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         var findTextNode = function(inside, text) {
1000             var nodes = inside.find(':not(iframe)').addBack().contents().filter(function() {
1001                 return this.nodeType === Node.TEXT_NODE && this.data === text;
1002             });
1003             if(nodes.length)
1004                 return nodes[0];
1005             return null;
1006         }
1007
1008         beforeEach(function() {
1009             getSelection = sinon.stub(window, 'getSelection');
1010         });
1011
1012         afterEach(function() {
1013             getSelection.restore();
1014         });
1015
1016         it('returns position when browser selection collapsed', function() {
1017             var c = canvas.fromXML('<section>Alice has a cat</section>'),
1018                 dom = c.doc().dom(),
1019                 text = findTextNode(dom, 'Alice has a cat');
1020
1021             expect(text.nodeType).to.equal(Node.TEXT_NODE, 'correct node selected');
1022             expect($(text).text()).to.equal('Alice has a cat');
1023
1024             getSelection.returns({
1025                 anchorNode: text,
1026                 focusNode: text,
1027                 anchorOffset: 5,
1028                 focusOffset: 5,
1029                 isCollapsed: true
1030             });
1031             var cursor = c.getCursor(),
1032                 position = cursor.getPosition();
1033
1034             expect(cursor.isSelecting()).to.equal(false, 'cursor is not selecting anything');
1035             expect(position.element.getText()).to.equal('Alice has a cat');
1036             expect(position.offset).to.equal(5);
1037             expect(position.offsetAtEnd).to.equal(false, 'offset is not at end');
1038
1039             getSelection.returns({
1040                 anchorNode: text,
1041                 focusNode: text,
1042                 anchorOffset: 15,
1043                 focusOffset: 15,
1044                 isCollapsed: true
1045             });
1046
1047             expect(cursor.getPosition().offsetAtEnd).to.equal(true, 'offset at end');
1048         });
1049
1050         it('returns boundries of selection when browser selection not collapsed', function() {
1051             var c = canvas.fromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
1052                 dom = c.doc().dom(),
1053                 text = {
1054                     alice: findTextNode(dom, 'Alice '),
1055                     has: findTextNode(dom, 'has'),
1056                     cat: findTextNode(dom, ' cat')
1057                 },
1058                 cursor = c.getCursor(),
1059                 aliceElement = c.getDocumentElement(text.alice),
1060                 catElement = c.getDocumentElement(text.cat);
1061
1062
1063                 [
1064                     {focus: text.alice, focusOffset: 1, anchor: text.cat,   anchorOffset: 2, selectionAnchor: catElement},
1065                     {focus: text.cat,   focusOffset: 2, anchor: text.alice, anchorOffset: 1, selectionAnchor: aliceElement}
1066                 ].forEach(function(s, idx) {
1067                     getSelection.returns({isColapsed: false, anchorNode: s.anchor, anchorOffset: s.anchorOffset, focusNode: s.focus, focusOffset: s.focusOffset});
1068
1069                     var selectionStart = cursor.getSelectionStart(),
1070                         selectionEnd = cursor.getSelectionEnd(),
1071                         selectionAnchor = cursor.getSelectionAnchor();
1072
1073                     expect(cursor.isSelecting()).to.equal(true, 'cursor is selecting');
1074                     expect(selectionStart.element.sameNode(aliceElement)).to.equal(true, '"Alice" is the start of the selection ' + idx);
1075                     expect(selectionStart.offset).to.equal(1, '"Alice" offset ok' + idx);
1076                     expect(selectionEnd.element.sameNode(catElement)).to.equal(true, '"Cat" is the start of the selection ' + idx);
1077                     expect(selectionEnd.offset).to.equal(2, '"Cat" offset ok' + idx);
1078                     expect(selectionAnchor.element.sameNode(s.selectionAnchor)).to.equal(true, 'anchor ok');
1079                     expect(selectionAnchor.offset).to.equal(s.anchorOffset, 'anchor offset ok');
1080                 });
1081         });
1082
1083         it('recognizes when browser selection boundries lies in sibling DocumentTextElements', function() {
1084             var c = canvas.fromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
1085                 dom = c.doc().dom(),
1086                 text = {
1087                     alice: findTextNode(dom, 'Alice '),
1088                     has: findTextNode(dom, 'has'),
1089                     a: findTextNode(dom, ' a '),
1090                     big: findTextNode(dom, 'big'),
1091                     cat: findTextNode(dom, ' cat'),
1092                 },
1093                 cursor = c.getCursor();
1094
1095             expect($(text.alice).text()).to.equal('Alice ');
1096             expect($(text.has).text()).to.equal('has');
1097             expect($(text.a).text()).to.equal(' a ');
1098             expect($(text.big).text()).to.equal('big');
1099             expect($(text.cat).text()).to.equal(' cat');
1100
1101             getSelection.returns({anchorNode: text.alice, focusNode: text.a});
1102             expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "a" are children');
1103
1104             getSelection.returns({anchorNode: text.alice, focusNode: text.cat});
1105             expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "cat" are children');
1106
1107             getSelection.returns({anchorNode: text.alice, focusNode: text.has});
1108             expect(cursor.isSelectingSiblings()).to.equal(false, '"Alice" and "has" are not children');
1109
1110             getSelection.returns({anchorNode: text.has, focusNode: text.big});
1111             expect(cursor.isSelectingSiblings()).to.equal(false, '"has" and "big" are not children');
1112             
1113         })
1114     });
1115
1116     describe('Serializing document to WLXML', function() {
1117         it('keeps document intact when no changes have been made', function() {
1118             var xmlIn = '<section>Alice<div>has</div>a <span class="uri" meta-uri="http://cat.com">cat</span>!</section>',
1119                 c = canvas.fromXML(xmlIn),
1120                 xmlOut = c.toXML();
1121
1122             var parser = new DOMParser(),
1123                 input = parser.parseFromString(xmlIn, "application/xml").childNodes[0],
1124                 output = parser.parseFromString(xmlOut, "application/xml").childNodes[0];
1125             
1126             expect(input.isEqualNode(output)).to.be.true;
1127         });
1128
1129         it('keeps arbitrary node attributes intact', function() {
1130             var xmlIn = '<section a="1" xmlns:dcterms="http://purl.org/dc/terms/"></section>',
1131                 $xmlOut = $(canvas.fromXML(xmlIn).toXML());
1132
1133             expect($xmlOut.attr('a')).to.equal('1');
1134             expect($xmlOut.attr('xmlns:dcterms')).to.equal('http://purl.org/dc/terms/');
1135         });
1136
1137         it('doesn\' serialize meta attribute if its empty', function() {
1138             var c;
1139
1140             c = canvas.fromXML('<section class="uri" meta-uri="some.uri"></section>');
1141             c.doc().setWlxmlMetaAttr('uri', '');
1142             expect($(c.toXML()).attr('meta-uri')).to.equal(undefined, 'overriding attribute with zero length string');
1143
1144             c = canvas.fromXML('<section class="uri"></section>');
1145             c.doc().setWlxmlMetaAttr('uri', '');
1146             expect($(c.toXML()).attr('meta-uri')).to.equal(undefined, 'setting attribute to zero length string');
1147         });
1148
1149         describe('output xml', function() {
1150             it('keeps entities intact', function() {
1151                 var xmlIn = '<section>&lt; &gt;</section>',
1152                     c = canvas.fromXML(xmlIn),
1153                     xmlOut = c.toXML();
1154                 expect(xmlOut).to.equal(xmlIn);
1155             });
1156             it('keeps entities intact when they form html/xml', function() {
1157                 var xmlIn = '<section>&lt;abc&gt;</section>',
1158                     c = canvas.fromXML(xmlIn),
1159                     xmlOut = c.toXML();
1160                 expect(xmlOut).to.equal(xmlIn);
1161             });
1162         });
1163
1164         describe('formatting output xml', function() {
1165             /*it('keeps white spaces at the edges of input xml', function() {
1166                 var xmlIn = '  <section></section>  ',
1167                 c = canvas.fromXML(xmlIn),
1168                 xmlOut = c.toXML();
1169
1170                 expect(xmlOut.substr(4)).to.equal('   <', 'start');
1171                 expect(xmlOut.substr(-2)).to.equal('>  ', 'end');
1172             });*/
1173             it('keeps white space between XML nodes', function() {
1174                 var xmlIn = '<section>\n\n\n<div></div>\n\n\n<div></div>\n\n\n</section>',
1175                 c = canvas.fromXML(xmlIn),
1176                 xmlOut = c.toXML();
1177
1178                 var partsIn = xmlIn.split('\n\n\n'),
1179                     partsOut = xmlOut.split('\n\n\n');
1180                 
1181                 expect(partsIn).to.deep.equal(partsOut);
1182             });
1183
1184             it('keeps white space between XML nodes - inline case', function() {
1185                 var xmlIn = '<section>\n\n\n<span></span>\n\n\n<span></span>\n\n\n</section>',
1186                 c = canvas.fromXML(xmlIn),
1187                 xmlOut = c.toXML();
1188
1189                 var partsIn = xmlIn.split('\n\n\n'),
1190                     partsOut = xmlOut.split('\n\n\n');
1191                 
1192                 expect(partsIn).to.deep.equal(partsOut);
1193             });
1194
1195             it('keeps white space at the beginning of text', function() {
1196                 var xmlIn = '<section>    abc<div>some div</div>    abc</section>',
1197                     c = canvas.fromXML(xmlIn),
1198                     xmlOut = c.toXML();
1199
1200                 expect(xmlOut).to.equal(xmlIn);
1201             });
1202
1203             it('nests new children block elements', function() {
1204                 var c = canvas.fromXML('<section></section>');
1205     
1206                 c.doc().append({tag: 'header'});
1207
1208                 var xmlOut = c.toXML();
1209                 expect(xmlOut.split('\n  ')[0]).to.equal('<section>', 'nesting start ok');
1210                 expect(xmlOut.split('\n').slice(-1)[0]).to.equal('</section>', 'nesting end ok');
1211
1212             });
1213
1214             it('doesn\'t nest new children inline elements', function() {
1215                 var c = canvas.fromXML('<section></section>');
1216     
1217                 c.doc().append({tag: 'span'});
1218
1219                 var xmlOut = c.toXML();
1220                 expect(xmlOut).to.equal('<section><span></span></section>');
1221             });
1222
1223             it('keeps original white space at the end of text', function() {
1224                 
1225                 var xmlIn = '<header>    Some text ended with white space \
1226                 \
1227                 <span class="uri">Some text</span> some text\
1228             \
1229             </header>',
1230                     c = canvas.fromXML(xmlIn);
1231
1232             var xmlOut = c.toXML();
1233             console.log(xmlOut);
1234             expect(xmlOut).to.equal(xmlIn);
1235             });
1236
1237             it('keeps white space around text node', function() {
1238                 var xmlIn = '<section>\
1239                 <header>header1</header>\
1240                 Some text surrounded by white space\
1241                 <header>header2</header>\
1242             </section>',
1243                     c = canvas.fromXML(xmlIn);
1244
1245                 var xmlOut = c.toXML();
1246                 expect(xmlOut).to.equal(xmlIn);
1247             });
1248
1249             it('keeps white space around text node - last node case', function() {
1250                 var xmlIn = '<section>\
1251                 <header>header</header>\
1252                     \
1253                 Some text surrounded by white space\
1254                     \
1255             </section>',
1256                     c = canvas.fromXML(xmlIn);
1257
1258                 var xmlOut = c.toXML();
1259                 expect(xmlOut).to.equal(xmlIn);
1260             });
1261
1262             it('keeps white space after detaching text element', function() {
1263                 var xmlIn = '<section><header>header</header>\n\
1264                     \n\
1265                 text1\n\
1266                     \n\
1267             </section>',
1268                     expectedXmlOut = '<section><header>header</header>\n\
1269                     \n\
1270                 \n\
1271                     \n\
1272             </section>',
1273                     c = canvas.fromXML(xmlIn),
1274                     children = c.doc().children(),
1275                     text = children[children.length-1];
1276                 
1277                 expect(text.getText()).to.equal('text1');
1278
1279                 text.detach();
1280
1281                 var xmlOut = c.toXML();
1282                 expect(xmlOut).to.equal(expectedXmlOut);
1283             });
1284
1285         })
1286     })
1287 });
1288
1289
1290 });