From 25c0b375d14eebb5fdb9df8971e440c070fd644b Mon Sep 17 00:00:00 2001 From: Aditya Bhatt Date: Tue, 9 Apr 2013 23:35:11 +0530 Subject: [PATCH] Check for previous sibling type and text offset to determine which positions to filter out during cursor movement. Now the cursor moves naturally across spans <-> text nodes. --- webodf/lib/ops/OdtDocument.js | 36 ++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/webodf/lib/ops/OdtDocument.js b/webodf/lib/ops/OdtDocument.js index 9e0bfb59..e4514592 100644 --- a/webodf/lib/ops/OdtDocument.js +++ b/webodf/lib/ops/OdtDocument.js @@ -68,12 +68,38 @@ ops.OdtDocument = function OdtDocument(odfCanvas) { function TextPositionFilter() { var /**@const*/accept = core.PositionFilter.FilterResult.FILTER_ACCEPT, /**@const*/reject = core.PositionFilter.FilterResult.FILTER_REJECT; + + /** + * This takes a node (a textNode or a span, for example) that exists at some level + * inside a p or h element, and returns it's previous sibling after ignoring the cursor + * or editinfo elements' existence. If the node is within a span, then we return the node + * before that span. + * @param {!Node} node + * @return {Node} + */ + function getPreviousSiblingInParagraph(node) { + var prev = node.previousSibling, + prevName = prev && prev.localName; + if (prevName === "editinfo") { + return null; + } else if (prevName === "cursor") { + return getPreviousSiblingInParagraph(prev); + } else if (prevName === null) { + if (node.parentNode.localName === "span") { + return getPreviousSiblingInParagraph(node.parentNode); + } + } + + return prev; + } /** * @param {!core.PositionIterator} iterator * @return {core.PositionFilter.FilterResult} */ this.acceptPosition = function (iterator) { - var n = iterator.container(), p, o, d; + var n = iterator.container(), p, o, d, + textOffset = iterator.textOffset(), + previousSibling = getPreviousSiblingInParagraph(n); // only stop in text nodes or at end of

, o if (n.nodeType !== 3) { if (n.localName !== "p" && n.localName !== "h" && n.localName !== "span") { @@ -91,10 +117,14 @@ ops.OdtDocument = function OdtDocument(odfCanvas) { return reject; } // do not stop between spaces - o = iterator.textOffset(); - if (o > 0 && iterator.substr(o - 1, 2) === " ") { + if (textOffset > 0 && iterator.substr(textOffset - 1, 2) === " ") { return reject; } + if (textOffset === 0) { + if (o === "span" || (previousSibling && previousSibling.localName) === "span") { + return reject; + } + } return accept; }; }