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 "JoinElementTxn.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_NULL_POINTER, 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::IsModifiableNode
|
|
|
|
#include "nsISupportsImpl.h" // for EditTxn::QueryInterface, etc
|
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
|
|
|
|
|
|
|
|
JoinElementTxn::JoinElementTxn()
|
|
|
|
: EditTxn()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-05-21 16:23:53 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED_3(JoinElementTxn, EditTxn,
|
|
|
|
mLeftNode,
|
|
|
|
mRightNode,
|
|
|
|
mParent)
|
2009-05-08 21:59:25 -07:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(JoinElementTxn)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(EditTxn)
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP JoinElementTxn::Init(nsEditor *aEditor,
|
2013-01-09 15:05:08 -08:00
|
|
|
nsINode *aLeftNode,
|
|
|
|
nsINode *aRightNode)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_PRECONDITION((aEditor && aLeftNode && aRightNode), "null arg");
|
|
|
|
if (!aEditor || !aLeftNode || !aRightNode) { return NS_ERROR_NULL_POINTER; }
|
|
|
|
mEditor = aEditor;
|
2013-01-09 15:05:08 -08:00
|
|
|
mLeftNode = aLeftNode;
|
2013-01-14 18:06:19 -08:00
|
|
|
nsCOMPtr<nsINode> leftParent = mLeftNode->GetParentNode();
|
2007-06-27 19:48:16 -07:00
|
|
|
if (!mEditor->IsModifiableNode(leftParent)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2013-01-09 15:05:08 -08:00
|
|
|
mRightNode = aRightNode;
|
2013-01-14 18:06:19 -08:00
|
|
|
mOffset = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// After DoTransaction() and RedoTransaction(), the left node is removed from the content tree and right node remains.
|
|
|
|
NS_IMETHODIMP JoinElementTxn::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 Join of %p and %p\n",
|
|
|
|
static_cast<void*>(this),
|
|
|
|
static_cast<void*>(mLeftNode.get()),
|
|
|
|
static_cast<void*>(mRightNode.get()));
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
NS_PRECONDITION((mEditor && mLeftNode && mRightNode), "null arg");
|
|
|
|
if (!mEditor || !mLeftNode || !mRightNode) { return NS_ERROR_NOT_INITIALIZED; }
|
|
|
|
|
2012-06-06 00:41:22 -07:00
|
|
|
nsCOMPtr<nsINode> leftNode = do_QueryInterface(mLeftNode);
|
|
|
|
NS_ENSURE_STATE(leftNode);
|
|
|
|
|
|
|
|
nsCOMPtr<nsINode> rightNode = do_QueryInterface(mRightNode);
|
|
|
|
NS_ENSURE_STATE(rightNode);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// get the parent node
|
2012-10-09 05:31:24 -07:00
|
|
|
nsCOMPtr<nsINode> leftParent = leftNode->GetParentNode();
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(leftParent, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// verify that mLeftNode and mRightNode have the same parent
|
2012-10-09 05:31:24 -07:00
|
|
|
nsCOMPtr<nsINode> rightParent = rightNode->GetParentNode();
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(rightParent, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-06 00:41:22 -07:00
|
|
|
if (leftParent != rightParent) {
|
2011-10-17 07:59:28 -07:00
|
|
|
NS_ASSERTION(false, "2 nodes do not have same parent");
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
2012-06-06 00:41:22 -07:00
|
|
|
|
|
|
|
// set this instance mParent.
|
|
|
|
// Other methods will see a non-null mParent and know all is well
|
2013-01-09 15:05:08 -08:00
|
|
|
mParent = leftParent;
|
2012-06-06 00:41:22 -07:00
|
|
|
mOffset = leftNode->Length();
|
|
|
|
|
2013-01-09 15:05:08 -08:00
|
|
|
nsresult rv = mEditor->JoinNodesImpl(mRightNode->AsDOMNode(),
|
|
|
|
mLeftNode->AsDOMNode(),
|
2013-07-24 00:39:01 -07:00
|
|
|
mParent->AsDOMNode());
|
2012-06-06 00:41:22 -07:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (NS_SUCCEEDED(rv) && gNoisy) {
|
|
|
|
printf(" left node = %p removed\n", static_cast<void*>(mLeftNode.get()));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//XXX: what if instead of split, we just deleted the unneeded children of mRight
|
|
|
|
// and re-inserted mLeft?
|
|
|
|
NS_IMETHODIMP JoinElementTxn::UndoTransaction(void)
|
|
|
|
{
|
2012-06-25 12:59:42 -07:00
|
|
|
#ifdef DEBUG
|
2010-01-17 15:11:04 -08:00
|
|
|
if (gNoisy)
|
|
|
|
{
|
|
|
|
printf("%p Undo Join, right node = %p\n",
|
|
|
|
static_cast<void*>(this),
|
|
|
|
static_cast<void*>(mRightNode.get()));
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
NS_ASSERTION(mRightNode && mLeftNode && mParent, "bad state");
|
|
|
|
if (!mRightNode || !mLeftNode || !mParent) { return NS_ERROR_NOT_INITIALIZED; }
|
|
|
|
// first, massage the existing node so it is in its post-split state
|
|
|
|
nsCOMPtr<nsIDOMCharacterData>rightNodeAsText = do_QueryInterface(mRightNode);
|
2013-01-09 15:05:08 -08:00
|
|
|
ErrorResult rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (rightNodeAsText)
|
|
|
|
{
|
2013-01-09 15:05:08 -08:00
|
|
|
rv = rightNodeAsText->DeleteData(0, mOffset);
|
|
|
|
NS_ENSURE_SUCCESS(rv.ErrorCode(), rv.ErrorCode());
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-01-14 18:06:19 -08:00
|
|
|
for (nsCOMPtr<nsINode> child = mRightNode->GetFirstChild();
|
2013-01-09 15:05:08 -08:00
|
|
|
child;
|
|
|
|
child = child->GetNextSibling())
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-01-09 15:05:08 -08:00
|
|
|
mLeftNode->AppendChild(*child, rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv.ErrorCode(), rv.ErrorCode());
|
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
|
|
|
|
mParent->InsertBefore(*mLeftNode, mRightNode, rv);
|
|
|
|
return rv.ErrorCode();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP JoinElementTxn::GetTxnDescription(nsAString& aString)
|
|
|
|
{
|
|
|
|
aString.AssignLiteral("JoinElementTxn");
|
|
|
|
return NS_OK;
|
|
|
|
}
|