gecko-dev/editor/libeditor/CreateElementTransaction.cpp

145 lines
3.9 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2012-05-21 11:12:37 +00:00
/* 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/. */
1999-01-07 22:36:23 +00:00
#include "CreateElementTransaction.h"
#include <algorithm>
#include <stdio.h>
#include "mozilla/dom/Element.h"
#include "mozilla/dom/Selection.h"
#include "mozilla/Casting.h"
#include "nsAlgorithm.h"
#include "nsAString.h"
#include "nsDebug.h"
#include "nsEditor.h"
#include "nsError.h"
#include "nsIContent.h"
#include "nsIDOMCharacterData.h"
#include "nsIEditor.h"
#include "nsINode.h"
#include "nsISupportsUtils.h"
#include "nsMemory.h"
#include "nsReadableUtils.h"
#include "nsStringFwd.h"
#include "nsString.h"
namespace mozilla {
using namespace dom;
CreateElementTransaction::CreateElementTransaction(nsEditor& aEditor,
nsIAtom& aTag,
nsINode& aParent,
int32_t aOffsetInParent)
: EditTxn()
, mEditor(&aEditor)
, mTag(&aTag)
, mParent(&aParent)
, mOffsetInParent(aOffsetInParent)
1999-01-07 22:36:23 +00:00
{
}
CreateElementTransaction::~CreateElementTransaction()
{
}
NS_IMPL_CYCLE_COLLECTION_INHERITED(CreateElementTransaction, EditTxn,
mParent,
mNewNode,
mRefNode)
NS_IMPL_ADDREF_INHERITED(CreateElementTransaction, EditTxn)
NS_IMPL_RELEASE_INHERITED(CreateElementTransaction, EditTxn)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CreateElementTransaction)
NS_INTERFACE_MAP_END_INHERITING(EditTxn)
NS_IMETHODIMP
CreateElementTransaction::DoTransaction()
1999-01-07 22:36:23 +00:00
{
MOZ_ASSERT(mEditor && mTag && mParent);
mNewNode = mEditor->CreateHTMLContent(mTag);
NS_ENSURE_STATE(mNewNode);
// Try to insert formatting whitespace for the new node:
mEditor->MarkNodeDirty(GetAsDOMNode(mNewNode));
// Insert the new node
ErrorResult rv;
if (mOffsetInParent == -1) {
mParent->AppendChild(*mNewNode, rv);
return rv.StealNSResult();
}
mOffsetInParent = std::min(mOffsetInParent,
static_cast<int32_t>(mParent->GetChildCount()));
// Note, it's ok for mRefNode to be null. That means append
mRefNode = mParent->GetChildAt(mOffsetInParent);
mParent->InsertBefore(*mNewNode, mRefNode, rv);
NS_ENSURE_TRUE(!rv.Failed(), rv.StealNSResult());
// Only set selection to insertion point if editor gives permission
if (!mEditor->GetShouldTxnSetSelection()) {
// Do nothing - DOM range gravity will adjust selection
return NS_OK;
1999-01-07 22:36:23 +00:00
}
Bug 1207245 - part 6 - rename nsRefPtr<T> to RefPtr<T>; r=ehsan; a=Tomcat The bulk of this commit was generated with a script, executed at the top level of a typical source code checkout. The only non-machine-generated part was modifying MFBT's moz.build to reflect the new naming. CLOSED TREE makes big refactorings like this a piece of cake. # The main substitution. find . -name '*.cpp' -o -name '*.cc' -o -name '*.h' -o -name '*.mm' -o -name '*.idl'| \ xargs perl -p -i -e ' s/nsRefPtr\.h/RefPtr\.h/g; # handle includes s/nsRefPtr ?</RefPtr</g; # handle declarations and variables ' # Handle a special friend declaration in gfx/layers/AtomicRefCountedWithFinalize.h. perl -p -i -e 's/::nsRefPtr;/::RefPtr;/' gfx/layers/AtomicRefCountedWithFinalize.h # Handle nsRefPtr.h itself, a couple places that define constructors # from nsRefPtr, and code generators specially. We do this here, rather # than indiscriminantly s/nsRefPtr/RefPtr/, because that would rename # things like nsRefPtrHashtable. perl -p -i -e 's/nsRefPtr/RefPtr/g' \ mfbt/nsRefPtr.h \ xpcom/glue/nsCOMPtr.h \ xpcom/base/OwningNonNull.h \ ipc/ipdl/ipdl/lower.py \ ipc/ipdl/ipdl/builtin.py \ dom/bindings/Codegen.py \ python/lldbutils/lldbutils/utils.py # In our indiscriminate substitution above, we renamed # nsRefPtrGetterAddRefs, the class behind getter_AddRefs. Fix that up. find . -name '*.cpp' -o -name '*.h' -o -name '*.idl' | \ xargs perl -p -i -e 's/nsRefPtrGetterAddRefs/RefPtrGetterAddRefs/g' if [ -d .git ]; then git mv mfbt/nsRefPtr.h mfbt/RefPtr.h else hg mv mfbt/nsRefPtr.h mfbt/RefPtr.h fi --HG-- rename : mfbt/nsRefPtr.h => mfbt/RefPtr.h
2015-10-18 05:24:48 +00:00
RefPtr<Selection> selection = mEditor->GetSelection();
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
rv = selection->CollapseNative(mParent, mParent->IndexOf(mNewNode) + 1);
NS_ASSERTION(!rv.Failed(),
"selection could not be collapsed after insert");
return NS_OK;
1999-01-07 22:36:23 +00:00
}
NS_IMETHODIMP
CreateElementTransaction::UndoTransaction()
1999-01-07 22:36:23 +00:00
{
MOZ_ASSERT(mEditor && mParent);
ErrorResult rv;
mParent->RemoveChild(*mNewNode, rv);
return rv.StealNSResult();
1999-01-07 22:36:23 +00:00
}
NS_IMETHODIMP
CreateElementTransaction::RedoTransaction()
1999-01-07 22:36:23 +00:00
{
MOZ_ASSERT(mEditor && mParent);
// First, reset mNewNode so it has no attributes or content
// XXX We never actually did this, we only cleared mNewNode's contents if it
// was a CharacterData node (which it's not, it's an Element)
// Now, reinsert mNewNode
ErrorResult rv;
mParent->InsertBefore(*mNewNode, mRefNode, rv);
return rv.StealNSResult();
1999-01-07 22:36:23 +00:00
}
NS_IMETHODIMP
CreateElementTransaction::GetTxnDescription(nsAString& aString)
1999-01-07 22:36:23 +00:00
{
aString.AssignLiteral("CreateElementTransaction: ");
aString += nsDependentAtomString(mTag);
1999-01-07 22:36:23 +00:00
return NS_OK;
}
already_AddRefed<Element>
CreateElementTransaction::GetNewNode()
{
return nsCOMPtr<Element>(mNewNode).forget();
}
} // namespace mozilla