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('returns boundries of selection when browser selection not collapsed', function() {
189 var c = getCanvasFromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
192 alice: findTextNode(dom, 'Alice '),
193 has: findTextNode(dom, 'has'),
194 cat: findTextNode(dom, ' cat')
196 cursor = c.getCursor(),
197 aliceElement = c.getDocumentElement(text.alice),
198 catElement = c.getDocumentElement(text.cat);
201 {focus: text.alice, focusOffset: 1, anchor: text.cat, anchorOffset: 2, selectionAnchor: catElement},
202 {focus: text.cat, focusOffset: 2, anchor: text.alice, anchorOffset: 1, selectionAnchor: aliceElement}
203 ].forEach(function(s, idx) {
204 getSelection.returns({isColapsed: false, anchorNode: s.anchor, anchorOffset: s.anchorOffset, focusNode: s.focus, focusOffset: s.focusOffset});
206 var selectionStart = cursor.getSelectionStart(),
207 selectionEnd = cursor.getSelectionEnd(),
208 selectionAnchor = cursor.getSelectionAnchor();
210 expect(cursor.isSelecting()).to.equal(true, 'cursor is selecting');
211 expect(selectionStart.element.sameNode(aliceElement)).to.equal(true, '"Alice" is the start of the selection ' + idx);
212 expect(selectionStart.offset).to.equal(1, '"Alice" offset ok' + idx);
213 expect(selectionEnd.element.sameNode(catElement)).to.equal(true, '"Cat" is the start of the selection ' + idx);
214 expect(selectionEnd.offset).to.equal(2, '"Cat" offset ok' + idx);
215 expect(selectionAnchor.element.sameNode(s.selectionAnchor)).to.equal(true, 'anchor ok');
216 expect(selectionAnchor.offset).to.equal(s.anchorOffset, 'anchor offset ok');
220 it('recognizes when browser selection boundries lies in sibling DocumentTextElements', function() {
221 var c = getCanvasFromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
224 alice: findTextNode(dom, 'Alice '),
225 has: findTextNode(dom, 'has'),
226 a: findTextNode(dom, ' a '),
227 big: findTextNode(dom, 'big'),
228 cat: findTextNode(dom, ' cat'),
230 cursor = c.getCursor();
232 expect($(text.alice).text()).to.equal('Alice ');
233 expect($(text.has).text()).to.equal('has');
234 expect($(text.a).text()).to.equal(' a ');
235 expect($(text.big).text()).to.equal('big');
236 expect($(text.cat).text()).to.equal(' cat');
238 getSelection.returns({anchorNode: text.alice, focusNode: text.a});
239 expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "a" are children');
241 getSelection.returns({anchorNode: text.alice, focusNode: text.cat});
242 expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "cat" are children');
244 getSelection.returns({anchorNode: text.alice, focusNode: text.has});
245 expect(cursor.isSelectingSiblings()).to.equal(false, '"Alice" and "has" are not children');
247 getSelection.returns({anchorNode: text.has, focusNode: text.big});
248 expect(cursor.isSelectingSiblings()).to.equal(false, '"has" and "big" are not children');