diff --git a/editor/libeditor/EditorCommands.cpp b/editor/libeditor/EditorCommands.cpp index d9a0b6da122f..51f269954b91 100644 --- a/editor/libeditor/EditorCommands.cpp +++ b/editor/libeditor/EditorCommands.cpp @@ -37,32 +37,31 @@ namespace mozilla { NS_IMPL_ISUPPORTS(EditorCommand, nsIControllerCommand) +NS_IMETHODIMP +EditorCommand::IsCommandEnabled(const char* aCommandName, + nsISupports* aCommandRefCon, bool* aIsEnabled) { + if (NS_WARN_IF(!aCommandName) || NS_WARN_IF(!aIsEnabled)) { + return NS_ERROR_INVALID_ARG; + } + + nsCOMPtr editor = do_QueryInterface(aCommandRefCon); + TextEditor* textEditor = editor ? editor->AsTextEditor() : nullptr; + *aIsEnabled = IsCommandEnabled(aCommandName, textEditor); + return NS_OK; +} + /****************************************************************************** * mozilla::UndoCommand ******************************************************************************/ StaticRefPtr UndoCommand::sInstance; -NS_IMETHODIMP -UndoCommand::IsCommandEnabled(const char* aCommandName, - nsISupports* aCommandRefCon, bool* aIsEnabled) { - if (NS_WARN_IF(!aIsEnabled)) { - return NS_ERROR_INVALID_ARG; +bool UndoCommand::IsCommandEnabled(const char* aCommandName, + TextEditor* aTextEditor) const { + if (!aTextEditor) { + return false; } - - *aIsEnabled = false; - - nsCOMPtr editor = do_QueryInterface(aCommandRefCon); - if (!editor) { - return NS_OK; - } - TextEditor* textEditor = editor->AsTextEditor(); - MOZ_ASSERT(textEditor); - if (!textEditor->IsSelectionEditable()) { - return NS_OK; - } - *aIsEnabled = textEditor->CanUndo(); - return NS_OK; + return aTextEditor->IsSelectionEditable() && aTextEditor->CanUndo(); } NS_IMETHODIMP @@ -99,26 +98,12 @@ UndoCommand::GetCommandStateParams(const char* aCommandName, StaticRefPtr RedoCommand::sInstance; -NS_IMETHODIMP -RedoCommand::IsCommandEnabled(const char* aCommandName, - nsISupports* aCommandRefCon, bool* aIsEnabled) { - if (NS_WARN_IF(!aIsEnabled)) { - return NS_ERROR_INVALID_ARG; +bool RedoCommand::IsCommandEnabled(const char* aCommandName, + TextEditor* aTextEditor) const { + if (!aTextEditor) { + return false; } - - *aIsEnabled = false; - - nsCOMPtr editor = do_QueryInterface(aCommandRefCon); - if (!editor) { - return NS_OK; - } - TextEditor* textEditor = editor->AsTextEditor(); - MOZ_ASSERT(textEditor); - if (!textEditor->IsSelectionEditable()) { - return NS_OK; - } - *aIsEnabled = textEditor->CanRedo(); - return NS_OK; + return aTextEditor->IsSelectionEditable() && aTextEditor->CanRedo(); } NS_IMETHODIMP @@ -155,26 +140,12 @@ RedoCommand::GetCommandStateParams(const char* aCommandName, StaticRefPtr CutCommand::sInstance; -NS_IMETHODIMP -CutCommand::IsCommandEnabled(const char* aCommandName, - nsISupports* aCommandRefCon, bool* aIsEnabled) { - if (NS_WARN_IF(!aIsEnabled)) { - return NS_ERROR_INVALID_ARG; +bool CutCommand::IsCommandEnabled(const char* aCommandName, + TextEditor* aTextEditor) const { + if (!aTextEditor) { + return false; } - - *aIsEnabled = false; - - nsCOMPtr editor = do_QueryInterface(aCommandRefCon); - if (!editor) { - return NS_OK; - } - TextEditor* textEditor = editor->AsTextEditor(); - MOZ_ASSERT(textEditor); - if (!textEditor->IsSelectionEditable()) { - return NS_OK; - } - *aIsEnabled = textEditor->CanCut(); - return NS_OK; + return aTextEditor->IsSelectionEditable() && aTextEditor->CanCut(); } NS_IMETHODIMP @@ -209,22 +180,12 @@ CutCommand::GetCommandStateParams(const char* aCommandName, StaticRefPtr CutOrDeleteCommand::sInstance; -NS_IMETHODIMP -CutOrDeleteCommand::IsCommandEnabled(const char* aCommandName, - nsISupports* aCommandRefCon, - bool* aIsEnabled) { - if (NS_WARN_IF(!aIsEnabled)) { - return NS_ERROR_INVALID_ARG; +bool CutOrDeleteCommand::IsCommandEnabled(const char* aCommandName, + TextEditor* aTextEditor) const { + if (!aTextEditor) { + return false; } - nsCOMPtr editor = do_QueryInterface(aCommandRefCon); - if (!editor) { - *aIsEnabled = false; - return NS_OK; - } - TextEditor* textEditor = editor->AsTextEditor(); - MOZ_ASSERT(textEditor); - *aIsEnabled = textEditor->IsSelectionEditable(); - return NS_OK; + return aTextEditor->IsSelectionEditable(); } NS_IMETHODIMP @@ -270,21 +231,12 @@ CutOrDeleteCommand::GetCommandStateParams(const char* aCommandName, StaticRefPtr CopyCommand::sInstance; -NS_IMETHODIMP -CopyCommand::IsCommandEnabled(const char* aCommandName, - nsISupports* aCommandRefCon, bool* aIsEnabled) { - if (NS_WARN_IF(!aIsEnabled)) { - return NS_ERROR_INVALID_ARG; +bool CopyCommand::IsCommandEnabled(const char* aCommandName, + TextEditor* aTextEditor) const { + if (!aTextEditor) { + return false; } - nsCOMPtr editor = do_QueryInterface(aCommandRefCon); - if (!editor) { - *aIsEnabled = false; - return NS_OK; - } - TextEditor* textEditor = editor->AsTextEditor(); - MOZ_ASSERT(textEditor); - *aIsEnabled = textEditor->CanCopy(); - return NS_OK; + return aTextEditor->CanCopy(); } NS_IMETHODIMP @@ -320,23 +272,12 @@ CopyCommand::GetCommandStateParams(const char* aCommandName, StaticRefPtr CopyOrDeleteCommand::sInstance; -NS_IMETHODIMP -CopyOrDeleteCommand::IsCommandEnabled(const char* aCommandName, - nsISupports* aCommandRefCon, - bool* aIsEnabled) { - if (NS_WARN_IF(!aIsEnabled)) { - return NS_ERROR_INVALID_ARG; +bool CopyOrDeleteCommand::IsCommandEnabled(const char* aCommandName, + TextEditor* aTextEditor) const { + if (!aTextEditor) { + return false; } - - nsCOMPtr editor = do_QueryInterface(aCommandRefCon); - if (!editor) { - *aIsEnabled = false; - return NS_OK; - } - TextEditor* textEditor = editor->AsTextEditor(); - MOZ_ASSERT(textEditor); - *aIsEnabled = textEditor->IsSelectionEditable(); - return NS_OK; + return aTextEditor->IsSelectionEditable(); } NS_IMETHODIMP @@ -382,26 +323,13 @@ CopyOrDeleteCommand::GetCommandStateParams(const char* aCommandName, StaticRefPtr PasteCommand::sInstance; -NS_IMETHODIMP -PasteCommand::IsCommandEnabled(const char* aCommandName, - nsISupports* aCommandRefCon, bool* aIsEnabled) { - if (NS_WARN_IF(!aIsEnabled)) { - return NS_ERROR_INVALID_ARG; +bool PasteCommand::IsCommandEnabled(const char* aCommandName, + TextEditor* aTextEditor) const { + if (!aTextEditor) { + return false; } - - *aIsEnabled = false; - - nsCOMPtr editor = do_QueryInterface(aCommandRefCon); - if (!editor) { - return NS_OK; - } - TextEditor* textEditor = editor->AsTextEditor(); - MOZ_ASSERT(textEditor); - if (!textEditor->IsSelectionEditable()) { - return NS_OK; - } - *aIsEnabled = textEditor->CanPaste(nsIClipboard::kGlobalClipboard); - return NS_OK; + return aTextEditor->IsSelectionEditable() && + aTextEditor->CanPaste(nsIClipboard::kGlobalClipboard); } NS_IMETHODIMP @@ -439,27 +367,13 @@ PasteCommand::GetCommandStateParams(const char* aCommandName, StaticRefPtr PasteTransferableCommand::sInstance; -NS_IMETHODIMP -PasteTransferableCommand::IsCommandEnabled(const char* aCommandName, - nsISupports* aCommandRefCon, - bool* aIsEnabled) { - if (NS_WARN_IF(!aIsEnabled)) { - return NS_ERROR_INVALID_ARG; +bool PasteTransferableCommand::IsCommandEnabled(const char* aCommandName, + TextEditor* aTextEditor) const { + if (!aTextEditor) { + return false; } - - *aIsEnabled = false; - - nsCOMPtr editor = do_QueryInterface(aCommandRefCon); - if (!editor) { - return NS_OK; - } - TextEditor* textEditor = editor->AsTextEditor(); - MOZ_ASSERT(textEditor); - if (!textEditor->IsSelectionEditable()) { - return NS_OK; - } - *aIsEnabled = textEditor->CanPasteTransferable(nullptr); - return NS_OK; + return aTextEditor->IsSelectionEditable() && + aTextEditor->CanPasteTransferable(nullptr); } NS_IMETHODIMP @@ -533,22 +447,12 @@ PasteTransferableCommand::GetCommandStateParams(const char* aCommandName, StaticRefPtr SwitchTextDirectionCommand::sInstance; -NS_IMETHODIMP -SwitchTextDirectionCommand::IsCommandEnabled(const char* aCommandName, - nsISupports* aCommandRefCon, - bool* aIsEnabled) { - if (NS_WARN_IF(!aIsEnabled)) { - return NS_ERROR_INVALID_ARG; +bool SwitchTextDirectionCommand::IsCommandEnabled( + const char* aCommandName, TextEditor* aTextEditor) const { + if (!aTextEditor) { + return false; } - nsCOMPtr editor = do_QueryInterface(aCommandRefCon); - if (!editor) { - *aIsEnabled = false; - return NS_OK; - } - TextEditor* textEditor = editor->AsTextEditor(); - MOZ_ASSERT(textEditor); - *aIsEnabled = textEditor->IsSelectionEditable(); - return NS_OK; + return aTextEditor->IsSelectionEditable(); } NS_IMETHODIMP @@ -587,32 +491,20 @@ SwitchTextDirectionCommand::GetCommandStateParams(const char* aCommandName, StaticRefPtr DeleteCommand::sInstance; -NS_IMETHODIMP -DeleteCommand::IsCommandEnabled(const char* aCommandName, - nsISupports* aCommandRefCon, bool* aIsEnabled) { - if (NS_WARN_IF(!aIsEnabled)) { - return NS_ERROR_INVALID_ARG; +bool DeleteCommand::IsCommandEnabled(const char* aCommandName, + TextEditor* aTextEditor) const { + if (!aTextEditor) { + return false; } - - *aIsEnabled = false; - - nsCOMPtr editor = do_QueryInterface(aCommandRefCon); - if (!editor) { - return NS_OK; - } - - TextEditor* textEditor = editor->AsTextEditor(); - MOZ_ASSERT(textEditor); - // We can generally delete whenever the selection is editable. However, // cmd_delete doesn't make sense if the selection is collapsed because it's // directionless, which is the same condition under which we can't cut. - *aIsEnabled = textEditor->IsSelectionEditable(); + bool isEnabled = aTextEditor->IsSelectionEditable(); - if (!nsCRT::strcmp("cmd_delete", aCommandName) && *aIsEnabled) { - *aIsEnabled = textEditor->CanDelete(); + if (!nsCRT::strcmp("cmd_delete", aCommandName) && isEnabled) { + return aTextEditor->CanDelete(); } - return NS_OK; + return isEnabled; } NS_IMETHODIMP @@ -678,32 +570,20 @@ DeleteCommand::GetCommandStateParams(const char* aCommandName, StaticRefPtr SelectAllCommand::sInstance; -NS_IMETHODIMP -SelectAllCommand::IsCommandEnabled(const char* aCommandName, - nsISupports* aCommandRefCon, - bool* aIsEnabled) { - NS_ENSURE_ARG_POINTER(aIsEnabled); - - nsresult rv = NS_OK; +bool SelectAllCommand::IsCommandEnabled(const char* aCommandName, + TextEditor* aTextEditor) const { // You can always select all, unless the selection is editable, // and the editable region is empty! - *aIsEnabled = true; - - nsCOMPtr editor = do_QueryInterface(aCommandRefCon); - if (!editor) { - return NS_OK; + if (!aTextEditor) { + return true; } // You can select all if there is an editor which is non-empty - TextEditor* textEditor = editor->AsTextEditor(); - MOZ_ASSERT(textEditor); bool isEmpty = false; - rv = textEditor->IsEmpty(&isEmpty); - if (NS_WARN_IF(NS_FAILED(rv))) { - return rv; + if (NS_WARN_IF(NS_FAILED(aTextEditor->IsEmpty(&isEmpty)))) { + return false; } - *aIsEnabled = !isEmpty; - return NS_OK; + return !isEmpty; } NS_IMETHODIMP @@ -740,20 +620,12 @@ SelectAllCommand::GetCommandStateParams(const char* aCommandName, StaticRefPtr SelectionMoveCommands::sInstance; -NS_IMETHODIMP -SelectionMoveCommands::IsCommandEnabled(const char* aCommandName, - nsISupports* aCommandRefCon, - bool* aIsEnabled) { - NS_ENSURE_ARG_POINTER(aIsEnabled); - nsCOMPtr editor = do_QueryInterface(aCommandRefCon); - if (!editor) { - *aIsEnabled = false; - return NS_OK; +bool SelectionMoveCommands::IsCommandEnabled(const char* aCommandName, + TextEditor* aTextEditor) const { + if (!aTextEditor) { + return false; } - TextEditor* textEditor = editor->AsTextEditor(); - MOZ_ASSERT(textEditor); - *aIsEnabled = textEditor->IsSelectionEditable(); - return NS_OK; + return aTextEditor->IsSelectionEditable(); } static const struct ScrollCommand { @@ -886,22 +758,12 @@ SelectionMoveCommands::GetCommandStateParams(const char* aCommandName, StaticRefPtr InsertPlaintextCommand::sInstance; -NS_IMETHODIMP -InsertPlaintextCommand::IsCommandEnabled(const char* aCommandName, - nsISupports* aCommandRefCon, - bool* aIsEnabled) { - if (NS_WARN_IF(!aIsEnabled)) { - return NS_ERROR_INVALID_ARG; +bool InsertPlaintextCommand::IsCommandEnabled(const char* aCommandName, + TextEditor* aTextEditor) const { + if (!aTextEditor) { + return false; } - nsCOMPtr editor = do_QueryInterface(aCommandRefCon); - if (NS_WARN_IF(!editor)) { - *aIsEnabled = false; - return NS_ERROR_FAILURE; - } - TextEditor* textEditor = editor->AsTextEditor(); - MOZ_ASSERT(textEditor); - *aIsEnabled = textEditor->IsSelectionEditable(); - return NS_OK; + return aTextEditor->IsSelectionEditable(); } NS_IMETHODIMP @@ -977,23 +839,12 @@ InsertPlaintextCommand::GetCommandStateParams(const char* aCommandName, StaticRefPtr InsertParagraphCommand::sInstance; -NS_IMETHODIMP -InsertParagraphCommand::IsCommandEnabled(const char* aCommandName, - nsISupports* aCommandRefCon, - bool* aIsEnabled) { - if (NS_WARN_IF(!aIsEnabled)) { - return NS_ERROR_INVALID_ARG; +bool InsertParagraphCommand::IsCommandEnabled(const char* aCommandName, + TextEditor* aTextEditor) const { + if (!aTextEditor) { + return false; } - nsCOMPtr editor = do_QueryInterface(aCommandRefCon); - if (NS_WARN_IF(!editor)) { - *aIsEnabled = false; - return NS_ERROR_FAILURE; - } - - TextEditor* textEditor = editor->AsTextEditor(); - MOZ_ASSERT(textEditor); - *aIsEnabled = textEditor->IsSelectionEditable(); - return NS_OK; + return aTextEditor->IsSelectionEditable(); } NS_IMETHODIMP @@ -1037,23 +888,12 @@ InsertParagraphCommand::GetCommandStateParams(const char* aCommandName, StaticRefPtr InsertLineBreakCommand::sInstance; -NS_IMETHODIMP -InsertLineBreakCommand::IsCommandEnabled(const char* aCommandName, - nsISupports* aCommandRefCon, - bool* aIsEnabled) { - if (NS_WARN_IF(!aIsEnabled)) { - return NS_ERROR_INVALID_ARG; +bool InsertLineBreakCommand::IsCommandEnabled(const char* aCommandName, + TextEditor* aTextEditor) const { + if (!aTextEditor) { + return false; } - nsCOMPtr editor = do_QueryInterface(aCommandRefCon); - if (NS_WARN_IF(!editor)) { - *aIsEnabled = false; - return NS_ERROR_FAILURE; - } - - TextEditor* textEditor = editor->AsTextEditor(); - MOZ_ASSERT(textEditor); - *aIsEnabled = textEditor->IsSelectionEditable(); - return NS_OK; + return aTextEditor->IsSelectionEditable(); } NS_IMETHODIMP @@ -1097,27 +937,13 @@ InsertLineBreakCommand::GetCommandStateParams(const char* aCommandName, StaticRefPtr PasteQuotationCommand::sInstance; -NS_IMETHODIMP -PasteQuotationCommand::IsCommandEnabled(const char* aCommandName, - nsISupports* aCommandRefCon, - bool* aIsEnabled) { - if (NS_WARN_IF(!aIsEnabled)) { - return NS_ERROR_INVALID_ARG; +bool PasteQuotationCommand::IsCommandEnabled(const char* aCommandName, + TextEditor* aTextEditor) const { + if (!aTextEditor) { + return false; } - - *aIsEnabled = false; - - nsCOMPtr editor = do_QueryInterface(aCommandRefCon); - if (!editor) { - return NS_OK; - } - TextEditor* textEditor = editor->AsTextEditor(); - MOZ_ASSERT(textEditor); - if (textEditor->IsSingleLineEditor()) { - return NS_OK; - } - *aIsEnabled = textEditor->CanPaste(nsIClipboard::kGlobalClipboard); - return NS_OK; + return !aTextEditor->IsSingleLineEditor() && + aTextEditor->CanPaste(nsIClipboard::kGlobalClipboard); } NS_IMETHODIMP diff --git a/editor/libeditor/EditorCommands.h b/editor/libeditor/EditorCommands.h index aeefdd8c6669..f2c0c6d3aa87 100644 --- a/editor/libeditor/EditorCommands.h +++ b/editor/libeditor/EditorCommands.h @@ -18,6 +18,7 @@ class nsICommandParams; namespace mozilla { class HTMLEditor; +class TextEditor; /** * This is a base class for commands registered with the editor controller. @@ -29,6 +30,15 @@ class EditorCommand : public nsIControllerCommand { public: NS_DECL_ISUPPORTS + // nsIControllerCommand methods. Use EditorCommand specific methods instead + // for internal use. + NS_IMETHOD IsCommandEnabled(const char* aCommandName, + nsISupports* aCommandRefCon, + bool* aIsEnabled) final; + + virtual bool IsCommandEnabled(const char* aCommandName, + TextEditor* aTextEditor) const = 0; + protected: EditorCommand() = default; virtual ~EditorCommand() = default; @@ -36,9 +46,9 @@ class EditorCommand : public nsIControllerCommand { #define NS_DECL_EDITOR_COMMAND_METHODS(_cmd) \ public: \ - NS_IMETHOD IsCommandEnabled(const char* aCommandName, \ - nsISupports* aCommandRefCon, bool* aIsEnabled) \ - final; \ + virtual bool IsCommandEnabled(const char* aCommandName, \ + TextEditor* aTextEditor) const final; \ + using EditorCommand::IsCommandEnabled; \ MOZ_CAN_RUN_SCRIPT \ NS_IMETHOD DoCommand(const char* aCommandName, nsISupports* aCommandRefCon) \ final; \ diff --git a/editor/libeditor/HTMLEditorCommands.cpp b/editor/libeditor/HTMLEditorCommands.cpp index 3cc138402827..95d0a8dd0fc6 100644 --- a/editor/libeditor/HTMLEditorCommands.cpp +++ b/editor/libeditor/HTMLEditorCommands.cpp @@ -54,32 +54,19 @@ MOZ_CAN_RUN_SCRIPT_BOUNDARY // XXX Needs to change nsIControllerCommand.idl nsRefPtrHashtable StateUpdatingCommandBase::sTagNameTable; -NS_IMETHODIMP -StateUpdatingCommandBase::IsCommandEnabled(const char* aCommandName, - nsISupports* refCon, - bool* outCmdEnabled) { - nsCOMPtr editor = do_QueryInterface(refCon); - if (!editor) { - *outCmdEnabled = false; - return NS_OK; +bool StateUpdatingCommandBase::IsCommandEnabled(const char* aCommandName, + TextEditor* aTextEditor) const { + if (!aTextEditor) { + return false; } - TextEditor* textEditor = editor->AsTextEditor(); - if (!textEditor) { - *outCmdEnabled = false; - return NS_OK; + if (!aTextEditor->IsSelectionEditable()) { + return false; } - if (!textEditor->IsSelectionEditable()) { - *outCmdEnabled = false; - return NS_OK; + if (!strcmp(aCommandName, "cmd_absPos")) { + HTMLEditor* htmlEditor = aTextEditor->AsHTMLEditor(); + return htmlEditor && htmlEditor->IsAbsolutePositionEditorEnabled(); } - if (!nsCRT::strcmp(aCommandName, "cmd_absPos")) { - HTMLEditor* htmlEditor = textEditor->AsHTMLEditor(); - *outCmdEnabled = - htmlEditor && htmlEditor->IsAbsolutePositionEditorEnabled(); - return NS_OK; - } - *outCmdEnabled = true; - return NS_OK; + return true; } NS_IMETHODIMP @@ -132,27 +119,20 @@ StateUpdatingCommandBase::GetCommandStateParams(const char* aCommandName, StaticRefPtr PasteNoFormattingCommand::sInstance; -NS_IMETHODIMP -PasteNoFormattingCommand::IsCommandEnabled(const char* aCommandName, - nsISupports* refCon, - bool* outCmdEnabled) { - NS_ENSURE_ARG_POINTER(outCmdEnabled); - *outCmdEnabled = false; - - nsCOMPtr editor = do_QueryInterface(refCon); - if (NS_WARN_IF(!editor)) { - return NS_ERROR_FAILURE; +bool PasteNoFormattingCommand::IsCommandEnabled(const char* aCommandName, + TextEditor* aTextEditor) const { + if (!aTextEditor) { + return false; } // This command is only implemented by nsIHTMLEditor, since // pasting in a plaintext editor automatically only supplies // "unformatted" text - mozilla::HTMLEditor* htmlEditor = editor->AsHTMLEditor(); - if (NS_WARN_IF(!htmlEditor)) { - return NS_ERROR_FAILURE; + HTMLEditor* htmlEditor = aTextEditor->AsHTMLEditor(); + if (!htmlEditor) { + return false; } - *outCmdEnabled = htmlEditor->CanPaste(nsIClipboard::kGlobalClipboard); - return NS_OK; + return htmlEditor->CanPaste(nsIClipboard::kGlobalClipboard); } NS_IMETHODIMP @@ -419,35 +399,29 @@ nsresult ListItemCommand::ToggleState(nsAtom* aTagName, StaticRefPtr RemoveListCommand::sInstance; -NS_IMETHODIMP -RemoveListCommand::IsCommandEnabled(const char* aCommandName, - nsISupports* refCon, bool* outCmdEnabled) { - *outCmdEnabled = false; - nsCOMPtr editor = do_QueryInterface(refCon); - if (!editor) { - return NS_OK; +bool RemoveListCommand::IsCommandEnabled(const char* aCommandName, + TextEditor* aTextEditor) const { + if (!aTextEditor) { + return false; } - mozilla::EditorBase* editorBase = editor->AsEditorBase(); - MOZ_ASSERT(editorBase); - - if (!editorBase->IsSelectionEditable()) { - return NS_OK; + if (!aTextEditor->IsSelectionEditable()) { + return false; } // It is enabled if we are in any list type - mozilla::HTMLEditor* htmlEditor = editor->AsHTMLEditor(); + HTMLEditor* htmlEditor = aTextEditor->AsHTMLEditor(); if (NS_WARN_IF(!htmlEditor)) { - return NS_ERROR_FAILURE; + return false; } bool bMixed; nsAutoString localName; nsresult rv = GetListState(MOZ_KnownLive(htmlEditor), &bMixed, localName); - NS_ENSURE_SUCCESS(rv, rv); - - *outCmdEnabled = bMixed || !localName.IsEmpty(); - return NS_OK; + if (NS_WARN_IF(NS_FAILED(rv))) { + return false; + } + return bMixed || !localName.IsEmpty(); } NS_IMETHODIMP @@ -486,18 +460,12 @@ RemoveListCommand::GetCommandStateParams(const char* aCommandName, StaticRefPtr IndentCommand::sInstance; -NS_IMETHODIMP -IndentCommand::IsCommandEnabled(const char* aCommandName, nsISupports* refCon, - bool* outCmdEnabled) { - nsCOMPtr editor = do_QueryInterface(refCon); - if (!editor) { - *outCmdEnabled = false; - return NS_OK; +bool IndentCommand::IsCommandEnabled(const char* aCommandName, + TextEditor* aTextEditor) const { + if (!aTextEditor) { + return false; } - mozilla::EditorBase* editorBase = editor->AsEditorBase(); - MOZ_ASSERT(editorBase); - *outCmdEnabled = editorBase->IsSelectionEditable(); - return NS_OK; + return aTextEditor->IsSelectionEditable(); } NS_IMETHODIMP @@ -538,18 +506,12 @@ IndentCommand::GetCommandStateParams(const char* aCommandName, StaticRefPtr OutdentCommand::sInstance; -NS_IMETHODIMP -OutdentCommand::IsCommandEnabled(const char* aCommandName, nsISupports* refCon, - bool* outCmdEnabled) { - nsCOMPtr editor = do_QueryInterface(refCon); - if (!editor) { - *outCmdEnabled = false; - return NS_OK; +bool OutdentCommand::IsCommandEnabled(const char* aCommandName, + TextEditor* aTextEditor) const { + if (!aTextEditor) { + return false; } - mozilla::EditorBase* editorBase = editor->AsEditorBase(); - MOZ_ASSERT(editorBase); - *outCmdEnabled = editorBase->IsSelectionEditable(); - return NS_OK; + return aTextEditor->IsSelectionEditable(); } NS_IMETHODIMP @@ -589,20 +551,13 @@ OutdentCommand::GetCommandStateParams(const char* aCommandName, * mozilla::MultiStateCommandBase *****************************************************************************/ -NS_IMETHODIMP -MultiStateCommandBase::IsCommandEnabled(const char* aCommandName, - nsISupports* refCon, - bool* outCmdEnabled) { - nsCOMPtr editor = do_QueryInterface(refCon); - if (!editor) { - *outCmdEnabled = false; - return NS_OK; +bool MultiStateCommandBase::IsCommandEnabled(const char* aCommandName, + TextEditor* aTextEditor) const { + if (!aTextEditor) { + return false; } - mozilla::EditorBase* editorBase = editor->AsEditorBase(); - MOZ_ASSERT(editorBase); // should be disabled sometimes, like if the current selection is an image - *outCmdEnabled = editorBase->IsSelectionEditable(); - return NS_OK; + return aTextEditor->IsSelectionEditable(); } NS_IMETHODIMP @@ -1059,33 +1014,23 @@ nsresult AbsolutePositioningCommand::ToggleState(nsAtom* aTagName, StaticRefPtr DecreaseZIndexCommand::sInstance; -NS_IMETHODIMP -DecreaseZIndexCommand::IsCommandEnabled(const char* aCommandName, - nsISupports* aRefCon, - bool* aOutCmdEnabled) { - nsCOMPtr editor = do_QueryInterface(aRefCon); - if (NS_WARN_IF(!editor)) { - return NS_ERROR_FAILURE; +bool DecreaseZIndexCommand::IsCommandEnabled(const char* aCommandName, + TextEditor* aTextEditor) const { + if (!aTextEditor) { + return false; } - mozilla::HTMLEditor* htmlEditor = editor->AsHTMLEditor(); - if (NS_WARN_IF(!htmlEditor)) { - return NS_ERROR_FAILURE; + HTMLEditor* htmlEditor = aTextEditor->AsHTMLEditor(); + if (!htmlEditor) { + return false; } - if (!htmlEditor->IsAbsolutePositionEditorEnabled()) { - *aOutCmdEnabled = false; - return NS_OK; + return false; } - RefPtr positionedElement = htmlEditor->GetPositionedElement(); if (!positionedElement) { - *aOutCmdEnabled = false; - return NS_OK; + return false; } - - int32_t z = htmlEditor->GetZIndex(*positionedElement); - *aOutCmdEnabled = (z > 0); - return NS_OK; + return htmlEditor->GetZIndex(*positionedElement) > 0; } NS_IMETHODIMP @@ -1128,26 +1073,19 @@ DecreaseZIndexCommand::GetCommandStateParams(const char* aCommandName, StaticRefPtr IncreaseZIndexCommand::sInstance; -NS_IMETHODIMP -IncreaseZIndexCommand::IsCommandEnabled(const char* aCommandName, - nsISupports* aRefCon, - bool* aOutCmdEnabled) { - nsCOMPtr editor = do_QueryInterface(aRefCon); - if (NS_WARN_IF(!editor)) { - return NS_ERROR_FAILURE; +bool IncreaseZIndexCommand::IsCommandEnabled(const char* aCommandName, + TextEditor* aTextEditor) const { + if (!aTextEditor) { + return false; } - mozilla::HTMLEditor* htmlEditor = editor->AsHTMLEditor(); - if (NS_WARN_IF(!htmlEditor)) { - return NS_ERROR_FAILURE; + HTMLEditor* htmlEditor = aTextEditor->AsHTMLEditor(); + if (!htmlEditor) { + return false; } - if (!htmlEditor->IsAbsolutePositionEditorEnabled()) { - *aOutCmdEnabled = false; - return NS_OK; + return false; } - - *aOutCmdEnabled = !!htmlEditor->GetPositionedElement(); - return NS_OK; + return !!htmlEditor->GetPositionedElement(); } NS_IMETHODIMP @@ -1190,20 +1128,13 @@ IncreaseZIndexCommand::GetCommandStateParams(const char* aCommandName, StaticRefPtr RemoveStylesCommand::sInstance; -NS_IMETHODIMP -RemoveStylesCommand::IsCommandEnabled(const char* aCommandName, - nsISupports* refCon, - bool* outCmdEnabled) { - nsCOMPtr editor = do_QueryInterface(refCon); - if (!editor) { - *outCmdEnabled = false; - return NS_OK; +bool RemoveStylesCommand::IsCommandEnabled(const char* aCommandName, + TextEditor* aTextEditor) const { + if (!aTextEditor) { + return false; } - mozilla::EditorBase* editorBase = editor->AsEditorBase(); - MOZ_ASSERT(editorBase); // test if we have any styles? - *outCmdEnabled = editorBase->IsSelectionEditable(); - return NS_OK; + return aTextEditor->IsSelectionEditable(); } NS_IMETHODIMP @@ -1241,20 +1172,13 @@ RemoveStylesCommand::GetCommandStateParams(const char* aCommandName, StaticRefPtr IncreaseFontSizeCommand::sInstance; -NS_IMETHODIMP -IncreaseFontSizeCommand::IsCommandEnabled(const char* aCommandName, - nsISupports* refCon, - bool* outCmdEnabled) { - nsCOMPtr editor = do_QueryInterface(refCon); - if (!editor) { - *outCmdEnabled = false; - return NS_OK; +bool IncreaseFontSizeCommand::IsCommandEnabled(const char* aCommandName, + TextEditor* aTextEditor) const { + if (!aTextEditor) { + return false; } - mozilla::EditorBase* editorBase = editor->AsEditorBase(); - MOZ_ASSERT(editorBase); // test if we are at max size? - *outCmdEnabled = editorBase->IsSelectionEditable(); - return NS_OK; + return aTextEditor->IsSelectionEditable(); } NS_IMETHODIMP @@ -1293,20 +1217,13 @@ IncreaseFontSizeCommand::GetCommandStateParams(const char* aCommandName, StaticRefPtr DecreaseFontSizeCommand::sInstance; -NS_IMETHODIMP -DecreaseFontSizeCommand::IsCommandEnabled(const char* aCommandName, - nsISupports* refCon, - bool* outCmdEnabled) { - nsCOMPtr editor = do_QueryInterface(refCon); - if (!editor) { - *outCmdEnabled = false; - return NS_OK; +bool DecreaseFontSizeCommand::IsCommandEnabled(const char* aCommandName, + TextEditor* aTextEditor) const { + if (!aTextEditor) { + return false; } - mozilla::EditorBase* editorBase = editor->AsEditorBase(); - MOZ_ASSERT(editorBase); // test if we are at min size? - *outCmdEnabled = editorBase->IsSelectionEditable(); - return NS_OK; + return aTextEditor->IsSelectionEditable(); } NS_IMETHODIMP @@ -1345,19 +1262,12 @@ DecreaseFontSizeCommand::GetCommandStateParams(const char* aCommandName, StaticRefPtr InsertHTMLCommand::sInstance; -NS_IMETHODIMP -InsertHTMLCommand::IsCommandEnabled(const char* aCommandName, - nsISupports* refCon, bool* outCmdEnabled) { - NS_ENSURE_ARG_POINTER(outCmdEnabled); - nsCOMPtr editor = do_QueryInterface(refCon); - if (!editor) { - *outCmdEnabled = false; - return NS_OK; +bool InsertHTMLCommand::IsCommandEnabled(const char* aCommandName, + TextEditor* aTextEditor) const { + if (!aTextEditor) { + return false; } - mozilla::EditorBase* editorBase = editor->AsEditorBase(); - MOZ_ASSERT(editorBase); - *outCmdEnabled = editorBase->IsSelectionEditable(); - return NS_OK; + return aTextEditor->IsSelectionEditable(); } NS_IMETHODIMP @@ -1423,19 +1333,12 @@ InsertHTMLCommand::GetCommandStateParams(const char* aCommandName, StaticRefPtr InsertTagCommand::sInstance; nsRefPtrHashtable InsertTagCommand::sTagNameTable; -NS_IMETHODIMP -InsertTagCommand::IsCommandEnabled(const char* aCommandName, - nsISupports* refCon, bool* outCmdEnabled) { - NS_ENSURE_ARG_POINTER(outCmdEnabled); - nsCOMPtr editor = do_QueryInterface(refCon); - if (!editor) { - *outCmdEnabled = false; - return NS_OK; +bool InsertTagCommand::IsCommandEnabled(const char* aCommandName, + TextEditor* aTextEditor) const { + if (!aTextEditor) { + return false; } - mozilla::EditorBase* editorBase = editor->AsEditorBase(); - MOZ_ASSERT(editorBase); - *outCmdEnabled = editorBase->IsSelectionEditable(); - return NS_OK; + return aTextEditor->IsSelectionEditable(); } // corresponding STATE_ATTRIBUTE is: src (img) and href (a) diff --git a/editor/libeditor/HTMLEditorDocumentCommands.cpp b/editor/libeditor/HTMLEditorDocumentCommands.cpp index 9bb8fd9d4319..2680cd53b1f7 100644 --- a/editor/libeditor/HTMLEditorDocumentCommands.cpp +++ b/editor/libeditor/HTMLEditorDocumentCommands.cpp @@ -46,17 +46,10 @@ namespace mozilla { StaticRefPtr SetDocumentStateCommand::sInstance; -NS_IMETHODIMP -SetDocumentStateCommand::IsCommandEnabled(const char* aCommandName, - nsISupports* refCon, - bool* outCmdEnabled) { - if (NS_WARN_IF(!outCmdEnabled)) { - return NS_ERROR_INVALID_ARG; - } - +bool SetDocumentStateCommand::IsCommandEnabled(const char* aCommandName, + TextEditor* aTextEditor) const { // These commands are always enabled - *outCmdEnabled = true; - return NS_OK; + return true; } NS_IMETHODIMP @@ -448,17 +441,10 @@ SetDocumentStateCommand::GetCommandStateParams(const char* aCommandName, StaticRefPtr DocumentStateCommand::sInstance; -NS_IMETHODIMP -DocumentStateCommand::IsCommandEnabled(const char* aCommandName, - nsISupports* refCon, - bool* outCmdEnabled) { - if (NS_WARN_IF(!outCmdEnabled)) { - return NS_ERROR_INVALID_ARG; - } - +bool DocumentStateCommand::IsCommandEnabled(const char* aCommandName, + TextEditor* aTextEditor) const { // Always return false to discourage callers from using DoCommand() - *outCmdEnabled = false; - return NS_OK; + return false; } NS_IMETHODIMP