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);
36 expect(c.doc().children()[0].canvas).to.equal(c);
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().data('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.data('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.data('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'),
197 var check = function(label, expected) {
198 var cursor = c.getCursor();
199 label = label + ': ';
200 expect(cursor.getSelectionStart().element.getText()).to.equal(expected.start.text, label + 'start element ok');
201 expect(cursor.getSelectionStart().offset).to.equal(expected.start.offset, label + 'start offset ok');
202 expect(cursor.getSelectionEnd().element.getText()).to.equal(expected.end.text, label + 'end element ok');
203 expect(cursor.getSelectionEnd().offset).to.equal(expected.end.offset, label + 'end offset ok');
206 getSelection.returns({
207 anchorNode: textFirst,
208 focusNode: textFirst,
214 check('same element, anchor first', {
215 start: {text: 'Alice', offset: 1},
216 end: {text: 'Alice', offset:3}
220 getSelection.returns({
221 anchorNode: textFirst,
222 focusNode: textFirst,
228 check('same element, anchor second', {
229 start: {text: 'Alice', offset: 1},
230 end: {text: 'Alice', offset:3}
234 getSelection.returns({
242 check('same parent, anchor first', {
243 start: {text: 'abc', offset: 3},
244 end: {text: 'cde', offset:1}
248 getSelection.returns({
256 check('same parent, anchor second', {
257 start: {text: 'abc', offset: 3},
258 end: {text: 'cde', offset:1}
262 getSelection.returns({
263 anchorNode: textFirst,
264 focusNode: textSecond,
270 check('different parents, anchor first', {
271 start: {text: 'Alice', offset: 1},
272 end: {text: 'has a cat', offset:3}
276 getSelection.returns({
277 anchorNode: textSecond,
278 focusNode: textFirst,
284 check('different parents, anchor second', {
285 start: {text: 'Alice', offset: 1},
286 end: {text: 'has a cat', offset:3}
290 it('returns boundries of selection when browser selection not collapsed', function() {
291 var c = getCanvasFromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
294 alice: findTextNode(dom, 'Alice '),
295 has: findTextNode(dom, 'has'),
296 cat: findTextNode(dom, ' cat')
298 cursor = c.getCursor(),
299 aliceElement = c.getDocumentElement(text.alice),
300 catElement = c.getDocumentElement(text.cat);
303 {focus: text.alice, focusOffset: 1, anchor: text.cat, anchorOffset: 2, selectionAnchor: catElement},
304 {focus: text.cat, focusOffset: 2, anchor: text.alice, anchorOffset: 1, selectionAnchor: aliceElement}
305 ].forEach(function(s, idx) {
306 getSelection.returns({isColapsed: false, anchorNode: s.anchor, anchorOffset: s.anchorOffset, focusNode: s.focus, focusOffset: s.focusOffset});
308 var selectionStart = cursor.getSelectionStart(),
309 selectionEnd = cursor.getSelectionEnd(),
310 selectionAnchor = cursor.getSelectionAnchor();
312 expect(cursor.isSelecting()).to.equal(true, 'cursor is selecting');
313 expect(selectionStart.element.sameNode(aliceElement)).to.equal(true, '"Alice" is the start of the selection ' + idx);
314 expect(selectionStart.offset).to.equal(1, '"Alice" offset ok' + idx);
315 expect(selectionEnd.element.sameNode(catElement)).to.equal(true, '"Cat" is the start of the selection ' + idx);
316 expect(selectionEnd.offset).to.equal(2, '"Cat" offset ok' + idx);
317 expect(selectionAnchor.element.sameNode(s.selectionAnchor)).to.equal(true, 'anchor ok');
318 expect(selectionAnchor.offset).to.equal(s.anchorOffset, 'anchor offset ok');
322 it('recognizes when browser selection boundries lies in sibling DocumentTextElements', function() {
323 var c = getCanvasFromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
326 alice: findTextNode(dom, 'Alice '),
327 has: findTextNode(dom, 'has'),
328 a: findTextNode(dom, ' a '),
329 big: findTextNode(dom, 'big'),
330 cat: findTextNode(dom, ' cat'),
332 cursor = c.getCursor();
334 expect($(text.alice).text()).to.equal('Alice ');
335 expect($(text.has).text()).to.equal('has');
336 expect($(text.a).text()).to.equal(' a ');
337 expect($(text.big).text()).to.equal('big');
338 expect($(text.cat).text()).to.equal(' cat');
340 getSelection.returns({anchorNode: text.alice, focusNode: text.a});
341 expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "a" are children');
343 getSelection.returns({anchorNode: text.alice, focusNode: text.cat});
344 expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "cat" are children');
346 getSelection.returns({anchorNode: text.alice, focusNode: text.has});
347 expect(cursor.isSelectingSiblings()).to.equal(false, '"Alice" and "has" are not children');
349 getSelection.returns({anchorNode: text.has, focusNode: text.big});
350 expect(cursor.isSelectingSiblings()).to.equal(false, '"has" and "big" are not children');