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"
|
2017-11-07 13:50:25 +03:00
|
|
|
#include "mozilla/EditorDOMPoint.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;
|
|
|
|
|
2017-12-15 11:54:10 +03:00
|
|
|
already_AddRefed<CreateElementTransaction>
|
|
|
|
CreateElementTransaction::Create(EditorBase& aEditorBase,
|
|
|
|
nsAtom& aTag,
|
|
|
|
const EditorRawDOMPoint& aPointToInsert)
|
|
|
|
{
|
|
|
|
RefPtr<CreateElementTransaction> transaction =
|
|
|
|
new CreateElementTransaction(aEditorBase, aTag, aPointToInsert);
|
|
|
|
return transaction.forget();
|
|
|
|
}
|
|
|
|
|
2017-11-08 10:49:06 +03:00
|
|
|
CreateElementTransaction::CreateElementTransaction(
|
|
|
|
EditorBase& aEditorBase,
|
|
|
|
nsAtom& aTag,
|
|
|
|
const EditorRawDOMPoint& aPointToInsert)
|
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)
|
2017-11-08 10:49:06 +03:00
|
|
|
, mPointToInsert(aPointToInsert)
|
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,
|
2017-11-08 10:49:06 +03:00
|
|
|
mPointToInsert,
|
|
|
|
mNewNode)
|
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-11-08 10:49:06 +03:00
|
|
|
if (NS_WARN_IF(!mEditorBase) || NS_WARN_IF(!mTag) ||
|
|
|
|
NS_WARN_IF(!mPointToInsert.IsSet())) {
|
2017-03-21 13:00:36 +03:00
|
|
|
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
|
2017-11-08 10:49:06 +03:00
|
|
|
ErrorResult error;
|
|
|
|
InsertNewNode(error);
|
|
|
|
if (NS_WARN_IF(error.Failed())) {
|
|
|
|
return error.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
|
|
|
// 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-11-07 13:50:25 +03:00
|
|
|
EditorRawDOMPoint afterNewNode(mNewNode);
|
|
|
|
if (NS_WARN_IF(!afterNewNode.AdvanceOffset())) {
|
|
|
|
// If mutation observer or mutation event listener moved or removed the
|
|
|
|
// new node, we hit this case. Should we use script blocker while we're
|
|
|
|
// in this method?
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2017-11-08 10:49:06 +03:00
|
|
|
selection->Collapse(afterNewNode, error);
|
|
|
|
if (error.Failed()) {
|
|
|
|
NS_WARNING("selection could not be collapsed after insert");
|
|
|
|
error.SuppressException();
|
|
|
|
}
|
2014-08-20 16:25:16 +04:00
|
|
|
return NS_OK;
|
1999-01-08 01:36:23 +03:00
|
|
|
}
|
|
|
|
|
2017-11-08 10:49:06 +03:00
|
|
|
void
|
|
|
|
CreateElementTransaction::InsertNewNode(ErrorResult& aError)
|
|
|
|
{
|
|
|
|
if (mPointToInsert.IsSetAndValid()) {
|
|
|
|
if (mPointToInsert.IsEndOfContainer()) {
|
2017-12-07 12:45:52 +03:00
|
|
|
mPointToInsert.GetContainer()->AppendChild(*mNewNode, aError);
|
2017-11-08 10:49:06 +03:00
|
|
|
NS_WARNING_ASSERTION(!aError.Failed(), "Failed to append the new node");
|
|
|
|
return;
|
|
|
|
}
|
2017-12-07 12:45:52 +03:00
|
|
|
mPointToInsert.GetContainer()->
|
|
|
|
InsertBefore(*mNewNode,
|
2017-12-07 13:08:56 +03:00
|
|
|
mPointToInsert.GetChild(),
|
2017-12-07 12:45:52 +03:00
|
|
|
aError);
|
2017-11-08 10:49:06 +03:00
|
|
|
NS_WARNING_ASSERTION(!aError.Failed(), "Failed to insert the new node");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-07 13:08:56 +03:00
|
|
|
if (NS_WARN_IF(mPointToInsert.GetChild() &&
|
2017-12-07 12:45:52 +03:00
|
|
|
mPointToInsert.GetContainer() !=
|
2017-12-07 13:08:56 +03:00
|
|
|
mPointToInsert.GetChild()->GetParentNode())) {
|
2017-11-08 10:49:06 +03:00
|
|
|
aError.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If mPointToInsert has only offset and it's not valid, we need to treat
|
|
|
|
// it as pointing end of the container.
|
2017-12-07 12:45:52 +03:00
|
|
|
mPointToInsert.GetContainer()->AppendChild(*mNewNode, aError);
|
2017-11-08 10:49:06 +03:00
|
|
|
NS_WARNING_ASSERTION(!aError.Failed(), "Failed to append the new node");
|
|
|
|
}
|
|
|
|
|
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-11-08 10:49:06 +03:00
|
|
|
if (NS_WARN_IF(!mEditorBase) || NS_WARN_IF(!mPointToInsert.IsSet())) {
|
2017-03-21 13:00:36 +03:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
1999-08-19 17:30:48 +04:00
|
|
|
|
2017-11-08 10:49:06 +03:00
|
|
|
ErrorResult error;
|
2017-12-07 12:45:52 +03:00
|
|
|
mPointToInsert.GetContainer()->RemoveChild(*mNewNode, error);
|
2017-11-08 10:49:06 +03:00
|
|
|
if (NS_WARN_IF(error.Failed())) {
|
|
|
|
return error.StealNSResult();
|
|
|
|
}
|
|
|
|
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::RedoTransaction()
|
1999-01-08 01:36:23 +03:00
|
|
|
{
|
2017-11-08 10:49:06 +03:00
|
|
|
if (NS_WARN_IF(!mEditorBase) || NS_WARN_IF(!mPointToInsert.IsSet())) {
|
2017-03-21 13:00:36 +03:00
|
|
|
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)
|
2017-11-08 10:49:06 +03:00
|
|
|
// XXX Don't we need to set selection like DoTransaction()?
|
2014-08-20 16:25:16 +04:00
|
|
|
|
|
|
|
// Now, reinsert mNewNode
|
2017-11-08 10:49:06 +03:00
|
|
|
ErrorResult error;
|
|
|
|
InsertNewNode(error);
|
|
|
|
if (NS_WARN_IF(error.Failed())) {
|
|
|
|
return error.StealNSResult();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
1999-01-08 01:36:23 +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
|