5 'modules/documentCanvas/canvas/canvas',
6 'modules/documentCanvas/canvas/utils',
8 ], function($, chai, sinon, canvas, utils, wlxml) {
11 /* global describe, it, beforeEach, afterEach */
13 var expect = chai.expect;
15 var getCanvasFromXML = function(xml) {
16 return canvas.fromXMLDocument(getDocumentFromXML(xml));
19 var getDocumentFromXML = function(xml) {
20 return wlxml.WLXMLDocumentFromXML(xml);
23 var wait = function(callback, timeout) {
25 return window.setTimeout(callback, timeout || 0.5);
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);
34 expect(c.doc().children()).to.have.length(3);
35 expect(c.doc().children()[0].canvas).to.equal(c);
36 expect(c.doc().children()[0].wlxmlNode.sameNode(doc.root));
40 describe('Handling empty text nodes', function() {
41 it('puts zero width space into node with about to be remove text', function(done) {
42 var c = getCanvasFromXML('<section>Alice</section>'),
43 textElement = c.doc().children()[0];
44 textElement.setText('');
46 /* Wait for MutationObserver to kick in. */
48 expect(textElement.getText({raw:true})).to.equal(utils.unicode.ZWS, 'ZWS in canvas');
49 expect(c.wlxmlDocument.root.contents()[0].getText()).to.equal('', 'empty string in a document');
55 describe('Handling changes to the document', function() {
56 it('replaces the whole canvas content when document root node replaced', function() {
57 var doc = getDocumentFromXML('<section></section>'),
58 c = canvas.fromXMLDocument(doc);
60 var header = doc.root.replaceWith({tagName: 'header'});
61 expect(c.doc().wlxmlNode.sameNode(header)).to.equal(true);
65 describe('Listening to document changes', function() {
67 it('Handling element node moved', function() {
68 var doc = getDocumentFromXML('<section><a></a><b></b></section>'),
69 a = doc.root.contents()[0],
70 b = doc.root.contents()[1],
71 c = canvas.fromXMLDocument(doc);
74 var sectionChildren = c.doc().children();
75 expect(sectionChildren.length).to.equal(2);
76 expect(sectionChildren[0].getWlxmlTag()).to.equal('b');
77 expect(sectionChildren[1].getWlxmlTag()).to.equal('a');
80 it('Handling text node moved', function() {
81 var doc = getDocumentFromXML('<section><a></a>Alice</section>'),
82 a = doc.root.contents()[0],
83 textNode = doc.root.contents()[1],
84 c = canvas.fromXMLDocument(doc);
87 var sectionChildren = c.doc().children();
88 expect(sectionChildren.length).to.equal(2);
89 expect(sectionChildren[0].getText()).to.equal('Alice');
90 expect(sectionChildren[1].getWlxmlTag()).to.equal('a');
93 it('Handles nodeTagChange event', function() {
95 var doc = wlxml.WLXMLDocumentFromXML('<section><div>Alice</div></section>'),
96 c = canvas.fromXMLDocument(doc);
98 doc.root.contents()[0].setTag('header');
100 var headerNode = doc.root.contents()[0],
101 headerElement = c.doc().children()[0];
103 expect(headerElement.getWlxmlTag()).to.equal('header', 'element ok');
105 /* Make sure we handle invalidation of reference to wlxmlNode after changing its tag */
106 expect(headerNode.getData('canvasElement').sameNode(headerElement)).to.equal(true, 'node->element');
107 expect(headerElement.wlxmlNode.sameNode(headerNode)).to.equal(true, 'element->node');
110 it('Handles nodeDetached event for an empty text node', function(done) {
111 var doc = wlxml.WLXMLDocumentFromXML('<section><div>A<span>b</span></div></section>'),
112 aTextNode = doc.root.contents()[0].contents()[0],
115 canvas.fromXMLDocument(doc);
116 aTextElement = utils.findCanvasElementInParent(aTextNode, aTextNode.parent()); // TODO: This really should be easier...
118 aTextElement.setText('');
121 var parent = aTextElement.parent();
122 expect(aTextElement.getText({raw:true})).to.equal(utils.unicode.ZWS, 'canvas represents this as empty node');
123 aTextElement.wlxmlNode.detach();
124 expect(parent.children().length).to.equal(1);
125 expect(parent.children()[0].getWlxmlTag()).to.equal('span');
131 describe('Cursor', function() {
135 var findTextNode = function(inside, text) {
136 var nodes = inside.find(':not(iframe)').addBack().contents().filter(function() {
137 return this.nodeType === Node.TEXT_NODE && this.data === text;
145 beforeEach(function() {
147 getSelection = sinon.stub(window, 'getSelection');
150 afterEach(function() {
151 getSelection.restore();
154 it('returns position when browser selection collapsed', function() {
155 var c = getCanvasFromXML('<section>Alice has a cat</section>'),
157 text = findTextNode(dom, 'Alice has a cat');
159 expect(text.nodeType).to.equal(Node.TEXT_NODE, 'correct node selected');
160 expect($(text).text()).to.equal('Alice has a cat');
162 getSelection.returns({
169 var cursor = c.getCursor(),
170 position = cursor.getPosition();
172 expect(cursor.isSelecting()).to.equal(false, 'cursor is not selecting anything');
173 expect(position.element.getText()).to.equal('Alice has a cat');
174 expect(position.offset).to.equal(5);
175 expect(position.offsetAtEnd).to.equal(false, 'offset is not at end');
177 getSelection.returns({
185 expect(cursor.getPosition().offsetAtEnd).to.equal(true, 'offset at end');
188 it('recognizes selection start and end on document order', function() {
189 var c = getCanvasFromXML('<section><span>Alice</span><span>has a cat</span><div>abc<span>...</span>cde</div></section>'),
191 textFirst = findTextNode(dom, 'Alice'),
192 textSecond = findTextNode(dom, 'has a cat'),
193 textAbc = findTextNode(dom, 'abc'),
194 textCde = findTextNode(dom, 'cde');
196 var check = function(label, expected) {
197 var cursor = c.getCursor();
198 label = label + ': ';
199 expect(cursor.getSelectionStart().element.getText()).to.equal(expected.start.text, label + 'start element ok');
200 expect(cursor.getSelectionStart().offset).to.equal(expected.start.offset, label + 'start offset ok');
201 expect(cursor.getSelectionEnd().element.getText()).to.equal(expected.end.text, label + 'end element ok');
202 expect(cursor.getSelectionEnd().offset).to.equal(expected.end.offset, label + 'end offset ok');
205 getSelection.returns({
206 anchorNode: textFirst,
207 focusNode: textFirst,
213 check('same element, anchor first', {
214 start: {text: 'Alice', offset: 1},
215 end: {text: 'Alice', offset:3}
219 getSelection.returns({
220 anchorNode: textFirst,
221 focusNode: textFirst,
227 check('same element, anchor second', {
228 start: {text: 'Alice', offset: 1},
229 end: {text: 'Alice', offset:3}
233 getSelection.returns({
241 check('same parent, anchor first', {
242 start: {text: 'abc', offset: 3},
243 end: {text: 'cde', offset:1}
247 getSelection.returns({
255 check('same parent, anchor second', {
256 start: {text: 'abc', offset: 3},
257 end: {text: 'cde', offset:1}
261 getSelection.returns({
262 anchorNode: textFirst,
263 focusNode: textSecond,
269 check('different parents, anchor first', {
270 start: {text: 'Alice', offset: 1},
271 end: {text: 'has a cat', offset:3}
275 getSelection.returns({
276 anchorNode: textSecond,
277 focusNode: textFirst,
283 check('different parents, anchor second', {
284 start: {text: 'Alice', offset: 1},
285 end: {text: 'has a cat', offset:3}
289 it('returns boundries of selection when browser selection not collapsed', function() {
290 var c = getCanvasFromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
293 alice: findTextNode(dom, 'Alice '),
294 has: findTextNode(dom, 'has'),
295 cat: findTextNode(dom, ' cat')
297 cursor = c.getCursor(),
298 aliceElement = c.getDocumentElement(text.alice),
299 catElement = c.getDocumentElement(text.cat);
302 {focus: text.alice, focusOffset: 1, anchor: text.cat, anchorOffset: 2, selectionAnchor: catElement},
303 {focus: text.cat, focusOffset: 2, anchor: text.alice, anchorOffset: 1, selectionAnchor: aliceElement}
304 ].forEach(function(s, idx) {
305 getSelection.returns({isColapsed: false, anchorNode: s.anchor, anchorOffset: s.anchorOffset, focusNode: s.focus, focusOffset: s.focusOffset});
307 var selectionStart = cursor.getSelectionStart(),
308 selectionEnd = cursor.getSelectionEnd(),
309 selectionAnchor = cursor.getSelectionAnchor();
311 expect(cursor.isSelecting()).to.equal(true, 'cursor is selecting');
312 expect(selectionStart.element.sameNode(aliceElement)).to.equal(true, '"Alice" is the start of the selection ' + idx);
313 expect(selectionStart.offset).to.equal(1, '"Alice" offset ok' + idx);
314 expect(selectionEnd.element.sameNode(catElement)).to.equal(true, '"Cat" is the start of the selection ' + idx);
315 expect(selectionEnd.offset).to.equal(2, '"Cat" offset ok' + idx);
316 expect(selectionAnchor.element.sameNode(s.selectionAnchor)).to.equal(true, 'anchor ok');
317 expect(selectionAnchor.offset).to.equal(s.anchorOffset, 'anchor offset ok');
321 it('recognizes when browser selection boundries lies in sibling DocumentTextElements', function() {
322 var c = getCanvasFromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
325 alice: findTextNode(dom, 'Alice '),
326 has: findTextNode(dom, 'has'),
327 a: findTextNode(dom, ' a '),
328 big: findTextNode(dom, 'big'),
329 cat: findTextNode(dom, ' cat'),
331 cursor = c.getCursor();
333 expect($(text.alice).text()).to.equal('Alice ');
334 expect($(text.has).text()).to.equal('has');
335 expect($(text.a).text()).to.equal(' a ');
336 expect($(text.big).text()).to.equal('big');
337 expect($(text.cat).text()).to.equal(' cat');
339 getSelection.returns({anchorNode: text.alice, focusNode: text.a});
340 expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "a" are children');
342 getSelection.returns({anchorNode: text.alice, focusNode: text.cat});
343 expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "cat" are children');
345 getSelection.returns({anchorNode: text.alice, focusNode: text.has});
346 expect(cursor.isSelectingSiblings()).to.equal(false, '"Alice" and "has" are not children');
348 getSelection.returns({anchorNode: text.has, focusNode: text.big});
349 expect(cursor.isSelectingSiblings()).to.equal(false, '"has" and "big" are not children');