canvas: Fixing nodeMoved event handling
[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     return window.setTimeout(callback, timeout || 0.5);
26 };
27
28
29 describe('new Canvas', function() {
30     it('abc', function() {
31         var doc = wlxml.WLXMLDocumentFromXML('<section>Alice <span>has</span> a cat!</div>'),
32             c = canvas.fromXMLDocument(doc);
33
34         expect(c.doc().children()).to.have.length(3);
35     });
36 });
37
38 describe('Handling empty text nodes', function() {
39     it('puts zero width space into node with about to be remove text', function(done) {
40         var c = getCanvasFromXML('<section>Alice</section>'),
41             textElement = c.doc().children()[0];
42         textElement.setText('');
43
44         /* Wait for MutationObserver to kick in. */
45         wait(function() {
46             expect(textElement.getText({raw:true})).to.equal(utils.unicode.ZWS, 'ZWS in canvas');
47             expect(c.wlxmlDocument.root.contents()[0].getText()).to.equal('', 'empty string in a document');
48             done();
49         });
50     });
51 });
52
53 describe('Handling changes to the document', function() {
54     it('replaces the whole canvas content when document root node replaced', function() {
55         var doc = getDocumentFromXML('<section></section>'),
56             c = canvas.fromXMLDocument(doc);
57
58         var header = doc.root.replaceWith({tagName: 'header'});
59         expect(c.doc().data('wlxmlNode').sameNode(header)).to.be.true;
60     });
61 });
62
63 describe('Listening to document changes', function() {
64
65     it('Handling element node moved', function() {
66         var doc = getDocumentFromXML('<section><a></a><b></b></section>'),
67             a = doc.root.contents()[0],
68             b = doc.root.contents()[1],
69             c = canvas.fromXMLDocument(doc);
70
71         a.before(b);
72         var sectionChildren = c.doc().children();
73         expect(sectionChildren.length).to.equal(2);
74         expect(sectionChildren[0].getWlxmlTag()).to.equal('b');
75         expect(sectionChildren[1].getWlxmlTag()).to.equal('a');
76     });
77
78     it('Handling text node moved', function() {
79         var doc = getDocumentFromXML('<section><a></a>Alice</section>'),
80             a = doc.root.contents()[0],
81             textNode = doc.root.contents()[1],
82             c = canvas.fromXMLDocument(doc);
83
84         a.before(textNode);
85         var sectionChildren = c.doc().children();
86         expect(sectionChildren.length).to.equal(2);
87         expect(sectionChildren[0].getText()).to.equal('Alice');
88         expect(sectionChildren[1].getWlxmlTag()).to.equal('a');
89     });
90 });
91
92 describe('Cursor', function() {
93
94     var getSelection;
95
96     var findTextNode = function(inside, text) {
97         var nodes = inside.find(':not(iframe)').addBack().contents().filter(function() {
98             return this.nodeType === Node.TEXT_NODE && this.data === text;
99         });
100         if(nodes.length) {
101             return nodes[0];
102         }
103         return null;
104     };
105
106     beforeEach(function() {
107         getSelection = sinon.stub(window, 'getSelection');
108     });
109
110     afterEach(function() {
111         getSelection.restore();
112     });
113
114     it('returns position when browser selection collapsed', function() {
115         var c = getCanvasFromXML('<section>Alice has a cat</section>'),
116             dom = c.doc().dom(),
117             text = findTextNode(dom, 'Alice has a cat');
118
119         expect(text.nodeType).to.equal(Node.TEXT_NODE, 'correct node selected');
120         expect($(text).text()).to.equal('Alice has a cat');
121
122         getSelection.returns({
123             anchorNode: text,
124             focusNode: text,
125             anchorOffset: 5,
126             focusOffset: 5,
127             isCollapsed: true
128         });
129         var cursor = c.getCursor(),
130             position = cursor.getPosition();
131
132         expect(cursor.isSelecting()).to.equal(false, 'cursor is not selecting anything');
133         expect(position.element.getText()).to.equal('Alice has a cat');
134         expect(position.offset).to.equal(5);
135         expect(position.offsetAtEnd).to.equal(false, 'offset is not at end');
136
137         getSelection.returns({
138             anchorNode: text,
139             focusNode: text,
140             anchorOffset: 15,
141             focusOffset: 15,
142             isCollapsed: true
143         });
144
145         expect(cursor.getPosition().offsetAtEnd).to.equal(true, 'offset at end');
146     });
147
148     it('returns boundries of selection when browser selection not collapsed', function() {
149         var c = getCanvasFromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
150             dom = c.doc().dom(),
151             text = {
152                 alice: findTextNode(dom, 'Alice '),
153                 has: findTextNode(dom, 'has'),
154                 cat: findTextNode(dom, ' cat')
155             },
156             cursor = c.getCursor(),
157             aliceElement = c.getDocumentElement(text.alice),
158             catElement = c.getDocumentElement(text.cat);
159
160         [
161             {focus: text.alice, focusOffset: 1, anchor: text.cat,   anchorOffset: 2, selectionAnchor: catElement},
162             {focus: text.cat,   focusOffset: 2, anchor: text.alice, anchorOffset: 1, selectionAnchor: aliceElement}
163         ].forEach(function(s, idx) {
164             getSelection.returns({isColapsed: false, anchorNode: s.anchor, anchorOffset: s.anchorOffset, focusNode: s.focus, focusOffset: s.focusOffset});
165
166             var selectionStart = cursor.getSelectionStart(),
167                 selectionEnd = cursor.getSelectionEnd(),
168                 selectionAnchor = cursor.getSelectionAnchor();
169
170             expect(cursor.isSelecting()).to.equal(true, 'cursor is selecting');
171             expect(selectionStart.element.sameNode(aliceElement)).to.equal(true, '"Alice" is the start of the selection ' + idx);
172             expect(selectionStart.offset).to.equal(1, '"Alice" offset ok' + idx);
173             expect(selectionEnd.element.sameNode(catElement)).to.equal(true, '"Cat" is the start of the selection ' + idx);
174             expect(selectionEnd.offset).to.equal(2, '"Cat" offset ok' + idx);
175             expect(selectionAnchor.element.sameNode(s.selectionAnchor)).to.equal(true, 'anchor ok');
176             expect(selectionAnchor.offset).to.equal(s.anchorOffset, 'anchor offset ok');
177         });
178     });
179
180     it('recognizes when browser selection boundries lies in sibling DocumentTextElements', function() {
181         var c = getCanvasFromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
182             dom = c.doc().dom(),
183             text = {
184                 alice: findTextNode(dom, 'Alice '),
185                 has: findTextNode(dom, 'has'),
186                 a: findTextNode(dom, ' a '),
187                 big: findTextNode(dom, 'big'),
188                 cat: findTextNode(dom, ' cat'),
189             },
190             cursor = c.getCursor();
191
192         expect($(text.alice).text()).to.equal('Alice ');
193         expect($(text.has).text()).to.equal('has');
194         expect($(text.a).text()).to.equal(' a ');
195         expect($(text.big).text()).to.equal('big');
196         expect($(text.cat).text()).to.equal(' cat');
197
198         getSelection.returns({anchorNode: text.alice, focusNode: text.a});
199         expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "a" are children');
200
201         getSelection.returns({anchorNode: text.alice, focusNode: text.cat});
202         expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "cat" are children');
203
204         getSelection.returns({anchorNode: text.alice, focusNode: text.has});
205         expect(cursor.isSelectingSiblings()).to.equal(false, '"Alice" and "has" are not children');
206
207         getSelection.returns({anchorNode: text.has, focusNode: text.big});
208         expect(cursor.isSelectingSiblings()).to.equal(false, '"has" and "big" are not children');
209     });
210 });
211
212 });