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(getDocumentFromXML(xml));
20 var getDocumentFromXML = function(xml) {
21 return wlxml.WLXMLDocumentFromXML(xml);
24 var wait = function(callback, timeout) {
26 return window.setTimeout(callback, timeout || 0.5);
30 describe('new Canvas', function() {
31 it('abc', function() {
32 var doc = wlxml.WLXMLDocumentFromXML('<section>Alice <span>has</span> a cat!</div>'),
33 c = canvas.fromXMLDocument(doc);
35 expect(c.doc().children()).to.have.length(3);
39 describe('Handling empty text nodes', function() {
40 it('puts zero width space into node with about to be remove text', function(done) {
41 var c = getCanvasFromXML('<section>Alice</section>'),
42 textElement = c.doc().children()[0];
43 textElement.setText('');
45 /* Wait for MutationObserver to kick in. */
47 expect(textElement.getText({raw:true})).to.equal(utils.unicode.ZWS, 'ZWS in canvas');
48 expect(c.wlxmlDocument.root.contents()[0].getText()).to.equal('', 'empty string in a document');
54 describe('Handling changes to the document', function() {
55 it('replaces the whole canvas content when document root node replaced', function() {
56 var doc = getDocumentFromXML('<section></section>'),
57 c = canvas.fromXMLDocument(doc);
59 var header = doc.root.replaceWith({tagName: 'header'});
60 expect(c.doc().data('wlxmlNode').sameNode(header)).to.equal(true);
64 describe('Listening to document changes', function() {
66 it('Handling element node moved', function() {
67 var doc = getDocumentFromXML('<section><a></a><b></b></section>'),
68 a = doc.root.contents()[0],
69 b = doc.root.contents()[1],
70 c = canvas.fromXMLDocument(doc);
73 var sectionChildren = c.doc().children();
74 expect(sectionChildren.length).to.equal(2);
75 expect(sectionChildren[0].getWlxmlTag()).to.equal('b');
76 expect(sectionChildren[1].getWlxmlTag()).to.equal('a');
79 it('Handling text node moved', function() {
80 var doc = getDocumentFromXML('<section><a></a>Alice</section>'),
81 a = doc.root.contents()[0],
82 textNode = doc.root.contents()[1],
83 c = canvas.fromXMLDocument(doc);
86 var sectionChildren = c.doc().children();
87 expect(sectionChildren.length).to.equal(2);
88 expect(sectionChildren[0].getText()).to.equal('Alice');
89 expect(sectionChildren[1].getWlxmlTag()).to.equal('a');
93 describe('Cursor', function() {
97 var findTextNode = function(inside, text) {
98 var nodes = inside.find(':not(iframe)').addBack().contents().filter(function() {
99 return this.nodeType === Node.TEXT_NODE && this.data === text;
107 beforeEach(function() {
109 getSelection = sinon.stub(window, 'getSelection');
112 afterEach(function() {
113 getSelection.restore();
116 it('returns position when browser selection collapsed', function() {
117 var c = getCanvasFromXML('<section>Alice has a cat</section>'),
119 text = findTextNode(dom, 'Alice has a cat');
121 expect(text.nodeType).to.equal(Node.TEXT_NODE, 'correct node selected');
122 expect($(text).text()).to.equal('Alice has a cat');
124 getSelection.returns({
131 var cursor = c.getCursor(),
132 position = cursor.getPosition();
134 expect(cursor.isSelecting()).to.equal(false, 'cursor is not selecting anything');
135 expect(position.element.getText()).to.equal('Alice has a cat');
136 expect(position.offset).to.equal(5);
137 expect(position.offsetAtEnd).to.equal(false, 'offset is not at end');
139 getSelection.returns({
147 expect(cursor.getPosition().offsetAtEnd).to.equal(true, 'offset at end');
150 it('returns boundries of selection when browser selection not collapsed', function() {
151 var c = getCanvasFromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
154 alice: findTextNode(dom, 'Alice '),
155 has: findTextNode(dom, 'has'),
156 cat: findTextNode(dom, ' cat')
158 cursor = c.getCursor(),
159 aliceElement = c.getDocumentElement(text.alice),
160 catElement = c.getDocumentElement(text.cat);
163 {focus: text.alice, focusOffset: 1, anchor: text.cat, anchorOffset: 2, selectionAnchor: catElement},
164 {focus: text.cat, focusOffset: 2, anchor: text.alice, anchorOffset: 1, selectionAnchor: aliceElement}
165 ].forEach(function(s, idx) {
166 getSelection.returns({isColapsed: false, anchorNode: s.anchor, anchorOffset: s.anchorOffset, focusNode: s.focus, focusOffset: s.focusOffset});
168 var selectionStart = cursor.getSelectionStart(),
169 selectionEnd = cursor.getSelectionEnd(),
170 selectionAnchor = cursor.getSelectionAnchor();
172 expect(cursor.isSelecting()).to.equal(true, 'cursor is selecting');
173 expect(selectionStart.element.sameNode(aliceElement)).to.equal(true, '"Alice" is the start of the selection ' + idx);
174 expect(selectionStart.offset).to.equal(1, '"Alice" offset ok' + idx);
175 expect(selectionEnd.element.sameNode(catElement)).to.equal(true, '"Cat" is the start of the selection ' + idx);
176 expect(selectionEnd.offset).to.equal(2, '"Cat" offset ok' + idx);
177 expect(selectionAnchor.element.sameNode(s.selectionAnchor)).to.equal(true, 'anchor ok');
178 expect(selectionAnchor.offset).to.equal(s.anchorOffset, 'anchor offset ok');
182 it('recognizes when browser selection boundries lies in sibling DocumentTextElements', function() {
183 var c = getCanvasFromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
186 alice: findTextNode(dom, 'Alice '),
187 has: findTextNode(dom, 'has'),
188 a: findTextNode(dom, ' a '),
189 big: findTextNode(dom, 'big'),
190 cat: findTextNode(dom, ' cat'),
192 cursor = c.getCursor();
194 expect($(text.alice).text()).to.equal('Alice ');
195 expect($(text.has).text()).to.equal('has');
196 expect($(text.a).text()).to.equal(' a ');
197 expect($(text.big).text()).to.equal('big');
198 expect($(text.cat).text()).to.equal(' cat');
200 getSelection.returns({anchorNode: text.alice, focusNode: text.a});
201 expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "a" are children');
203 getSelection.returns({anchorNode: text.alice, focusNode: text.cat});
204 expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "cat" are children');
206 getSelection.returns({anchorNode: text.alice, focusNode: text.has});
207 expect(cursor.isSelectingSiblings()).to.equal(false, '"Alice" and "has" are not children');
209 getSelection.returns({anchorNode: text.has, focusNode: text.big});
210 expect(cursor.isSelectingSiblings()).to.equal(false, '"has" and "big" are not children');