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 sample document', function() {
15 it('works', function() {
16 var c = canvas.fromXML('\
18 This is some text without its own wrapping tag.\
19 <div class="p.subclass">\
23 This is text in a div <span>with some inline text</span>.\
25 This is some text without its own wrapping tag.\
28 var expected = '<div wlxml-tag="section">'
29 + 'This is some text without its own wrapping tag.'
30 + '<div wlxml-tag="div" wlxml-class="p-subclass">This is a paragraph.</div>'
31 + '<div wlxml-tag="div">This is text in a div <div wlxml-tag="span">with some inline text</div>.</div>'
32 + 'This is some text without its own wrapping tag.'
34 expect(c.doc().dom()[0].isEqualNode($(expected)[0])).to.be.true;
38 describe('Internal HTML representation of a DocumentNodeElement', function() {
39 it('is always a div tag', function() {
40 ['section', 'header', 'span', 'aside', 'figure'].forEach(function(tagName) {
41 var dom = canvas.fromXML('<' + tagName + '></' + tagName + '>').doc().dom();
42 expect(dom.prop('tagName')).to.equal('DIV', tagName + ' is represented as div');
45 it('has wlxml tag put into wlxml-tag attribute', function() {
46 var dom = canvas.fromXML('<section></section>').doc().dom();
47 expect(dom.attr('wlxml-tag')).to.equal('section');
49 it('has wlxml class put into wlxml-class, dots replaced with dashes', function() {
50 var dom = canvas.fromXML('<section class="some.class"></section>').doc().dom();
51 expect(dom.attr('wlxml-class')).to.equal('some-class');
55 describe('Internal HTML representation of a DocumentTextElement', function() {
56 it('is just a TextNode', function() {
57 var dom = canvas.fromXML('<section>Alice</section>').doc().children()[0].dom();
58 expect(dom[0].nodeType === Node.TEXT_NODE);
62 describe('basic properties', function() {
63 it('renders empty document when canvas created from empty XML', function() {
64 var c = canvas.fromXML('');
65 expect(c.doc()).to.equal(null);
68 it('gives access to its document root node', function() {
69 var c = canvas.fromXML('<section></section>');
70 expect(c.doc().wlxmlTag).to.equal('section');
73 describe('DocumentTextElement', function() {
74 it('can have its content set', function() {
75 var c = canvas.fromXML('<section>Alice</section>'),
77 text = root.children()[0];
79 text.setText('a cat');
80 expect(root.children()[0].getText()).to.equal('a cat');
84 describe('DocumentNodeElement', function() {
85 it('knows index of its child', function() {
86 var c = canvas.fromXML('<section><div></div><header></header><span></span></section>'),
88 child = root.children()[1];
89 expect(root.childIndex(child)).to.equal(1);
92 it('knows WLXML tag it renders', function(){
93 var c = canvas.fromXML('<section></section>'),
95 expect(section.getWlxmlTag()).to.equal('section', 'initial tag is section');
96 section.setWlxmlTag('header');
97 expect(section.getWlxmlTag()).to.equal('header', 'tag is changed to header');
100 it('knows WLXML class of a WLXML tag it renders', function(){
101 var c = canvas.fromXML('<section class="some.class"></section>'),
103 expect(section.getWlxmlClass()).to.equal('some.class');
104 section.setWlxmlClass('some.other.class');
105 expect(section.getWlxmlClass()).to.equal('some.other.class');
112 describe('document representation api', function() {
113 describe('document root element', function() {
114 var c = canvas.fromXML('<section></section>');
115 it('exists', function() {
116 expect(c.doc()).to.be.instanceOf(documentElement.DocumentElement);
118 it('is of type DocumentNodeElement', function() {
119 expect(c.doc()).to.be.instanceOf(documentElement.DocumentNodeElement);
123 describe('DocumentElements comparison', function() {
124 it('reports dwo DocumentElements to be the same when they represent the same wlxml document element', function() {
125 var c = canvas.fromXML('<section><div></div><div></div></section>'),
126 first_div1 = c.doc().children()[0],
127 first_div2 = c.doc().children()[0],
128 second_div = c.doc().children()[1];
129 expect(first_div1.sameNode(first_div1)).to.be.true;
130 expect(first_div1.sameNode(first_div2)).to.be.true;
131 expect(first_div1.sameNode(second_div)).to.be.false;
135 describe('traversing', function() {
136 it('reports element nodes', function() {
137 var c = canvas.fromXML('<section><div></div></section>'),
138 children = c.doc().children();
139 expect(children.length).to.equal(1);
140 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
142 c = canvas.fromXML('<section><div></div><div></div></section>'),
143 children = c.doc().children();
144 expect(children.length).to.equal(2);
145 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
146 expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
149 it('reports text nodes', function() {
150 var c = canvas.fromXML('<section>Alice</section>'),
151 children = c.doc().children();
152 expect(children.length).to.equal(1);
153 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
156 describe('accessing parents', function() {
157 it('returns DocumentNodeElement representing parent in wlxml document as DocumentNodeElement parent', function() {
158 var c = canvas.fromXML('<section><div></div></section>'),
159 div = c.doc().children()[0];
160 expect(div.parent().sameNode(c.doc())).to.be.true;
162 it('returns DocumentNodeElement representing parent in wlxml document as DocumentTextElement parent', function() {
163 var c = canvas.fromXML('<section>Alice</section>'),
164 text = c.doc().children()[0];
165 expect(text.parent().sameNode(c.doc())).to.be.true;
169 describe('free text handling', function() {
170 it('sees free text', function() {
171 var c = canvas.fromXML('<section>Alice <span>has</span> a cat</section>'),
172 children = c.doc().children();
173 expect(children.length).to.equal(3);
174 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
175 expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
176 expect(children[2]).to.be.instanceOf(documentElement.DocumentTextElement);
180 describe('white characters handling', function() {
181 it('says empty element node has no children', function() {
182 var c = canvas.fromXML('<section></section>');
183 expect(c.doc().children().length).to.equal(0);
185 it('says element node with one space has one DocumentTextElement', function() {
186 var c = canvas.fromXML('<section> </section>');
187 expect(c.doc().children().length).to.equal(1);
188 expect(c.doc().children()[0]).to.be.instanceOf(documentElement.DocumentTextElement);
189 expect(c.doc().children()[0].getText()).to.equal(' ');
191 it('ignores white space surrounding block elements', function() {
192 var c = canvas.fromXML('<section> <div></div> </section>');
193 var children = c.doc().children();
194 expect(children.length).to.equal(1);
195 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
197 it('ignores white space between block elements', function() {
198 var c = canvas.fromXML('<section><div></div> <div></div></section>');
199 var children = c.doc().children();
200 expect(children.length === 2);
201 [0,1].forEach(function(idx) {
202 expect(children[idx]).to.be.instanceOf(documentElement.DocumentNodeElement);
206 it('trims white space from the beginning and the end of the block elements', function() {
207 var c = canvas.fromXML('<section> Alice <span>has</span> a cat </section>');
208 expect(c.doc().children()[0].getText()).to.equal('Alice ');
209 expect(c.doc().children()[2].getText()).to.equal(' a cat');
212 it('normalizes string of white characters to one space at the inline element boundries', function() {
213 var c = canvas.fromXML('<span> Alice has a cat </span>');
214 expect(c.doc().children()[0].getText()).to.equal(' Alice has a cat ');
217 it('normalizes string of white characters to one space before inline element', function() {
218 var c = canvas.fromXML('<div>Alice has <span>a cat</span></div>');
219 expect(c.doc().children()[0].getText()).to.equal('Alice has ');
222 it('normalizes string of white characters to one space after inline element', function() {
223 var c = canvas.fromXML('<div>Alice has <span>a</span> cat</div>');
224 expect(c.doc().children()[2].getText()).to.equal(' cat');
229 describe('manipulation api', function() {
231 describe('Basic Element inserting', function() {
232 it('can put new NodeElement at the end', function() {
233 var c = canvas.fromXML('<section></section>'),
234 appended = c.doc().append({tag: 'header', klass: 'some.class'}),
235 children = c.doc().children();
237 expect(children.length).to.equal(1);
238 expect(children[0].sameNode(appended));
241 it('can put new TextElement at the end', function() {
242 var c = canvas.fromXML('<section></section>'),
243 appended = c.doc().append({text: 'Alice'}),
244 children = c.doc().children();
246 expect(children.length).to.equal(1);
247 expect(children[0].sameNode(appended));
248 expect(children[0].getText()).to.equal('Alice');
251 it('can put new NodeElement after another NodeElement', function() {
252 var c = canvas.fromXML('<section><div></div></section>'),
253 div = c.doc().children()[0],
254 added = div.after({tag: 'header', klass: 'some.class'}),
255 children = c.doc().children();
256 expect(children.length).to.equal(2);
257 expect(children[1].sameNode(added));
260 it('can put new Nodeelement before another element', function() {
261 var c = canvas.fromXML('<section><div></div></section>'),
262 div = c.doc().children()[0],
263 added = div.before({tag: 'header', klass: 'some.class'}),
264 children = c.doc().children();
265 expect(children.length).to.equal(2);
266 expect(children[0].sameNode(added));
269 it('can put new DocumentNodeElement after DocumentTextElement', function() {
270 var c = canvas.fromXML('<section>Alice</section>'),
271 text = c.doc().children()[0],
272 added = text.after({tag: 'p'}),
273 children = c.doc().children();
275 expect(children.length).to.equal(2);
276 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
277 expect(children[0].getText()).to.equal('Alice');
278 expect(children[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
279 expect(children[1].sameNode(added)).to.be.true;
281 it('can put new DocumentNodeElement before DocumentTextElement', function() {
282 var c = canvas.fromXML('<section>Alice</section>'),
283 text = c.doc().children()[0],
284 added = text.before({tag: 'p'}),
285 children = c.doc().children();
287 expect(children.length).to.equal(2);
288 expect(children[0]).to.be.instanceOf(documentElement.DocumentNodeElement);
289 expect(children[0].sameNode(added)).to.be.true;
290 expect(children[1]).to.be.instanceOf(documentElement.DocumentTextElement);
291 expect(children[1].getText()).to.equal('Alice');
295 describe('Splitting text', function() {
297 it('splits DocumentTextElement\'s parent into two DocumentNodeElements of the same type', function() {
298 var c = canvas.fromXML('<section><header>Some header</header></section>'),
300 text = section.children()[0].children()[0];
302 text.split({offset: 5});
303 expect(section.children().length).to.equal(2, 'section has two children');
305 var header1 = section.children()[0];
306 var header2 = section.children()[1];
308 expect(header1.wlxmlTag).to.equal('header', 'first section child represents wlxml header');
309 expect(header1.children().length).to.equal(1, 'first header has one text child');
310 expect(header1.children()[0].getText()).to.equal('Some ', 'first header has correct content');
311 expect(header2.wlxmlTag).to.equal('header', 'second section child represents wlxml header');
312 expect(header2.children().length).to.equal(1, 'second header has one text child');
313 expect(header2.children()[0].getText()).to.equal('header', 'second header has correct content');
316 it('keeps DocumentTextElement\'s parent\'s children elements intact', function() {
317 var c = canvas.fromXML('\
320 A <span>fancy</span> and <span>nice</span> header\
324 header = section.children()[0],
325 textAnd = header.children()[2];
327 textAnd.split({offset: 2});
329 var sectionChildren = section.children();
330 expect(sectionChildren.length).to.equal(2, 'Section has two children');
331 expect(sectionChildren[0].wlxmlTag).to.equal('header', 'First section element is a wlxml header');
332 expect(sectionChildren[1].wlxmlTag).to.equal('header', 'Second section element is a wlxml header');
334 var firstHeaderChildren = sectionChildren[0].children();
335 expect(firstHeaderChildren.length).to.equal(3, 'First header has three children');
336 expect(firstHeaderChildren[0].getText()).to.equal('A ', 'First header starts with a text');
337 expect(firstHeaderChildren[1].wlxmlTag).to.equal('span', 'First header has span in the middle');
338 expect(firstHeaderChildren[2].getText()).to.equal(' a', 'First header ends with text');
340 var secondHeaderChildren = sectionChildren[1].children();
341 expect(secondHeaderChildren.length).to.equal(3, 'Second header has three children');
342 expect(secondHeaderChildren[0].getText()).to.equal('nd ', 'Second header starts with text');
343 expect(secondHeaderChildren[1].wlxmlTag).to.equal('span', 'Second header has span in the middle');
344 expect(secondHeaderChildren[2].getText()).to.equal(' header', 'Second header ends with text');
348 describe('wrapping', function() {
349 it('wraps DocumentNodeElement', function() {
350 var c = canvas.fromXML('<section><div></div></section>'),
351 div = c.doc().children()[0];
353 var returned = div.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
354 parent = div.parent(),
355 parent2 = c.doc().children()[0];
357 expect(returned.sameNode(parent)).to.be.true;
358 expect(returned.sameNode(parent2)).to.be.true;
360 it('wraps DocumentTextElement', function() {
361 var c = canvas.fromXML('<section>Alice</section>'),
362 text = c.doc().children()[0];
364 var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class'}),
365 parent = text.parent(),
366 parent2 = c.doc().children()[0];
368 expect(returned.sameNode(parent)).to.be.true;
369 expect(returned.sameNode(parent2)).to.be.true;
372 it('wraps part of DocumentTextElement', function() {
373 var c = canvas.fromXML('<section>Alice has a cat</section>'),
374 text = c.doc().children()[0];
376 var returned = text.wrapWithNodeElement({tag: 'header', klass: 'some.class', start: 5, end: 12}),
377 children = c.doc().children();
379 expect(children.length).to.equal(3);
381 expect(children[0]).to.be.instanceOf(documentElement.DocumentTextElement);
382 expect(children[0].getText()).to.equal('Alice');
384 expect(children[1].sameNode(returned)).to.be.true;
385 expect(children[1].children().length).to.equal(1);
386 expect(children[1].children()[0].getText()).to.equal(' has a ');
388 expect(children[2]).to.be.instanceOf(documentElement.DocumentTextElement);
389 expect(children[2].getText()).to.equal('cat');
392 it('wraps text spanning multiple sibling DocumentTextNodes', function() {
393 var c = canvas.fromXML('<section>Alice has a <span>small</span> cat</section>'),
395 wrapper = c.wrapText({
397 _with: {tag: 'span', klass: 'some.class'},
403 expect(section.children().length).to.equal(2);
404 expect(section.children()[0]).to.be.instanceOf(documentElement.DocumentTextElement);
405 expect(section.children()[0].getText()).to.equal('Alice ');
407 var wrapper2 = section.children()[1];
408 expect(wrapper2.sameNode(wrapper)).to.be.true;
410 var wrapperChildren = wrapper.children();
411 expect(wrapperChildren.length).to.equal(3);
412 expect(wrapperChildren[0].getText()).to.equal('has a ');
414 expect(wrapperChildren[1]).to.be.instanceOf(documentElement.DocumentNodeElement);
415 expect(wrapperChildren[1].children().length).to.equal(1);
416 expect(wrapperChildren[1].children()[0].getText()).to.equal('small');
418 expect(wrapperChildren[2].getText()).to.equal(' cat');
423 describe('Lists api', function() {
424 it('allows creation of a list from existing sibling DocumentElements', function() {
425 var c = canvas.fromXML('\
433 textAlice = section.children()[0],
434 divCat = section.children()[3]
436 c.list.create({element1: textAlice, element2: divCat});
438 expect(section.children().length).to.equal(1, 'section has one child element');
440 var list = section.children()[0];
441 expect(list.is('list')).to.equal(true, 'section\'s only child is a list');
442 expect(list.children().length).to.equal(4, 'list contains four elements');
443 list.children().forEach(function(child) {
444 expect(child.getWlxmlClass()).to.equal('item', 'list childs have wlxml class of item');