Bug 1004522 part 10 - Clean up nsWSRunObject::CheckLeading/TrailingNBSP, ConvertToNBSP; r=ehsan

This commit is contained in:
Aryeh Gregor 2014-05-02 15:15:27 +03:00
parent 5e9f1f02fb
commit ba7d0c9171
2 changed files with 77 additions and 85 deletions

View File

@ -249,7 +249,7 @@ nsWSRunObject::InsertBreak(nsCOMPtr<nsINode>* aInOutParent,
NS_ENSURE_SUCCESS(res, nullptr); NS_ENSURE_SUCCESS(res, nullptr);
} else if (beforeRun->mType == WSType::normalWS) { } else if (beforeRun->mType == WSType::normalWS) {
// Try to change an nbsp to a space, just to prevent nbsp proliferation // Try to change an nbsp to a space, just to prevent nbsp proliferation
res = CheckTrailingNBSP(beforeRun, GetAsDOMNode(*aInOutParent), *aInOutOffset); res = CheckTrailingNBSP(beforeRun, *aInOutParent, *aInOutOffset);
NS_ENSURE_SUCCESS(res, nullptr); NS_ENSURE_SUCCESS(res, nullptr);
} }
} }
@ -303,7 +303,7 @@ nsWSRunObject::InsertText(const nsAString& aStringToInsert,
} else if (afterRun->mType == WSType::normalWS) { } else if (afterRun->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
// proliferation // proliferation
res = CheckLeadingNBSP(afterRun, GetAsDOMNode(*aInOutParent), *aInOutOffset); res = CheckLeadingNBSP(afterRun, *aInOutParent, *aInOutOffset);
NS_ENSURE_SUCCESS(res, res); NS_ENSURE_SUCCESS(res, res);
} }
@ -319,7 +319,7 @@ nsWSRunObject::InsertText(const nsAString& aStringToInsert,
} 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
// proliferation // proliferation
res = CheckTrailingNBSP(beforeRun, GetAsDOMNode(*aInOutParent), *aInOutOffset); res = CheckTrailingNBSP(beforeRun, *aInOutParent, *aInOutOffset);
NS_ENSURE_SUCCESS(res, res); NS_ENSURE_SUCCESS(res, res);
} }
} }
@ -1530,48 +1530,43 @@ nsWSRunObject::GetCharBefore(const WSPoint &aPoint)
return outPoint; return outPoint;
} }
nsresult nsresult
nsWSRunObject::ConvertToNBSP(WSPoint aPoint, AreaRestriction aAR) nsWSRunObject::ConvertToNBSP(WSPoint aPoint, 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(aPoint.mTextNode, NS_ERROR_NULL_POINTER); NS_ENSURE_TRUE(aPoint.mTextNode, NS_ERROR_NULL_POINTER);
if (aAR == eOutsideUserSelectAll) if (aAR == eOutsideUserSelectAll) {
{ nsCOMPtr<nsIDOMNode> san =
nsCOMPtr<nsIDOMNode> domnode = do_QueryInterface(aPoint.mTextNode); mHTMLEditor->FindUserSelectAllNode(GetAsDOMNode(aPoint.mTextNode));
if (domnode) if (san) {
{ return NS_OK;
nsCOMPtr<nsIDOMNode> san = mHTMLEditor->FindUserSelectAllNode(domnode);
if (san)
return NS_OK;
} }
} }
nsCOMPtr<nsIDOMCharacterData> textNode(do_QueryInterface(aPoint.mTextNode)); // First, insert an nbsp
NS_ENSURE_TRUE(textNode, NS_ERROR_NULL_POINTER);
nsCOMPtr<nsIDOMNode> node(do_QueryInterface(textNode));
// first, insert an nbsp
nsAutoTxnsConserveSelection dontSpazMySelection(mHTMLEditor); nsAutoTxnsConserveSelection dontSpazMySelection(mHTMLEditor);
nsAutoString nbspStr(nbsp); nsAutoString nbspStr(nbsp);
nsresult res = mHTMLEditor->InsertTextIntoTextNodeImpl(nbspStr, textNode, aPoint.mOffset, true); nsresult res = mHTMLEditor->InsertTextIntoTextNodeImpl(nbspStr,
aPoint.mTextNode, aPoint.mOffset, true);
NS_ENSURE_SUCCESS(res, res); NS_ENSURE_SUCCESS(res, res);
// next, find range of ws it will replace // Next, find range of ws it will replace
nsCOMPtr<nsIDOMNode> startNode, endNode; nsCOMPtr<Text> startNode, endNode;
int32_t startOffset=0, endOffset=0; int32_t startOffset = 0, endOffset = 0;
GetAsciiWSBounds(eAfter, node, aPoint.mOffset+1, address_of(startNode), GetAsciiWSBounds(eAfter, aPoint.mTextNode, aPoint.mOffset + 1,
&startOffset, address_of(endNode), &endOffset); getter_AddRefs(startNode), &startOffset,
getter_AddRefs(endNode), &endOffset);
// finally, delete that replaced ws, if any
if (startNode) // Finally, delete that replaced ws, if any
{ if (startNode) {
res = DeleteChars(startNode, startOffset, endNode, endOffset); res = DeleteChars(GetAsDOMNode(startNode), startOffset, GetAsDOMNode(endNode), endOffset);
NS_ENSURE_SUCCESS(res, res);
} }
return res; return NS_OK;
} }
void void
@ -1943,85 +1938,80 @@ nsWSRunObject::CheckTrailingNBSPOfRun(WSFragment *aRun)
} }
nsresult nsresult
nsWSRunObject::CheckTrailingNBSP(WSFragment *aRun, nsIDOMNode *aNode, int32_t aOffset) nsWSRunObject::CheckTrailingNBSP(WSFragment* aRun, nsINode* aNode,
{ int32_t aOffset)
// try to change an nbsp to a space, if possible, just to prevent nbsp proliferation. {
// this routine is called when we about to make this point in the ws abut an inserted break // Try to change an nbsp to a space, if possible, just to prevent nbsp
// or text, so we don't have to worry about what is after it. What is after it now will // proliferation. This routine is called when we are about to make this
// end up after the inserted object. // point in the ws abut an inserted break or text, so we don't have to worry
// about what is after it. What is after it now will end up after the
// inserted object.
NS_ENSURE_TRUE(aRun && aNode, NS_ERROR_NULL_POINTER); NS_ENSURE_TRUE(aRun && aNode, NS_ERROR_NULL_POINTER);
bool canConvert = false; bool canConvert = false;
nsCOMPtr<nsINode> node(do_QueryInterface(aNode)); WSPoint thePoint = GetCharBefore(aNode, aOffset);
WSPoint thePoint = GetCharBefore(node, aOffset);
if (thePoint.mTextNode && thePoint.mChar == nbsp) { if (thePoint.mTextNode && thePoint.mChar == nbsp) {
WSPoint prevPoint = GetCharBefore(thePoint); WSPoint prevPoint = GetCharBefore(thePoint);
if (prevPoint.mTextNode) { if (prevPoint.mTextNode) {
if (!nsCRT::IsAsciiSpace(prevPoint.mChar)) canConvert = true; if (!nsCRT::IsAsciiSpace(prevPoint.mChar)) {
} else if (aRun->mLeftType == WSType::text) { canConvert = true;
canConvert = true; }
} else if (aRun->mLeftType == WSType::special) { } else if (aRun->mLeftType == WSType::text ||
aRun->mLeftType == WSType::special) {
canConvert = true; canConvert = true;
} }
} }
if (canConvert) if (canConvert) {
{ // First, insert a space
// first, insert a space
nsCOMPtr<nsIDOMCharacterData> textNode(do_QueryInterface(thePoint.mTextNode));
NS_ENSURE_TRUE(textNode, NS_ERROR_NULL_POINTER);
nsAutoTxnsConserveSelection dontSpazMySelection(mHTMLEditor); nsAutoTxnsConserveSelection dontSpazMySelection(mHTMLEditor);
nsAutoString spaceStr(char16_t(32)); nsAutoString spaceStr(char16_t(32));
nsresult res = mHTMLEditor->InsertTextIntoTextNodeImpl(spaceStr, textNode, nsresult res = mHTMLEditor->InsertTextIntoTextNodeImpl(spaceStr,
thePoint.mOffset, thePoint.mTextNode, thePoint.mOffset, true);
true);
NS_ENSURE_SUCCESS(res, res); NS_ENSURE_SUCCESS(res, res);
// finally, delete that nbsp // Finally, delete that nbsp
nsCOMPtr<nsIDOMNode> delNode(do_QueryInterface(thePoint.mTextNode)); res = DeleteChars(GetAsDOMNode(thePoint.mTextNode), thePoint.mOffset + 1,
res = DeleteChars(delNode, thePoint.mOffset+1, delNode, thePoint.mOffset+2); GetAsDOMNode(thePoint.mTextNode), thePoint.mOffset + 2);
NS_ENSURE_SUCCESS(res, res); NS_ENSURE_SUCCESS(res, res);
} }
return NS_OK; return NS_OK;
} }
nsresult nsresult
nsWSRunObject::CheckLeadingNBSP(WSFragment *aRun, nsIDOMNode *aNode, int32_t aOffset) nsWSRunObject::CheckLeadingNBSP(WSFragment* aRun, nsINode* aNode,
{ int32_t aOffset)
// try to change an nbsp to a space, if possible, just to prevent nbsp proliferation {
// this routine is called when we about to make this point in the ws abut an inserted // Try to change an nbsp to a space, if possible, just to prevent nbsp
// text, so we don't have to worry about what is before it. What is before it now will // proliferation This routine is called when we are about to make this point
// end up before the inserted text. // in the ws abut an inserted text, so we don't have to worry about what is
// before it. What is before it now will end up before the inserted text.
bool canConvert = false; bool canConvert = false;
nsCOMPtr<nsINode> node(do_QueryInterface(aNode)); WSPoint thePoint = GetCharAfter(aNode, aOffset);
WSPoint thePoint = GetCharAfter(node, aOffset);
if (thePoint.mChar == nbsp) { if (thePoint.mChar == nbsp) {
WSPoint tmp = thePoint; WSPoint tmp = thePoint;
tmp.mOffset++; // we want to be after thePoint // we want to be after thePoint
tmp.mOffset++;
WSPoint nextPoint = GetCharAfter(tmp); WSPoint nextPoint = GetCharAfter(tmp);
if (nextPoint.mTextNode) { if (nextPoint.mTextNode) {
if (!nsCRT::IsAsciiSpace(nextPoint.mChar)) canConvert = true; if (!nsCRT::IsAsciiSpace(nextPoint.mChar)) {
} else if (aRun->mRightType == WSType::text) { canConvert = true;
canConvert = true; }
} else if (aRun->mRightType == WSType::special) { } else if (aRun->mRightType == WSType::text ||
canConvert = true; aRun->mRightType == WSType::special ||
} else if (aRun->mRightType == WSType::br) { aRun->mRightType == WSType::br) {
canConvert = true; canConvert = true;
} }
} }
if (canConvert) if (canConvert) {
{ // First, insert a space
// first, insert a space
nsCOMPtr<nsIDOMCharacterData> textNode(do_QueryInterface(thePoint.mTextNode));
NS_ENSURE_TRUE(textNode, NS_ERROR_NULL_POINTER);
nsAutoTxnsConserveSelection dontSpazMySelection(mHTMLEditor); nsAutoTxnsConserveSelection dontSpazMySelection(mHTMLEditor);
nsAutoString spaceStr(char16_t(32)); nsAutoString spaceStr(char16_t(32));
nsresult res = mHTMLEditor->InsertTextIntoTextNodeImpl(spaceStr, textNode, nsresult res = mHTMLEditor->InsertTextIntoTextNodeImpl(spaceStr,
thePoint.mOffset, thePoint.mTextNode, thePoint.mOffset, true);
true);
NS_ENSURE_SUCCESS(res, res); NS_ENSURE_SUCCESS(res, res);
// finally, delete that nbsp // Finally, delete that nbsp
nsCOMPtr<nsIDOMNode> delNode(do_QueryInterface(thePoint.mTextNode)); res = DeleteChars(GetAsDOMNode(thePoint.mTextNode), thePoint.mOffset + 1,
res = DeleteChars(delNode, thePoint.mOffset+1, delNode, thePoint.mOffset+2); GetAsDOMNode(thePoint.mTextNode), thePoint.mOffset + 2);
NS_ENSURE_SUCCESS(res, res); NS_ENSURE_SUCCESS(res, res);
} }
return NS_OK; return NS_OK;

View File

@ -350,8 +350,10 @@ class MOZ_STACK_CLASS nsWSRunObject
WSPoint GetWSPointAfter(nsIDOMNode *aNode, int32_t aOffset); WSPoint GetWSPointAfter(nsIDOMNode *aNode, int32_t aOffset);
WSPoint GetWSPointBefore(nsIDOMNode *aNode, int32_t aOffset); WSPoint GetWSPointBefore(nsIDOMNode *aNode, int32_t aOffset);
nsresult CheckTrailingNBSPOfRun(WSFragment *aRun); nsresult CheckTrailingNBSPOfRun(WSFragment *aRun);
nsresult CheckTrailingNBSP(WSFragment *aRun, nsIDOMNode *aNode, int32_t aOffset); nsresult CheckTrailingNBSP(WSFragment* aRun, nsINode* aNode,
nsresult CheckLeadingNBSP(WSFragment *aRun, nsIDOMNode *aNode, int32_t aOffset); int32_t aOffset);
nsresult CheckLeadingNBSP(WSFragment* aRun, nsINode* aNode,
int32_t aOffset);
nsresult Scrub(); nsresult Scrub();
nsresult GetPreviousWSNodeInner(nsINode* aStartNode, nsINode* aBlockParent, nsresult GetPreviousWSNodeInner(nsINode* aStartNode, nsINode* aBlockParent,