Bringing back cursor tests
[fnpeditor.git] / src / editor / modules / documentCanvas / canvas / canvas.test.js
1 define([
2 'libs/chai',
3 'modules/documentCanvas/canvas/canvas',
4 'modules/documentCanvas/canvas/documentElement',
5 'modules/documentCanvas/canvas/utils',
6 'wlxml/wlxml'
7 ], function(chai, canvas, documentElement, utils, wlxml) {
8     
9 'use strict';
10
11 var expect = chai.expect;
12
13
14 var nodeFromXML = function(xml) {
15     return wlxml.WLXMLElementNodeFromXML(xml);
16 };
17
18 var getCanvasFromXML = function(xml) {
19     return canvas.fromXMLDocument(wlxml.WLXMLDocumentFromXML(xml));
20 };
21
22
23 describe('new Canvas', function() {
24     it('abc', function() {
25         var doc = wlxml.WLXMLDocumentFromXML('<section>Alice <span>has</span> a cat!</div>'),
26             c = canvas.fromXMLDocument(doc);
27
28         expect(c.doc().children()).to.have.length(3)
29     });
30 })
31
32 describe('Cursor', function() {
33
34     var getSelection;
35
36     var findTextNode = function(inside, text) {
37         var nodes = inside.find(':not(iframe)').addBack().contents().filter(function() {
38             return this.nodeType === Node.TEXT_NODE && this.data === text;
39         });
40         if(nodes.length)
41             return nodes[0];
42         return null;
43     }
44
45     beforeEach(function() {
46         getSelection = sinon.stub(window, 'getSelection');
47     });
48
49     afterEach(function() {
50         getSelection.restore();
51     });
52
53     it('returns position when browser selection collapsed', function() {
54         var c = getCanvasFromXML('<section>Alice has a cat</section>'),
55             dom = c.doc().dom(),
56             text = findTextNode(dom, 'Alice has a cat');
57
58         expect(text.nodeType).to.equal(Node.TEXT_NODE, 'correct node selected');
59         expect($(text).text()).to.equal('Alice has a cat');
60
61         getSelection.returns({
62             anchorNode: text,
63             focusNode: text,
64             anchorOffset: 5,
65             focusOffset: 5,
66             isCollapsed: true
67         });
68         var cursor = c.getCursor(),
69             position = cursor.getPosition();
70
71         expect(cursor.isSelecting()).to.equal(false, 'cursor is not selecting anything');
72         expect(position.element.getText()).to.equal('Alice has a cat');
73         expect(position.offset).to.equal(5);
74         expect(position.offsetAtEnd).to.equal(false, 'offset is not at end');
75
76         getSelection.returns({
77             anchorNode: text,
78             focusNode: text,
79             anchorOffset: 15,
80             focusOffset: 15,
81             isCollapsed: true
82         });
83
84         expect(cursor.getPosition().offsetAtEnd).to.equal(true, 'offset at end');
85     });
86
87     it('returns boundries of selection when browser selection not collapsed', function() {
88         var c = getCanvasFromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
89             dom = c.doc().dom(),
90             text = {
91                 alice: findTextNode(dom, 'Alice '),
92                 has: findTextNode(dom, 'has'),
93                 cat: findTextNode(dom, ' cat')
94             },
95             cursor = c.getCursor(),
96             aliceElement = c.getDocumentElement(text.alice),
97             catElement = c.getDocumentElement(text.cat);
98
99         [
100             {focus: text.alice, focusOffset: 1, anchor: text.cat,   anchorOffset: 2, selectionAnchor: catElement},
101             {focus: text.cat,   focusOffset: 2, anchor: text.alice, anchorOffset: 1, selectionAnchor: aliceElement}
102         ].forEach(function(s, idx) {
103             getSelection.returns({isColapsed: false, anchorNode: s.anchor, anchorOffset: s.anchorOffset, focusNode: s.focus, focusOffset: s.focusOffset});
104
105             var selectionStart = cursor.getSelectionStart(),
106                 selectionEnd = cursor.getSelectionEnd(),
107                 selectionAnchor = cursor.getSelectionAnchor();
108
109             expect(cursor.isSelecting()).to.equal(true, 'cursor is selecting');
110             expect(selectionStart.element.sameNode(aliceElement)).to.equal(true, '"Alice" is the start of the selection ' + idx);
111             expect(selectionStart.offset).to.equal(1, '"Alice" offset ok' + idx);
112             expect(selectionEnd.element.sameNode(catElement)).to.equal(true, '"Cat" is the start of the selection ' + idx);
113             expect(selectionEnd.offset).to.equal(2, '"Cat" offset ok' + idx);
114             expect(selectionAnchor.element.sameNode(s.selectionAnchor)).to.equal(true, 'anchor ok');
115             expect(selectionAnchor.offset).to.equal(s.anchorOffset, 'anchor offset ok');
116         });
117     });
118
119     it('recognizes when browser selection boundries lies in sibling DocumentTextElements', 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                 a: findTextNode(dom, ' a '),
126                 big: findTextNode(dom, 'big'),
127                 cat: findTextNode(dom, ' cat'),
128             },
129             cursor = c.getCursor();
130
131         expect($(text.alice).text()).to.equal('Alice ');
132         expect($(text.has).text()).to.equal('has');
133         expect($(text.a).text()).to.equal(' a ');
134         expect($(text.big).text()).to.equal('big');
135         expect($(text.cat).text()).to.equal(' cat');
136
137         getSelection.returns({anchorNode: text.alice, focusNode: text.a});
138         expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "a" are children');
139
140         getSelection.returns({anchorNode: text.alice, focusNode: text.cat});
141         expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "cat" are children');
142
143         getSelection.returns({anchorNode: text.alice, focusNode: text.has});
144         expect(cursor.isSelectingSiblings()).to.equal(false, '"Alice" and "has" are not children');
145
146         getSelection.returns({anchorNode: text.has, focusNode: text.big});
147         expect(cursor.isSelectingSiblings()).to.equal(false, '"has" and "big" are not children');
148     });
149 });
150
151 });