Bug 1319340 - part3: ToggleState() in nsComposerCommands should take HTMLEditor* instead of nsIEditor* r?m_kato draft
authorMasayuki Nakano <masayuki@d-toybox.com>
Fri, 04 Aug 2017 15:57:57 +0900
changeset 643892 613f4cf50f7ffce8740b4d37ff764e96970519c9
parent 643891 8ff888208339e3eaefc6f90848e6697ffb4e58e5
child 643893 2299c7daf3bda0f79de8856b81842b310160a8a3
push id73245
push usermasayuki@d-toybox.com
push dateThu, 10 Aug 2017 07:32:18 +0000
reviewersm_kato
bugs1319340
milestone57.0a1
Bug 1319340 - part3: ToggleState() in nsComposerCommands should take HTMLEditor* instead of nsIEditor* r?m_kato Similar to GetCurrentState(), all ToggleState() methods require HTMLEditor. So, they should take HTMLEditor* instead of nsIEditor*. MozReview-Commit-ID: BwM6WRKFn6Q
editor/composer/nsComposerCommands.cpp
editor/composer/nsComposerCommands.h
--- a/editor/composer/nsComposerCommands.cpp
+++ b/editor/composer/nsComposerCommands.cpp
@@ -84,19 +84,24 @@ nsBaseStateUpdatingCommand::IsCommandEna
 }
 
 
 NS_IMETHODIMP
 nsBaseStateUpdatingCommand::DoCommand(const char *aCommandName,
                                       nsISupports *refCon)
 {
   nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
-  NS_ENSURE_TRUE(editor, NS_ERROR_NOT_INITIALIZED);
-
-  return ToggleState(editor);
+  if (NS_WARN_IF(!editor)) {
+    return NS_ERROR_INVALID_ARG;
+  }
+  mozilla::HTMLEditor* htmlEditor = editor->AsHTMLEditor();
+  if (NS_WARN_IF(!htmlEditor)) {
+    return NS_ERROR_INVALID_ARG;
+  }
+  return ToggleState(htmlEditor);
 }
 
 NS_IMETHODIMP
 nsBaseStateUpdatingCommand::DoCommandParams(const char *aCommandName,
                                             nsICommandParams *aParams,
                                             nsISupports *refCon)
 {
   return DoCommand(aCommandName, refCon);
@@ -200,23 +205,19 @@ nsStyleUpdatingCommand::GetCurrentState(
   aParams->SetBooleanValue(STATE_MIXED, anyOfSelectionHasProp
            && !allOfSelectionHasProp);
   aParams->SetBooleanValue(STATE_BEGIN, firstOfSelectionHasProp);
   aParams->SetBooleanValue(STATE_END, allOfSelectionHasProp);//not completely accurate
   return NS_OK;
 }
 
 nsresult
-nsStyleUpdatingCommand::ToggleState(nsIEditor *aEditor)
+nsStyleUpdatingCommand::ToggleState(mozilla::HTMLEditor* aHTMLEditor)
 {
-  if (NS_WARN_IF(!aEditor)) {
-    return NS_ERROR_INVALID_ARG;
-  }
-  mozilla::HTMLEditor* htmlEditor = aEditor->AsHTMLEditor();
-  if (NS_WARN_IF(!htmlEditor)) {
+  if (NS_WARN_IF(!aHTMLEditor)) {
     return NS_ERROR_INVALID_ARG;
   }
 
   //create some params now...
   nsresult rv;
   nsCOMPtr<nsICommandParams> params =
       do_CreateInstance(NS_COMMAND_PARAMS_CONTRACTID,&rv);
   if (NS_FAILED(rv) || !params)
@@ -224,48 +225,48 @@ nsStyleUpdatingCommand::ToggleState(nsIE
 
   // tags "href" and "name" are special cases in the core editor
   // they are used to remove named anchor/link and shouldn't be used for insertion
   bool doTagRemoval;
   if (mTagName == nsGkAtoms::href || mTagName == nsGkAtoms::name) {
     doTagRemoval = true;
   } else {
     // check current selection; set doTagRemoval if formatting should be removed
-    rv = GetCurrentState(htmlEditor, params);
+    rv = GetCurrentState(aHTMLEditor, params);
     NS_ENSURE_SUCCESS(rv, rv);
     rv = params->GetBooleanValue(STATE_ALL, &doTagRemoval);
     NS_ENSURE_SUCCESS(rv, rv);
   }
 
   if (doTagRemoval) {
     // Also remove equivalent properties (bug 317093)
     if (mTagName == nsGkAtoms::b) {
-      rv = RemoveTextProperty(htmlEditor, NS_LITERAL_STRING("strong"));
+      rv = RemoveTextProperty(aHTMLEditor, NS_LITERAL_STRING("strong"));
       NS_ENSURE_SUCCESS(rv, rv);
     } else if (mTagName == nsGkAtoms::i) {
-      rv = RemoveTextProperty(htmlEditor, NS_LITERAL_STRING("em"));
+      rv = RemoveTextProperty(aHTMLEditor, NS_LITERAL_STRING("em"));
       NS_ENSURE_SUCCESS(rv, rv);
     } else if (mTagName == nsGkAtoms::strike) {
-      rv = RemoveTextProperty(htmlEditor, NS_LITERAL_STRING("s"));
+      rv = RemoveTextProperty(aHTMLEditor, NS_LITERAL_STRING("s"));
       NS_ENSURE_SUCCESS(rv, rv);
     }
 
-    rv = RemoveTextProperty(htmlEditor, nsDependentAtomString(mTagName));
+    rv = RemoveTextProperty(aHTMLEditor, nsDependentAtomString(mTagName));
   } else {
     // Superscript and Subscript styles are mutually exclusive
-    aEditor->BeginTransaction();
+    aHTMLEditor->BeginTransaction();
 
     nsDependentAtomString tagName(mTagName);
     if (mTagName == nsGkAtoms::sub || mTagName == nsGkAtoms::sup) {
-      rv = RemoveTextProperty(htmlEditor, tagName);
+      rv = RemoveTextProperty(aHTMLEditor, tagName);
     }
     if (NS_SUCCEEDED(rv))
-      rv = SetTextProperty(htmlEditor, tagName);
+      rv = SetTextProperty(aHTMLEditor, tagName);
 
-    aEditor->EndTransaction();
+    aHTMLEditor->EndTransaction();
   }
 
   return rv;
 }
 
 nsListCommand::nsListCommand(nsIAtom* aTagName)
 : nsBaseStateUpdatingCommand(aTagName)
 {
@@ -287,44 +288,40 @@ nsListCommand::GetCurrentState(mozilla::
   bool inList = mTagName->Equals(localName);
   aParams->SetBooleanValue(STATE_ALL, !bMixed && inList);
   aParams->SetBooleanValue(STATE_MIXED, bMixed);
   aParams->SetBooleanValue(STATE_ENABLED, true);
   return NS_OK;
 }
 
 nsresult
-nsListCommand::ToggleState(nsIEditor *aEditor)
+nsListCommand::ToggleState(mozilla::HTMLEditor* aHTMLEditor)
 {
-  if (NS_WARN_IF(!aEditor)) {
-    return NS_ERROR_INVALID_ARG;
-  }
-  mozilla::HTMLEditor* htmlEditor = aEditor->AsHTMLEditor();
-  if (NS_WARN_IF(!htmlEditor)) {
+  if (NS_WARN_IF(!aHTMLEditor)) {
     return NS_ERROR_INVALID_ARG;
   }
 
   nsresult rv;
   nsCOMPtr<nsICommandParams> params =
       do_CreateInstance(NS_COMMAND_PARAMS_CONTRACTID,&rv);
   if (NS_FAILED(rv) || !params)
     return rv;
 
-  rv = GetCurrentState(htmlEditor, params);
+  rv = GetCurrentState(aHTMLEditor, params);
   NS_ENSURE_SUCCESS(rv, rv);
 
   bool inList;
   rv = params->GetBooleanValue(STATE_ALL,&inList);
   NS_ENSURE_SUCCESS(rv, rv);
 
   nsDependentAtomString listType(mTagName);
   if (inList) {
-    rv = htmlEditor->RemoveList(listType);
+    rv = aHTMLEditor->RemoveList(listType);
   } else {
-    rv = htmlEditor->MakeOrChangeList(listType, false, EmptyString());
+    rv = aHTMLEditor->MakeOrChangeList(listType, false, EmptyString());
   }
 
   return rv;
 }
 
 nsListItemCommand::nsListItemCommand(nsIAtom* aTagName)
 : nsBaseStateUpdatingCommand(aTagName)
 {
@@ -355,55 +352,51 @@ nsListItemCommand::GetCurrentState(mozil
 
   aParams->SetBooleanValue(STATE_ALL, !bMixed && inList);
   aParams->SetBooleanValue(STATE_MIXED, bMixed);
 
   return NS_OK;
 }
 
 nsresult
-nsListItemCommand::ToggleState(nsIEditor *aEditor)
+nsListItemCommand::ToggleState(mozilla::HTMLEditor* aHTMLEditor)
 {
-  if (NS_WARN_IF(!aEditor)) {
-    return NS_ERROR_INVALID_ARG;
-  }
-  mozilla::HTMLEditor* htmlEditor = aEditor->AsHTMLEditor();
-  if (NS_WARN_IF(!htmlEditor)) {
+  if (NS_WARN_IF(!aHTMLEditor)) {
     return NS_ERROR_INVALID_ARG;
   }
 
   bool inList;
   // Need to use mTagName????
   nsresult rv;
   nsCOMPtr<nsICommandParams> params =
       do_CreateInstance(NS_COMMAND_PARAMS_CONTRACTID,&rv);
   if (NS_FAILED(rv) || !params)
     return rv;
-  rv = GetCurrentState(htmlEditor, params);
+  rv = GetCurrentState(aHTMLEditor, params);
   rv = params->GetBooleanValue(STATE_ALL,&inList);
   NS_ENSURE_SUCCESS(rv, rv);
   NS_ENSURE_SUCCESS(rv, rv);
 
   if (inList) {
     // To remove a list, first get what kind of list we're in
     bool bMixed;
     nsAutoString localName;
-    rv = GetListState(htmlEditor, &bMixed, localName);
+    rv = GetListState(aHTMLEditor, &bMixed, localName);
     NS_ENSURE_SUCCESS(rv, rv);
     if (localName.IsEmpty() || bMixed) {
       return rv;
     }
-    return htmlEditor->RemoveList(localName);
+    return aHTMLEditor->RemoveList(localName);
   }
 
   // Set to the requested paragraph type
   //XXX Note: This actually doesn't work for "LI",
   //    but we currently don't use this for non DL lists anyway.
   // Problem: won't this replace any current block paragraph style?
-  return htmlEditor->SetParagraphFormat(nsDependentAtomString(mTagName));
+  return aHTMLEditor->SetParagraphFormat(nsDependentAtomString(mTagName));
 }
 
 NS_IMETHODIMP
 nsRemoveListCommand::IsCommandEnabled(const char * aCommandName,
                                       nsISupports *refCon,
                                       bool *outCmdEnabled)
 {
   *outCmdEnabled = false;
@@ -1032,28 +1025,28 @@ nsAbsolutePositioningCommand::GetCurrent
     outStateString.AssignLiteral("absolute");
 
   aParams->SetBooleanValue(STATE_MIXED,false);
   aParams->SetCStringValue(STATE_ATTRIBUTE, NS_ConvertUTF16toUTF8(outStateString).get());
   return NS_OK;
 }
 
 nsresult
-nsAbsolutePositioningCommand::ToggleState(nsIEditor *aEditor)
+nsAbsolutePositioningCommand::ToggleState(mozilla::HTMLEditor* aHTMLEditor)
 {
-  NS_ASSERTION(aEditor, "Need an editor here");
-
-  nsCOMPtr<nsIHTMLAbsPosEditor> htmlEditor = do_QueryInterface(aEditor);
-  NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE);
+  if (NS_WARN_IF(!aHTMLEditor)) {
+    return NS_ERROR_INVALID_ARG;
+  }
 
   nsCOMPtr<nsIDOMElement> elt;
-  nsresult rv = htmlEditor->GetAbsolutelyPositionedSelectionContainer(getter_AddRefs(elt));
+  nsresult rv =
+    aHTMLEditor->GetAbsolutelyPositionedSelectionContainer(getter_AddRefs(elt));
   NS_ENSURE_SUCCESS(rv, rv);
 
-  return htmlEditor->AbsolutePositionSelection(!elt);
+  return aHTMLEditor->AbsolutePositionSelection(!elt);
 }
 
 
 NS_IMETHODIMP
 nsDecreaseZIndexCommand::IsCommandEnabled(const char * aCommandName,
                                           nsISupports *refCon,
                                           bool *outCmdEnabled)
 {
--- a/editor/composer/nsComposerCommands.h
+++ b/editor/composer/nsComposerCommands.h
@@ -68,17 +68,17 @@ public:
 protected:
   virtual ~nsBaseStateUpdatingCommand();
 
   // get the current state (on or off) for this style or block format
   virtual nsresult GetCurrentState(mozilla::HTMLEditor* aHTMLEditor,
                                    nsICommandParams* aParams) = 0;
 
   // add/remove the style
-  virtual nsresult  ToggleState(nsIEditor* aEditor) = 0;
+  virtual nsresult ToggleState(mozilla::HTMLEditor* aHTMLEditor) = 0;
 
 protected:
   nsIAtom* mTagName;
 };
 
 
 // Shared class for the various style updating commands like bold, italics etc.
 // Suitable for commands whose state is either 'on' or 'off'.
@@ -89,17 +89,17 @@ public:
 
 protected:
 
   // get the current state (on or off) for this style or block format
   virtual nsresult GetCurrentState(mozilla::HTMLEditor* aHTMLEditor,
                                    nsICommandParams* aParams) override final;
 
   // add/remove the style
-  virtual nsresult  ToggleState(nsIEditor* aEditor);
+  virtual nsresult ToggleState(mozilla::HTMLEditor* aHTMLEditor) override final;
 };
 
 
 class nsInsertTagCommand : public nsBaseComposerCommand
 {
 public:
   explicit nsInsertTagCommand(nsIAtom* aTagName);
 
@@ -121,32 +121,32 @@ public:
 
 protected:
 
   // get the current state (on or off) for this style or block format
   virtual nsresult GetCurrentState(mozilla::HTMLEditor* aHTMLEditor,
                                    nsICommandParams* aParams) override final;
 
   // add/remove the style
-  virtual nsresult  ToggleState(nsIEditor* aEditor);
+  virtual nsresult ToggleState(mozilla::HTMLEditor* aHTMLEditor) override final;
 };
 
 class nsListItemCommand final : public nsBaseStateUpdatingCommand
 {
 public:
   explicit nsListItemCommand(nsIAtom* aTagName);
 
 protected:
 
   // get the current state (on or off) for this style or block format
   virtual nsresult GetCurrentState(mozilla::HTMLEditor* aHTMLEditor,
                                    nsICommandParams* aParams) override final;
 
   // add/remove the style
-  virtual nsresult  ToggleState(nsIEditor* aEditor);
+  virtual nsresult ToggleState(mozilla::HTMLEditor* aHTMLEditor) override final;
 };
 
 // Base class for commands whose state consists of a string (e.g. para format)
 class nsMultiStateCommand : public nsBaseComposerCommand
 {
 public:
 
   nsMultiStateCommand();
@@ -255,17 +255,17 @@ class nsAbsolutePositioningCommand final
 public:
   nsAbsolutePositioningCommand();
 
 protected:
 
   NS_IMETHOD IsCommandEnabled(const char *aCommandName, nsISupports *aCommandRefCon, bool *_retval);
   virtual nsresult GetCurrentState(mozilla::HTMLEditor* aHTMLEditor,
                                    nsICommandParams* aParams) override final;
-  virtual nsresult  ToggleState(nsIEditor* aEditor);
+  virtual nsresult ToggleState(mozilla::HTMLEditor* aHTMLEditor) override final;
 };
 
 // composer commands
 
 NS_DECL_COMPOSER_COMMAND(nsCloseCommand)
 NS_DECL_COMPOSER_COMMAND(nsDocumentStateCommand)
 NS_DECL_COMPOSER_COMMAND(nsSetDocumentStateCommand)
 NS_DECL_COMPOSER_COMMAND(nsSetDocumentOptionsCommand)