Bug 1649121: part 2) Factor moving start and end of out `CreateDOMFragmentForPaste`. r=masayuki

Differential Revision: https://phabricator.services.mozilla.com/D81540
This commit is contained in:
Mirko Brodesser 2020-06-30 13:12:29 +00:00
Родитель 9ae430ad1a
Коммит f52a74862a
2 изменённых файлов: 49 добавлений и 28 удалений

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

@ -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<nsINode>* aOutStartNode,
nsCOMPtr<nsINode>* aOutEndNode);
/**
* CollectTopMostChildContentsCompletelyInRange() collects topmost child
* contents which are completely in the given range.

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

@ -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<nsINode>* aOutStartNode,
nsCOMPtr<nsINode>* 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,