2014-11-02 15:04:12 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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/. */
|
|
|
|
|
2016-07-07 10:16:12 +03:00
|
|
|
#include "JoinNodeTransaction.h"
|
2016-07-08 07:10:13 +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
|
|
|
#include "HTMLEditUtils.h"
|
2020-04-30 18:26:09 +03:00
|
|
|
#include "mozilla/HTMLEditor.h" // for HTMLEditor
|
2018-03-19 22:45:55 +03:00
|
|
|
#include "mozilla/dom/Text.h"
|
2014-11-02 15:04:12 +03:00
|
|
|
#include "nsAString.h"
|
2016-07-08 08:15:21 +03:00
|
|
|
#include "nsDebug.h" // for NS_ASSERTION, etc.
|
|
|
|
#include "nsError.h" // for NS_ERROR_NULL_POINTER, etc.
|
2014-11-02 15:04:12 +03:00
|
|
|
#include "nsIContent.h" // for nsIContent
|
2016-07-08 08:15:21 +03:00
|
|
|
#include "nsISupportsImpl.h" // for QueryInterface, etc.
|
2014-11-02 15:04:12 +03:00
|
|
|
|
2016-07-07 10:16:12 +03:00
|
|
|
namespace mozilla {
|
2014-11-02 15:04:12 +03:00
|
|
|
|
2016-07-07 10:16:12 +03:00
|
|
|
using namespace dom;
|
|
|
|
|
2017-12-15 15:53:08 +03:00
|
|
|
// static
|
|
|
|
already_AddRefed<JoinNodeTransaction> JoinNodeTransaction::MaybeCreate(
|
2020-04-30 18:26:09 +03:00
|
|
|
HTMLEditor& aHTMLEditor, nsIContent& aLeftContent,
|
2020-04-03 11:30:37 +03:00
|
|
|
nsIContent& aRightContent) {
|
2017-12-15 15:53:08 +03:00
|
|
|
RefPtr<JoinNodeTransaction> transaction =
|
2020-04-30 18:26:09 +03:00
|
|
|
new JoinNodeTransaction(aHTMLEditor, aLeftContent, aRightContent);
|
2017-12-15 15:53:08 +03:00
|
|
|
if (NS_WARN_IF(!transaction->CanDoIt())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return transaction.forget();
|
|
|
|
}
|
|
|
|
|
2020-04-30 18:26:09 +03:00
|
|
|
JoinNodeTransaction::JoinNodeTransaction(HTMLEditor& aHTMLEditor,
|
2020-04-03 11:30:37 +03:00
|
|
|
nsIContent& aLeftContent,
|
|
|
|
nsIContent& aRightContent)
|
2020-04-30 18:26:09 +03:00
|
|
|
: mHTMLEditor(&aHTMLEditor),
|
2020-04-03 11:30:37 +03:00
|
|
|
mLeftContent(&aLeftContent),
|
|
|
|
mRightContent(&aRightContent),
|
2014-11-02 15:04:12 +03:00
|
|
|
mOffset(0) {}
|
|
|
|
|
2016-07-08 03:48:34 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(JoinNodeTransaction, EditTransactionBase,
|
2020-04-30 18:26:09 +03:00
|
|
|
mHTMLEditor, mLeftContent, mRightContent,
|
2020-04-03 11:30:37 +03:00
|
|
|
mParentNode)
|
2014-11-02 15:04:12 +03:00
|
|
|
|
2016-07-07 10:16:12 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(JoinNodeTransaction)
|
2016-07-08 03:48:34 +03:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(EditTransactionBase)
|
2014-11-02 15:04:12 +03:00
|
|
|
|
2017-03-10 07:46:27 +03:00
|
|
|
bool JoinNodeTransaction::CanDoIt() const {
|
2020-04-03 11:30:37 +03:00
|
|
|
if (NS_WARN_IF(!mLeftContent) || NS_WARN_IF(!mRightContent) ||
|
2020-04-30 18:26:09 +03:00
|
|
|
NS_WARN_IF(!mHTMLEditor) || !mLeftContent->GetParentNode()) {
|
2017-03-10 07:46:27 +03:00
|
|
|
return false;
|
2014-11-02 15:04:12 +03:00
|
|
|
}
|
2020-04-30 18:26:09 +03:00
|
|
|
return HTMLEditUtils::IsRemovableFromParentNode(*mLeftContent);
|
2014-11-02 15:04:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// After DoTransaction() and RedoTransaction(), the left node is removed from
|
|
|
|
// the content tree and right node remains.
|
2020-04-03 11:32:01 +03:00
|
|
|
NS_IMETHODIMP JoinNodeTransaction::DoTransaction() {
|
2020-04-30 18:26:09 +03:00
|
|
|
if (NS_WARN_IF(!mHTMLEditor) || NS_WARN_IF(!mLeftContent) ||
|
2020-04-03 11:30:37 +03:00
|
|
|
NS_WARN_IF(!mRightContent)) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2017-03-21 13:00:36 +03:00
|
|
|
}
|
|
|
|
|
2014-11-02 15:04:12 +03:00
|
|
|
// Get the parent node
|
2020-04-16 04:21:17 +03:00
|
|
|
nsINode* leftContentParent = mLeftContent->GetParentNode();
|
2020-04-03 11:30:37 +03:00
|
|
|
if (NS_WARN_IF(!leftContentParent)) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2020-03-10 17:20:22 +03:00
|
|
|
}
|
2014-11-02 15:04:12 +03:00
|
|
|
|
2020-04-03 11:30:37 +03:00
|
|
|
// Verify that mLeftContent and mRightContent have the same parent
|
|
|
|
if (leftContentParent != mRightContent->GetParentNode()) {
|
2014-11-02 15:04:12 +03:00
|
|
|
NS_ASSERTION(false, "Nodes do not have same parent");
|
2020-04-03 11:30:37 +03:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2014-11-02 15:04:12 +03:00
|
|
|
}
|
|
|
|
|
2020-04-03 11:30:37 +03:00
|
|
|
// Set this instance's mParentNode. Other methods will see a non-null
|
|
|
|
// mParentNode and know all is well
|
|
|
|
mParentNode = leftContentParent;
|
|
|
|
mOffset = mLeftContent->Length();
|
2014-11-02 15:04:12 +03:00
|
|
|
|
2020-04-30 18:26:09 +03:00
|
|
|
OwningNonNull<HTMLEditor> htmlEditor = *mHTMLEditor;
|
2020-04-16 04:21:17 +03:00
|
|
|
OwningNonNull<nsIContent> leftContent = *mLeftContent;
|
|
|
|
OwningNonNull<nsIContent> rightContent = *mRightContent;
|
2020-04-30 18:26:09 +03:00
|
|
|
nsresult rv = htmlEditor->DoJoinNodes(rightContent, leftContent);
|
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "HTMLEditor::DoJoinNodes() failed");
|
2020-03-10 17:20:22 +03:00
|
|
|
return rv;
|
2014-11-02 15:04:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: What if instead of split, we just deleted the unneeded children of
|
|
|
|
// mRight and re-inserted mLeft?
|
2020-04-03 11:32:01 +03:00
|
|
|
NS_IMETHODIMP JoinNodeTransaction::UndoTransaction() {
|
2020-04-03 11:30:37 +03:00
|
|
|
if (NS_WARN_IF(!mParentNode) || NS_WARN_IF(!mLeftContent) ||
|
2020-04-30 18:26:09 +03:00
|
|
|
NS_WARN_IF(!mRightContent) || NS_WARN_IF(!mHTMLEditor)) {
|
2020-04-03 11:30:37 +03:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2017-03-21 13:00:36 +03:00
|
|
|
}
|
2014-11-02 15:04:12 +03:00
|
|
|
|
2020-04-03 11:30:37 +03:00
|
|
|
OwningNonNull<nsIContent> leftContent = *mLeftContent;
|
|
|
|
OwningNonNull<nsIContent> rightContent = *mRightContent;
|
|
|
|
OwningNonNull<nsINode> parentNode = *mParentNode;
|
|
|
|
|
2014-11-02 15:04:12 +03:00
|
|
|
// First, massage the existing node so it is in its post-split state
|
2020-03-10 17:20:22 +03:00
|
|
|
ErrorResult error;
|
2020-04-03 11:30:37 +03:00
|
|
|
if (Text* rightTextNode = rightContent->GetAsText()) {
|
2020-04-30 18:26:09 +03:00
|
|
|
OwningNonNull<HTMLEditor> htmlEditor = *mHTMLEditor;
|
|
|
|
htmlEditor->DoDeleteText(MOZ_KnownLive(*rightTextNode), 0, mOffset, error);
|
2020-03-10 17:20:22 +03:00
|
|
|
if (error.Failed()) {
|
|
|
|
NS_WARNING("EditorBase::DoDeleteText() failed");
|
|
|
|
return error.StealNSResult();
|
2018-03-19 22:45:31 +03:00
|
|
|
}
|
2014-11-02 15:04:12 +03:00
|
|
|
} else {
|
2020-04-03 11:30:37 +03:00
|
|
|
AutoTArray<OwningNonNull<nsIContent>, 24> movingChildren;
|
|
|
|
if (nsIContent* child = mRightContent->GetFirstChild()) {
|
|
|
|
movingChildren.AppendElement(*child);
|
|
|
|
for (uint32_t i = 0; i < mOffset; i++) {
|
|
|
|
child = child->GetNextSibling();
|
|
|
|
if (!child) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
movingChildren.AppendElement(*child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (OwningNonNull<nsIContent>& child : movingChildren) {
|
|
|
|
leftContent->AppendChild(child, error);
|
2020-03-10 17:20:22 +03:00
|
|
|
if (error.Failed()) {
|
2020-04-03 11:30:37 +03:00
|
|
|
NS_WARNING("nsINode::AppendChild() failed");
|
2020-03-10 17:20:22 +03:00
|
|
|
return error.StealNSResult();
|
2014-11-02 15:04:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-10 17:20:22 +03:00
|
|
|
|
|
|
|
NS_WARNING_ASSERTION(!error.Failed(), "The previous error was ignored");
|
|
|
|
|
2014-11-02 15:04:12 +03:00
|
|
|
// Second, re-insert the left node into the tree
|
2020-04-03 11:30:37 +03:00
|
|
|
parentNode->InsertBefore(leftContent, rightContent, error);
|
2020-03-10 17:20:22 +03:00
|
|
|
NS_WARNING_ASSERTION(!error.Failed(), "nsINode::InsertBefore() failed");
|
|
|
|
return error.StealNSResult();
|
2014-11-02 15:04:12 +03:00
|
|
|
}
|
|
|
|
|
2016-07-07 10:16:12 +03:00
|
|
|
} // namespace mozilla
|