editor: fixing canvas.cursor getSelectionStart/getSelectionEnd
[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/documentElement',
7 'modules/documentCanvas/canvas/utils',
8 'wlxml/wlxml'
9 ], function($, chai, sinon, canvas, documentElement, utils, wlxml) {
10     
11 'use strict';
12 /* global describe, it, beforeEach, afterEach */
13
14 var expect = chai.expect;
15
16 var getCanvasFromXML = function(xml) {
17     return canvas.fromXMLDocument(getDocumentFromXML(xml));
18 };
19
20 var getDocumentFromXML = function(xml) {
21     return wlxml.WLXMLDocumentFromXML(xml);
22 };
23
24 var wait = function(callback, timeout) {
25     /* globals window */
26     return window.setTimeout(callback, timeout || 0.5);
27 };
28
29
30 describe('new Canvas', function() {
31     it('abc', function() {
32         var doc = wlxml.WLXMLDocumentFromXML('<section>Alice <span>has</span> a cat!</div>'),
33             c = canvas.fromXMLDocument(doc);
34
35         expect(c.doc().children()).to.have.length(3);
36         expect(c.doc().children()[0].canvas).to.equal(c);
37     });
38 });
39
40 describe('Handling empty text nodes', function() {
41     it('puts zero width space into node with about to be remove text', function(done) {
42         var c = getCanvasFromXML('<section>Alice</section>'),
43             textElement = c.doc().children()[0];
44         textElement.setText('');
45
46         /* Wait for MutationObserver to kick in. */
47         wait(function() {
48             expect(textElement.getText({raw:true})).to.equal(utils.unicode.ZWS, 'ZWS in canvas');
49             expect(c.wlxmlDocument.root.contents()[0].getText()).to.equal('', 'empty string in a document');
50             done();
51         });
52     });
53 });
54
55 describe('Handling changes to the document', function() {
56     it('replaces the whole canvas content when document root node replaced', function() {
57         var doc = getDocumentFromXML('<section></section>'),
58             c = canvas.fromXMLDocument(doc);
59
60         var header = doc.root.replaceWith({tagName: 'header'});
61         expect(c.doc().data('wlxmlNode').sameNode(header)).to.equal(true);
62     });
63 });
64
65 describe('Listening to document changes', function() {
66
67     it('Handling element node moved', function() {
68         var doc = getDocumentFromXML('<section><a></a><b></b></section>'),
69             a = doc.root.contents()[0],
70             b = doc.root.contents()[1],
71             c = canvas.fromXMLDocument(doc);
72
73         a.before(b);
74         var sectionChildren = c.doc().children();
75         expect(sectionChildren.length).to.equal(2);
76         expect(sectionChildren[0].getWlxmlTag()).to.equal('b');
77         expect(sectionChildren[1].getWlxmlTag()).to.equal('a');
78     });
79
80     it('Handling text node moved', function() {
81         var doc = getDocumentFromXML('<section><a></a>Alice</section>'),
82             a = doc.root.contents()[0],
83             textNode = doc.root.contents()[1],
84             c = canvas.fromXMLDocument(doc);
85
86         a.before(textNode);
87         var sectionChildren = c.doc().children();
88         expect(sectionChildren.length).to.equal(2);
89         expect(sectionChildren[0].getText()).to.equal('Alice');
90         expect(sectionChildren[1].getWlxmlTag()).to.equal('a');
91     });
92
93     it('Handles nodeTagChange event', function() {
94
95         var doc = wlxml.WLXMLDocumentFromXML('<section><div>Alice</div></section>'),
96             c = canvas.fromXMLDocument(doc);
97
98         doc.root.contents()[0].setTag('header');
99
100         var headerNode = doc.root.contents()[0],
101             headerElement = c.doc().children()[0];
102
103         expect(headerElement.getWlxmlTag()).to.equal('header', 'element ok');
104
105         /* Make sure we handle invalidation of reference to wlxmlNode after changing its tag */
106         expect(headerNode.getData('canvasElement').sameNode(headerElement)).to.equal(true, 'node->element');
107         expect(headerElement.data('wlxmlNode').sameNode(headerNode)).to.equal(true, 'element->node');
108     });
109
110     it('Handles nodeDetached event for an empty text node', function(done) {
111         var doc = wlxml.WLXMLDocumentFromXML('<section><div>A<span>b</span></div></section>'),
112             aTextNode = doc.root.contents()[0].contents()[0],
113             aTextElement;
114
115         canvas.fromXMLDocument(doc);
116         aTextElement = utils.findCanvasElementInParent(aTextNode, aTextNode.parent()); // TODO: This really should be easier...
117
118         aTextElement.setText('');
119
120         wait(function() {
121             var parent = aTextElement.parent();
122             expect(aTextElement.getText({raw:true})).to.equal(utils.unicode.ZWS, 'canvas represents this as empty node');
123             aTextElement.data('wlxmlNode').detach();
124             expect(parent.children().length).to.equal(1);
125             expect(parent.children()[0].getWlxmlTag()).to.equal('span');
126             done();
127         });
128     });
129 });
130
131 describe('Cursor', function() {
132     /* globals Node */
133     var getSelection;
134
135     var findTextNode = function(inside, text) {
136         var nodes = inside.find(':not(iframe)').addBack().contents().filter(function() {
137             return this.nodeType === Node.TEXT_NODE && this.data === text;
138         });
139         if(nodes.length) {
140             return nodes[0];
141         }
142         return null;
143     };
144
145     beforeEach(function() {
146         /* globals window */
147         getSelection = sinon.stub(window, 'getSelection');
148     });
149
150     afterEach(function() {
151         getSelection.restore();
152     });
153
154     it('returns position when browser selection collapsed', function() {
155         var c = getCanvasFromXML('<section>Alice has a cat</section>'),
156             dom = c.doc().dom(),
157             text = findTextNode(dom, 'Alice has a cat');
158
159         expect(text.nodeType).to.equal(Node.TEXT_NODE, 'correct node selected');
160         expect($(text).text()).to.equal('Alice has a cat');
161
162         getSelection.returns({
163             anchorNode: text,
164             focusNode: text,
165             anchorOffset: 5,
166             focusOffset: 5,
167             isCollapsed: true
168         });
169         var cursor = c.getCursor(),
170             position = cursor.getPosition();
171
172         expect(cursor.isSelecting()).to.equal(false, 'cursor is not selecting anything');
173         expect(position.element.getText()).to.equal('Alice has a cat');
174         expect(position.offset).to.equal(5);
175         expect(position.offsetAtEnd).to.equal(false, 'offset is not at end');
176
177         getSelection.returns({
178             anchorNode: text,
179             focusNode: text,
180             anchorOffset: 15,
181             focusOffset: 15,
182             isCollapsed: true
183         });
184
185         expect(cursor.getPosition().offsetAtEnd).to.equal(true, 'offset at end');
186     });
187
188     it('recognizes selection start and end on document order', function() {
189         var c = getCanvasFromXML('<section><span>Alice</span><span>has a cat</span><div>abc<span>...</span>cde</div></section>'),
190             dom = c.doc().dom(),
191             textFirst = findTextNode(dom, 'Alice'),
192             textSecond = findTextNode(dom, 'has a cat'),
193             textAbc = findTextNode(dom, 'abc'),
194             textCde = findTextNode(dom, 'cde'),
195             cursor, label;
196
197         var check = function(label, expected) {
198             var cursor = c.getCursor();
199             label = label + ': ';
200             expect(cursor.getSelectionStart().element.getText()).to.equal(expected.start.text, label + 'start element ok');
201             expect(cursor.getSelectionStart().offset).to.equal(expected.start.offset, label + 'start offset ok');
202             expect(cursor.getSelectionEnd().element.getText()).to.equal(expected.end.text, label + 'end element ok');
203             expect(cursor.getSelectionEnd().offset).to.equal(expected.end.offset, label + 'end offset ok');
204         }
205
206         getSelection.returns({
207             anchorNode: textFirst,
208             focusNode: textFirst,
209             anchorOffset: 1,
210             focusOffset: 3,
211             isCollapsed: false
212         });
213
214         check('same element, anchor first', {
215             start: {text: 'Alice', offset: 1},
216             end: {text: 'Alice', offset:3}
217         });
218
219
220         getSelection.returns({
221             anchorNode: textFirst,
222             focusNode: textFirst,
223             anchorOffset: 3,
224             focusOffset: 1,
225             isCollapsed: false
226         });
227
228         check('same element, anchor second', {
229             start: {text: 'Alice', offset: 1},
230             end: {text: 'Alice', offset:3}
231         });
232
233
234         getSelection.returns({
235             anchorNode: textAbc,
236             focusNode: textCde,
237             anchorOffset: 3,
238             focusOffset: 1,
239             isCollapsed: false
240         });
241
242         check('same parent, anchor first', {
243             start: {text: 'abc', offset: 3},
244             end: {text: 'cde', offset:1}
245         });
246
247
248         getSelection.returns({
249             anchorNode: textCde,
250             focusNode: textAbc,
251             anchorOffset: 1,
252             focusOffset: 3,
253             isCollapsed: false
254         });
255
256         check('same parent, anchor second', {
257             start: {text: 'abc', offset: 3},
258             end: {text: 'cde', offset:1}
259         });
260
261
262         getSelection.returns({
263             anchorNode: textFirst,
264             focusNode: textSecond,
265             anchorOffset: 1,
266             focusOffset: 3,
267             isCollapsed: false
268         });
269
270         check('different parents, anchor first', {
271             start: {text: 'Alice', offset: 1},
272             end: {text: 'has a cat', offset:3}
273         });
274
275
276         getSelection.returns({
277             anchorNode: textSecond,
278             focusNode: textFirst,
279             anchorOffset: 3,
280             focusOffset: 1,
281             isCollapsed: false
282         });
283
284         check('different parents, anchor second', {
285             start: {text: 'Alice', offset: 1},
286             end: {text: 'has a cat', offset:3}
287         });
288     });
289
290     it('returns boundries of selection when browser selection not collapsed', function() {
291         var c = getCanvasFromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
292             dom = c.doc().dom(),
293             text = {
294                 alice: findTextNode(dom, 'Alice '),
295                 has: findTextNode(dom, 'has'),
296                 cat: findTextNode(dom, ' cat')
297             },
298             cursor = c.getCursor(),
299             aliceElement = c.getDocumentElement(text.alice),
300             catElement = c.getDocumentElement(text.cat);
301
302         [
303             {focus: text.alice, focusOffset: 1, anchor: text.cat,   anchorOffset: 2, selectionAnchor: catElement},
304             {focus: text.cat,   focusOffset: 2, anchor: text.alice, anchorOffset: 1, selectionAnchor: aliceElement}
305         ].forEach(function(s, idx) {
306             getSelection.returns({isColapsed: false, anchorNode: s.anchor, anchorOffset: s.anchorOffset, focusNode: s.focus, focusOffset: s.focusOffset});
307
308             var selectionStart = cursor.getSelectionStart(),
309                 selectionEnd = cursor.getSelectionEnd(),
310                 selectionAnchor = cursor.getSelectionAnchor();
311
312             expect(cursor.isSelecting()).to.equal(true, 'cursor is selecting');
313             expect(selectionStart.element.sameNode(aliceElement)).to.equal(true, '"Alice" is the start of the selection ' + idx);
314             expect(selectionStart.offset).to.equal(1, '"Alice" offset ok' + idx);
315             expect(selectionEnd.element.sameNode(catElement)).to.equal(true, '"Cat" is the start of the selection ' + idx);
316             expect(selectionEnd.offset).to.equal(2, '"Cat" offset ok' + idx);
317             expect(selectionAnchor.element.sameNode(s.selectionAnchor)).to.equal(true, 'anchor ok');
318             expect(selectionAnchor.offset).to.equal(s.anchorOffset, 'anchor offset ok');
319         });
320     });
321
322     it('recognizes when browser selection boundries lies in sibling DocumentTextElements', function() {
323         var c = getCanvasFromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
324             dom = c.doc().dom(),
325             text = {
326                 alice: findTextNode(dom, 'Alice '),
327                 has: findTextNode(dom, 'has'),
328                 a: findTextNode(dom, ' a '),
329                 big: findTextNode(dom, 'big'),
330                 cat: findTextNode(dom, ' cat'),
331             },
332             cursor = c.getCursor();
333
334         expect($(text.alice).text()).to.equal('Alice ');
335         expect($(text.has).text()).to.equal('has');
336         expect($(text.a).text()).to.equal(' a ');
337         expect($(text.big).text()).to.equal('big');
338         expect($(text.cat).text()).to.equal(' cat');
339
340         getSelection.returns({anchorNode: text.alice, focusNode: text.a});
341         expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "a" are children');
342
343         getSelection.returns({anchorNode: text.alice, focusNode: text.cat});
344         expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "cat" are children');
345
346         getSelection.returns({anchorNode: text.alice, focusNode: text.has});
347         expect(cursor.isSelectingSiblings()).to.equal(false, '"Alice" and "has" are not children');
348
349         getSelection.returns({anchorNode: text.has, focusNode: text.big});
350         expect(cursor.isSelectingSiblings()).to.equal(false, '"has" and "big" are not children');
351     });
352 });
353
354 });