Bug 1407854 - Part 2: Add an optional argument to CreateNode() to allow callers to pass the child node when they know it; r=masayuki

This commit is contained in:
Ehsan Akhgari 2017-10-11 20:51:42 -04:00
Родитель e9a78d5b18
Коммит bf1f474a3d
4 изменённых файлов: 23 добавлений и 11 удалений

Просмотреть файл

@ -34,12 +34,14 @@ using namespace dom;
CreateElementTransaction::CreateElementTransaction(EditorBase& aEditorBase,
nsAtom& aTag,
nsINode& aParent,
int32_t aOffsetInParent)
int32_t aOffsetInParent,
nsIContent* aChildAtOffset)
: EditTransactionBase()
, mEditorBase(&aEditorBase)
, mTag(&aTag)
, mParent(&aParent)
, mOffsetInParent(aOffsetInParent)
, mRefNode(aChildAtOffset)
{
}
@ -83,8 +85,10 @@ CreateElementTransaction::DoTransaction()
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);
if (!mRefNode) {
// Note, it's ok for mRefNode to be null. That means append
mRefNode = mParent->GetChildAt(mOffsetInParent);
}
nsCOMPtr<nsIContent> refNode = mRefNode;
mParent->InsertBefore(*mNewNode, refNode, rv);

Просмотреть файл

@ -41,7 +41,8 @@ public:
CreateElementTransaction(EditorBase& aEditorBase,
nsAtom& aTag,
nsINode& aParent,
int32_t aOffsetInParent);
int32_t aOffsetInParent,
nsIContent* aChildAtOffset);
NS_DECL_ISUPPORTS_INHERITED
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(CreateElementTransaction,
@ -71,7 +72,8 @@ protected:
// The new node to insert.
nsCOMPtr<dom::Element> mNewNode;
// The node we will insert mNewNode before. We compute this ourselves.
// The node we will insert mNewNode before. We compute this ourselves if it
// is not set by the constructor.
nsCOMPtr<nsIContent> mRefNode;
};

Просмотреть файл

@ -1418,7 +1418,8 @@ EditorBase::SetSpellcheckUserOverride(bool enable)
already_AddRefed<Element>
EditorBase::CreateNode(nsAtom* aTag,
nsINode* aParent,
int32_t aPosition)
int32_t aPosition,
nsIContent* aChildAtPosition)
{
MOZ_ASSERT(aTag && aParent);
@ -1435,7 +1436,8 @@ EditorBase::CreateNode(nsAtom* aTag,
nsCOMPtr<Element> ret;
RefPtr<CreateElementTransaction> transaction =
CreateTxnForCreateElement(*aTag, *aParent, aPosition);
CreateTxnForCreateElement(*aTag, *aParent, aPosition,
aChildAtPosition);
nsresult rv = DoTransaction(transaction);
if (NS_SUCCEEDED(rv)) {
ret = transaction->GetNewNode();
@ -4371,10 +4373,12 @@ EditorBase::CreateTxnForRemoveAttribute(Element& aElement,
already_AddRefed<CreateElementTransaction>
EditorBase::CreateTxnForCreateElement(nsAtom& aTag,
nsINode& aParent,
int32_t aPosition)
int32_t aPosition,
nsIContent* aChildAtPosition)
{
RefPtr<CreateElementTransaction> transaction =
new CreateElementTransaction(*this, aTag, aParent, aPosition);
new CreateElementTransaction(*this, aTag, aParent, aPosition,
aChildAtPosition);
return transaction.forget();
}

Просмотреть файл

@ -398,10 +398,12 @@ protected:
already_AddRefed<CreateElementTransaction>
CreateTxnForCreateElement(nsAtom& aTag,
nsINode& aParent,
int32_t aPosition);
int32_t aPosition,
nsIContent* aChildAtPosition);
already_AddRefed<Element> CreateNode(nsAtom* aTag, nsINode* aParent,
int32_t aPosition);
int32_t aPosition,
nsIContent* aChildAtPosition = nullptr);
/**
* Create a transaction for inserting aNode as a child of aParent.