Bug 1319340 - part4: SetState() of nsComposerCommands should take HTMLEditor* instead of nsIEditor* r?m_kato draft
authorMasayuki Nakano <masayuki@d-toybox.com>
Fri, 04 Aug 2017 16:20:56 +0900
changeset 643893 2299c7daf3bda0f79de8856b81842b310160a8a3
parent 643892 613f4cf50f7ffce8740b4d37ff764e96970519c9
child 643894 8c2ff36f2db87f8010d1471dda4a3a44c58ca1e1
push id73245
push usermasayuki@d-toybox.com
push dateThu, 10 Aug 2017 07:32:18 +0000
reviewersm_kato
bugs1319340
milestone57.0a1
Bug 1319340 - part4: SetState() of nsComposerCommands should take HTMLEditor* instead of nsIEditor* r?m_kato All SetState() methods in nsComposerCommands require HTMLEditor. So, it should take HTMLEditor* rather than nsIEditor*. MozReview-Commit-ID: AVbnRsMsmeY
editor/composer/nsComposerCommands.cpp
editor/composer/nsComposerCommands.h
--- a/editor/composer/nsComposerCommands.cpp
+++ b/editor/composer/nsComposerCommands.cpp
@@ -588,34 +588,34 @@ nsMultiStateCommand::DoCommand(const cha
 }
 
 NS_IMETHODIMP
 nsMultiStateCommand::DoCommandParams(const char *aCommandName,
                                      nsICommandParams *aParams,
                                      nsISupports *refCon)
 {
   nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
-
-  nsresult rv = NS_OK;
-  if (editor) {
-      nsAutoString tString;
-
-      if (aParams) {
-        nsXPIDLCString s;
-        rv = aParams->GetCStringValue(STATE_ATTRIBUTE, getter_Copies(s));
-        if (NS_SUCCEEDED(rv))
-          CopyASCIItoUTF16(s, tString);
-        else
-          rv = aParams->GetStringValue(STATE_ATTRIBUTE, tString);
-      }
-
-      rv = SetState(editor, tString);
+  if (!editor) {
+    return NS_OK;
+  }
+  mozilla::HTMLEditor* htmlEditor = editor->AsHTMLEditor();
+  if (NS_WARN_IF(!htmlEditor)) {
+    return NS_ERROR_INVALID_ARG;
   }
 
-  return rv;
+  nsAutoString tString;
+  if (aParams) {
+    nsXPIDLCString s;
+    nsresult rv = aParams->GetCStringValue(STATE_ATTRIBUTE, getter_Copies(s));
+    if (NS_SUCCEEDED(rv))
+      CopyASCIItoUTF16(s, tString);
+    else
+      aParams->GetStringValue(STATE_ATTRIBUTE, tString);
+  }
+  return SetState(htmlEditor, tString);
 }
 
 NS_IMETHODIMP
 nsMultiStateCommand::GetCommandStateParams(const char *aCommandName,
                                            nsICommandParams *aParams,
                                            nsISupports *refCon)
 {
   nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
@@ -649,25 +649,24 @@ nsParagraphStateCommand::GetCurrentState
     nsAutoCString tOutStateString;
     LossyCopyUTF16toASCII(outStateString, tOutStateString);
     aParams->SetBooleanValue(STATE_MIXED,outMixed);
     aParams->SetCStringValue(STATE_ATTRIBUTE, tOutStateString.get());
   }
   return rv;
 }
 
-
 nsresult
-nsParagraphStateCommand::SetState(nsIEditor *aEditor, nsString& newState)
+nsParagraphStateCommand::SetState(mozilla::HTMLEditor* aHTMLEditor,
+                                  nsString& newState)
 {
-  NS_ASSERTION(aEditor, "Need an editor here");
-  nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(aEditor);
-  NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE);
-
-  return htmlEditor->SetParagraphFormat(newState);
+  if (NS_WARN_IF(!aHTMLEditor)) {
+    return NS_ERROR_INVALID_ARG;
+  }
+  return aHTMLEditor->SetParagraphFormat(newState);
 }
 
 nsFontFaceStateCommand::nsFontFaceStateCommand()
 : nsMultiStateCommand()
 {
 }
 
 nsresult
@@ -684,43 +683,44 @@ nsFontFaceStateCommand::GetCurrentState(
   if (NS_SUCCEEDED(rv)) {
     aParams->SetBooleanValue(STATE_MIXED,outMixed);
     aParams->SetCStringValue(STATE_ATTRIBUTE, NS_ConvertUTF16toUTF8(outStateString).get());
   }
   return rv;
 }
 
 nsresult
-nsFontFaceStateCommand::SetState(nsIEditor *aEditor, nsString& newState)
+nsFontFaceStateCommand::SetState(mozilla::HTMLEditor* aHTMLEditor,
+                                 nsString& newState)
 {
-  NS_ASSERTION(aEditor, "Need an editor here");
-  nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(aEditor);
-  NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE);
+  if (NS_WARN_IF(!aHTMLEditor)) {
+    return NS_ERROR_INVALID_ARG;
+  }
 
   if (newState.EqualsLiteral("tt")) {
     // The old "teletype" attribute
-    nsresult rv = htmlEditor->SetInlineProperty(nsGkAtoms::tt, EmptyString(),
-                                                EmptyString());
+    nsresult rv = aHTMLEditor->SetInlineProperty(nsGkAtoms::tt, EmptyString(),
+                                                 EmptyString());
     NS_ENSURE_SUCCESS(rv, rv);
     // Clear existing font face
-    return htmlEditor->RemoveInlineProperty(nsGkAtoms::font,
-                                            NS_LITERAL_STRING("face"));
+    return aHTMLEditor->RemoveInlineProperty(nsGkAtoms::font,
+                                             NS_LITERAL_STRING("face"));
   }
 
   // Remove any existing TT nodes
-  nsresult rv = htmlEditor->RemoveInlineProperty(nsGkAtoms::tt, EmptyString());
+  nsresult rv = aHTMLEditor->RemoveInlineProperty(nsGkAtoms::tt, EmptyString());
   NS_ENSURE_SUCCESS(rv, rv);
 
   if (newState.IsEmpty() || newState.EqualsLiteral("normal")) {
-    return htmlEditor->RemoveInlineProperty(nsGkAtoms::font,
-                                            NS_LITERAL_STRING("face"));
+    return aHTMLEditor->RemoveInlineProperty(nsGkAtoms::font,
+                                             NS_LITERAL_STRING("face"));
   }
 
-  return htmlEditor->SetInlineProperty(nsGkAtoms::font,
-                                       NS_LITERAL_STRING("face"), newState);
+  return aHTMLEditor->SetInlineProperty(nsGkAtoms::font,
+                                        NS_LITERAL_STRING("face"), newState);
 }
 
 nsFontSizeStateCommand::nsFontSizeStateCommand()
   : nsMultiStateCommand()
 {
 }
 
 nsresult
@@ -757,38 +757,39 @@ nsFontSizeStateCommand::GetCurrentState(
 //   -1
 //    0
 //   +1
 //   +2
 //   +3
 //   medium
 //   normal
 nsresult
-nsFontSizeStateCommand::SetState(nsIEditor *aEditor, nsString& newState)
+nsFontSizeStateCommand::SetState(mozilla::HTMLEditor* aHTMLEditor,
+                                 nsString& newState)
 {
-  NS_ASSERTION(aEditor, "Need an editor here");
-  nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(aEditor);
-  NS_ENSURE_TRUE(htmlEditor, NS_ERROR_INVALID_ARG);
+  if (NS_WARN_IF(!aHTMLEditor)) {
+    return NS_ERROR_INVALID_ARG;
+  }
 
   if (!newState.IsEmpty() &&
       !newState.EqualsLiteral("normal") &&
       !newState.EqualsLiteral("medium")) {
-    return htmlEditor->SetInlineProperty(nsGkAtoms::font,
-                                         NS_LITERAL_STRING("size"), newState);
+    return aHTMLEditor->SetInlineProperty(nsGkAtoms::font,
+                                          NS_LITERAL_STRING("size"), newState);
   }
 
   // remove any existing font size, big or small
-  nsresult rv = htmlEditor->RemoveInlineProperty(nsGkAtoms::font,
-                                                 NS_LITERAL_STRING("size"));
+  nsresult rv = aHTMLEditor->RemoveInlineProperty(nsGkAtoms::font,
+                                                  NS_LITERAL_STRING("size"));
   NS_ENSURE_SUCCESS(rv, rv);
 
-  rv = htmlEditor->RemoveInlineProperty(nsGkAtoms::big, EmptyString());
+  rv = aHTMLEditor->RemoveInlineProperty(nsGkAtoms::big, EmptyString());
   NS_ENSURE_SUCCESS(rv, rv);
 
-  return htmlEditor->RemoveInlineProperty(nsGkAtoms::small, EmptyString());
+  return aHTMLEditor->RemoveInlineProperty(nsGkAtoms::small, EmptyString());
 }
 
 nsFontColorStateCommand::nsFontColorStateCommand()
 : nsMultiStateCommand()
 {
 }
 
 nsresult
@@ -807,29 +808,30 @@ nsFontColorStateCommand::GetCurrentState
   nsAutoCString tOutStateString;
   LossyCopyUTF16toASCII(outStateString, tOutStateString);
   aParams->SetBooleanValue(STATE_MIXED, outMixed);
   aParams->SetCStringValue(STATE_ATTRIBUTE, tOutStateString.get());
   return NS_OK;
 }
 
 nsresult
-nsFontColorStateCommand::SetState(nsIEditor *aEditor, nsString& newState)
+nsFontColorStateCommand::SetState(mozilla::HTMLEditor* aHTMLEditor,
+                                  nsString& newState)
 {
-  NS_ASSERTION(aEditor, "Need an editor here");
-  nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(aEditor);
-  NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE);
+  if (NS_WARN_IF(!aHTMLEditor)) {
+    return NS_ERROR_INVALID_ARG;
+  }
 
   if (newState.IsEmpty() || newState.EqualsLiteral("normal")) {
-    return htmlEditor->RemoveInlineProperty(nsGkAtoms::font,
-                                            NS_LITERAL_STRING("color"));
+    return aHTMLEditor->RemoveInlineProperty(nsGkAtoms::font,
+                                             NS_LITERAL_STRING("color"));
   }
 
-  return htmlEditor->SetInlineProperty(nsGkAtoms::font,
-                                       NS_LITERAL_STRING("color"), newState);
+  return aHTMLEditor->SetInlineProperty(nsGkAtoms::font,
+                                        NS_LITERAL_STRING("color"), newState);
 }
 
 nsHighlightColorStateCommand::nsHighlightColorStateCommand()
 : nsMultiStateCommand()
 {
 }
 
 nsresult
@@ -848,30 +850,31 @@ nsHighlightColorStateCommand::GetCurrent
   nsAutoCString tOutStateString;
   LossyCopyUTF16toASCII(outStateString, tOutStateString);
   aParams->SetBooleanValue(STATE_MIXED, outMixed);
   aParams->SetCStringValue(STATE_ATTRIBUTE, tOutStateString.get());
   return NS_OK;
 }
 
 nsresult
-nsHighlightColorStateCommand::SetState(nsIEditor *aEditor, nsString& newState)
+nsHighlightColorStateCommand::SetState(mozilla::HTMLEditor* aHTMLEditor,
+                                       nsString& newState)
 {
-  NS_ASSERTION(aEditor, "Need an editor here");
-  nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(aEditor);
-  NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE);
+  if (NS_WARN_IF(!aHTMLEditor)) {
+    return NS_ERROR_INVALID_ARG;
+  }
 
   if (newState.IsEmpty() || newState.EqualsLiteral("normal")) {
-    return htmlEditor->RemoveInlineProperty(nsGkAtoms::font,
-                                            NS_LITERAL_STRING("bgcolor"));
+    return aHTMLEditor->RemoveInlineProperty(nsGkAtoms::font,
+                                             NS_LITERAL_STRING("bgcolor"));
   }
 
-  return htmlEditor->SetInlineProperty(nsGkAtoms::font,
-                                       NS_LITERAL_STRING("bgcolor"),
-                                       newState);
+  return aHTMLEditor->SetInlineProperty(nsGkAtoms::font,
+                                        NS_LITERAL_STRING("bgcolor"),
+                                        newState);
 }
 
 NS_IMETHODIMP
 nsHighlightColorStateCommand::IsCommandEnabled(const char * aCommandName,
                                                nsISupports *refCon,
                                                bool *outCmdEnabled)
 {
   nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
@@ -904,24 +907,23 @@ nsBackgroundColorStateCommand::GetCurren
   nsAutoCString tOutStateString;
   LossyCopyUTF16toASCII(outStateString, tOutStateString);
   aParams->SetBooleanValue(STATE_MIXED, outMixed);
   aParams->SetCStringValue(STATE_ATTRIBUTE, tOutStateString.get());
   return NS_OK;
 }
 
 nsresult
-nsBackgroundColorStateCommand::SetState(nsIEditor *aEditor, nsString& newState)
+nsBackgroundColorStateCommand::SetState(mozilla::HTMLEditor* aHTMLEditor,
+                                        nsString& newState)
 {
-  NS_ASSERTION(aEditor, "Need an editor here");
-
-  nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(aEditor);
-  NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE);
-
-  return htmlEditor->SetBackgroundColor(newState);
+  if (NS_WARN_IF(!aHTMLEditor)) {
+    return NS_ERROR_INVALID_ARG;
+  }
+  return aHTMLEditor->SetBackgroundColor(newState);
 }
 
 nsAlignCommand::nsAlignCommand()
 : nsMultiStateCommand()
 {
 }
 
 nsresult
@@ -960,24 +962,23 @@ nsAlignCommand::GetCurrentState(mozilla:
   nsAutoCString tOutStateString;
   LossyCopyUTF16toASCII(outStateString, tOutStateString);
   aParams->SetBooleanValue(STATE_MIXED,outMixed);
   aParams->SetCStringValue(STATE_ATTRIBUTE, tOutStateString.get());
   return NS_OK;
 }
 
 nsresult
-nsAlignCommand::SetState(nsIEditor *aEditor, nsString& newState)
+nsAlignCommand::SetState(mozilla::HTMLEditor* aHTMLEditor,
+                         nsString& newState)
 {
-  NS_ASSERTION(aEditor, "Need an editor here");
-
-  nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(aEditor);
-  NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE);
-
-  return htmlEditor->Align(newState);
+  if (NS_WARN_IF(!aHTMLEditor)) {
+    return NS_ERROR_INVALID_ARG;
+  }
+  return aHTMLEditor->Align(newState);
 }
 
 nsAbsolutePositioningCommand::nsAbsolutePositioningCommand()
 : nsBaseStateUpdatingCommand(nsGkAtoms::_empty)
 {
 }
 
 NS_IMETHODIMP
--- a/editor/composer/nsComposerCommands.h
+++ b/editor/composer/nsComposerCommands.h
@@ -154,105 +154,113 @@ public:
   NS_DECL_ISUPPORTS_INHERITED
   NS_DECL_NSICONTROLLERCOMMAND
 
 protected:
   virtual ~nsMultiStateCommand();
 
   virtual nsresult GetCurrentState(mozilla::HTMLEditor* aHTMLEditor,
                                    nsICommandParams* aParams) = 0;
-  virtual nsresult SetState(nsIEditor *aEditor, nsString& newState) = 0;
+  virtual nsresult SetState(mozilla::HTMLEditor* aHTMLEditor,
+                            nsString& newState) = 0;
 
 };
 
 
 class nsParagraphStateCommand final : public nsMultiStateCommand
 {
 public:
                    nsParagraphStateCommand();
 
 protected:
 
   virtual nsresult GetCurrentState(mozilla::HTMLEditor* aHTMLEditor,
                                    nsICommandParams* aParams) override final;
-  virtual nsresult SetState(nsIEditor *aEditor, nsString& newState);
+  virtual nsresult SetState(mozilla::HTMLEditor* aHTMLEditor,
+                            nsString& newState) override final;
 };
 
 class nsFontFaceStateCommand final : public nsMultiStateCommand
 {
 public:
                    nsFontFaceStateCommand();
 
 protected:
 
   virtual nsresult GetCurrentState(mozilla::HTMLEditor* aHTMLEditor,
                                    nsICommandParams* aParams) override final;
-  virtual nsresult SetState(nsIEditor *aEditor, nsString& newState);
+  virtual nsresult SetState(mozilla::HTMLEditor* aHTMLEditor,
+                            nsString& newState) override final;
 };
 
 class nsFontSizeStateCommand final : public nsMultiStateCommand
 {
 public:
                    nsFontSizeStateCommand();
 
 protected:
 
   virtual nsresult GetCurrentState(mozilla::HTMLEditor* aHTMLEditor,
                                    nsICommandParams* aParams) override final;
-  virtual nsresult SetState(nsIEditor *aEditor, nsString& newState);
+  virtual nsresult SetState(mozilla::HTMLEditor* aHTMLEditor,
+                            nsString& newState) override final;
 };
 
 class nsHighlightColorStateCommand final : public nsMultiStateCommand
 {
 public:
                    nsHighlightColorStateCommand();
 
 protected:
 
   NS_IMETHOD IsCommandEnabled(const char *aCommandName, nsISupports *aCommandRefCon, bool *_retval);
   virtual nsresult GetCurrentState(mozilla::HTMLEditor* aHTMLEditor,
                                    nsICommandParams* aParams) override final;
-  virtual nsresult SetState(nsIEditor *aEditor, nsString& newState);
+  virtual nsresult SetState(mozilla::HTMLEditor* aHTMLEditor,
+                            nsString& newState) override final;
 
 };
 
 class nsFontColorStateCommand final : public nsMultiStateCommand
 {
 public:
                    nsFontColorStateCommand();
 
 protected:
 
   virtual nsresult GetCurrentState(mozilla::HTMLEditor* aHTMLEditor,
                                    nsICommandParams* aParams) override final;
-  virtual nsresult SetState(nsIEditor *aEditor, nsString& newState);
+  virtual nsresult SetState(mozilla::HTMLEditor* aHTMLEditor,
+                            nsString& newState) override final;
 };
 
 class nsAlignCommand final : public nsMultiStateCommand
 {
 public:
                    nsAlignCommand();
 
 protected:
 
   virtual nsresult GetCurrentState(mozilla::HTMLEditor* aHTMLEditor,
                                    nsICommandParams* aParams) override final;
-  virtual nsresult SetState(nsIEditor *aEditor, nsString& newState);
+  virtual nsresult SetState(mozilla::HTMLEditor* aHTMLEditor,
+                            nsString& newState) override final;
 };
 
 class nsBackgroundColorStateCommand final : public nsMultiStateCommand
 {
 public:
                    nsBackgroundColorStateCommand();
 
 protected:
 
   virtual nsresult GetCurrentState(mozilla::HTMLEditor* aHTMLEditor,
                                    nsICommandParams* aParams) override final;
-  virtual nsresult SetState(nsIEditor *aEditor, nsString& newState);
+  virtual nsresult SetState(mozilla::HTMLEditor* aHTMLEditor,
+                            nsString& newState) override final;
 };
 
 class nsAbsolutePositioningCommand final : public nsBaseStateUpdatingCommand
 {
 public:
   nsAbsolutePositioningCommand();
 
 protected: