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"
|
2016-07-08 04:10:13 +00:00
|
|
|
#include "mozilla/EditorBase.h"
|
2014-08-20 12:25:16 +00:00
|
|
|
|
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"
|
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsIContent.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;
|
|
|
|
|
2016-07-08 04:10:13 +00:00
|
|
|
CreateElementTransaction::CreateElementTransaction(EditorBase& aEditorBase,
|
2016-07-07 05:33:56 +00:00
|
|
|
nsIAtom& aTag,
|
|
|
|
nsINode& aParent,
|
|
|
|
int32_t aOffsetInParent)
|
2016-07-08 00:48:34 +00:00
|
|
|
: EditTransactionBase()
|
2016-07-08 04:10:13 +00:00
|
|
|
, mEditorBase(&aEditorBase)
|
2014-08-20 12:25:16 +00:00
|
|
|
, 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-08 00:48:34 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(CreateElementTransaction,
|
|
|
|
EditTransactionBase,
|
2017-03-21 10:00:36 +00:00
|
|
|
mEditorBase,
|
2014-04-25 16:49:00 +00:00
|
|
|
mParent,
|
|
|
|
mNewNode,
|
|
|
|
mRefNode)
|
2009-05-09 04:59:25 +00:00
|
|
|
|
2016-07-08 00:48:34 +00:00
|
|
|
NS_IMPL_ADDREF_INHERITED(CreateElementTransaction, EditTransactionBase)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(CreateElementTransaction, EditTransactionBase)
|
2016-07-07 05:33:56 +00:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CreateElementTransaction)
|
2016-07-08 00:48:34 +00:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(EditTransactionBase)
|
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
|
|
|
{
|
2017-03-21 10:00:36 +00:00
|
|
|
if (NS_WARN_IF(!mEditorBase) || NS_WARN_IF(!mTag) || NS_WARN_IF(!mParent)) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
1999-08-30 22:12:11 +00:00
|
|
|
|
2016-07-08 04:10:13 +00:00
|
|
|
mNewNode = mEditorBase->CreateHTMLContent(mTag);
|
2014-08-20 12:25:16 +00:00
|
|
|
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:
|
2016-07-08 04:10:13 +00:00
|
|
|
mEditorBase->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
|
|
|
|
2016-10-14 12:33:42 +00:00
|
|
|
nsCOMPtr<nsIContent> refNode = mRefNode;
|
|
|
|
mParent->InsertBefore(*mNewNode, refNode, 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
|
2016-07-08 04:10:13 +00:00
|
|
|
if (!mEditorBase->GetShouldTxnSetSelection()) {
|
2014-08-20 12:25:16 +00:00
|
|
|
// 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
|
|
|
|
2016-07-08 04:10:13 +00:00
|
|
|
RefPtr<Selection> selection = mEditorBase->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
|
|
|
{
|
2017-03-21 10:00:36 +00:00
|
|
|
if (NS_WARN_IF(!mEditorBase) || NS_WARN_IF(!mParent)) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
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
|
|
|
{
|
2017-03-21 10:00:36 +00:00
|
|
|
if (NS_WARN_IF(!mEditorBase) || NS_WARN_IF(!mParent)) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
2014-08-20 12:25:16 +00:00
|
|
|
|
|
|
|
// 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;
|
2016-10-14 12:33:42 +00:00
|
|
|
nsCOMPtr<nsIContent> refNode = mRefNode;
|
|
|
|
mParent->InsertBefore(*mNewNode, refNode, 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
|