2001-09-25 22:53:13 +00:00
|
|
|
/* -*- 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
|
|
|
|
2016-07-07 05:33:56 +00:00
|
|
|
#include "CreateElementTransaction.h"
|
2014-12-02 05:07:42 +00:00
|
|
|
|
2014-08-20 12:25:16 +00:00
|
|
|
#include <algorithm>
|
2012-07-13 06:33:42 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "mozilla/dom/Element.h"
|
2014-08-20 12:25:16 +00:00
|
|
|
#include "mozilla/dom/Selection.h"
|
|
|
|
|
|
|
|
#include "mozilla/Casting.h"
|
|
|
|
|
2012-07-13 06:33:42 +00:00
|
|
|
#include "nsAlgorithm.h"
|
2014-08-20 12:25:16 +00:00
|
|
|
#include "nsAString.h"
|
2012-07-13 06:33:42 +00:00
|
|
|
#include "nsDebug.h"
|
1999-05-05 04:05:19 +00:00
|
|
|
#include "nsEditor.h"
|
2012-07-13 06:33:42 +00:00
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIDOMCharacterData.h"
|
2014-06-05 03:51:34 +00:00
|
|
|
#include "nsIEditor.h"
|
2012-07-13 06:33:42 +00:00
|
|
|
#include "nsINode.h"
|
|
|
|
#include "nsISupportsUtils.h"
|
|
|
|
#include "nsMemory.h"
|
2001-09-29 08:28:41 +00:00
|
|
|
#include "nsReadableUtils.h"
|
2012-07-13 06:33:42 +00:00
|
|
|
#include "nsStringFwd.h"
|
|
|
|
#include "nsString.h"
|
2000-07-13 23:15:41 +00:00
|
|
|
|
2016-07-07 05:33:56 +00:00
|
|
|
namespace mozilla {
|
2012-06-06 07:35:47 +00:00
|
|
|
|
2016-07-07 05:33:56 +00:00
|
|
|
using namespace dom;
|
|
|
|
|
|
|
|
CreateElementTransaction::CreateElementTransaction(nsEditor& aEditor,
|
|
|
|
nsIAtom& aTag,
|
|
|
|
nsINode& aParent,
|
|
|
|
int32_t aOffsetInParent)
|
1999-01-21 01:51:09 +00:00
|
|
|
: EditTxn()
|
2014-08-20 12:25:16 +00:00
|
|
|
, mEditor(&aEditor)
|
|
|
|
, mTag(&aTag)
|
|
|
|
, mParent(&aParent)
|
|
|
|
, mOffsetInParent(aOffsetInParent)
|
1999-01-07 22:36:23 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-07-07 05:33:56 +00:00
|
|
|
CreateElementTransaction::~CreateElementTransaction()
|
2014-07-08 21:23:18 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-07-07 05:33:56 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(CreateElementTransaction, EditTxn,
|
2014-04-25 16:49:00 +00:00
|
|
|
mParent,
|
|
|
|
mNewNode,
|
|
|
|
mRefNode)
|
2009-05-09 04:59:25 +00:00
|
|
|
|
2016-07-07 05:33:56 +00:00
|
|
|
NS_IMPL_ADDREF_INHERITED(CreateElementTransaction, EditTxn)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(CreateElementTransaction, EditTxn)
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CreateElementTransaction)
|
2009-05-09 04:59:25 +00:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(EditTxn)
|
1999-01-21 01:51:09 +00:00
|
|
|
|
|
|
|
|
2014-08-20 12:25:16 +00:00
|
|
|
NS_IMETHODIMP
|
2016-07-07 05:33:56 +00:00
|
|
|
CreateElementTransaction::DoTransaction()
|
1999-01-07 22:36:23 +00:00
|
|
|
{
|
2014-08-20 12:25:16 +00:00
|
|
|
MOZ_ASSERT(mEditor && mTag && mParent);
|
1999-08-30 22:12:11 +00:00
|
|
|
|
2014-08-20 12:25:16 +00:00
|
|
|
mNewNode = mEditor->CreateHTMLContent(mTag);
|
|
|
|
NS_ENSURE_STATE(mNewNode);
|
2012-06-06 07:41:51 +00:00
|
|
|
|
2007-06-30 03:09:53 +00:00
|
|
|
// Try to insert formatting whitespace for the new node:
|
2014-08-20 12:25:16 +00:00
|
|
|
mEditor->MarkNodeDirty(GetAsDOMNode(mNewNode));
|
1999-08-19 13:30:48 +00:00
|
|
|
|
2014-08-20 12:25:16 +00:00
|
|
|
// Insert the new node
|
|
|
|
ErrorResult rv;
|
|
|
|
if (mOffsetInParent == -1) {
|
|
|
|
mParent->AppendChild(*mNewNode, rv);
|
2015-04-27 13:18:51 +00:00
|
|
|
return rv.StealNSResult();
|
1999-08-19 13:30:48 +00:00
|
|
|
}
|
2012-06-06 07:41:51 +00:00
|
|
|
|
2014-08-20 12:25:16 +00:00
|
|
|
mOffsetInParent = std::min(mOffsetInParent,
|
|
|
|
static_cast<int32_t>(mParent->GetChildCount()));
|
2012-06-06 07:41:51 +00:00
|
|
|
|
2014-08-20 12:25:16 +00:00
|
|
|
// Note, it's ok for mRefNode to be null. That means append
|
|
|
|
mRefNode = mParent->GetChildAt(mOffsetInParent);
|
2012-06-06 07:41:51 +00:00
|
|
|
|
2014-08-20 12:25:16 +00:00
|
|
|
mParent->InsertBefore(*mNewNode, mRefNode, rv);
|
2015-04-27 13:18:52 +00:00
|
|
|
NS_ENSURE_TRUE(!rv.Failed(), rv.StealNSResult());
|
2012-06-06 07:41:51 +00:00
|
|
|
|
2014-08-20 12:25:16 +00:00
|
|
|
// Only set selection to insertion point if editor gives permission
|
|
|
|
if (!mEditor->GetShouldTxnSetSelection()) {
|
|
|
|
// Do nothing - DOM range gravity will adjust selection
|
2012-06-06 07:41:51 +00:00
|
|
|
return NS_OK;
|
1999-01-07 22:36:23 +00:00
|
|
|
}
|
2012-06-06 07:41:51 +00:00
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<Selection> selection = mEditor->GetSelection();
|
2012-06-06 07:41:51 +00:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
|
|
|
|
2014-08-20 12:25:16 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-08-20 12:25:16 +00:00
|
|
|
NS_IMETHODIMP
|
2016-07-07 05:33:56 +00:00
|
|
|
CreateElementTransaction::UndoTransaction()
|
1999-01-07 22:36:23 +00:00
|
|
|
{
|
2014-08-20 12:25:16 +00:00
|
|
|
MOZ_ASSERT(mEditor && mParent);
|
1999-08-19 13:30:48 +00:00
|
|
|
|
2014-08-20 12:25:16 +00:00
|
|
|
ErrorResult rv;
|
|
|
|
mParent->RemoveChild(*mNewNode, rv);
|
|
|
|
|
2015-04-27 13:18:51 +00:00
|
|
|
return rv.StealNSResult();
|
1999-01-07 22:36:23 +00:00
|
|
|
}
|
|
|
|
|
2014-08-20 12:25:16 +00:00
|
|
|
NS_IMETHODIMP
|
2016-07-07 05:33:56 +00:00
|
|
|
CreateElementTransaction::RedoTransaction()
|
1999-01-07 22:36:23 +00:00
|
|
|
{
|
2014-08-20 12:25:16 +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);
|
2015-04-27 13:18:51 +00:00
|
|
|
return rv.StealNSResult();
|
1999-01-07 22:36:23 +00:00
|
|
|
}
|
|
|
|
|
2014-08-20 12:25:16 +00:00
|
|
|
NS_IMETHODIMP
|
2016-07-07 05:33:56 +00:00
|
|
|
CreateElementTransaction::GetTxnDescription(nsAString& aString)
|
1999-01-07 22:36:23 +00:00
|
|
|
{
|
2016-07-07 05:33:56 +00:00
|
|
|
aString.AssignLiteral("CreateElementTransaction: ");
|
2014-08-20 12:25:16 +00:00
|
|
|
aString += nsDependentAtomString(mTag);
|
1999-01-07 22:36:23 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-02-24 17:24:37 +00:00
|
|
|
|
2014-08-20 12:25:16 +00:00
|
|
|
already_AddRefed<Element>
|
2016-07-07 05:33:56 +00:00
|
|
|
CreateElementTransaction::GetNewNode()
|
1999-02-24 17:24:37 +00:00
|
|
|
{
|
2014-08-20 12:25:16 +00:00
|
|
|
return nsCOMPtr<Element>(mNewNode).forget();
|
1999-02-24 17:24:37 +00:00
|
|
|
}
|
2016-07-07 05:33:56 +00:00
|
|
|
|
|
|
|
} // namespace mozilla
|