Bug 1004522 part 12 - Clean up nsWSRunObject::DeleteChars; r=ehsan

This commit is contained in:
Aryeh Gregor 2014-05-02 15:15:27 +03:00
parent 4c178c661b
commit 4d0b76a7fc
4 changed files with 100 additions and 110 deletions

View File

@ -8,6 +8,7 @@
#include "mozilla/Assertions.h" // for MOZ_ASSERT, etc. #include "mozilla/Assertions.h" // for MOZ_ASSERT, etc.
#include "mozilla/TypedEnum.h" // for MOZ_BEGIN_ENUM_CLASS, etc. #include "mozilla/TypedEnum.h" // for MOZ_BEGIN_ENUM_CLASS, etc.
#include "mozilla/dom/Text.h"
#include "nsAutoPtr.h" // for nsRefPtr #include "nsAutoPtr.h" // for nsRefPtr
#include "nsCOMArray.h" // for nsCOMArray #include "nsCOMArray.h" // for nsCOMArray
#include "nsCOMPtr.h" // for already_AddRefed, nsCOMPtr #include "nsCOMPtr.h" // for already_AddRefed, nsCOMPtr
@ -330,6 +331,13 @@ protected:
uint32_t aOffset, uint32_t aOffset,
uint32_t aLength); uint32_t aLength);
inline nsresult DeleteText(mozilla::dom::Text* aText, uint32_t aOffset,
uint32_t aLength)
{
return DeleteText(static_cast<nsIDOMCharacterData*>(GetAsDOMNode(aText)),
aOffset, aLength);
}
// NS_IMETHOD DeleteRange(nsIDOMRange *aRange); // NS_IMETHOD DeleteRange(nsIDOMRange *aRange);
nsresult CreateTxnForDeleteText(nsIDOMCharacterData* aElement, nsresult CreateTxnForDeleteText(nsIDOMCharacterData* aElement,

View File

@ -311,6 +311,7 @@ public:
EStripWrappers aStripWrappers); EStripWrappers aStripWrappers);
nsresult DeleteNode(nsINode* aNode); nsresult DeleteNode(nsINode* aNode);
NS_IMETHODIMP DeleteNode(nsIDOMNode * aNode); NS_IMETHODIMP DeleteNode(nsIDOMNode * aNode);
using nsEditor::DeleteText;
NS_IMETHODIMP DeleteText(nsIDOMCharacterData *aTextNode, NS_IMETHODIMP DeleteText(nsIDOMCharacterData *aTextNode,
uint32_t aOffset, uint32_t aOffset,
uint32_t aLength); uint32_t aLength);

View File

@ -219,8 +219,8 @@ nsWSRunObject::InsertBreak(nsCOMPtr<nsINode>* aInOutParent,
// Delete the leading ws that is after insertion point. We don't // Delete the leading ws that is after insertion point. We don't
// have to (it would still not be significant after br), but it's // have to (it would still not be significant after br), but it's
// just more aesthetically pleasing to. // just more aesthetically pleasing to.
res = DeleteChars(GetAsDOMNode(*aInOutParent), *aInOutOffset, res = DeleteChars(*aInOutParent, *aInOutOffset,
GetAsDOMNode(afterRun->mEndNode), afterRun->mEndOffset, afterRun->mEndNode, afterRun->mEndOffset,
eOutsideUserSelectAll); eOutsideUserSelectAll);
NS_ENSURE_SUCCESS(res, nullptr); NS_ENSURE_SUCCESS(res, nullptr);
} else if (afterRun->mType == WSType::normalWS) { } else if (afterRun->mType == WSType::normalWS) {
@ -243,8 +243,8 @@ nsWSRunObject::InsertBreak(nsCOMPtr<nsINode>* aInOutParent,
} else if (beforeRun->mType & WSType::trailingWS) { } else if (beforeRun->mType & WSType::trailingWS) {
// Need to delete the trailing ws that is before insertion point, because it // Need to delete the trailing ws that is before insertion point, because it
// would become significant after break inserted. // would become significant after break inserted.
res = DeleteChars(GetAsDOMNode(beforeRun->mStartNode), beforeRun->mStartOffset, res = DeleteChars(beforeRun->mStartNode, beforeRun->mStartOffset,
GetAsDOMNode(*aInOutParent), *aInOutOffset, *aInOutParent, *aInOutOffset,
eOutsideUserSelectAll); eOutsideUserSelectAll);
NS_ENSURE_SUCCESS(res, nullptr); NS_ENSURE_SUCCESS(res, nullptr);
} else if (beforeRun->mType == WSType::normalWS) { } else if (beforeRun->mType == WSType::normalWS) {
@ -297,7 +297,7 @@ nsWSRunObject::InsertText(const nsAString& aStringToInsert,
} else if (afterRun->mType & WSType::leadingWS) { } else if (afterRun->mType & WSType::leadingWS) {
// Delete the leading ws that is after insertion point, because it // Delete the leading ws that is after insertion point, because it
// would become significant after text inserted. // would become significant after text inserted.
res = DeleteChars(GetAsDOMNode(*aInOutParent), *aInOutOffset, GetAsDOMNode(afterRun->mEndNode), res = DeleteChars(*aInOutParent, *aInOutOffset, afterRun->mEndNode,
afterRun->mEndOffset, eOutsideUserSelectAll); afterRun->mEndOffset, eOutsideUserSelectAll);
NS_ENSURE_SUCCESS(res, res); NS_ENSURE_SUCCESS(res, res);
} else if (afterRun->mType == WSType::normalWS) { } else if (afterRun->mType == WSType::normalWS) {
@ -313,8 +313,8 @@ nsWSRunObject::InsertText(const nsAString& aStringToInsert,
} else if (beforeRun->mType & WSType::trailingWS) { } else if (beforeRun->mType & WSType::trailingWS) {
// Need to delete the trailing ws that is before insertion point, because // Need to delete the trailing ws that is before insertion point, because
// it would become significant after text inserted. // it would become significant after text inserted.
res = DeleteChars(GetAsDOMNode(beforeRun->mStartNode), beforeRun->mStartOffset, res = DeleteChars(beforeRun->mStartNode, beforeRun->mStartOffset,
GetAsDOMNode(*aInOutParent), *aInOutOffset, eOutsideUserSelectAll); *aInOutParent, *aInOutOffset, eOutsideUserSelectAll);
NS_ENSURE_SUCCESS(res, res); NS_ENSURE_SUCCESS(res, res);
} else if (beforeRun->mType == WSType::normalWS) { } else if (beforeRun->mType == WSType::normalWS) {
// Try to change an nbsp to a space, if possible, just to prevent nbsp // Try to change an nbsp to a space, if possible, just to prevent nbsp
@ -399,8 +399,8 @@ nsWSRunObject::DeleteWSBackward()
if (mPRE) { if (mPRE) {
// easy case, preformatted ws // easy case, preformatted ws
if (nsCRT::IsAsciiSpace(point.mChar) || point.mChar == nbsp) { if (nsCRT::IsAsciiSpace(point.mChar) || point.mChar == nbsp) {
return DeleteChars(GetAsDOMNode(point.mTextNode), point.mOffset, return DeleteChars(point.mTextNode, point.mOffset,
GetAsDOMNode(point.mTextNode), point.mOffset + 1); point.mTextNode, point.mOffset + 1);
} }
} }
@ -423,8 +423,7 @@ nsWSRunObject::DeleteWSBackward()
NS_ENSURE_SUCCESS(res, res); NS_ENSURE_SUCCESS(res, res);
// finally, delete that ws // finally, delete that ws
return DeleteChars(GetAsDOMNode(startNode), startOffset, return DeleteChars(startNode, startOffset, endNode, endOffset);
GetAsDOMNode(endNode), endOffset);
} else if (point.mChar == nbsp) { } else if (point.mChar == nbsp) {
nsCOMPtr<nsINode> node(point.mTextNode); nsCOMPtr<nsINode> node(point.mTextNode);
// adjust surrounding ws // adjust surrounding ws
@ -437,7 +436,7 @@ nsWSRunObject::DeleteWSBackward()
NS_ENSURE_SUCCESS(res, res); NS_ENSURE_SUCCESS(res, res);
// finally, delete that ws // finally, delete that ws
return DeleteChars(GetAsDOMNode(node), startOffset, GetAsDOMNode(node), endOffset); return DeleteChars(node, startOffset, node, endOffset);
} }
return NS_OK; return NS_OK;
} }
@ -451,8 +450,8 @@ nsWSRunObject::DeleteWSForward()
if (mPRE) { if (mPRE) {
// easy case, preformatted ws // easy case, preformatted ws
if (nsCRT::IsAsciiSpace(point.mChar) || point.mChar == nbsp) { if (nsCRT::IsAsciiSpace(point.mChar) || point.mChar == nbsp) {
return DeleteChars(GetAsDOMNode(point.mTextNode), point.mOffset, return DeleteChars(point.mTextNode, point.mOffset,
GetAsDOMNode(point.mTextNode), point.mOffset + 1); point.mTextNode, point.mOffset + 1);
} }
} }
@ -472,7 +471,7 @@ nsWSRunObject::DeleteWSForward()
NS_ENSURE_SUCCESS(res, res); NS_ENSURE_SUCCESS(res, res);
// Finally, delete that ws // Finally, delete that ws
return DeleteChars(GetAsDOMNode(startNode), startOffset, GetAsDOMNode(endNode), endOffset); return DeleteChars(startNode, startOffset, endNode, endOffset);
} else if (point.mChar == nbsp) { } else if (point.mChar == nbsp) {
nsCOMPtr<nsINode> node(point.mTextNode); nsCOMPtr<nsINode> node(point.mTextNode);
// Adjust surrounding ws // Adjust surrounding ws
@ -483,7 +482,7 @@ nsWSRunObject::DeleteWSForward()
NS_ENSURE_SUCCESS(res, res); NS_ENSURE_SUCCESS(res, res);
// Finally, delete that ws // Finally, delete that ws
return DeleteChars(GetAsDOMNode(node), startOffset, GetAsDOMNode(node), endOffset); return DeleteChars(node, startOffset, node, endOffset);
} }
return NS_OK; return NS_OK;
} }
@ -1219,7 +1218,8 @@ nsWSRunObject::PrepareToDeleteRangePriv(nsWSRunObject* aEndObject)
// trim after run of any leading ws // trim after run of any leading ws
if (afterRun && (afterRun->mType & WSType::leadingWS)) { if (afterRun && (afterRun->mType & WSType::leadingWS)) {
res = aEndObject->DeleteChars(GetAsDOMNode(aEndObject->mNode), aEndObject->mOffset, GetAsDOMNode(afterRun->mEndNode), afterRun->mEndOffset, res = aEndObject->DeleteChars(aEndObject->mNode, aEndObject->mOffset,
afterRun->mEndNode, afterRun->mEndOffset,
eOutsideUserSelectAll); eOutsideUserSelectAll);
NS_ENSURE_SUCCESS(res, res); NS_ENSURE_SUCCESS(res, res);
} }
@ -1240,8 +1240,8 @@ nsWSRunObject::PrepareToDeleteRangePriv(nsWSRunObject* aEndObject)
} }
// trim before run of any trailing ws // trim before run of any trailing ws
if (beforeRun && (beforeRun->mType & WSType::trailingWS)) { if (beforeRun && (beforeRun->mType & WSType::trailingWS)) {
res = DeleteChars(GetAsDOMNode(beforeRun->mStartNode), beforeRun->mStartOffset, GetAsDOMNode(mNode), mOffset, res = DeleteChars(beforeRun->mStartNode, beforeRun->mStartOffset,
eOutsideUserSelectAll); mNode, mOffset, eOutsideUserSelectAll);
NS_ENSURE_SUCCESS(res, res); NS_ENSURE_SUCCESS(res, res);
} else if (beforeRun && beforeRun->mType == WSType::normalWS && !mPRE) { } else if (beforeRun && beforeRun->mType == WSType::normalWS && !mPRE) {
if ((afterRun && (afterRun->mType & WSType::trailingWS)) || if ((afterRun && (afterRun->mType & WSType::trailingWS)) ||
@ -1308,111 +1308,91 @@ nsWSRunObject::PrepareToSplitAcrossBlocksPriv()
return res; return res;
} }
nsresult nsresult
nsWSRunObject::DeleteChars(nsIDOMNode *aStartNode, int32_t aStartOffset, nsWSRunObject::DeleteChars(nsINode* aStartNode, int32_t aStartOffset,
nsIDOMNode *aEndNode, int32_t aEndOffset, nsINode* aEndNode, int32_t aEndOffset,
AreaRestriction aAR) AreaRestriction aAR)
{ {
// MOOSE: this routine needs to be modified to preserve the integrity of the // MOOSE: this routine needs to be modified to preserve the integrity of the
// wsFragment info. // wsFragment info.
NS_ENSURE_TRUE(aStartNode && aEndNode, NS_ERROR_NULL_POINTER); NS_ENSURE_TRUE(aStartNode && aEndNode, NS_ERROR_NULL_POINTER);
if (aAR == eOutsideUserSelectAll) if (aAR == eOutsideUserSelectAll) {
{ nsCOMPtr<nsIDOMNode> san =
nsCOMPtr<nsIDOMNode> san = mHTMLEditor->FindUserSelectAllNode(aStartNode); mHTMLEditor->FindUserSelectAllNode(GetAsDOMNode(aStartNode));
if (san) if (san) {
return NS_OK; return NS_OK;
}
if (aStartNode != aEndNode)
{ if (aStartNode != aEndNode) {
san = mHTMLEditor->FindUserSelectAllNode(aEndNode); san = mHTMLEditor->FindUserSelectAllNode(GetAsDOMNode(aEndNode));
if (san) if (san) {
return NS_OK; return NS_OK;
}
}
if ((aStartNode == aEndNode) && (aStartOffset == aEndOffset))
return NS_OK; // nothing to delete
nsresult res = NS_OK;
nsCOMPtr<nsINode> startNode = do_QueryInterface(aStartNode);
int32_t idx = mNodeArray.IndexOf(startNode);
if (idx==-1) idx = 0; // if our strarting point wasn't one of our ws text nodes,
// then just go through them from the beginning.
nsCOMPtr<nsIDOMNode> node;
nsCOMPtr<nsIDOMCharacterData> textnode;
nsRefPtr<nsRange> range;
if (aStartNode == aEndNode)
{
textnode = do_QueryInterface(aStartNode);
if (textnode)
{
return mHTMLEditor->DeleteText(textnode, (uint32_t)aStartOffset,
(uint32_t)(aEndOffset-aStartOffset));
}
}
int32_t count = mNodeArray.Length();
while (idx < count)
{
node = GetAsDOMNode(mNodeArray[idx]);
if (!node)
break; // we ran out of ws nodes; must have been deleting to end
if (node == aStartNode)
{
textnode = do_QueryInterface(node);
uint32_t len;
textnode->GetLength(&len);
if (uint32_t(aStartOffset)<len)
{
res = mHTMLEditor->DeleteText(textnode, (uint32_t)aStartOffset, len-aStartOffset);
NS_ENSURE_SUCCESS(res, res);
} }
} }
else if (node == aEndNode) }
{
if (aEndOffset) if (aStartNode == aEndNode && aStartOffset == aEndOffset) {
{ // Nothing to delete
textnode = do_QueryInterface(node); return NS_OK;
res = mHTMLEditor->DeleteText(textnode, 0, (uint32_t)aEndOffset); }
int32_t idx = mNodeArray.IndexOf(aStartNode);
if (idx == -1) {
// If our strarting point wasn't one of our ws text nodes, then just go
// through them from the beginning.
idx = 0;
}
if (aStartNode == aEndNode && aStartNode->IsNodeOfType(nsINode::eTEXT)) {
return mHTMLEditor->DeleteText(static_cast<Text*>(aStartNode),
(uint32_t)aStartOffset, (uint32_t)(aEndOffset - aStartOffset));
}
nsresult res;
nsRefPtr<nsRange> range;
int32_t count = mNodeArray.Length();
for (; idx < count; idx++) {
nsCOMPtr<Text> node = mNodeArray[idx];
if (!node) {
// We ran out of ws nodes; must have been deleting to end
return NS_OK;
}
if (node == aStartNode) {
uint32_t len = node->Length();
if (uint32_t(aStartOffset) < len) {
res = mHTMLEditor->DeleteText(node, (uint32_t)aStartOffset,
len - aStartOffset);
NS_ENSURE_SUCCESS(res, res);
}
} else if (node == aEndNode) {
if (aEndOffset) {
res = mHTMLEditor->DeleteText(node, 0, (uint32_t)aEndOffset);
NS_ENSURE_SUCCESS(res, res); NS_ENSURE_SUCCESS(res, res);
} }
break; break;
} } else {
else if (!range) {
{ range = new nsRange(aStartNode);
if (!range) res = range->Set(aStartNode, aStartOffset, aEndNode, aEndOffset);
{
nsCOMPtr<nsINode> startNode = do_QueryInterface(aStartNode);
NS_ENSURE_STATE(startNode);
range = new nsRange(startNode);
res = range->SetStart(startNode, aStartOffset);
NS_ENSURE_SUCCESS(res, res);
res = range->SetEnd(aEndNode, aEndOffset);
NS_ENSURE_SUCCESS(res, res); NS_ENSURE_SUCCESS(res, res);
} }
bool nodeBefore, nodeAfter; bool nodeBefore, nodeAfter;
nsCOMPtr<nsIContent> content (do_QueryInterface(node)); res = nsRange::CompareNodeToRange(node, range, &nodeBefore, &nodeAfter);
res = nsRange::CompareNodeToRange(content, range, &nodeBefore, &nodeAfter);
NS_ENSURE_SUCCESS(res, res); NS_ENSURE_SUCCESS(res, res);
if (nodeAfter) if (nodeAfter) {
{
break; break;
} }
if (!nodeBefore) if (!nodeBefore) {
{
res = mHTMLEditor->DeleteNode(node); res = mHTMLEditor->DeleteNode(node);
NS_ENSURE_SUCCESS(res, res); NS_ENSURE_SUCCESS(res, res);
nsCOMPtr<nsINode> node_ = do_QueryInterface(node); mNodeArray.RemoveElement(node);
mNodeArray.RemoveElement(node_);
--count; --count;
--idx; --idx;
} }
} }
idx++;
} }
return res; return NS_OK;
} }
nsWSRunObject::WSPoint nsWSRunObject::WSPoint
@ -1540,7 +1520,7 @@ nsWSRunObject::ConvertToNBSP(WSPoint aPoint, AreaRestriction aAR)
// Finally, delete that replaced ws, if any // Finally, delete that replaced ws, if any
if (startNode) { if (startNode) {
res = DeleteChars(GetAsDOMNode(startNode), startOffset, GetAsDOMNode(endNode), endOffset); res = DeleteChars(startNode, startOffset, endNode, endOffset);
NS_ENSURE_SUCCESS(res, res); NS_ENSURE_SUCCESS(res, res);
} }
@ -1882,8 +1862,8 @@ nsWSRunObject::CheckTrailingNBSPOfRun(WSFragment *aRun)
NS_ENSURE_SUCCESS(res, res); NS_ENSURE_SUCCESS(res, res);
// Finally, delete that nbsp // Finally, delete that nbsp
res = DeleteChars(GetAsDOMNode(thePoint.mTextNode), thePoint.mOffset + 1, res = DeleteChars(thePoint.mTextNode, thePoint.mOffset + 1,
GetAsDOMNode(thePoint.mTextNode), thePoint.mOffset + 2); thePoint.mTextNode, thePoint.mOffset + 2);
NS_ENSURE_SUCCESS(res, res); NS_ENSURE_SUCCESS(res, res);
} else if (!mPRE && spaceNBSP && rightCheck) { } else if (!mPRE && spaceNBSP && rightCheck) {
// Don't mess with this preformatted for now. We have a run of ASCII // Don't mess with this preformatted for now. We have a run of ASCII
@ -1900,8 +1880,8 @@ nsWSRunObject::CheckTrailingNBSPOfRun(WSFragment *aRun)
getter_AddRefs(endNode), &endOffset); getter_AddRefs(endNode), &endOffset);
// Delete that nbsp // Delete that nbsp
res = DeleteChars(GetAsDOMNode(thePoint.mTextNode), thePoint.mOffset, res = DeleteChars(thePoint.mTextNode, thePoint.mOffset,
GetAsDOMNode(thePoint.mTextNode), thePoint.mOffset + 1); thePoint.mTextNode, thePoint.mOffset + 1);
NS_ENSURE_SUCCESS(res, res); NS_ENSURE_SUCCESS(res, res);
// Finally, insert that nbsp before the ASCII ws run // Finally, insert that nbsp before the ASCII ws run
@ -1947,8 +1927,8 @@ nsWSRunObject::CheckTrailingNBSP(WSFragment* aRun, nsINode* aNode,
NS_ENSURE_SUCCESS(res, res); NS_ENSURE_SUCCESS(res, res);
// Finally, delete that nbsp // Finally, delete that nbsp
res = DeleteChars(GetAsDOMNode(thePoint.mTextNode), thePoint.mOffset + 1, res = DeleteChars(thePoint.mTextNode, thePoint.mOffset + 1,
GetAsDOMNode(thePoint.mTextNode), thePoint.mOffset + 2); thePoint.mTextNode, thePoint.mOffset + 2);
NS_ENSURE_SUCCESS(res, res); NS_ENSURE_SUCCESS(res, res);
} }
return NS_OK; return NS_OK;
@ -1988,8 +1968,8 @@ nsWSRunObject::CheckLeadingNBSP(WSFragment* aRun, nsINode* aNode,
NS_ENSURE_SUCCESS(res, res); NS_ENSURE_SUCCESS(res, res);
// Finally, delete that nbsp // Finally, delete that nbsp
res = DeleteChars(GetAsDOMNode(thePoint.mTextNode), thePoint.mOffset + 1, res = DeleteChars(thePoint.mTextNode, thePoint.mOffset + 1,
GetAsDOMNode(thePoint.mTextNode), thePoint.mOffset + 2); thePoint.mTextNode, thePoint.mOffset + 2);
NS_ENSURE_SUCCESS(res, res); NS_ENSURE_SUCCESS(res, res);
} }
return NS_OK; return NS_OK;
@ -2003,7 +1983,8 @@ nsWSRunObject::Scrub()
while (run) while (run)
{ {
if (run->mType & (WSType::leadingWS | WSType::trailingWS)) { if (run->mType & (WSType::leadingWS | WSType::trailingWS)) {
nsresult res = DeleteChars(GetAsDOMNode(run->mStartNode), run->mStartOffset, GetAsDOMNode(run->mEndNode), run->mEndOffset); nsresult res = DeleteChars(run->mStartNode, run->mStartOffset,
run->mEndNode, run->mEndOffset);
NS_ENSURE_SUCCESS(res, res); NS_ENSURE_SUCCESS(res, res);
} }
run = run->mRight; run = run->mRight;

View File

@ -325,8 +325,8 @@ class MOZ_STACK_CLASS nsWSRunObject
nsCOMPtr<nsINode>* aNextNode); nsCOMPtr<nsINode>* aNextNode);
nsresult PrepareToDeleteRangePriv(nsWSRunObject* aEndObject); nsresult PrepareToDeleteRangePriv(nsWSRunObject* aEndObject);
nsresult PrepareToSplitAcrossBlocksPriv(); nsresult PrepareToSplitAcrossBlocksPriv();
nsresult DeleteChars(nsIDOMNode *aStartNode, int32_t aStartOffset, nsresult DeleteChars(nsINode* aStartNode, int32_t aStartOffset,
nsIDOMNode *aEndNode, int32_t aEndOffset, nsINode* aEndNode, int32_t aEndOffset,
AreaRestriction aAR = eAnywhere); AreaRestriction aAR = eAnywhere);
WSPoint GetCharAfter(nsINode* aNode, int32_t aOffset); WSPoint GetCharAfter(nsINode* aNode, int32_t aOffset);
WSPoint GetCharBefore(nsINode* aNode, int32_t aOffset); WSPoint GetCharBefore(nsINode* aNode, int32_t aOffset);