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,
|
2014-06-04 20:51:34 -07:00
|
|
|
nsIDOMNode *aLeftNode,
|
|
|
|
nsIDOMNode *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;
|
2014-06-04 20:51:34 -07:00
|
|
|
mLeftNode = do_QueryInterface(aLeftNode);
|
|
|
|
nsCOMPtr<nsIDOMNode>leftParent;
|
|
|
|
nsresult result = mLeftNode->GetParentNode(getter_AddRefs(leftParent));
|
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2007-06-27 19:48:16 -07:00
|
|
|
if (!mEditor->IsModifiableNode(leftParent)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2014-06-04 20:51:34 -07:00
|
|
|
mRightNode = do_QueryInterface(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
|
2014-06-04 20:51:34 -07:00
|
|
|
nsCOMPtr<nsINode> leftNode = do_QueryInterface(mLeftNode);
|
|
|
|
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
|
2014-06-04 20:51:34 -07:00
|
|
|
nsCOMPtr<nsINode> rightNode = do_QueryInterface(mRightNode);
|
|
|
|
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
|
2014-06-04 20:51:34 -07:00
|
|
|
mParent = leftParent->AsDOMNode();
|
|
|
|
mOffset = leftNode->Length();
|
2012-06-06 00:41:22 -07:00
|
|
|
|
2014-06-04 20:51:34 -07:00
|
|
|
nsCOMPtr<nsINode> parent = do_QueryInterface(mParent);
|
|
|
|
return mEditor->JoinNodesImpl(rightNode, leftNode, parent);
|
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
|
2014-06-04 20:51:34 -07:00
|
|
|
nsresult result;
|
|
|
|
nsCOMPtr<nsIDOMNode>resultNode;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIDOMCharacterData>rightNodeAsText = do_QueryInterface(mRightNode);
|
|
|
|
if (rightNodeAsText)
|
|
|
|
{
|
2014-06-04 20:51:34 -07:00
|
|
|
result = rightNodeAsText->DeleteData(0, mOffset);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-06-04 20:51:34 -07:00
|
|
|
nsCOMPtr<nsIDOMNode>child;
|
|
|
|
result = mRightNode->GetFirstChild(getter_AddRefs(child));
|
|
|
|
nsCOMPtr<nsIDOMNode>nextSibling;
|
|
|
|
uint32_t i;
|
|
|
|
for (i=0; i<mOffset; i++)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-06-04 20:51:34 -07:00
|
|
|
if (NS_FAILED(result)) {return result;}
|
|
|
|
if (!child) {return NS_ERROR_NULL_POINTER;}
|
|
|
|
child->GetNextSibling(getter_AddRefs(nextSibling));
|
|
|
|
result = mLeftNode->AppendChild(child, getter_AddRefs(resultNode));
|
|
|
|
child = do_QueryInterface(nextSibling);
|
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
|
2014-06-04 20:51:34 -07:00
|
|
|
result = mParent->InsertBefore(mLeftNode, mRightNode, getter_AddRefs(resultNode));
|
|
|
|
return result;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP JoinElementTxn::GetTxnDescription(nsAString& aString)
|
|
|
|
{
|
|
|
|
aString.AssignLiteral("JoinElementTxn");
|
|
|
|
return NS_OK;
|
|
|
|
}
|