2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-12 23:33:42 -07:00
|
|
|
#include <stdio.h> // for printf
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "SplitElementTxn.h"
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "nsAString.h"
|
|
|
|
#include "nsDebug.h" // for NS_ASSERTION, etc
|
|
|
|
#include "nsEditor.h" // for nsEditor
|
|
|
|
#include "nsError.h" // for NS_ERROR_NOT_INITIALIZED, etc
|
2013-01-09 15:05:08 -08:00
|
|
|
#include "nsIContent.h" // for nsIContent
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "nsIDOMCharacterData.h" // for nsIDOMCharacterData
|
|
|
|
#include "nsIEditor.h" // for nsEditor::DebugDumpContent, etc
|
|
|
|
#include "nsISelection.h" // for nsISelection
|
|
|
|
#include "nsISupportsUtils.h" // for NS_ADDREF
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-01-09 15:05:08 -08:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2012-06-25 12:59:42 -07:00
|
|
|
#ifdef DEBUG
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool gNoisy = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
// note that aEditor is not refcounted
|
|
|
|
SplitElementTxn::SplitElementTxn()
|
|
|
|
: EditTxn()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-05-21 16:23:53 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED_2(SplitElementTxn, EditTxn,
|
|
|
|
mParent,
|
|
|
|
mNewLeftNode)
|
2009-05-08 21:59:25 -07:00
|
|
|
|
2011-02-19 14:18:37 -08:00
|
|
|
NS_IMPL_ADDREF_INHERITED(SplitElementTxn, EditTxn)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(SplitElementTxn, EditTxn)
|
2009-05-08 21:59:25 -07:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(SplitElementTxn)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(EditTxn)
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP SplitElementTxn::Init(nsEditor *aEditor,
|
2013-01-09 15:05:08 -08:00
|
|
|
nsINode *aNode,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aOffset)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aEditor && aNode, "bad args");
|
|
|
|
if (!aEditor || !aNode) { return NS_ERROR_NOT_INITIALIZED; }
|
|
|
|
|
|
|
|
mEditor = aEditor;
|
|
|
|
mExistingRightNode = do_QueryInterface(aNode);
|
|
|
|
mOffset = aOffset;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP SplitElementTxn::DoTransaction(void)
|
|
|
|
{
|
2012-06-25 12:59:42 -07:00
|
|
|
#ifdef DEBUG
|
2010-01-17 15:11:04 -08:00
|
|
|
if (gNoisy)
|
|
|
|
{
|
|
|
|
printf("%p Do Split of node %p offset %d\n",
|
|
|
|
static_cast<void*>(this),
|
|
|
|
static_cast<void*>(mExistingRightNode.get()),
|
|
|
|
mOffset);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
NS_ASSERTION(mExistingRightNode && mEditor, "bad state");
|
|
|
|
if (!mExistingRightNode || !mEditor) { return NS_ERROR_NOT_INITIALIZED; }
|
|
|
|
|
|
|
|
// create a new node
|
2013-01-09 15:05:08 -08:00
|
|
|
ErrorResult rv;
|
|
|
|
mNewLeftNode = mExistingRightNode->CloneNode(false, rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv.ErrorCode(), rv.ErrorCode());
|
|
|
|
NS_ASSERTION(mNewLeftNode, "could not create element.");
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(mNewLeftNode, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
mEditor->MarkNodeDirty(mExistingRightNode);
|
|
|
|
|
2012-06-25 12:59:42 -07:00
|
|
|
#ifdef DEBUG
|
2010-01-17 15:11:04 -08:00
|
|
|
if (gNoisy)
|
|
|
|
{
|
|
|
|
printf(" created left node = %p\n",
|
|
|
|
static_cast<void*>(mNewLeftNode.get()));
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// get the parent node
|
2013-01-09 15:05:08 -08:00
|
|
|
mParent = mExistingRightNode->GetParentNode();
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(mParent, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// insert the new node
|
2013-01-09 15:05:08 -08:00
|
|
|
nsresult result = mEditor->SplitNodeImpl(mExistingRightNode->AsDOMNode(),
|
|
|
|
mOffset,
|
|
|
|
mNewLeftNode->AsDOMNode(),
|
|
|
|
mParent->AsDOMNode());
|
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2010-11-30 21:24:09 -08:00
|
|
|
if (mNewLeftNode) {
|
2011-09-28 23:19:26 -07:00
|
|
|
bool bAdjustSelection;
|
2010-11-30 21:24:09 -08:00
|
|
|
mEditor->ShouldTxnSetSelection(&bAdjustSelection);
|
|
|
|
if (bAdjustSelection)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
result = mEditor->GetSelection(getter_AddRefs(selection));
|
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2013-01-09 15:05:08 -08:00
|
|
|
result = selection->Collapse(mNewLeftNode->AsDOMNode(), mOffset);
|
2010-11-30 21:24:09 -08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// do nothing - dom range gravity will adjust selection
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP SplitElementTxn::UndoTransaction(void)
|
|
|
|
{
|
2012-06-25 12:59:42 -07:00
|
|
|
#ifdef DEBUG
|
2007-03-22 10:30:00 -07:00
|
|
|
if (gNoisy) {
|
2010-01-17 15:11:04 -08:00
|
|
|
printf("%p Undo Split of existing node %p and new node %p offset %d\n",
|
|
|
|
static_cast<void*>(this),
|
|
|
|
static_cast<void*>(mExistingRightNode.get()),
|
|
|
|
static_cast<void*>(mNewLeftNode.get()),
|
|
|
|
mOffset);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
NS_ASSERTION(mEditor && mExistingRightNode && mNewLeftNode && mParent, "bad state");
|
|
|
|
if (!mEditor || !mExistingRightNode || !mNewLeftNode || !mParent) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// this assumes Do inserted the new node in front of the prior existing node
|
2013-07-24 00:39:08 -07:00
|
|
|
nsresult rv = mEditor->JoinNodesImpl(mExistingRightNode, mNewLeftNode,
|
|
|
|
mParent);
|
2012-06-25 12:59:42 -07:00
|
|
|
#ifdef DEBUG
|
2007-03-22 10:30:00 -07:00
|
|
|
if (gNoisy)
|
|
|
|
{
|
2010-01-17 15:11:04 -08:00
|
|
|
printf("** after join left child node %p into right node %p\n",
|
|
|
|
static_cast<void*>(mNewLeftNode.get()),
|
|
|
|
static_cast<void*>(mExistingRightNode.get()));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (gNoisy) {mEditor->DebugDumpContent(); } // DEBUG
|
|
|
|
}
|
2013-07-24 00:39:08 -07:00
|
|
|
if (NS_SUCCEEDED(rv))
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-01-17 15:11:04 -08:00
|
|
|
if (gNoisy)
|
|
|
|
{
|
|
|
|
printf(" left node = %p removed\n",
|
|
|
|
static_cast<void*>(mNewLeftNode.get()));
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-07-24 00:39:08 -07:00
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* redo cannot simply resplit the right node, because subsequent transactions
|
|
|
|
* on the redo stack may depend on the left node existing in its previous state.
|
|
|
|
*/
|
|
|
|
NS_IMETHODIMP SplitElementTxn::RedoTransaction(void)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mEditor && mExistingRightNode && mNewLeftNode && mParent, "bad state");
|
|
|
|
if (!mEditor || !mExistingRightNode || !mNewLeftNode || !mParent) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
2012-06-25 12:59:42 -07:00
|
|
|
#ifdef DEBUG
|
2007-03-22 10:30:00 -07:00
|
|
|
if (gNoisy) {
|
2010-01-17 15:11:04 -08:00
|
|
|
printf("%p Redo Split of existing node %p and new node %p offset %d\n",
|
|
|
|
static_cast<void*>(this),
|
|
|
|
static_cast<void*>(mExistingRightNode.get()),
|
|
|
|
static_cast<void*>(mNewLeftNode.get()),
|
|
|
|
mOffset);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (gNoisy) {mEditor->DebugDumpContent(); } // DEBUG
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// first, massage the existing node so it is in its post-split state
|
|
|
|
nsCOMPtr<nsIDOMCharacterData>rightNodeAsText = do_QueryInterface(mExistingRightNode);
|
|
|
|
if (rightNodeAsText)
|
|
|
|
{
|
2013-01-09 16:09:34 -08:00
|
|
|
nsresult result = rightNodeAsText->DeleteData(0, mOffset);
|
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2012-06-25 12:59:42 -07:00
|
|
|
#ifdef DEBUG
|
2007-03-22 10:30:00 -07:00
|
|
|
if (gNoisy)
|
|
|
|
{
|
2010-01-17 15:11:04 -08:00
|
|
|
printf("** after delete of text in right text node %p offset %d\n",
|
|
|
|
static_cast<void*>(rightNodeAsText.get()),
|
|
|
|
mOffset);
|
2007-03-22 10:30:00 -07:00
|
|
|
mEditor->DebugDumpContent(); // DEBUG
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-01-14 18:06:19 -08:00
|
|
|
nsCOMPtr<nsINode> child = mExistingRightNode->GetFirstChild();
|
2013-01-09 15:05:08 -08:00
|
|
|
for (int32_t i=0; i<mOffset; i++)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (!child) {return NS_ERROR_NULL_POINTER;}
|
2013-01-09 15:05:08 -08:00
|
|
|
ErrorResult rv;
|
|
|
|
mExistingRightNode->RemoveChild(*child, rv);
|
|
|
|
if (NS_SUCCEEDED(rv.ErrorCode()))
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-01-09 15:05:08 -08:00
|
|
|
mNewLeftNode->AppendChild(*child, rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv.ErrorCode(), rv.ErrorCode());
|
2012-06-25 12:59:42 -07:00
|
|
|
#ifdef DEBUG
|
2013-01-09 15:05:08 -08:00
|
|
|
if (gNoisy)
|
|
|
|
{
|
2010-01-17 15:11:04 -08:00
|
|
|
printf("** move child node %p from right node %p to left node %p\n",
|
2013-01-09 15:05:08 -08:00
|
|
|
static_cast<void*>(child),
|
2010-01-17 15:11:04 -08:00
|
|
|
static_cast<void*>(mExistingRightNode.get()),
|
|
|
|
static_cast<void*>(mNewLeftNode.get()));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (gNoisy) {mEditor->DebugDumpContent(); } // DEBUG
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2013-01-09 15:05:08 -08:00
|
|
|
child = child->GetNextSibling();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
2013-01-09 15:05:08 -08:00
|
|
|
// second, re-insert the left node into the tree
|
|
|
|
ErrorResult rv;
|
|
|
|
mParent->InsertBefore(*mNewLeftNode, mExistingRightNode, rv);
|
2012-06-25 12:59:42 -07:00
|
|
|
#ifdef DEBUG
|
2013-01-09 15:05:08 -08:00
|
|
|
if (gNoisy)
|
|
|
|
{
|
2010-01-17 15:11:04 -08:00
|
|
|
printf("** reinsert left child node %p before right node %p\n",
|
|
|
|
static_cast<void*>(mNewLeftNode.get()),
|
|
|
|
static_cast<void*>(mExistingRightNode.get()));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (gNoisy) {mEditor->DebugDumpContent(); } // DEBUG
|
|
|
|
}
|
|
|
|
#endif
|
2013-01-09 15:05:08 -08:00
|
|
|
return rv.ErrorCode();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP SplitElementTxn::GetTxnDescription(nsAString& aString)
|
|
|
|
{
|
|
|
|
aString.AssignLiteral("SplitElementTxn");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-01-09 15:05:08 -08:00
|
|
|
NS_IMETHODIMP SplitElementTxn::GetNewNode(nsINode **aNewNode)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aNewNode, NS_ERROR_NULL_POINTER);
|
|
|
|
NS_ENSURE_TRUE(mNewLeftNode, NS_ERROR_NOT_INITIALIZED);
|
2007-03-22 10:30:00 -07:00
|
|
|
*aNewNode = mNewLeftNode;
|
|
|
|
NS_ADDREF(*aNewNode);
|
|
|
|
return NS_OK;
|
|
|
|
}
|