Bug 1549661 - part 3: Create `EditorCommand::DoCommandParam(Command aCommand, const nsACString& aCStringParam, TextEditor& aTextEditor)` r=m_kato
authorMasayuki Nakano <masayuki@d-toybox.com>
Tue, 21 May 2019 07:45:22 +0000
changeset 474680 7c1521b96d93706a984c57c81f44f1f4864317a8
parent 474679 b9fd628d913acbb9d70eb04809e4f1008a61fc15
child 474681 c3c796dd6070d4011e0f4f9a474c2134761ea419
push id36044
push userrmaries@mozilla.com
push dateTue, 21 May 2019 15:45:34 +0000
treeherdermozilla-central@78571bb1f20e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersm_kato
bugs1549661
milestone69.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 1549661 - part 3: Create `EditorCommand::DoCommandParam(Command aCommand, const nsACString& aCStringParam, TextEditor& aTextEditor)` r=m_kato If `nsIControllerCommand::DoCommandParams()` is called with `nullptr` for its `aParams`, this patch sets `VoidCString()` to `DoCommandParam()` for making each implementation be able to consider whether the case is an error or treat it as specific default value. Differential Revision: https://phabricator.services.mozilla.com/D30497
editor/libeditor/EditorCommands.cpp
editor/libeditor/EditorCommands.h
editor/libeditor/HTMLEditorDocumentCommands.cpp
--- a/editor/libeditor/EditorCommands.cpp
+++ b/editor/libeditor/EditorCommands.cpp
@@ -116,16 +116,42 @@ EditorCommand::DoCommandParams(const cha
           NS_SUCCEEDED(rv),
           "Failed to do command from nsIControllerCommand::DoCommandParams()");
       return rv;
     }
     MOZ_ASSERT_UNREACHABLE("Unexpected state for bool");
     return NS_ERROR_NOT_IMPLEMENTED;
   }
 
+  if (Any(paramType & EditorCommandParamType::CString)) {
+    if (!params) {
+      nsresult rv = DoCommandParam(command, VoidCString(),
+                                   MOZ_KnownLive(*editor->AsTextEditor()));
+      NS_WARNING_ASSERTION(
+          NS_SUCCEEDED(rv),
+          "Failed to do command from nsIControllerCommand::DoCommandParams()");
+      return rv;
+    }
+    if (Any(paramType & EditorCommandParamType::StateAttribute)) {
+      nsCString cStringParam;
+      nsresult rv = params->GetCString(STATE_ATTRIBUTE, cStringParam);
+      if (NS_WARN_IF(NS_FAILED(rv))) {
+        return rv;
+      }
+      rv = DoCommandParam(command, cStringParam,
+                          MOZ_KnownLive(*editor->AsTextEditor()));
+      NS_WARNING_ASSERTION(
+          NS_SUCCEEDED(rv),
+          "Failed to do command from nsIControllerCommand::DoCommandParams()");
+      return rv;
+    }
+    MOZ_ASSERT_UNREACHABLE("Unexpected state for CString");
+    return NS_ERROR_NOT_IMPLEMENTED;
+  }
+
   nsresult rv = DoCommandParams(command, MOZ_KnownLive(params),
                                 MOZ_KnownLive(*editor->AsTextEditor()));
   NS_WARNING_ASSERTION(
       NS_SUCCEEDED(rv),
       "Failed to do command from nsIControllerCommand::DoCommandParams()");
   return rv;
 }
 
--- a/editor/libeditor/EditorCommands.h
+++ b/editor/libeditor/EditorCommands.h
@@ -176,18 +176,18 @@ class EditorCommand : public nsIControll
       // SetDocumentStateCommand
       case Command::SetDocumentModified:
       case Command::SetDocumentUseCSS:
       case Command::SetDocumentReadOnly:
       case Command::SetDocumentInsertBROnEnterKeyPress:
         return EditorCommandParamType::Bool |
                EditorCommandParamType::StateAttribute;
       case Command::SetDocumentDefaultParagraphSeparator:
-        // XXX Treat as Unknown for now
-        return EditorCommandParamType::Transferable;
+        return EditorCommandParamType::CString |
+               EditorCommandParamType::StateAttribute;
       case Command::ToggleObjectResizers:
       case Command::ToggleInlineTableEditor:
       case Command::ToggleAbsolutePositionEditor:
         return EditorCommandParamType::Bool |
                EditorCommandParamType::StateAttribute;
 
       // IndentCommand
       case Command::FormatIndent:
@@ -315,16 +315,29 @@ class EditorCommand : public nsIControll
   MOZ_CAN_RUN_SCRIPT
   virtual nsresult DoCommandParam(Command aCommand,
                                   const Maybe<bool>& aBoolParam,
                                   TextEditor& aTextEditor) const {
     MOZ_ASSERT_UNREACHABLE("Wrong overload is called");
     return NS_ERROR_NOT_IMPLEMENTED;
   }
 
+  /**
+   * Called only when the result of EditorCommand::GetParamType(aCommand)
+   * includes EditorCommandParamType::CString.  If aCStringParam is void, it
+   * means that given param was nullptr.
+   */
+  MOZ_CAN_RUN_SCRIPT
+  virtual nsresult DoCommandParam(Command aCommand,
+                                  const nsACString& aCStringParam,
+                                  TextEditor& aTextEditor) const {
+    MOZ_ASSERT_UNREACHABLE("Wrong overload is called");
+    return NS_ERROR_NOT_IMPLEMENTED;
+  }
+
  protected:
   EditorCommand() = default;
   virtual ~EditorCommand() = default;
 };
 
 #define NS_DECL_EDITOR_COMMAND_COMMON_METHODS                              \
  public:                                                                   \
   MOZ_CAN_RUN_SCRIPT                                                       \
@@ -358,16 +371,23 @@ class EditorCommand : public nsIControll
 
 #define NS_DECL_DO_COMMAND_PARAM_FOR_BOOL_PARAM                  \
  public:                                                         \
   MOZ_CAN_RUN_SCRIPT                                             \
   virtual nsresult DoCommandParam(Command aCommand,              \
                                   const Maybe<bool>& aBoolParam, \
                                   TextEditor& aTextEditor) const final;
 
+#define NS_DECL_DO_COMMAND_PARAM_FOR_CSTRING_PARAM                 \
+ public:                                                           \
+  MOZ_CAN_RUN_SCRIPT                                               \
+  virtual nsresult DoCommandParam(Command aCommand,                \
+                                  const nsACString& aCStringParam, \
+                                  TextEditor& aTextEditor) const final;
+
 #define NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(_cmd) \
  public:                                                   \
   static _cmd* GetInstance() {                             \
     if (!sInstance) {                                      \
       sInstance = new _cmd();                              \
     }                                                      \
     return sInstance;                                      \
   }                                                        \
@@ -405,16 +425,27 @@ class EditorCommand : public nsIControll
     NS_DECL_DO_COMMAND_PARAM_FOR_BOOL_PARAM            \
     NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(_cmd) \
                                                        \
    protected:                                          \
     _cmd() = default;                                  \
     virtual ~_cmd() = default;                         \
   };
 
+#define NS_DECL_EDITOR_COMMAND_FOR_CSTRING_PARAM(_cmd) \
+  class _cmd final : public EditorCommand {            \
+    NS_DECL_EDITOR_COMMAND_COMMON_METHODS              \
+    NS_DECL_DO_COMMAND_PARAM_FOR_CSTRING_PARAM         \
+    NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(_cmd) \
+                                                       \
+   protected:                                          \
+    _cmd() = default;                                  \
+    virtual ~_cmd() = default;                         \
+  };
+
 // basic editor commands
 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(UndoCommand)
 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(RedoCommand)
 
 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(CutCommand)
 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(CutOrDeleteCommand)
 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(CopyCommand)
 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(CopyOrDeleteCommand)
@@ -746,18 +777,18 @@ class AbsolutePositioningCommand final :
 
 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(DocumentStateCommand)
 
 class SetDocumentStateCommand final : public EditorCommand {
  public:
   NS_INLINE_DECL_REFCOUNTING_INHERITED(SetDocumentStateCommand, EditorCommand)
 
   NS_DECL_EDITOR_COMMAND_COMMON_METHODS
-  NS_DECL_DO_COMMAND_PARAMS
   NS_DECL_DO_COMMAND_PARAM_FOR_BOOL_PARAM
+  NS_DECL_DO_COMMAND_PARAM_FOR_CSTRING_PARAM
   NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(SetDocumentStateCommand)
 
  private:
   SetDocumentStateCommand() = default;
   virtual ~SetDocumentStateCommand() = default;
 };
 
 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(DecreaseZIndexCommand)
@@ -778,17 +809,19 @@ NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH
 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(DecreaseFontSizeCommand)
 
 // Insert content commands
 NS_DECL_EDITOR_COMMAND(InsertHTMLCommand)
 
 #undef NS_DECL_EDITOR_COMMAND
 #undef NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE
 #undef NS_DECL_EDITOR_COMMAND_FOR_BOOL_PARAM
+#undef NS_DECL_EDITOR_COMMAND_FOR_CSTRING_PARAM
 #undef NS_DECL_EDITOR_COMMAND_COMMON_METHODS
 #undef NS_DECL_DO_COMMAND_PARAMS
 #undef NS_DECL_DO_COMMAND_PARAM_DELEGATE_TO_DO_COMMAND
 #undef NS_DECL_DO_COMMAND_PARAM_FOR_BOOL_PARAM
+#undef NS_DECL_DO_COMMAND_PARAM_FOR_CSTRING_PARAM
 #undef NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON
 
 }  // namespace mozilla
 
 #endif  // #ifndef mozilla_EditorCommands_h
--- a/editor/libeditor/HTMLEditorDocumentCommands.cpp
+++ b/editor/libeditor/HTMLEditorDocumentCommands.cpp
@@ -117,54 +117,49 @@ nsresult SetDocumentStateCommand::DoComm
           ->EnableAbsolutePositionEditor(aBoolParam.value());
       return NS_OK;
     }
     default:
       return NS_ERROR_NOT_IMPLEMENTED;
   }
 }
 
-nsresult SetDocumentStateCommand::DoCommandParams(
-    Command aCommand, nsCommandParams* aParams, TextEditor& aTextEditor) const {
-  if (NS_WARN_IF(!aParams)) {
+nsresult SetDocumentStateCommand::DoCommandParam(
+    Command aCommand, const nsACString& aCStringParam,
+    TextEditor& aTextEditor) const {
+  if (NS_WARN_IF(aCStringParam.IsVoid())) {
     return NS_ERROR_INVALID_ARG;
   }
 
   if (NS_WARN_IF(!aTextEditor.AsHTMLEditor())) {
     return NS_ERROR_FAILURE;
   }
 
   switch (aCommand) {
     case Command::SetDocumentDefaultParagraphSeparator: {
-      HTMLEditor* htmlEditor = aTextEditor.AsHTMLEditor();
-      if (NS_WARN_IF(!htmlEditor)) {
-        return NS_ERROR_INVALID_ARG;
-      }
-
-      nsAutoCString newValue;
-      nsresult rv = aParams->GetCString(STATE_ATTRIBUTE, newValue);
-      if (NS_WARN_IF(NS_FAILED(rv))) {
-        return rv;
-      }
-
-      if (newValue.LowerCaseEqualsLiteral("div")) {
-        htmlEditor->SetDefaultParagraphSeparator(ParagraphSeparator::div);
+      if (aCStringParam.LowerCaseEqualsLiteral("div")) {
+        aTextEditor.AsHTMLEditor()->SetDefaultParagraphSeparator(
+            ParagraphSeparator::div);
         return NS_OK;
       }
-      if (newValue.LowerCaseEqualsLiteral("p")) {
-        htmlEditor->SetDefaultParagraphSeparator(ParagraphSeparator::p);
+      if (aCStringParam.LowerCaseEqualsLiteral("p")) {
+        aTextEditor.AsHTMLEditor()->SetDefaultParagraphSeparator(
+            ParagraphSeparator::p);
         return NS_OK;
       }
-      if (newValue.LowerCaseEqualsLiteral("br")) {
+      if (aCStringParam.LowerCaseEqualsLiteral("br")) {
         // Mozilla extension for backwards compatibility
-        htmlEditor->SetDefaultParagraphSeparator(ParagraphSeparator::br);
+        aTextEditor.AsHTMLEditor()->SetDefaultParagraphSeparator(
+            ParagraphSeparator::br);
         return NS_OK;
       }
 
       // This should not be reachable from nsHTMLDocument::ExecCommand
+      // XXX Shouldn't return error in this case because Chrome does not throw
+      //     exception in this case.
       NS_WARNING("Invalid default paragraph separator");
       return NS_ERROR_UNEXPECTED;
     }
     default:
       return NS_ERROR_NOT_IMPLEMENTED;
   }
 }