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