Bug 1482013 - Create HTMLEditor::SetInlinePropertyInternal() for internal use r=m_kato
authorMasayuki Nakano <masayuki@d-toybox.com>
Mon, 13 Aug 2018 05:16:10 +0000
changeset 486270 50e4f06ac01a1f6dc187ef6ea23226e551f6f2a0
parent 486269 a415ada9fdae85a9162a898928a8d7e854bbd338
child 486271 179fc264c8643d1f8770ef2b2e861abf56a5287e
push id9719
push userffxbld-merge
push dateFri, 24 Aug 2018 17:49:46 +0000
treeherdermozilla-beta@719ec98fba77 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersm_kato
bugs1482013
milestone63.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 1482013 - Create HTMLEditor::SetInlinePropertyInternal() for internal use r=m_kato For making it possible to distinguish if SetInlineProperty() is called by outer class or the editor itself, this patch creates SetInlinePropertyInternal(). Additionally, this makes the first argument of SetInlineProperty() from nsAtom* to nsAtom& since it's not nullable. Differential Revision: https://phabricator.services.mozilla.com/D2999
editor/libeditor/HTMLEditor.cpp
editor/libeditor/HTMLEditor.h
editor/libeditor/HTMLEditorCommands.cpp
editor/libeditor/HTMLStyleEditor.cpp
--- a/editor/libeditor/HTMLEditor.cpp
+++ b/editor/libeditor/HTMLEditor.cpp
@@ -2790,18 +2790,20 @@ HTMLEditor::InsertLinkAroundSelection(El
       // We must clear the string buffers
       //   because GetValue appends to previous string!
       value.Truncate();
 
       nsAtom* name = attribute->NodeInfo()->NameAtom();
 
       attribute->GetValue(value);
 
-      rv = SetInlineProperty(nsGkAtoms::a, name, value);
-      NS_ENSURE_SUCCESS(rv, rv);
+      rv = SetInlinePropertyInternal(*nsGkAtoms::a, name, value);
+      if (NS_WARN_IF(NS_FAILED(rv))) {
+        return rv;
+      }
     }
   }
   return NS_OK;
 }
 
 nsresult
 HTMLEditor::SetHTMLBackgroundColorWithTransaction(const nsAString& aColor)
 {
--- a/editor/libeditor/HTMLEditor.h
+++ b/editor/libeditor/HTMLEditor.h
@@ -237,19 +237,27 @@ public:
   int32_t GetZIndex(Element& aElement);
 
   /**
    * adds aChange to the z-index of the currently positioned element.
    * @param aChange [IN] relative change to apply to current z-index
    */
   nsresult AddZIndex(int32_t aChange);
 
-  nsresult SetInlineProperty(nsAtom* aProperty,
+  nsresult SetInlineProperty(nsAtom& aProperty,
                              nsAtom* aAttribute,
-                             const nsAString& aValue);
+                             const nsAString& aValue)
+  {
+    nsresult rv = SetInlinePropertyInternal(aProperty, aAttribute, aValue);
+    if (NS_WARN_IF(NS_FAILED(rv))) {
+      return rv;
+    }
+    return NS_OK;
+  }
+
   nsresult GetInlineProperty(nsAtom* aProperty,
                              nsAtom* aAttribute,
                              const nsAString& aValue,
                              bool* aFirst,
                              bool* aAny,
                              bool* aAll);
   nsresult GetInlinePropertyWithAttrValue(nsAtom* aProperty,
                                           nsAtom* aAttr,
@@ -804,16 +812,20 @@ protected: // Shouldn't be used by frien
    * last chunk.  Then, each chunk starting with ">" is inserted after wrapping
    * with <span _moz_quote="true">, and each chunk not starting with ">" is
    * inserted as normal text.
    */
   nsresult InsertTextWithQuotationsInternal(const nsAString& aStringToInsert);
 
   nsresult LoadHTML(const nsAString& aInputString);
 
+  nsresult SetInlinePropertyInternal(nsAtom& aProperty,
+                                     nsAtom* aAttribute,
+                                     const nsAString& aValue);
+
   /**
    * ReplaceHeadContentsWithSourceWithTransaction() replaces all children of
    * <head> element with given source code.  This is undoable.
    *
    * @param aSourceToInsert     HTML source fragment to replace the children
    *                            of <head> element.
    */
   nsresult
--- a/editor/libeditor/HTMLEditorCommands.cpp
+++ b/editor/libeditor/HTMLEditorCommands.cpp
@@ -281,17 +281,17 @@ StyleUpdatingCommand::ToggleState(HTMLEd
   if (mTagName == nsGkAtoms::sub || mTagName == nsGkAtoms::sup) {
     nsresult rv = aHTMLEditor->RemoveInlineProperty(mTagName, nullptr);
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
   }
 
   nsresult rv =
-    aHTMLEditor->SetInlineProperty(mTagName, nullptr, EmptyString());
+    aHTMLEditor->SetInlineProperty(*mTagName, nullptr, EmptyString());
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
 
   return rv;
 }
 
 ListCommand::ListCommand(nsAtom* aTagName)
@@ -733,33 +733,40 @@ FontFaceStateCommand::SetState(HTMLEdito
                                const nsString& newState)
 {
   if (NS_WARN_IF(!aHTMLEditor)) {
     return NS_ERROR_INVALID_ARG;
   }
 
   if (newState.EqualsLiteral("tt")) {
     // The old "teletype" attribute
-    nsresult rv = aHTMLEditor->SetInlineProperty(nsGkAtoms::tt, nullptr,
+    nsresult rv = aHTMLEditor->SetInlineProperty(*nsGkAtoms::tt, nullptr,
                                                  EmptyString());
-    NS_ENSURE_SUCCESS(rv, rv);
+    if (NS_WARN_IF(NS_FAILED(rv))) {
+      return rv;
+    }
     // Clear existing font face
     return aHTMLEditor->RemoveInlineProperty(nsGkAtoms::font, nsGkAtoms::face);
   }
 
   // Remove any existing TT nodes
   nsresult rv = aHTMLEditor->RemoveInlineProperty(nsGkAtoms::tt, nullptr);
   NS_ENSURE_SUCCESS(rv, rv);
 
   if (newState.IsEmpty() || newState.EqualsLiteral("normal")) {
     return aHTMLEditor->RemoveInlineProperty(nsGkAtoms::font, nsGkAtoms::face);
   }
 
-  return aHTMLEditor->SetInlineProperty(nsGkAtoms::font, nsGkAtoms::face,
-                                        newState);
+  rv = aHTMLEditor->SetInlineProperty(*nsGkAtoms::font,
+                                      nsGkAtoms::face,
+                                      newState);
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    return rv;
+  }
+  return NS_OK;
 }
 
 FontSizeStateCommand::FontSizeStateCommand()
   : MultiStateCommandBase()
 {
 }
 
 nsresult
@@ -806,18 +813,23 @@ FontSizeStateCommand::SetState(HTMLEdito
 {
   if (NS_WARN_IF(!aHTMLEditor)) {
     return NS_ERROR_INVALID_ARG;
   }
 
   if (!newState.IsEmpty() &&
       !newState.EqualsLiteral("normal") &&
       !newState.EqualsLiteral("medium")) {
-    return aHTMLEditor->SetInlineProperty(nsGkAtoms::font,
-                                          nsGkAtoms::size, newState);
+    nsresult rv = aHTMLEditor->SetInlineProperty(*nsGkAtoms::font,
+                                                 nsGkAtoms::size,
+                                                 newState);
+    if (NS_WARN_IF(NS_FAILED(rv))) {
+      return rv;
+    }
+    return NS_OK;
   }
 
   // remove any existing font size, big or small
   nsresult rv = aHTMLEditor->RemoveInlineProperty(nsGkAtoms::font,
                                                   nsGkAtoms::size);
   NS_ENSURE_SUCCESS(rv, rv);
 
   rv = aHTMLEditor->RemoveInlineProperty(nsGkAtoms::big, nullptr);
@@ -860,18 +872,23 @@ FontColorStateCommand::SetState(HTMLEdit
     return NS_ERROR_INVALID_ARG;
   }
 
   if (newState.IsEmpty() || newState.EqualsLiteral("normal")) {
     return aHTMLEditor->RemoveInlineProperty(nsGkAtoms::font,
                                              nsGkAtoms::color);
   }
 
-  return aHTMLEditor->SetInlineProperty(nsGkAtoms::font, nsGkAtoms::color,
-                                        newState);
+  nsresult rv = aHTMLEditor->SetInlineProperty(*nsGkAtoms::font,
+                                               nsGkAtoms::color,
+                                               newState);
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    return rv;
+  }
+  return NS_OK;
 }
 
 HighlightColorStateCommand::HighlightColorStateCommand()
   : MultiStateCommandBase()
 {
 }
 
 nsresult
@@ -903,19 +920,23 @@ HighlightColorStateCommand::SetState(HTM
     return NS_ERROR_INVALID_ARG;
   }
 
   if (newState.IsEmpty() || newState.EqualsLiteral("normal")) {
     return aHTMLEditor->RemoveInlineProperty(nsGkAtoms::font,
                                              nsGkAtoms::bgcolor);
   }
 
-  return aHTMLEditor->SetInlineProperty(nsGkAtoms::font,
-                                        nsGkAtoms::bgcolor,
-                                        newState);
+  nsresult rv = aHTMLEditor->SetInlineProperty(*nsGkAtoms::font,
+                                               nsGkAtoms::bgcolor,
+                                               newState);
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    return rv;
+  }
+  return NS_OK;
 }
 
 NS_IMETHODIMP
 HighlightColorStateCommand::IsCommandEnabled(const char* aCommandName,
                                              nsISupports* refCon,
                                              bool* outCmdEnabled)
 {
   nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
--- a/editor/libeditor/HTMLStyleEditor.cpp
+++ b/editor/libeditor/HTMLStyleEditor.cpp
@@ -55,37 +55,44 @@ HTMLEditor::IsEmptyTextNode(nsINode& aNo
 }
 
 NS_IMETHODIMP
 HTMLEditor::SetInlineProperty(const nsAString& aProperty,
                               const nsAString& aAttribute,
                               const nsAString& aValue)
 {
   RefPtr<nsAtom> property = NS_Atomize(aProperty);
+  if (NS_WARN_IF(!property)) {
+    return NS_ERROR_INVALID_ARG;
+  }
   RefPtr<nsAtom> attribute = NS_Atomize(aAttribute);
-  return SetInlineProperty(property, attribute, aValue);
+  return SetInlinePropertyInternal(*property, attribute, aValue);
 }
 
 nsresult
-HTMLEditor::SetInlineProperty(nsAtom* aProperty,
-                              nsAtom* aAttribute,
-                              const nsAString& aValue)
+HTMLEditor::SetInlinePropertyInternal(nsAtom& aProperty,
+                                      nsAtom* aAttribute,
+                                      const nsAString& aValue)
 {
-  NS_ENSURE_TRUE(aProperty, NS_ERROR_NULL_POINTER);
-  NS_ENSURE_TRUE(mRules, NS_ERROR_NOT_INITIALIZED);
+  if (NS_WARN_IF(!mRules)) {
+    return NS_ERROR_NOT_INITIALIZED;
+  }
+
   RefPtr<TextEditRules> rules(mRules);
   CommitComposition();
 
   RefPtr<Selection> selection = GetSelection();
-  NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
+  if (NS_WARN_IF(!selection)) {
+    return NS_ERROR_FAILURE;
+  }
 
   if (selection->IsCollapsed()) {
     // Manipulating text attributes on a collapsed selection only sets state
     // for the next text insertion
-    mTypeInState->SetProp(aProperty, aAttribute, aValue);
+    mTypeInState->SetProp(&aProperty, aAttribute, aValue);
     return NS_OK;
   }
 
   AutoPlaceholderBatch batchIt(this);
   AutoTopLevelEditSubActionNotifier maybeTopLevelEditSubAction(
                                       *this, EditSubAction::eInsertElement,
                                       nsIEditor::eNext);
   AutoSelectionRestorer selectionRestorer(selection, this);
@@ -101,27 +108,31 @@ HTMLEditor::SetInlineProperty(nsAtom* aP
   }
   if (!cancel && !handled) {
     // Loop through the ranges in the selection
     AutoRangeArray arrayOfRanges(selection);
     for (auto& range : arrayOfRanges.mRanges) {
       // Adjust range to include any ancestors whose children are entirely
       // selected
       rv = PromoteInlineRange(*range);
-      NS_ENSURE_SUCCESS(rv, rv);
+      if (NS_WARN_IF(NS_FAILED(rv))) {
+        return rv;
+      }
 
       // Check for easy case: both range endpoints in same text node
       nsCOMPtr<nsINode> startNode = range->GetStartContainer();
       nsCOMPtr<nsINode> endNode = range->GetEndContainer();
       if (startNode && startNode == endNode && startNode->GetAsText()) {
         rv = SetInlinePropertyOnTextNode(*startNode->GetAsText(),
                                          range->StartOffset(),
                                          range->EndOffset(),
-                                         *aProperty, aAttribute, aValue);
-        NS_ENSURE_SUCCESS(rv, rv);
+                                         aProperty, aAttribute, aValue);
+        if (NS_WARN_IF(NS_FAILED(rv))) {
+          return rv;
+        }
         continue;
       }
 
       // Not the easy case.  Range not contained in single text node.  There
       // are up to three phases here.  There are all the nodes reported by the
       // subtree iterator to be processed.  And there are potentially a
       // starting textnode and an ending textnode which are only partially
       // contained by the range.
@@ -150,41 +161,51 @@ HTMLEditor::SetInlineProperty(nsAtom* aP
         }
       }
       // First check the start parent of the range to see if it needs to be
       // separately handled (it does if it's a text node, due to how the
       // subtree iterator works - it will not have reported it).
       if (startNode && startNode->GetAsText() && IsEditable(startNode)) {
         rv = SetInlinePropertyOnTextNode(*startNode->GetAsText(),
                                          range->StartOffset(),
-                                         startNode->Length(), *aProperty,
+                                         startNode->Length(), aProperty,
                                          aAttribute, aValue);
-        NS_ENSURE_SUCCESS(rv, rv);
+        if (NS_WARN_IF(NS_FAILED(rv))) {
+          return rv;
+        }
       }
 
       // Then loop through the list, set the property on each node
       for (auto& node : arrayOfNodes) {
-        rv = SetInlinePropertyOnNode(*node, *aProperty, aAttribute, aValue);
-        NS_ENSURE_SUCCESS(rv, rv);
+        rv = SetInlinePropertyOnNode(*node, aProperty, aAttribute, aValue);
+        if (NS_WARN_IF(NS_FAILED(rv))) {
+          return rv;
+        }
       }
 
       // Last check the end parent of the range to see if it needs to be
       // separately handled (it does if it's a text node, due to how the
       // subtree iterator works - it will not have reported it).
       if (endNode && endNode->GetAsText() && IsEditable(endNode)) {
         rv = SetInlinePropertyOnTextNode(*endNode->GetAsText(), 0,
-                                          range->EndOffset(), *aProperty,
+                                          range->EndOffset(), aProperty,
                                           aAttribute, aValue);
-        NS_ENSURE_SUCCESS(rv, rv);
+        if (NS_WARN_IF(NS_FAILED(rv))) {
+          return rv;
+        }
       }
     }
   }
-  if (!cancel) {
-    // Post-process
-    return rules->DidDoAction(selection, subActionInfo, rv);
+  if (cancel) {
+    return NS_OK;
+  }
+
+  rv = rules->DidDoAction(selection, subActionInfo, rv);
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    return rv;
   }
   return NS_OK;
 }
 
 // Helper function for SetInlinePropertyOn*: is aNode a simple old <b>, <font>,
 // <span style="">, etc. that we can reuse instead of creating a new one?
 bool
 HTMLEditor::IsSimpleModifiableNode(nsIContent* aContent,