Bug 1004522 part 14 - Clean up nsWSRunObject::GetWSPointBefore/After; r=ehsan

This commit is contained in:
Aryeh Gregor 2014-05-02 15:15:27 +03:00
parent e186271f6e
commit 1bf0a89ab7
2 changed files with 51 additions and 63 deletions

View File

@ -14,10 +14,8 @@
#include "nsError.h"
#include "nsHTMLEditor.h"
#include "nsIContent.h"
#include "nsIDOMCharacterData.h"
#include "nsIDOMDocument.h"
#include "nsIDOMNode.h"
#include "nsIDOMRange.h"
#include "nsISupportsImpl.h"
#include "nsRange.h"
#include "nsSelectionState.h"
@ -1405,7 +1403,7 @@ nsWSRunObject::GetCharAfter(nsINode* aNode, int32_t aOffset)
int32_t idx = mNodeArray.IndexOf(aNode);
if (idx == -1) {
// Use range comparisons to get right ws node
return GetWSPointAfter(GetAsDOMNode(aNode), aOffset);
return GetWSPointAfter(aNode, aOffset);
} else {
// Use WSPoint version of GetCharAfter()
return GetCharAfter(WSPoint(aNode, aOffset, 0));
@ -1420,7 +1418,7 @@ nsWSRunObject::GetCharBefore(nsINode* aNode, int32_t aOffset)
int32_t idx = mNodeArray.IndexOf(aNode);
if (idx == -1) {
// Use range comparisons to get right ws node
return GetWSPointBefore(GetAsDOMNode(aNode), aOffset);
return GetWSPointBefore(aNode, aOffset);
} else {
// Use WSPoint version of GetCharBefore()
return GetCharBefore(WSPoint(aNode, aOffset, 0));
@ -1647,52 +1645,48 @@ nsWSRunObject::GetCharAt(nsIContent *aTextNode, int32_t aOffset)
}
nsWSRunObject::WSPoint
nsWSRunObject::GetWSPointAfter(nsIDOMNode *aNode, int32_t aOffset)
nsWSRunObject::GetWSPointAfter(nsINode* aNode, int32_t aOffset)
{
// Note: only to be called if aNode is not a ws node.
// binary search on wsnodes
uint32_t numNodes, firstNum, curNum, lastNum;
numNodes = mNodeArray.Length();
// Note: only to be called if aNode is not a ws node.
// Binary search on wsnodes
uint32_t numNodes = mNodeArray.Length();
if (!numNodes) {
// do nothing if there are no nodes to search
// Do nothing if there are no nodes to search
WSPoint outPoint;
return outPoint;
}
firstNum = 0;
curNum = numNodes/2;
lastNum = numNodes;
int16_t cmp=0;
nsCOMPtr<nsIDOMNode> curNode;
// begin binary search
// we do this because we need to minimize calls to ComparePoints(),
// which is mongo expensive
while (curNum != lastNum)
{
curNode = GetAsDOMNode(mNodeArray[curNum]);
uint32_t firstNum = 0, curNum = numNodes/2, lastNum = numNodes;
int16_t cmp = 0;
nsCOMPtr<Text> curNode;
// Begin binary search. We do this because we need to minimize calls to
// ComparePoints(), which is expensive.
while (curNum != lastNum) {
curNode = mNodeArray[curNum];
cmp = nsContentUtils::ComparePoints(aNode, aOffset, curNode, 0);
if (cmp < 0)
if (cmp < 0) {
lastNum = curNum;
else
} else {
firstNum = curNum + 1;
curNum = (lastNum - firstNum) / 2 + firstNum;
NS_ASSERTION(firstNum <= curNum && curNum <= lastNum, "Bad binary search");
}
curNum = (lastNum - firstNum)/2 + firstNum;
MOZ_ASSERT(firstNum <= curNum && curNum <= lastNum, "Bad binary search");
}
// When the binary search is complete, we always know that the current node
// is the same as the end node, which is always past our range. Therefore,
// we've found the node immediately after the point of interest.
if (curNum == mNodeArray.Length()) {
// they asked for past our range (it's after the last node). GetCharAfter
// hey asked for past our range (it's after the last node). GetCharAfter
// will do the work for us when we pass it the last index of the last node.
nsCOMPtr<Text> textNode(mNodeArray[curNum - 1]);
WSPoint point(textNode, textNode->TextLength(), 0);
return GetCharAfter(point);
} else {
// The char after the point of interest is the first character of our range.
// The char after the point is the first character of our range.
nsCOMPtr<Text> textNode(mNodeArray[curNum]);
WSPoint point(textNode, 0, 0);
return GetCharAfter(point);
@ -1700,52 +1694,48 @@ nsWSRunObject::GetWSPointAfter(nsIDOMNode *aNode, int32_t aOffset)
}
nsWSRunObject::WSPoint
nsWSRunObject::GetWSPointBefore(nsIDOMNode *aNode, int32_t aOffset)
nsWSRunObject::GetWSPointBefore(nsINode* aNode, int32_t aOffset)
{
// Note: only to be called if aNode is not a ws node.
// binary search on wsnodes
uint32_t numNodes, firstNum, curNum, lastNum;
numNodes = mNodeArray.Length();
// Note: only to be called if aNode is not a ws node.
// Binary search on wsnodes
uint32_t numNodes = mNodeArray.Length();
if (!numNodes) {
// do nothing if there are no nodes to search
// Do nothing if there are no nodes to search
WSPoint outPoint;
return outPoint;
}
firstNum = 0;
curNum = numNodes/2;
lastNum = numNodes;
int16_t cmp=0;
nsCOMPtr<nsIDOMNode> curNode;
// begin binary search
// we do this because we need to minimize calls to ComparePoints(),
// which is mongo expensive
while (curNum != lastNum)
{
curNode = GetAsDOMNode(mNodeArray[curNum]);
uint32_t firstNum = 0, curNum = numNodes/2, lastNum = numNodes;
int16_t cmp = 0;
nsCOMPtr<Text> curNode;
// Begin binary search. We do this because we need to minimize calls to
// ComparePoints(), which is expensive.
while (curNum != lastNum) {
curNode = mNodeArray[curNum];
cmp = nsContentUtils::ComparePoints(aNode, aOffset, curNode, 0);
if (cmp < 0)
if (cmp < 0) {
lastNum = curNum;
else
} else {
firstNum = curNum + 1;
curNum = (lastNum - firstNum) / 2 + firstNum;
NS_ASSERTION(firstNum <= curNum && curNum <= lastNum, "Bad binary search");
}
curNum = (lastNum - firstNum)/2 + firstNum;
MOZ_ASSERT(firstNum <= curNum && curNum <= lastNum, "Bad binary search");
}
// When the binary search is complete, we always know that the current node
// is the same as the end node, which is always past our range. Therefore,
// we've found the node immediately after the point of interest.
if (curNum == mNodeArray.Length()) {
// get the point before the end of the last node, we can pass the length
// of the node into GetCharBefore, and it will return the last character.
// Get the point before the end of the last node, we can pass the length of
// the node into GetCharBefore, and it will return the last character.
nsCOMPtr<Text> textNode(mNodeArray[curNum - 1]);
WSPoint point(textNode, textNode->TextLength(), 0);
return GetCharBefore(point);
} else {
// we can just ask the current node for the point immediately before it,
// We can just ask the current node for the point immediately before it,
// it will handle moving to the previous node (if any) and returning the
// appropriate character
nsCOMPtr<Text> textNode(mNodeArray[curNum]);

View File

@ -6,10 +6,8 @@
#ifndef __wsrunobject_h__
#define __wsrunobject_h__
#include "nsCOMArray.h"
#include "nsCOMPtr.h"
#include "nsIDOMNode.h"
#include "nsIEditor.h"
#include "nsIEditor.h" // for EDirection
#include "nsINode.h"
#include "nscore.h"
#include "mozilla/dom/Text.h"
@ -342,8 +340,8 @@ class MOZ_STACK_CLASS nsWSRunObject
void FindRun(nsINode* aNode, int32_t aOffset, WSFragment** outRun,
bool after);
char16_t GetCharAt(nsIContent *aTextNode, int32_t aOffset);
WSPoint GetWSPointAfter(nsIDOMNode *aNode, int32_t aOffset);
WSPoint GetWSPointBefore(nsIDOMNode *aNode, int32_t aOffset);
WSPoint GetWSPointAfter(nsINode* aNode, int32_t aOffset);
WSPoint GetWSPointBefore(nsINode* aNode, int32_t aOffset);
nsresult CheckTrailingNBSPOfRun(WSFragment *aRun);
nsresult CheckTrailingNBSP(WSFragment* aRun, nsINode* aNode,
int32_t aOffset);