editor/libeditor/DeleteNodeTransaction.cpp
author Masayuki Nakano <masayuki@d-toybox.com>
Tue, 21 Mar 2017 19:00:36 +0900
changeset 349374 596faf466bbc07412cfa2277ef4d233c748fc233
parent 347044 5f4e819e9d5308ae136dc6fe4ef4490b4effdbab
child 364085 87a6c22ddbd49f8ad8746c1b9ec3b4d73bca24f0
permissions -rw-r--r--
Bug 1349138 Edit transactions should store their editor instance with strong reference r=smaug Edit transactions should store their editor instance with strong reference and they should be released at destroying the editor. MozReview-Commit-ID: D67KU8WFxyo

/* -*- 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/. */

#include "DeleteNodeTransaction.h"
#include "mozilla/EditorBase.h"
#include "mozilla/SelectionState.h" // RangeUpdater
#include "nsDebug.h"
#include "nsError.h"
#include "nsAString.h"

namespace mozilla {

DeleteNodeTransaction::DeleteNodeTransaction(EditorBase& aEditorBase,
                                             nsINode& aNodeToDelete,
                                             RangeUpdater* aRangeUpdater)
  : mEditorBase(&aEditorBase)
  , mNodeToDelete(&aNodeToDelete)
  , mParentNode(aNodeToDelete.GetParentNode())
  , mRangeUpdater(aRangeUpdater)
{
  // XXX We're not sure if this is really necessary.
  if (!CanDoIt()) {
    mRangeUpdater = nullptr;
  }
}

DeleteNodeTransaction::~DeleteNodeTransaction()
{
}

NS_IMPL_CYCLE_COLLECTION_INHERITED(DeleteNodeTransaction, EditTransactionBase,
                                   mEditorBase,
                                   mNodeToDelete,
                                   mParentNode,
                                   mRefNode)

NS_IMPL_ADDREF_INHERITED(DeleteNodeTransaction, EditTransactionBase)
NS_IMPL_RELEASE_INHERITED(DeleteNodeTransaction, EditTransactionBase)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DeleteNodeTransaction)
NS_INTERFACE_MAP_END_INHERITING(EditTransactionBase)

bool
DeleteNodeTransaction::CanDoIt() const
{
  if (NS_WARN_IF(!mNodeToDelete) || NS_WARN_IF(!mEditorBase) ||
      !mParentNode || !mEditorBase->IsModifiableNode(mParentNode)) {
    return false;
  }
  return true;
}

NS_IMETHODIMP
DeleteNodeTransaction::DoTransaction()
{
  if (NS_WARN_IF(!CanDoIt())) {
    return NS_OK;
  }

  // Remember which child mNodeToDelete was (by remembering which child was
  // next).  Note that mRefNode can be nullptr.
  mRefNode = mNodeToDelete->GetNextSibling();

  // give range updater a chance.  SelAdjDeleteNode() needs to be called
  // *before* we do the action, unlike some of the other RangeItem update
  // methods.
  if (mRangeUpdater) {
    mRangeUpdater->SelAdjDeleteNode(mNodeToDelete->AsDOMNode());
  }

  ErrorResult error;
  mParentNode->RemoveChild(*mNodeToDelete, error);
  return error.StealNSResult();
}

NS_IMETHODIMP
DeleteNodeTransaction::UndoTransaction()
{
  if (NS_WARN_IF(!CanDoIt())) {
    // This is a legal state, the transaction is a no-op.
    return NS_OK;
  }
  ErrorResult error;
  nsCOMPtr<nsIContent> refNode = mRefNode;
  mParentNode->InsertBefore(*mNodeToDelete, refNode, error);
  return error.StealNSResult();
}

NS_IMETHODIMP
DeleteNodeTransaction::RedoTransaction()
{
  if (NS_WARN_IF(!CanDoIt())) {
    // This is a legal state, the transaction is a no-op.
    return NS_OK;
  }

  if (mRangeUpdater) {
    mRangeUpdater->SelAdjDeleteNode(mNodeToDelete->AsDOMNode());
  }

  ErrorResult error;
  mParentNode->RemoveChild(*mNodeToDelete, error);
  return error.StealNSResult();
}

NS_IMETHODIMP
DeleteNodeTransaction::GetTxnDescription(nsAString& aString)
{
  aString.AssignLiteral("DeleteNodeTransaction");
  return NS_OK;
}

} // namespace mozilla