Bug 1627175 - part 60: Move `HTMLEditor::GetNearestAncestorListItemElement()` to `HTMLEditUtils` r=m_kato

Differential Revision: https://phabricator.services.mozilla.com/D115169
This commit is contained in:
Masayuki Nakano 2021-05-17 22:12:13 +00:00
Родитель 21110b26dd
Коммит 57b8116405
3 изменённых файлов: 45 добавлений и 31 удалений

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

@ -1402,7 +1402,8 @@ EditActionResult HTMLEditor::InsertParagraphSeparatorAsSubAction() {
MOZ_ASSERT(resultOfInsertingBRElement.inspect());
}
RefPtr<Element> listItem = GetNearestAncestorListItemElement(*blockElement);
RefPtr<Element> listItem = HTMLEditUtils::GetClosestAncestorListItemElement(
*blockElement, editingHost);
if (listItem && listItem != editingHost) {
nsresult rv = HandleInsertParagraphInListItemElement(
*listItem, MOZ_KnownLive(*atStartOfSelection.GetContainer()),
@ -3794,6 +3795,11 @@ nsresult HTMLEditor::HandleHTMLIndentAtSelectionInternal() {
return rv;
}
RefPtr<Element> editingHost = GetActiveEditingHost();
if (NS_WARN_IF(!editingHost)) {
return NS_ERROR_FAILURE;
}
// Ok, now go through all the nodes and put them in a blockquote,
// or whatever is appropriate. Wohoo!
RefPtr<Element> curList, curQuote, indentedLI;
@ -3832,7 +3838,9 @@ nsresult HTMLEditor::HandleHTMLIndentAtSelectionInternal() {
// we only want to indent that li once, we must keep track of the most
// recent indented list item, and not indent it if we find another node
// to act on that is still inside the same li.
if (RefPtr<Element> listItem = GetNearestAncestorListItemElement(content)) {
if (RefPtr<Element> listItem =
HTMLEditUtils::GetClosestAncestorListItemElement(content,
editingHost)) {
if (indentedLI == listItem) {
// already indented this list item
continue;
@ -6380,27 +6388,6 @@ void HTMLEditor::MakeTransitionList(
}
}
Element* HTMLEditor::GetNearestAncestorListItemElement(
nsIContent& aContent) const {
// XXX Why don't we test whether aContent is in an editing host?
if (HTMLEditUtils::IsListItem(&aContent)) {
return aContent.AsElement();
}
// XXX This loop is too expensive since calling IsDescendantOfEditorRoot()
// a lot. Rewrite this with caching active editing host and check
// whether we reach it or not.
for (Element* parentElement = aContent.GetParentElement();
parentElement && IsDescendantOfEditorRoot(parentElement) &&
!HTMLEditUtils::IsAnyTableElement(parentElement);
parentElement = parentElement->GetParentElement()) {
if (HTMLEditUtils::IsListItem(parentElement)) {
return parentElement;
}
}
return nullptr;
}
nsresult HTMLEditor::HandleInsertParagraphInHeadingElement(Element& aHeader,
nsINode& aNode,
int32_t aOffset) {
@ -9346,6 +9333,11 @@ nsresult HTMLEditor::MoveSelectedContentsToDivElementToMakeItAbsolutePosition(
return rv;
}
RefPtr<Element> editingHost = GetActiveEditingHost();
if (NS_WARN_IF(!editingHost)) {
return NS_ERROR_FAILURE;
}
// `<div>` element to be positioned absolutely. This may have already
// existed or newly created by this method.
RefPtr<Element> targetDivElement;
@ -9433,7 +9425,8 @@ nsresult HTMLEditor::MoveSelectedContentsToDivElementToMakeItAbsolutePosition(
// node into the target `<div>` element with the list item element itself
// because we want to keep indent level of the contents.
if (RefPtr<Element> listItemElement =
GetNearestAncestorListItemElement(content)) {
HTMLEditUtils::GetClosestAncestorListItemElement(content,
editingHost)) {
if (handledListItemElement == listItemElement) {
// Current node has already been moved into the `<div>` element.
continue;

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

@ -1094,6 +1094,34 @@ class HTMLEditUtils final {
static Element* GetClosestAncestorAnyListElement(const nsIContent& aContent);
/**
* GetClosestAncestorListItemElement() returns a list item element if
* aContent or its ancestor in editing host is one. However, this won't
* cross table related element.
*/
static Element* GetClosestAncestorListItemElement(
const nsIContent& aContent, const Element* aAncestorLimit = nullptr) {
MOZ_ASSERT_IF(aAncestorLimit,
aContent.IsInclusiveDescendantOf(aAncestorLimit));
if (HTMLEditUtils::IsListItem(&aContent)) {
return const_cast<Element*>(aContent.AsElement());
}
for (Element* parentElement : aContent.AncestorsOfType<Element>()) {
if (HTMLEditUtils::IsAnyTableElement(parentElement)) {
return nullptr;
}
if (HTMLEditUtils::IsListItem(parentElement)) {
return parentElement;
}
if (parentElement == aAncestorLimit) {
return nullptr;
}
}
return nullptr;
}
/**
* GetMostDistantAncestorInlineElement() returns the most distant ancestor
* inline element between aContent and the aEditingHost. Even if aEditingHost

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

@ -1647,13 +1647,6 @@ class HTMLEditor final : public TextEditor,
HandleInsertParagraphInListItemElement(Element& aListItem, nsINode& aNode,
int32_t aOffset);
/**
* GetNearestAncestorListItemElement() returns a list item element if
* aContent or its ancestor in editing host is one. However, this won't
* cross table related element.
*/
Element* GetNearestAncestorListItemElement(nsIContent& aContent) const;
/**
* InsertParagraphSeparatorAsSubAction() handles insertPargraph commad
* (i.e., handling Enter key press) with the above helper methods.