Bug 1810902 - Make `HTMLEditUtils::GetRangeSelectingAllContentInAllListItems` return correct range r=m_kato

`AutoDeleteRangesHandler::Run` shrink the range not to delete list item if
the list is not empty.  However, due to the bug of
`HTMLEditUtils::GetRangeSelectingAllContentInAllListItems`, it may get empty
range (in the testcase, first the only `<li>` element is the list has empty
text node as first child, and the range is collapsed into it).  Therefore,
`AutoDeleteRangesHandler::Run` gives up to delete the list items, but does not
update the `Selection`.  Therefore, from the caller point of view, `Selection`
is unexpectedly not collapsed even after deleting `Selection`.

Therefore, this patch also makes `AutoDeleteRangeHandler::Run` collapse
`Selection` if it gives up to delete something.

Differential Revision: https://phabricator.services.mozilla.com/D169044
This commit is contained in:
Masayuki Nakano 2023-02-15 23:00:12 +00:00
Родитель bfb8a73f52
Коммит cc43626912
5 изменённых файлов: 57 добавлений и 11 удалений

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

@ -1457,17 +1457,8 @@ class HTMLEditUtils final {
return EditorDOMRangeType();
}
return EditorDOMRangeType(
typename EditorDOMRangeType::PointType(
firstListItem->GetFirstChild() &&
firstListItem->GetFirstChild()->IsText()
? firstListItem->GetFirstChild()
: static_cast<nsIContent*>(firstListItem),
0u),
EditorDOMRangeType::PointType::AtEndOf(
lastListItem->GetLastChild() &&
lastListItem->GetLastChild()->IsText()
? *lastListItem->GetFirstChild()
: static_cast<nsIContent&>(*lastListItem)));
typename EditorDOMRangeType::PointType(firstListItem, 0u),
EditorDOMRangeType::PointType::AtEndOf(*lastListItem));
}
/**

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

@ -3372,6 +3372,14 @@ HTMLEditor::AutoDeleteRangesHandler::HandleDeleteNonCollapsedRanges(
}
if (NS_WARN_IF(aRangesToDelete.FirstRangeRef()->Collapsed())) {
// Hmm, there is nothing to delete...?
// In this case, the callers want collapsed selection. Therefore, we need
// to change the `Selection` here.
nsresult rv = aHTMLEditor.CollapseSelectionTo(
aRangesToDelete.GetFirstRangeStartPoint<EditorRawDOMPoint>());
if (NS_FAILED(rv)) {
NS_WARNING("EditorBase::CollapseSelectionTo() failed");
return Err(rv);
}
return EditActionResult::HandledResult();
}
MOZ_ASSERT(aRangesToDelete.IsFirstRangeEditable(aEditingHost));

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

@ -0,0 +1,33 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script>
document.addEventListener("DOMContentLoaded", () => {
document.execCommand("selectAll");
const editingHost = document.querySelector("thead[contenteditable]");
editingHost.addEventListener("focus", () => {
editingHost.addEventListener("focusout", () => {
document.execCommand("italic");
document.execCommand("insertText", false, "A");
document.execCommand("insertUnorderedList");
});
document.designMode = "on";
document.execCommand("selectAll");
document.execCommand("insertHTML", false, "A");
});
editingHost.focus();
});
</script>
</head>
<body>
<big>
</big>
<table>
<thead contenteditable>
</thead></table>
<p>
<object></object>
</p>
</body>
</html>

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

@ -3012,4 +3012,11 @@ var browserTests = [
"<ol>\n<li>{}<br></li></ol>"],
[true],
{}],
// Select all list item children when list items have multiple nodes.
["{<ul><li>abc<span>def</span>ghi</li><li>jkl<span>opq</span>rst</li></ul>}",
[["delete",""]],
"<ul><li>{}<br></li></ul>",
[true],
{}],
]

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

@ -2892,4 +2892,11 @@ var browserTests = [
"<ol>\n<li>{}<br></li></ol>"],
[true],
{}],
// Select all list item children when list items have multiple nodes.
["{<ul><li>abc<span>def</span>ghi</li><li>jkl<span>opq</span>rst</li></ul>}",
[["delete",""]],
"<ul><li>{}<br></li></ul>",
[true],
{}],
]