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