Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "DeleteNodeTxn.h" |
michael@0 | 7 | #include "nsDebug.h" |
michael@0 | 8 | #include "nsEditor.h" |
michael@0 | 9 | #include "nsError.h" |
michael@0 | 10 | #include "nsSelectionState.h" // nsRangeUpdater |
michael@0 | 11 | #include "nsAString.h" |
michael@0 | 12 | |
michael@0 | 13 | using namespace mozilla; |
michael@0 | 14 | |
michael@0 | 15 | DeleteNodeTxn::DeleteNodeTxn() |
michael@0 | 16 | : EditTxn(), mNode(), mParent(), mRefNode(), mRangeUpdater(nullptr) |
michael@0 | 17 | { |
michael@0 | 18 | } |
michael@0 | 19 | |
michael@0 | 20 | NS_IMPL_CYCLE_COLLECTION_INHERITED(DeleteNodeTxn, EditTxn, |
michael@0 | 21 | mNode, |
michael@0 | 22 | mParent, |
michael@0 | 23 | mRefNode) |
michael@0 | 24 | |
michael@0 | 25 | NS_IMPL_ADDREF_INHERITED(DeleteNodeTxn, EditTxn) |
michael@0 | 26 | NS_IMPL_RELEASE_INHERITED(DeleteNodeTxn, EditTxn) |
michael@0 | 27 | NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DeleteNodeTxn) |
michael@0 | 28 | NS_INTERFACE_MAP_END_INHERITING(EditTxn) |
michael@0 | 29 | |
michael@0 | 30 | nsresult |
michael@0 | 31 | DeleteNodeTxn::Init(nsEditor* aEditor, nsINode* aNode, |
michael@0 | 32 | nsRangeUpdater* aRangeUpdater) |
michael@0 | 33 | { |
michael@0 | 34 | NS_ENSURE_TRUE(aEditor && aNode, NS_ERROR_NULL_POINTER); |
michael@0 | 35 | mEditor = aEditor; |
michael@0 | 36 | mNode = aNode; |
michael@0 | 37 | mParent = aNode->GetParentNode(); |
michael@0 | 38 | |
michael@0 | 39 | // do nothing if the node has a parent and it's read-only |
michael@0 | 40 | NS_ENSURE_TRUE(!mParent || mEditor->IsModifiableNode(mParent), |
michael@0 | 41 | NS_ERROR_FAILURE); |
michael@0 | 42 | |
michael@0 | 43 | mRangeUpdater = aRangeUpdater; |
michael@0 | 44 | return NS_OK; |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | |
michael@0 | 48 | NS_IMETHODIMP |
michael@0 | 49 | DeleteNodeTxn::DoTransaction() |
michael@0 | 50 | { |
michael@0 | 51 | NS_ENSURE_TRUE(mNode, NS_ERROR_NOT_INITIALIZED); |
michael@0 | 52 | |
michael@0 | 53 | if (!mParent) { |
michael@0 | 54 | // this is a no-op, there's no parent to delete mNode from |
michael@0 | 55 | return NS_OK; |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | // remember which child mNode was (by remembering which child was next); |
michael@0 | 59 | // mRefNode can be null |
michael@0 | 60 | mRefNode = mNode->GetNextSibling(); |
michael@0 | 61 | |
michael@0 | 62 | // give range updater a chance. SelAdjDeleteNode() needs to be called |
michael@0 | 63 | // *before* we do the action, unlike some of the other nsRangeStore update |
michael@0 | 64 | // methods. |
michael@0 | 65 | if (mRangeUpdater) { |
michael@0 | 66 | mRangeUpdater->SelAdjDeleteNode(mNode->AsDOMNode()); |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | ErrorResult error; |
michael@0 | 70 | mParent->RemoveChild(*mNode, error); |
michael@0 | 71 | return error.ErrorCode(); |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | NS_IMETHODIMP |
michael@0 | 75 | DeleteNodeTxn::UndoTransaction() |
michael@0 | 76 | { |
michael@0 | 77 | if (!mParent) { |
michael@0 | 78 | // this is a legal state, the txn is a no-op |
michael@0 | 79 | return NS_OK; |
michael@0 | 80 | } |
michael@0 | 81 | if (!mNode) { |
michael@0 | 82 | return NS_ERROR_NULL_POINTER; |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | ErrorResult error; |
michael@0 | 86 | mParent->InsertBefore(*mNode, mRefNode, error); |
michael@0 | 87 | return error.ErrorCode(); |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | NS_IMETHODIMP |
michael@0 | 91 | DeleteNodeTxn::RedoTransaction() |
michael@0 | 92 | { |
michael@0 | 93 | if (!mParent) { |
michael@0 | 94 | // this is a legal state, the txn is a no-op |
michael@0 | 95 | return NS_OK; |
michael@0 | 96 | } |
michael@0 | 97 | if (!mNode) { |
michael@0 | 98 | return NS_ERROR_NULL_POINTER; |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | if (mRangeUpdater) { |
michael@0 | 102 | mRangeUpdater->SelAdjDeleteNode(mNode->AsDOMNode()); |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | ErrorResult error; |
michael@0 | 106 | mParent->RemoveChild(*mNode, error); |
michael@0 | 107 | return error.ErrorCode(); |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | NS_IMETHODIMP |
michael@0 | 111 | DeleteNodeTxn::GetTxnDescription(nsAString& aString) |
michael@0 | 112 | { |
michael@0 | 113 | aString.AssignLiteral("DeleteNodeTxn"); |
michael@0 | 114 | return NS_OK; |
michael@0 | 115 | } |