editor: canvas - fix problem with detaching empty text nodes
[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('returns boundries of selection when browser selection not collapsed', function() {
189         var c = getCanvasFromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
190             dom = c.doc().dom(),
191             text = {
192                 alice: findTextNode(dom, 'Alice '),
193                 has: findTextNode(dom, 'has'),
194                 cat: findTextNode(dom, ' cat')
195             },
196             cursor = c.getCursor(),
197             aliceElement = c.getDocumentElement(text.alice),
198             catElement = c.getDocumentElement(text.cat);
199
200         [
201             {focus: text.alice, focusOffset: 1, anchor: text.cat,   anchorOffset: 2, selectionAnchor: catElement},
202             {focus: text.cat,   focusOffset: 2, anchor: text.alice, anchorOffset: 1, selectionAnchor: aliceElement}
203         ].forEach(function(s, idx) {
204             getSelection.returns({isColapsed: false, anchorNode: s.anchor, anchorOffset: s.anchorOffset, focusNode: s.focus, focusOffset: s.focusOffset});
205
206             var selectionStart = cursor.getSelectionStart(),
207                 selectionEnd = cursor.getSelectionEnd(),
208                 selectionAnchor = cursor.getSelectionAnchor();
209
210             expect(cursor.isSelecting()).to.equal(true, 'cursor is selecting');
211             expect(selectionStart.element.sameNode(aliceElement)).to.equal(true, '"Alice" is the start of the selection ' + idx);
212             expect(selectionStart.offset).to.equal(1, '"Alice" offset ok' + idx);
213             expect(selectionEnd.element.sameNode(catElement)).to.equal(true, '"Cat" is the start of the selection ' + idx);
214             expect(selectionEnd.offset).to.equal(2, '"Cat" offset ok' + idx);
215             expect(selectionAnchor.element.sameNode(s.selectionAnchor)).to.equal(true, 'anchor ok');
216             expect(selectionAnchor.offset).to.equal(s.anchorOffset, 'anchor offset ok');
217         });
218     });
219
220     it('recognizes when browser selection boundries lies in sibling DocumentTextElements', function() {
221         var c = getCanvasFromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
222             dom = c.doc().dom(),
223             text = {
224                 alice: findTextNode(dom, 'Alice '),
225                 has: findTextNode(dom, 'has'),
226                 a: findTextNode(dom, ' a '),
227                 big: findTextNode(dom, 'big'),
228                 cat: findTextNode(dom, ' cat'),
229             },
230             cursor = c.getCursor();
231
232         expect($(text.alice).text()).to.equal('Alice ');
233         expect($(text.has).text()).to.equal('has');
234         expect($(text.a).text()).to.equal(' a ');
235         expect($(text.big).text()).to.equal('big');
236         expect($(text.cat).text()).to.equal(' cat');
237
238         getSelection.returns({anchorNode: text.alice, focusNode: text.a});
239         expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "a" are children');
240
241         getSelection.returns({anchorNode: text.alice, focusNode: text.cat});
242         expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "cat" are children');
243
244         getSelection.returns({anchorNode: text.alice, focusNode: text.has});
245         expect(cursor.isSelectingSiblings()).to.equal(false, '"Alice" and "has" are not children');
246
247         getSelection.returns({anchorNode: text.has, focusNode: text.big});
248         expect(cursor.isSelectingSiblings()).to.equal(false, '"has" and "big" are not children');
249     });
250 });
251
252 });