From a2ecad4a6701fd3255417def1e605a976efa9042 Mon Sep 17 00:00:00 2001 From: Masayuki Nakano Date: Mon, 23 Jul 2018 16:05:30 +0900 Subject: [PATCH] Bug 1476897 - part 5: Move implementation of TextEditor::InsertAsQuotation() to new method r=m_kato TextEditor::InsertAsQuotation() is not used as a method of nsIEditorMailSupport, however, this is used internally. So, we need to keep this method's function as a non-virtual method but we can make the method just return NS_ERROR_NOT_IMPLEMENTED. MozReview-Commit-ID: 2CcY4SZGwyr --HG-- extra : rebase_source : d490145b571bea465be5379872b9ea01daa633cc --- editor/libeditor/HTMLEditorDataTransfer.cpp | 26 ++++++++++----- editor/libeditor/TextEditor.cpp | 35 ++++++++++++++------- editor/libeditor/TextEditor.h | 9 ++++++ 3 files changed, 50 insertions(+), 20 deletions(-) diff --git a/editor/libeditor/HTMLEditorDataTransfer.cpp b/editor/libeditor/HTMLEditorDataTransfer.cpp index e3e9d2c3b785..c46f2389888a 100644 --- a/editor/libeditor/HTMLEditorDataTransfer.cpp +++ b/editor/libeditor/HTMLEditorDataTransfer.cpp @@ -1780,9 +1780,14 @@ HTMLEditor::InsertAsPlaintextQuotation(const nsAString& aQuotedText, bool aAddCites, nsINode** aNodeInserted) { - // get selection + if (aNodeInserted) { + *aNodeInserted = nullptr; + } + RefPtr selection = GetSelection(); - NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER); + if (NS_WARN_IF(!selection)) { + return NS_ERROR_FAILURE; + } AutoPlaceholderBatch beginBatching(this); AutoTopLevelEditSubActionNotifier maybeTopLevelEditSubAction( @@ -1809,7 +1814,7 @@ HTMLEditor::InsertAsPlaintextQuotation(const nsAString& aQuotedText, // We could use 100vw, but 98vw avoids a horizontal scroll bar where possible. // All this is done to wrap overlong lines to the screen and not to the // container element, the width-restricted body. - nsCOMPtr newNode = + RefPtr newNode = DeleteSelectionAndCreateElement(*nsGkAtoms::span); // If this succeeded, then set selection inside the pre @@ -1832,11 +1837,15 @@ HTMLEditor::InsertAsPlaintextQuotation(const nsAString& aQuotedText, } // and set the selection inside it: - selection->Collapse(newNode, 0); + DebugOnly rv = selection->Collapse(newNode, 0); + NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), + "Failed to collapse selection into the new node"); } if (aAddCites) { - rv = TextEditor::InsertAsQuotation(aQuotedText, aNodeInserted); + rv = InsertWithQuotationsAsSubAction(aQuotedText); + NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), + "Failed to insert the text with quotations"); } else { rv = InsertTextAsAction(aQuotedText); NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), @@ -1847,15 +1856,16 @@ HTMLEditor::InsertAsPlaintextQuotation(const nsAString& aQuotedText, // don't need to know the inserted node. if (aNodeInserted && NS_SUCCEEDED(rv)) { - *aNodeInserted = newNode; - NS_IF_ADDREF(*aNodeInserted); + newNode.forget(aNodeInserted); } // Set the selection to just after the inserted node: if (NS_SUCCEEDED(rv) && newNode) { EditorRawDOMPoint afterNewNode(newNode); if (afterNewNode.AdvanceOffset()) { - selection->Collapse(afterNewNode); + DebugOnly rv = selection->Collapse(afterNewNode); + NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), + "Failed to collapse after the new node"); } } diff --git a/editor/libeditor/TextEditor.cpp b/editor/libeditor/TextEditor.cpp index fbe04606c43c..e378445ef195 100644 --- a/editor/libeditor/TextEditor.cpp +++ b/editor/libeditor/TextEditor.cpp @@ -1876,7 +1876,9 @@ TextEditor::PasteAsQuotation(int32_t aSelectionType) nsAutoString stuffToPaste; textDataObj->GetData ( stuffToPaste ); AutoPlaceholderBatch beginBatching(this); - rv = InsertAsQuotation(stuffToPaste, 0); + rv = InsertWithQuotationsAsSubAction(stuffToPaste); + NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), + "Failed to insert the text with quotations"); } } } @@ -1887,6 +1889,12 @@ TextEditor::PasteAsQuotation(int32_t aSelectionType) NS_IMETHODIMP TextEditor::InsertAsQuotation(const nsAString& aQuotedText, nsINode** aNodeInserted) +{ + return NS_ERROR_NOT_IMPLEMENTED; +} + +nsresult +TextEditor::InsertWithQuotationsAsSubAction(const nsAString& aQuotedText) { // Protect the edit rules object from dying RefPtr rules(mRules); @@ -1894,7 +1902,9 @@ TextEditor::InsertAsQuotation(const nsAString& aQuotedText, // Let the citer quote it for us: nsString quotedStuff; nsresult rv = InternetCiter::GetCiteString(aQuotedText, quotedStuff); - NS_ENSURE_SUCCESS(rv, rv); + if (NS_WARN_IF(NS_FAILED(rv))) { + return rv; + } // It's best to put a blank line after the quoted text so that mails // written without thinking won't be so ugly. @@ -1902,16 +1912,18 @@ TextEditor::InsertAsQuotation(const nsAString& aQuotedText, quotedStuff.Append(char16_t('\n')); } - // get selection RefPtr selection = GetSelection(); - NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER); + if (NS_WARN_IF(!selection)) { + return NS_ERROR_FAILURE; + } - AutoPlaceholderBatch beginBatching(this); AutoTopLevelEditSubActionNotifier maybeTopLevelEditSubAction( *this, EditSubAction::eInsertText, nsIEditor::eNext); - // give rules a chance to handle or cancel + // XXX This WillDoAction() usage is hacky. If it returns as handled, + // this method cannot work as expected. So, this should have specific + // sub-action rather than using eInsertElement. EditSubActionInfo subActionInfo(EditSubAction::eInsertElement); bool cancel, handled; rv = rules->WillDoAction(selection, subActionInfo, &cancel, &handled); @@ -1921,17 +1933,16 @@ TextEditor::InsertAsQuotation(const nsAString& aQuotedText, if (cancel) { return NS_OK; // Rules canceled the operation. } + MOZ_ASSERT(handled, "WillDoAction() shouldn't handle in this case"); if (!handled) { + // TODO: Use InsertTextAsSubAction() when bug 1467796 is fixed. rv = InsertTextAsAction(quotedStuff); - NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Failed to insert quoted text"); - - // XXX Should set *aNodeInserted to the first node inserted - if (aNodeInserted && NS_SUCCEEDED(rv)) { - *aNodeInserted = nullptr; + if (NS_WARN_IF(NS_FAILED(rv))) { + return rv; } } // XXX Why don't we call TextEditRules::DidDoAction()? - return rv; + return NS_OK; } NS_IMETHODIMP diff --git a/editor/libeditor/TextEditor.h b/editor/libeditor/TextEditor.h index 0520790523c5..15883b5d1fba 100644 --- a/editor/libeditor/TextEditor.h +++ b/editor/libeditor/TextEditor.h @@ -332,6 +332,15 @@ protected: // Shouldn't be used by friend classes int32_t aDestOffset, bool aDoDeleteSelection) override; + /** + * InsertWithQuotationsAsSubAction() inserts aQuotedText with appending ">" + * to start of every line. + * + * @param aQuotedText String to insert. This will be quoted by ">" + * automatically. + */ + nsresult InsertWithQuotationsAsSubAction(const nsAString& aQuotedText); + /** * Return true if the data is safe to insert as the source and destination * principals match, or we are in a editor context where this doesn't matter.