Bug 582863 - Spell checker shouldn't copy the text node contents unnecessarily; r=roc a=blocking2.0+

--HG--
extra : rebase_source : 7313d44dfe2645a8df15a4fca78e75ecfc39b536
This commit is contained in:
Ehsan Akhgari 2010-07-30 18:55:42 -04:00
Родитель 80727e5a17
Коммит aaa3235f97
3 изменённых файлов: 31 добавлений и 42 удалений

Просмотреть файл

@ -42,7 +42,6 @@
*/ */
#include "nsTextFragment.h" #include "nsTextFragment.h"
#include "nsString.h"
#include "nsCRT.h" #include "nsCRT.h"
#include "nsReadableUtils.h" #include "nsReadableUtils.h"
#include "nsMemory.h" #include "nsMemory.h"
@ -241,27 +240,6 @@ nsTextFragment::SetTo(const PRUnichar* aBuffer, PRInt32 aLength)
mState.mLength = aLength; mState.mLength = aLength;
} }
void
nsTextFragment::AppendTo(nsAString& aString) const
{
if (mState.mIs2b) {
aString.Append(m2b, mState.mLength);
} else {
AppendASCIItoUTF16(Substring(m1b, m1b + mState.mLength),
aString);
}
}
void
nsTextFragment::AppendTo(nsAString& aString, PRInt32 aOffset, PRInt32 aLength) const
{
if (mState.mIs2b) {
aString.Append(m2b + aOffset, aLength);
} else {
AppendASCIItoUTF16(Substring(m1b + aOffset, m1b + aOffset + aLength), aString);
}
}
void void
nsTextFragment::CopyTo(PRUnichar *aDest, PRInt32 aOffset, PRInt32 aCount) nsTextFragment::CopyTo(PRUnichar *aDest, PRInt32 aOffset, PRInt32 aCount)
{ {

Просмотреть файл

@ -44,7 +44,7 @@
#ifndef nsTextFragment_h___ #ifndef nsTextFragment_h___
#define nsTextFragment_h___ #define nsTextFragment_h___
#include "nsAString.h" #include "nsString.h"
#include "nsTraceRefcnt.h" #include "nsTraceRefcnt.h"
class nsString; class nsString;
class nsCString; class nsCString;
@ -165,14 +165,27 @@ public:
/** /**
* Append the contents of this string fragment to aString * Append the contents of this string fragment to aString
*/ */
void AppendTo(nsAString& aString) const; void AppendTo(nsAString& aString) const {
if (mState.mIs2b) {
aString.Append(m2b, mState.mLength);
} else {
AppendASCIItoUTF16(Substring(m1b, m1b + mState.mLength),
aString);
}
}
/** /**
* Append a substring of the contents of this string fragment to aString. * Append a substring of the contents of this string fragment to aString.
* @param aOffset where to start the substring in this text fragment * @param aOffset where to start the substring in this text fragment
* @param aLength the length of the substring * @param aLength the length of the substring
*/ */
void AppendTo(nsAString& aString, PRInt32 aOffset, PRInt32 aLength) const; void AppendTo(nsAString& aString, PRInt32 aOffset, PRInt32 aLength) const {
if (mState.mIs2b) {
aString.Append(m2b + aOffset, aLength);
} else {
AppendASCIItoUTF16(Substring(m1b + aOffset, m1b + aOffset + aLength), aString);
}
}
/** /**
* Make a copy of the fragments contents starting at offset for * Make a copy of the fragments contents starting at offset for

Просмотреть файл

@ -51,6 +51,7 @@
#include "nsUnicharUtilCIID.h" #include "nsUnicharUtilCIID.h"
#include "nsServiceManagerUtils.h" #include "nsServiceManagerUtils.h"
#include "nsIContent.h" #include "nsIContent.h"
#include "nsTextFragment.h"
// IsIgnorableCharacter // IsIgnorableCharacter
// //
@ -429,13 +430,6 @@ IsBRElement(nsIDOMNode* aNode)
return NS_SUCCEEDED(rv); return NS_SUCCEEDED(rv);
} }
static void
GetNodeText(nsIDOMNode* aNode, nsAutoString& aText)
{
nsresult rv = aNode->GetNodeValue(aText);
NS_ASSERTION(NS_SUCCEEDED(rv), "Unable to get node text");
}
// Find the previous node in the DOM tree in preorder. This isn't fast because // Find the previous node in the DOM tree in preorder. This isn't fast because
// one call to GetPrevSibling can be O(N) in the number of siblings... // one call to GetPrevSibling can be O(N) in the number of siblings...
static nsIDOMNode* static nsIDOMNode*
@ -480,10 +474,12 @@ ContainsDOMWordSeparator(nsIDOMNode* aNode, PRInt32 aBeforeOffset,
if (!IsTextNode(aNode)) if (!IsTextNode(aNode))
return PR_FALSE; return PR_FALSE;
nsAutoString str; nsCOMPtr<nsIContent> content = do_QueryInterface(aNode);
GetNodeText(aNode, str); NS_ASSERTION(content, "Where is our content?");
for (PRInt32 i = NS_MIN(aBeforeOffset, PRInt32(str.Length())) - 1; i >= 0; --i) { const nsTextFragment* textFragment = content->GetText();
if (IsDOMWordSeparator(str.CharAt(i))) { NS_ASSERTION(textFragment, "Where is our text?");
for (PRInt32 i = NS_MIN(aBeforeOffset, PRInt32(textFragment->GetLength())) - 1; i >= 0; --i) {
if (IsDOMWordSeparator(textFragment->CharAt(i))) {
*aSeparatorOffset = i; *aSeparatorOffset = i;
return PR_TRUE; return PR_TRUE;
} }
@ -584,7 +580,6 @@ mozInlineSpellWordUtil::BuildSoftText()
PRBool seenSoftEnd = PR_FALSE; PRBool seenSoftEnd = PR_FALSE;
// Leave this outside the loop so large heap string allocations can be reused // Leave this outside the loop so large heap string allocations can be reused
// across iterations // across iterations
nsAutoString str;
while (node) { while (node) {
if (node == mSoftEnd.mNode) { if (node == mSoftEnd.mNode) {
seenSoftEnd = PR_TRUE; seenSoftEnd = PR_TRUE;
@ -592,14 +587,17 @@ mozInlineSpellWordUtil::BuildSoftText()
PRBool exit = PR_FALSE; PRBool exit = PR_FALSE;
if (IsTextNode(node)) { if (IsTextNode(node)) {
GetNodeText(node, str); nsCOMPtr<nsIContent> content = do_QueryInterface(node);
PRInt32 lastOffsetInNode = str.Length(); NS_ASSERTION(content, "Where is our content?");
const nsTextFragment* textFragment = content->GetText();
NS_ASSERTION(textFragment, "Where is our text?");
PRInt32 lastOffsetInNode = textFragment->GetLength();
if (seenSoftEnd) { if (seenSoftEnd) {
// check whether we can stop after this // check whether we can stop after this
for (PRInt32 i = node == mSoftEnd.mNode ? mSoftEnd.mOffset : 0; for (PRInt32 i = node == mSoftEnd.mNode ? mSoftEnd.mOffset : 0;
i < PRInt32(str.Length()); ++i) { i < PRInt32(textFragment->GetLength()); ++i) {
if (IsDOMWordSeparator(str.CharAt(i))) { if (IsDOMWordSeparator(textFragment->CharAt(i))) {
exit = PR_TRUE; exit = PR_TRUE;
// stop at the first separator after the soft end point // stop at the first separator after the soft end point
lastOffsetInNode = i; lastOffsetInNode = i;
@ -612,7 +610,7 @@ mozInlineSpellWordUtil::BuildSoftText()
PRInt32 len = lastOffsetInNode - firstOffsetInNode; PRInt32 len = lastOffsetInNode - firstOffsetInNode;
mSoftTextDOMMapping.AppendElement( mSoftTextDOMMapping.AppendElement(
DOMTextMapping(NodeOffset(node, firstOffsetInNode), mSoftText.Length(), len)); DOMTextMapping(NodeOffset(node, firstOffsetInNode), mSoftText.Length(), len));
mSoftText.Append(Substring(str, firstOffsetInNode, len)); textFragment->AppendTo(mSoftText, firstOffsetInNode, len);
} }
firstOffsetInNode = 0; firstOffsetInNode = 0;