5 'modules/documentCanvas/canvas/canvas',
6 'modules/documentCanvas/canvas/documentElement',
7 'modules/documentCanvas/canvas/utils',
9 ], function($, chai, sinon, canvas, documentElement, utils, wlxml) {
12 /* global describe, it, beforeEach, afterEach */
14 var expect = chai.expect;
16 var getCanvasFromXML = function(xml) {
17 return canvas.fromXMLDocument(wlxml.WLXMLDocumentFromXML(xml));
20 var wait = function(callback, timeout) {
21 return window.setTimeout(callback, timeout || 0.5);
25 describe('new Canvas', function() {
26 it('abc', function() {
27 var doc = wlxml.WLXMLDocumentFromXML('<section>Alice <span>has</span> a cat!</div>'),
28 c = canvas.fromXMLDocument(doc);
30 expect(c.doc().children()).to.have.length(3);
34 describe('Handling empty text nodes', function() {
35 it('puts zero width space into node with about to be remove text', function(done) {
36 var c = getCanvasFromXML('<section>Alice</section>'),
37 textElement = c.doc().children()[0];
38 textElement.setText('');
40 /* Wait for MutationObserver to kick in. */
42 expect(textElement.getText({raw:true})).to.equal(utils.unicode.ZWS, 'ZWS in canvas');
43 expect(c.wlxmlDocument.root.contents()[0].getText()).to.equal('', 'empty string in a document');
49 describe('Cursor', function() {
53 var findTextNode = function(inside, text) {
54 var nodes = inside.find(':not(iframe)').addBack().contents().filter(function() {
55 return this.nodeType === Node.TEXT_NODE && this.data === text;
63 beforeEach(function() {
64 getSelection = sinon.stub(window, 'getSelection');
67 afterEach(function() {
68 getSelection.restore();
71 it('returns position when browser selection collapsed', function() {
72 var c = getCanvasFromXML('<section>Alice has a cat</section>'),
74 text = findTextNode(dom, 'Alice has a cat');
76 expect(text.nodeType).to.equal(Node.TEXT_NODE, 'correct node selected');
77 expect($(text).text()).to.equal('Alice has a cat');
79 getSelection.returns({
86 var cursor = c.getCursor(),
87 position = cursor.getPosition();
89 expect(cursor.isSelecting()).to.equal(false, 'cursor is not selecting anything');
90 expect(position.element.getText()).to.equal('Alice has a cat');
91 expect(position.offset).to.equal(5);
92 expect(position.offsetAtEnd).to.equal(false, 'offset is not at end');
94 getSelection.returns({
102 expect(cursor.getPosition().offsetAtEnd).to.equal(true, 'offset at end');
105 it('returns boundries of selection when browser selection not collapsed', function() {
106 var c = getCanvasFromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
109 alice: findTextNode(dom, 'Alice '),
110 has: findTextNode(dom, 'has'),
111 cat: findTextNode(dom, ' cat')
113 cursor = c.getCursor(),
114 aliceElement = c.getDocumentElement(text.alice),
115 catElement = c.getDocumentElement(text.cat);
118 {focus: text.alice, focusOffset: 1, anchor: text.cat, anchorOffset: 2, selectionAnchor: catElement},
119 {focus: text.cat, focusOffset: 2, anchor: text.alice, anchorOffset: 1, selectionAnchor: aliceElement}
120 ].forEach(function(s, idx) {
121 getSelection.returns({isColapsed: false, anchorNode: s.anchor, anchorOffset: s.anchorOffset, focusNode: s.focus, focusOffset: s.focusOffset});
123 var selectionStart = cursor.getSelectionStart(),
124 selectionEnd = cursor.getSelectionEnd(),
125 selectionAnchor = cursor.getSelectionAnchor();
127 expect(cursor.isSelecting()).to.equal(true, 'cursor is selecting');
128 expect(selectionStart.element.sameNode(aliceElement)).to.equal(true, '"Alice" is the start of the selection ' + idx);
129 expect(selectionStart.offset).to.equal(1, '"Alice" offset ok' + idx);
130 expect(selectionEnd.element.sameNode(catElement)).to.equal(true, '"Cat" is the start of the selection ' + idx);
131 expect(selectionEnd.offset).to.equal(2, '"Cat" offset ok' + idx);
132 expect(selectionAnchor.element.sameNode(s.selectionAnchor)).to.equal(true, 'anchor ok');
133 expect(selectionAnchor.offset).to.equal(s.anchorOffset, 'anchor offset ok');
137 it('recognizes when browser selection boundries lies in sibling DocumentTextElements', function() {
138 var c = getCanvasFromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
141 alice: findTextNode(dom, 'Alice '),
142 has: findTextNode(dom, 'has'),
143 a: findTextNode(dom, ' a '),
144 big: findTextNode(dom, 'big'),
145 cat: findTextNode(dom, ' cat'),
147 cursor = c.getCursor();
149 expect($(text.alice).text()).to.equal('Alice ');
150 expect($(text.has).text()).to.equal('has');
151 expect($(text.a).text()).to.equal(' a ');
152 expect($(text.big).text()).to.equal('big');
153 expect($(text.cat).text()).to.equal(' cat');
155 getSelection.returns({anchorNode: text.alice, focusNode: text.a});
156 expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "a" are children');
158 getSelection.returns({anchorNode: text.alice, focusNode: text.cat});
159 expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "cat" are children');
161 getSelection.returns({anchorNode: text.alice, focusNode: text.has});
162 expect(cursor.isSelectingSiblings()).to.equal(false, '"Alice" and "has" are not children');
164 getSelection.returns({anchorNode: text.has, focusNode: text.big});
165 expect(cursor.isSelectingSiblings()).to.equal(false, '"has" and "big" are not children');