From 2869ffa4bf584c7d82e0455445a656d1ec3603d9 Mon Sep 17 00:00:00 2001 From: Aditya Bhatt Date: Thu, 25 Apr 2013 01:27:25 +0530 Subject: [PATCH] Make positions in the beginning of a container but right before a span unwalkable. Additionally, make the offset=0 in spans that have no allowable siblings to the left walkable. --- webodf/lib/core/PositionIterator.js | 6 ++++++ webodf/lib/ops/OdtDocument.js | 30 ++++++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/webodf/lib/core/PositionIterator.js b/webodf/lib/core/PositionIterator.js index 93844e24..44e08c1a 100644 --- a/webodf/lib/core/PositionIterator.js +++ b/webodf/lib/core/PositionIterator.js @@ -145,6 +145,12 @@ core.PositionIterator = function PositionIterator(root, whatToShow, filter, } return n; }; + /** + * @return {!Node} + */ + this.getCurrentNode = function () { + return walker.currentNode; + }; /** * @return {!number} */ diff --git a/webodf/lib/ops/OdtDocument.js b/webodf/lib/ops/OdtDocument.js index f8cabee5..7904c1d9 100644 --- a/webodf/lib/ops/OdtDocument.js +++ b/webodf/lib/ops/OdtDocument.js @@ -74,10 +74,14 @@ ops.OdtDocument = function OdtDocument(odfCanvas) { * @return {core.PositionFilter.FilterResult} */ this.acceptPosition = function (iterator) { - var n = iterator.container(), p, o, + var n = iterator.container(), p, o, filteredSibling, + nodeFilter = iterator.getNodeFilter(), textOffset = iterator.textOffset(), previousSibling = iterator.getPreviousSibling(), - nextSibling = iterator.getNextSibling(); + currentNode = iterator.getCurrentNode(), + nextSibling = iterator.getNextSibling(), + hasPreviousSibling = true; + // 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,6 +95,11 @@ ops.OdtDocument = function OdtDocument(odfCanvas) { if (n.localName === "span" && !n.textContent.length) { return reject; } + // Don't stop at positions that are in the beginning of a container + // but right before a span + if (iterator.offset() === 0 && currentNode.localName === "span") { + return reject; + } return accept; } @@ -99,9 +108,24 @@ ops.OdtDocument = function OdtDocument(odfCanvas) { // Don't stop in textnodes with offset 0 if we are in a span or if the previous // sibling is a span. That is: avoid left extremeties around/in spans. if (textOffset === 0) { - if (o === "span" || (previousSibling && previousSibling.localName) === "span") { + if ((previousSibling && previousSibling.localName) === "span") { return reject; } + // If this span has no previous allowed siblings, then text offset 0 is walkable + if (o === "span") { + filteredSibling = p; + do { + filteredSibling = filteredSibling.previousSibling; + if (!filteredSibling) { + hasPreviousSibling = false; + break; + } + } while (nodeFilter.acceptNode(filteredSibling) !== 1); + + if (hasPreviousSibling) { + return reject; + } + } } if (n.length === 0) { return reject;