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