Bug 1574852 - part 54: Move HTMLEditRules::ConvertListType() to HTMLEditor r=m_kato

Differential Revision: https://phabricator.services.mozilla.com/D44193

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Masayuki Nakano 2019-09-03 05:13:35 +00:00
parent 2fc50edf06
commit d4fab048af
3 changed files with 69 additions and 61 deletions

View File

@ -3709,8 +3709,10 @@ EditActionResult HTMLEditRules::TryToJoinBlocksWithTransaction(
return EditActionIgnored(NS_ERROR_EDITOR_DESTROYED);
}
if (pt.IsSet() && mergeLists) {
CreateElementResult convertListTypeResult = ConvertListType(
*rightBlock, MOZ_KnownLive(*existingList), *nsGkAtoms::li);
CreateElementResult convertListTypeResult =
MOZ_KnownLive(HTMLEditorRef())
.ChangeListElementType(*rightBlock, MOZ_KnownLive(*existingList),
*nsGkAtoms::li);
if (NS_WARN_IF(convertListTypeResult.Rv() == NS_ERROR_EDITOR_DESTROYED)) {
return EditActionIgnored(NS_ERROR_EDITOR_DESTROYED);
}
@ -4205,8 +4207,8 @@ nsresult HTMLEditRules::MakeList(nsAtom& aListType, bool aEntireList,
if (curList && !EditorUtils::IsDescendantOf(*curNode, *curList)) {
// move all of our children into curList. cheezy way to do it: move
// whole list and then RemoveContainerWithTransaction() on the list.
// ConvertListType first: that routine handles converting the list
// item types, if needed.
// ChangeListElementType() first: that routine handles converting the
// list item types, if needed.
nsresult rv = MOZ_KnownLive(HTMLEditorRef())
.MoveNodeToEndWithTransaction(*curNode, *curList);
if (NS_WARN_IF(!CanHandleEditAction())) {
@ -4215,8 +4217,10 @@ nsresult HTMLEditRules::MakeList(nsAtom& aListType, bool aEntireList,
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
CreateElementResult convertListTypeResult = ConvertListType(
MOZ_KnownLive(*curNode->AsElement()), aListType, aItemType);
CreateElementResult convertListTypeResult =
MOZ_KnownLive(HTMLEditorRef())
.ChangeListElementType(MOZ_KnownLive(*curNode->AsElement()),
aListType, aItemType);
if (NS_WARN_IF(convertListTypeResult.Failed())) {
return convertListTypeResult.Rv();
}
@ -4232,8 +4236,10 @@ nsresult HTMLEditRules::MakeList(nsAtom& aListType, bool aEntireList,
newBlock = convertListTypeResult.forget();
} else {
// replace list with new list type
CreateElementResult convertListTypeResult = ConvertListType(
MOZ_KnownLive(*curNode->AsElement()), aListType, aItemType);
CreateElementResult convertListTypeResult =
MOZ_KnownLive(HTMLEditorRef())
.ChangeListElementType(MOZ_KnownLive(*curNode->AsElement()),
aListType, aItemType);
if (NS_WARN_IF(convertListTypeResult.Failed())) {
return convertListTypeResult.Rv();
}
@ -5911,49 +5917,53 @@ SplitRangeOffFromNodeResult HTMLEditRules::OutdentPartOfBlock(
return splitResult;
}
CreateElementResult HTMLEditRules::ConvertListType(Element& aListElement,
CreateElementResult HTMLEditor::ChangeListElementType(Element& aListElement,
nsAtom& aNewListTag,
nsAtom& aNewListItemTag) {
MOZ_ASSERT(IsEditorDataAvailable());
MOZ_ASSERT(IsEditActionDataAvailable());
nsCOMPtr<nsINode> child = aListElement.GetFirstChild();
while (child) {
if (child->IsElement()) {
Element* element = child->AsElement();
if (HTMLEditUtils::IsListItem(element) &&
!element->IsHTMLElement(&aNewListItemTag)) {
child = MOZ_KnownLive(HTMLEditorRef())
.ReplaceContainerWithTransaction(MOZ_KnownLive(*element),
aNewListItemTag);
if (NS_WARN_IF(!CanHandleEditAction())) {
for (nsIContent* childContent = aListElement.GetFirstChild(); childContent;
childContent = childContent->GetNextSibling()) {
if (!childContent->IsElement()) {
continue;
}
if (HTMLEditUtils::IsListItem(childContent->AsElement()) &&
!childContent->IsHTMLElement(&aNewListItemTag)) {
OwningNonNull<Element> listItemElement = *childContent->AsElement();
RefPtr<Element> newListItemElement =
ReplaceContainerWithTransaction(listItemElement, aNewListItemTag);
if (NS_WARN_IF(Destroyed())) {
return CreateElementResult(NS_ERROR_EDITOR_DESTROYED);
}
if (NS_WARN_IF(!child)) {
if (NS_WARN_IF(!newListItemElement)) {
return CreateElementResult(NS_ERROR_FAILURE);
}
} else if (HTMLEditUtils::IsList(element) &&
!element->IsHTMLElement(&aNewListTag)) {
childContent = newListItemElement;
continue;
}
if (HTMLEditUtils::IsList(childContent->AsElement()) &&
!childContent->IsHTMLElement(&aNewListTag)) {
// XXX List elements shouldn't have other list elements as their
// child. Why do we handle such invalid tree?
CreateElementResult convertListTypeResult = ConvertListType(
MOZ_KnownLive(*child->AsElement()), aNewListTag, aNewListItemTag);
// -> Maybe, for bug 525888.
OwningNonNull<Element> listElement = *childContent->AsElement();
CreateElementResult convertListTypeResult =
ChangeListElementType(listElement, aNewListTag, aNewListItemTag);
if (NS_WARN_IF(convertListTypeResult.Failed())) {
return convertListTypeResult;
}
child = convertListTypeResult.forget();
childContent = convertListTypeResult.GetNewNode();
continue;
}
}
child = child->GetNextSibling();
}
if (aListElement.IsHTMLElement(&aNewListTag)) {
return CreateElementResult(&aListElement);
}
RefPtr<Element> listElement =
MOZ_KnownLive(HTMLEditorRef())
.ReplaceContainerWithTransaction(aListElement, aNewListTag);
if (NS_WARN_IF(!CanHandleEditAction())) {
ReplaceContainerWithTransaction(aListElement, aNewListTag);
if (NS_WARN_IF(Destroyed())) {
return CreateElementResult(NS_ERROR_EDITOR_DESTROYED);
}
NS_WARNING_ASSERTION(listElement != nullptr, "Failed to create list element");

View File

@ -520,25 +520,6 @@ class HTMLEditRules : public TextEditRules {
const nsAString* aBulletType, bool* aCancel,
nsAtom& aItemType);
/**
* ConvertListType() replaces child list items of aListElement with
* new list item element whose tag name is aNewListItemTag.
* Note that if there are other list elements as children of aListElement,
* this calls itself recursively even though it's invalid structure.
*
* @param aListElement The list element whose list items will be
* replaced.
* @param aNewListTag New list tag name.
* @param aNewListItemTag New list item tag name.
* @return New list element or an error code if it fails.
* New list element may be aListElement if its
* tag name is same as aNewListTag.
*/
MOZ_CAN_RUN_SCRIPT
MOZ_MUST_USE CreateElementResult ConvertListType(Element& aListElement,
nsAtom& aListType,
nsAtom& aItemType);
/**
* MaybeDeleteTopMostEmptyAncestor() looks for top most empty block ancestor
* of aStartNode in aEditingHostElement.

View File

@ -1867,6 +1867,23 @@ class HTMLEditor final : public TextEditor,
*/
static bool NodesInDifferentTableElements(nsINode& aNode1, nsINode& aNode2);
/**
* ChangeListElementType() replaces child list items of aListElement with
* new list item element whose tag name is aNewListItemTag.
* Note that if there are other list elements as children of aListElement,
* this calls itself recursively even though it's invalid structure.
*
* @param aListElement The list element whose list items will be
* replaced.
* @param aNewListTag New list tag name.
* @param aNewListItemTag New list item tag name.
* @return New list element or an error code if it fails.
* New list element may be aListElement if its
* tag name is same as aNewListTag.
*/
MOZ_CAN_RUN_SCRIPT MOZ_MUST_USE CreateElementResult ChangeListElementType(
Element& aListElement, nsAtom& aListType, nsAtom& aItemType);
protected: // Called by helper classes.
virtual void OnStartToHandleTopLevelEditSubAction(
EditSubAction aEditSubAction, nsIEditor::EDirection aDirection) override;