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;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
JoinElementTxn::JoinElementTxn()
|
|
|
|
: EditTxn()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-04-25 09:49:00 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(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)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION((mEditor && mLeftNode && mRightNode), "null arg");
|
|
|
|
if (!mEditor || !mLeftNode || !mRightNode) { return NS_ERROR_NOT_INITIALIZED; }
|
|
|
|
|
|
|
|
// get the parent node
|
2013-07-24 00:39:08 -07:00
|
|
|
nsCOMPtr<nsINode> leftParent = mLeftNode->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
|
2013-07-24 00:39:08 -07:00
|
|
|
nsCOMPtr<nsINode> rightParent = mRightNode->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;
|
2013-07-24 00:39:08 -07:00
|
|
|
mOffset = mLeftNode->Length();
|
2012-06-06 00:41:22 -07:00
|
|
|
|
2013-11-12 05:55:43 -08:00
|
|
|
return mEditor->JoinNodesImpl(mRightNode, mLeftNode, mParent);
|
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)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|