Bug 1574852 - part 71: Move HTMLEditRules::WillRemoveList() to HTMLEditor r=m_kato

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

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Masayuki Nakano 2019-09-06 06:01:57 +00:00
parent cfd3ee7e70
commit de7cc1a3a0
6 changed files with 44 additions and 70 deletions

View File

@ -794,17 +794,6 @@ nsresult HTMLEditRules::WillDoAction(EditSubActionInfo& aInfo, bool* aCancel,
return WillRemoveAbsolutePosition(aCancel, aHandled);
case EditSubAction::eSetOrClearAlignment:
return WillAlign(*aInfo.alignType, aCancel, aHandled);
case EditSubAction::eRemoveList: {
nsresult rv = WillRemoveList(aCancel, aHandled);
if (NS_WARN_IF(rv == NS_ERROR_EDITOR_DESTROYED) ||
NS_WARN_IF(!CanHandleEditAction())) {
return NS_ERROR_EDITOR_DESTROYED;
}
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return NS_OK;
}
case EditSubAction::eInsertElement:
case EditSubAction::eInsertQuotedText: {
nsresult rv = MOZ_KnownLive(HTMLEditorRef()).WillInsert(aCancel);
@ -825,6 +814,7 @@ nsresult HTMLEditRules::WillDoAction(EditSubActionInfo& aInfo, bool* aCancel,
case EditSubAction::eInsertParagraphSeparator:
case EditSubAction::eUndo:
case EditSubAction::eRedo:
case EditSubAction::eRemoveList:
MOZ_ASSERT_UNREACHABLE("This path should've been dead code");
return NS_ERROR_UNEXPECTED;
default:
@ -871,6 +861,7 @@ nsresult HTMLEditRules::DidDoAction(EditSubActionInfo& aInfo,
case EditSubAction::eInsertParagraphSeparator:
case EditSubAction::eUndo:
case EditSubAction::eRedo:
case EditSubAction::eRemoveList:
MOZ_ASSERT_UNREACHABLE("This path should've been dead code");
return NS_ERROR_UNEXPECTED;
default:
@ -4672,34 +4663,36 @@ EditActionResult HTMLEditor::ChangeSelectedHardLinesToList(
return EditActionHandled();
}
nsresult HTMLEditRules::WillRemoveList(bool* aCancel, bool* aHandled) {
MOZ_ASSERT(IsEditorDataAvailable());
nsresult HTMLEditor::RemoveListAtSelectionAsSubAction() {
MOZ_ASSERT(IsEditActionDataAvailable());
if (NS_WARN_IF(!aCancel) || NS_WARN_IF(!aHandled)) {
return NS_ERROR_INVALID_ARG;
EditActionResult result = CanHandleHTMLEditSubAction();
NS_WARNING_ASSERTION(result.Succeeded(),
"CanHandleHTMLEditSubAction() failed");
if (result.Failed() || result.Canceled()) {
return result.Rv();
}
// initialize out param
*aCancel = false;
*aHandled = true;
AutoPlaceholderBatch treatAsOneTransaction(*this);
AutoEditSubActionNotifier startToHandleEditSubAction(
*this, EditSubAction::eRemoveList, nsIEditor::eNext);
if (!SelectionRefPtr()->IsCollapsed()) {
nsresult rv = MOZ_KnownLive(HTMLEditorRef())
.MaybeExtendSelectionToHardLineEdgesForBlockEditAction();
nsresult rv = MaybeExtendSelectionToHardLineEdgesForBlockEditAction();
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
}
AutoSelectionRestorer restoreSelectionLater(HTMLEditorRef());
AutoSelectionRestorer restoreSelectionLater(*this);
AutoTArray<OwningNonNull<nsINode>, 64> arrayOfNodes;
{
AutoTransactionsConserveSelection dontChangeMySelection(HTMLEditorRef());
AutoTransactionsConserveSelection dontChangeMySelection(*this);
nsresult rv =
MOZ_KnownLive(HTMLEditorRef())
.SplitInlinesAndCollectEditTargetNodesInExtendedSelectionRanges(
arrayOfNodes, EditSubAction::eCreateOrChangeList,
HTMLEditor::CollectNonEditableNodes::No);
SplitInlinesAndCollectEditTargetNodesInExtendedSelectionRanges(
arrayOfNodes, EditSubAction::eCreateOrChangeList,
CollectNonEditableNodes::No);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
@ -4708,7 +4701,7 @@ nsresult HTMLEditRules::WillRemoveList(bool* aCancel, bool* aHandled) {
// Remove all non-editable nodes. Leave them be.
for (int32_t i = arrayOfNodes.Length() - 1; i >= 0; i--) {
OwningNonNull<nsINode> testNode = arrayOfNodes[i];
if (!HTMLEditorRef().IsEditable(testNode)) {
if (!IsEditable(testNode)) {
arrayOfNodes.RemoveElementAt(i);
}
}
@ -4718,21 +4711,21 @@ nsresult HTMLEditRules::WillRemoveList(bool* aCancel, bool* aHandled) {
// here's where we actually figure out what to do
if (HTMLEditUtils::IsListItem(curNode)) {
// unlist this listitem
nsresult rv = MOZ_KnownLive(HTMLEditorRef())
.LiftUpListItemElement(
MOZ_KnownLive(*curNode->AsElement()),
HTMLEditor::LiftUpFromAllParentListElements::Yes);
nsresult rv = LiftUpListItemElement(MOZ_KnownLive(*curNode->AsElement()),
LiftUpFromAllParentListElements::Yes);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
} else if (HTMLEditUtils::IsList(curNode)) {
continue;
}
if (HTMLEditUtils::IsList(curNode)) {
// node is a list, move list items out
nsresult rv = MOZ_KnownLive(HTMLEditorRef())
.DestroyListStructureRecursively(
MOZ_KnownLive(*curNode->AsElement()));
nsresult rv =
DestroyListStructureRecursively(MOZ_KnownLive(*curNode->AsElement()));
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
continue;
}
}
return NS_OK;

View File

@ -125,17 +125,6 @@ class HTMLEditRules : public TextEditRules {
*/
MOZ_CAN_RUN_SCRIPT MOZ_MUST_USE nsresult DidDeleteSelection();
/**
* Called before removing a list element. This method actually removes
* list elements and list item elements at Selection. And move contents
* in them where the removed list was.
*
* @param aCancel Returns true if the operation is canceled.
* @param aHandled Returns true if the edit action is handled.
*/
MOZ_CAN_RUN_SCRIPT
MOZ_MUST_USE nsresult WillRemoveList(bool* aCancel, bool* aHandled);
/**
* Called before indenting around Selection. This method actually tries to
* indent the contents.

View File

@ -2091,27 +2091,10 @@ nsresult HTMLEditor::RemoveListAsAction(const nsAString& aListType,
return NS_ERROR_NOT_INITIALIZED;
}
// Protect the edit rules object from dying
RefPtr<TextEditRules> rules(mRules);
AutoPlaceholderBatch treatAsOneTransaction(*this);
AutoEditSubActionNotifier startToHandleEditSubAction(
*this, EditSubAction::eRemoveList, nsIEditor::eNext);
EditSubActionInfo subActionInfo(EditSubAction::eRemoveList);
bool cancel, handled;
nsresult rv = rules->WillDoAction(subActionInfo, &cancel, &handled);
if (cancel || NS_FAILED(rv)) {
return EditorBase::ToGenericNSResult(rv);
}
// no default behavior for this yet. what would it mean?
rv = rules->DidDoAction(subActionInfo, rv);
if (NS_WARN_IF(NS_FAILED(rv))) {
return EditorBase::ToGenericNSResult(rv);
}
return NS_OK;
nsresult rv = RemoveListAtSelectionAsSubAction();
NS_WARNING_ASSERTION(NS_FAILED(rv),
"RemoveListAtSelectionAsSubAction() failed");
return rv;
}
nsresult HTMLEditor::FormatBlockContainerAsSubAction(nsAtom& aTagName) {

View File

@ -262,8 +262,8 @@ class HTMLEditor final : public TextEditor,
nsresult AlignAsAction(const nsAString& aAlignType,
nsIPrincipal* aPrincipal = nullptr);
nsresult RemoveListAsAction(const nsAString& aListType,
nsIPrincipal* aPrincipal = nullptr);
MOZ_CAN_RUN_SCRIPT nsresult RemoveListAsAction(
const nsAString& aListType, nsIPrincipal* aPrincipal = nullptr);
/**
* MakeOrChangeListAsAction() makes selected hard lines list element(s).
@ -2316,6 +2316,13 @@ class HTMLEditor final : public TextEditor,
MOZ_CAN_RUN_SCRIPT MOZ_MUST_USE nsresult
DestroyListStructureRecursively(Element& aListElement);
/**
* RemoveListAtSelectionAsSubAction() removes list elements and list item
* elements at Selection. And move contents in them where the removed list
* was.
*/
MOZ_CAN_RUN_SCRIPT MOZ_MUST_USE nsresult RemoveListAtSelectionAsSubAction();
protected: // Called by helper classes.
virtual void OnStartToHandleTopLevelEditSubAction(
EditSubAction aEditSubAction, nsIEditor::EDirection aDirection) override;

View File

@ -404,7 +404,8 @@ nsresult RemoveListCommand::DoCommand(Command aCommand, TextEditor& aTextEditor,
return NS_OK;
}
// This removes any list type
nsresult rv = htmlEditor->RemoveListAsAction(EmptyString(), aPrincipal);
nsresult rv =
MOZ_KnownLive(htmlEditor)->RemoveListAsAction(EmptyString(), aPrincipal);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "RemoveListAsAction() failed");
return rv;
}

View File

@ -293,6 +293,7 @@ interface nsIHTMLEditor : nsISupports
*
* @param aListType Unused.
*/
[can_run_script]
void removeList(in AString aListType);
/**