2001-09-26 02:53:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
1999-01-14 21:02:45 +03:00
|
|
|
|
2016-07-07 09:08:33 +03:00
|
|
|
#include "DeleteNodeTransaction.h"
|
Bug 1627175 - part 2: Move `EditorBase::IsModifiableNode()`, `EditorBase::IsEditable()`, `EditorBase::IsTextElementOrText()` and `EditorBase::IsPaddingBRElementForEmptyEditor()` to `EditorUtils` r=m_kato
Due to the include hell, `EditorBase.h` cannot include `EditorUtils.h`.
Therefore we need these 3 methods once. Additionally, `IsModifiableNode()`
is really odd method and looks like that it's used for the following 2 purposes:
1. Simply can be editable.
2. Can be removed from parent.
For the former case, we should sort out it with
`EditorUtils::IsEditableContent()`, but for now, this patch moves it to
`HTMLEditUtils::IsSimplyEditable()`. On the other hand, for the latter case,
we obviously has a bug. Therefore, this patch creates
`HTMLEditUtils::IsRemovableFromParentNode()` and make it check whether the
removing node is also editable.
Unfortunately, `EditorUtils::IsEditableContent()` needs to take editor type.
But it's most callers are in `HTMLEditor` and most of remains are in
common methods of `EditorBase`. I guess we could remove this ugly argument
in the future.
Depends on D70874
Differential Revision: https://phabricator.services.mozilla.com/D70875
--HG--
extra : moz-landing-system : lando
2020-04-15 18:27:38 +03:00
|
|
|
|
|
|
|
#include "HTMLEditUtils.h"
|
2016-07-08 07:10:13 +03:00
|
|
|
#include "mozilla/EditorBase.h"
|
2016-07-09 05:34:41 +03:00
|
|
|
#include "mozilla/SelectionState.h" // RangeUpdater
|
2020-04-27 06:36:57 +03:00
|
|
|
#include "mozilla/TextEditor.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsAString.h"
|
1999-01-21 22:44:26 +03:00
|
|
|
|
2016-07-07 09:08:33 +03:00
|
|
|
namespace mozilla {
|
1999-01-21 04:51:09 +03:00
|
|
|
|
2017-12-15 15:24:33 +03:00
|
|
|
// static
|
|
|
|
already_AddRefed<DeleteNodeTransaction> DeleteNodeTransaction::MaybeCreate(
|
2020-04-03 11:30:37 +03:00
|
|
|
EditorBase& aEditorBase, nsIContent& aContentToDelete) {
|
2017-12-15 15:24:33 +03:00
|
|
|
RefPtr<DeleteNodeTransaction> transaction =
|
2020-04-03 11:30:37 +03:00
|
|
|
new DeleteNodeTransaction(aEditorBase, aContentToDelete);
|
2017-12-15 15:24:33 +03:00
|
|
|
if (NS_WARN_IF(!transaction->CanDoIt())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return transaction.forget();
|
|
|
|
}
|
|
|
|
|
2017-03-10 07:23:40 +03:00
|
|
|
DeleteNodeTransaction::DeleteNodeTransaction(EditorBase& aEditorBase,
|
2020-04-03 11:30:37 +03:00
|
|
|
nsIContent& aContentToDelete)
|
2017-03-21 13:00:36 +03:00
|
|
|
: mEditorBase(&aEditorBase),
|
2020-04-03 11:30:37 +03:00
|
|
|
mContentToDelete(&aContentToDelete),
|
|
|
|
mParentNode(aContentToDelete.GetParentNode()) {}
|
1999-01-14 21:02:45 +03:00
|
|
|
|
2016-07-08 03:48:34 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(DeleteNodeTransaction, EditTransactionBase,
|
2020-04-03 11:30:37 +03:00
|
|
|
mEditorBase, mContentToDelete, mParentNode,
|
|
|
|
mRefContent)
|
2009-05-09 08:59:25 +04:00
|
|
|
|
2016-07-08 03:48:34 +03:00
|
|
|
NS_IMPL_ADDREF_INHERITED(DeleteNodeTransaction, EditTransactionBase)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(DeleteNodeTransaction, EditTransactionBase)
|
2016-07-07 09:08:33 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DeleteNodeTransaction)
|
2016-07-08 03:48:34 +03:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(EditTransactionBase)
|
2009-05-09 08:59:25 +04:00
|
|
|
|
2017-03-10 07:23:40 +03:00
|
|
|
bool DeleteNodeTransaction::CanDoIt() const {
|
2020-04-03 11:30:37 +03:00
|
|
|
if (NS_WARN_IF(!mContentToDelete) || NS_WARN_IF(!mEditorBase) ||
|
Bug 1627175 - part 2: Move `EditorBase::IsModifiableNode()`, `EditorBase::IsEditable()`, `EditorBase::IsTextElementOrText()` and `EditorBase::IsPaddingBRElementForEmptyEditor()` to `EditorUtils` r=m_kato
Due to the include hell, `EditorBase.h` cannot include `EditorUtils.h`.
Therefore we need these 3 methods once. Additionally, `IsModifiableNode()`
is really odd method and looks like that it's used for the following 2 purposes:
1. Simply can be editable.
2. Can be removed from parent.
For the former case, we should sort out it with
`EditorUtils::IsEditableContent()`, but for now, this patch moves it to
`HTMLEditUtils::IsSimplyEditable()`. On the other hand, for the latter case,
we obviously has a bug. Therefore, this patch creates
`HTMLEditUtils::IsRemovableFromParentNode()` and make it check whether the
removing node is also editable.
Unfortunately, `EditorUtils::IsEditableContent()` needs to take editor type.
But it's most callers are in `HTMLEditor` and most of remains are in
common methods of `EditorBase`. I guess we could remove this ugly argument
in the future.
Depends on D70874
Differential Revision: https://phabricator.services.mozilla.com/D70875
--HG--
extra : moz-landing-system : lando
2020-04-15 18:27:38 +03:00
|
|
|
!mParentNode) {
|
2017-03-10 07:23:40 +03:00
|
|
|
return false;
|
|
|
|
}
|
Bug 1627175 - part 2: Move `EditorBase::IsModifiableNode()`, `EditorBase::IsEditable()`, `EditorBase::IsTextElementOrText()` and `EditorBase::IsPaddingBRElementForEmptyEditor()` to `EditorUtils` r=m_kato
Due to the include hell, `EditorBase.h` cannot include `EditorUtils.h`.
Therefore we need these 3 methods once. Additionally, `IsModifiableNode()`
is really odd method and looks like that it's used for the following 2 purposes:
1. Simply can be editable.
2. Can be removed from parent.
For the former case, we should sort out it with
`EditorUtils::IsEditableContent()`, but for now, this patch moves it to
`HTMLEditUtils::IsSimplyEditable()`. On the other hand, for the latter case,
we obviously has a bug. Therefore, this patch creates
`HTMLEditUtils::IsRemovableFromParentNode()` and make it check whether the
removing node is also editable.
Unfortunately, `EditorUtils::IsEditableContent()` needs to take editor type.
But it's most callers are in `HTMLEditor` and most of remains are in
common methods of `EditorBase`. I guess we could remove this ugly argument
in the future.
Depends on D70874
Differential Revision: https://phabricator.services.mozilla.com/D70875
--HG--
extra : moz-landing-system : lando
2020-04-15 18:27:38 +03:00
|
|
|
return mEditorBase->IsTextEditor() ||
|
|
|
|
HTMLEditUtils::IsSimplyEditableNode(*mParentNode);
|
1999-01-14 21:02:45 +03:00
|
|
|
}
|
|
|
|
|
2020-03-09 17:57:23 +03:00
|
|
|
NS_IMETHODIMP DeleteNodeTransaction::DoTransaction() {
|
2017-03-10 07:23:40 +03:00
|
|
|
if (NS_WARN_IF(!CanDoIt())) {
|
2012-06-19 17:23:36 +04:00
|
|
|
return NS_OK;
|
1999-01-21 04:51:09 +03:00
|
|
|
}
|
1999-01-14 21:02:45 +03:00
|
|
|
|
Bug 1627175 - part 2: Move `EditorBase::IsModifiableNode()`, `EditorBase::IsEditable()`, `EditorBase::IsTextElementOrText()` and `EditorBase::IsPaddingBRElementForEmptyEditor()` to `EditorUtils` r=m_kato
Due to the include hell, `EditorBase.h` cannot include `EditorUtils.h`.
Therefore we need these 3 methods once. Additionally, `IsModifiableNode()`
is really odd method and looks like that it's used for the following 2 purposes:
1. Simply can be editable.
2. Can be removed from parent.
For the former case, we should sort out it with
`EditorUtils::IsEditableContent()`, but for now, this patch moves it to
`HTMLEditUtils::IsSimplyEditable()`. On the other hand, for the latter case,
we obviously has a bug. Therefore, this patch creates
`HTMLEditUtils::IsRemovableFromParentNode()` and make it check whether the
removing node is also editable.
Unfortunately, `EditorUtils::IsEditableContent()` needs to take editor type.
But it's most callers are in `HTMLEditor` and most of remains are in
common methods of `EditorBase`. I guess we could remove this ugly argument
in the future.
Depends on D70874
Differential Revision: https://phabricator.services.mozilla.com/D70875
--HG--
extra : moz-landing-system : lando
2020-04-15 18:27:38 +03:00
|
|
|
if (mEditorBase->IsTextEditor() && mContentToDelete->IsText()) {
|
2020-04-03 11:30:37 +03:00
|
|
|
uint32_t length = mContentToDelete->AsText()->TextLength();
|
2019-07-22 06:53:36 +03:00
|
|
|
if (length > 0) {
|
|
|
|
mEditorBase->AsTextEditor()->WillDeleteText(length, 0, length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-03 11:30:37 +03:00
|
|
|
// Remember which child mContentToDelete was (by remembering which child was
|
|
|
|
// next). Note that mRefContent can be nullptr.
|
|
|
|
mRefContent = mContentToDelete->GetNextSibling();
|
1999-01-14 21:02:45 +03:00
|
|
|
|
2012-06-19 17:23:36 +04:00
|
|
|
// give range updater a chance. SelAdjDeleteNode() needs to be called
|
2016-06-24 08:44:14 +03:00
|
|
|
// *before* we do the action, unlike some of the other RangeItem update
|
2012-06-19 17:23:36 +04:00
|
|
|
// methods.
|
2020-04-03 11:30:37 +03:00
|
|
|
mEditorBase->RangeUpdaterRef().SelAdjDeleteNode(*mContentToDelete);
|
2002-11-10 18:11:08 +03:00
|
|
|
|
2020-04-03 11:30:37 +03:00
|
|
|
OwningNonNull<nsINode> parentNode = *mParentNode;
|
|
|
|
OwningNonNull<nsIContent> contentToDelete = *mContentToDelete;
|
2012-10-09 16:31:24 +04:00
|
|
|
ErrorResult error;
|
2020-04-03 11:30:37 +03:00
|
|
|
parentNode->RemoveChild(contentToDelete, error);
|
2020-03-09 17:57:23 +03:00
|
|
|
NS_WARNING_ASSERTION(!error.Failed(), "nsINode::RemoveChild() failed");
|
2015-04-27 16:18:51 +03:00
|
|
|
return error.StealNSResult();
|
1999-01-14 21:02:45 +03:00
|
|
|
}
|
|
|
|
|
2020-03-09 17:57:23 +03:00
|
|
|
MOZ_CAN_RUN_SCRIPT_BOUNDARY NS_IMETHODIMP
|
2016-07-07 09:08:33 +03:00
|
|
|
DeleteNodeTransaction::UndoTransaction() {
|
2017-03-10 07:23:40 +03:00
|
|
|
if (NS_WARN_IF(!CanDoIt())) {
|
|
|
|
// This is a legal state, the transaction is a no-op.
|
2012-06-19 17:23:36 +04:00
|
|
|
return NS_OK;
|
2010-01-18 02:11:04 +03:00
|
|
|
}
|
2012-10-09 16:31:24 +04:00
|
|
|
ErrorResult error;
|
2020-04-03 11:30:37 +03:00
|
|
|
OwningNonNull<EditorBase> editorBase = *mEditorBase;
|
|
|
|
OwningNonNull<nsINode> parentNode = *mParentNode;
|
|
|
|
OwningNonNull<nsIContent> contentToDelete = *mContentToDelete;
|
|
|
|
nsCOMPtr<nsIContent> refContent = mRefContent;
|
|
|
|
// XXX Perhaps, we should check `refContent` is a child of `parentNode`,
|
|
|
|
// and if it's not, we should stop undoing or something.
|
|
|
|
parentNode->InsertBefore(contentToDelete, refContent, error);
|
2020-03-09 17:57:23 +03:00
|
|
|
if (error.Failed()) {
|
|
|
|
NS_WARNING("nsINode::InsertBefore() failed");
|
2019-07-22 06:53:36 +03:00
|
|
|
return error.StealNSResult();
|
|
|
|
}
|
Bug 1627175 - part 2: Move `EditorBase::IsModifiableNode()`, `EditorBase::IsEditable()`, `EditorBase::IsTextElementOrText()` and `EditorBase::IsPaddingBRElementForEmptyEditor()` to `EditorUtils` r=m_kato
Due to the include hell, `EditorBase.h` cannot include `EditorUtils.h`.
Therefore we need these 3 methods once. Additionally, `IsModifiableNode()`
is really odd method and looks like that it's used for the following 2 purposes:
1. Simply can be editable.
2. Can be removed from parent.
For the former case, we should sort out it with
`EditorUtils::IsEditableContent()`, but for now, this patch moves it to
`HTMLEditUtils::IsSimplyEditable()`. On the other hand, for the latter case,
we obviously has a bug. Therefore, this patch creates
`HTMLEditUtils::IsRemovableFromParentNode()` and make it check whether the
removing node is also editable.
Unfortunately, `EditorUtils::IsEditableContent()` needs to take editor type.
But it's most callers are in `HTMLEditor` and most of remains are in
common methods of `EditorBase`. I guess we could remove this ugly argument
in the future.
Depends on D70874
Differential Revision: https://phabricator.services.mozilla.com/D70875
--HG--
extra : moz-landing-system : lando
2020-04-15 18:27:38 +03:00
|
|
|
if (editorBase->IsTextEditor() && contentToDelete->IsText()) {
|
2020-04-03 11:30:37 +03:00
|
|
|
uint32_t length = contentToDelete->AsText()->TextLength();
|
2019-07-22 06:53:36 +03:00
|
|
|
if (length > 0) {
|
2020-03-09 17:57:23 +03:00
|
|
|
nsresult rv = MOZ_KnownLive(editorBase->AsTextEditor())
|
|
|
|
->DidInsertText(length, 0, length);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("TextEditor::DidInsertText() failed");
|
|
|
|
return rv;
|
2019-07-22 06:53:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
1999-01-14 21:02:45 +03:00
|
|
|
}
|
|
|
|
|
2020-03-09 17:57:23 +03:00
|
|
|
NS_IMETHODIMP DeleteNodeTransaction::RedoTransaction() {
|
2017-03-10 07:23:40 +03:00
|
|
|
if (NS_WARN_IF(!CanDoIt())) {
|
|
|
|
// This is a legal state, the transaction is a no-op.
|
2012-06-19 17:23:36 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-01-14 21:02:45 +03:00
|
|
|
|
Bug 1627175 - part 2: Move `EditorBase::IsModifiableNode()`, `EditorBase::IsEditable()`, `EditorBase::IsTextElementOrText()` and `EditorBase::IsPaddingBRElementForEmptyEditor()` to `EditorUtils` r=m_kato
Due to the include hell, `EditorBase.h` cannot include `EditorUtils.h`.
Therefore we need these 3 methods once. Additionally, `IsModifiableNode()`
is really odd method and looks like that it's used for the following 2 purposes:
1. Simply can be editable.
2. Can be removed from parent.
For the former case, we should sort out it with
`EditorUtils::IsEditableContent()`, but for now, this patch moves it to
`HTMLEditUtils::IsSimplyEditable()`. On the other hand, for the latter case,
we obviously has a bug. Therefore, this patch creates
`HTMLEditUtils::IsRemovableFromParentNode()` and make it check whether the
removing node is also editable.
Unfortunately, `EditorUtils::IsEditableContent()` needs to take editor type.
But it's most callers are in `HTMLEditor` and most of remains are in
common methods of `EditorBase`. I guess we could remove this ugly argument
in the future.
Depends on D70874
Differential Revision: https://phabricator.services.mozilla.com/D70875
--HG--
extra : moz-landing-system : lando
2020-04-15 18:27:38 +03:00
|
|
|
if (mEditorBase->IsTextEditor() && mContentToDelete->IsText()) {
|
2020-04-03 11:30:37 +03:00
|
|
|
uint32_t length = mContentToDelete->AsText()->TextLength();
|
2019-07-22 06:53:36 +03:00
|
|
|
if (length > 0) {
|
|
|
|
mEditorBase->AsTextEditor()->WillDeleteText(length, 0, length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-03 11:30:37 +03:00
|
|
|
mEditorBase->RangeUpdaterRef().SelAdjDeleteNode(*mContentToDelete);
|
2002-11-10 18:11:08 +03:00
|
|
|
|
2020-04-03 11:30:37 +03:00
|
|
|
OwningNonNull<nsINode> parentNode = *mParentNode;
|
|
|
|
OwningNonNull<nsIContent> contentToDelete = *mContentToDelete;
|
2012-10-09 16:31:24 +04:00
|
|
|
ErrorResult error;
|
2020-04-03 11:30:37 +03:00
|
|
|
parentNode->RemoveChild(contentToDelete, error);
|
2020-03-09 17:57:23 +03:00
|
|
|
NS_WARNING_ASSERTION(!error.Failed(), "nsINode::RemoveChild() failed");
|
2015-04-27 16:18:51 +03:00
|
|
|
return error.StealNSResult();
|
1999-01-14 21:02:45 +03:00
|
|
|
}
|
|
|
|
|
2016-07-07 09:08:33 +03:00
|
|
|
} // namespace mozilla
|