From fcaaeedf181ef7232247b9821c87f6abcaf1ff7e Mon Sep 17 00:00:00 2001 From: Aditya Bhatt Date: Tue, 23 Apr 2013 01:21:02 +0530 Subject: [PATCH] eject the text nodes that will never be traversed in getTextNeighborhood, and in OpRemoveText: instead of searching for the current textNode in the neighborhood, start deleting from position zero. --- webodf/lib/ops/OdtDocument.js | 29 ++++++++++++++++++++++++++--- webodf/lib/ops/OpRemoveText.js | 13 ++----------- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/webodf/lib/ops/OdtDocument.js b/webodf/lib/ops/OdtDocument.js index e3fa443e..a5493308 100644 --- a/webodf/lib/ops/OdtDocument.js +++ b/webodf/lib/ops/OdtDocument.js @@ -151,13 +151,17 @@ ops.OdtDocument = function OdtDocument(odfCanvas) { iteratedLength, advance, visited = false, - i; + inFirstNeighborhood = true, + i, + j; advance = length > 0 ? iterator.nextPosition : iterator.previousPosition; - iteratedLength = (currentNode.nodeType === 3 ? currentNode.data.length : 0) - iterator.text().length; + iteratedLength = 0; + do { currentNeighborhood = iterator.textNeighborhood(); currentNode = iterator.container(); + if (currentNeighborhood.length && currentNode.nodeType === 3) { visited = false; for (i = 0; i < neighborhood.length; i += 1) { @@ -177,8 +181,27 @@ ops.OdtDocument = function OdtDocument(odfCanvas) { if (length < 0) { currentNeighborhood.reverse(); } + // When in the first local neighborhood, remove the elements that are in + // a direction opposite to the length. + if (inFirstNeighborhood) { + for (j = 0; j < currentNeighborhood.length; j += 1) { + if (currentNeighborhood[j] === currentNode) { + currentNeighborhood.splice(0, j); + break; + } + } + if (length < 0) { + currentNeighborhood.splice(0, 1); + } + inFirstNeighborhood = false; + } + neighborhood = neighborhood.concat(currentNeighborhood); - iteratedLength += iterator.text().length; + + for (j = 0; j < currentNeighborhood.length; j += 1) { + iteratedLength += currentNeighborhood[j].data.length; + } + } } } while (advance() && iteratedLength < Math.abs(length)); diff --git a/webodf/lib/ops/OpRemoveText.js b/webodf/lib/ops/OpRemoveText.js index 3ae606c2..281380d4 100644 --- a/webodf/lib/ops/OpRemoveText.js +++ b/webodf/lib/ops/OpRemoveText.js @@ -75,7 +75,6 @@ ops.OpRemoveText = function OpRemoveText() { this.execute = function (odtDocument) { var neighborhood = [], - textNode, paragraphElement, textNodeSequence = [], remainingLength = Math.abs(length), @@ -86,22 +85,14 @@ ops.OpRemoveText = function OpRemoveText() { i, firstIndex; - textNode = odtDocument.getPositionInTextNode(position).textNode; - paragraphElement = odtDocument.getParagraphElement(textNode); neighborhood = odtDocument.getTextNeighborhood(position, length); if (neighborhood.length) { - // Pick the index of the textNode from the starting neighborhood. - firstIndex = neighborhood.indexOf(textNode); - runtime.assert(firstIndex !== -1, "OpRemoveText: textNode not found in neighborhood"); - if (removalType === 'delete') { - firstIndex += 1; - } + paragraphElement = odtDocument.getParagraphElement(neighborhood[0]); - for (i = firstIndex; i < neighborhood.length && remainingLength; i += 1) { + for (i = 0; i < neighborhood.length && remainingLength; i += 1) { currentTextNode = neighborhood[i]; currentParent = currentTextNode.parentNode; - runtime.assert(currentParent !== undefined, "OpRemoveText: undefined currentParent"); currentLength = currentTextNode.data.length; if (currentLength <= remainingLength) {