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) {
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);
38 describe('Handling empty text nodes', function() {
39 it('puts zero width space into node with about to be remove text', function(done) {
40 var c = getCanvasFromXML('<section>Alice</section>'),
41 textElement = c.doc().children()[0];
42 textElement.setText('');
44 /* Wait for MutationObserver to kick in. */
46 expect(textElement.getText({raw:true})).to.equal(utils.unicode.ZWS, 'ZWS in canvas');
47 expect(c.wlxmlDocument.root.contents()[0].getText()).to.equal('', 'empty string in a document');
53 describe('Handling changes to the document', function() {
54 it('replaces the whole canvas content when document root node replaced', function() {
55 var doc = getDocumentFromXML('<section></section>'),
56 c = canvas.fromXMLDocument(doc);
58 var header = doc.root.replaceWith({tagName: 'header'});
59 expect(c.doc().data('wlxmlNode').sameNode(header)).to.be.true;
63 describe('Listening to document changes', function() {
65 it('Handling element node moved', function() {
66 var doc = getDocumentFromXML('<section><a></a><b></b></section>'),
67 a = doc.root.contents()[0],
68 b = doc.root.contents()[1],
69 c = canvas.fromXMLDocument(doc);
72 var sectionChildren = c.doc().children();
73 expect(sectionChildren.length).to.equal(2);
74 expect(sectionChildren[0].getWlxmlTag()).to.equal('b');
75 expect(sectionChildren[1].getWlxmlTag()).to.equal('a');
78 it('Handling text node moved', function() {
79 var doc = getDocumentFromXML('<section><a></a>Alice</section>'),
80 a = doc.root.contents()[0],
81 textNode = doc.root.contents()[1],
82 c = canvas.fromXMLDocument(doc);
85 var sectionChildren = c.doc().children();
86 expect(sectionChildren.length).to.equal(2);
87 expect(sectionChildren[0].getText()).to.equal('Alice');
88 expect(sectionChildren[1].getWlxmlTag()).to.equal('a');
92 describe('Cursor', function() {
96 var findTextNode = function(inside, text) {
97 var nodes = inside.find(':not(iframe)').addBack().contents().filter(function() {
98 return this.nodeType === Node.TEXT_NODE && this.data === text;
106 beforeEach(function() {
107 getSelection = sinon.stub(window, 'getSelection');
110 afterEach(function() {
111 getSelection.restore();
114 it('returns position when browser selection collapsed', function() {
115 var c = getCanvasFromXML('<section>Alice has a cat</section>'),
117 text = findTextNode(dom, 'Alice has a cat');
119 expect(text.nodeType).to.equal(Node.TEXT_NODE, 'correct node selected');
120 expect($(text).text()).to.equal('Alice has a cat');
122 getSelection.returns({
129 var cursor = c.getCursor(),
130 position = cursor.getPosition();
132 expect(cursor.isSelecting()).to.equal(false, 'cursor is not selecting anything');
133 expect(position.element.getText()).to.equal('Alice has a cat');
134 expect(position.offset).to.equal(5);
135 expect(position.offsetAtEnd).to.equal(false, 'offset is not at end');
137 getSelection.returns({
145 expect(cursor.getPosition().offsetAtEnd).to.equal(true, 'offset at end');
148 it('returns boundries of selection when browser selection not collapsed', function() {
149 var c = getCanvasFromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
152 alice: findTextNode(dom, 'Alice '),
153 has: findTextNode(dom, 'has'),
154 cat: findTextNode(dom, ' cat')
156 cursor = c.getCursor(),
157 aliceElement = c.getDocumentElement(text.alice),
158 catElement = c.getDocumentElement(text.cat);
161 {focus: text.alice, focusOffset: 1, anchor: text.cat, anchorOffset: 2, selectionAnchor: catElement},
162 {focus: text.cat, focusOffset: 2, anchor: text.alice, anchorOffset: 1, selectionAnchor: aliceElement}
163 ].forEach(function(s, idx) {
164 getSelection.returns({isColapsed: false, anchorNode: s.anchor, anchorOffset: s.anchorOffset, focusNode: s.focus, focusOffset: s.focusOffset});
166 var selectionStart = cursor.getSelectionStart(),
167 selectionEnd = cursor.getSelectionEnd(),
168 selectionAnchor = cursor.getSelectionAnchor();
170 expect(cursor.isSelecting()).to.equal(true, 'cursor is selecting');
171 expect(selectionStart.element.sameNode(aliceElement)).to.equal(true, '"Alice" is the start of the selection ' + idx);
172 expect(selectionStart.offset).to.equal(1, '"Alice" offset ok' + idx);
173 expect(selectionEnd.element.sameNode(catElement)).to.equal(true, '"Cat" is the start of the selection ' + idx);
174 expect(selectionEnd.offset).to.equal(2, '"Cat" offset ok' + idx);
175 expect(selectionAnchor.element.sameNode(s.selectionAnchor)).to.equal(true, 'anchor ok');
176 expect(selectionAnchor.offset).to.equal(s.anchorOffset, 'anchor offset ok');
180 it('recognizes when browser selection boundries lies in sibling DocumentTextElements', function() {
181 var c = getCanvasFromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
184 alice: findTextNode(dom, 'Alice '),
185 has: findTextNode(dom, 'has'),
186 a: findTextNode(dom, ' a '),
187 big: findTextNode(dom, 'big'),
188 cat: findTextNode(dom, ' cat'),
190 cursor = c.getCursor();
192 expect($(text.alice).text()).to.equal('Alice ');
193 expect($(text.has).text()).to.equal('has');
194 expect($(text.a).text()).to.equal(' a ');
195 expect($(text.big).text()).to.equal('big');
196 expect($(text.cat).text()).to.equal(' cat');
198 getSelection.returns({anchorNode: text.alice, focusNode: text.a});
199 expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "a" are children');
201 getSelection.returns({anchorNode: text.alice, focusNode: text.cat});
202 expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "cat" are children');
204 getSelection.returns({anchorNode: text.alice, focusNode: text.has});
205 expect(cursor.isSelectingSiblings()).to.equal(false, '"Alice" and "has" are not children');
207 getSelection.returns({anchorNode: text.has, focusNode: text.big});
208 expect(cursor.isSelectingSiblings()).to.equal(false, '"has" and "big" are not children');