Skip to content
This repository has been archived by the owner on Jul 30, 2022. It is now read-only.

Commit

Permalink
Bug 1655392 - part 6: Split the text deletion part of start and/or en…
Browse files Browse the repository at this point in the history
…d of range off from `HandleDeleteNonCollapsedRanges` r=m_kato

Differential Revision: https://phabricator.services.mozilla.com/D86791
  • Loading branch information
masayuki-nakano committed Aug 17, 2020
1 parent d3cae8b commit 7f61fd9
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 28 deletions.
68 changes: 40 additions & 28 deletions editor/libeditor/HTMLEditSubActionHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3609,6 +3609,40 @@ Result<bool, nsresult> HTMLEditor::AutoBlockElementsJoiner::
return join;
}

nsresult HTMLEditor::AutoBlockElementsJoiner::DeleteTextAtStartAndEndOfRange(
HTMLEditor& aHTMLEditor, nsRange& aRange) {
EditorDOMPoint rangeStart(aRange.StartRef());
EditorDOMPoint rangeEnd(aRange.EndRef());
if (rangeStart.IsInTextNode() && !rangeStart.IsEndOfContainer()) {
// Delete to last character
OwningNonNull<Text> textNode = *rangeStart.GetContainerAsText();
nsresult rv = aHTMLEditor.DeleteTextWithTransaction(
textNode, rangeStart.Offset(),
rangeStart.GetContainer()->Length() - rangeStart.Offset());
if (NS_WARN_IF(aHTMLEditor.Destroyed())) {
return NS_ERROR_EDITOR_DESTROYED;
}
if (NS_FAILED(rv)) {
NS_WARNING("HTMLEditor::DeleteTextWithTransaction() failed");
return rv;
}
}
if (rangeEnd.IsInTextNode() && !rangeEnd.IsStartOfContainer()) {
// Delete to first character
OwningNonNull<Text> textNode = *rangeEnd.GetContainerAsText();
nsresult rv =
aHTMLEditor.DeleteTextWithTransaction(textNode, 0, rangeEnd.Offset());
if (NS_WARN_IF(aHTMLEditor.Destroyed())) {
return NS_ERROR_EDITOR_DESTROYED;
}
if (NS_FAILED(rv)) {
NS_WARNING("HTMLEditor::DeleteTextWithTransaction() failed");
return rv;
}
}
return NS_OK;
}

EditActionResult
HTMLEditor::AutoBlockElementsJoiner::HandleDeleteNonCollapsedRanges(
HTMLEditor& aHTMLEditor, nsIEditor::EDirection aDirectionAndAmount,
Expand Down Expand Up @@ -3653,34 +3687,12 @@ HTMLEditor::AutoBlockElementsJoiner::HandleDeleteNonCollapsedRanges(
// text node is found, we can delete to end or to begining as
// appropriate, since the case where both sel endpoints in same text
// node was already handled (we wouldn't be here)
EditorDOMPoint firstRangeStart(aRangesToDelete.FirstRangeRef()->StartRef());
EditorDOMPoint firstRangeEnd(aRangesToDelete.FirstRangeRef()->EndRef());
if (firstRangeStart.IsInTextNode() && !firstRangeStart.IsEndOfContainer()) {
// Delete to last character
OwningNonNull<Text> textNode = *firstRangeStart.GetContainerAsText();
nsresult rv = aHTMLEditor.DeleteTextWithTransaction(
textNode, firstRangeStart.Offset(),
firstRangeStart.GetContainer()->Length() - firstRangeStart.Offset());
if (NS_WARN_IF(aHTMLEditor.Destroyed())) {
return result.SetResult(NS_ERROR_EDITOR_DESTROYED);
}
if (NS_FAILED(rv)) {
NS_WARNING("HTMLEditor::DeleteTextWithTransaction() failed");
return result.SetResult(rv);
}
}
if (firstRangeEnd.IsInTextNode() && !firstRangeEnd.IsStartOfContainer()) {
// Delete to first character
OwningNonNull<Text> textNode = *firstRangeEnd.GetContainerAsText();
nsresult rv = aHTMLEditor.DeleteTextWithTransaction(
textNode, 0, firstRangeEnd.Offset());
if (NS_WARN_IF(aHTMLEditor.Destroyed())) {
return result.SetResult(NS_ERROR_EDITOR_DESTROYED);
}
if (NS_FAILED(rv)) {
NS_WARNING("HTMLEditor::DeleteTextWithTransaction() failed");
return result.SetResult(rv);
}
nsresult rv = DeleteTextAtStartAndEndOfRange(
aHTMLEditor, MOZ_KnownLive(aRangesToDelete.FirstRangeRef()));
if (NS_FAILED(rv)) {
NS_WARNING(
"AutoBlockElementsJoiner::DeleteTextAtStartAndEndOfRange() failed");
return EditActionHandled(rv);
}

if (joinInclusiveAncestorBlockElements) {
Expand Down
7 changes: 7 additions & 0 deletions editor/libeditor/HTMLEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -2950,6 +2950,13 @@ class HTMLEditor final : public TextEditor,
DeleteContentButKeepTableStructure(HTMLEditor& aHTMLEditor,
nsIContent& aContent);

/**
* DeleteTextAtStartAndEndOfRange() removes text if start and/or end of
* aRange is in a text node.
*/
[[nodiscard]] MOZ_CAN_RUN_SCRIPT nsresult
DeleteTextAtStartAndEndOfRange(HTMLEditor& aHTMLEditor, nsRange& aRange);

enum class Mode {
NotInitialized,
JoinCurrentBlock,
Expand Down

0 comments on commit 7f61fd9

Please sign in to comment.