diff --git a/editor/libeditor/html/nsHTMLCSSUtils.cpp b/editor/libeditor/html/nsHTMLCSSUtils.cpp index c044d4c731f5..4b1ce734b5a9 100644 --- a/editor/libeditor/html/nsHTMLCSSUtils.cpp +++ b/editor/libeditor/html/nsHTMLCSSUtils.cpp @@ -964,7 +964,22 @@ nsHTMLCSSUtils::RemoveCSSEquivalentToHTMLStyle(nsIDOMNode * aNode, bool aSuppressTransaction) { nsCOMPtr element = do_QueryInterface(aNode); - if (!element || !IsCSSEditableProperty(element, aHTMLProperty, aAttribute)) { + NS_ENSURE_TRUE(element, NS_OK); + + return RemoveCSSEquivalentToHTMLStyle(element, aHTMLProperty, aAttribute, + aValue, aSuppressTransaction); +} + +nsresult +nsHTMLCSSUtils::RemoveCSSEquivalentToHTMLStyle(dom::Element* aElement, + nsIAtom* aHTMLProperty, + const nsAString* aAttribute, + const nsAString* aValue, + bool aSuppressTransaction) +{ + MOZ_ASSERT(aElement); + + if (!IsCSSEditableProperty(aElement, aHTMLProperty, aAttribute)) { return NS_OK; } @@ -974,11 +989,11 @@ nsHTMLCSSUtils::RemoveCSSEquivalentToHTMLStyle(nsIDOMNode * aNode, // Find the CSS equivalence to the HTML style nsTArray cssPropertyArray; nsTArray cssValueArray; - GenerateCSSDeclarationsFromHTMLStyle(element, aHTMLProperty, aAttribute, + GenerateCSSDeclarationsFromHTMLStyle(aElement, aHTMLProperty, aAttribute, aValue, cssPropertyArray, cssValueArray, true); - nsCOMPtr domElement = do_QueryInterface(element); + nsCOMPtr domElement = do_QueryInterface(aElement); // remove the individual CSS inline styles int32_t count = cssPropertyArray.Length(); for (int32_t index = 0; index < count; index++) { diff --git a/editor/libeditor/html/nsHTMLCSSUtils.h b/editor/libeditor/html/nsHTMLCSSUtils.h index 7ce042c42606..7bc47a0e3266 100644 --- a/editor/libeditor/html/nsHTMLCSSUtils.h +++ b/editor/libeditor/html/nsHTMLCSSUtils.h @@ -239,6 +239,20 @@ public: const nsAString *aAttribute, const nsAString *aValue, bool aSuppressTransaction); + /** removes from the node the CSS inline styles equivalent to the HTML style + * + * @param aElement [IN] a DOM Element (must not be null) + * @param aHTMLProperty [IN] an atom containing an HTML property + * @param aAttribute [IN] a pointer to an attribute name or nullptr if irrelevant + * @param aValue [IN] the attribute value + * @param aSuppressTransaction [IN] a boolean indicating, when true, + * that no transaction should be recorded + */ + nsresult RemoveCSSEquivalentToHTMLStyle(mozilla::dom::Element* aElement, + nsIAtom* aHTMLProperty, + const nsAString* aAttribute, + const nsAString* aValue, + bool aSuppressTransaction); /** parses a "xxxx.xxxxxuuu" string where x is a digit and u an alpha char * we need such a parser because nsIDOMCSSStyleDeclaration::GetPropertyCSSValue() is not diff --git a/editor/libeditor/html/nsHTMLEditor.cpp b/editor/libeditor/html/nsHTMLEditor.cpp index dfcf318fa8ac..6ea58774d775 100644 --- a/editor/libeditor/html/nsHTMLEditor.cpp +++ b/editor/libeditor/html/nsHTMLEditor.cpp @@ -4579,26 +4579,30 @@ nsHTMLEditor::SetAttributeOrEquivalent(nsIDOMElement * aElement, } nsresult -nsHTMLEditor::RemoveAttributeOrEquivalent(nsIDOMElement * aElement, - const nsAString & aAttribute, +nsHTMLEditor::RemoveAttributeOrEquivalent(nsIDOMElement* aElement, + const nsAString& aAttribute, bool aSuppressTransaction) { + nsCOMPtr element = do_QueryInterface(aElement); + NS_ENSURE_TRUE(element, NS_OK); + + nsCOMPtr attribute = do_GetAtom(aAttribute); + MOZ_ASSERT(attribute); + nsresult res = NS_OK; if (IsCSSEnabled() && mHTMLCSSUtils) { - res = mHTMLCSSUtils->RemoveCSSEquivalentToHTMLStyle(aElement, nullptr, &aAttribute, nullptr, - aSuppressTransaction); + res = mHTMLCSSUtils->RemoveCSSEquivalentToHTMLStyle( + element, nullptr, &aAttribute, nullptr, aSuppressTransaction); NS_ENSURE_SUCCESS(res, res); } - nsAutoString existingValue; - bool wasSet = false; - res = GetAttributeValue(aElement, aAttribute, existingValue, &wasSet); - NS_ENSURE_SUCCESS(res, res); - if (wasSet) { - if (aSuppressTransaction) - res = aElement->RemoveAttribute(aAttribute); - else + if (element->HasAttr(kNameSpaceID_None, attribute)) { + if (aSuppressTransaction) { + res = element->UnsetAttr(kNameSpaceID_None, attribute, + /* aNotify = */ true); + } else { res = RemoveAttribute(aElement, aAttribute); + } } return res; }