diff --git a/editor/libeditor/CSSEditUtils.cpp b/editor/libeditor/CSSEditUtils.cpp index f85eff07c96b..0171e428ed76 100644 --- a/editor/libeditor/CSSEditUtils.cpp +++ b/editor/libeditor/CSSEditUtils.cpp @@ -571,7 +571,8 @@ already_AddRefed CSSEditUtils::GetComputedStyle( // remove the CSS style "aProperty : aPropertyValue" and possibly remove the // whole node if it is a span and if its only attribute is _moz_dirty -nsresult CSSEditUtils::RemoveCSSInlineStyleWithTransaction( +Result +CSSEditUtils::RemoveCSSInlineStyleWithTransaction( nsStyledElement& aStyledElement, nsAtom* aProperty, const nsAString& aPropertyValue) { // remove the property from the style attribute @@ -579,30 +580,20 @@ nsresult CSSEditUtils::RemoveCSSInlineStyleWithTransaction( aPropertyValue); if (NS_FAILED(rv)) { NS_WARNING("CSSEditUtils::RemoveCSSPropertyWithTransaction() failed"); - return rv; + return Err(rv); } if (!aStyledElement.IsHTMLElement(nsGkAtoms::span) || HTMLEditor::HasAttributes(&aStyledElement)) { - return NS_OK; + return EditorDOMPoint(); } OwningNonNull htmlEditor(*mHTMLEditor); - const Result unwrapStyledElementResult = + Result unwrapStyledElementResult = htmlEditor->RemoveContainerWithTransaction(aStyledElement); - if (MOZ_UNLIKELY(unwrapStyledElementResult.isErr())) { - NS_WARNING("HTMLEditor::RemoveContainerWithTransaction() failed"); - return unwrapStyledElementResult.inspectErr(); - } - const EditorDOMPoint& pointToPutCaret = unwrapStyledElementResult.inspect(); - if (!htmlEditor->AllowsTransactionsToChangeSelection() || - !pointToPutCaret.IsSet()) { - return NS_OK; - } - rv = htmlEditor->CollapseSelectionTo(pointToPutCaret); - NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), - "EditorBase::CollapseSelectionTo() failed"); - return rv; + NS_WARNING_ASSERTION(unwrapStyledElementResult.isOk(), + "HTMLEditor::RemoveContainerWithTransaction() failed"); + return unwrapStyledElementResult; } // Answers true if the property can be removed by setting a "none" CSS value diff --git a/editor/libeditor/CSSEditUtils.h b/editor/libeditor/CSSEditUtils.h index 58ebb5cea563..5581b9c57397 100644 --- a/editor/libeditor/CSSEditUtils.h +++ b/editor/libeditor/CSSEditUtils.h @@ -134,10 +134,12 @@ class CSSEditUtils final { * @param aProperty [IN] The CSS property atom to remove. * @param aPropertyValue [IN] The value of the property we have to remove * if the property accepts more than one value. + * @return A candidate point to put caret. */ - [[nodiscard]] MOZ_CAN_RUN_SCRIPT nsresult RemoveCSSInlineStyleWithTransaction( - nsStyledElement& aStyledElement, nsAtom* aProperty, - const nsAString& aPropertyValue); + [[nodiscard]] MOZ_CAN_RUN_SCRIPT Result + RemoveCSSInlineStyleWithTransaction(nsStyledElement& aStyledElement, + nsAtom* aProperty, + const nsAString& aPropertyValue); /** * Answers true is the property can be removed by setting a "none" CSS value diff --git a/editor/libeditor/HTMLEditSubActionHandler.cpp b/editor/libeditor/HTMLEditSubActionHandler.cpp index 261c85118a75..a9aa60feb522 100644 --- a/editor/libeditor/HTMLEditSubActionHandler.cpp +++ b/editor/libeditor/HTMLEditSubActionHandler.cpp @@ -9720,14 +9720,23 @@ nsresult HTMLEditor::RemoveAlignFromDescendants(Element& aElement, // MOZ_KnownLive(*styledBlockOrHRElement): It's `blockOrHRElement // which is OwningNonNull. nsAutoString dummyCssValue; - nsresult rv = mCSSEditUtils->RemoveCSSInlineStyleWithTransaction( - MOZ_KnownLive(*styledBlockOrHRElement), nsGkAtoms::textAlign, - dummyCssValue); - if (NS_FAILED(rv)) { + Result pointToPutCaretOrError = + mCSSEditUtils->RemoveCSSInlineStyleWithTransaction( + MOZ_KnownLive(*styledBlockOrHRElement), nsGkAtoms::textAlign, + dummyCssValue); + if (MOZ_UNLIKELY(pointToPutCaretOrError.isErr())) { NS_WARNING( "CSSEditUtils::RemoveCSSInlineStyleWithTransaction(nsGkAtoms::" "textAlign) failed"); - return rv; + return pointToPutCaretOrError.unwrapErr(); + } + if (AllowsTransactionsToChangeSelection() && + pointToPutCaretOrError.inspect().IsSet()) { + nsresult rv = CollapseSelectionTo(pointToPutCaretOrError.inspect()); + if (NS_FAILED(rv)) { + NS_WARNING("EditorBase::CollapseSelectionTo() failed"); + return rv; + } } } }