second batch: works, but failing tests - ie blocking span
[fnpeditor.git] / src / editor / modules / documentCanvas / canvas / canvas.test.js
1 define([
2 'libs/jquery',
3 'libs/chai',
4 'libs/sinon',
5 'modules/documentCanvas/canvas/canvas',
6 'modules/documentCanvas/canvas/utils',
7 'wlxml/wlxml',
8 ], function($, chai, sinon, canvas, utils, wlxml) {
9     
10 'use strict';
11 /* global describe, it, beforeEach, afterEach */
12
13 var expect = chai.expect;
14
15 var getCanvasFromXML = function(xml) {
16     return canvas.fromXMLDocument(getDocumentFromXML(xml), null);
17 };
18
19 var getDocumentFromXML = function(xml) {
20     return wlxml.WLXMLDocumentFromXML(xml);
21 };
22
23 var wait = function(callback, timeout) {
24     /* globals window */
25     return window.setTimeout(callback, timeout || 0.5);
26 };
27
28
29 describe('wtf', function() {
30     it('wtf!', function() {
31         var c = getCanvasFromXML('<section>Alice</section>'),
32             doc = c.wlxmlDocument;
33
34         var txtNode = doc.root.contents()[0];
35         txtNode.wrapWith({tagName: 'header', start: 1, end: 2});
36         expect(c.doc().children().length).to.equal(3);
37     });
38 })
39
40 describe('new Canvas', function() {
41     it('abc', function() {
42         var doc = wlxml.WLXMLDocumentFromXML('<section>Alice <span>has</span> a cat!</div>'),
43             c = canvas.fromXMLDocument(doc);
44
45         expect(c.doc().children()).to.have.length(3);
46         expect(c.doc().children()[0].canvas).to.equal(c);
47         expect(c.doc().children()[0].wlxmlNode.sameNode(doc.root));
48     });
49 });
50
51 describe('Handling empty text nodes', function() {
52     it('puts zero width space into node with about to be remove text', function(done) {
53         var c = getCanvasFromXML('<section>Alice</section>'),
54             textElement = c.doc().children()[0];
55         textElement.setText('');
56
57         /* Wait for MutationObserver to kick in. */
58         wait(function() {
59             expect(textElement.getText({raw:true})).to.equal(utils.unicode.ZWS, 'ZWS in canvas');
60             expect(c.wlxmlDocument.root.contents()[0].getText()).to.equal('', 'empty string in a document');
61             done();
62         });
63     });
64 });
65
66 describe('Handling changes to the document', function() {
67     it('replaces the whole canvas content when document root node replaced', function() {
68         var doc = getDocumentFromXML('<section></section>'),
69             c = canvas.fromXMLDocument(doc);
70
71         var header = doc.root.replaceWith({tagName: 'header'});
72         expect(c.doc().wlxmlNode.sameNode(header)).to.equal(true);
73     });
74 });
75
76 describe('Listening to document changes', function() {
77
78     it('Handling element node moved', function() {
79         var doc = getDocumentFromXML('<section><a></a><b></b></section>'),
80             a = doc.root.contents()[0],
81             b = doc.root.contents()[1],
82             c = canvas.fromXMLDocument(doc);
83
84         debugger;
85         a.before(b);
86         var sectionChildren = c.doc().children();
87         expect(sectionChildren.length).to.equal(2);
88         expect(sectionChildren[0].wlxmlNode.getTagName()).to.equal('b');
89         expect(sectionChildren[1].wlxmlNode.getTagName()).to.equal('a');
90     });
91
92     it('Handling text node moved', function() {
93         var doc = getDocumentFromXML('<section><a></a>Alice</section>'),
94             a = doc.root.contents()[0],
95             textNode = doc.root.contents()[1],
96             c = canvas.fromXMLDocument(doc);
97
98         a.before(textNode);
99         var sectionChildren = c.doc().children();
100         expect(sectionChildren.length).to.equal(2);
101         expect(sectionChildren[0].getText()).to.equal('Alice');
102         expect(sectionChildren[1].wlxmlNode.getTagName()).to.equal('a');
103     });
104
105     it('Handles nodeTagChange event', function() {
106
107         var doc = wlxml.WLXMLDocumentFromXML('<section><div>Alice</div></section>'),
108             c = canvas.fromXMLDocument(doc);
109
110         doc.root.contents()[0].setTag('header');
111
112         var headerNode = doc.root.contents()[0],
113             headerElement = c.doc().children()[0];
114
115         expect(headerElement.wlxmlNode.getTagName()).to.equal('header', 'element ok');
116
117         /* Make sure we handle invalidation of reference to wlxmlNode after changing its tag */
118         expect(headerNode.getData('canvasElement').sameNode(headerElement)).to.equal(true, 'node->element');
119         expect(headerElement.wlxmlNode.sameNode(headerNode)).to.equal(true, 'element->node');
120     });
121
122     it('Handles nodeDetached event for an empty text node', function(done) {
123         var doc = wlxml.WLXMLDocumentFromXML('<section><div>A<span>b</span></div></section>'),
124             aTextNode = doc.root.contents()[0].contents()[0],
125             aTextElement;
126
127         canvas.fromXMLDocument(doc);
128         aTextElement = utils.findCanvasElementInParent(aTextNode, aTextNode.parent()); // TODO: This really should be easier...
129
130         aTextElement.setText('');
131
132         wait(function() {
133             var parent = aTextElement.parent();
134             expect(aTextElement.getText({raw:true})).to.equal(utils.unicode.ZWS, 'canvas represents this as empty node');
135             aTextElement.wlxmlNode.detach();
136             expect(parent.children().length).to.equal(1);
137             expect(parent.children()[0].wlxmlNode.getTagName()).to.equal('span');
138             done();
139         });
140     });
141 });
142
143 describe('Displaying span nodes', function() {
144     it('inlines a span element with a text', function() {
145         var c = getCanvasFromXML('<section><span>Alice</span></section>'),
146             spanElement = c.doc().children()[0];
147         expect(spanElement.isBlock()).to.equal(false);
148     });
149     it('renders non-span element as a block', function() {
150         var c = getCanvasFromXML('<section><span></span></section>'),
151             element = c.doc().children()[0],
152             node = element.wlxmlNode;
153
154         expect(element.isBlock()).to.equal(false, 'initially inline');
155         node = node.setTag('div');
156         expect(node.getData('canvasElement').isBlock()).to.equal(true, 'block');
157     });
158
159     it('inlines a span element if its block content gets removed', function() {
160         var c = getCanvasFromXML('<section><span>Alice <div>has</div> a cat!</span></section>'),
161             spanElement = c.doc().children()[0],
162             divNode = spanElement.wlxmlNode.contents()[1];
163
164         expect(spanElement.isBlock()).to.equal(true, 'initially a block');
165         divNode.detach();
166         expect(spanElement.isBlock()).to.equal(false, 'inlined after removing inner block');
167         
168         spanElement.wlxmlNode.append({tagName: 'div'});
169
170         expect(spanElement.isBlock()).to.equal(true, 'block again after bringing back inner block');
171     });
172
173     it('keeps showing element as a block after changing its node tag to span if it contains elements of non-span nodes', function() {
174         var c = getCanvasFromXML('<section><div><div></div></div></section>'),
175             outerDivElement = c.doc().children()[0],
176             outerDivNode = outerDivElement.wlxmlNode;
177         outerDivNode = outerDivNode.setTag('span');
178         expect(c.doc().children()[0].isBlock()).to.equal(true);
179     });
180 });
181
182
183 describe('Default document changes handling', function() {
184     it('handles added node', function() {
185         var c = getCanvasFromXML('<section></section>');
186         c.wlxmlDocument.root.append({tagName:'div'});
187         expect(c.doc().children().length).to.equal(1);
188         c.wlxmlDocument.root.prepend({tagName:'div'});
189         expect(c.doc().children().length).to.equal(2);
190
191         var node = c.wlxmlDocument.root.contents()[1];
192         node.before({tagName: 'div'});
193         expect(c.doc().children().length).to.equal(3);
194         node.after({tagName: 'div'});
195         expect(c.doc().children().length).to.equal(4);
196     });
197
198     it('handles attribute value change for a class attribute', function() {
199         var c = getCanvasFromXML('<section></section>');
200         c.wlxmlDocument.root.setAttr('class', 'test');
201         expect(c.doc().wlxmlNode.getClass()).to.equal('test');
202     });
203
204     it('handles detached node', function() {
205         var c = getCanvasFromXML('<section><div></div></section>');
206         c.wlxmlDocument.root.contents()[0].detach();
207         expect(c.doc().children().length).to.equal(0);
208     });
209
210     it('handles moved node', function() {
211         var doc = getDocumentFromXML('<section><a></a><b></b></section>'),
212             a = doc.root.contents()[0],
213             b = doc.root.contents()[1],
214             c = canvas.fromXMLDocument(doc);
215
216         a.before(b);
217         var sectionChildren = c.doc().children();
218         expect(sectionChildren.length).to.equal(2);
219         expect(sectionChildren[0].wlxmlNode.getTagName()).to.equal('b');
220         expect(sectionChildren[1].wlxmlNode.getTagName()).to.equal('a');
221     });
222
223     it('handles change in a text node', function() {
224         var c = getCanvasFromXML('<section>Alice</section>');
225         c.wlxmlDocument.root.contents()[0].setText('cat');
226         expect(c.doc().children()[0].getText()).to.equal('cat');
227     });
228 });
229     
230 describe('Custom elements based on wlxml class attribute', function() {
231     it('allows custom rendering', function() {
232         var c = getCanvasFromXML('<section><div class="testClass"></div></section>', {
233             testClass: {
234                 init: function() {
235                     debugger;
236                     this.dom.append('<test></test>');
237                 }
238             }
239         });
240         expect(c.doc().children()[0]._container().children('test').length).to.equal(1); // @!
241     });
242
243     it('allows handling changes to internal structure of rendered node', function() {
244         var c = getCanvasFromXML('<section><div class="testClass"><a></a></div></section>', {
245             testClass: {
246                 init: function() {
247                     this.header = $('<h1>');
248                     this.dom.append(this.header);
249                     this.refresh2();
250                 },
251                 refresh2: function() {
252                     this.header.text(this.el.wlxmlNode.contents().length);
253                 },
254                 onNodeAdded: function(event) {
255                     void(event);
256                     this.refresh2();
257                 }
258             }
259         });
260
261         var node = c.wlxmlDocument.root.contents()[0],
262             element = node.getData('canvasElement');
263
264         var header = element.dom().find('h1');
265         expect(header.text()).to.equal('1', 'just <a>');
266
267         node.append({tagName: 'div'});
268
269         expect(header.text()).to.equal('2', 'added div');
270     });
271
272     describe('Handling unknown class', function() {
273         it('Inherits default behavior', function() {
274             var c = getCanvasFromXML('<section><div class="unknown">Hi!</div></section>');
275             expect(c.doc().children()[0].children()[0].getText()).to.equal('Hi!');
276         });
277     });
278 });
279
280 describe('Cursor', function() {
281     /* globals Node */
282     var getSelection;
283
284     var findTextNode = function(inside, text) {
285         var nodes = inside.find(':not(iframe)').addBack().contents().filter(function() {
286             return this.nodeType === Node.TEXT_NODE && this.data === text;
287         });
288         if(nodes.length) {
289             return nodes[0];
290         }
291         return null;
292     };
293
294     beforeEach(function() {
295         /* globals window */
296         getSelection = sinon.stub(window, 'getSelection');
297     });
298
299     afterEach(function() {
300         getSelection.restore();
301     });
302
303     it('returns position when browser selection collapsed', function() {
304         var c = getCanvasFromXML('<section>Alice has a cat</section>'),
305             dom = c.doc().dom(),
306             text = findTextNode(dom, 'Alice has a cat');
307
308         expect(text.nodeType).to.equal(Node.TEXT_NODE, 'correct node selected');
309         expect($(text).text()).to.equal('Alice has a cat');
310
311         getSelection.returns({
312             anchorNode: text,
313             focusNode: text,
314             anchorOffset: 5,
315             focusOffset: 5,
316             isCollapsed: true
317         });
318         var cursor = c.getCursor(),
319             position = cursor.getPosition();
320
321         expect(cursor.isSelecting()).to.equal(false, 'cursor is not selecting anything');
322         expect(position.element.getText()).to.equal('Alice has a cat');
323         expect(position.offset).to.equal(5);
324         expect(position.offsetAtEnd).to.equal(false, 'offset is not at end');
325
326         getSelection.returns({
327             anchorNode: text,
328             focusNode: text,
329             anchorOffset: 15,
330             focusOffset: 15,
331             isCollapsed: true
332         });
333
334         expect(cursor.getPosition().offsetAtEnd).to.equal(true, 'offset at end');
335     });
336
337     it('recognizes selection start and end on document order', function() {
338         var c = getCanvasFromXML('<section><span>Alice</span><span>has a cat</span><div>abc<span>...</span>cde</div></section>'),
339             dom = c.doc().dom(),
340             textFirst = findTextNode(dom, 'Alice'),
341             textSecond = findTextNode(dom, 'has a cat'),
342             textAbc = findTextNode(dom, 'abc'),
343             textCde = findTextNode(dom, 'cde');
344
345         var check = function(label, expected) {
346             var cursor = c.getCursor();
347             label = label + ': ';
348             expect(cursor.getSelectionStart().element.getText()).to.equal(expected.start.text, label + 'start element ok');
349             expect(cursor.getSelectionStart().offset).to.equal(expected.start.offset, label + 'start offset ok');
350             expect(cursor.getSelectionEnd().element.getText()).to.equal(expected.end.text, label + 'end element ok');
351             expect(cursor.getSelectionEnd().offset).to.equal(expected.end.offset, label + 'end offset ok');
352         };
353
354         getSelection.returns({
355             anchorNode: textFirst,
356             focusNode: textFirst,
357             anchorOffset: 1,
358             focusOffset: 3,
359             isCollapsed: false
360         });
361
362         check('same element, anchor first', {
363             start: {text: 'Alice', offset: 1},
364             end: {text: 'Alice', offset:3}
365         });
366
367
368         getSelection.returns({
369             anchorNode: textFirst,
370             focusNode: textFirst,
371             anchorOffset: 3,
372             focusOffset: 1,
373             isCollapsed: false
374         });
375
376         check('same element, anchor second', {
377             start: {text: 'Alice', offset: 1},
378             end: {text: 'Alice', offset:3}
379         });
380
381
382         getSelection.returns({
383             anchorNode: textAbc,
384             focusNode: textCde,
385             anchorOffset: 3,
386             focusOffset: 1,
387             isCollapsed: false
388         });
389
390         check('same parent, anchor first', {
391             start: {text: 'abc', offset: 3},
392             end: {text: 'cde', offset:1}
393         });
394
395
396         getSelection.returns({
397             anchorNode: textCde,
398             focusNode: textAbc,
399             anchorOffset: 1,
400             focusOffset: 3,
401             isCollapsed: false
402         });
403
404         check('same parent, anchor second', {
405             start: {text: 'abc', offset: 3},
406             end: {text: 'cde', offset:1}
407         });
408
409
410         getSelection.returns({
411             anchorNode: textFirst,
412             focusNode: textSecond,
413             anchorOffset: 1,
414             focusOffset: 3,
415             isCollapsed: false
416         });
417
418         check('different parents, anchor first', {
419             start: {text: 'Alice', offset: 1},
420             end: {text: 'has a cat', offset:3}
421         });
422
423
424         getSelection.returns({
425             anchorNode: textSecond,
426             focusNode: textFirst,
427             anchorOffset: 3,
428             focusOffset: 1,
429             isCollapsed: false
430         });
431
432         check('different parents, anchor second', {
433             start: {text: 'Alice', offset: 1},
434             end: {text: 'has a cat', offset:3}
435         });
436     });
437
438     it('returns boundries of selection when browser selection not collapsed', function() {
439         var c = getCanvasFromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
440             dom = c.doc().dom(),
441             text = {
442                 alice: findTextNode(dom, 'Alice '),
443                 has: findTextNode(dom, 'has'),
444                 cat: findTextNode(dom, ' cat')
445             },
446             cursor = c.getCursor(),
447             aliceElement = c.getDocumentElement(text.alice),
448             catElement = c.getDocumentElement(text.cat);
449
450         [
451             {focus: text.alice, focusOffset: 1, anchor: text.cat,   anchorOffset: 2, selectionAnchor: catElement},
452             {focus: text.cat,   focusOffset: 2, anchor: text.alice, anchorOffset: 1, selectionAnchor: aliceElement}
453         ].forEach(function(s, idx) {
454             getSelection.returns({isColapsed: false, anchorNode: s.anchor, anchorOffset: s.anchorOffset, focusNode: s.focus, focusOffset: s.focusOffset});
455
456             var selectionStart = cursor.getSelectionStart(),
457                 selectionEnd = cursor.getSelectionEnd(),
458                 selectionAnchor = cursor.getSelectionAnchor();
459
460             expect(cursor.isSelecting()).to.equal(true, 'cursor is selecting');
461             expect(selectionStart.element.sameNode(aliceElement)).to.equal(true, '"Alice" is the start of the selection ' + idx);
462             expect(selectionStart.offset).to.equal(1, '"Alice" offset ok' + idx);
463             expect(selectionEnd.element.sameNode(catElement)).to.equal(true, '"Cat" is the start of the selection ' + idx);
464             expect(selectionEnd.offset).to.equal(2, '"Cat" offset ok' + idx);
465             expect(selectionAnchor.element.sameNode(s.selectionAnchor)).to.equal(true, 'anchor ok');
466             expect(selectionAnchor.offset).to.equal(s.anchorOffset, 'anchor offset ok');
467         });
468     });
469
470     it('recognizes when browser selection boundries lies in sibling DocumentTextElements', function() {
471         var c = getCanvasFromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
472             dom = c.doc().dom(),
473             text = {
474                 alice: findTextNode(dom, 'Alice '),
475                 has: findTextNode(dom, 'has'),
476                 a: findTextNode(dom, ' a '),
477                 big: findTextNode(dom, 'big'),
478                 cat: findTextNode(dom, ' cat'),
479             },
480             cursor = c.getCursor();
481
482         expect($(text.alice).text()).to.equal('Alice ');
483         expect($(text.has).text()).to.equal('has');
484         expect($(text.a).text()).to.equal(' a ');
485         expect($(text.big).text()).to.equal('big');
486         expect($(text.cat).text()).to.equal(' cat');
487
488         getSelection.returns({anchorNode: text.alice, focusNode: text.a});
489         expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "a" are children');
490
491         getSelection.returns({anchorNode: text.alice, focusNode: text.cat});
492         expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "cat" are children');
493
494         getSelection.returns({anchorNode: text.alice, focusNode: text.has});
495         expect(cursor.isSelectingSiblings()).to.equal(false, '"Alice" and "has" are not children');
496
497         getSelection.returns({anchorNode: text.has, focusNode: text.big});
498         expect(cursor.isSelectingSiblings()).to.equal(false, '"has" and "big" are not children');
499     });
500 });
501
502 });