From f52a74862abf96917858186e394c1d9b979a1288 Mon Sep 17 00:00:00 2001 From: Mirko Brodesser Date: Tue, 30 Jun 2020 13:12:29 +0000 Subject: [PATCH] Bug 1649121: part 2) Factor moving start and end of out `CreateDOMFragmentForPaste`. r=masayuki Differential Revision: https://phabricator.services.mozilla.com/D81540 --- editor/libeditor/HTMLEditor.h | 8 +++ editor/libeditor/HTMLEditorDataTransfer.cpp | 69 ++++++++++++--------- 2 files changed, 49 insertions(+), 28 deletions(-) diff --git a/editor/libeditor/HTMLEditor.h b/editor/libeditor/HTMLEditor.h index 8252e6626341..0aacb4e171c6 100644 --- a/editor/libeditor/HTMLEditor.h +++ b/editor/libeditor/HTMLEditor.h @@ -4396,6 +4396,14 @@ class HTMLEditor final : public TextEditor, nsresult ParseFragment(const nsAString& aStr, nsAtom* aContextLocalName, Document* aTargetDoc, dom::DocumentFragment** aFragment, bool aTrustedInput); + + /** + * @param aInfoStr as indicated by nsITransferable's kHTMLInfo. + */ + [[nodiscard]] static nsresult MoveStartAndEndAccordingToHTMLInfo( + const nsAString& aInfoStr, nsCOMPtr* aOutStartNode, + nsCOMPtr* aOutEndNode); + /** * CollectTopMostChildContentsCompletelyInRange() collects topmost child * contents which are completely in the given range. diff --git a/editor/libeditor/HTMLEditorDataTransfer.cpp b/editor/libeditor/HTMLEditorDataTransfer.cpp index 81e4952c7072..5a8b5fa47384 100644 --- a/editor/libeditor/HTMLEditorDataTransfer.cpp +++ b/editor/libeditor/HTMLEditorDataTransfer.cpp @@ -2996,35 +2996,12 @@ nsresult HTMLEditor::CreateDOMFragmentFromPaste( *aOutFragNode = std::move(documentFragmentToInsert); *aOutStartOffset = 0; - // get the infoString contents if (!aInfoStr.IsEmpty()) { - int32_t sep = aInfoStr.FindChar((char16_t)','); - nsAutoString numstr1(Substring(aInfoStr, 0, sep)); - nsAutoString numstr2( - Substring(aInfoStr, sep + 1, aInfoStr.Length() - (sep + 1))); - - // Move the start and end children. - nsresult rvIgnored; - int32_t num = numstr1.ToInteger(&rvIgnored); - NS_WARNING_ASSERTION(NS_SUCCEEDED(rvIgnored), - "nsAString::ToInteger() failed, but ignored"); - while (num--) { - nsINode* tmp = (*aOutStartNode)->GetFirstChild(); - if (!tmp) { - NS_WARNING("aOutStartNode did not have children"); - return NS_ERROR_FAILURE; - } - *aOutStartNode = tmp; - } - - num = numstr2.ToInteger(&rvIgnored); - while (num--) { - nsINode* tmp = (*aOutEndNode)->GetLastChild(); - if (!tmp) { - NS_WARNING("aOutEndNode did not have children"); - return NS_ERROR_FAILURE; - } - *aOutEndNode = tmp; + const nsresult rv = HTMLEditor::MoveStartAndEndAccordingToHTMLInfo( + aInfoStr, aOutStartNode, aOutEndNode); + if (NS_FAILED(rv)) { + NS_WARNING("HTMLEditor::MoveStartAndEndAccordingToHTMLInfo() failed"); + return rv; } } @@ -3032,6 +3009,42 @@ nsresult HTMLEditor::CreateDOMFragmentFromPaste( return NS_OK; } +// static +nsresult HTMLEditor::MoveStartAndEndAccordingToHTMLInfo( + const nsAString& aInfoStr, nsCOMPtr* aOutStartNode, + nsCOMPtr* aOutEndNode) { + int32_t sep = aInfoStr.FindChar((char16_t)','); + nsAutoString numstr1(Substring(aInfoStr, 0, sep)); + nsAutoString numstr2( + Substring(aInfoStr, sep + 1, aInfoStr.Length() - (sep + 1))); + + // Move the start and end children. + nsresult rvIgnored; + int32_t num = numstr1.ToInteger(&rvIgnored); + NS_WARNING_ASSERTION(NS_SUCCEEDED(rvIgnored), + "nsAString::ToInteger() failed, but ignored"); + while (num--) { + nsINode* tmp = (*aOutStartNode)->GetFirstChild(); + if (!tmp) { + NS_WARNING("aOutStartNode did not have children"); + return NS_ERROR_FAILURE; + } + *aOutStartNode = tmp; + } + + num = numstr2.ToInteger(&rvIgnored); + while (num--) { + nsINode* tmp = (*aOutEndNode)->GetLastChild(); + if (!tmp) { + NS_WARNING("aOutEndNode did not have children"); + return NS_ERROR_FAILURE; + } + *aOutEndNode = tmp; + } + + return NS_OK; +} + nsresult HTMLEditor::ParseFragment(const nsAString& aFragStr, nsAtom* aContextLocalName, Document* aTargetDocument,