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     it('Handling element node moved', function() {
65         var doc = getDocumentFromXML('<section><a></a><b></b></section>'),
66             a = doc.root.contents()[0],
67             b = doc.root.contents()[1],
68             c = canvas.fromXMLDocument(doc);
69
70         a.before(b);
71         var sectionChildren = c.doc().children();
72         expect(sectionChildren.length).to.equal(2);
73         expect(sectionChildren[0].getWlxmlTag()).to.equal('b');
74         expect(sectionChildren[1].getWlxmlTag()).to.equal('a');
75     });
76     it('Handling text node moved', function() {
77         var doc = getDocumentFromXML('<section><a></a>Alice</section>'),
78             a = doc.root.contents()[0],
79             textNode = doc.root.contents()[1],
80             c = canvas.fromXMLDocument(doc);
81
82         a.before(textNode);
83         var sectionChildren = c.doc().children();
84         expect(sectionChildren.length).to.equal(2);
85         expect(sectionChildren[0].getText()).to.equal('Alice');
86         expect(sectionChildren[1].getWlxmlTag()).to.equal('a');
87     });
88 });
89
90 describe('Cursor', function() {
91
92     var getSelection;
93
94     var findTextNode = function(inside, text) {
95         var nodes = inside.find(':not(iframe)').addBack().contents().filter(function() {
96             return this.nodeType === Node.TEXT_NODE && this.data === text;
97         });
98         if(nodes.length) {
99             return nodes[0];
100         }
101         return null;
102     };
103
104     beforeEach(function() {
105         getSelection = sinon.stub(window, 'getSelection');
106     });
107
108     afterEach(function() {
109         getSelection.restore();
110     });
111
112     it('returns position when browser selection collapsed', function() {
113         var c = getCanvasFromXML('<section>Alice has a cat</section>'),
114             dom = c.doc().dom(),
115             text = findTextNode(dom, 'Alice has a cat');
116
117         expect(text.nodeType).to.equal(Node.TEXT_NODE, 'correct node selected');
118         expect($(text).text()).to.equal('Alice has a cat');
119
120         getSelection.returns({
121             anchorNode: text,
122             focusNode: text,
123             anchorOffset: 5,
124             focusOffset: 5,
125             isCollapsed: true
126         });
127         var cursor = c.getCursor(),
128             position = cursor.getPosition();
129
130         expect(cursor.isSelecting()).to.equal(false, 'cursor is not selecting anything');
131         expect(position.element.getText()).to.equal('Alice has a cat');
132         expect(position.offset).to.equal(5);
133         expect(position.offsetAtEnd).to.equal(false, 'offset is not at end');
134
135         getSelection.returns({
136             anchorNode: text,
137             focusNode: text,
138             anchorOffset: 15,
139             focusOffset: 15,
140             isCollapsed: true
141         });
142
143         expect(cursor.getPosition().offsetAtEnd).to.equal(true, 'offset at end');
144     });
145
146     it('returns boundries of selection when browser selection not collapsed', function() {
147         var c = getCanvasFromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
148             dom = c.doc().dom(),
149             text = {
150                 alice: findTextNode(dom, 'Alice '),
151                 has: findTextNode(dom, 'has'),
152                 cat: findTextNode(dom, ' cat')
153             },
154             cursor = c.getCursor(),
155             aliceElement = c.getDocumentElement(text.alice),
156             catElement = c.getDocumentElement(text.cat);
157
158         [
159             {focus: text.alice, focusOffset: 1, anchor: text.cat,   anchorOffset: 2, selectionAnchor: catElement},
160             {focus: text.cat,   focusOffset: 2, anchor: text.alice, anchorOffset: 1, selectionAnchor: aliceElement}
161         ].forEach(function(s, idx) {
162             getSelection.returns({isColapsed: false, anchorNode: s.anchor, anchorOffset: s.anchorOffset, focusNode: s.focus, focusOffset: s.focusOffset});
163
164             var selectionStart = cursor.getSelectionStart(),
165                 selectionEnd = cursor.getSelectionEnd(),
166                 selectionAnchor = cursor.getSelectionAnchor();
167
168             expect(cursor.isSelecting()).to.equal(true, 'cursor is selecting');
169             expect(selectionStart.element.sameNode(aliceElement)).to.equal(true, '"Alice" is the start of the selection ' + idx);
170             expect(selectionStart.offset).to.equal(1, '"Alice" offset ok' + idx);
171             expect(selectionEnd.element.sameNode(catElement)).to.equal(true, '"Cat" is the start of the selection ' + idx);
172             expect(selectionEnd.offset).to.equal(2, '"Cat" offset ok' + idx);
173             expect(selectionAnchor.element.sameNode(s.selectionAnchor)).to.equal(true, 'anchor ok');
174             expect(selectionAnchor.offset).to.equal(s.anchorOffset, 'anchor offset ok');
175         });
176     });
177
178     it('recognizes when browser selection boundries lies in sibling DocumentTextElements', function() {
179         var c = getCanvasFromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
180             dom = c.doc().dom(),
181             text = {
182                 alice: findTextNode(dom, 'Alice '),
183                 has: findTextNode(dom, 'has'),
184                 a: findTextNode(dom, ' a '),
185                 big: findTextNode(dom, 'big'),
186                 cat: findTextNode(dom, ' cat'),
187             },
188             cursor = c.getCursor();
189
190         expect($(text.alice).text()).to.equal('Alice ');
191         expect($(text.has).text()).to.equal('has');
192         expect($(text.a).text()).to.equal(' a ');
193         expect($(text.big).text()).to.equal('big');
194         expect($(text.cat).text()).to.equal(' cat');
195
196         getSelection.returns({anchorNode: text.alice, focusNode: text.a});
197         expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "a" are children');
198
199         getSelection.returns({anchorNode: text.alice, focusNode: text.cat});
200         expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "cat" are children');
201
202         getSelection.returns({anchorNode: text.alice, focusNode: text.has});
203         expect(cursor.isSelectingSiblings()).to.equal(false, '"Alice" and "has" are not children');
204
205         getSelection.returns({anchorNode: text.has, focusNode: text.big});
206         expect(cursor.isSelectingSiblings()).to.equal(false, '"has" and "big" are not children');
207     });
208 });
209
210 });