From d481dba0c155a091738b6bfff6c697c24111e3fa Mon Sep 17 00:00:00 2001 From: Masayuki Nakano Date: Fri, 2 Nov 2018 14:24:33 +0000 Subject: [PATCH] Bug 1503473 - part 5: Move InsertParagraphSeparator*() into HTMLEditor r=m_kato Now, TextEditor needs only InsertLineBreak*() so that InsertParagraphSeparator*() is necessary only in HTMLEditor. With overriding nsIPlaintextEditor::InsertLineBreak() in HTMLEditor, we can do it simply. Differential Revision: https://phabricator.services.mozilla.com/D10525 --HG-- extra : moz-landing-system : lando --- editor/libeditor/EditorCommands.cpp | 8 ++-- editor/libeditor/HTMLEditor.cpp | 69 +++++++++++++++++++++++++++++ editor/libeditor/HTMLEditor.h | 14 ++++++ editor/libeditor/TextEditor.cpp | 69 +---------------------------- editor/libeditor/TextEditor.h | 12 ----- 5 files changed, 90 insertions(+), 82 deletions(-) diff --git a/editor/libeditor/EditorCommands.cpp b/editor/libeditor/EditorCommands.cpp index e516725e1fb9..b41f4b2fa265 100644 --- a/editor/libeditor/EditorCommands.cpp +++ b/editor/libeditor/EditorCommands.cpp @@ -1178,9 +1178,11 @@ InsertParagraphCommand::DoCommand(const char* aCommandName, return NS_ERROR_FAILURE; } - TextEditor* textEditor = editor->AsTextEditor(); - MOZ_ASSERT(textEditor); - return textEditor->InsertParagraphSeparatorAsAction(); + HTMLEditor* htmlEditor = editor->AsHTMLEditor(); + if (!htmlEditor) { + return NS_OK; // Do nothing for now. + } + return htmlEditor->InsertParagraphSeparatorAsAction(); } NS_IMETHODIMP diff --git a/editor/libeditor/HTMLEditor.cpp b/editor/libeditor/HTMLEditor.cpp index eee109996106..9cac637f3547 100644 --- a/editor/libeditor/HTMLEditor.cpp +++ b/editor/libeditor/HTMLEditor.cpp @@ -1122,6 +1122,18 @@ HTMLEditor::UpdateBaseURL() return NS_OK; } +NS_IMETHODIMP +HTMLEditor::InsertLineBreak() +{ + // XPCOM method's InsertLineBreak() should insert paragraph separator in + // HTMLEditor. + nsresult rv = InsertParagraphSeparatorAsSubAction(); + if (NS_WARN_IF(NS_FAILED(rv))) { + return rv; + } + return NS_OK; +} + nsresult HTMLEditor::InsertLineBreakAsAction() { @@ -1130,6 +1142,8 @@ HTMLEditor::InsertLineBreakAsAction() return NS_ERROR_NOT_INITIALIZED; } + // XXX This method may be called by "insertLineBreak" command. So, using + // TypingTxnName here is odd in such case. AutoPlaceholderBatch treatAsOneTransaction(*this, *nsGkAtoms::TypingTxnName); nsresult rv = InsertBrElementAtSelectionWithTransaction(); if (NS_WARN_IF(NS_FAILED(rv))) { @@ -1138,6 +1152,61 @@ HTMLEditor::InsertLineBreakAsAction() return NS_OK; } +nsresult +HTMLEditor::InsertParagraphSeparatorAsAction() +{ + AutoEditActionDataSetter editActionData( + *this, + EditAction::eInsertParagraphSeparator); + if (NS_WARN_IF(!editActionData.CanHandle())) { + return NS_ERROR_NOT_INITIALIZED; + } + + // XXX This may be called by execCommand() with "insertParagraph". + // In such case, naming the transaction "TypingTxnName" is odd. + AutoPlaceholderBatch treatAsOneTransaction(*this, *nsGkAtoms::TypingTxnName); + nsresult rv = InsertParagraphSeparatorAsSubAction(); + if (NS_WARN_IF(NS_FAILED(rv))) { + return rv; + } + return NS_OK; +} + +nsresult +HTMLEditor::InsertParagraphSeparatorAsSubAction() +{ + MOZ_ASSERT(IsEditActionDataAvailable()); + + if (!mRules) { + return NS_ERROR_NOT_INITIALIZED; + } + + // Protect the edit rules object from dying + RefPtr rules(mRules); + + AutoTopLevelEditSubActionNotifier maybeTopLevelEditSubAction( + *this, + EditSubAction::eInsertParagraphSeparator, + nsIEditor::eNext); + + EditSubActionInfo subActionInfo(EditSubAction::eInsertParagraphSeparator); + subActionInfo.maxLength = mMaxTextLength; + bool cancel, handled; + nsresult rv = rules->WillDoAction(subActionInfo, &cancel, &handled); + if (cancel) { + return rv; // We don't need to call DidDoAction() if canceled. + } + // XXX DidDoAction() does nothing for eInsertParagraphSeparator. However, + // we should call it until we keep using this style. Perhaps, each + // editor method should call necessary method of + // TextEditRules/HTMLEditRules directly. + rv = rules->DidDoAction(subActionInfo, rv); + if (NS_WARN_IF(NS_FAILED(rv))) { + return rv; + } + return NS_OK; +} + nsresult HTMLEditor::TabInTable(bool inIsShift, bool* outHandled) diff --git a/editor/libeditor/HTMLEditor.h b/editor/libeditor/HTMLEditor.h index 594cfce4678e..50e68b4d6c5d 100644 --- a/editor/libeditor/HTMLEditor.h +++ b/editor/libeditor/HTMLEditor.h @@ -139,6 +139,8 @@ public: NS_IMETHOD DeleteNode(nsINode* aNode) override; + NS_IMETHOD InsertLineBreak() override; + virtual nsresult HandleKeyPressEvent( WidgetKeyboardEvent* aKeyboardEvent) override; virtual nsIContent* GetFocusedContent() override; @@ -176,6 +178,12 @@ public: */ virtual nsresult InsertLineBreakAsAction() override; + /** + * InsertParagraphSeparatorAsAction() is called when user tries to separate + * current paragraph with Enter key press in HTMLEditor or something. + */ + nsresult InsertParagraphSeparatorAsAction(); + /** * CreateElementWithDefaults() creates new element whose name is * aTagName with some default attributes are set. Note that this is a @@ -998,6 +1006,12 @@ protected: // Called by helper classes. protected: // Shouldn't be used by friend classes virtual ~HTMLEditor(); + /** + * InsertParagraphSeparatorAsSubAction() inserts a line break if it's + * HTMLEditor and it's possible. + */ + nsresult InsertParagraphSeparatorAsSubAction(); + virtual nsresult SelectAllInternal() override; /** diff --git a/editor/libeditor/TextEditor.cpp b/editor/libeditor/TextEditor.cpp index 1fad01cae88f..7f36bfa4b42f 100644 --- a/editor/libeditor/TextEditor.cpp +++ b/editor/libeditor/TextEditor.cpp @@ -460,26 +460,6 @@ TextEditor::InsertLineBreakAsAction() return NS_OK; } -nsresult -TextEditor::InsertParagraphSeparatorAsAction() -{ - AutoEditActionDataSetter editActionData( - *this, - EditAction::eInsertParagraphSeparator); - if (NS_WARN_IF(!editActionData.CanHandle())) { - return NS_ERROR_NOT_INITIALIZED; - } - - // XXX This may be called by execCommand() with "insertParagraph". - // In such case, naming the transaction "TypingTxnName" is odd. - AutoPlaceholderBatch treatAsOneTransaction(*this, *nsGkAtoms::TypingTxnName); - nsresult rv = InsertParagraphSeparatorAsSubAction(); - if (NS_WARN_IF(NS_FAILED(rv))) { - return rv; - } - return NS_OK; -} - template already_AddRefed TextEditor::InsertBrElementWithTransaction( @@ -1090,23 +1070,13 @@ TextEditor::InsertTextAsSubAction(const nsAString& aStringToInsert) NS_IMETHODIMP TextEditor::InsertLineBreak() { - EditAction editAction = - AsHTMLEditor() ? EditAction::eInsertParagraphSeparator : - EditAction::eInsertLineBreak; - AutoEditActionDataSetter editActionData(*this, editAction); + AutoEditActionDataSetter editActionData(*this, EditAction::eInsertLineBreak); if (NS_WARN_IF(!editActionData.CanHandle())) { return NS_ERROR_NOT_INITIALIZED; } AutoPlaceholderBatch treatAsOneTransaction(*this); - if (editAction == EditAction::eInsertLineBreak) { - nsresult rv = InsertLineBreakAsSubAction(); - if (NS_WARN_IF(NS_FAILED(rv))) { - return rv; - } - return NS_OK; - } - nsresult rv = InsertParagraphSeparatorAsSubAction(); + nsresult rv = InsertLineBreakAsSubAction(); if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } @@ -1148,41 +1118,6 @@ TextEditor::InsertLineBreakAsSubAction() return NS_OK; } -nsresult -TextEditor::InsertParagraphSeparatorAsSubAction() -{ - MOZ_ASSERT(IsEditActionDataAvailable()); - - if (!mRules) { - return NS_ERROR_NOT_INITIALIZED; - } - - // Protect the edit rules object from dying - RefPtr rules(mRules); - - AutoTopLevelEditSubActionNotifier maybeTopLevelEditSubAction( - *this, - EditSubAction::eInsertParagraphSeparator, - nsIEditor::eNext); - - EditSubActionInfo subActionInfo(EditSubAction::eInsertParagraphSeparator); - subActionInfo.maxLength = mMaxTextLength; - bool cancel, handled; - nsresult rv = rules->WillDoAction(subActionInfo, &cancel, &handled); - if (cancel) { - return rv; // We don't need to call DidDoAction() if canceled. - } - // XXX DidDoAction() does nothing for eInsertParagraphSeparator. However, - // we should call it until we keep using this style. Perhaps, each - // editor method should call necessary method of - // TextEditRules/HTMLEditRules directly. - rv = rules->DidDoAction(subActionInfo, rv); - if (NS_WARN_IF(NS_FAILED(rv))) { - return rv; - } - return NS_OK; -} - nsresult TextEditor::SetText(const nsAString& aString) { diff --git a/editor/libeditor/TextEditor.h b/editor/libeditor/TextEditor.h index 281163ba0dcb..450325258002 100644 --- a/editor/libeditor/TextEditor.h +++ b/editor/libeditor/TextEditor.h @@ -192,12 +192,6 @@ public: */ virtual nsresult InsertLineBreakAsAction(); - /** - * InsertParagraphSeparatorAsAction() is called when user tries to separate - * current paragraph with Enter key press in HTMLEditor or something. - */ - nsresult InsertParagraphSeparatorAsAction(); - /** * OnCompositionStart() is called when editor receives eCompositionStart * event which should be handled in this editor. @@ -387,12 +381,6 @@ protected: // Shouldn't be used by friend classes */ nsresult InsertLineBreakAsSubAction(); - /** - * InsertParagraphSeparatorAsSubAction() inserts a line break if it's - * HTMLEditor and it's possible. - */ - nsresult InsertParagraphSeparatorAsSubAction(); - nsresult InsertTextAt(const nsAString& aStringToInsert, nsINode* aDestinationNode, int32_t aDestOffset,