Refactoring: cleaning directories structure
[fnpeditor.git] / src / editor / modules / documentCanvas / canvas / canvas.test.js
1 define([
2 'libs/chai',
3 'libs/sinon',
4 'modules/documentCanvas/canvas/canvas',
5 'modules/documentCanvas/canvas/documentElement',
6 'modules/documentCanvas/canvas/utils'
7 ], function(chai, sinon, canvas, documentElement, utils) {
8     
9 'use strict';
10
11 var expect = chai.expect;
12
13
14 describe('Canvas', function() {
15
16
17
18     describe('ZWS', function() {
19         var view, section, textElement;
20         
21         beforeEach(function() {
22             var c = canvas.fromXML('<section></section>');
23
24             section = c.doc();
25             textElement = section.children()[0];
26             view = c.view()[0];
27             document.getElementsByTagName('body')[0].appendChild(view);
28         });
29
30         afterEach(function() {
31             view.parentNode.removeChild(view);
32         });
33
34         var getTextContainerNode = function(textElement) {
35             return textElement.dom().contents()[0];
36         }
37
38         it('is set automatically on all empty DocumentTextElements', function() {
39             expect(getTextContainerNode(textElement).data).to.equal(utils.unicode.ZWS);
40
41             var header = section.append({tag: 'header'}),
42                 newText = header.append({text: ''}),
43                 textNode = getTextContainerNode(textElement);
44             
45             expect(textNode.data).to.equal(utils.unicode.ZWS);
46         });
47
48         it('is added automatically when whole text gets deleted', function() {
49             getTextContainerNode(textElement).data = '';
50             
51             window.setTimeout(function() {
52                 expect(getTextContainerNode(textElement).data).to.equal(utils.unicode.ZWS);
53             }, 0)
54             
55             var header = section.append({tag: 'header'}),
56                 newText = header.append({text: 'Alice'}),
57                 textNode = getTextContainerNode(newText);
58
59             expect(textNode.data).to.have.length('Alice'.length);
60             textNode.data = '';
61
62             window.setTimeout(function() {
63                 expect(textNode.data).to.equal(utils.unicode.ZWS);
64             }, 0)
65         });
66     });
67
68     describe('Internal HTML representation of a DocumentNodeElement', function() {
69         it('is always a div tag', function() {
70             ['section', 'header', 'span', 'aside', 'figure'].forEach(function(tagName) {
71                 var dom = canvas.fromXML('<' + tagName + '></' + tagName + '>').doc().dom();
72                 expect(dom.prop('tagName')).to.equal('DIV', tagName + ' is represented as div');
73             });
74         });
75         it('has wlxml tag put into wlxml-tag attribute of its internal container', function() {
76             var dom = canvas.fromXML('<section></section>').doc().dom();
77             expect(dom.children('[document-element-content]').attr('wlxml-tag')).to.equal('section');
78         });
79         it('has wlxml class put into wlxml-class attribute of its internal containr, dots replaced with dashes', function() {
80             var dom = canvas.fromXML('<section class="some.class"></section>').doc().dom();
81             expect(dom.children('[document-element-content]').attr('wlxml-class')).to.equal('some-class');
82         });
83     });
84
85     describe('Internal HTML representation of a DocumentTextElement', function() {
86         it('is text node wrapped in a div with document-text-element attribute set', function() {
87             var dom = canvas.fromXML('<section>Alice</section>').doc().children()[0].dom();
88             expect(dom.prop('tagName')).to.equal('DIV');
89             expect(dom.attr('document-text-element')).to.equal('');
90             expect(dom.contents().length).to.equal(1);
91             expect(dom.contents()[0].nodeType).to.equal(Node.TEXT_NODE);
92             expect($(dom.contents()[0]).text()).to.equal('Alice');
93         });
94     });
95
96     describe('basic properties', function() {
97         it('renders empty document when canvas created from empty XML', function() {
98             var c = canvas.fromXML('');
99             expect(c.doc()).to.equal(null);
100         });
101
102         it('gives access to its document root node', function() {
103             var c = canvas.fromXML('<section></section>');
104             expect(c.doc().getWlxmlTag()).to.equal('section');
105         });
106
107         describe('root element', function() {
108             it('has no parent', function() {
109                 var c = canvas.fromXML('<section></section>');
110                 expect(c.doc().parent()).to.be.null;
111             });
112         });
113
114         describe('DocumentTextElement', function() {
115             it('can have its content set', function() {
116                 var c = canvas.fromXML('<section>Alice</section>'),
117                     root = c.doc(),
118                     text = root.children()[0];
119                 
120                 text.setText('a cat');
121                 expect(root.children()[0].getText()).to.equal('a cat');
122             });
123         });
124
125         describe('DocumentNodeElement', function() {
126             it('knows index of its child', function() {
127                 var c = canvas.fromXML('<section><div></div><header></header><span></span></section>'),
128                     root = c.doc(),
129                     child = root.children()[1];
130                 expect(root.childIndex(child)).to.equal(1);
131             });
132
133             it('knows WLXML tag it renders', function(){
134                 var c = canvas.fromXML('<section></section>'),
135                     section = c.doc();
136                 expect(section.getWlxmlTag()).to.equal('section', 'initial tag is section');
137                 section.setWlxmlTag('header');
138                 expect(section.getWlxmlTag()).to.equal('header', 'tag is changed to header');
139             });
140
141             it('knows WLXML class of a WLXML tag it renders', function(){
142                 var c = canvas.fromXML('<section class="some.class.A"></section>'),
143                     section = c.doc();
144                 expect(section.getWlxmlClass()).to.equal('some.class.A');
145                 section.setWlxmlClass('some.class.B');
146                 expect(section.getWlxmlClass()).to.equal('some.class.B');
147                 section.setWlxmlClass(null);
148                 expect(section.getWlxmlClass()).to.be.undefined;
149             });
150
151
152
153             describe('element has meta attributes', function() {
154                 it('can change its meta attributes', function() {
155                     var c = canvas.fromXML('<section><span class="uri" meta-uri="someuri"></span></section>'),
156                     span = c.doc().children()[0];
157                     
158                     expect(span.getWlxmlMetaAttr('uri')).to.equal('someuri');
159                     span.setWlxmlMetaAttr('uri', 'otheruri');
160                     expect(span.getWlxmlMetaAttr('uri')).to.equal('otheruri');
161                 });
162
163                 it('changes its meta attributes with class change', function() {
164                     var c = canvas.fromXML('<section><span class="uri" meta-uri="someuri"></span></section>'),
165                     span = c.doc().children()[0];
166                     
167                     expect(span.getWlxmlMetaAttr('uri')).to.equal('someuri');
168                     span.setWlxmlClass('author');
169                     expect(span.getWlxmlMetaAttr('uri')).to.be.undefined;
170                 });
171
172                 it('keeps meta attribute value on class change if a new class has this attribute', function() {
173                     var c = canvas.fromXML('<section><span class="uri" meta-uri="someuri"></span></section>'),
174                     span = c.doc().children()[0];
175                     span.setWlxmlClass('uri.some.subclass');
176                     expect(span.getWlxmlMetaAttr('uri')).to.equal('someuri');
177                 });
178             });
179         });
180
181         it('returns DocumentNodeElement instance from HTMLElement', function() {
182             var c = canvas.fromXML('<section></section>'),
183                 htmlElement = c.doc().dom().get(0),
184                 element = c.getDocumentElement(htmlElement);
185             expect(element).to.be.instanceOf(documentElement.DocumentNodeElement);
186             expect(element.sameNode(c.doc()));
187         });
188         
189         it('returns DocumentTextElement instance from Text Node', function() {
190             var c = canvas.fromXML('<section>Alice</section>'),
191                 aliceElement = c.doc().children()[0],
192                 textNode = aliceElement.dom().contents()[0],
193                 element = c.getDocumentElement(textNode);
194
195             expect(textNode.nodeType).to.equal(Node.TEXT_NODE, 'text node selected');
196             expect($(textNode).text()).to.equal('Alice');
197
198             expect(element).to.be.instanceOf(documentElement.DocumentTextElement);
199             expect(element.sameNode(c.doc().children()[0]));
200         });
201     });
202
203
204
205     describe('document representation api', function() {
206         describe('document root element', function() {
207             var c = canvas.fromXML('<section></section>');
208             it('exists', function() {
209                 expect(c.doc()).to.be.instanceOf(documentElement.DocumentElement);
210             });
211             it('is of type DocumentNodeElement', function() {
212                 expect(c.doc()).to.be.instanceOf(documentElement.DocumentNodeElement);
213             });
214         });
215
216         describe('DocumentElements comparison', function() {
217             it('reports dwo DocumentElements to be the same when they represent the same wlxml document element', function() {
218                 var c = canvas.fromXML('<section><div></div><div></div></section>'),
219                     first_div1 = c.doc().children()[0],
220                     first_div2 = c.doc().children()[0],
221                     second_div = c.doc().children()[1];
222                 expect(first_div1.sameNode(first_div1)).to.be.true;
223                 expect(first_div1.sameNode(first_div2)).to.be.true;
224                 expect(first_div1.sameNode(second_div)).to.be.false;
225             });
226         });
227
228         describe('traversing', function() {
229             it('reports element nodes', function() {
230                 var c = canvas.fromXML('<section><div></div></section>'),
231                     children = c.doc().children();
232                 expect(children.length).to.equal(1);
233                 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
234
235                 c = canvas.fromXML('<section><div></div><div></div></section>'),
236                     children = c.doc().children();
237                 expect(children.length).to.equal(2);
238                 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
239                 expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
240             });
241             
242             it('reports text nodes', function() {
243                 var c = canvas.fromXML('<section>Alice</section>'),
244                     children = c.doc().children();
245                 expect(children.length).to.equal(1);
246                 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
247             });
248
249             describe('accessing parents', function() {
250                 it('returns DocumentNodeElement representing parent in wlxml document as DocumentNodeElement parent', function() {
251                     var c = canvas.fromXML('<section><div></div></section>'),
252                         div = c.doc().children()[0];
253                     expect(div.parent().sameNode(c.doc())).to.be.true;
254                 });
255                 it('returns DocumentNodeElement representing parent in wlxml document as DocumentTextElement parent', function() {
256                     var c = canvas.fromXML('<section>Alice</section>'),
257                         text = c.doc().children()[0];
258                     expect(text.parent().sameNode(c.doc())).to.be.true;
259                 });
260             });
261
262             describe('accessing sibling parents of two elements', function() {
263                 it('returns elements themself if they have direct common parent', function() {
264                     var c = canvas.fromXML('<section>\
265                         <div>\
266                             <div>A</div>\
267                             <div>B</div>\
268                         </div>\
269                     </section>'),
270                         section = c.doc(),
271                         wrappingDiv = c.doc().children()[0],
272                         divA = wrappingDiv.children()[0],
273                         divB = wrappingDiv.children()[1];
274
275                     var siblingParents = c.getSiblingParents({element1: divA, element2: divB});
276
277                     expect(siblingParents.element1.sameNode(divA)).to.equal(true, 'divA');
278                     expect(siblingParents.element2.sameNode(divB)).to.equal(true, 'divB');
279                 });
280
281                 it('returns sibling parents - example 1', function() {
282                     var c = canvas.fromXML('<section>Alice <span>has a cat</span></section>'),
283                         section = c.doc(),
284                         aliceText = section.children()[0],
285                         span = section.children()[1],
286                         spanText = span.children()[0];
287
288                     var siblingParents = c.getSiblingParents({element1: aliceText, element2: spanText});
289
290                     expect(siblingParents.element1.sameNode(aliceText)).to.equal(true, 'aliceText');
291                     expect(siblingParents.element2.sameNode(span)).to.equal(true, 'span');
292                 });
293             })
294
295             describe('free text handling', function() {
296                     it('sees free text', function() {
297                         var c = canvas.fromXML('<section>Alice <span>has</span> a cat</section>'),
298                             children = c.doc().children();
299                         expect(children.length).to.equal(3);
300                         expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
301                         expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
302                         expect(children[2]).to.be.instanceOf(documentElement.DocumentTextElement);
303                     });
304             });
305
306             describe('empty nodes handling', function() {
307                 it('says empty element node from XML source has one empty DocumentTextElement', function() {
308                     var c = canvas.fromXML('<section></section>');
309                     expect(c.doc().children()).to.have.length(1);
310                     expect(c.doc().children()[0].getText()).to.equal('');
311                 });
312
313                 it('allows creation of an empty element node', function() {
314                     var c = canvas.fromXML('<section></section>'),
315                         section = c.doc(),
316                         header = section.append({tag: 'header'});
317                     expect(header.children()).to.have.length(0);
318                 });
319             });
320             
321             describe('white characters handling', function() {
322
323                 it('says element node with one space has one DocumentTextElement', function() {
324                     var c = canvas.fromXML('<section> </section>');
325                     expect(c.doc().children().length).to.equal(1);
326                     expect(c.doc().children()[0]).to.be.instanceOf(documentElement.DocumentTextElement);
327                     expect(c.doc().children()[0].getText()).to.equal(' ');
328                 });
329                 it('ignores white space surrounding block elements', function() {
330                     var c = canvas.fromXML('<section> <div></div> </section>');
331                     var children = c.doc().children();
332                     expect(children.length).to.equal(1);
333                     expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
334                 });
335                 it('ignores white space between block elements', function() {
336                     var c = canvas.fromXML('<section><div></div> <div></div></section>');
337                     var children = c.doc().children();
338                     expect(children.length === 2);
339                     [0,1].forEach(function(idx) {
340                         expect(children[idx]).to.be.instanceOf(documentElement.DocumentNodeElement);
341                     });
342                 });
343
344                 it('trims white space from the beginning and the end of the block elements', function() {
345                     var c = canvas.fromXML('<section> Alice <span>has</span> a cat </section>');
346                     expect(c.doc().children()[0].getText()).to.equal('Alice ');
347                     expect(c.doc().children()[2].getText()).to.equal(' a cat');
348                 });
349
350                 it('normalizes string of white characters to one space at the inline element boundries', function() {
351                     var c = canvas.fromXML('<span>   Alice has a cat   </span>');
352                     expect(c.doc().children()[0].getText()).to.equal(' Alice has a cat ');
353                 });
354
355                 it('normalizes string of white characters to one space before inline element', function() {
356                     var c = canvas.fromXML('<div>Alice has  <span>a cat</span></div>');
357                     expect(c.doc().children()[0].getText()).to.equal('Alice has ');
358                 });
359                 
360                 it('normalizes string of white characters to one space after inline element', function() {
361                     var c = canvas.fromXML('<div>Alice has <span>a</span>  cat</div>');
362                     expect(c.doc().children()[2].getText()).to.equal(' cat');
363                 });
364             });
365
366             describe('getting vertically first text element', function() {
367                 it('returns the first child if it\'s text element, ignores metadata', function() {
368                     var c = canvas.fromXML('<section><metadata><dc:author>author</dc:author></metadata>Alice<div>has</div>a cat</section>'),
369                         first = c.doc().getVerticallyFirstTextElement();
370
371                     expect(first.sameNode(c.doc().children()[1])).to.be.true;
372                 });
373
374                 it('looks recursively inside node elements if they precede text element', function() {
375                     var c = canvas.fromXML('\
376                             <section>\
377                                 <div>\
378                                     <div>\
379                                         Alice\
380                                     </div>\
381                                 </div>\
382                                 Some text\
383                             </section>'),
384                         textAlice = c.doc().children()[0].children()[0].children()[0],
385                         first = c.doc().getVerticallyFirstTextElement();
386
387                     expect(textAlice).to.be.instanceOf(documentElement.DocumentTextElement);
388                     expect(first.sameNode(textAlice)).to.be.true;
389                 });
390             });
391         });
392
393         describe('manipulation api', function() {
394
395             describe('Basic Element inserting', function() {
396                 it('can put new NodeElement at the end', function() {
397                     var c = canvas.fromXML('<section><div></div></section>'),
398                         appended = c.doc().append({tag: 'header', klass: 'some.class'}),
399                         children = c.doc().children();
400
401                     expect(children.length).to.equal(2);
402                     expect(children[1].sameNode(appended)).to.be.true;
403                 });
404
405                 it('can put new TextElement at the end', function() {
406                     var c = canvas.fromXML('<section><div><div></section>'),
407                         appended = c.doc().append({text: 'Alice'}),
408                         children = c.doc().children();
409
410                     expect(children.length).to.equal(2);
411                     expect(children[1].sameNode(appended)).to.be.true;
412                     expect(children[1].getText()).to.equal('Alice');
413                 });
414
415                 it('can put new NodeElement at the beginning', function() {
416                     var c = canvas.fromXML('<section><div></div></section>'),
417                         prepended = c.doc().prepend({tag: 'header', klass: 'some.class'}),
418                         children = c.doc().children();
419
420                     expect(children).to.have.length(2);
421                     expect(children[0].sameNode(prepended)).to.be.true;
422                 });
423
424                 it('can put new TextElement at the beginning', function() {
425                     var c = canvas.fromXML('<section><div></div></section>'),
426                         prepended = c.doc().prepend({text: 'Alice'}),
427                         children = c.doc().children();
428
429                     expect(children).to.have.length(2)
430                     expect(children[0].sameNode(prepended)).to.be.true;
431                     expect(children[0].getText()).to.equal('Alice');
432                 });
433
434                 it('can put new NodeElement after another NodeElement', function() {
435                     var c = canvas.fromXML('<section><div></div></section>'),
436                         div = c.doc().children()[0],
437                         added = div.after({tag: 'header', klass: 'some.class'}),
438                         children = c.doc().children();
439                     expect(children.length).to.equal(2);
440                     expect(children[1].sameNode(added)).to.be.true;
441                 });
442
443                 it('can put new Nodeelement before another element', function() {
444                     var c = canvas.fromXML('<section><div></div></section>'),
445                         div = c.doc().children()[0],
446                         added = div.before({tag: 'header', klass: 'some.class'}),
447                         children = c.doc().children();
448                     expect(children.length).to.equal(2);
449                     expect(children[0].sameNode(added)).to.be.true;
450                 });
451
452                 it('can put new DocumentNodeElement after DocumentTextElement', function() {
453                     var c = canvas.fromXML('<section>Alice</section>'),
454                         text = c.doc().children()[0],
455                         added = text.after({tag: 'p'}),
456                         children = c.doc().children();
457
458                     expect(children.length).to.equal(2);
459                     expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
460                     expect(children[0].getText()).to.equal('Alice');
461                     expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
462                     expect(children[1].sameNode(added)).to.be.true;
463                 });
464                 it('can put new DocumentNodeElement before DocumentTextElement', function() {
465                     var c = canvas.fromXML('<section>Alice</section>'),
466                         text = c.doc().children()[0],
467                         added = text.before({tag: 'p'}),
468                         children = c.doc().children();
469
470                     expect(children.length).to.equal(2);
471                     expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
472                     expect(children[0].sameNode(added)).to.be.true;
473                     expect(children[1]).to.be.instanceOf(documentElement.DocumentTextElement);
474                     expect(children[1].getText()).to.equal('Alice');
475                 });
476
477                 it('can divide DocumentTextElement with a new DocumentNodeElement', function() {
478                     var c = canvas.fromXML('<section>Alice has a cat</section>'),
479                         section = c.doc(),
480                         text = section.children()[0];
481
482                     var returned = text.divide({tag: 'aside', klass: 'footnote', offset: 5}),
483                         sectionChildren = section.children(),
484                         lhsText = sectionChildren[0],
485                         rhsText = sectionChildren[2];
486
487                     expect(lhsText.getText()).to.equal('Alice');
488                     expect(returned.sameNode(sectionChildren[1]));
489                     expect(rhsText.getText()).to.equal(' has a cat');
490                 });
491
492                 it('treats dividing DocumentTextElement at the very end as appending after it', function() {
493                     var c = canvas.fromXML('<section>Alice has a cat</section>'),
494                         section = c.doc(),
495                         text = section.children()[0];
496
497                     var returned = text.divide({tag: 'aside', klass: 'footnote', offset: 15}),
498                         sectionChildren = section.children(),
499                         textElement = sectionChildren[0],
500                         nodeElement = sectionChildren[1];
501
502                     expect(sectionChildren.length).to.equal(2);
503                     expect(textElement.getText()).to.equal('Alice has a cat');
504                     expect(returned.sameNode(nodeElement)).to.be.true;
505                     expect(nodeElement.getWlxmlTag()).to.equal('aside');
506                 });
507
508                 it('treats dividing DocumentTextElement at the very beginning as appending before it', function() {
509                     var c = canvas.fromXML('<section>Alice has a cat</section>'),
510                         section = c.doc(),
511                         text = section.children()[0];
512
513                     var returned = text.divide({tag: 'aside', klass: 'footnote', offset: 0}),
514                         sectionChildren = section.children(),
515                         nodeElement = sectionChildren[0],
516                         textElement = sectionChildren[1];
517                         
518                     expect(sectionChildren.length).to.equal(2);
519                     expect(textElement.getText()).to.equal('Alice has a cat');
520                     expect(returned.sameNode(nodeElement)).to.be.true;
521                     expect(nodeElement.getWlxmlTag()).to.equal('aside');
522                 });
523             });
524
525             describe('Removing elements', function() {
526                 it('merges left and right DocumentTextElement sibling of a detached DocumentNodeElement', function() {
527                     var c = canvas.fromXML('<section>Alice<div>has</div>a cat</section>'),
528                         section = c.doc(),
529                         div = section.children()[1];
530
531                     div.detach();
532
533                     var sectionChildren = section.children(),
534                         textElement = sectionChildren[0];
535
536                     expect(sectionChildren).to.have.length(1);
537                     expect(textElement.getText()).to.equal('Alicea cat');
538                 });
539             });
540
541             describe('Splitting text', function() {
542                 
543                 it('splits DocumentTextElement\'s parent into two DocumentNodeElements of the same type', function() {
544                     var c = canvas.fromXML('<section><header>Some header</header></section>'),
545                         section = c.doc(),
546                         text = section.children()[0].children()[0];
547
548                     var returnedValue = text.split({offset: 5});
549                     expect(section.children().length).to.equal(2, 'section has two children');
550                     
551                     var header1 = section.children()[0];
552                     var header2 = section.children()[1];
553
554                     expect(header1.getWlxmlTag()).to.equal('header', 'first section child represents wlxml header');
555                     expect(header1.children().length).to.equal(1, 'first header has one text child');
556                     expect(header1.children()[0].getText()).to.equal('Some ', 'first header has correct content');
557                     expect(header2.getWlxmlTag()).to.equal('header', 'second section child represents wlxml header');
558                     expect(header2.children().length).to.equal(1, 'second header has one text child');
559                     expect(header2.children()[0].getText()).to.equal('header', 'second header has correct content');
560
561                     expect(returnedValue.first.sameNode(header1)).to.equal(true, 'first node returnde');
562                     expect(returnedValue.second.sameNode(header2)).to.equal(true, 'second node returned');
563                 });
564
565                 it('leaves empty copy of DocumentNodeElement if splitting at the very beginning', function() {
566                         var c = canvas.fromXML('<section><header>Some header</header></section>'),
567                         section = c.doc(),
568                         text = section.children()[0].children()[0];
569
570                         text.split({offset: 0});
571                         
572                         var header1 = section.children()[0];
573                         var header2 = section.children()[1];
574
575                         expect(header1.children().length).to.equal(0);
576                         expect(header2.children()[0].getText()).to.equal('Some header');
577                 });
578
579                 it('leaves empty copy of DocumentNodeElement if splitting at the very end', function() {
580                         var c = canvas.fromXML('<section><header>Some header</header></section>'),
581                         section = c.doc(),
582                         text = section.children()[0].children()[0];
583
584                         text.split({offset: 11});
585                         
586                         var header1 = section.children()[0];
587                         var header2 = section.children()[1];
588
589                         expect(header1.children()[0].getText()).to.equal('Some header');
590                         expect(header2.children().length).to.equal(0);
591                 });
592
593                 it('keeps DocumentTextElement\'s parent\'s children elements intact', function() {
594                     var c = canvas.fromXML('\
595                             <section>\
596                                 <header>\
597                                     A <span>fancy</span> and <span>nice</span> header\
598                                 </header>\
599                             </section>'),
600                         section = c.doc(),
601                         header = section.children()[0],
602                         textAnd = header.children()[2];
603
604                     textAnd.split({offset: 2});
605                     
606                     var sectionChildren = section.children();
607                     expect(sectionChildren.length).to.equal(2, 'Section has two children');
608                     expect(sectionChildren[0].getWlxmlTag()).to.equal('header', 'First section element is a wlxml header');
609                     expect(sectionChildren[1].getWlxmlTag()).to.equal('header', 'Second section element is a wlxml header');
610
611                     var firstHeaderChildren = sectionChildren[0].children();
612                     expect(firstHeaderChildren.length).to.equal(3, 'First header has three children');
613                     expect(firstHeaderChildren[0].getText()).to.equal('A ', 'First header starts with a text');
614                     expect(firstHeaderChildren[1].getWlxmlTag()).to.equal('span', 'First header has span in the middle');
615                     expect(firstHeaderChildren[2].getText()).to.equal(' a', 'First header ends with text');
616
617                     var secondHeaderChildren = sectionChildren[1].children();
618                     expect(secondHeaderChildren.length).to.equal(3, 'Second header has three children');
619                     expect(secondHeaderChildren[0].getText()).to.equal('nd ', 'Second header starts with text');
620                     expect(secondHeaderChildren[1].getWlxmlTag()).to.equal('span', 'Second header has span in the middle');
621                     expect(secondHeaderChildren[2].getText()).to.equal(' header', 'Second header ends with text');
622                 });
623             });
624
625             describe('wrapping', function() {
626                 it('wraps DocumentNodeElement', function() {
627                     var c = canvas.fromXML('<section><div></div></section>'),
628                         div = c.doc().children()[0];
629                     
630                     var returned = div.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
631                         parent = div.parent(),
632                         parent2 = c.doc().children()[0];
633
634                     expect(returned.sameNode(parent)).to.be.true;
635                     expect(returned.sameNode(parent2)).to.be.true;
636                     expect(returned.getWlxmlTag()).to.equal('header');
637                     expect(returned.getWlxmlClass()).to.equal('some.class');
638                 });
639                 it('wraps DocumentTextElement', function() {
640                     var c = canvas.fromXML('<section>Alice</section>'),
641                         text = c.doc().children()[0];
642                     
643                     var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
644                         parent = text.parent(),
645                         parent2 = c.doc().children()[0];
646
647                     expect(returned.sameNode(parent)).to.be.true;
648                     expect(returned.sameNode(parent2)).to.be.true;
649                     expect(returned.getWlxmlTag()).to.equal('header');
650                     expect(returned.getWlxmlClass()).to.equal('some.class');
651                 });
652                 
653                 describe('wrapping part of DocumentTextElement', function() {
654                     [{start: 5, end: 12}, {start: 12, end: 5}].forEach(function(offsets) {
655                         it('wraps in the middle ' + offsets.start + '/' + offsets.end, function() {
656                             var c = canvas.fromXML('<section>Alice has a cat</section>'),
657                                 text = c.doc().children()[0];
658                             
659                             var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class', start: offsets.start, end: offsets.end}),
660                                 children = c.doc().children();
661
662                             expect(children.length).to.equal(3);
663                             
664                             expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
665                             expect(children[0].getText()).to.equal('Alice');
666
667                             expect(children[1].sameNode(returned)).to.be.true;
668                             expect(returned.getWlxmlTag()).to.equal('header');
669                             expect(returned.getWlxmlClass()).to.equal('some.class');
670                             expect(children[1].children().length).to.equal(1);
671                             expect(children[1].children()[0].getText()).to.equal(' has a ');
672
673                             expect(children[2]).to.be.instanceOf(documentElement.DocumentTextElement);
674                             expect(children[2].getText()).to.equal('cat');
675                         });
676                     });
677
678                     it('wraps whole text inside DocumentTextElement if offsets span entire content', function() {
679                          var c = canvas.fromXML('<section>Alice has a cat</section>'),
680                              text = c.doc().children()[0];
681                          
682                          var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class', start: 0, end: 15}),
683                              children = c.doc().children();
684
685                          expect(children.length).to.equal(1);
686                          expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
687                          expect(children[0].children()[0].getText()).to.equal('Alice has a cat');
688                     });
689                 });
690
691                 it('wraps text spanning multiple sibling DocumentTextNodes', function() {
692                     var c = canvas.fromXML('<section>Alice has a <span>small</span> cat</section>'),
693                         section = c.doc(),
694                         wrapper = c.wrapText({
695                             inside: section, 
696                             _with: {tag: 'span', klass: 'some.class'},
697                             offsetStart: 6,
698                             offsetEnd: 4,
699                             textNodeIdx: [0,2]
700                         });
701
702                     expect(section.children().length).to.equal(2);
703                     expect(section.children()[0]).to.be.instanceOf(documentElement.DocumentTextElement);
704                     expect(section.children()[0].getText()).to.equal('Alice ');
705
706                     var wrapper2 = section.children()[1];
707                     expect(wrapper2.sameNode(wrapper)).to.be.true;
708
709                     var wrapperChildren = wrapper.children();
710                     expect(wrapperChildren.length).to.equal(3);
711                     expect(wrapperChildren[0].getText()).to.equal('has a ');
712
713                     expect(wrapperChildren[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
714                     expect(wrapperChildren[1].children().length).to.equal(1);
715                     expect(wrapperChildren[1].children()[0].getText()).to.equal('small');
716
717                     expect(wrapperChildren[2].getText()).to.equal(' cat');
718                 });
719
720                 it('wraps multiple sibling Elements', function() {
721                     var c = canvas.fromXML('<section>Alice<div>has</div><div>a cat</div></section>'),
722                         section = c.doc(),
723                         aliceText = section.children()[0],
724                         firstDiv = section.children()[1],
725                         lastDiv = section.children()[section.children().length -1];
726
727                     var returned = c.wrapElements({
728                             element1: aliceText,
729                             element2: lastDiv,
730                             _with: {tag: 'header'}
731                         });
732
733                     var sectionChildren = section.children(),
734                         header = sectionChildren[0],
735                         headerChildren = header.children();
736
737                     expect(sectionChildren).to.have.length(1);
738                     expect(header.sameNode(returned)).to.equal(true, 'wrapper returned');
739                     expect(headerChildren).to.have.length(3);
740                     expect(headerChildren[0].sameNode(aliceText)).to.equal(true, 'first node wrapped');
741                     expect(headerChildren[1].sameNode(firstDiv)).to.equal(true, 'second node wrapped');
742                     expect(headerChildren[2].sameNode(lastDiv)).to.equal(true, 'third node wrapped');
743                 });
744                 it('wraps multiple sibling Elements - middle case', function() {
745                     var c = canvas.fromXML('<section><div></div>div></div><div></div><div></div></section>'),
746                         section = c.doc(),
747                         div1 = section.children()[0],
748                         div2 = section.children()[1],
749                         div3 = section.children()[2],
750                         div4 = section.children()[3];
751
752                     var returned = c.wrapElements({
753                             element1: div2,
754                             element2: div3,
755                             _with: {tag: 'header'}
756                         });
757
758                     var sectionChildren = section.children(),
759                         header = sectionChildren[1],
760                         headerChildren = header.children();
761
762                     expect(sectionChildren).to.have.length(3);
763                     expect(headerChildren).to.have.length(2);
764                     expect(headerChildren[0].sameNode(div2)).to.equal(true, 'first node wrapped');
765                     expect(headerChildren[1].sameNode(div3)).to.equal(true, 'second node wrapped');
766                 });
767             });
768
769             describe('unwrapping DocumentTextElement from its parent DocumentNodeElement if it\'s its only child', function() {
770                 it('unwraps text element from its parent and stays between its old parent siblings', function() {
771                     var c = canvas.fromXML('<section><div>Alice</div><div>has</div><div>a cat</div></section>'),
772                         section = c.doc(),
773                         sectionChildren = section.children(),
774                         divAlice = sectionChildren[0],
775                         divHas = sectionChildren[1],
776                         textHas = divHas.children()[0],
777                         divCat = sectionChildren[2];
778
779                     var newTextContainer = textHas.unwrap(),
780                         sectionChildren = section.children();
781
782                     expect(sectionChildren[0].sameNode(divAlice)).to.equal(true, 'divAlice ok');
783                     expect(newTextContainer.sameNode(section)).to.equal(true, 'unwrap returns new text parent DocumentNodeElement');
784                     expect(sectionChildren[1].getText()).to.equal('has');
785                     expect(sectionChildren[2].sameNode(divCat)).to.equal(true, 'divCat ok');
786
787                 });
788                 it('unwraps and join with its old parent adjacent text elements ', function() {
789                     var c = canvas.fromXML('<section>Alice <span>has a</span> cat</section>'),
790                     section = c.doc(),
791                     text = section.children()[1].children()[0];
792
793                     var newTextContainer = text.unwrap();
794
795                     expect(section.children().length).to.equal(1, 'section has one child');
796                     expect(section.children()[0].getText()).to.equal('Alice has a cat');
797                     expect(newTextContainer.sameNode(c.doc())).to.equal(true, 'unwrap returns new text parent DocumentNodeElement');
798                 });
799
800                 it('unwraps text element from its parent - first child case', function() {
801                     var c = canvas.fromXML('<section><span class="uri">Some</span>text</section>'),
802                         section = c.doc(),
803                         span = section.children()[0];
804
805                     span.children()[0].unwrap();
806
807                     var sectionChildren = section.children();
808
809                     expect(sectionChildren).to.have.length(1);
810                     expect(sectionChildren[0].getText()).to.equal('Sometext');
811                 });
812             });
813
814             describe('unwrapping the whole content of a DocumentNodeElement', function() {
815                 it('removes a DocumentNodeElement but keeps its content', function() {
816                     var c = canvas.fromXML('<section><div>Alice has<span>a</span> cat</div></section>'),
817                         section = c.doc(),
818                         div = c.doc().children()[0],
819                         span = div.children()[1];
820
821                     var range = div.unwrapContents(),
822                         sectionChildren = section.children();
823
824                     expect(sectionChildren).to.have.length(3);
825                     expect(sectionChildren[0].getText()).to.equal('Alice has');
826                     expect(sectionChildren[1].sameNode(span)).to.equal(true, 'span ok');
827                     expect(sectionChildren[2].getText()).to.equal(' cat');
828
829                     expect(range.element1.sameNode(sectionChildren[0])).to.equal(true, 'range start ok');
830                     expect(range.element2.sameNode(sectionChildren[2])).to.equal(true, 'range end ok');
831                 });
832                 it('merges text elements on the boundries', function() {
833                     var c = canvas.fromXML('<section>Alice<div>has a <span>cat</span>!</div>!!</section>'),
834                         section = c.doc(),
835                         div = c.doc().children()[1],
836                         span = div.children()[1];
837
838                     var range = div.unwrapContents(),
839                         sectionChildren = section.children();
840
841                     expect(sectionChildren).to.have.length(3);
842                     expect(sectionChildren[0].getText()).to.equal('Alicehas a ');
843                     expect(sectionChildren[1].sameNode(span)).to.equal(true, 'span ok');
844                     expect(sectionChildren[2].getText()).to.equal('!!!');
845
846                     expect(range.element1.sameNode(sectionChildren[0])).to.equal(true, 'range start ok');
847                     expect(range.element2.sameNode(sectionChildren[2])).to.equal(true, 'range end ok');
848                 });
849
850                 it('merges text elements on the boundries - single child case', function() {
851                     var c = canvas.fromXML('<section>Alice <span>has</span> a cat</section>'),
852                         section = c.doc(),
853                         span = section.children()[1];
854
855                     var range = span.unwrapContents(),
856                         sectionChildren = section.children();
857
858                     expect(sectionChildren).to.have.length(1);
859                     expect(sectionChildren[0].getText()).to.equal('Alice has a cat');
860                 });
861             });
862             
863         });
864
865         describe('Lists api', function() {
866             describe('creating lists', function() {
867                 it('allows creation of a list from existing sibling DocumentElements', function() {
868                     var c = canvas.fromXML('\
869                         <section>\
870                             Alice\
871                             <div>has</div>\
872                             a\
873                             <div>cat</div>\
874                         </section>'),
875                         section = c.doc(),
876                         textHas = section.children()[1],
877                         divA = section.children()[2]
878                     
879                     c.list.create({element1: textHas, element2: divA});
880
881                     expect(section.children().length).to.equal(3, 'section has three child elements');
882
883                     var child1 = section.children()[0],
884                         list = section.children()[1],
885                         child3 = section.children()[2];
886
887                     expect(child1.getText()).to.equal('Alice');
888                     expect(list.is('list')).to.equal(true, 'second child is a list');
889                     expect(list.children().length).to.equal(2, 'list contains two elements');
890                     list.children().forEach(function(child) {
891                         expect(child.getWlxmlClass()).to.equal('item', 'list childs have wlxml class of item');
892                     });
893                     expect(child3.children()[0].getText()).to.equal('cat');
894                 });
895                 
896                 it('allows creating nested list from existing sibling list items', function() {
897                     var c = canvas.fromXML('\
898                         <section>\
899                             <div class="list-items">\
900                                 <div class="item">A</div>\
901                                 <div class="item">B</div>\
902                                 <div class="item">C</div>\
903                                 <div class="item">D</div>\
904                             </div>\
905                         </section>'),
906                         outerList = c.doc().children()[0],
907                         itemB = outerList.children()[1],
908                         itemC = outerList.children()[2];
909
910
911                         c.list.create({element1: itemB, element2: itemC});
912
913                     var outerListItems = outerList.children(),
914                         innerList = outerListItems[1].children()[0],
915                         innerListItems = innerList.children();
916
917                     expect(outerListItems.length).to.equal(3, 'outer list has three items');
918                     expect(outerListItems[0].children()[0].getText()).to.equal('A', 'first outer item ok');
919                     expect(outerListItems[1].getWlxmlClass()).to.equal('item', 'inner list is wrapped by item element');
920
921                     expect(innerList.is('list')).to.equal(true, 'inner list created');
922                     expect(innerListItems.length).to.equal(2, 'inner list has two items');
923                     expect(innerListItems[0].children()[0].getText()).to.equal('B', 'first inner item ok');
924                     expect(innerListItems[1].children()[0].getText()).to.equal('C', 'second inner item ok');
925
926                     expect(outerListItems[2].children()[0].getText()).to.equal('D', 'last outer item ok');
927
928                 });
929
930             });
931
932             describe('extracting list items', function() {
933                 it('creates two lists with extracted items in the middle if extracting from the middle of the list', function() {
934                     var c = canvas.fromXML('\
935                         <section>\
936                             <div class="list.items">\
937                                 <div class="item">0</div>\
938                                 <div class="item">1</div>\
939                                 <div class="item">2</div>\
940                                 <div class="item">3</div>\
941                             </div>\
942                         </section>'),
943                         list = c.doc().children()[0],
944                         item1 = list.children()[1],
945                         item2 = list.children()[2];
946
947                     c.list.extractItems({element1: item1, element2: item2});
948
949                     var section = c.doc(),
950                         list1 = section.children()[0],
951                         oldItem1 = section.children()[1],
952                         oldItem2 = section.children()[2],
953                         list2 = section.children()[3];
954
955                     expect(section.children().length).to.equal(4, 'section contains four children');
956                     
957                     expect(list1.is('list')).to.equal(true, 'first section child is a list');
958                     expect(list1.children().length).to.equal(1, 'first list has one child');
959                     expect(list1.children()[0].children()[0].getText()).to.equal('0', 'first item of the first list is a first item of the original list');
960
961                     expect(oldItem1.children()[0].getText()).to.equal('1', 'first item got extracted');
962                     expect(oldItem1.getWlxmlClass() === undefined).to.equal(true, 'first extracted element has no wlxml class');
963
964                     expect(oldItem2.children()[0].getText()).to.equal('2', 'second item got extracted');
965                     expect(oldItem2.getWlxmlClass() === undefined).to.equal(true, 'second extracted element has no wlxml class');
966
967                     expect(list2.is('list')).to.equal(true, 'last section child is a list');
968                     expect(list2.children().length).to.equal(1, 'second list has one child');
969                     expect(list2.children()[0].children()[0].getText()).to.equal('3', 'first item of the second list is a last item of the original list');
970                 });
971
972                 it('puts extracted items above the list if starting item is the first one', function() {
973                     var c = canvas.fromXML('\
974                         <section>\
975                             <div class="list.items">\
976                                 <div class="item">0</div>\
977                                 <div class="item">1</div>\
978                                 <div class="item">2</div>\
979                             </div>\
980                         </section>'),
981                         list = c.doc().children()[0],
982                         item1 = list.children()[0],
983                         item2 = list.children()[1],
984                         item3 = list.children()[2];
985
986                     c.list.extractItems({element1: item1, element2: item2});
987
988                     var section = c.doc(),
989                         oldItem1 = section.children()[0],
990                         oldItem2 = section.children()[1],
991                         newList = section.children()[2];
992
993                     expect(section.children().length).to.equal(3, 'section has three children');
994                     expect(oldItem1.children()[0].getText()).to.equal('0', 'first item extracted');
995                     expect(oldItem2.children()[0].getText()).to.equal('1', 'second item extracted');
996                     expect(newList.is('list')).to.equal(true, 'list lies below extracted item');
997                     expect(newList.children().length).to.equal(1, 'list has now one child');
998                 });
999
1000                 it('puts extracted items below the list if ending item is the last one', function() {
1001                     var c = canvas.fromXML('\
1002                         <section>\
1003                             <div class="list.items">\
1004                                 <div class="item">0</div>\
1005                                 <div class="item">1</div>\
1006                                 <div class="item">2</div>\
1007                             </div>\
1008                         </section>'),
1009                         list = c.doc().children()[0],
1010                         item1 = list.children()[0],
1011                         item2 = list.children()[1],
1012                         item3 = list.children()[2];
1013
1014                     c.list.extractItems({element1: item2, element2: item3});
1015
1016                     var section = c.doc(),
1017                         oldItem1 = section.children()[1],
1018                         oldItem2 = section.children()[2],
1019                         newList = section.children()[0];
1020
1021                     expect(section.children().length).to.equal(3, 'section has three children');
1022                     expect(oldItem1.children()[0].getText()).to.equal('1', 'first item extracted');
1023                     expect(oldItem2.children()[0].getText()).to.equal('2', 'second item extracted');
1024                     expect(newList.is('list')).to.equal(true, 'list lies above extracted item');
1025                     expect(newList.children().length).to.equal(1, 'list has now one child');
1026                 });
1027
1028                 it('removes list if all its items are extracted', function() {
1029                     var c = canvas.fromXML('\
1030                         <section>\
1031                             <div class="list.items">\
1032                                 <div class="item">some item</div>\
1033                                 <div class="item">some item 2</div>\
1034                             </div>\
1035                         </section>'),
1036                         list = c.doc().children()[0],
1037                         item1 = list.children()[0],
1038                         item2 = list.children()[1];
1039
1040                     c.list.extractItems({element1: item1, element2: item2});
1041
1042                     var section = c.doc(),
1043                         list1 = section.children()[0],
1044                         oldItem1 = section.children()[0],
1045                         oldItem2 = section.children()[1];
1046
1047                     expect(section.children().length).to.equal(2, 'section contains two children');
1048                     expect(oldItem1.children()[0].getText()).to.equal('some item');
1049                     expect(oldItem2.children()[0].getText()).to.equal('some item 2');
1050                 });
1051
1052                 it('creates two lists with extracted items in the middle if extracting from the middle of the list - nested case' , function() {
1053                     var c = canvas.fromXML('\
1054                         <section>\
1055                             <div class="list.items">\
1056                                 <div class="item">0</div>\
1057                                 <div class="item">\
1058                                     <div class="list.items">\
1059                                         <div class="item">1.1</div>\
1060                                         <div class="item">1.2</div>\
1061                                         <div class="item">1.3</div>\
1062                                     </div>\
1063                                 </div>\
1064                                 <div class="item">2</div>\
1065                             </div>\
1066                         </section>'),
1067                         list = c.doc().children()[0],
1068                         nestedList = list.children()[1].children()[0],
1069                         nestedListItem = nestedList.children()[1];
1070
1071                     c.list.extractItems({element1: nestedListItem, element2: nestedListItem});
1072
1073                     var section = c.doc(),
1074                         list = section.children()[0],
1075                         item1 = list.children()[0],
1076                         item2 = list.children()[1], //
1077                         item3 = list.children()[2],
1078                         item4 = list.children()[3], //
1079                         item5 = list.children()[4],
1080                         nestedList1 = item2.children()[0],
1081                         nestedList2 = item4.children()[0];
1082
1083                     expect(list.children().length).to.equal(5, 'top list has five items');
1084                     
1085                     expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
1086
1087                     expect(item2.getWlxmlClass()).to.equal('item', 'first nested list is still wrapped in item element');
1088                     expect(nestedList1.children().length).to.equal(1, 'first nested list is left with one child');
1089                     expect(nestedList1.children()[0].children()[0].getText()).to.equal('1.1', 'first nested list item left alone');
1090                     
1091                     expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
1092
1093                     expect(item4.getWlxmlClass()).to.equal('item', 'second nested list is still wrapped in item element');
1094                     expect(nestedList2.children().length).to.equal(1, 'second nested list is left with one child');
1095                     expect(nestedList2.children()[0].children()[0].getText()).to.equal('1.3', 'second nested list item left alone');
1096
1097                     expect(item5.children()[0].getText()).to.equal('2', 'last item ok');
1098                 });
1099
1100                 it('puts extracted items below the list if ending item is the last one - nested case' , function() {
1101                     var c = canvas.fromXML('\
1102                         <section>\
1103                             <div class="list.items">\
1104                                 <div class="item">0</div>\
1105                                 <div class="item">\
1106                                     <div class="list.items">\
1107                                         <div class="item">1.1</div>\
1108                                         <div class="item">1.2</div>\
1109                                         <div class="item">1.3</div>\
1110                                     </div>\
1111                                 </div>\
1112                                 <div class="item">2</div>\
1113                             </div>\
1114                         </section>'),
1115                         list = c.doc().children()[0],
1116                         nestedList = list.children()[1].children()[0],
1117                         nestedListItem1 = nestedList.children()[1],
1118                         nestedListItem2 = nestedList.children()[2];
1119
1120                     c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
1121
1122                     var section = c.doc(),
1123                         list = section.children()[0],
1124                         item1 = list.children()[0],
1125                         item2 = list.children()[1],
1126                         item3 = list.children()[2],
1127                         item4 = list.children()[3],
1128                         item5 = list.children()[4];
1129                     nestedList = item2.children()[0];
1130
1131                     expect(list.children().length).to.equal(5, 'top list has five items');
1132                     expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
1133                     expect(item2.getWlxmlClass()).to.equal('item', 'nested list is still wrapped in item element');
1134                     expect(nestedList.children().length).to.equal(1, 'nested list is left with one child');
1135                     expect(nestedList.children()[0].children()[0].getText()).to.equal('1.1', 'nested list item left alone');
1136                     expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
1137                     expect(item4.children()[0].getText()).to.equal('1.3', 'fourth item ok');
1138                     expect(item5.children()[0].getText()).to.equal('2', 'fifth item ok');
1139                 });
1140
1141                 it('puts extracted items above the list if starting item is the first one - nested case' , function() {
1142                     var c = canvas.fromXML('\
1143                         <section>\
1144                             <div class="list.items">\
1145                                 <div class="item">0</div>\
1146                                 <div class="item">\
1147                                     <div class="list.items">\
1148                                         <div class="item">1.1</div>\
1149                                         <div class="item">1.2</div>\
1150                                         <div class="item">1.3</div>\
1151                                     </div>\
1152                                 </div>\
1153                                 <div class="item">2</div>\
1154                             </div>\
1155                         </section>'),
1156                         list = c.doc().children()[0],
1157                         nestedList = list.children()[1].children()[0],
1158                         nestedListItem1 = nestedList.children()[0],
1159                         nestedListItem2 = nestedList.children()[1];
1160
1161                     c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
1162
1163                     var section = c.doc(),
1164                         list = section.children()[0],
1165                         item1 = list.children()[0],
1166                         item2 = list.children()[1],
1167                         item3 = list.children()[2],
1168                         item4 = list.children()[3],
1169                         item5 = list.children()[4];
1170                     nestedList = item4.children()[0];
1171
1172                     expect(list.children().length).to.equal(5, 'top list has five items');
1173                     expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
1174                     expect(item2.children()[0].getText()).to.equal('1.1', 'second item ok');
1175                     expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
1176                     
1177                     expect(item4.getWlxmlClass()).to.equal('item', 'nested list is still wrapped in item element');
1178                     expect(nestedList.children().length).to.equal(1, 'nested list is left with one child');
1179                     expect(nestedList.children()[0].children()[0].getText()).to.equal('1.3', 'nested list item left alone');
1180                     expect(item5.children()[0].getText()).to.equal('2', 'fifth item ok');
1181                 });
1182
1183                 it('removes list if all its items are extracted - nested case', function() {
1184                     var c = canvas.fromXML('\
1185                         <section>\
1186                             <div class="list.items">\
1187                                 <div class="item">0</div>\
1188                                 <div class="item">\
1189                                     <div class="list.items">\
1190                                         <div class="item">1.1</div>\
1191                                         <div class="item">1.2</div>\
1192                                     </div>\
1193                                 </div>\
1194                                 <div class="item">2</div>\
1195                             </div>\
1196                         </section>'),
1197                         list = c.doc().children()[0],
1198                         nestedList = list.children()[1].children()[0],
1199                         nestedListItem1 = nestedList.children()[0],
1200                         nestedListItem2 = nestedList.children()[1];
1201
1202                     c.list.extractItems({element1: nestedListItem1, element2: nestedListItem2});
1203
1204                     var section = c.doc(),
1205                         list = section.children()[0],
1206                         item1 = list.children()[0],
1207                         item2 = list.children()[1],
1208                         item3 = list.children()[2],
1209                         item4 = list.children()[3];
1210
1211                     expect(list.children().length).to.equal(4, 'top list has four items');
1212                     expect(item1.children()[0].getText()).to.equal('0', 'first item ok');
1213                     expect(item2.children()[0].getText()).to.equal('1.1', 'second item ok');
1214                     expect(item3.children()[0].getText()).to.equal('1.2', 'third item ok');
1215                     expect(item4.children()[0].getText()).to.equal('2', 'fourth item ok');
1216                 });
1217
1218                 it('extracts items out of outer most list when merge flag is set to false', function() {
1219                     var c = canvas.fromXML('\
1220                         <section>\
1221                             <div class="list.items">\
1222                                 <div class="item">0</div>\
1223                                 <div class="item">\
1224                                     <div class="list.items">\
1225                                         <div class="item">1.1</div>\
1226                                         <div class="item">1.2</div>\
1227                                     </div>\
1228                                 </div>\
1229                                 <div class="item">2</div>\
1230                             </div>\
1231                         </section>'),
1232                         section = c.doc(),
1233                         list = section.children()[0],
1234                         nestedList = list.children()[1].children()[0],
1235                         nestedListItem = nestedList.children()[0];
1236
1237                     var test = c.list.extractItems({element1: nestedListItem, element2: nestedListItem, merge: false});
1238
1239                     expect(test).to.equal(true, 'extraction status ok');
1240
1241                     var sectionChildren = section.children(),
1242                         extractedItem = sectionChildren[1];
1243
1244                     expect(sectionChildren.length).to.equal(3, 'section has three children');
1245                     expect(sectionChildren[0].is('list')).to.equal(true, 'first child is a list');
1246
1247                     expect(extractedItem.getWlxmlTag()).to.equal('div', 'extracted item is a wlxml div');
1248                     expect(extractedItem.getWlxmlClass()).to.equal(undefined, 'extracted item has no wlxml class');
1249                     expect(extractedItem.children()[0].getText()).to.equal('1.1', 'extracted item ok');
1250                     expect(sectionChildren[2].is('list')).to.equal(true, 'second child is a list');
1251                 });
1252             });
1253         });
1254
1255     });
1256
1257     describe('Cursor', function() {
1258
1259         var getSelection;
1260
1261         var findTextNode = function(inside, text) {
1262             var nodes = inside.find(':not(iframe)').addBack().contents().filter(function() {
1263                 return this.nodeType === Node.TEXT_NODE && this.data === text;
1264             });
1265             if(nodes.length)
1266                 return nodes[0];
1267             return null;
1268         }
1269
1270         beforeEach(function() {
1271             getSelection = sinon.stub(window, 'getSelection');
1272         });
1273
1274         afterEach(function() {
1275             getSelection.restore();
1276         });
1277
1278         it('returns position when browser selection collapsed', function() {
1279             var c = canvas.fromXML('<section>Alice has a cat</section>'),
1280                 dom = c.doc().dom(),
1281                 text = findTextNode(dom, 'Alice has a cat');
1282
1283             expect(text.nodeType).to.equal(Node.TEXT_NODE, 'correct node selected');
1284             expect($(text).text()).to.equal('Alice has a cat');
1285
1286             getSelection.returns({
1287                 anchorNode: text,
1288                 focusNode: text,
1289                 anchorOffset: 5,
1290                 focusOffset: 5,
1291                 isCollapsed: true
1292             });
1293             var cursor = c.getCursor(),
1294                 position = cursor.getPosition();
1295
1296             expect(cursor.isSelecting()).to.equal(false, 'cursor is not selecting anything');
1297             expect(position.element.getText()).to.equal('Alice has a cat');
1298             expect(position.offset).to.equal(5);
1299             expect(position.offsetAtEnd).to.equal(false, 'offset is not at end');
1300
1301             getSelection.returns({
1302                 anchorNode: text,
1303                 focusNode: text,
1304                 anchorOffset: 15,
1305                 focusOffset: 15,
1306                 isCollapsed: true
1307             });
1308
1309             expect(cursor.getPosition().offsetAtEnd).to.equal(true, 'offset at end');
1310         });
1311
1312         it('returns boundries of selection when browser selection not collapsed', function() {
1313             var c = canvas.fromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
1314                 dom = c.doc().dom(),
1315                 text = {
1316                     alice: findTextNode(dom, 'Alice '),
1317                     has: findTextNode(dom, 'has'),
1318                     cat: findTextNode(dom, ' cat')
1319                 },
1320                 cursor = c.getCursor(),
1321                 aliceElement = c.getDocumentElement(text.alice),
1322                 catElement = c.getDocumentElement(text.cat);
1323
1324
1325                 [
1326                     {focus: text.alice, focusOffset: 1, anchor: text.cat,   anchorOffset: 2, selectionAnchor: catElement},
1327                     {focus: text.cat,   focusOffset: 2, anchor: text.alice, anchorOffset: 1, selectionAnchor: aliceElement}
1328                 ].forEach(function(s, idx) {
1329                     getSelection.returns({isColapsed: false, anchorNode: s.anchor, anchorOffset: s.anchorOffset, focusNode: s.focus, focusOffset: s.focusOffset});
1330
1331                     var selectionStart = cursor.getSelectionStart(),
1332                         selectionEnd = cursor.getSelectionEnd(),
1333                         selectionAnchor = cursor.getSelectionAnchor();
1334
1335                     expect(cursor.isSelecting()).to.equal(true, 'cursor is selecting');
1336                     expect(selectionStart.element.sameNode(aliceElement)).to.equal(true, '"Alice" is the start of the selection ' + idx);
1337                     expect(selectionStart.offset).to.equal(1, '"Alice" offset ok' + idx);
1338                     expect(selectionEnd.element.sameNode(catElement)).to.equal(true, '"Cat" is the start of the selection ' + idx);
1339                     expect(selectionEnd.offset).to.equal(2, '"Cat" offset ok' + idx);
1340                     expect(selectionAnchor.element.sameNode(s.selectionAnchor)).to.equal(true, 'anchor ok');
1341                     expect(selectionAnchor.offset).to.equal(s.anchorOffset, 'anchor offset ok');
1342                 });
1343         });
1344
1345         it('recognizes when browser selection boundries lies in sibling DocumentTextElements', function() {
1346             var c = canvas.fromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
1347                 dom = c.doc().dom(),
1348                 text = {
1349                     alice: findTextNode(dom, 'Alice '),
1350                     has: findTextNode(dom, 'has'),
1351                     a: findTextNode(dom, ' a '),
1352                     big: findTextNode(dom, 'big'),
1353                     cat: findTextNode(dom, ' cat'),
1354                 },
1355                 cursor = c.getCursor();
1356
1357             expect($(text.alice).text()).to.equal('Alice ');
1358             expect($(text.has).text()).to.equal('has');
1359             expect($(text.a).text()).to.equal(' a ');
1360             expect($(text.big).text()).to.equal('big');
1361             expect($(text.cat).text()).to.equal(' cat');
1362
1363             getSelection.returns({anchorNode: text.alice, focusNode: text.a});
1364             expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "a" are children');
1365
1366             getSelection.returns({anchorNode: text.alice, focusNode: text.cat});
1367             expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "cat" are children');
1368
1369             getSelection.returns({anchorNode: text.alice, focusNode: text.has});
1370             expect(cursor.isSelectingSiblings()).to.equal(false, '"Alice" and "has" are not children');
1371
1372             getSelection.returns({anchorNode: text.has, focusNode: text.big});
1373             expect(cursor.isSelectingSiblings()).to.equal(false, '"has" and "big" are not children');
1374             
1375         })
1376     });
1377
1378     describe('Serializing document to WLXML', function() {
1379         it('keeps document intact when no changes have been made', function() {
1380             var xmlIn = '<section>Alice<div>has</div>a <span class="uri" meta-uri="http://cat.com">cat</span>!</section>',
1381                 c = canvas.fromXML(xmlIn),
1382                 xmlOut = c.toXML();
1383
1384             var parser = new DOMParser(),
1385                 input = parser.parseFromString(xmlIn, "application/xml").childNodes[0],
1386                 output = parser.parseFromString(xmlOut, "application/xml").childNodes[0];
1387             
1388             expect(input.isEqualNode(output)).to.be.true;
1389         });
1390
1391         it('keeps arbitrary node attributes intact', function() {
1392             var xmlIn = '<section a="1" xmlns:dcterms="http://purl.org/dc/terms/"></section>',
1393                 $xmlOut = $(canvas.fromXML(xmlIn).toXML());
1394
1395             expect($xmlOut.attr('a')).to.equal('1');
1396             expect($xmlOut.attr('xmlns:dcterms')).to.equal('http://purl.org/dc/terms/');
1397         });
1398
1399         it('doesn\' serialize meta attribute if its empty', function() {
1400             var c;
1401
1402             c = canvas.fromXML('<section class="uri" meta-uri="some.uri"></section>');
1403             c.doc().setWlxmlMetaAttr('uri', '');
1404             expect($(c.toXML()).attr('meta-uri')).to.equal(undefined, 'overriding attribute with zero length string');
1405
1406             c = canvas.fromXML('<section class="uri"></section>');
1407             c.doc().setWlxmlMetaAttr('uri', '');
1408             expect($(c.toXML()).attr('meta-uri')).to.equal(undefined, 'setting attribute to zero length string');
1409         });
1410
1411         describe('output xml', function() {
1412             it('keeps entities intact', function() {
1413                 var xmlIn = '<section>&lt; &gt;</section>',
1414                     c = canvas.fromXML(xmlIn),
1415                     xmlOut = c.toXML();
1416                 expect(xmlOut).to.equal(xmlIn);
1417             });
1418             it('keeps entities intact when they form html/xml', function() {
1419                 var xmlIn = '<section>&lt;abc&gt;</section>',
1420                     c = canvas.fromXML(xmlIn),
1421                     xmlOut = c.toXML();
1422                 expect(xmlOut).to.equal(xmlIn);
1423             });
1424         });
1425
1426         describe('formatting output xml', function() {
1427             /*it('keeps white spaces at the edges of input xml', function() {
1428                 var xmlIn = '  <section></section>  ',
1429                 c = canvas.fromXML(xmlIn),
1430                 xmlOut = c.toXML();
1431
1432                 expect(xmlOut.substr(4)).to.equal('   <', 'start');
1433                 expect(xmlOut.substr(-2)).to.equal('>  ', 'end');
1434             });*/
1435             it('keeps white space between XML nodes', function() {
1436                 var xmlIn = '<section>\n\n\n<div></div>\n\n\n<div></div>\n\n\n</section>',
1437                 c = canvas.fromXML(xmlIn),
1438                 xmlOut = c.toXML();
1439
1440                 var partsIn = xmlIn.split('\n\n\n'),
1441                     partsOut = xmlOut.split('\n\n\n');
1442                 
1443                 expect(partsIn).to.deep.equal(partsOut);
1444             });
1445
1446             it('keeps white space between XML nodes - inline case', function() {
1447                 var xmlIn = '<section>\n\n\n<span></span>\n\n\n<span></span>\n\n\n</section>',
1448                 c = canvas.fromXML(xmlIn);
1449                 
1450                 var xmlOut = c.toXML();
1451
1452                 var partsIn = xmlIn.split('\n\n\n'),
1453                     partsOut = xmlOut.split('\n\n\n');
1454                 
1455                 expect(partsIn).to.deep.equal(partsOut);
1456             });
1457
1458             it('keeps white space at the beginning of text', function() {
1459                 var xmlIn = '<section>    abc<div>some div</div>    abc</section>',
1460                     c = canvas.fromXML(xmlIn),
1461                     xmlOut = c.toXML();
1462
1463                 expect(xmlOut).to.equal(xmlIn);
1464             });
1465
1466             it('nests new children block elements', function() {
1467                 var c = canvas.fromXML('<section></section>');
1468     
1469                 c.doc().append({tag: 'header'});
1470
1471                 var xmlOut = c.toXML();
1472                 expect(xmlOut.split('\n  ')[0]).to.equal('<section>', 'nesting start ok');
1473                 expect(xmlOut.split('\n').slice(-1)[0]).to.equal('</section>', 'nesting end ok');
1474
1475             });
1476
1477             it('doesn\'t nest new children inline elements', function() {
1478                 var c = canvas.fromXML('<section></section>');
1479     
1480                 c.doc().append({tag: 'span'});
1481
1482                 var xmlOut = c.toXML();
1483                 expect(xmlOut).to.equal('<section><span></span></section>');
1484             });
1485
1486             it('keeps original white space at the end of text', function() {
1487                 
1488                 var xmlIn = '<header>    Some text ended with white space \
1489                 \
1490                 <span class="uri">Some text</span> some text\
1491             \
1492             </header>',
1493                     c = canvas.fromXML(xmlIn);
1494
1495             var xmlOut = c.toXML();
1496             console.log(xmlOut);
1497             expect(xmlOut).to.equal(xmlIn);
1498             });
1499
1500             it('keeps white space around text node', function() {
1501                 var xmlIn = '<section>\
1502                 <header>header1</header>\
1503                 Some text surrounded by white space\
1504                 <header>header2</header>\
1505             </section>',
1506                     c = canvas.fromXML(xmlIn);
1507
1508                 var xmlOut = c.toXML();
1509                 expect(xmlOut).to.equal(xmlIn);
1510             });
1511
1512             it('keeps white space around text node - last node case', function() {
1513                 var xmlIn = '<section>\
1514                 <header>header</header>\
1515                     \
1516                 Some text surrounded by white space\
1517                     \
1518             </section>',
1519                     c = canvas.fromXML(xmlIn);
1520
1521                 var xmlOut = c.toXML();
1522                 expect(xmlOut).to.equal(xmlIn);
1523             });
1524
1525             it('keeps white space after detaching text element', function() {
1526                 var xmlIn = '<section><header>header</header>\n\
1527                     \n\
1528                 text1\n\
1529                     \n\
1530             </section>',
1531                     expectedXmlOut = '<section><header>header</header>\n\
1532                     \n\
1533                 \n\
1534                     \n\
1535             </section>',
1536                     c = canvas.fromXML(xmlIn),
1537                     children = c.doc().children(),
1538                     text = children[children.length-1];
1539                 
1540                 expect(text.getText()).to.equal('text1');
1541
1542                 text.detach();
1543
1544                 var xmlOut = c.toXML();
1545                 expect(xmlOut).to.equal(expectedXmlOut);
1546             });
1547
1548         })
1549     })
1550 });
1551
1552
1553 });