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"
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
,
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
)
35 DeleteNodeTxn::Init(nsEditor
* aEditor
, nsINode
* aNode
,
36 nsRangeUpdater
* aRangeUpdater
)
38 NS_ENSURE_TRUE(aEditor
&& aNode
, NS_ERROR_NULL_POINTER
);
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
),
47 mRangeUpdater
= aRangeUpdater
;
53 DeleteNodeTxn::DoTransaction()
55 NS_ENSURE_TRUE(mNode
, NS_ERROR_NOT_INITIALIZED
);
58 // this is a no-op, there's no parent to delete mNode from
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
70 mRangeUpdater
->SelAdjDeleteNode(mNode
->AsDOMNode());
74 mParent
->RemoveChild(*mNode
, error
);
75 return error
.StealNSResult();
79 DeleteNodeTxn::UndoTransaction()
82 // this is a legal state, the txn is a no-op
86 return NS_ERROR_NULL_POINTER
;
90 mParent
->InsertBefore(*mNode
, mRefNode
, error
);
91 return error
.StealNSResult();
95 DeleteNodeTxn::RedoTransaction()
98 // this is a legal state, the txn is a no-op
102 return NS_ERROR_NULL_POINTER
;
106 mRangeUpdater
->SelAdjDeleteNode(mNode
->AsDOMNode());
110 mParent
->RemoveChild(*mNode
, error
);
111 return error
.StealNSResult();
115 DeleteNodeTxn::GetTxnDescription(nsAString
& aString
)
117 aString
.AssignLiteral("DeleteNodeTxn");