Bug 1310618 Replace nsresult variables |res| with |rv| under editor/ r=Ehsan

In our coding rules, variable names of nsresult should be rv.  Indeed, when you see |rv| in the code, you must assume that its type if nsresult.

However, a lot of code under editor/ uses |res| for the variables of nsresult.  Let's replace |res| with |rv|.

And this patch improves following points:

1. When |rv| is set in both |if| and |else| block and they are check outside of them, this moves the check into each |if| and |else| block because even if the failure is notified with warning, you cannot see which case was performed and failed.  This change makes it clear.

2. When |return rv;| returns non-error code because |rv| is checked with NS_ENSURE_SUCCESS() immediately before, setting replacing it with |return NS_OK;| is clearer.

3. Move declaration of |nsresult rv| into smaller scope as far as possible.  This prevents setting rv to unexpected value and easier to check its value at reading the code.

MozReview-Commit-ID: 9MAqj7sFey3
This commit is contained in:
Masayuki Nakano
2016-10-19 18:09:33 +09:00
parent ee0fac6df7
commit 381c9c05ee
21 changed files with 3041 additions and 2787 deletions

View File

@@ -71,11 +71,12 @@ HTMLEditor::AbsolutePositionSelection(bool aEnabled)
bool cancel, handled;
// Protect the edit rules object from dying
nsCOMPtr<nsIEditRules> rules(mRules);
nsresult res = rules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
if (NS_FAILED(res) || cancel)
return res;
nsresult rv = rules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
if (NS_FAILED(rv) || cancel) {
return rv;
}
return rules->DidDoAction(selection, &ruleInfo, res);
return rules->DidDoAction(selection, &ruleInfo, rv);
}
NS_IMETHODIMP
@@ -86,10 +87,10 @@ HTMLEditor::GetAbsolutelyPositionedSelectionContainer(nsIDOMElement** _retval)
nsCOMPtr<nsIDOMNode> resultNode;
while (!resultNode && node && !node->IsHTMLElement(nsGkAtoms::html)) {
nsresult res =
nsresult rv =
mCSSEditUtils->GetComputedProperty(*node, *nsGkAtoms::position,
positionStr);
NS_ENSURE_SUCCESS(res, res);
NS_ENSURE_SUCCESS(rv, rv);
if (positionStr.EqualsLiteral("absolute"))
resultNode = GetAsDOMNode(node);
else {
@@ -135,8 +136,8 @@ HTMLEditor::RelativeChangeElementZIndex(nsIDOMElement* aElement,
return NS_OK;
int32_t zIndex;
nsresult res = GetElementZIndex(aElement, &zIndex);
NS_ENSURE_SUCCESS(res, res);
nsresult rv = GetElementZIndex(aElement, &zIndex);
NS_ENSURE_SUCCESS(rv, rv);
zIndex = std::max(zIndex + aChange, 0);
SetElementZIndex(aElement, zIndex);
@@ -177,11 +178,12 @@ HTMLEditor::RelativeChangeZIndex(int32_t aChange)
bool cancel, handled;
// Protect the edit rules object from dying
nsCOMPtr<nsIEditRules> rules(mRules);
nsresult res = rules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
if (cancel || NS_FAILED(res))
return res;
nsresult rv = rules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
if (cancel || NS_FAILED(rv)) {
return rv;
}
return rules->DidDoAction(selection, &ruleInfo, res);
return rules->DidDoAction(selection, &ruleInfo, rv);
}
NS_IMETHODIMP
@@ -193,29 +195,29 @@ HTMLEditor::GetElementZIndex(nsIDOMElement* aElement,
nsAutoString zIndexStr;
*aZindex = 0;
nsresult res = mCSSEditUtils->GetSpecifiedProperty(*element,
*nsGkAtoms::z_index,
zIndexStr);
NS_ENSURE_SUCCESS(res, res);
nsresult rv =
mCSSEditUtils->GetSpecifiedProperty(*element, *nsGkAtoms::z_index,
zIndexStr);
NS_ENSURE_SUCCESS(rv, rv);
if (zIndexStr.EqualsLiteral("auto")) {
// we have to look at the positioned ancestors
// cf. CSS 2 spec section 9.9.1
nsCOMPtr<nsIDOMNode> parentNode;
res = aElement->GetParentNode(getter_AddRefs(parentNode));
NS_ENSURE_SUCCESS(res, res);
rv = aElement->GetParentNode(getter_AddRefs(parentNode));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsINode> node = do_QueryInterface(parentNode);
nsAutoString positionStr;
while (node && zIndexStr.EqualsLiteral("auto") &&
!node->IsHTMLElement(nsGkAtoms::body)) {
res = mCSSEditUtils->GetComputedProperty(*node, *nsGkAtoms::position,
positionStr);
NS_ENSURE_SUCCESS(res, res);
rv = mCSSEditUtils->GetComputedProperty(*node, *nsGkAtoms::position,
positionStr);
NS_ENSURE_SUCCESS(rv, rv);
if (positionStr.EqualsLiteral("absolute")) {
// ah, we found one, what's its z-index ? If its z-index is auto,
// we have to continue climbing the document's tree
res = mCSSEditUtils->GetComputedProperty(*node, *nsGkAtoms::z_index,
zIndexStr);
NS_ENSURE_SUCCESS(res, res);
rv = mCSSEditUtils->GetComputedProperty(*node, *nsGkAtoms::z_index,
zIndexStr);
NS_ENSURE_SUCCESS(rv, rv);
}
node = node->GetParentNode();
}
@@ -254,17 +256,18 @@ HTMLEditor::RefreshGrabber()
{
NS_ENSURE_TRUE(mAbsolutelyPositionedObject, NS_ERROR_NULL_POINTER);
nsresult res = GetPositionAndDimensions(static_cast<nsIDOMElement*>(GetAsDOMNode(mAbsolutelyPositionedObject)),
mPositionedObjectX,
mPositionedObjectY,
mPositionedObjectWidth,
mPositionedObjectHeight,
mPositionedObjectBorderLeft,
mPositionedObjectBorderTop,
mPositionedObjectMarginLeft,
mPositionedObjectMarginTop);
NS_ENSURE_SUCCESS(res, res);
nsresult rv =
GetPositionAndDimensions(
static_cast<nsIDOMElement*>(GetAsDOMNode(mAbsolutelyPositionedObject)),
mPositionedObjectX,
mPositionedObjectY,
mPositionedObjectWidth,
mPositionedObjectHeight,
mPositionedObjectBorderLeft,
mPositionedObjectBorderTop,
mPositionedObjectMarginLeft,
mPositionedObjectMarginTop);
NS_ENSURE_SUCCESS(rv, rv);
SetAnonymousElementPosition(mPositionedObjectX+12,
mPositionedObjectY-14,
@@ -275,10 +278,10 @@ HTMLEditor::RefreshGrabber()
NS_IMETHODIMP
HTMLEditor::HideGrabber()
{
nsresult res = mAbsolutelyPositionedObject->UnsetAttr(kNameSpaceID_None,
nsGkAtoms::_moz_abspos,
true);
NS_ENSURE_SUCCESS(res, res);
nsresult rv = mAbsolutelyPositionedObject->UnsetAttr(kNameSpaceID_None,
nsGkAtoms::_moz_abspos,
true);
NS_ENSURE_SUCCESS(rv, rv);
mAbsolutelyPositionedObject = nullptr;
NS_ENSURE_TRUE(mGrabber, NS_ERROR_NULL_POINTER);
@@ -312,12 +315,12 @@ HTMLEditor::ShowGrabberOnElement(nsIDOMElement* aElement)
}
nsAutoString classValue;
nsresult res = CheckPositionedElementBGandFG(aElement, classValue);
NS_ENSURE_SUCCESS(res, res);
nsresult rv = CheckPositionedElementBGandFG(aElement, classValue);
NS_ENSURE_SUCCESS(rv, rv);
res = element->SetAttr(kNameSpaceID_None, nsGkAtoms::_moz_abspos,
classValue, true);
NS_ENSURE_SUCCESS(res, res);
rv = element->SetAttr(kNameSpaceID_None, nsGkAtoms::_moz_abspos,
classValue, true);
NS_ENSURE_SUCCESS(rv, rv);
// first, let's keep track of that element...
mAbsolutelyPositionedObject = element;
@@ -338,10 +341,10 @@ HTMLEditor::StartMoving(nsIDOMElement* aHandle)
mPositioningShadow = CreateShadow(GetAsDOMNode(parentNode),
static_cast<nsIDOMElement*>(GetAsDOMNode(mAbsolutelyPositionedObject)));
NS_ENSURE_TRUE(mPositioningShadow, NS_ERROR_FAILURE);
nsresult res = SetShadowPosition(mPositioningShadow,
mAbsolutelyPositionedObject,
mPositionedObjectX, mPositionedObjectY);
NS_ENSURE_SUCCESS(res,res);
nsresult rv = SetShadowPosition(mPositioningShadow,
mAbsolutelyPositionedObject,
mPositionedObjectX, mPositionedObjectY);
NS_ENSURE_SUCCESS(rv, rv);
// make the shadow appear
mPositioningShadow->UnsetAttr(kNameSpaceID_None, nsGkAtoms::_class, true);
@@ -353,7 +356,7 @@ HTMLEditor::StartMoving(nsIDOMElement* aHandle)
mPositionedObjectHeight);
mIsMoving = true;
return res;
return NS_OK; // XXX Looks like nobody refers this result
}
void
@@ -369,7 +372,7 @@ nsresult
HTMLEditor::GrabberClicked()
{
// add a mouse move listener to the editor
nsresult res = NS_OK;
nsresult rv = NS_OK;
if (!mMouseMotionListenerP) {
mMouseMotionListenerP = new ResizerMouseMotionListener(this);
if (!mMouseMotionListenerP) {return NS_ERROR_NULL_POINTER;}
@@ -377,14 +380,14 @@ HTMLEditor::GrabberClicked()
nsCOMPtr<nsIDOMEventTarget> piTarget = GetDOMEventTarget();
NS_ENSURE_TRUE(piTarget, NS_ERROR_FAILURE);
res = piTarget->AddEventListener(NS_LITERAL_STRING("mousemove"),
rv = piTarget->AddEventListener(NS_LITERAL_STRING("mousemove"),
mMouseMotionListenerP,
false, false);
NS_ASSERTION(NS_SUCCEEDED(res),
NS_ASSERTION(NS_SUCCEEDED(rv),
"failed to register mouse motion listener");
}
mGrabberClicked = true;
return res;
return rv;
}
nsresult
@@ -405,13 +408,11 @@ HTMLEditor::EndMoving()
nsCOMPtr<nsIDOMEventTarget> piTarget = GetDOMEventTarget();
if (piTarget && mMouseMotionListenerP) {
#ifdef DEBUG
nsresult res =
#endif
piTarget->RemoveEventListener(NS_LITERAL_STRING("mousemove"),
mMouseMotionListenerP,
false);
NS_ASSERTION(NS_SUCCEEDED(res), "failed to remove mouse motion listener");
DebugOnly<nsresult> rv =
piTarget->RemoveEventListener(NS_LITERAL_STRING("mousemove"),
mMouseMotionListenerP,
false);
NS_ASSERTION(NS_SUCCEEDED(rv), "failed to remove mouse motion listener");
}
mMouseMotionListenerP = nullptr;
@@ -427,8 +428,8 @@ nsresult
HTMLEditor::SetFinalPosition(int32_t aX,
int32_t aY)
{
nsresult res = EndMoving();
NS_ENSURE_SUCCESS(res, res);
nsresult rv = EndMoving();
NS_ENSURE_SUCCESS(rv, rv);
// we have now to set the new width and height of the resized object
// we don't set the x and y position because we don't control that in
@@ -508,8 +509,8 @@ HTMLEditor::AbsolutelyPositionElement(nsIDOMElement* aElement,
nsINode* parentNode = element->GetParentNode();
if (parentNode->GetChildCount() == 1) {
nsCOMPtr<nsIDOMNode> brNode;
nsresult res = CreateBR(parentNode->AsDOMNode(), 0, address_of(brNode));
NS_ENSURE_SUCCESS(res, res);
nsresult rv = CreateBR(parentNode->AsDOMNode(), 0, address_of(brNode));
NS_ENSURE_SUCCESS(rv, rv);
}
}
else {
@@ -535,10 +536,10 @@ HTMLEditor::AbsolutelyPositionElement(nsIDOMElement* aElement,
RefPtr<HTMLEditRules> htmlRules =
static_cast<HTMLEditRules*>(mRules.get());
NS_ENSURE_TRUE(htmlRules, NS_ERROR_FAILURE);
nsresult res = htmlRules->MakeSureElemStartsOrEndsOnCR(aElement);
NS_ENSURE_SUCCESS(res, res);
res = RemoveContainer(element);
NS_ENSURE_SUCCESS(res, res);
nsresult rv = htmlRules->MakeSureElemStartsOrEndsOnCR(aElement);
NS_ENSURE_SUCCESS(rv, rv);
rv = RemoveContainer(element);
NS_ENSURE_SUCCESS(rv, rv);
}
}
return NS_OK;
@@ -625,16 +626,16 @@ HTMLEditor::CheckPositionedElementBGandFG(nsIDOMElement* aElement,
aReturn.Truncate();
nsAutoString bgImageStr;
nsresult res =
nsresult rv =
mCSSEditUtils->GetComputedProperty(*element, *nsGkAtoms::background_image,
bgImageStr);
NS_ENSURE_SUCCESS(res, res);
NS_ENSURE_SUCCESS(rv, rv);
if (bgImageStr.EqualsLiteral("none")) {
nsAutoString bgColorStr;
res =
rv =
mCSSEditUtils->GetComputedProperty(*element, *nsGkAtoms::backgroundColor,
bgColorStr);
NS_ENSURE_SUCCESS(res, res);
NS_ENSURE_SUCCESS(rv, rv);
if (bgColorStr.EqualsLiteral("transparent")) {
RefPtr<nsComputedDOMStyle> cssDecl =
mCSSEditUtils->GetComputedStyle(element);