Bumping manifests a=b2g-bump
[gecko.git] / editor / libeditor / DeleteNodeTxn.cpp
blob2e15e40581a408fefeb0f98bdf34b8829eb7e9f6
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "DeleteNodeTxn.h"
7 #include "nsDebug.h"
8 #include "nsEditor.h"
9 #include "nsError.h"
10 #include "nsSelectionState.h" // nsRangeUpdater
11 #include "nsAString.h"
13 using namespace mozilla;
15 DeleteNodeTxn::DeleteNodeTxn()
16 : EditTxn(), mNode(), mParent(), mRefNode(), mRangeUpdater(nullptr)
20 DeleteNodeTxn::~DeleteNodeTxn()
24 NS_IMPL_CYCLE_COLLECTION_INHERITED(DeleteNodeTxn, EditTxn,
25 mNode,
26 mParent,
27 mRefNode)
29 NS_IMPL_ADDREF_INHERITED(DeleteNodeTxn, EditTxn)
30 NS_IMPL_RELEASE_INHERITED(DeleteNodeTxn, EditTxn)
31 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DeleteNodeTxn)
32 NS_INTERFACE_MAP_END_INHERITING(EditTxn)
34 nsresult
35 DeleteNodeTxn::Init(nsEditor* aEditor, nsINode* aNode,
36 nsRangeUpdater* aRangeUpdater)
38 NS_ENSURE_TRUE(aEditor && aNode, NS_ERROR_NULL_POINTER);
39 mEditor = aEditor;
40 mNode = aNode;
41 mParent = aNode->GetParentNode();
43 // do nothing if the node has a parent and it's read-only
44 NS_ENSURE_TRUE(!mParent || mEditor->IsModifiableNode(mParent),
45 NS_ERROR_FAILURE);
47 mRangeUpdater = aRangeUpdater;
48 return NS_OK;
52 NS_IMETHODIMP
53 DeleteNodeTxn::DoTransaction()
55 NS_ENSURE_TRUE(mNode, NS_ERROR_NOT_INITIALIZED);
57 if (!mParent) {
58 // this is a no-op, there's no parent to delete mNode from
59 return NS_OK;
62 // remember which child mNode was (by remembering which child was next);
63 // mRefNode can be null
64 mRefNode = mNode->GetNextSibling();
66 // give range updater a chance. SelAdjDeleteNode() needs to be called
67 // *before* we do the action, unlike some of the other nsRangeStore update
68 // methods.
69 if (mRangeUpdater) {
70 mRangeUpdater->SelAdjDeleteNode(mNode->AsDOMNode());
73 ErrorResult error;
74 mParent->RemoveChild(*mNode, error);
75 return error.ErrorCode();
78 NS_IMETHODIMP
79 DeleteNodeTxn::UndoTransaction()
81 if (!mParent) {
82 // this is a legal state, the txn is a no-op
83 return NS_OK;
85 if (!mNode) {
86 return NS_ERROR_NULL_POINTER;
89 ErrorResult error;
90 mParent->InsertBefore(*mNode, mRefNode, error);
91 return error.ErrorCode();
94 NS_IMETHODIMP
95 DeleteNodeTxn::RedoTransaction()
97 if (!mParent) {
98 // this is a legal state, the txn is a no-op
99 return NS_OK;
101 if (!mNode) {
102 return NS_ERROR_NULL_POINTER;
105 if (mRangeUpdater) {
106 mRangeUpdater->SelAdjDeleteNode(mNode->AsDOMNode());
109 ErrorResult error;
110 mParent->RemoveChild(*mNode, error);
111 return error.ErrorCode();
114 NS_IMETHODIMP
115 DeleteNodeTxn::GetTxnDescription(nsAString& aString)
117 aString.AssignLiteral("DeleteNodeTxn");
118 return NS_OK;