From: Aleksander Ɓukasz Date: Wed, 20 Nov 2013 12:17:22 +0000 (+0100) Subject: Bringing back cursor tests X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/7bd1d1364915cd2ead33d97a4396c69e22b39713 Bringing back cursor tests --- diff --git a/src/editor/modules/documentCanvas/canvas/canvas.test.js b/src/editor/modules/documentCanvas/canvas/canvas.test.js index f0ba395..5c36f90 100644 --- a/src/editor/modules/documentCanvas/canvas/canvas.test.js +++ b/src/editor/modules/documentCanvas/canvas/canvas.test.js @@ -15,6 +15,10 @@ var nodeFromXML = function(xml) { return wlxml.WLXMLElementNodeFromXML(xml); }; +var getCanvasFromXML = function(xml) { + return canvas.fromXMLDocument(wlxml.WLXMLDocumentFromXML(xml)); +}; + describe('new Canvas', function() { it('abc', function() { @@ -25,5 +29,123 @@ describe('new Canvas', function() { }); }) +describe('Cursor', function() { + + var getSelection; + + var findTextNode = function(inside, text) { + var nodes = inside.find(':not(iframe)').addBack().contents().filter(function() { + return this.nodeType === Node.TEXT_NODE && this.data === text; + }); + if(nodes.length) + return nodes[0]; + return null; + } + + beforeEach(function() { + getSelection = sinon.stub(window, 'getSelection'); + }); + + afterEach(function() { + getSelection.restore(); + }); + + it('returns position when browser selection collapsed', function() { + var c = getCanvasFromXML('
Alice has a cat
'), + dom = c.doc().dom(), + text = findTextNode(dom, 'Alice has a cat'); + + expect(text.nodeType).to.equal(Node.TEXT_NODE, 'correct node selected'); + expect($(text).text()).to.equal('Alice has a cat'); + + getSelection.returns({ + anchorNode: text, + focusNode: text, + anchorOffset: 5, + focusOffset: 5, + isCollapsed: true + }); + var cursor = c.getCursor(), + position = cursor.getPosition(); + + expect(cursor.isSelecting()).to.equal(false, 'cursor is not selecting anything'); + expect(position.element.getText()).to.equal('Alice has a cat'); + expect(position.offset).to.equal(5); + expect(position.offsetAtEnd).to.equal(false, 'offset is not at end'); + + getSelection.returns({ + anchorNode: text, + focusNode: text, + anchorOffset: 15, + focusOffset: 15, + isCollapsed: true + }); + + expect(cursor.getPosition().offsetAtEnd).to.equal(true, 'offset at end'); + }); + + it('returns boundries of selection when browser selection not collapsed', function() { + var c = getCanvasFromXML('
Alice has a big cat
'), + dom = c.doc().dom(), + text = { + alice: findTextNode(dom, 'Alice '), + has: findTextNode(dom, 'has'), + cat: findTextNode(dom, ' cat') + }, + cursor = c.getCursor(), + aliceElement = c.getDocumentElement(text.alice), + catElement = c.getDocumentElement(text.cat); + + [ + {focus: text.alice, focusOffset: 1, anchor: text.cat, anchorOffset: 2, selectionAnchor: catElement}, + {focus: text.cat, focusOffset: 2, anchor: text.alice, anchorOffset: 1, selectionAnchor: aliceElement} + ].forEach(function(s, idx) { + getSelection.returns({isColapsed: false, anchorNode: s.anchor, anchorOffset: s.anchorOffset, focusNode: s.focus, focusOffset: s.focusOffset}); + + var selectionStart = cursor.getSelectionStart(), + selectionEnd = cursor.getSelectionEnd(), + selectionAnchor = cursor.getSelectionAnchor(); + + expect(cursor.isSelecting()).to.equal(true, 'cursor is selecting'); + expect(selectionStart.element.sameNode(aliceElement)).to.equal(true, '"Alice" is the start of the selection ' + idx); + expect(selectionStart.offset).to.equal(1, '"Alice" offset ok' + idx); + expect(selectionEnd.element.sameNode(catElement)).to.equal(true, '"Cat" is the start of the selection ' + idx); + expect(selectionEnd.offset).to.equal(2, '"Cat" offset ok' + idx); + expect(selectionAnchor.element.sameNode(s.selectionAnchor)).to.equal(true, 'anchor ok'); + expect(selectionAnchor.offset).to.equal(s.anchorOffset, 'anchor offset ok'); + }); + }); + + it('recognizes when browser selection boundries lies in sibling DocumentTextElements', function() { + var c = getCanvasFromXML('
Alice has a big cat
'), + dom = c.doc().dom(), + text = { + alice: findTextNode(dom, 'Alice '), + has: findTextNode(dom, 'has'), + a: findTextNode(dom, ' a '), + big: findTextNode(dom, 'big'), + cat: findTextNode(dom, ' cat'), + }, + cursor = c.getCursor(); + + expect($(text.alice).text()).to.equal('Alice '); + expect($(text.has).text()).to.equal('has'); + expect($(text.a).text()).to.equal(' a '); + expect($(text.big).text()).to.equal('big'); + expect($(text.cat).text()).to.equal(' cat'); + + getSelection.returns({anchorNode: text.alice, focusNode: text.a}); + expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "a" are children'); + + getSelection.returns({anchorNode: text.alice, focusNode: text.cat}); + expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "cat" are children'); + + getSelection.returns({anchorNode: text.alice, focusNode: text.has}); + expect(cursor.isSelectingSiblings()).to.equal(false, '"Alice" and "has" are not children'); + + getSelection.returns({anchorNode: text.has, focusNode: text.big}); + expect(cursor.isSelectingSiblings()).to.equal(false, '"has" and "big" are not children'); + }); +}); }); \ No newline at end of file