3 'modules/documentCanvas/canvas/canvas',
 
   4 'modules/documentCanvas/canvas/documentElement'
 
   5 ], function(chai, canvas, documentElement) {
 
   9 var expect = chai.expect;
 
  12 describe('Canvas', function() {
 
  14     describe('Internal HTML representation of a DocumentNodeElement', function() {
 
  15         it('is always a div tag', function() {
 
  16             ['section', 'header', 'span', 'aside', 'figure'].forEach(function(tagName) {
 
  17                 var dom = canvas.fromXML('<' + tagName + '></' + tagName + '>').doc().dom();
 
  18                 expect(dom.prop('tagName')).to.equal('DIV', tagName + ' is represented as div');
 
  21         it('has wlxml tag put into wlxml-tag attribute', function() {
 
  22             var dom = canvas.fromXML('<section></section>').doc().dom();
 
  23             expect(dom.attr('wlxml-tag')).to.equal('section');
 
  25         it('has wlxml class put into wlxml-class, dots replaced with dashes', function() {
 
  26             var dom = canvas.fromXML('<section class="some.class"></section>').doc().dom();
 
  27             expect(dom.attr('wlxml-class')).to.equal('some-class');
 
  31     describe('Internal HTML representation of a DocumentTextElement', function() {
 
  32         it('is just a TextNode', function() {
 
  33             var dom = canvas.fromXML('<section>Alice</section>').doc().children()[0].dom();
 
  34             expect(dom[0].nodeType === Node.TEXT_NODE);
 
  38     describe('basic properties', function() {
 
  39         it('renders empty document when canvas created from empty XML', function() {
 
  40             var c = canvas.fromXML('');
 
  41             expect(c.doc()).to.equal(null);
 
  44         it('gives access to its document root node', function() {
 
  45             var c = canvas.fromXML('<section></section>');
 
  46             expect(c.doc().wlxmlTag).to.equal('section');
 
  49         describe('DocumentTextElement', function() {
 
  50             it('can have its content set', function() {
 
  51                 var c = canvas.fromXML('<section>Alice</section>'),
 
  53                     text = root.children()[0];
 
  55                 text.setText('a cat');
 
  56                 expect(root.children()[0].getText()).to.equal('a cat');
 
  60         describe('DocumentNodeElement', function() {
 
  61             it('knows index of its child', function() {
 
  62                 var c = canvas.fromXML('<section><div></div><header></header><span></span></section>'),
 
  64                     child = root.children()[1];
 
  65                 expect(root.childIndex(child)).to.equal(1);
 
  68             it('knows WLXML tag it renders', function(){
 
  69                 var c = canvas.fromXML('<section></section>'),
 
  71                 expect(section.getWlxmlTag()).to.equal('section', 'initial tag is section');
 
  72                 section.setWlxmlTag('header');
 
  73                 expect(section.getWlxmlTag()).to.equal('header', 'tag is changed to header');
 
  76             it('knows WLXML class of a WLXML tag it renders', function(){
 
  77                 var c = canvas.fromXML('<section class="some.class"></section>'),
 
  79                 expect(section.getWlxmlClass()).to.equal('some.class');
 
  80                 section.setWlxmlClass('some.other.class');
 
  81                 expect(section.getWlxmlClass()).to.equal('some.other.class');
 
  88     describe('document representation api', function() {
 
  89         describe('document root element', function() {
 
  90             var c = canvas.fromXML('<section></section>');
 
  91             it('exists', function() {
 
  92                 expect(c.doc()).to.be.instanceOf(documentElement.DocumentElement);
 
  94             it('is of type DocumentNodeElement', function() {
 
  95                 expect(c.doc()).to.be.instanceOf(documentElement.DocumentNodeElement);
 
  99         describe('DocumentElements comparison', function() {
 
 100             it('reports dwo DocumentElements to be the same when they represent the same wlxml document element', function() {
 
 101                 var c = canvas.fromXML('<section><div></div><div></div></section>'),
 
 102                     first_div1 = c.doc().children()[0],
 
 103                     first_div2 = c.doc().children()[0],
 
 104                     second_div = c.doc().children()[1];
 
 105                 expect(first_div1.sameNode(first_div1)).to.be.true;
 
 106                 expect(first_div1.sameNode(first_div2)).to.be.true;
 
 107                 expect(first_div1.sameNode(second_div)).to.be.false;
 
 111         describe('traversing', function() {
 
 112             it('reports element nodes', function() {
 
 113                 var c = canvas.fromXML('<section><div></div></section>'),
 
 114                     children = c.doc().children();
 
 115                 expect(children.length).to.equal(1);
 
 116                 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
 
 118                 c = canvas.fromXML('<section><div></div><div></div></section>'),
 
 119                     children = c.doc().children();
 
 120                 expect(children.length).to.equal(2);
 
 121                 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
 
 122                 expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
 
 125             it('reports text nodes', function() {
 
 126                 var c = canvas.fromXML('<section>Alice</section>'),
 
 127                     children = c.doc().children();
 
 128                 expect(children.length).to.equal(1);
 
 129                 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
 
 132             describe('accessing parents', function() {
 
 133                 it('returns DocumentNodeElement representing parent in wlxml document as DocumentNodeElement parent', function() {
 
 134                     var c = canvas.fromXML('<section><div></div></section>'),
 
 135                         div = c.doc().children()[0];
 
 136                     expect(div.parent().sameNode(c.doc())).to.be.true;
 
 138                 it('returns DocumentNodeElement representing parent in wlxml document as DocumentTextElement parent', function() {
 
 139                     var c = canvas.fromXML('<section>Alice</section>'),
 
 140                         text = c.doc().children()[0];
 
 141                     expect(text.parent().sameNode(c.doc())).to.be.true;
 
 145             describe('free text handling', function() {
 
 146                     it('sees free text', function() {
 
 147                         var c = canvas.fromXML('<section>Alice <span>has</span> a cat</section>'),
 
 148                             children = c.doc().children();
 
 149                         expect(children.length).to.equal(3);
 
 150                         expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
 
 151                         expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
 
 152                         expect(children[2]).to.be.instanceOf(documentElement.DocumentTextElement);
 
 156             describe('white characters handling', function() {
 
 157                 it('says empty element node has no children', function() {
 
 158                     var c = canvas.fromXML('<section></section>');
 
 159                     expect(c.doc().children().length).to.equal(0);
 
 161                 it('says element node with one space has one DocumentTextElement', function() {
 
 162                     var c = canvas.fromXML('<section> </section>');
 
 163                     expect(c.doc().children().length).to.equal(1);
 
 164                     expect(c.doc().children()[0]).to.be.instanceOf(documentElement.DocumentTextElement);
 
 166                 it('ignores white space surrounding block elements', function() {
 
 167                     var c = canvas.fromXML('<section> <div></div> </section>');
 
 168                     var children = c.doc().children();
 
 169                     expect(children.length).to.equal(1);
 
 170                     expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
 
 172                 it('ignores white space between block elements', function() {
 
 173                     var c = canvas.fromXML('<section><div></div> <div></div></section>');
 
 174                     var children = c.doc().children();
 
 175                     expect(children.length === 2);
 
 176                     [0,1].forEach(function(idx) {
 
 177                         expect(children[idx]).to.be.instanceOf(documentElement.DocumentNodeElement);
 
 181                 it('trims white space from the beginning and the end of the block elements', function() {
 
 182                     var c = canvas.fromXML('<section> Alice <span>has</span> a cat </section>');
 
 183                     expect(c.doc().children()[0].getText()).to.equal('Alice ');
 
 184                     expect(c.doc().children()[2].getText()).to.equal(' a cat');
 
 187                 it('normalizes string of white characters to one space at the inline element boundries', function() {
 
 188                     var c = canvas.fromXML('<span>   Alice has a cat   </span>');
 
 189                     expect(c.doc().children()[0].getText()).to.equal(' Alice has a cat ');
 
 192                 it('normalizes string of white characters to one space before inline element', function() {
 
 193                     var c = canvas.fromXML('<div>Alice has  <span>a cat</span></div>');
 
 194                     expect(c.doc().children()[0].getText()).to.equal('Alice has ');
 
 197                 it('normalizes string of white characters to one space after inline element', function() {
 
 198                     var c = canvas.fromXML('<div>Alice has <span>a</span>  cat</div>');
 
 199                     expect(c.doc().children()[2].getText()).to.equal(' cat');
 
 204         describe('manipulation api', function() {
 
 206             describe('Basic Element inserting', function() {
 
 207                 it('can put new NodeElement at the end', function() {
 
 208                     var c = canvas.fromXML('<section></section>'),
 
 209                         appended = c.doc().append({tag: 'header', klass: 'some.class'}),
 
 210                         children = c.doc().children();
 
 212                     expect(children.length).to.equal(1);
 
 213                     expect(children[0].sameNode(appended));
 
 216                 it('can put new TextElement at the end', function() {
 
 217                     var c = canvas.fromXML('<section></section>'),
 
 218                         appended = c.doc().append({text: 'Alice'}),
 
 219                         children = c.doc().children();
 
 221                     expect(children.length).to.equal(1);
 
 222                     expect(children[0].sameNode(appended));
 
 223                     expect(children[0].getText()).to.equal('Alice');
 
 226                 it('can put new NodeElement after another NodeElement', function() {
 
 227                     var c = canvas.fromXML('<section><div></div></section>'),
 
 228                         div = c.doc().children()[0],
 
 229                         added = div.after({tag: 'header', klass: 'some.class'}),
 
 230                         children = c.doc().children();
 
 231                     expect(children.length).to.equal(2);
 
 232                     expect(children[1].sameNode(added));
 
 235                 it('can put new Nodeelement before another element', function() {
 
 236                     var c = canvas.fromXML('<section><div></div></section>'),
 
 237                         div = c.doc().children()[0],
 
 238                         added = div.before({tag: 'header', klass: 'some.class'}),
 
 239                         children = c.doc().children();
 
 240                     expect(children.length).to.equal(2);
 
 241                     expect(children[0].sameNode(added));
 
 244                 it('can put new DocumentNodeElement after DocumentTextElement', function() {
 
 245                     var c = canvas.fromXML('<section>Alice</section>'),
 
 246                         text = c.doc().children()[0],
 
 247                         added = text.after({tag: 'p'}),
 
 248                         children = c.doc().children();
 
 250                     expect(children.length).to.equal(2);
 
 251                     expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
 
 252                     expect(children[0].getText()).to.equal('Alice');
 
 253                     expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
 
 254                     expect(children[1].sameNode(added)).to.be.true;
 
 256                 it('can put new DocumentNodeElement before DocumentTextElement', function() {
 
 257                     var c = canvas.fromXML('<section>Alice</section>'),
 
 258                         text = c.doc().children()[0],
 
 259                         added = text.before({tag: 'p'}),
 
 260                         children = c.doc().children();
 
 262                     expect(children.length).to.equal(2);
 
 263                     expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
 
 264                     expect(children[0].sameNode(added)).to.be.true;
 
 265                     expect(children[1]).to.be.instanceOf(documentElement.DocumentTextElement);
 
 266                     expect(children[1].getText()).to.equal('Alice');
 
 270             describe('Splitting text', function() {
 
 272                 it('splits DocumentTextElement\'s parent into two DocumentNodeElements of the same type', function() {
 
 273                     var c = canvas.fromXML('<section><header>Some header</header></section>'),
 
 275                         text = section.children()[0].children()[0];
 
 277                     text.split({offset: 5});
 
 278                     expect(section.children().length).to.equal(2, 'section has two children');
 
 280                     var header1 = section.children()[0];
 
 281                     var header2 = section.children()[1];
 
 283                     expect(header1.wlxmlTag).to.equal('header', 'first section child represents wlxml header');
 
 284                     expect(header1.children().length).to.equal(1, 'first header has one text child');
 
 285                     expect(header1.children()[0].getText()).to.equal('Some ', 'first header has correct content');
 
 286                     expect(header2.wlxmlTag).to.equal('header', 'second section child represents wlxml header');
 
 287                     expect(header2.children().length).to.equal(1, 'second header has one text child');
 
 288                     expect(header2.children()[0].getText()).to.equal('header', 'second header has correct content');
 
 291                 it('keeps DocumentTextElement\'s parent\'s children elements intact', function() {
 
 292                     var c = canvas.fromXML('\
 
 295                                     A <span>fancy</span> and <span>nice</span> header\
 
 299                         header = section.children()[0],
 
 300                         textAnd = header.children()[2];
 
 302                     textAnd.split({offset: 2});
 
 304                     var sectionChildren = section.children();
 
 305                     expect(sectionChildren.length).to.equal(2, 'Section has two children');
 
 306                     expect(sectionChildren[0].wlxmlTag).to.equal('header', 'First section element is a wlxml header');
 
 307                     expect(sectionChildren[1].wlxmlTag).to.equal('header', 'Second section element is a wlxml header');
 
 309                     var firstHeaderChildren = sectionChildren[0].children();
 
 310                     expect(firstHeaderChildren.length).to.equal(3, 'First header has three children');
 
 311                     expect(firstHeaderChildren[0].getText()).to.equal('A ', 'First header starts with a text');
 
 312                     expect(firstHeaderChildren[1].wlxmlTag).to.equal('span', 'First header has span in the middle');
 
 313                     expect(firstHeaderChildren[2].getText()).to.equal(' a', 'First header ends with text');
 
 315                     var secondHeaderChildren = sectionChildren[1].children();
 
 316                     expect(secondHeaderChildren.length).to.equal(3, 'Second header has three children');
 
 317                     expect(secondHeaderChildren[0].getText()).to.equal('nd ', 'Second header starts with text');
 
 318                     expect(secondHeaderChildren[1].wlxmlTag).to.equal('span', 'Second header has span in the middle');
 
 319                     expect(secondHeaderChildren[2].getText()).to.equal(' header', 'Second header ends with text');
 
 323             describe('wrapping', function() {
 
 324                 it('wraps DocumentNodeElement', function() {
 
 325                     var c = canvas.fromXML('<section><div></div></section>'),
 
 326                         div = c.doc().children()[0];
 
 328                     var returned = div.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
 
 329                         parent = div.parent(),
 
 330                         parent2 = c.doc().children()[0];
 
 332                     expect(returned.sameNode(parent)).to.be.true;
 
 333                     expect(returned.sameNode(parent2)).to.be.true;
 
 335                 it('wraps DocumentTextElement', function() {
 
 336                     var c = canvas.fromXML('<section>Alice</section>'),
 
 337                         text = c.doc().children()[0];
 
 339                     var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
 
 340                         parent = text.parent(),
 
 341                         parent2 = c.doc().children()[0];
 
 343                     expect(returned.sameNode(parent)).to.be.true;
 
 344                     expect(returned.sameNode(parent2)).to.be.true;
 
 347                 it('wraps part of DocumentTextElement', function() {
 
 348                     var c = canvas.fromXML('<section>Alice has a cat</section>'),
 
 349                         text = c.doc().children()[0];
 
 351                     var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class', start: 5, end: 12}),
 
 352                         children = c.doc().children();
 
 354                     expect(children.length).to.equal(3);
 
 356                     expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
 
 357                     expect(children[0].getText()).to.equal('Alice');
 
 359                     expect(children[1].sameNode(returned)).to.be.true;
 
 360                     expect(children[1].children().length).to.equal(1);
 
 361                     expect(children[1].children()[0].getText()).to.equal(' has a ');
 
 363                     expect(children[2]).to.be.instanceOf(documentElement.DocumentTextElement);
 
 364                     expect(children[2].getText()).to.equal('cat');
 
 367                 it('wraps text spanning multiple sibling DocumentTextNodes', function() {
 
 368                     var c = canvas.fromXML('<section>Alice has a <span>small</span> cat</section>'),
 
 370                         wrapper = c.wrapText({
 
 372                             _with: {tag: 'span', klass: 'some.class'},
 
 378                     expect(section.children().length).to.equal(2);
 
 379                     expect(section.children()[0]).to.be.instanceOf(documentElement.DocumentTextElement);
 
 380                     expect(section.children()[0].getText()).to.equal('Alice ');
 
 382                     var wrapper2 = section.children()[1];
 
 383                     expect(wrapper2.sameNode(wrapper)).to.be.true;
 
 385                     var wrapperChildren = wrapper.children();
 
 386                     expect(wrapperChildren.length).to.equal(3);
 
 387                     expect(wrapperChildren[0].getText()).to.equal('has a ');
 
 389                     expect(wrapperChildren[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
 
 390                     expect(wrapperChildren[1].children().length).to.equal(1);
 
 391                     expect(wrapperChildren[1].children()[0].getText()).to.equal('small');
 
 393                     expect(wrapperChildren[2].getText()).to.equal(' cat');
 
 398         describe('Lists api', function() {
 
 399             it('allows creation of a list from existing sibling DocumentElements', function() {
 
 400                 var c = canvas.fromXML('\
 
 408                     textAlice = section.children()[0],
 
 409                     divCat = section.children()[3]
 
 411                 c.list.create({element1: textAlice, element2: divCat});
 
 413                 expect(section.children().length).to.equal(1, 'section has one child element');
 
 415                 var list = section.children()[0];
 
 416                 expect(list.is('list')).to.equal(true, 'section\'s only child is a list');
 
 417                 expect(list.children().length).to.equal(4, 'list contains four elements');