Bug 1546839 - part 4: Make all editor command classes use Command instead of command name r=smaug

`strcmp` may be slow due to checking null character.  Therefore, internal
methods of them should use command as far as possible.  This patch makes
them take/use `Command` instead of `const char*`.

Differential Revision: https://phabricator.services.mozilla.com/D29174

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Masayuki Nakano 2019-04-30 04:24:49 +00:00
Родитель a658371e04
Коммит 6da5cf38da
4 изменённых файлов: 655 добавлений и 647 удалений

Просмотреть файл

@ -41,7 +41,8 @@ EditorCommand::IsCommandEnabled(const char* aCommandName,
nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon); nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
TextEditor* textEditor = editor ? editor->AsTextEditor() : nullptr; TextEditor* textEditor = editor ? editor->AsTextEditor() : nullptr;
*aIsEnabled = IsCommandEnabled(aCommandName, MOZ_KnownLive(textEditor)); *aIsEnabled = IsCommandEnabled(GetInternalCommand(aCommandName),
MOZ_KnownLive(textEditor));
return NS_OK; return NS_OK;
} }
@ -55,7 +56,8 @@ EditorCommand::DoCommand(const char* aCommandName,
if (NS_WARN_IF(!editor)) { if (NS_WARN_IF(!editor)) {
return NS_ERROR_INVALID_ARG; return NS_ERROR_INVALID_ARG;
} }
nsresult rv = DoCommand(aCommandName, MOZ_KnownLive(*editor->AsTextEditor())); nsresult rv = DoCommand(GetInternalCommand(aCommandName),
MOZ_KnownLive(*editor->AsTextEditor()));
NS_WARNING_ASSERTION( NS_WARNING_ASSERTION(
NS_SUCCEEDED(rv), NS_SUCCEEDED(rv),
"Failed to do command from nsIControllerCommand::DoCommand()"); "Failed to do command from nsIControllerCommand::DoCommand()");
@ -73,9 +75,17 @@ EditorCommand::DoCommandParams(const char* aCommandName,
if (NS_WARN_IF(!editor)) { if (NS_WARN_IF(!editor)) {
return NS_ERROR_INVALID_ARG; return NS_ERROR_INVALID_ARG;
} }
nsresult rv = Command command;
DoCommandParams(aCommandName, MOZ_KnownLive(aParams->AsCommandParams()), nsCommandParams* params = aParams->AsCommandParams();
MOZ_KnownLive(*editor->AsTextEditor())); if (params) {
nsAutoString value;
params->GetString(aCommandName, value);
command = GetInternalCommand(aCommandName, value);
} else {
command = GetInternalCommand(aCommandName);
}
nsresult rv = DoCommandParams(command, MOZ_KnownLive(params),
MOZ_KnownLive(*editor->AsTextEditor()));
NS_WARNING_ASSERTION( NS_WARNING_ASSERTION(
NS_SUCCEEDED(rv), NS_SUCCEEDED(rv),
"Failed to do command from nsIControllerCommand::DoCommandParams()"); "Failed to do command from nsIControllerCommand::DoCommandParams()");
@ -91,18 +101,19 @@ EditorCommand::GetCommandStateParams(const char* aCommandName,
} }
nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon); nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
if (editor) { if (editor) {
return GetCommandStateParams( return GetCommandStateParams(GetInternalCommand(aCommandName),
aCommandName, MOZ_KnownLive(*aParams->AsCommandParams()), MOZ_KnownLive(*aParams->AsCommandParams()),
MOZ_KnownLive(editor->AsTextEditor()), nullptr); MOZ_KnownLive(editor->AsTextEditor()),
nullptr);
} }
nsCOMPtr<nsIEditingSession> editingSession = nsCOMPtr<nsIEditingSession> editingSession =
do_QueryInterface(aCommandRefCon); do_QueryInterface(aCommandRefCon);
if (editingSession) { if (editingSession) {
return GetCommandStateParams(aCommandName, return GetCommandStateParams(GetInternalCommand(aCommandName),
MOZ_KnownLive(*aParams->AsCommandParams()), MOZ_KnownLive(*aParams->AsCommandParams()),
nullptr, editingSession); nullptr, editingSession);
} }
return GetCommandStateParams(aCommandName, return GetCommandStateParams(GetInternalCommand(aCommandName),
MOZ_KnownLive(*aParams->AsCommandParams()), MOZ_KnownLive(*aParams->AsCommandParams()),
nullptr, nullptr); nullptr, nullptr);
} }
@ -113,7 +124,7 @@ EditorCommand::GetCommandStateParams(const char* aCommandName,
StaticRefPtr<UndoCommand> UndoCommand::sInstance; StaticRefPtr<UndoCommand> UndoCommand::sInstance;
bool UndoCommand::IsCommandEnabled(const char* aCommandName, bool UndoCommand::IsCommandEnabled(Command aCommand,
TextEditor* aTextEditor) const { TextEditor* aTextEditor) const {
if (!aTextEditor) { if (!aTextEditor) {
return false; return false;
@ -121,22 +132,22 @@ bool UndoCommand::IsCommandEnabled(const char* aCommandName,
return aTextEditor->IsSelectionEditable() && aTextEditor->CanUndo(); return aTextEditor->IsSelectionEditable() && aTextEditor->CanUndo();
} }
nsresult UndoCommand::DoCommand(const char* aCommandName, nsresult UndoCommand::DoCommand(Command aCommand,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
return aTextEditor.Undo(1); return aTextEditor.Undo(1);
} }
nsresult UndoCommand::DoCommandParams(const char* aCommandName, nsresult UndoCommand::DoCommandParams(Command aCommand,
nsCommandParams* aParams, nsCommandParams* aParams,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
return DoCommand(aCommandName, aTextEditor); return DoCommand(aCommand, aTextEditor);
} }
nsresult UndoCommand::GetCommandStateParams( nsresult UndoCommand::GetCommandStateParams(
const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor, Command aCommand, nsCommandParams& aParams, TextEditor* aTextEditor,
nsIEditingSession* aEditingSession) const { nsIEditingSession* aEditingSession) const {
return aParams.SetBool(STATE_ENABLED, return aParams.SetBool(STATE_ENABLED,
IsCommandEnabled(aCommandName, aTextEditor)); IsCommandEnabled(aCommand, aTextEditor));
} }
/****************************************************************************** /******************************************************************************
@ -145,7 +156,7 @@ nsresult UndoCommand::GetCommandStateParams(
StaticRefPtr<RedoCommand> RedoCommand::sInstance; StaticRefPtr<RedoCommand> RedoCommand::sInstance;
bool RedoCommand::IsCommandEnabled(const char* aCommandName, bool RedoCommand::IsCommandEnabled(Command aCommand,
TextEditor* aTextEditor) const { TextEditor* aTextEditor) const {
if (!aTextEditor) { if (!aTextEditor) {
return false; return false;
@ -153,22 +164,22 @@ bool RedoCommand::IsCommandEnabled(const char* aCommandName,
return aTextEditor->IsSelectionEditable() && aTextEditor->CanRedo(); return aTextEditor->IsSelectionEditable() && aTextEditor->CanRedo();
} }
nsresult RedoCommand::DoCommand(const char* aCommandName, nsresult RedoCommand::DoCommand(Command aCommand,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
return aTextEditor.Redo(1); return aTextEditor.Redo(1);
} }
nsresult RedoCommand::DoCommandParams(const char* aCommandName, nsresult RedoCommand::DoCommandParams(Command aCommand,
nsCommandParams* aParams, nsCommandParams* aParams,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
return DoCommand(aCommandName, aTextEditor); return DoCommand(aCommand, aTextEditor);
} }
nsresult RedoCommand::GetCommandStateParams( nsresult RedoCommand::GetCommandStateParams(
const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor, Command aCommand, nsCommandParams& aParams, TextEditor* aTextEditor,
nsIEditingSession* aEditingSession) const { nsIEditingSession* aEditingSession) const {
return aParams.SetBool(STATE_ENABLED, return aParams.SetBool(STATE_ENABLED,
IsCommandEnabled(aCommandName, aTextEditor)); IsCommandEnabled(aCommand, aTextEditor));
} }
/****************************************************************************** /******************************************************************************
@ -177,7 +188,7 @@ nsresult RedoCommand::GetCommandStateParams(
StaticRefPtr<CutCommand> CutCommand::sInstance; StaticRefPtr<CutCommand> CutCommand::sInstance;
bool CutCommand::IsCommandEnabled(const char* aCommandName, bool CutCommand::IsCommandEnabled(Command aCommand,
TextEditor* aTextEditor) const { TextEditor* aTextEditor) const {
if (!aTextEditor) { if (!aTextEditor) {
return false; return false;
@ -185,22 +196,21 @@ bool CutCommand::IsCommandEnabled(const char* aCommandName,
return aTextEditor->IsSelectionEditable() && aTextEditor->CanCut(); return aTextEditor->IsSelectionEditable() && aTextEditor->CanCut();
} }
nsresult CutCommand::DoCommand(const char* aCommandName, nsresult CutCommand::DoCommand(Command aCommand,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
return aTextEditor.Cut(); return aTextEditor.Cut();
} }
nsresult CutCommand::DoCommandParams(const char* aCommandName, nsresult CutCommand::DoCommandParams(Command aCommand, nsCommandParams* aParams,
nsCommandParams* aParams,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
return DoCommand(aCommandName, aTextEditor); return DoCommand(aCommand, aTextEditor);
} }
nsresult CutCommand::GetCommandStateParams( nsresult CutCommand::GetCommandStateParams(
const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor, Command aCommand, nsCommandParams& aParams, TextEditor* aTextEditor,
nsIEditingSession* aEditingSession) const { nsIEditingSession* aEditingSession) const {
return aParams.SetBool(STATE_ENABLED, return aParams.SetBool(STATE_ENABLED,
IsCommandEnabled(aCommandName, aTextEditor)); IsCommandEnabled(aCommand, aTextEditor));
} }
/****************************************************************************** /******************************************************************************
@ -209,7 +219,7 @@ nsresult CutCommand::GetCommandStateParams(
StaticRefPtr<CutOrDeleteCommand> CutOrDeleteCommand::sInstance; StaticRefPtr<CutOrDeleteCommand> CutOrDeleteCommand::sInstance;
bool CutOrDeleteCommand::IsCommandEnabled(const char* aCommandName, bool CutOrDeleteCommand::IsCommandEnabled(Command aCommand,
TextEditor* aTextEditor) const { TextEditor* aTextEditor) const {
if (!aTextEditor) { if (!aTextEditor) {
return false; return false;
@ -217,7 +227,7 @@ bool CutOrDeleteCommand::IsCommandEnabled(const char* aCommandName,
return aTextEditor->IsSelectionEditable(); return aTextEditor->IsSelectionEditable();
} }
nsresult CutOrDeleteCommand::DoCommand(const char* aCommandName, nsresult CutOrDeleteCommand::DoCommand(Command aCommand,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
dom::Selection* selection = aTextEditor.GetSelection(); dom::Selection* selection = aTextEditor.GetSelection();
if (selection && selection->IsCollapsed()) { if (selection && selection->IsCollapsed()) {
@ -231,17 +241,17 @@ nsresult CutOrDeleteCommand::DoCommand(const char* aCommandName,
return aTextEditor.Cut(); return aTextEditor.Cut();
} }
nsresult CutOrDeleteCommand::DoCommandParams(const char* aCommandName, nsresult CutOrDeleteCommand::DoCommandParams(Command aCommand,
nsCommandParams* aParams, nsCommandParams* aParams,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
return DoCommand(aCommandName, aTextEditor); return DoCommand(aCommand, aTextEditor);
} }
nsresult CutOrDeleteCommand::GetCommandStateParams( nsresult CutOrDeleteCommand::GetCommandStateParams(
const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor, Command aCommand, nsCommandParams& aParams, TextEditor* aTextEditor,
nsIEditingSession* aEditingSession) const { nsIEditingSession* aEditingSession) const {
return aParams.SetBool(STATE_ENABLED, return aParams.SetBool(STATE_ENABLED,
IsCommandEnabled(aCommandName, aTextEditor)); IsCommandEnabled(aCommand, aTextEditor));
} }
/****************************************************************************** /******************************************************************************
@ -250,7 +260,7 @@ nsresult CutOrDeleteCommand::GetCommandStateParams(
StaticRefPtr<CopyCommand> CopyCommand::sInstance; StaticRefPtr<CopyCommand> CopyCommand::sInstance;
bool CopyCommand::IsCommandEnabled(const char* aCommandName, bool CopyCommand::IsCommandEnabled(Command aCommand,
TextEditor* aTextEditor) const { TextEditor* aTextEditor) const {
if (!aTextEditor) { if (!aTextEditor) {
return false; return false;
@ -258,22 +268,22 @@ bool CopyCommand::IsCommandEnabled(const char* aCommandName,
return aTextEditor->CanCopy(); return aTextEditor->CanCopy();
} }
nsresult CopyCommand::DoCommand(const char* aCommandName, nsresult CopyCommand::DoCommand(Command aCommand,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
return aTextEditor.Copy(); return aTextEditor.Copy();
} }
nsresult CopyCommand::DoCommandParams(const char* aCommandName, nsresult CopyCommand::DoCommandParams(Command aCommand,
nsCommandParams* aParams, nsCommandParams* aParams,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
return DoCommand(aCommandName, aTextEditor); return DoCommand(aCommand, aTextEditor);
} }
nsresult CopyCommand::GetCommandStateParams( nsresult CopyCommand::GetCommandStateParams(
const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor, Command aCommand, nsCommandParams& aParams, TextEditor* aTextEditor,
nsIEditingSession* aEditingSession) const { nsIEditingSession* aEditingSession) const {
return aParams.SetBool(STATE_ENABLED, return aParams.SetBool(STATE_ENABLED,
IsCommandEnabled(aCommandName, aTextEditor)); IsCommandEnabled(aCommand, aTextEditor));
} }
/****************************************************************************** /******************************************************************************
@ -282,7 +292,7 @@ nsresult CopyCommand::GetCommandStateParams(
StaticRefPtr<CopyOrDeleteCommand> CopyOrDeleteCommand::sInstance; StaticRefPtr<CopyOrDeleteCommand> CopyOrDeleteCommand::sInstance;
bool CopyOrDeleteCommand::IsCommandEnabled(const char* aCommandName, bool CopyOrDeleteCommand::IsCommandEnabled(Command aCommand,
TextEditor* aTextEditor) const { TextEditor* aTextEditor) const {
if (!aTextEditor) { if (!aTextEditor) {
return false; return false;
@ -290,7 +300,7 @@ bool CopyOrDeleteCommand::IsCommandEnabled(const char* aCommandName,
return aTextEditor->IsSelectionEditable(); return aTextEditor->IsSelectionEditable();
} }
nsresult CopyOrDeleteCommand::DoCommand(const char* aCommandName, nsresult CopyOrDeleteCommand::DoCommand(Command aCommand,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
dom::Selection* selection = aTextEditor.GetSelection(); dom::Selection* selection = aTextEditor.GetSelection();
if (selection && selection->IsCollapsed()) { if (selection && selection->IsCollapsed()) {
@ -304,17 +314,17 @@ nsresult CopyOrDeleteCommand::DoCommand(const char* aCommandName,
return aTextEditor.Copy(); return aTextEditor.Copy();
} }
nsresult CopyOrDeleteCommand::DoCommandParams(const char* aCommandName, nsresult CopyOrDeleteCommand::DoCommandParams(Command aCommand,
nsCommandParams* aParams, nsCommandParams* aParams,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
return DoCommand(aCommandName, aTextEditor); return DoCommand(aCommand, aTextEditor);
} }
nsresult CopyOrDeleteCommand::GetCommandStateParams( nsresult CopyOrDeleteCommand::GetCommandStateParams(
const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor, Command aCommand, nsCommandParams& aParams, TextEditor* aTextEditor,
nsIEditingSession* aEditingSession) const { nsIEditingSession* aEditingSession) const {
return aParams.SetBool(STATE_ENABLED, return aParams.SetBool(STATE_ENABLED,
IsCommandEnabled(aCommandName, aTextEditor)); IsCommandEnabled(aCommand, aTextEditor));
} }
/****************************************************************************** /******************************************************************************
@ -323,7 +333,7 @@ nsresult CopyOrDeleteCommand::GetCommandStateParams(
StaticRefPtr<PasteCommand> PasteCommand::sInstance; StaticRefPtr<PasteCommand> PasteCommand::sInstance;
bool PasteCommand::IsCommandEnabled(const char* aCommandName, bool PasteCommand::IsCommandEnabled(Command aCommand,
TextEditor* aTextEditor) const { TextEditor* aTextEditor) const {
if (!aTextEditor) { if (!aTextEditor) {
return false; return false;
@ -332,22 +342,22 @@ bool PasteCommand::IsCommandEnabled(const char* aCommandName,
aTextEditor->CanPaste(nsIClipboard::kGlobalClipboard); aTextEditor->CanPaste(nsIClipboard::kGlobalClipboard);
} }
nsresult PasteCommand::DoCommand(const char* aCommandName, nsresult PasteCommand::DoCommand(Command aCommand,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
return aTextEditor.PasteAsAction(nsIClipboard::kGlobalClipboard, true); return aTextEditor.PasteAsAction(nsIClipboard::kGlobalClipboard, true);
} }
nsresult PasteCommand::DoCommandParams(const char* aCommandName, nsresult PasteCommand::DoCommandParams(Command aCommand,
nsCommandParams* aParams, nsCommandParams* aParams,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
return DoCommand(aCommandName, aTextEditor); return DoCommand(aCommand, aTextEditor);
} }
nsresult PasteCommand::GetCommandStateParams( nsresult PasteCommand::GetCommandStateParams(
const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor, Command aCommand, nsCommandParams& aParams, TextEditor* aTextEditor,
nsIEditingSession* aEditingSession) const { nsIEditingSession* aEditingSession) const {
return aParams.SetBool(STATE_ENABLED, return aParams.SetBool(STATE_ENABLED,
IsCommandEnabled(aCommandName, aTextEditor)); IsCommandEnabled(aCommand, aTextEditor));
} }
/****************************************************************************** /******************************************************************************
@ -356,7 +366,7 @@ nsresult PasteCommand::GetCommandStateParams(
StaticRefPtr<PasteTransferableCommand> PasteTransferableCommand::sInstance; StaticRefPtr<PasteTransferableCommand> PasteTransferableCommand::sInstance;
bool PasteTransferableCommand::IsCommandEnabled(const char* aCommandName, bool PasteTransferableCommand::IsCommandEnabled(Command aCommand,
TextEditor* aTextEditor) const { TextEditor* aTextEditor) const {
if (!aTextEditor) { if (!aTextEditor) {
return false; return false;
@ -365,14 +375,13 @@ bool PasteTransferableCommand::IsCommandEnabled(const char* aCommandName,
aTextEditor->CanPasteTransferable(nullptr); aTextEditor->CanPasteTransferable(nullptr);
} }
nsresult PasteTransferableCommand::DoCommand(const char* aCommandName, nsresult PasteTransferableCommand::DoCommand(Command aCommand,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;
} }
nsresult PasteTransferableCommand::DoCommandParams( nsresult PasteTransferableCommand::DoCommandParams(
const char* aCommandName, nsCommandParams* aParams, Command aCommand, nsCommandParams* aParams, TextEditor& aTextEditor) const {
TextEditor& aTextEditor) const {
if (NS_WARN_IF(!aParams)) { if (NS_WARN_IF(!aParams)) {
return NS_ERROR_INVALID_ARG; return NS_ERROR_INVALID_ARG;
} }
@ -397,7 +406,7 @@ nsresult PasteTransferableCommand::DoCommandParams(
} }
nsresult PasteTransferableCommand::GetCommandStateParams( nsresult PasteTransferableCommand::GetCommandStateParams(
const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor, Command aCommand, nsCommandParams& aParams, TextEditor* aTextEditor,
nsIEditingSession* aEditingSession) const { nsIEditingSession* aEditingSession) const {
if (NS_WARN_IF(!aTextEditor)) { if (NS_WARN_IF(!aTextEditor)) {
return NS_ERROR_INVALID_ARG; return NS_ERROR_INVALID_ARG;
@ -425,29 +434,28 @@ nsresult PasteTransferableCommand::GetCommandStateParams(
StaticRefPtr<SwitchTextDirectionCommand> SwitchTextDirectionCommand::sInstance; StaticRefPtr<SwitchTextDirectionCommand> SwitchTextDirectionCommand::sInstance;
bool SwitchTextDirectionCommand::IsCommandEnabled( bool SwitchTextDirectionCommand::IsCommandEnabled(
const char* aCommandName, TextEditor* aTextEditor) const { Command aCommand, TextEditor* aTextEditor) const {
if (!aTextEditor) { if (!aTextEditor) {
return false; return false;
} }
return aTextEditor->IsSelectionEditable(); return aTextEditor->IsSelectionEditable();
} }
nsresult SwitchTextDirectionCommand::DoCommand(const char* aCommandName, nsresult SwitchTextDirectionCommand::DoCommand(Command aCommand,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
return aTextEditor.ToggleTextDirection(); return aTextEditor.ToggleTextDirection();
} }
nsresult SwitchTextDirectionCommand::DoCommandParams( nsresult SwitchTextDirectionCommand::DoCommandParams(
const char* aCommandName, nsCommandParams* aParams, Command aCommand, nsCommandParams* aParams, TextEditor& aTextEditor) const {
TextEditor& aTextEditor) const { return DoCommand(aCommand, aTextEditor);
return DoCommand(aCommandName, aTextEditor);
} }
nsresult SwitchTextDirectionCommand::GetCommandStateParams( nsresult SwitchTextDirectionCommand::GetCommandStateParams(
const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor, Command aCommand, nsCommandParams& aParams, TextEditor* aTextEditor,
nsIEditingSession* aEditingSession) const { nsIEditingSession* aEditingSession) const {
return aParams.SetBool(STATE_ENABLED, return aParams.SetBool(STATE_ENABLED,
IsCommandEnabled(aCommandName, aTextEditor)); IsCommandEnabled(aCommand, aTextEditor));
} }
/****************************************************************************** /******************************************************************************
@ -456,7 +464,7 @@ nsresult SwitchTextDirectionCommand::GetCommandStateParams(
StaticRefPtr<DeleteCommand> DeleteCommand::sInstance; StaticRefPtr<DeleteCommand> DeleteCommand::sInstance;
bool DeleteCommand::IsCommandEnabled(const char* aCommandName, bool DeleteCommand::IsCommandEnabled(Command aCommand,
TextEditor* aTextEditor) const { TextEditor* aTextEditor) const {
if (!aTextEditor) { if (!aTextEditor) {
return false; return false;
@ -466,34 +474,42 @@ bool DeleteCommand::IsCommandEnabled(const char* aCommandName,
// directionless, which is the same condition under which we can't cut. // directionless, which is the same condition under which we can't cut.
bool isEnabled = aTextEditor->IsSelectionEditable(); bool isEnabled = aTextEditor->IsSelectionEditable();
if (!nsCRT::strcmp("cmd_delete", aCommandName) && isEnabled) { if (aCommand == Command::Delete && isEnabled) {
return aTextEditor->CanDelete(); return aTextEditor->CanDelete();
} }
return isEnabled; return isEnabled;
} }
nsresult DeleteCommand::DoCommand(const char* aCommandName, nsresult DeleteCommand::DoCommand(Command aCommand,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
nsIEditor::EDirection deleteDir = nsIEditor::eNone; nsIEditor::EDirection deleteDir = nsIEditor::eNone;
if (!strcmp("cmd_delete", aCommandName)) { switch (aCommand) {
// Really this should probably be eNone, but it only makes a difference if case Command::Delete:
// the selection is collapsed, and then this command is disabled. So let's // Really this should probably be eNone, but it only makes a difference
// keep it as it always was to avoid breaking things. // if the selection is collapsed, and then this command is disabled. So
deleteDir = nsIEditor::ePrevious; // let's keep it as it always was to avoid breaking things.
} else if (!strcmp("cmd_deleteCharForward", aCommandName)) { deleteDir = nsIEditor::ePrevious;
deleteDir = nsIEditor::eNext; break;
} else if (!strcmp("cmd_deleteCharBackward", aCommandName)) { case Command::DeleteCharForward:
deleteDir = nsIEditor::ePrevious; deleteDir = nsIEditor::eNext;
} else if (!strcmp("cmd_deleteWordBackward", aCommandName)) { break;
deleteDir = nsIEditor::ePreviousWord; case Command::DeleteCharBackward:
} else if (!strcmp("cmd_deleteWordForward", aCommandName)) { deleteDir = nsIEditor::ePrevious;
deleteDir = nsIEditor::eNextWord; break;
} else if (!strcmp("cmd_deleteToBeginningOfLine", aCommandName)) { case Command::DeleteWordBackward:
deleteDir = nsIEditor::eToBeginningOfLine; deleteDir = nsIEditor::ePreviousWord;
} else if (!strcmp("cmd_deleteToEndOfLine", aCommandName)) { break;
deleteDir = nsIEditor::eToEndOfLine; case Command::DeleteWordForward:
} else { deleteDir = nsIEditor::eNextWord;
MOZ_CRASH("Unrecognized nsDeleteCommand"); break;
case Command::DeleteToBeginningOfLine:
deleteDir = nsIEditor::eToBeginningOfLine;
break;
case Command::DeleteToEndOfLine:
deleteDir = nsIEditor::eToEndOfLine;
break;
default:
MOZ_CRASH("Unrecognized nsDeleteCommand");
} }
nsresult rv = nsresult rv =
aTextEditor.DeleteSelectionAsAction(deleteDir, nsIEditor::eStrip); aTextEditor.DeleteSelectionAsAction(deleteDir, nsIEditor::eStrip);
@ -503,17 +519,17 @@ nsresult DeleteCommand::DoCommand(const char* aCommandName,
return NS_OK; return NS_OK;
} }
nsresult DeleteCommand::DoCommandParams(const char* aCommandName, nsresult DeleteCommand::DoCommandParams(Command aCommand,
nsCommandParams* aParams, nsCommandParams* aParams,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
return DoCommand(aCommandName, aTextEditor); return DoCommand(aCommand, aTextEditor);
} }
nsresult DeleteCommand::GetCommandStateParams( nsresult DeleteCommand::GetCommandStateParams(
const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor, Command aCommand, nsCommandParams& aParams, TextEditor* aTextEditor,
nsIEditingSession* aEditingSession) const { nsIEditingSession* aEditingSession) const {
return aParams.SetBool(STATE_ENABLED, return aParams.SetBool(STATE_ENABLED,
IsCommandEnabled(aCommandName, aTextEditor)); IsCommandEnabled(aCommand, aTextEditor));
} }
/****************************************************************************** /******************************************************************************
@ -522,7 +538,7 @@ nsresult DeleteCommand::GetCommandStateParams(
StaticRefPtr<SelectAllCommand> SelectAllCommand::sInstance; StaticRefPtr<SelectAllCommand> SelectAllCommand::sInstance;
bool SelectAllCommand::IsCommandEnabled(const char* aCommandName, bool SelectAllCommand::IsCommandEnabled(Command aCommand,
TextEditor* aTextEditor) const { TextEditor* aTextEditor) const {
// You can always select all, unless the selection is editable, // You can always select all, unless the selection is editable,
// and the editable region is empty! // and the editable region is empty!
@ -538,22 +554,22 @@ bool SelectAllCommand::IsCommandEnabled(const char* aCommandName,
return !isEmpty; return !isEmpty;
} }
nsresult SelectAllCommand::DoCommand(const char* aCommandName, nsresult SelectAllCommand::DoCommand(Command aCommand,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
return aTextEditor.SelectAll(); return aTextEditor.SelectAll();
} }
nsresult SelectAllCommand::DoCommandParams(const char* aCommandName, nsresult SelectAllCommand::DoCommandParams(Command aCommand,
nsCommandParams* aParams, nsCommandParams* aParams,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
return DoCommand(aCommandName, aTextEditor); return DoCommand(aCommand, aTextEditor);
} }
nsresult SelectAllCommand::GetCommandStateParams( nsresult SelectAllCommand::GetCommandStateParams(
const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor, Command aCommand, nsCommandParams& aParams, TextEditor* aTextEditor,
nsIEditingSession* aEditingSession) const { nsIEditingSession* aEditingSession) const {
return aParams.SetBool(STATE_ENABLED, return aParams.SetBool(STATE_ENABLED,
IsCommandEnabled(aCommandName, aTextEditor)); IsCommandEnabled(aCommand, aTextEditor));
} }
/****************************************************************************** /******************************************************************************
@ -562,7 +578,7 @@ nsresult SelectAllCommand::GetCommandStateParams(
StaticRefPtr<SelectionMoveCommands> SelectionMoveCommands::sInstance; StaticRefPtr<SelectionMoveCommands> SelectionMoveCommands::sInstance;
bool SelectionMoveCommands::IsCommandEnabled(const char* aCommandName, bool SelectionMoveCommands::IsCommandEnabled(Command aCommand,
TextEditor* aTextEditor) const { TextEditor* aTextEditor) const {
if (!aTextEditor) { if (!aTextEditor) {
return false; return false;
@ -571,53 +587,58 @@ bool SelectionMoveCommands::IsCommandEnabled(const char* aCommandName,
} }
static const struct ScrollCommand { static const struct ScrollCommand {
const char* reverseScroll; Command mReverseScroll;
const char* forwardScroll; Command mForwardScroll;
nsresult (NS_STDCALL nsISelectionController::*scroll)(bool); nsresult (NS_STDCALL nsISelectionController::*scroll)(bool);
} scrollCommands[] = {{"cmd_scrollTop", "cmd_scrollBottom", } scrollCommands[] = {{Command::ScrollTop, Command::ScrollBottom,
&nsISelectionController::CompleteScroll}, &nsISelectionController::CompleteScroll},
{"cmd_scrollPageUp", "cmd_scrollPageDown", {Command::ScrollPageUp, Command::ScrollPageDown,
&nsISelectionController::ScrollPage}, &nsISelectionController::ScrollPage},
{"cmd_scrollLineUp", "cmd_scrollLineDown", {Command::ScrollLineUp, Command::ScrollLineDown,
&nsISelectionController::ScrollLine}}; &nsISelectionController::ScrollLine}};
static const struct MoveCommand { static const struct MoveCommand {
const char* reverseMove; Command mReverseMove;
const char* forwardMove; Command mForwardMove;
const char* reverseSelect; Command mReverseSelect;
const char* forwardSelect; Command mForwardSelect;
nsresult (NS_STDCALL nsISelectionController::*move)(bool, bool); nsresult (NS_STDCALL nsISelectionController::*move)(bool, bool);
} moveCommands[] = { } moveCommands[] = {
{"cmd_charPrevious", "cmd_charNext", "cmd_selectCharPrevious", {Command::CharPrevious, Command::CharNext, Command::SelectCharPrevious,
"cmd_selectCharNext", &nsISelectionController::CharacterMove}, Command::SelectCharNext, &nsISelectionController::CharacterMove},
{"cmd_linePrevious", "cmd_lineNext", "cmd_selectLinePrevious", {Command::LinePrevious, Command::LineNext, Command::SelectLinePrevious,
"cmd_selectLineNext", &nsISelectionController::LineMove}, Command::SelectLineNext, &nsISelectionController::LineMove},
{"cmd_wordPrevious", "cmd_wordNext", "cmd_selectWordPrevious", {Command::WordPrevious, Command::WordNext, Command::SelectWordPrevious,
"cmd_selectWordNext", &nsISelectionController::WordMove}, Command::SelectWordNext, &nsISelectionController::WordMove},
{"cmd_beginLine", "cmd_endLine", "cmd_selectBeginLine", "cmd_selectEndLine", {Command::BeginLine, Command::EndLine, Command::SelectBeginLine,
&nsISelectionController::IntraLineMove}, Command::SelectEndLine, &nsISelectionController::IntraLineMove},
{"cmd_movePageUp", "cmd_movePageDown", "cmd_selectPageUp", {Command::MovePageUp, Command::MovePageDown, Command::SelectPageUp,
"cmd_selectPageDown", &nsISelectionController::PageMove}, Command::SelectPageDown, &nsISelectionController::PageMove},
{"cmd_moveTop", "cmd_moveBottom", "cmd_selectTop", "cmd_selectBottom", {Command::MoveTop, Command::MoveBottom, Command::SelectTop,
&nsISelectionController::CompleteMove}}; Command::SelectBottom, &nsISelectionController::CompleteMove}};
static const struct PhysicalCommand { static const struct PhysicalCommand {
const char* move; Command mMove;
const char* select; Command mSelect;
int16_t direction; int16_t direction;
int16_t amount; int16_t amount;
} physicalCommands[] = { } physicalCommands[] = {
{"cmd_moveLeft", "cmd_selectLeft", nsISelectionController::MOVE_LEFT, 0}, {Command::MoveLeft, Command::SelectLeft, nsISelectionController::MOVE_LEFT,
{"cmd_moveRight", "cmd_selectRight", nsISelectionController::MOVE_RIGHT, 0}, 0},
{"cmd_moveUp", "cmd_selectUp", nsISelectionController::MOVE_UP, 0}, {Command::MoveRight, Command::SelectRight,
{"cmd_moveDown", "cmd_selectDown", nsISelectionController::MOVE_DOWN, 0}, nsISelectionController::MOVE_RIGHT, 0},
{"cmd_moveLeft2", "cmd_selectLeft2", nsISelectionController::MOVE_LEFT, 1}, {Command::MoveUp, Command::SelectUp, nsISelectionController::MOVE_UP, 0},
{"cmd_moveRight2", "cmd_selectRight2", nsISelectionController::MOVE_RIGHT, {Command::MoveDown, Command::SelectDown, nsISelectionController::MOVE_DOWN,
1}, 0},
{"cmd_moveUp2", "cmd_selectUp2", nsISelectionController::MOVE_UP, 1}, {Command::MoveLeft2, Command::SelectLeft2,
{"cmd_moveDown2", "cmd_selectDown2", nsISelectionController::MOVE_DOWN, 1}}; nsISelectionController::MOVE_LEFT, 1},
{Command::MoveRight2, Command::SelectRight2,
nsISelectionController::MOVE_RIGHT, 1},
{Command::MoveUp2, Command::SelectUp2, nsISelectionController::MOVE_UP, 1},
{Command::MoveDown2, Command::SelectDown2,
nsISelectionController::MOVE_DOWN, 1}};
nsresult SelectionMoveCommands::DoCommand(const char* aCommandName, nsresult SelectionMoveCommands::DoCommand(Command aCommand,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
RefPtr<Document> document = aTextEditor.GetDocument(); RefPtr<Document> document = aTextEditor.GetDocument();
if (document) { if (document) {
@ -635,9 +656,10 @@ nsresult SelectionMoveCommands::DoCommand(const char* aCommandName,
// scroll commands // scroll commands
for (size_t i = 0; i < ArrayLength(scrollCommands); i++) { for (size_t i = 0; i < ArrayLength(scrollCommands); i++) {
const ScrollCommand& cmd = scrollCommands[i]; const ScrollCommand& cmd = scrollCommands[i];
if (!strcmp(aCommandName, cmd.reverseScroll)) { if (aCommand == cmd.mReverseScroll) {
return (selectionController->*(cmd.scroll))(false); return (selectionController->*(cmd.scroll))(false);
} else if (!strcmp(aCommandName, cmd.forwardScroll)) { }
if (aCommand == cmd.mForwardScroll) {
return (selectionController->*(cmd.scroll))(true); return (selectionController->*(cmd.scroll))(true);
} }
} }
@ -645,13 +667,16 @@ nsresult SelectionMoveCommands::DoCommand(const char* aCommandName,
// caret movement/selection commands // caret movement/selection commands
for (size_t i = 0; i < ArrayLength(moveCommands); i++) { for (size_t i = 0; i < ArrayLength(moveCommands); i++) {
const MoveCommand& cmd = moveCommands[i]; const MoveCommand& cmd = moveCommands[i];
if (!strcmp(aCommandName, cmd.reverseMove)) { if (aCommand == cmd.mReverseMove) {
return (selectionController->*(cmd.move))(false, false); return (selectionController->*(cmd.move))(false, false);
} else if (!strcmp(aCommandName, cmd.forwardMove)) { }
if (aCommand == cmd.mForwardMove) {
return (selectionController->*(cmd.move))(true, false); return (selectionController->*(cmd.move))(true, false);
} else if (!strcmp(aCommandName, cmd.reverseSelect)) { }
if (aCommand == cmd.mReverseSelect) {
return (selectionController->*(cmd.move))(false, true); return (selectionController->*(cmd.move))(false, true);
} else if (!strcmp(aCommandName, cmd.forwardSelect)) { }
if (aCommand == cmd.mForwardSelect) {
return (selectionController->*(cmd.move))(true, true); return (selectionController->*(cmd.move))(true, true);
} }
} }
@ -659,10 +684,11 @@ nsresult SelectionMoveCommands::DoCommand(const char* aCommandName,
// physical-direction movement/selection // physical-direction movement/selection
for (size_t i = 0; i < ArrayLength(physicalCommands); i++) { for (size_t i = 0; i < ArrayLength(physicalCommands); i++) {
const PhysicalCommand& cmd = physicalCommands[i]; const PhysicalCommand& cmd = physicalCommands[i];
if (!strcmp(aCommandName, cmd.move)) { if (aCommand == cmd.mMove) {
return selectionController->PhysicalMove(cmd.direction, cmd.amount, return selectionController->PhysicalMove(cmd.direction, cmd.amount,
false); false);
} else if (!strcmp(aCommandName, cmd.select)) { }
if (aCommand == cmd.mSelect) {
return selectionController->PhysicalMove(cmd.direction, cmd.amount, true); return selectionController->PhysicalMove(cmd.direction, cmd.amount, true);
} }
} }
@ -670,17 +696,17 @@ nsresult SelectionMoveCommands::DoCommand(const char* aCommandName,
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;
} }
nsresult SelectionMoveCommands::DoCommandParams(const char* aCommandName, nsresult SelectionMoveCommands::DoCommandParams(Command aCommand,
nsCommandParams* aParams, nsCommandParams* aParams,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
return DoCommand(aCommandName, aTextEditor); return DoCommand(aCommand, aTextEditor);
} }
nsresult SelectionMoveCommands::GetCommandStateParams( nsresult SelectionMoveCommands::GetCommandStateParams(
const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor, Command aCommand, nsCommandParams& aParams, TextEditor* aTextEditor,
nsIEditingSession* aEditingSession) const { nsIEditingSession* aEditingSession) const {
return aParams.SetBool(STATE_ENABLED, return aParams.SetBool(STATE_ENABLED,
IsCommandEnabled(aCommandName, aTextEditor)); IsCommandEnabled(aCommand, aTextEditor));
} }
/****************************************************************************** /******************************************************************************
@ -689,7 +715,7 @@ nsresult SelectionMoveCommands::GetCommandStateParams(
StaticRefPtr<InsertPlaintextCommand> InsertPlaintextCommand::sInstance; StaticRefPtr<InsertPlaintextCommand> InsertPlaintextCommand::sInstance;
bool InsertPlaintextCommand::IsCommandEnabled(const char* aCommandName, bool InsertPlaintextCommand::IsCommandEnabled(Command aCommand,
TextEditor* aTextEditor) const { TextEditor* aTextEditor) const {
if (!aTextEditor) { if (!aTextEditor) {
return false; return false;
@ -697,7 +723,7 @@ bool InsertPlaintextCommand::IsCommandEnabled(const char* aCommandName,
return aTextEditor->IsSelectionEditable(); return aTextEditor->IsSelectionEditable();
} }
nsresult InsertPlaintextCommand::DoCommand(const char* aCommandName, nsresult InsertPlaintextCommand::DoCommand(Command aCommand,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
// XXX InsertTextAsAction() is not same as OnInputText(). However, other // XXX InsertTextAsAction() is not same as OnInputText(). However, other
// commands to insert line break or paragraph separator use OnInput*(). // commands to insert line break or paragraph separator use OnInput*().
@ -711,8 +737,7 @@ nsresult InsertPlaintextCommand::DoCommand(const char* aCommandName,
} }
nsresult InsertPlaintextCommand::DoCommandParams( nsresult InsertPlaintextCommand::DoCommandParams(
const char* aCommandName, nsCommandParams* aParams, Command aCommand, nsCommandParams* aParams, TextEditor& aTextEditor) const {
TextEditor& aTextEditor) const {
if (NS_WARN_IF(!aParams)) { if (NS_WARN_IF(!aParams)) {
return NS_ERROR_INVALID_ARG; return NS_ERROR_INVALID_ARG;
} }
@ -736,10 +761,10 @@ nsresult InsertPlaintextCommand::DoCommandParams(
} }
nsresult InsertPlaintextCommand::GetCommandStateParams( nsresult InsertPlaintextCommand::GetCommandStateParams(
const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor, Command aCommand, nsCommandParams& aParams, TextEditor* aTextEditor,
nsIEditingSession* aEditingSession) const { nsIEditingSession* aEditingSession) const {
return aParams.SetBool(STATE_ENABLED, return aParams.SetBool(STATE_ENABLED,
IsCommandEnabled(aCommandName, aTextEditor)); IsCommandEnabled(aCommand, aTextEditor));
} }
/****************************************************************************** /******************************************************************************
@ -748,7 +773,7 @@ nsresult InsertPlaintextCommand::GetCommandStateParams(
StaticRefPtr<InsertParagraphCommand> InsertParagraphCommand::sInstance; StaticRefPtr<InsertParagraphCommand> InsertParagraphCommand::sInstance;
bool InsertParagraphCommand::IsCommandEnabled(const char* aCommandName, bool InsertParagraphCommand::IsCommandEnabled(Command aCommand,
TextEditor* aTextEditor) const { TextEditor* aTextEditor) const {
if (!aTextEditor) { if (!aTextEditor) {
return false; return false;
@ -756,7 +781,7 @@ bool InsertParagraphCommand::IsCommandEnabled(const char* aCommandName,
return aTextEditor->IsSelectionEditable(); return aTextEditor->IsSelectionEditable();
} }
nsresult InsertParagraphCommand::DoCommand(const char* aCommandName, nsresult InsertParagraphCommand::DoCommand(Command aCommand,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
HTMLEditor* htmlEditor = aTextEditor.AsHTMLEditor(); HTMLEditor* htmlEditor = aTextEditor.AsHTMLEditor();
if (!htmlEditor) { if (!htmlEditor) {
@ -766,16 +791,15 @@ nsresult InsertParagraphCommand::DoCommand(const char* aCommandName,
} }
nsresult InsertParagraphCommand::DoCommandParams( nsresult InsertParagraphCommand::DoCommandParams(
const char* aCommandName, nsCommandParams* aParams, Command aCommand, nsCommandParams* aParams, TextEditor& aTextEditor) const {
TextEditor& aTextEditor) const { return DoCommand(aCommand, aTextEditor);
return DoCommand(aCommandName, aTextEditor);
} }
nsresult InsertParagraphCommand::GetCommandStateParams( nsresult InsertParagraphCommand::GetCommandStateParams(
const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor, Command aCommand, nsCommandParams& aParams, TextEditor* aTextEditor,
nsIEditingSession* aEditingSession) const { nsIEditingSession* aEditingSession) const {
return aParams.SetBool(STATE_ENABLED, return aParams.SetBool(STATE_ENABLED,
IsCommandEnabled(aCommandName, aTextEditor)); IsCommandEnabled(aCommand, aTextEditor));
} }
/****************************************************************************** /******************************************************************************
@ -784,7 +808,7 @@ nsresult InsertParagraphCommand::GetCommandStateParams(
StaticRefPtr<InsertLineBreakCommand> InsertLineBreakCommand::sInstance; StaticRefPtr<InsertLineBreakCommand> InsertLineBreakCommand::sInstance;
bool InsertLineBreakCommand::IsCommandEnabled(const char* aCommandName, bool InsertLineBreakCommand::IsCommandEnabled(Command aCommand,
TextEditor* aTextEditor) const { TextEditor* aTextEditor) const {
if (!aTextEditor) { if (!aTextEditor) {
return false; return false;
@ -792,7 +816,7 @@ bool InsertLineBreakCommand::IsCommandEnabled(const char* aCommandName,
return aTextEditor->IsSelectionEditable(); return aTextEditor->IsSelectionEditable();
} }
nsresult InsertLineBreakCommand::DoCommand(const char* aCommandName, nsresult InsertLineBreakCommand::DoCommand(Command aCommand,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
HTMLEditor* htmlEditor = aTextEditor.AsHTMLEditor(); HTMLEditor* htmlEditor = aTextEditor.AsHTMLEditor();
if (!htmlEditor) { if (!htmlEditor) {
@ -802,16 +826,15 @@ nsresult InsertLineBreakCommand::DoCommand(const char* aCommandName,
} }
nsresult InsertLineBreakCommand::DoCommandParams( nsresult InsertLineBreakCommand::DoCommandParams(
const char* aCommandName, nsCommandParams* aParams, Command aCommand, nsCommandParams* aParams, TextEditor& aTextEditor) const {
TextEditor& aTextEditor) const { return DoCommand(aCommand, aTextEditor);
return DoCommand(aCommandName, aTextEditor);
} }
nsresult InsertLineBreakCommand::GetCommandStateParams( nsresult InsertLineBreakCommand::GetCommandStateParams(
const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor, Command aCommand, nsCommandParams& aParams, TextEditor* aTextEditor,
nsIEditingSession* aEditingSession) const { nsIEditingSession* aEditingSession) const {
return aParams.SetBool(STATE_ENABLED, return aParams.SetBool(STATE_ENABLED,
IsCommandEnabled(aCommandName, aTextEditor)); IsCommandEnabled(aCommand, aTextEditor));
} }
/****************************************************************************** /******************************************************************************
@ -820,7 +843,7 @@ nsresult InsertLineBreakCommand::GetCommandStateParams(
StaticRefPtr<PasteQuotationCommand> PasteQuotationCommand::sInstance; StaticRefPtr<PasteQuotationCommand> PasteQuotationCommand::sInstance;
bool PasteQuotationCommand::IsCommandEnabled(const char* aCommandName, bool PasteQuotationCommand::IsCommandEnabled(Command aCommand,
TextEditor* aTextEditor) const { TextEditor* aTextEditor) const {
if (!aTextEditor) { if (!aTextEditor) {
return false; return false;
@ -829,7 +852,7 @@ bool PasteQuotationCommand::IsCommandEnabled(const char* aCommandName,
aTextEditor->CanPaste(nsIClipboard::kGlobalClipboard); aTextEditor->CanPaste(nsIClipboard::kGlobalClipboard);
} }
nsresult PasteQuotationCommand::DoCommand(const char* aCommandName, nsresult PasteQuotationCommand::DoCommand(Command aCommand,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
nsresult rv = aTextEditor.PasteAsQuotationAsAction( nsresult rv = aTextEditor.PasteAsQuotationAsAction(
nsIClipboard::kGlobalClipboard, true); nsIClipboard::kGlobalClipboard, true);
@ -839,14 +862,14 @@ nsresult PasteQuotationCommand::DoCommand(const char* aCommandName,
return NS_OK; return NS_OK;
} }
nsresult PasteQuotationCommand::DoCommandParams(const char* aCommandName, nsresult PasteQuotationCommand::DoCommandParams(Command aCommand,
nsCommandParams* aParams, nsCommandParams* aParams,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
return DoCommand(aCommandName, aTextEditor); return DoCommand(aCommand, aTextEditor);
} }
nsresult PasteQuotationCommand::GetCommandStateParams( nsresult PasteQuotationCommand::GetCommandStateParams(
const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor, Command aCommand, nsCommandParams& aParams, TextEditor* aTextEditor,
nsIEditingSession* aEditingSession) const { nsIEditingSession* aEditingSession) const {
if (!aTextEditor) { if (!aTextEditor) {
return NS_OK; return NS_OK;

Просмотреть файл

@ -51,14 +51,13 @@ class EditorCommand : public nsIControllerCommand {
nsISupports* aCommandRefCon) final; nsISupports* aCommandRefCon) final;
MOZ_CAN_RUN_SCRIPT MOZ_CAN_RUN_SCRIPT
virtual bool IsCommandEnabled(const char* aCommandName, virtual bool IsCommandEnabled(Command aCommand,
TextEditor* aTextEditor) const = 0; TextEditor* aTextEditor) const = 0;
MOZ_CAN_RUN_SCRIPT MOZ_CAN_RUN_SCRIPT
virtual nsresult DoCommand(const char* aCommandName, virtual nsresult DoCommand(Command aCommand,
TextEditor& aTextEditor) const = 0; TextEditor& aTextEditor) const = 0;
MOZ_CAN_RUN_SCRIPT MOZ_CAN_RUN_SCRIPT
virtual nsresult DoCommandParams(const char* aCommandName, virtual nsresult DoCommandParams(Command aCommand, nsCommandParams* aParams,
nsCommandParams* aParams,
TextEditor& aTextEditor) const = 0; TextEditor& aTextEditor) const = 0;
/** /**
* @param aTextEditor If the context is an editor, should be set to * @param aTextEditor If the context is an editor, should be set to
@ -70,34 +69,32 @@ class EditorCommand : public nsIControllerCommand {
*/ */
MOZ_CAN_RUN_SCRIPT MOZ_CAN_RUN_SCRIPT
virtual nsresult GetCommandStateParams( virtual nsresult GetCommandStateParams(
const char* aCommandName, nsCommandParams& aParams, Command aCommand, nsCommandParams& aParams, TextEditor* aTextEditor,
TextEditor* aTextEditor, nsIEditingSession* aEditingSession) const = 0; nsIEditingSession* aEditingSession) const = 0;
protected: protected:
EditorCommand() = default; EditorCommand() = default;
virtual ~EditorCommand() = default; virtual ~EditorCommand() = default;
}; };
#define NS_DECL_EDITOR_COMMAND_METHODS(_cmd) \ #define NS_DECL_EDITOR_COMMAND_METHODS(_cmd) \
public: \ public: \
MOZ_CAN_RUN_SCRIPT \ MOZ_CAN_RUN_SCRIPT \
virtual bool IsCommandEnabled(const char* aCommandName, \ virtual bool IsCommandEnabled(Command aCommand, TextEditor* aTextEditor) \
TextEditor* aTextEditor) const final; \ const final; \
using EditorCommand::IsCommandEnabled; \ using EditorCommand::IsCommandEnabled; \
MOZ_CAN_RUN_SCRIPT \ MOZ_CAN_RUN_SCRIPT \
virtual nsresult DoCommand(const char* aCommandName, \ virtual nsresult DoCommand(Command aCommand, TextEditor& aTextEditor) \
TextEditor& aTextEditor) const final; \ const final; \
using EditorCommand::DoCommand; \ using EditorCommand::DoCommand; \
MOZ_CAN_RUN_SCRIPT \ MOZ_CAN_RUN_SCRIPT \
virtual nsresult DoCommandParams(const char* aCommandName, \ virtual nsresult DoCommandParams(Command aCommand, nsCommandParams* aParams, \
nsCommandParams* aParams, \ TextEditor& aTextEditor) const final; \
TextEditor& aTextEditor) const final; \ using EditorCommand::DoCommandParams; \
using EditorCommand::DoCommandParams; \ MOZ_CAN_RUN_SCRIPT \
MOZ_CAN_RUN_SCRIPT \ virtual nsresult GetCommandStateParams( \
virtual nsresult GetCommandStateParams( \ Command aCommand, nsCommandParams& aParams, TextEditor* aTextEditor, \
const char* aCommandName, nsCommandParams& aParams, \ nsIEditingSession* aEditingSession) const final; \
TextEditor* aTextEditor, nsIEditingSession* aEditingSession) \
const final; \
using EditorCommand::GetCommandStateParams; using EditorCommand::GetCommandStateParams;
#define NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(_cmd) \ #define NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(_cmd) \
@ -160,7 +157,7 @@ class StateUpdatingCommandBase : public EditorCommand {
protected: protected:
StateUpdatingCommandBase() = default; StateUpdatingCommandBase() = default;
virtual ~StateUpdatingCommandBase() { sTagNameTable.Clear(); } virtual ~StateUpdatingCommandBase() = default;
// get the current state (on or off) for this style or block format // get the current state (on or off) for this style or block format
MOZ_CAN_RUN_SCRIPT MOZ_CAN_RUN_SCRIPT
@ -172,41 +169,57 @@ class StateUpdatingCommandBase : public EditorCommand {
virtual nsresult ToggleState(nsAtom* aTagName, virtual nsresult ToggleState(nsAtom* aTagName,
HTMLEditor* aHTMLEditor) const = 0; HTMLEditor* aHTMLEditor) const = 0;
static already_AddRefed<nsAtom> TagName(const char* aCommandName) { static nsAtom* GetTagName(Command aCommand) {
MOZ_DIAGNOSTIC_ASSERT(aCommandName); switch (aCommand) {
if (NS_WARN_IF(!aCommandName)) { case Command::FormatBold:
return nullptr; return nsGkAtoms::b;
case Command::FormatItalic:
return nsGkAtoms::i;
case Command::FormatUnderline:
return nsGkAtoms::u;
case Command::FormatTeletypeText:
return nsGkAtoms::tt;
case Command::FormatStrikeThrough:
return nsGkAtoms::strike;
case Command::FormatSuperscript:
return nsGkAtoms::sup;
case Command::FormatSubscript:
return nsGkAtoms::sub;
case Command::FormatNoBreak:
return nsGkAtoms::nobr;
case Command::FormatEmphasis:
return nsGkAtoms::em;
case Command::FormatStrong:
return nsGkAtoms::strong;
case Command::FormatCitation:
return nsGkAtoms::cite;
case Command::FormatAbbreviation:
return nsGkAtoms::abbr;
case Command::FormatAcronym:
return nsGkAtoms::acronym;
case Command::FormatCode:
return nsGkAtoms::code;
case Command::FormatSample:
return nsGkAtoms::samp;
case Command::FormatVariable:
return nsGkAtoms::var;
case Command::FormatRemoveLink:
return nsGkAtoms::href;
case Command::InsertOrderedList:
return nsGkAtoms::ol;
case Command::InsertUnorderedList:
return nsGkAtoms::ul;
case Command::InsertDefinitionTerm:
return nsGkAtoms::dt;
case Command::InsertDefinitionDetails:
return nsGkAtoms::dd;
case Command::FormatAbsolutePosition:
return nsGkAtoms::_empty;
default:
return nullptr;
} }
if (!sTagNameTable.Count()) {
sTagNameTable.Put("cmd_bold", nsGkAtoms::b);
sTagNameTable.Put("cmd_italic", nsGkAtoms::i);
sTagNameTable.Put("cmd_underline", nsGkAtoms::u);
sTagNameTable.Put("cmd_tt", nsGkAtoms::tt);
sTagNameTable.Put("cmd_strikethrough", nsGkAtoms::strike);
sTagNameTable.Put("cmd_superscript", nsGkAtoms::sup);
sTagNameTable.Put("cmd_subscript", nsGkAtoms::sub);
sTagNameTable.Put("cmd_nobreak", nsGkAtoms::nobr);
sTagNameTable.Put("cmd_em", nsGkAtoms::em);
sTagNameTable.Put("cmd_strong", nsGkAtoms::strong);
sTagNameTable.Put("cmd_cite", nsGkAtoms::cite);
sTagNameTable.Put("cmd_abbr", nsGkAtoms::abbr);
sTagNameTable.Put("cmd_acronym", nsGkAtoms::acronym);
sTagNameTable.Put("cmd_code", nsGkAtoms::code);
sTagNameTable.Put("cmd_samp", nsGkAtoms::samp);
sTagNameTable.Put("cmd_var", nsGkAtoms::var);
sTagNameTable.Put("cmd_removeLinks", nsGkAtoms::href);
sTagNameTable.Put("cmd_ol", nsGkAtoms::ol);
sTagNameTable.Put("cmd_ul", nsGkAtoms::ul);
sTagNameTable.Put("cmd_dt", nsGkAtoms::dt);
sTagNameTable.Put("cmd_dd", nsGkAtoms::dd);
sTagNameTable.Put("cmd_absPos", nsGkAtoms::_empty);
}
RefPtr<nsAtom> tagName = sTagNameTable.Get(aCommandName);
MOZ_DIAGNOSTIC_ASSERT(tagName);
return tagName.forget();
} }
friend class InsertTagCommand; // for allowing it to access GetTagName()
static nsRefPtrHashtable<nsCharPtrHashKey, nsAtom> sTagNameTable;
}; };
// Shared class for the various style updating commands like bold, italics etc. // Shared class for the various style updating commands like bold, italics etc.
@ -238,24 +251,20 @@ class InsertTagCommand final : public EditorCommand {
protected: protected:
InsertTagCommand() = default; InsertTagCommand() = default;
virtual ~InsertTagCommand() { sTagNameTable.Clear(); } virtual ~InsertTagCommand() = default;
static already_AddRefed<nsAtom> TagName(const char* aCommandName) { static nsAtom* GetTagName(Command aCommand) {
MOZ_DIAGNOSTIC_ASSERT(aCommandName); switch (aCommand) {
if (NS_WARN_IF(!aCommandName)) { case Command::InsertLink:
return nullptr; return nsGkAtoms::a;
case Command::InsertImage:
return nsGkAtoms::img;
case Command::InsertHorizontalRule:
return nsGkAtoms::hr;
default:
return StateUpdatingCommandBase::GetTagName(aCommand);
} }
if (!sTagNameTable.Count()) {
sTagNameTable.Put("cmd_insertLinkNoUI", nsGkAtoms::a);
sTagNameTable.Put("cmd_insertImageNoUI", nsGkAtoms::img);
sTagNameTable.Put("cmd_insertHR", nsGkAtoms::hr);
}
RefPtr<nsAtom> tagName = sTagNameTable.Get(aCommandName);
MOZ_DIAGNOSTIC_ASSERT(tagName);
return tagName.forget();
} }
static nsRefPtrHashtable<nsCharPtrHashKey, nsAtom> sTagNameTable;
}; };
class ListCommand final : public StateUpdatingCommandBase { class ListCommand final : public StateUpdatingCommandBase {

Просмотреть файл

@ -11,10 +11,10 @@
#include "mozilla/HTMLEditor.h" // for HTMLEditor #include "mozilla/HTMLEditor.h" // for HTMLEditor
#include "mozilla/dom/Element.h" #include "mozilla/dom/Element.h"
#include "nsAString.h" #include "nsAString.h"
#include "nsAtom.h" // for nsAtom, nsStaticAtom, etc
#include "nsCommandParams.h" // for nsCommandParams, etc #include "nsCommandParams.h" // for nsCommandParams, etc
#include "nsComponentManagerUtils.h" // for do_CreateInstance #include "nsComponentManagerUtils.h" // for do_CreateInstance
#include "nsGkAtoms.h" // for nsGkAtoms, nsGkAtoms::font, etc #include "nsGkAtoms.h" // for nsGkAtoms, nsGkAtoms::font, etc
#include "nsAtom.h" // for nsAtom, etc
#include "nsIClipboard.h" // for nsIClipboard, etc #include "nsIClipboard.h" // for nsIClipboard, etc
#include "nsIEditingSession.h" #include "nsIEditingSession.h"
#include "nsLiteralString.h" // for NS_LITERAL_STRING #include "nsLiteralString.h" // for NS_LITERAL_STRING
@ -46,10 +46,7 @@ static nsresult GetListState(HTMLEditor* aHTMLEditor, bool* aMixed,
* mozilla::StateUpdatingCommandBase * mozilla::StateUpdatingCommandBase
*****************************************************************************/ *****************************************************************************/
nsRefPtrHashtable<nsCharPtrHashKey, nsAtom> bool StateUpdatingCommandBase::IsCommandEnabled(Command aCommand,
StateUpdatingCommandBase::sTagNameTable;
bool StateUpdatingCommandBase::IsCommandEnabled(const char* aCommandName,
TextEditor* aTextEditor) const { TextEditor* aTextEditor) const {
if (!aTextEditor) { if (!aTextEditor) {
return false; return false;
@ -57,34 +54,33 @@ bool StateUpdatingCommandBase::IsCommandEnabled(const char* aCommandName,
if (!aTextEditor->IsSelectionEditable()) { if (!aTextEditor->IsSelectionEditable()) {
return false; return false;
} }
if (!strcmp(aCommandName, "cmd_absPos")) { if (aCommand == Command::FormatAbsolutePosition) {
HTMLEditor* htmlEditor = aTextEditor->AsHTMLEditor(); HTMLEditor* htmlEditor = aTextEditor->AsHTMLEditor();
return htmlEditor && htmlEditor->IsAbsolutePositionEditorEnabled(); return htmlEditor && htmlEditor->IsAbsolutePositionEditorEnabled();
} }
return true; return true;
} }
nsresult StateUpdatingCommandBase::DoCommand(const char* aCommandName, nsresult StateUpdatingCommandBase::DoCommand(Command aCommand,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
HTMLEditor* htmlEditor = aTextEditor.AsHTMLEditor(); HTMLEditor* htmlEditor = aTextEditor.AsHTMLEditor();
if (NS_WARN_IF(!htmlEditor)) { if (NS_WARN_IF(!htmlEditor)) {
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;
} }
RefPtr<nsAtom> tagName = TagName(aCommandName); nsAtom* tagName = GetTagName(aCommand);
if (NS_WARN_IF(!tagName)) { if (NS_WARN_IF(!tagName)) {
return NS_ERROR_UNEXPECTED; return NS_ERROR_UNEXPECTED;
} }
return ToggleState(tagName, MOZ_KnownLive(htmlEditor)); return ToggleState(MOZ_KnownLive(tagName), MOZ_KnownLive(htmlEditor));
} }
nsresult StateUpdatingCommandBase::DoCommandParams( nsresult StateUpdatingCommandBase::DoCommandParams(
const char* aCommandName, nsCommandParams* aParams, Command aCommand, nsCommandParams* aParams, TextEditor& aTextEditor) const {
TextEditor& aTextEditor) const { return DoCommand(aCommand, aTextEditor);
return DoCommand(aCommandName, aTextEditor);
} }
nsresult StateUpdatingCommandBase::GetCommandStateParams( nsresult StateUpdatingCommandBase::GetCommandStateParams(
const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor, Command aCommand, nsCommandParams& aParams, TextEditor* aTextEditor,
nsIEditingSession* aEditingSession) const { nsIEditingSession* aEditingSession) const {
if (!aTextEditor) { if (!aTextEditor) {
return NS_OK; return NS_OK;
@ -93,11 +89,12 @@ nsresult StateUpdatingCommandBase::GetCommandStateParams(
if (NS_WARN_IF(!htmlEditor)) { if (NS_WARN_IF(!htmlEditor)) {
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;
} }
RefPtr<nsAtom> tagName = TagName(aCommandName); nsAtom* tagName = GetTagName(aCommand);
if (NS_WARN_IF(!tagName)) { if (NS_WARN_IF(!tagName)) {
return NS_ERROR_UNEXPECTED; return NS_ERROR_UNEXPECTED;
} }
return GetCurrentState(tagName, MOZ_KnownLive(htmlEditor), aParams); return GetCurrentState(MOZ_KnownLive(tagName), MOZ_KnownLive(htmlEditor),
aParams);
} }
/***************************************************************************** /*****************************************************************************
@ -106,7 +103,7 @@ nsresult StateUpdatingCommandBase::GetCommandStateParams(
StaticRefPtr<PasteNoFormattingCommand> PasteNoFormattingCommand::sInstance; StaticRefPtr<PasteNoFormattingCommand> PasteNoFormattingCommand::sInstance;
bool PasteNoFormattingCommand::IsCommandEnabled(const char* aCommandName, bool PasteNoFormattingCommand::IsCommandEnabled(Command aCommand,
TextEditor* aTextEditor) const { TextEditor* aTextEditor) const {
if (!aTextEditor) { if (!aTextEditor) {
return false; return false;
@ -122,7 +119,7 @@ bool PasteNoFormattingCommand::IsCommandEnabled(const char* aCommandName,
return htmlEditor->CanPaste(nsIClipboard::kGlobalClipboard); return htmlEditor->CanPaste(nsIClipboard::kGlobalClipboard);
} }
nsresult PasteNoFormattingCommand::DoCommand(const char* aCommandName, nsresult PasteNoFormattingCommand::DoCommand(Command aCommand,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
HTMLEditor* htmlEditor = aTextEditor.AsHTMLEditor(); HTMLEditor* htmlEditor = aTextEditor.AsHTMLEditor();
if (NS_WARN_IF(!htmlEditor)) { if (NS_WARN_IF(!htmlEditor)) {
@ -134,16 +131,15 @@ nsresult PasteNoFormattingCommand::DoCommand(const char* aCommandName,
} }
nsresult PasteNoFormattingCommand::DoCommandParams( nsresult PasteNoFormattingCommand::DoCommandParams(
const char* aCommandName, nsCommandParams* aParams, Command aCommand, nsCommandParams* aParams, TextEditor& aTextEditor) const {
TextEditor& aTextEditor) const { return DoCommand(aCommand, aTextEditor);
return DoCommand(aCommandName, aTextEditor);
} }
nsresult PasteNoFormattingCommand::GetCommandStateParams( nsresult PasteNoFormattingCommand::GetCommandStateParams(
const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor, Command aCommand, nsCommandParams& aParams, TextEditor* aTextEditor,
nsIEditingSession* aEditingSession) const { nsIEditingSession* aEditingSession) const {
return aParams.SetBool(STATE_ENABLED, return aParams.SetBool(STATE_ENABLED,
IsCommandEnabled(aCommandName, aTextEditor)); IsCommandEnabled(aCommand, aTextEditor));
} }
/***************************************************************************** /*****************************************************************************
@ -372,7 +368,7 @@ nsresult ListItemCommand::ToggleState(nsAtom* aTagName,
StaticRefPtr<RemoveListCommand> RemoveListCommand::sInstance; StaticRefPtr<RemoveListCommand> RemoveListCommand::sInstance;
bool RemoveListCommand::IsCommandEnabled(const char* aCommandName, bool RemoveListCommand::IsCommandEnabled(Command aCommand,
TextEditor* aTextEditor) const { TextEditor* aTextEditor) const {
if (!aTextEditor) { if (!aTextEditor) {
return false; return false;
@ -397,7 +393,7 @@ bool RemoveListCommand::IsCommandEnabled(const char* aCommandName,
return bMixed || !localName.IsEmpty(); return bMixed || !localName.IsEmpty();
} }
nsresult RemoveListCommand::DoCommand(const char* aCommandName, nsresult RemoveListCommand::DoCommand(Command aCommand,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
HTMLEditor* htmlEditor = aTextEditor.AsHTMLEditor(); HTMLEditor* htmlEditor = aTextEditor.AsHTMLEditor();
if (!htmlEditor) { if (!htmlEditor) {
@ -407,17 +403,17 @@ nsresult RemoveListCommand::DoCommand(const char* aCommandName,
return htmlEditor->RemoveList(EmptyString()); return htmlEditor->RemoveList(EmptyString());
} }
nsresult RemoveListCommand::DoCommandParams(const char* aCommandName, nsresult RemoveListCommand::DoCommandParams(Command aCommand,
nsCommandParams* aParams, nsCommandParams* aParams,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
return DoCommand(aCommandName, aTextEditor); return DoCommand(aCommand, aTextEditor);
} }
nsresult RemoveListCommand::GetCommandStateParams( nsresult RemoveListCommand::GetCommandStateParams(
const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor, Command aCommand, nsCommandParams& aParams, TextEditor* aTextEditor,
nsIEditingSession* aEditingSession) const { nsIEditingSession* aEditingSession) const {
return aParams.SetBool(STATE_ENABLED, return aParams.SetBool(STATE_ENABLED,
IsCommandEnabled(aCommandName, aTextEditor)); IsCommandEnabled(aCommand, aTextEditor));
} }
/***************************************************************************** /*****************************************************************************
@ -426,7 +422,7 @@ nsresult RemoveListCommand::GetCommandStateParams(
StaticRefPtr<IndentCommand> IndentCommand::sInstance; StaticRefPtr<IndentCommand> IndentCommand::sInstance;
bool IndentCommand::IsCommandEnabled(const char* aCommandName, bool IndentCommand::IsCommandEnabled(Command aCommand,
TextEditor* aTextEditor) const { TextEditor* aTextEditor) const {
if (!aTextEditor) { if (!aTextEditor) {
return false; return false;
@ -434,7 +430,7 @@ bool IndentCommand::IsCommandEnabled(const char* aCommandName,
return aTextEditor->IsSelectionEditable(); return aTextEditor->IsSelectionEditable();
} }
nsresult IndentCommand::DoCommand(const char* aCommandName, nsresult IndentCommand::DoCommand(Command aCommand,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
HTMLEditor* htmlEditor = aTextEditor.AsHTMLEditor(); HTMLEditor* htmlEditor = aTextEditor.AsHTMLEditor();
if (!htmlEditor) { if (!htmlEditor) {
@ -447,17 +443,17 @@ nsresult IndentCommand::DoCommand(const char* aCommandName,
return NS_OK; return NS_OK;
} }
nsresult IndentCommand::DoCommandParams(const char* aCommandName, nsresult IndentCommand::DoCommandParams(Command aCommand,
nsCommandParams* aParams, nsCommandParams* aParams,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
return DoCommand(aCommandName, aTextEditor); return DoCommand(aCommand, aTextEditor);
} }
nsresult IndentCommand::GetCommandStateParams( nsresult IndentCommand::GetCommandStateParams(
const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor, Command aCommand, nsCommandParams& aParams, TextEditor* aTextEditor,
nsIEditingSession* aEditingSession) const { nsIEditingSession* aEditingSession) const {
return aParams.SetBool(STATE_ENABLED, return aParams.SetBool(STATE_ENABLED,
IsCommandEnabled(aCommandName, aTextEditor)); IsCommandEnabled(aCommand, aTextEditor));
} }
/***************************************************************************** /*****************************************************************************
@ -466,7 +462,7 @@ nsresult IndentCommand::GetCommandStateParams(
StaticRefPtr<OutdentCommand> OutdentCommand::sInstance; StaticRefPtr<OutdentCommand> OutdentCommand::sInstance;
bool OutdentCommand::IsCommandEnabled(const char* aCommandName, bool OutdentCommand::IsCommandEnabled(Command aCommand,
TextEditor* aTextEditor) const { TextEditor* aTextEditor) const {
if (!aTextEditor) { if (!aTextEditor) {
return false; return false;
@ -474,7 +470,7 @@ bool OutdentCommand::IsCommandEnabled(const char* aCommandName,
return aTextEditor->IsSelectionEditable(); return aTextEditor->IsSelectionEditable();
} }
nsresult OutdentCommand::DoCommand(const char* aCommandName, nsresult OutdentCommand::DoCommand(Command aCommand,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
HTMLEditor* htmlEditor = aTextEditor.AsHTMLEditor(); HTMLEditor* htmlEditor = aTextEditor.AsHTMLEditor();
if (!htmlEditor) { if (!htmlEditor) {
@ -487,24 +483,24 @@ nsresult OutdentCommand::DoCommand(const char* aCommandName,
return NS_OK; return NS_OK;
} }
nsresult OutdentCommand::DoCommandParams(const char* aCommandName, nsresult OutdentCommand::DoCommandParams(Command aCommand,
nsCommandParams* aParams, nsCommandParams* aParams,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
return DoCommand(aCommandName, aTextEditor); return DoCommand(aCommand, aTextEditor);
} }
nsresult OutdentCommand::GetCommandStateParams( nsresult OutdentCommand::GetCommandStateParams(
const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor, Command aCommand, nsCommandParams& aParams, TextEditor* aTextEditor,
nsIEditingSession* aEditingSession) const { nsIEditingSession* aEditingSession) const {
return aParams.SetBool(STATE_ENABLED, return aParams.SetBool(STATE_ENABLED,
IsCommandEnabled(aCommandName, aTextEditor)); IsCommandEnabled(aCommand, aTextEditor));
} }
/***************************************************************************** /*****************************************************************************
* mozilla::MultiStateCommandBase * mozilla::MultiStateCommandBase
*****************************************************************************/ *****************************************************************************/
bool MultiStateCommandBase::IsCommandEnabled(const char* aCommandName, bool MultiStateCommandBase::IsCommandEnabled(Command aCommand,
TextEditor* aTextEditor) const { TextEditor* aTextEditor) const {
if (!aTextEditor) { if (!aTextEditor) {
return false; return false;
@ -513,14 +509,14 @@ bool MultiStateCommandBase::IsCommandEnabled(const char* aCommandName,
return aTextEditor->IsSelectionEditable(); return aTextEditor->IsSelectionEditable();
} }
nsresult MultiStateCommandBase::DoCommand(const char* aCommandName, nsresult MultiStateCommandBase::DoCommand(Command aCommand,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
NS_WARNING( NS_WARNING(
"who is calling MultiStateCommandBase::DoCommand (no implementation)?"); "who is calling MultiStateCommandBase::DoCommand (no implementation)?");
return NS_OK; return NS_OK;
} }
nsresult MultiStateCommandBase::DoCommandParams(const char* aCommandName, nsresult MultiStateCommandBase::DoCommandParams(Command aCommand,
nsCommandParams* aParams, nsCommandParams* aParams,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
HTMLEditor* htmlEditor = aTextEditor.AsHTMLEditor(); HTMLEditor* htmlEditor = aTextEditor.AsHTMLEditor();
@ -542,7 +538,7 @@ nsresult MultiStateCommandBase::DoCommandParams(const char* aCommandName,
} }
nsresult MultiStateCommandBase::GetCommandStateParams( nsresult MultiStateCommandBase::GetCommandStateParams(
const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor, Command aCommand, nsCommandParams& aParams, TextEditor* aTextEditor,
nsIEditingSession* aEditingSession) const { nsIEditingSession* aEditingSession) const {
if (!aTextEditor) { if (!aTextEditor) {
return NS_OK; return NS_OK;
@ -950,7 +946,7 @@ nsresult AbsolutePositioningCommand::ToggleState(
StaticRefPtr<DecreaseZIndexCommand> DecreaseZIndexCommand::sInstance; StaticRefPtr<DecreaseZIndexCommand> DecreaseZIndexCommand::sInstance;
bool DecreaseZIndexCommand::IsCommandEnabled(const char* aCommandName, bool DecreaseZIndexCommand::IsCommandEnabled(Command aCommand,
TextEditor* aTextEditor) const { TextEditor* aTextEditor) const {
if (!aTextEditor) { if (!aTextEditor) {
return false; return false;
@ -969,7 +965,7 @@ bool DecreaseZIndexCommand::IsCommandEnabled(const char* aCommandName,
return htmlEditor->GetZIndex(*positionedElement) > 0; return htmlEditor->GetZIndex(*positionedElement) > 0;
} }
nsresult DecreaseZIndexCommand::DoCommand(const char* aCommandName, nsresult DecreaseZIndexCommand::DoCommand(Command aCommand,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
HTMLEditor* htmlEditor = aTextEditor.AsHTMLEditor(); HTMLEditor* htmlEditor = aTextEditor.AsHTMLEditor();
if (NS_WARN_IF(!htmlEditor)) { if (NS_WARN_IF(!htmlEditor)) {
@ -978,17 +974,17 @@ nsresult DecreaseZIndexCommand::DoCommand(const char* aCommandName,
return htmlEditor->AddZIndex(-1); return htmlEditor->AddZIndex(-1);
} }
nsresult DecreaseZIndexCommand::DoCommandParams(const char* aCommandName, nsresult DecreaseZIndexCommand::DoCommandParams(Command aCommand,
nsCommandParams* aParams, nsCommandParams* aParams,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
return DoCommand(aCommandName, aTextEditor); return DoCommand(aCommand, aTextEditor);
} }
nsresult DecreaseZIndexCommand::GetCommandStateParams( nsresult DecreaseZIndexCommand::GetCommandStateParams(
const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor, Command aCommand, nsCommandParams& aParams, TextEditor* aTextEditor,
nsIEditingSession* aEditingSession) const { nsIEditingSession* aEditingSession) const {
return aParams.SetBool(STATE_ENABLED, return aParams.SetBool(STATE_ENABLED,
IsCommandEnabled(aCommandName, aTextEditor)); IsCommandEnabled(aCommand, aTextEditor));
} }
/***************************************************************************** /*****************************************************************************
@ -997,7 +993,7 @@ nsresult DecreaseZIndexCommand::GetCommandStateParams(
StaticRefPtr<IncreaseZIndexCommand> IncreaseZIndexCommand::sInstance; StaticRefPtr<IncreaseZIndexCommand> IncreaseZIndexCommand::sInstance;
bool IncreaseZIndexCommand::IsCommandEnabled(const char* aCommandName, bool IncreaseZIndexCommand::IsCommandEnabled(Command aCommand,
TextEditor* aTextEditor) const { TextEditor* aTextEditor) const {
if (!aTextEditor) { if (!aTextEditor) {
return false; return false;
@ -1012,7 +1008,7 @@ bool IncreaseZIndexCommand::IsCommandEnabled(const char* aCommandName,
return !!htmlEditor->GetPositionedElement(); return !!htmlEditor->GetPositionedElement();
} }
nsresult IncreaseZIndexCommand::DoCommand(const char* aCommandName, nsresult IncreaseZIndexCommand::DoCommand(Command aCommand,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
HTMLEditor* htmlEditor = aTextEditor.AsHTMLEditor(); HTMLEditor* htmlEditor = aTextEditor.AsHTMLEditor();
if (NS_WARN_IF(!htmlEditor)) { if (NS_WARN_IF(!htmlEditor)) {
@ -1021,17 +1017,17 @@ nsresult IncreaseZIndexCommand::DoCommand(const char* aCommandName,
return htmlEditor->AddZIndex(1); return htmlEditor->AddZIndex(1);
} }
nsresult IncreaseZIndexCommand::DoCommandParams(const char* aCommandName, nsresult IncreaseZIndexCommand::DoCommandParams(Command aCommand,
nsCommandParams* aParams, nsCommandParams* aParams,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
return DoCommand(aCommandName, aTextEditor); return DoCommand(aCommand, aTextEditor);
} }
nsresult IncreaseZIndexCommand::GetCommandStateParams( nsresult IncreaseZIndexCommand::GetCommandStateParams(
const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor, Command aCommand, nsCommandParams& aParams, TextEditor* aTextEditor,
nsIEditingSession* aEditingSession) const { nsIEditingSession* aEditingSession) const {
return aParams.SetBool(STATE_ENABLED, return aParams.SetBool(STATE_ENABLED,
IsCommandEnabled(aCommandName, aTextEditor)); IsCommandEnabled(aCommand, aTextEditor));
} }
/***************************************************************************** /*****************************************************************************
@ -1040,7 +1036,7 @@ nsresult IncreaseZIndexCommand::GetCommandStateParams(
StaticRefPtr<RemoveStylesCommand> RemoveStylesCommand::sInstance; StaticRefPtr<RemoveStylesCommand> RemoveStylesCommand::sInstance;
bool RemoveStylesCommand::IsCommandEnabled(const char* aCommandName, bool RemoveStylesCommand::IsCommandEnabled(Command aCommand,
TextEditor* aTextEditor) const { TextEditor* aTextEditor) const {
if (!aTextEditor) { if (!aTextEditor) {
return false; return false;
@ -1049,7 +1045,7 @@ bool RemoveStylesCommand::IsCommandEnabled(const char* aCommandName,
return aTextEditor->IsSelectionEditable(); return aTextEditor->IsSelectionEditable();
} }
nsresult RemoveStylesCommand::DoCommand(const char* aCommandName, nsresult RemoveStylesCommand::DoCommand(Command aCommand,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
HTMLEditor* htmlEditor = aTextEditor.AsHTMLEditor(); HTMLEditor* htmlEditor = aTextEditor.AsHTMLEditor();
if (!htmlEditor) { if (!htmlEditor) {
@ -1058,17 +1054,17 @@ nsresult RemoveStylesCommand::DoCommand(const char* aCommandName,
return MOZ_KnownLive(htmlEditor)->RemoveAllInlineProperties(); return MOZ_KnownLive(htmlEditor)->RemoveAllInlineProperties();
} }
nsresult RemoveStylesCommand::DoCommandParams(const char* aCommandName, nsresult RemoveStylesCommand::DoCommandParams(Command aCommand,
nsCommandParams* aParams, nsCommandParams* aParams,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
return DoCommand(aCommandName, aTextEditor); return DoCommand(aCommand, aTextEditor);
} }
nsresult RemoveStylesCommand::GetCommandStateParams( nsresult RemoveStylesCommand::GetCommandStateParams(
const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor, Command aCommand, nsCommandParams& aParams, TextEditor* aTextEditor,
nsIEditingSession* aEditingSession) const { nsIEditingSession* aEditingSession) const {
return aParams.SetBool(STATE_ENABLED, return aParams.SetBool(STATE_ENABLED,
IsCommandEnabled(aCommandName, aTextEditor)); IsCommandEnabled(aCommand, aTextEditor));
} }
/***************************************************************************** /*****************************************************************************
@ -1077,7 +1073,7 @@ nsresult RemoveStylesCommand::GetCommandStateParams(
StaticRefPtr<IncreaseFontSizeCommand> IncreaseFontSizeCommand::sInstance; StaticRefPtr<IncreaseFontSizeCommand> IncreaseFontSizeCommand::sInstance;
bool IncreaseFontSizeCommand::IsCommandEnabled(const char* aCommandName, bool IncreaseFontSizeCommand::IsCommandEnabled(Command aCommand,
TextEditor* aTextEditor) const { TextEditor* aTextEditor) const {
if (!aTextEditor) { if (!aTextEditor) {
return false; return false;
@ -1086,7 +1082,7 @@ bool IncreaseFontSizeCommand::IsCommandEnabled(const char* aCommandName,
return aTextEditor->IsSelectionEditable(); return aTextEditor->IsSelectionEditable();
} }
nsresult IncreaseFontSizeCommand::DoCommand(const char* aCommandName, nsresult IncreaseFontSizeCommand::DoCommand(Command aCommand,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
HTMLEditor* htmlEditor = aTextEditor.AsHTMLEditor(); HTMLEditor* htmlEditor = aTextEditor.AsHTMLEditor();
if (!htmlEditor) { if (!htmlEditor) {
@ -1096,16 +1092,15 @@ nsresult IncreaseFontSizeCommand::DoCommand(const char* aCommandName,
} }
nsresult IncreaseFontSizeCommand::DoCommandParams( nsresult IncreaseFontSizeCommand::DoCommandParams(
const char* aCommandName, nsCommandParams* aParams, Command aCommand, nsCommandParams* aParams, TextEditor& aTextEditor) const {
TextEditor& aTextEditor) const { return DoCommand(aCommand, aTextEditor);
return DoCommand(aCommandName, aTextEditor);
} }
nsresult IncreaseFontSizeCommand::GetCommandStateParams( nsresult IncreaseFontSizeCommand::GetCommandStateParams(
const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor, Command aCommand, nsCommandParams& aParams, TextEditor* aTextEditor,
nsIEditingSession* aEditingSession) const { nsIEditingSession* aEditingSession) const {
return aParams.SetBool(STATE_ENABLED, return aParams.SetBool(STATE_ENABLED,
IsCommandEnabled(aCommandName, aTextEditor)); IsCommandEnabled(aCommand, aTextEditor));
} }
/***************************************************************************** /*****************************************************************************
@ -1114,7 +1109,7 @@ nsresult IncreaseFontSizeCommand::GetCommandStateParams(
StaticRefPtr<DecreaseFontSizeCommand> DecreaseFontSizeCommand::sInstance; StaticRefPtr<DecreaseFontSizeCommand> DecreaseFontSizeCommand::sInstance;
bool DecreaseFontSizeCommand::IsCommandEnabled(const char* aCommandName, bool DecreaseFontSizeCommand::IsCommandEnabled(Command aCommand,
TextEditor* aTextEditor) const { TextEditor* aTextEditor) const {
if (!aTextEditor) { if (!aTextEditor) {
return false; return false;
@ -1123,7 +1118,7 @@ bool DecreaseFontSizeCommand::IsCommandEnabled(const char* aCommandName,
return aTextEditor->IsSelectionEditable(); return aTextEditor->IsSelectionEditable();
} }
nsresult DecreaseFontSizeCommand::DoCommand(const char* aCommandName, nsresult DecreaseFontSizeCommand::DoCommand(Command aCommand,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
HTMLEditor* htmlEditor = aTextEditor.AsHTMLEditor(); HTMLEditor* htmlEditor = aTextEditor.AsHTMLEditor();
if (!htmlEditor) { if (!htmlEditor) {
@ -1133,16 +1128,15 @@ nsresult DecreaseFontSizeCommand::DoCommand(const char* aCommandName,
} }
nsresult DecreaseFontSizeCommand::DoCommandParams( nsresult DecreaseFontSizeCommand::DoCommandParams(
const char* aCommandName, nsCommandParams* aParams, Command aCommand, nsCommandParams* aParams, TextEditor& aTextEditor) const {
TextEditor& aTextEditor) const { return DoCommand(aCommand, aTextEditor);
return DoCommand(aCommandName, aTextEditor);
} }
nsresult DecreaseFontSizeCommand::GetCommandStateParams( nsresult DecreaseFontSizeCommand::GetCommandStateParams(
const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor, Command aCommand, nsCommandParams& aParams, TextEditor* aTextEditor,
nsIEditingSession* aEditingSession) const { nsIEditingSession* aEditingSession) const {
return aParams.SetBool(STATE_ENABLED, return aParams.SetBool(STATE_ENABLED,
IsCommandEnabled(aCommandName, aTextEditor)); IsCommandEnabled(aCommand, aTextEditor));
} }
/***************************************************************************** /*****************************************************************************
@ -1151,7 +1145,7 @@ nsresult DecreaseFontSizeCommand::GetCommandStateParams(
StaticRefPtr<InsertHTMLCommand> InsertHTMLCommand::sInstance; StaticRefPtr<InsertHTMLCommand> InsertHTMLCommand::sInstance;
bool InsertHTMLCommand::IsCommandEnabled(const char* aCommandName, bool InsertHTMLCommand::IsCommandEnabled(Command aCommand,
TextEditor* aTextEditor) const { TextEditor* aTextEditor) const {
if (!aTextEditor) { if (!aTextEditor) {
return false; return false;
@ -1159,7 +1153,7 @@ bool InsertHTMLCommand::IsCommandEnabled(const char* aCommandName,
return aTextEditor->IsSelectionEditable(); return aTextEditor->IsSelectionEditable();
} }
nsresult InsertHTMLCommand::DoCommand(const char* aCommandName, nsresult InsertHTMLCommand::DoCommand(Command aCommand,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
// If nsInsertHTMLCommand is called with no parameters, it was probably called // If nsInsertHTMLCommand is called with no parameters, it was probably called
// with an empty string parameter ''. In this case, it should act the same as // with an empty string parameter ''. In this case, it should act the same as
@ -1172,7 +1166,7 @@ nsresult InsertHTMLCommand::DoCommand(const char* aCommandName,
return MOZ_KnownLive(htmlEditor)->InsertHTML(html); return MOZ_KnownLive(htmlEditor)->InsertHTML(html);
} }
nsresult InsertHTMLCommand::DoCommandParams(const char* aCommandName, nsresult InsertHTMLCommand::DoCommandParams(Command aCommand,
nsCommandParams* aParams, nsCommandParams* aParams,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
if (NS_WARN_IF(!aParams)) { if (NS_WARN_IF(!aParams)) {
@ -1194,10 +1188,10 @@ nsresult InsertHTMLCommand::DoCommandParams(const char* aCommandName,
} }
nsresult InsertHTMLCommand::GetCommandStateParams( nsresult InsertHTMLCommand::GetCommandStateParams(
const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor, Command aCommand, nsCommandParams& aParams, TextEditor* aTextEditor,
nsIEditingSession* aEditingSession) const { nsIEditingSession* aEditingSession) const {
return aParams.SetBool(STATE_ENABLED, return aParams.SetBool(STATE_ENABLED,
IsCommandEnabled(aCommandName, aTextEditor)); IsCommandEnabled(aCommand, aTextEditor));
} }
/***************************************************************************** /*****************************************************************************
@ -1205,9 +1199,8 @@ nsresult InsertHTMLCommand::GetCommandStateParams(
*****************************************************************************/ *****************************************************************************/
StaticRefPtr<InsertTagCommand> InsertTagCommand::sInstance; StaticRefPtr<InsertTagCommand> InsertTagCommand::sInstance;
nsRefPtrHashtable<nsCharPtrHashKey, nsAtom> InsertTagCommand::sTagNameTable;
bool InsertTagCommand::IsCommandEnabled(const char* aCommandName, bool InsertTagCommand::IsCommandEnabled(Command aCommand,
TextEditor* aTextEditor) const { TextEditor* aTextEditor) const {
if (!aTextEditor) { if (!aTextEditor) {
return false; return false;
@ -1216,9 +1209,9 @@ bool InsertTagCommand::IsCommandEnabled(const char* aCommandName,
} }
// corresponding STATE_ATTRIBUTE is: src (img) and href (a) // corresponding STATE_ATTRIBUTE is: src (img) and href (a)
nsresult InsertTagCommand::DoCommand(const char* aCmdName, nsresult InsertTagCommand::DoCommand(Command aCommand,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
RefPtr<nsAtom> tagName = TagName(aCmdName); nsAtom* tagName = GetTagName(aCommand);
if (NS_WARN_IF(tagName != nsGkAtoms::hr)) { if (NS_WARN_IF(tagName != nsGkAtoms::hr)) {
return NS_ERROR_NOT_IMPLEMENTED; return NS_ERROR_NOT_IMPLEMENTED;
} }
@ -1229,7 +1222,8 @@ nsresult InsertTagCommand::DoCommand(const char* aCmdName,
} }
RefPtr<Element> newElement = RefPtr<Element> newElement =
MOZ_KnownLive(htmlEditor)->CreateElementWithDefaults(*tagName); MOZ_KnownLive(htmlEditor)
->CreateElementWithDefaults(MOZ_KnownLive(*tagName));
if (NS_WARN_IF(!newElement)) { if (NS_WARN_IF(!newElement)) {
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;
} }
@ -1241,18 +1235,18 @@ nsresult InsertTagCommand::DoCommand(const char* aCmdName,
return NS_OK; return NS_OK;
} }
nsresult InsertTagCommand::DoCommandParams(const char* aCommandName, nsresult InsertTagCommand::DoCommandParams(Command aCommand,
nsCommandParams* aParams, nsCommandParams* aParams,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
// inserting an hr shouldn't have an parameters, just call DoCommand for that // inserting an hr shouldn't have an parameters, just call DoCommand for that
if (!strcmp(aCommandName, "cmd_insertHR")) { if (aCommand == Command::InsertHorizontalRule) {
return DoCommand(aCommandName, aTextEditor); return DoCommand(aCommand, aTextEditor);
} }
if (NS_WARN_IF(!aParams)) { if (NS_WARN_IF(!aParams)) {
return NS_ERROR_INVALID_ARG; return NS_ERROR_INVALID_ARG;
} }
RefPtr<nsAtom> tagName = TagName(aCommandName); nsAtom* tagName = GetTagName(aCommand);
if (NS_WARN_IF(!tagName)) { if (NS_WARN_IF(!tagName)) {
return NS_ERROR_UNEXPECTED; return NS_ERROR_UNEXPECTED;
} }
@ -1285,7 +1279,8 @@ nsresult InsertTagCommand::DoCommandParams(const char* aCommandName,
} }
RefPtr<Element> newElement = RefPtr<Element> newElement =
MOZ_KnownLive(htmlEditor)->CreateElementWithDefaults(*tagName); MOZ_KnownLive(htmlEditor)
->CreateElementWithDefaults(MOZ_KnownLive(*tagName));
if (NS_WARN_IF(!newElement)) { if (NS_WARN_IF(!newElement)) {
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;
} }
@ -1313,10 +1308,10 @@ nsresult InsertTagCommand::DoCommandParams(const char* aCommandName,
} }
nsresult InsertTagCommand::GetCommandStateParams( nsresult InsertTagCommand::GetCommandStateParams(
const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor, Command aCommand, nsCommandParams& aParams, TextEditor* aTextEditor,
nsIEditingSession* aEditingSession) const { nsIEditingSession* aEditingSession) const {
return aParams.SetBool(STATE_ENABLED, return aParams.SetBool(STATE_ENABLED,
IsCommandEnabled(aCommandName, aTextEditor)); IsCommandEnabled(aCommand, aTextEditor));
} }
/***************************************************************************** /*****************************************************************************

Просмотреть файл

@ -37,181 +37,175 @@ namespace mozilla {
StaticRefPtr<SetDocumentStateCommand> SetDocumentStateCommand::sInstance; StaticRefPtr<SetDocumentStateCommand> SetDocumentStateCommand::sInstance;
bool SetDocumentStateCommand::IsCommandEnabled(const char* aCommandName, bool SetDocumentStateCommand::IsCommandEnabled(Command aCommand,
TextEditor* aTextEditor) const { TextEditor* aTextEditor) const {
// These commands are always enabled // These commands are always enabled
return true; return true;
} }
nsresult SetDocumentStateCommand::DoCommand(const char* aCommandName, nsresult SetDocumentStateCommand::DoCommand(Command aCommand,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
return NS_ERROR_NOT_IMPLEMENTED; return NS_ERROR_NOT_IMPLEMENTED;
} }
nsresult SetDocumentStateCommand::DoCommandParams( nsresult SetDocumentStateCommand::DoCommandParams(
const char* aCommandName, nsCommandParams* aParams, Command aCommand, nsCommandParams* aParams, TextEditor& aTextEditor) const {
TextEditor& aTextEditor) const {
if (NS_WARN_IF(!aParams)) { if (NS_WARN_IF(!aParams)) {
return NS_ERROR_INVALID_ARG; return NS_ERROR_INVALID_ARG;
} }
if (!strcmp(aCommandName, "cmd_setDocumentModified")) { switch (aCommand) {
ErrorResult error; case Command::SetDocumentModified: {
bool modified = aParams->GetBool(STATE_ATTRIBUTE, error); ErrorResult error;
// Should we fail if this param wasn't set? bool modified = aParams->GetBool(STATE_ATTRIBUTE, error);
// I'm not sure we should be that strict // Should we fail if this param wasn't set?
if (NS_WARN_IF(error.Failed())) { // I'm not sure we should be that strict
return error.StealNSResult(); if (NS_WARN_IF(error.Failed())) {
} return error.StealNSResult();
if (modified) { }
nsresult rv = aTextEditor.IncrementModificationCount(1); if (modified) {
nsresult rv = aTextEditor.IncrementModificationCount(1);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return NS_OK;
}
nsresult rv = aTextEditor.ResetModificationCount();
if (NS_WARN_IF(NS_FAILED(rv))) { if (NS_WARN_IF(NS_FAILED(rv))) {
return rv; return rv;
} }
return NS_OK; return NS_OK;
} }
nsresult rv = aTextEditor.ResetModificationCount(); case Command::SetDocumentReadOnly: {
if (NS_WARN_IF(NS_FAILED(rv))) { ErrorResult error;
return rv; bool isReadOnly = aParams->GetBool(STATE_ATTRIBUTE, error);
} if (NS_WARN_IF(error.Failed())) {
return NS_OK; return error.StealNSResult();
} }
if (isReadOnly) {
if (!strcmp(aCommandName, "cmd_setDocumentReadOnly")) { nsresult rv =
ErrorResult error; aTextEditor.AddFlags(nsIPlaintextEditor::eEditorReadonlyMask);
bool isReadOnly = aParams->GetBool(STATE_ATTRIBUTE, error); if (NS_WARN_IF(NS_FAILED(rv))) {
if (NS_WARN_IF(error.Failed())) { return rv;
return error.StealNSResult(); }
} return NS_OK;
if (isReadOnly) { }
nsresult rv = nsresult rv =
aTextEditor.AddFlags(nsIPlaintextEditor::eEditorReadonlyMask); aTextEditor.RemoveFlags(nsIPlaintextEditor::eEditorReadonlyMask);
if (NS_WARN_IF(NS_FAILED(rv))) { if (NS_WARN_IF(NS_FAILED(rv))) {
return rv; return rv;
} }
return NS_OK; return NS_OK;
} }
nsresult rv = case Command::SetDocumentUseCSS: {
aTextEditor.RemoveFlags(nsIPlaintextEditor::eEditorReadonlyMask); HTMLEditor* htmlEditor = aTextEditor.AsHTMLEditor();
if (NS_WARN_IF(NS_FAILED(rv))) { if (NS_WARN_IF(!htmlEditor)) {
return rv; return NS_ERROR_INVALID_ARG;
} }
return NS_OK; ErrorResult error;
} bool desireCSS = aParams->GetBool(STATE_ATTRIBUTE, error);
if (NS_WARN_IF(error.Failed())) {
if (!strcmp(aCommandName, "cmd_setDocumentUseCSS")) { return error.StealNSResult();
HTMLEditor* htmlEditor = aTextEditor.AsHTMLEditor(); }
if (NS_WARN_IF(!htmlEditor)) { nsresult rv = htmlEditor->SetIsCSSEnabled(desireCSS);
return NS_ERROR_INVALID_ARG; if (NS_WARN_IF(NS_FAILED(rv))) {
} return rv;
ErrorResult error; }
bool desireCSS = aParams->GetBool(STATE_ATTRIBUTE, error);
if (NS_WARN_IF(error.Failed())) {
return error.StealNSResult();
}
nsresult rv = htmlEditor->SetIsCSSEnabled(desireCSS);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return NS_OK;
}
if (!strcmp(aCommandName, "cmd_insertBrOnReturn")) {
HTMLEditor* htmlEditor = aTextEditor.AsHTMLEditor();
if (NS_WARN_IF(!htmlEditor)) {
return NS_ERROR_INVALID_ARG;
}
ErrorResult error;
bool insertBrOnReturn = aParams->GetBool(STATE_ATTRIBUTE, error);
if (NS_WARN_IF(error.Failed())) {
return error.StealNSResult();
}
nsresult rv =
htmlEditor->SetReturnInParagraphCreatesNewParagraph(!insertBrOnReturn);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return NS_OK;
}
if (!strcmp(aCommandName, "cmd_defaultParagraphSeparator")) {
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);
return NS_OK; return NS_OK;
} }
if (newValue.LowerCaseEqualsLiteral("p")) { case Command::SetDocumentInsertBROnEnterKeyPress: {
htmlEditor->SetDefaultParagraphSeparator(ParagraphSeparator::p); HTMLEditor* htmlEditor = aTextEditor.AsHTMLEditor();
if (NS_WARN_IF(!htmlEditor)) {
return NS_ERROR_INVALID_ARG;
}
ErrorResult error;
bool insertBrOnReturn = aParams->GetBool(STATE_ATTRIBUTE, error);
if (NS_WARN_IF(error.Failed())) {
return error.StealNSResult();
}
nsresult rv = htmlEditor->SetReturnInParagraphCreatesNewParagraph(
!insertBrOnReturn);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return NS_OK; return NS_OK;
} }
if (newValue.LowerCaseEqualsLiteral("br")) { case Command::SetDocumentDefaultParagraphSeparator: {
// Mozilla extension for backwards compatibility HTMLEditor* htmlEditor = aTextEditor.AsHTMLEditor();
htmlEditor->SetDefaultParagraphSeparator(ParagraphSeparator::br); 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);
return NS_OK;
}
if (newValue.LowerCaseEqualsLiteral("p")) {
htmlEditor->SetDefaultParagraphSeparator(ParagraphSeparator::p);
return NS_OK;
}
if (newValue.LowerCaseEqualsLiteral("br")) {
// Mozilla extension for backwards compatibility
htmlEditor->SetDefaultParagraphSeparator(ParagraphSeparator::br);
return NS_OK;
}
// This should not be reachable from nsHTMLDocument::ExecCommand
NS_WARNING("Invalid default paragraph separator");
return NS_ERROR_UNEXPECTED;
}
case Command::ToggleObjectResizers: {
HTMLEditor* htmlEditor = aTextEditor.AsHTMLEditor();
if (NS_WARN_IF(!htmlEditor)) {
return NS_ERROR_INVALID_ARG;
}
ErrorResult error;
bool enabled = aParams->GetBool(STATE_ATTRIBUTE, error);
if (NS_WARN_IF(error.Failed())) {
return error.StealNSResult();
}
htmlEditor->EnableObjectResizer(enabled);
return NS_OK; return NS_OK;
} }
case Command::ToggleInlineTableEditor: {
// This should not be reachable from nsHTMLDocument::ExecCommand HTMLEditor* htmlEditor = aTextEditor.AsHTMLEditor();
NS_WARNING("Invalid default paragraph separator"); if (NS_WARN_IF(!htmlEditor)) {
return NS_ERROR_UNEXPECTED; return NS_ERROR_INVALID_ARG;
}
ErrorResult error;
bool enabled = aParams->GetBool(STATE_ATTRIBUTE, error);
if (NS_WARN_IF(error.Failed())) {
return error.StealNSResult();
}
htmlEditor->EnableInlineTableEditor(enabled);
return NS_OK;
}
case Command::ToggleAbsolutePositionEditor: {
HTMLEditor* htmlEditor = aTextEditor.AsHTMLEditor();
if (NS_WARN_IF(!htmlEditor)) {
return NS_ERROR_INVALID_ARG;
}
ErrorResult error;
bool enabled = aParams->GetBool(STATE_ATTRIBUTE, error);
if (NS_WARN_IF(error.Failed())) {
return error.StealNSResult();
}
htmlEditor->EnableAbsolutePositionEditor(enabled);
return NS_OK;
}
default:
return NS_ERROR_NOT_IMPLEMENTED;
} }
if (!strcmp(aCommandName, "cmd_enableObjectResizing")) {
HTMLEditor* htmlEditor = aTextEditor.AsHTMLEditor();
if (NS_WARN_IF(!htmlEditor)) {
return NS_ERROR_INVALID_ARG;
}
ErrorResult error;
bool enabled = aParams->GetBool(STATE_ATTRIBUTE, error);
if (NS_WARN_IF(error.Failed())) {
return error.StealNSResult();
}
htmlEditor->EnableObjectResizer(enabled);
return NS_OK;
}
if (!strcmp(aCommandName, "cmd_enableInlineTableEditing")) {
HTMLEditor* htmlEditor = aTextEditor.AsHTMLEditor();
if (NS_WARN_IF(!htmlEditor)) {
return NS_ERROR_INVALID_ARG;
}
ErrorResult error;
bool enabled = aParams->GetBool(STATE_ATTRIBUTE, error);
if (NS_WARN_IF(error.Failed())) {
return error.StealNSResult();
}
htmlEditor->EnableInlineTableEditor(enabled);
return NS_OK;
}
if (!strcmp(aCommandName, "cmd_enableAbsolutePositionEditing")) {
HTMLEditor* htmlEditor = aTextEditor.AsHTMLEditor();
if (NS_WARN_IF(!htmlEditor)) {
return NS_ERROR_INVALID_ARG;
}
ErrorResult error;
bool enabled = aParams->GetBool(STATE_ATTRIBUTE, error);
if (NS_WARN_IF(error.Failed())) {
return error.StealNSResult();
}
htmlEditor->EnableAbsolutePositionEditor(enabled);
return NS_OK;
}
return NS_ERROR_NOT_IMPLEMENTED;
} }
nsresult SetDocumentStateCommand::GetCommandStateParams( nsresult SetDocumentStateCommand::GetCommandStateParams(
const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor, Command aCommand, nsCommandParams& aParams, TextEditor* aTextEditor,
nsIEditingSession* aEditingSession) const { nsIEditingSession* aEditingSession) const {
// If the result is set to STATE_ALL as bool value, queryCommandState() // If the result is set to STATE_ALL as bool value, queryCommandState()
// returns the bool value. // returns the bool value.
@ -224,148 +218,134 @@ nsresult SetDocumentStateCommand::GetCommandStateParams(
return NS_ERROR_INVALID_ARG; return NS_ERROR_INVALID_ARG;
} }
// Always get the enabled state // Always get the enabled state
nsresult rv = aParams.SetBool(STATE_ENABLED, nsresult rv =
IsCommandEnabled(aCommandName, aTextEditor)); aParams.SetBool(STATE_ENABLED, IsCommandEnabled(aCommand, aTextEditor));
if (NS_WARN_IF(NS_FAILED(rv))) { if (NS_WARN_IF(NS_FAILED(rv))) {
return rv; return rv;
} }
// cmd_setDocumentModified is an internal command. switch (aCommand) {
if (!strcmp(aCommandName, "cmd_setDocumentModified")) { case Command::SetDocumentModified: {
bool modified; bool modified;
rv = aTextEditor->GetDocumentModified(&modified); rv = aTextEditor->GetDocumentModified(&modified);
if (NS_WARN_IF(NS_FAILED(rv))) { if (NS_WARN_IF(NS_FAILED(rv))) {
return rv; return rv;
}
// XXX Nobody refers this result due to wrong type.
rv = aParams.SetBool(STATE_ATTRIBUTE, modified);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return NS_OK;
}
// cmd_setDocumentReadOnly is a Gecko specific command, "contentReadOnly".
if (!strcmp(aCommandName, "cmd_setDocumentReadOnly")) {
// XXX Nobody refers this result due to wrong type.
rv = aParams.SetBool(STATE_ATTRIBUTE, aTextEditor->IsReadonly());
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return NS_OK;
}
// cmd_setDocumentUseCSS is a common command, "styleWithCSS".
if (!strcmp(aCommandName, "cmd_setDocumentUseCSS")) {
HTMLEditor* htmlEditor = aTextEditor->AsHTMLEditor();
if (NS_WARN_IF(!htmlEditor)) {
return NS_ERROR_INVALID_ARG;
}
rv = aParams.SetBool(STATE_ALL, htmlEditor->IsCSSEnabled());
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return NS_OK;
}
// cmd_insertBrOnReturn is a Gecko specific command, "insertBrOrReturn".
if (!strcmp(aCommandName, "cmd_insertBrOnReturn")) {
HTMLEditor* htmlEditor = aTextEditor->AsHTMLEditor();
if (NS_WARN_IF(!htmlEditor)) {
return NS_ERROR_INVALID_ARG;
}
bool createPOnReturn;
htmlEditor->GetReturnInParagraphCreatesNewParagraph(&createPOnReturn);
// XXX Nobody refers this result due to wrong type.
rv = aParams.SetBool(STATE_ATTRIBUTE, !createPOnReturn);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return NS_OK;
}
// cmd_defaultParagraphSeparator is a common command,
// "defaultParagraphSeparator".
if (!strcmp(aCommandName, "cmd_defaultParagraphSeparator")) {
HTMLEditor* htmlEditor = aTextEditor->AsHTMLEditor();
if (NS_WARN_IF(!htmlEditor)) {
return NS_ERROR_INVALID_ARG;
}
switch (htmlEditor->GetDefaultParagraphSeparator()) {
case ParagraphSeparator::div: {
DebugOnly<nsresult> rv =
aParams.SetCString(STATE_ATTRIBUTE, NS_LITERAL_CSTRING("div"));
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"Failed to set command params to return \"div\"");
return NS_OK;
} }
case ParagraphSeparator::p: { // XXX Nobody refers this result due to wrong type.
DebugOnly<nsresult> rv = rv = aParams.SetBool(STATE_ATTRIBUTE, modified);
aParams.SetCString(STATE_ATTRIBUTE, NS_LITERAL_CSTRING("p")); if (NS_WARN_IF(NS_FAILED(rv))) {
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), return rv;
"Failed to set command params to return \"p\"");
return NS_OK;
} }
case ParagraphSeparator::br: { return NS_OK;
DebugOnly<nsresult> rv = }
aParams.SetCString(STATE_ATTRIBUTE, NS_LITERAL_CSTRING("br")); case Command::SetDocumentReadOnly: {
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), // XXX Nobody refers this result due to wrong type.
"Failed to set command params to return \"br\""); rv = aParams.SetBool(STATE_ATTRIBUTE, aTextEditor->IsReadonly());
return NS_OK; if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
} }
default: return NS_OK;
MOZ_ASSERT_UNREACHABLE("Invalid paragraph separator value");
return NS_ERROR_UNEXPECTED;
} }
} case Command::SetDocumentUseCSS: {
HTMLEditor* htmlEditor = aTextEditor->AsHTMLEditor();
if (NS_WARN_IF(!htmlEditor)) {
return NS_ERROR_INVALID_ARG;
}
rv = aParams.SetBool(STATE_ALL, htmlEditor->IsCSSEnabled());
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return NS_OK;
}
case Command::SetDocumentInsertBROnEnterKeyPress: {
HTMLEditor* htmlEditor = aTextEditor->AsHTMLEditor();
if (NS_WARN_IF(!htmlEditor)) {
return NS_ERROR_INVALID_ARG;
}
bool createPOnReturn;
htmlEditor->GetReturnInParagraphCreatesNewParagraph(&createPOnReturn);
// XXX Nobody refers this result due to wrong type.
rv = aParams.SetBool(STATE_ATTRIBUTE, !createPOnReturn);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return NS_OK;
}
case Command::SetDocumentDefaultParagraphSeparator: {
HTMLEditor* htmlEditor = aTextEditor->AsHTMLEditor();
if (NS_WARN_IF(!htmlEditor)) {
return NS_ERROR_INVALID_ARG;
}
// cmd_enableObjectResizing is a Gecko specific command, switch (htmlEditor->GetDefaultParagraphSeparator()) {
// "enableObjectResizing". case ParagraphSeparator::div: {
if (!strcmp(aCommandName, "cmd_enableObjectResizing")) { DebugOnly<nsresult> rv =
HTMLEditor* htmlEditor = aTextEditor->AsHTMLEditor(); aParams.SetCString(STATE_ATTRIBUTE, NS_LITERAL_CSTRING("div"));
if (NS_WARN_IF(!htmlEditor)) { NS_WARNING_ASSERTION(
return NS_ERROR_INVALID_ARG; NS_SUCCEEDED(rv),
"Failed to set command params to return \"div\"");
return NS_OK;
}
case ParagraphSeparator::p: {
DebugOnly<nsresult> rv =
aParams.SetCString(STATE_ATTRIBUTE, NS_LITERAL_CSTRING("p"));
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"Failed to set command params to return \"p\"");
return NS_OK;
}
case ParagraphSeparator::br: {
DebugOnly<nsresult> rv =
aParams.SetCString(STATE_ATTRIBUTE, NS_LITERAL_CSTRING("br"));
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"Failed to set command params to return \"br\"");
return NS_OK;
}
default:
MOZ_ASSERT_UNREACHABLE("Invalid paragraph separator value");
return NS_ERROR_UNEXPECTED;
}
} }
// We returned the result as STATE_ATTRIBUTE with bool value 60 or earlier. case Command::ToggleObjectResizers: {
// So, the result was ignored by both nsHTMLDocument::QueryCommandValue() HTMLEditor* htmlEditor = aTextEditor->AsHTMLEditor();
// and nsHTMLDocument::QueryCommandState(). if (NS_WARN_IF(!htmlEditor)) {
rv = aParams.SetBool(STATE_ALL, htmlEditor->IsObjectResizerEnabled()); return NS_ERROR_INVALID_ARG;
if (NS_WARN_IF(NS_FAILED(rv))) { }
return rv; // We returned the result as STATE_ATTRIBUTE with bool value 60 or
// earlier. So, the result was ignored by both
// nsHTMLDocument::QueryCommandValue() and
// nsHTMLDocument::QueryCommandState().
rv = aParams.SetBool(STATE_ALL, htmlEditor->IsObjectResizerEnabled());
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return NS_OK;
} }
return NS_OK; case Command::ToggleInlineTableEditor: {
HTMLEditor* htmlEditor = aTextEditor->AsHTMLEditor();
if (NS_WARN_IF(!htmlEditor)) {
return NS_ERROR_INVALID_ARG;
}
// We returned the result as STATE_ATTRIBUTE with bool value 60 or
// earlier. So, the result was ignored by both
// nsHTMLDocument::QueryCommandValue() and
// nsHTMLDocument::QueryCommandState().
rv = aParams.SetBool(STATE_ALL, htmlEditor->IsInlineTableEditorEnabled());
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return NS_OK;
}
case Command::ToggleAbsolutePositionEditor: {
HTMLEditor* htmlEditor = aTextEditor->AsHTMLEditor();
if (NS_WARN_IF(!htmlEditor)) {
return NS_ERROR_INVALID_ARG;
}
return aParams.SetBool(STATE_ALL,
htmlEditor->IsAbsolutePositionEditorEnabled());
}
default:
return NS_ERROR_NOT_IMPLEMENTED;
} }
// cmd_enableInlineTableEditing is a Gecko specific command,
// "enableInlineTableEditing".
if (!strcmp(aCommandName, "cmd_enableInlineTableEditing")) {
HTMLEditor* htmlEditor = aTextEditor->AsHTMLEditor();
if (NS_WARN_IF(!htmlEditor)) {
return NS_ERROR_INVALID_ARG;
}
// We returned the result as STATE_ATTRIBUTE with bool value 60 or earlier.
// So, the result was ignored by both nsHTMLDocument::QueryCommandValue()
// and nsHTMLDocument::QueryCommandState().
rv = aParams.SetBool(STATE_ALL, htmlEditor->IsInlineTableEditorEnabled());
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return NS_OK;
}
// cmd_enableAbsolutePositionEditing is a Gecko specific command,
// "cenableAbsolutePositionEditing".
if (!strcmp(aCommandName, "cmd_enableAbsolutePositionEditing")) {
HTMLEditor* htmlEditor = aTextEditor->AsHTMLEditor();
if (NS_WARN_IF(!htmlEditor)) {
return NS_ERROR_INVALID_ARG;
}
return aParams.SetBool(STATE_ALL,
htmlEditor->IsAbsolutePositionEditorEnabled());
}
return NS_ERROR_NOT_IMPLEMENTED;
} }
/***************************************************************************** /*****************************************************************************
@ -407,69 +387,70 @@ nsresult SetDocumentStateCommand::GetCommandStateParams(
StaticRefPtr<DocumentStateCommand> DocumentStateCommand::sInstance; StaticRefPtr<DocumentStateCommand> DocumentStateCommand::sInstance;
bool DocumentStateCommand::IsCommandEnabled(const char* aCommandName, bool DocumentStateCommand::IsCommandEnabled(Command aCommand,
TextEditor* aTextEditor) const { TextEditor* aTextEditor) const {
// Always return false to discourage callers from using DoCommand() // Always return false to discourage callers from using DoCommand()
return false; return false;
} }
nsresult DocumentStateCommand::DoCommand(const char* aCommandName, nsresult DocumentStateCommand::DoCommand(Command aCommand,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
return NS_ERROR_NOT_IMPLEMENTED; return NS_ERROR_NOT_IMPLEMENTED;
} }
nsresult DocumentStateCommand::DoCommandParams(const char* aCommandName, nsresult DocumentStateCommand::DoCommandParams(Command aCommand,
nsCommandParams* aParams, nsCommandParams* aParams,
TextEditor& aTextEditor) const { TextEditor& aTextEditor) const {
return NS_ERROR_NOT_IMPLEMENTED; return NS_ERROR_NOT_IMPLEMENTED;
} }
nsresult DocumentStateCommand::GetCommandStateParams( nsresult DocumentStateCommand::GetCommandStateParams(
const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor, Command aCommand, nsCommandParams& aParams, TextEditor* aTextEditor,
nsIEditingSession* aEditingSession) const { nsIEditingSession* aEditingSession) const {
if (!strcmp(aCommandName, "obs_documentCreated")) { switch (aCommand) {
uint32_t editorStatus = nsIEditingSession::eEditorErrorUnknown; case Command::EditorObserverDocumentCreated: {
if (aEditingSession) { uint32_t editorStatus = nsIEditingSession::eEditorErrorUnknown;
// Current context is initially set to nsIEditingSession until editor is if (aEditingSession) {
// successfully created and source doc is loaded. Embedder gets error // Current context is initially set to nsIEditingSession until editor is
// status if this fails. If called before startup is finished, // successfully created and source doc is loaded. Embedder gets error
// status will be eEditorCreationInProgress. // status if this fails. If called before startup is finished,
nsresult rv = aEditingSession->GetEditorStatus(&editorStatus); // status will be eEditorCreationInProgress.
nsresult rv = aEditingSession->GetEditorStatus(&editorStatus);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
} else if (aTextEditor) {
// If current context is an editor, then everything started up OK!
editorStatus = nsIEditingSession::eEditorOK;
}
// Note that if refCon is not-null, but is neither
// an nsIEditingSession or nsIEditor, we return "eEditorErrorUnknown"
DebugOnly<nsresult> rv = aParams.SetInt(STATE_DATA, editorStatus);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Failed to set editor status");
return NS_OK;
}
case Command::EditorObserverDocumentLocationChanged: {
if (!aTextEditor) {
return NS_OK;
}
Document* document = aTextEditor->GetDocument();
if (NS_WARN_IF(!document)) {
return NS_ERROR_FAILURE;
}
nsIURI* uri = document->GetDocumentURI();
if (NS_WARN_IF(!uri)) {
return NS_ERROR_FAILURE;
}
nsresult rv = aParams.SetISupports(STATE_DATA, uri);
if (NS_WARN_IF(NS_FAILED(rv))) { if (NS_WARN_IF(NS_FAILED(rv))) {
return rv; return rv;
} }
} else if (aTextEditor) {
// If current context is an editor, then everything started up OK!
editorStatus = nsIEditingSession::eEditorOK;
}
// Note that if refCon is not-null, but is neither
// an nsIEditingSession or nsIEditor, we return "eEditorErrorUnknown"
DebugOnly<nsresult> rv = aParams.SetInt(STATE_DATA, editorStatus);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Failed to set editor status");
return NS_OK;
}
if (!strcmp(aCommandName, "obs_documentLocationChanged")) {
if (!aTextEditor) {
return NS_OK; return NS_OK;
} }
Document* document = aTextEditor->GetDocument(); default:
if (NS_WARN_IF(!document)) { return NS_ERROR_NOT_IMPLEMENTED;
return NS_ERROR_FAILURE;
}
nsIURI* uri = document->GetDocumentURI();
if (NS_WARN_IF(!uri)) {
return NS_ERROR_FAILURE;
}
nsresult rv = aParams.SetISupports(STATE_DATA, uri);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return NS_OK;
} }
return NS_ERROR_NOT_IMPLEMENTED;
} }
} // namespace mozilla } // namespace mozilla