editor/libeditor/EditAggregateTransaction.cpp
author Masayuki Nakano <masayuki@d-toybox.com>
Fri, 21 Oct 2016 17:08:13 +0900
changeset 428357 861a307d2c7b8e9c42b8222e46058a2644d8a18d
parent 386003 942dac3830549a98809abca0d62aeb7599c51631
child 428358 959ac5a254edd0e89beef25b2996bf54cb633aba
permissions -rw-r--r--
Bug 1311240 Fix odd "{" and "}" of control statements in editor for conforming to our coding rules r?smaug Currently, editor code uses following style (or similar style) in a lot of places: if (foo) { } else { } This patch fixes this as conforming to our coding rules, i.e., it becomes: if (foo) { } else { } Additionally, this fixes other odd control statements in the files which include above issue because it's difficult to find following issues with searching the files: * if (foo) bar; * if (foo) { bar; } * if (foo) bar; Finally, if it becomes much simpler than current code, this patch rewrites existing code with "early return style". But this case is only a few places because this is risky. MozReview-Commit-ID: 2Gs26goWXrF

/* -*- 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 "EditAggregateTransaction.h"
#include "nsAString.h"
#include "nsCOMPtr.h"                   // for nsCOMPtr
#include "nsError.h"                    // for NS_OK, etc.
#include "nsISupportsUtils.h"           // for NS_ADDREF
#include "nsITransaction.h"             // for nsITransaction
#include "nsString.h"                   // for nsAutoString

namespace mozilla {

EditAggregateTransaction::EditAggregateTransaction()
{
}

EditAggregateTransaction::~EditAggregateTransaction()
{
}

NS_IMPL_CYCLE_COLLECTION_INHERITED(EditAggregateTransaction,
                                   EditTransactionBase,
                                   mChildren)

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

NS_IMETHODIMP
EditAggregateTransaction::DoTransaction()
{
  nsresult result=NS_OK;  // it's legal (but not very useful) to have an empty child list
  for (uint32_t i = 0, length = mChildren.Length(); i < length; ++i) {
    nsITransaction *txn = mChildren[i];
    if (!txn) {
      return NS_ERROR_NULL_POINTER;
    }
    result = txn->DoTransaction();
    if (NS_FAILED(result)) {
      break;
    }
  }
  return result;
}

NS_IMETHODIMP
EditAggregateTransaction::UndoTransaction()
{
  nsresult result=NS_OK;  // it's legal (but not very useful) to have an empty child list
  // undo goes through children backwards
  for (uint32_t i = mChildren.Length(); i--; ) {
    nsITransaction *txn = mChildren[i];
    if (!txn) {
      return NS_ERROR_NULL_POINTER;
    }
    result = txn->UndoTransaction();
    if (NS_FAILED(result)) {
      break;
    }
  }
  return result;
}

NS_IMETHODIMP
EditAggregateTransaction::RedoTransaction()
{
  nsresult result=NS_OK;  // it's legal (but not very useful) to have an empty child list
  for (uint32_t i = 0, length = mChildren.Length(); i < length; ++i) {
    nsITransaction *txn = mChildren[i];
    if (!txn) {
      return NS_ERROR_NULL_POINTER;
    }
    result = txn->RedoTransaction();
    if (NS_FAILED(result)) {
      break;
    }
  }
  return result;
}

NS_IMETHODIMP
EditAggregateTransaction::Merge(nsITransaction* aTransaction,
                                bool* aDidMerge)
{
  if (aDidMerge) {
    *aDidMerge = false;
  }
  if (mChildren.IsEmpty()) {
    return NS_OK;
  }
  // FIXME: Is this really intended not to loop?  It looks like the code
  // that used to be here sort of intended to loop, but didn't.
  nsITransaction *txn = mChildren[0];
  if (!txn) {
    return NS_ERROR_NULL_POINTER;
  }
  return txn->Merge(aTransaction, aDidMerge);
}

NS_IMETHODIMP
EditAggregateTransaction::GetTxnDescription(nsAString& aString)
{
  aString.AssignLiteral("EditAggregateTransaction: ");

  if (mName) {
    nsAutoString name;
    mName->ToString(name);
    aString += name;
  }

  return NS_OK;
}

NS_IMETHODIMP
EditAggregateTransaction::AppendChild(EditTransactionBase* aTransaction)
{
  if (!aTransaction) {
    return NS_ERROR_NULL_POINTER;
  }

  RefPtr<EditTransactionBase>* slot = mChildren.AppendElement();
  if (!slot) {
    return NS_ERROR_OUT_OF_MEMORY;
  }

  *slot = aTransaction;
  return NS_OK;
}

NS_IMETHODIMP
EditAggregateTransaction::GetName(nsIAtom** aName)
{
  if (aName && mName) {
    *aName = mName;
    NS_ADDREF(*aName);
    return NS_OK;
  }
  return NS_ERROR_NULL_POINTER;
}

} // namespace mozilla