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() {
64 it('Handling element node moved', function() {
65 var doc = getDocumentFromXML('<section><a></a><b></b></section>'),
66 a = doc.root.contents()[0],
67 b = doc.root.contents()[1],
68 c = canvas.fromXMLDocument(doc);
71 var sectionChildren = c.doc().children();
72 expect(sectionChildren.length).to.equal(2);
73 expect(sectionChildren[0].getWlxmlTag()).to.equal('b');
74 expect(sectionChildren[1].getWlxmlTag()).to.equal('a');
76 it('Handling text node moved', function() {
77 var doc = getDocumentFromXML('<section><a></a>Alice</section>'),
78 a = doc.root.contents()[0],
79 textNode = doc.root.contents()[1],
80 c = canvas.fromXMLDocument(doc);
83 var sectionChildren = c.doc().children();
84 expect(sectionChildren.length).to.equal(2);
85 expect(sectionChildren[0].getText()).to.equal('Alice');
86 expect(sectionChildren[1].getWlxmlTag()).to.equal('a');
90 describe('Cursor', function() {
94 var findTextNode = function(inside, text) {
95 var nodes = inside.find(':not(iframe)').addBack().contents().filter(function() {
96 return this.nodeType === Node.TEXT_NODE && this.data === text;
104 beforeEach(function() {
105 getSelection = sinon.stub(window, 'getSelection');
108 afterEach(function() {
109 getSelection.restore();
112 it('returns position when browser selection collapsed', function() {
113 var c = getCanvasFromXML('<section>Alice has a cat</section>'),
115 text = findTextNode(dom, 'Alice has a cat');
117 expect(text.nodeType).to.equal(Node.TEXT_NODE, 'correct node selected');
118 expect($(text).text()).to.equal('Alice has a cat');
120 getSelection.returns({
127 var cursor = c.getCursor(),
128 position = cursor.getPosition();
130 expect(cursor.isSelecting()).to.equal(false, 'cursor is not selecting anything');
131 expect(position.element.getText()).to.equal('Alice has a cat');
132 expect(position.offset).to.equal(5);
133 expect(position.offsetAtEnd).to.equal(false, 'offset is not at end');
135 getSelection.returns({
143 expect(cursor.getPosition().offsetAtEnd).to.equal(true, 'offset at end');
146 it('returns boundries of selection when browser selection not collapsed', function() {
147 var c = getCanvasFromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
150 alice: findTextNode(dom, 'Alice '),
151 has: findTextNode(dom, 'has'),
152 cat: findTextNode(dom, ' cat')
154 cursor = c.getCursor(),
155 aliceElement = c.getDocumentElement(text.alice),
156 catElement = c.getDocumentElement(text.cat);
159 {focus: text.alice, focusOffset: 1, anchor: text.cat, anchorOffset: 2, selectionAnchor: catElement},
160 {focus: text.cat, focusOffset: 2, anchor: text.alice, anchorOffset: 1, selectionAnchor: aliceElement}
161 ].forEach(function(s, idx) {
162 getSelection.returns({isColapsed: false, anchorNode: s.anchor, anchorOffset: s.anchorOffset, focusNode: s.focus, focusOffset: s.focusOffset});
164 var selectionStart = cursor.getSelectionStart(),
165 selectionEnd = cursor.getSelectionEnd(),
166 selectionAnchor = cursor.getSelectionAnchor();
168 expect(cursor.isSelecting()).to.equal(true, 'cursor is selecting');
169 expect(selectionStart.element.sameNode(aliceElement)).to.equal(true, '"Alice" is the start of the selection ' + idx);
170 expect(selectionStart.offset).to.equal(1, '"Alice" offset ok' + idx);
171 expect(selectionEnd.element.sameNode(catElement)).to.equal(true, '"Cat" is the start of the selection ' + idx);
172 expect(selectionEnd.offset).to.equal(2, '"Cat" offset ok' + idx);
173 expect(selectionAnchor.element.sameNode(s.selectionAnchor)).to.equal(true, 'anchor ok');
174 expect(selectionAnchor.offset).to.equal(s.anchorOffset, 'anchor offset ok');
178 it('recognizes when browser selection boundries lies in sibling DocumentTextElements', function() {
179 var c = getCanvasFromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
182 alice: findTextNode(dom, 'Alice '),
183 has: findTextNode(dom, 'has'),
184 a: findTextNode(dom, ' a '),
185 big: findTextNode(dom, 'big'),
186 cat: findTextNode(dom, ' cat'),
188 cursor = c.getCursor();
190 expect($(text.alice).text()).to.equal('Alice ');
191 expect($(text.has).text()).to.equal('has');
192 expect($(text.a).text()).to.equal(' a ');
193 expect($(text.big).text()).to.equal('big');
194 expect($(text.cat).text()).to.equal(' cat');
196 getSelection.returns({anchorNode: text.alice, focusNode: text.a});
197 expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "a" are children');
199 getSelection.returns({anchorNode: text.alice, focusNode: text.cat});
200 expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "cat" are children');
202 getSelection.returns({anchorNode: text.alice, focusNode: text.has});
203 expect(cursor.isSelectingSiblings()).to.equal(false, '"Alice" and "has" are not children');
205 getSelection.returns({anchorNode: text.has, focusNode: text.big});
206 expect(cursor.isSelectingSiblings()).to.equal(false, '"has" and "big" are not children');