Bug 1660378 - part 1: Get rid of `CSSEditUtils::GetInlineStyles()` r=m_kato
authorMasayuki Nakano <masayuki@d-toybox.com>
Wed, 26 Aug 2020 04:48:12 +0000
changeset 546249 7ee8df5f9eb9432e7a3db3b31d64995c30281108
parent 546248 a48af1a966b77975d525f615520b7e1e164ac1c1
child 546250 ee82ab27ad02e47fac66d0e558ef140f8236e226
push id124997
push usermasayuki@d-toybox.com
push dateWed, 26 Aug 2020 06:09:10 +0000
treeherderautoland@d1a42f86dd61 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersm_kato
bugs1660378
milestone82.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1660378 - part 1: Get rid of `CSSEditUtils::GetInlineStyles()` r=m_kato It should take `nsStyledElement&` instead of `const Element&`. Then, it won't fail and will just return the result of `nsStyleElement::Style()`. Therefore, we can get rid of it. Note that this patch makes all its callers keep using strong pointer because I'm not sure whether the layout APIs which are called with them are safe or not. Differential Revision: https://phabricator.services.mozilla.com/D87982
editor/libeditor/CSSEditUtils.cpp
editor/libeditor/CSSEditUtils.h
--- a/editor/libeditor/CSSEditUtils.cpp
+++ b/editor/libeditor/CSSEditUtils.cpp
@@ -1192,31 +1192,41 @@ bool CSSEditUtils::DoElementsHaveSameSty
       // need to discuss this issue before any modification.
       return false;
     }
   } else if (isElementClassSet || isOtherElementClassSet) {
     // one span only carries a class, early way out
     return false;
   }
 
-  nsCOMPtr<nsICSSDeclaration> firstCSSDecl, otherCSSDecl;
-  uint32_t firstLength, otherLength;
-  nsresult rv =
-      GetInlineStyles(aElement, getter_AddRefs(firstCSSDecl), &firstLength);
-  if (NS_FAILED(rv) || !firstCSSDecl) {
-    NS_WARNING("CSSEditUtils::GetInlineStyles() failed");
+  nsCOMPtr<nsStyledElement> styledElement =
+      do_QueryInterface(const_cast<Element*>(&aElement));
+  if (NS_WARN_IF(!styledElement)) {
     return false;
   }
-  rv = GetInlineStyles(aOtherElement, getter_AddRefs(otherCSSDecl),
-                       &otherLength);
-  if (NS_FAILED(rv) || !otherCSSDecl) {
-    NS_WARNING("CSSEditUtils::GetInlineStyles() failed");
+  // XXX If `GetPropertyValue()` won't run script, we can stop using
+  //     nsCOMPtr here.
+  nsCOMPtr<nsICSSDeclaration> firstCSSDecl = styledElement->Style();
+  if (!firstCSSDecl) {
+    NS_WARNING("nsStyledElement::Style() failed");
+    return false;
+  }
+  nsCOMPtr<nsStyledElement> otherStyledElement =
+      do_QueryInterface(const_cast<Element*>(&aOtherElement));
+  if (NS_WARN_IF(!otherStyledElement)) {
+    return false;
+  }
+  nsCOMPtr<nsICSSDeclaration> otherCSSDecl = otherStyledElement->Style();
+  if (!otherCSSDecl) {
+    NS_WARNING("nsStyledElement::Style() failed");
     return false;
   }
 
+  const uint32_t firstLength = firstCSSDecl->Length();
+  const uint32_t otherLength = otherCSSDecl->Length();
   if (firstLength != otherLength) {
     // early way out if we can
     return false;
   }
 
   if (!firstLength) {
     // no inline style !
     return true;
@@ -1253,32 +1263,9 @@ bool CSSEditUtils::DoElementsHaveSameSty
     if (!firstValue.Equals(otherValue)) {
       return false;
     }
   }
 
   return true;
 }
 
-// static
-nsresult CSSEditUtils::GetInlineStyles(const Element& aElement,
-                                       nsICSSDeclaration** aCssDecl,
-                                       uint32_t* aLength) {
-  if (NS_WARN_IF(!aLength)) {
-    return NS_ERROR_INVALID_ARG;
-  }
-  *aLength = 0;
-  // TODO: Perhaps, this method should take nsStyledElement& instead.
-  nsCOMPtr<nsStyledElement> styledElement =
-      do_QueryInterface(const_cast<Element*>(&aElement));
-  if (NS_WARN_IF(!styledElement)) {
-    return NS_ERROR_INVALID_ARG;
-  }
-
-  nsCOMPtr<nsICSSDeclaration> cssDecl = styledElement->Style();
-  MOZ_ASSERT(cssDecl);
-
-  cssDecl.forget(aCssDecl);
-  *aLength = (*aCssDecl)->Length();
-  return NS_OK;
-}
-
 }  // namespace mozilla
--- a/editor/libeditor/CSSEditUtils.h
+++ b/editor/libeditor/CSSEditUtils.h
@@ -13,16 +13,17 @@
 #include "nscore.h"    // for nsAString, nsresult, nullptr
 
 class nsComputedDOMStyle;
 class nsAtom;
 class nsIContent;
 class nsICSSDeclaration;
 class nsINode;
 class nsStaticAtom;
+class nsStyledElement;
 
 namespace mozilla {
 
 class HTMLEditor;
 namespace dom {
 class Element;
 }  // namespace dom
 
@@ -306,28 +307,16 @@ class CSSEditUtils final {
    * @param aElement             [IN] A DOM node.
    * @param aOtherElement        [IN] A DOM node.
    * @return                     true if the two elements are considered to
    *                             have same styles.
    */
   static bool DoElementsHaveSameStyle(const dom::Element& aElement,
                                       const dom::Element& aOtherElement);
 
-  /**
-   * Get the specified inline styles (style attribute) for an element.
-   *
-   * @param aElement        [IN] The element node.
-   * @param aCssDecl        [OUT] The CSS declaration corresponding to the
-   *                              style attribute.
-   * @param aLength         [OUT] The number of declarations in aCssDecl.
-   */
-  static nsresult GetInlineStyles(const dom::Element& aElement,
-                                  nsICSSDeclaration** aCssDecl,
-                                  uint32_t* aLength);
-
  public:
   /**
    * Gets the computed style for a given element.  Can return null.
    */
   static already_AddRefed<nsComputedDOMStyle> GetComputedStyle(
       dom::Element* aElement);
 
  private: