1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/editor/libeditor/base/DeleteNodeTxn.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,115 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "DeleteNodeTxn.h" 1.10 +#include "nsDebug.h" 1.11 +#include "nsEditor.h" 1.12 +#include "nsError.h" 1.13 +#include "nsSelectionState.h" // nsRangeUpdater 1.14 +#include "nsAString.h" 1.15 + 1.16 +using namespace mozilla; 1.17 + 1.18 +DeleteNodeTxn::DeleteNodeTxn() 1.19 + : EditTxn(), mNode(), mParent(), mRefNode(), mRangeUpdater(nullptr) 1.20 +{ 1.21 +} 1.22 + 1.23 +NS_IMPL_CYCLE_COLLECTION_INHERITED(DeleteNodeTxn, EditTxn, 1.24 + mNode, 1.25 + mParent, 1.26 + mRefNode) 1.27 + 1.28 +NS_IMPL_ADDREF_INHERITED(DeleteNodeTxn, EditTxn) 1.29 +NS_IMPL_RELEASE_INHERITED(DeleteNodeTxn, EditTxn) 1.30 +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DeleteNodeTxn) 1.31 +NS_INTERFACE_MAP_END_INHERITING(EditTxn) 1.32 + 1.33 +nsresult 1.34 +DeleteNodeTxn::Init(nsEditor* aEditor, nsINode* aNode, 1.35 + nsRangeUpdater* aRangeUpdater) 1.36 +{ 1.37 + NS_ENSURE_TRUE(aEditor && aNode, NS_ERROR_NULL_POINTER); 1.38 + mEditor = aEditor; 1.39 + mNode = aNode; 1.40 + mParent = aNode->GetParentNode(); 1.41 + 1.42 + // do nothing if the node has a parent and it's read-only 1.43 + NS_ENSURE_TRUE(!mParent || mEditor->IsModifiableNode(mParent), 1.44 + NS_ERROR_FAILURE); 1.45 + 1.46 + mRangeUpdater = aRangeUpdater; 1.47 + return NS_OK; 1.48 +} 1.49 + 1.50 + 1.51 +NS_IMETHODIMP 1.52 +DeleteNodeTxn::DoTransaction() 1.53 +{ 1.54 + NS_ENSURE_TRUE(mNode, NS_ERROR_NOT_INITIALIZED); 1.55 + 1.56 + if (!mParent) { 1.57 + // this is a no-op, there's no parent to delete mNode from 1.58 + return NS_OK; 1.59 + } 1.60 + 1.61 + // remember which child mNode was (by remembering which child was next); 1.62 + // mRefNode can be null 1.63 + mRefNode = mNode->GetNextSibling(); 1.64 + 1.65 + // give range updater a chance. SelAdjDeleteNode() needs to be called 1.66 + // *before* we do the action, unlike some of the other nsRangeStore update 1.67 + // methods. 1.68 + if (mRangeUpdater) { 1.69 + mRangeUpdater->SelAdjDeleteNode(mNode->AsDOMNode()); 1.70 + } 1.71 + 1.72 + ErrorResult error; 1.73 + mParent->RemoveChild(*mNode, error); 1.74 + return error.ErrorCode(); 1.75 +} 1.76 + 1.77 +NS_IMETHODIMP 1.78 +DeleteNodeTxn::UndoTransaction() 1.79 +{ 1.80 + if (!mParent) { 1.81 + // this is a legal state, the txn is a no-op 1.82 + return NS_OK; 1.83 + } 1.84 + if (!mNode) { 1.85 + return NS_ERROR_NULL_POINTER; 1.86 + } 1.87 + 1.88 + ErrorResult error; 1.89 + mParent->InsertBefore(*mNode, mRefNode, error); 1.90 + return error.ErrorCode(); 1.91 +} 1.92 + 1.93 +NS_IMETHODIMP 1.94 +DeleteNodeTxn::RedoTransaction() 1.95 +{ 1.96 + if (!mParent) { 1.97 + // this is a legal state, the txn is a no-op 1.98 + return NS_OK; 1.99 + } 1.100 + if (!mNode) { 1.101 + return NS_ERROR_NULL_POINTER; 1.102 + } 1.103 + 1.104 + if (mRangeUpdater) { 1.105 + mRangeUpdater->SelAdjDeleteNode(mNode->AsDOMNode()); 1.106 + } 1.107 + 1.108 + ErrorResult error; 1.109 + mParent->RemoveChild(*mNode, error); 1.110 + return error.ErrorCode(); 1.111 +} 1.112 + 1.113 +NS_IMETHODIMP 1.114 +DeleteNodeTxn::GetTxnDescription(nsAString& aString) 1.115 +{ 1.116 + aString.AssignLiteral("DeleteNodeTxn"); 1.117 + return NS_OK; 1.118 +}