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-02-24 20:24:37 +03:00
|
|
|
|
2012-07-13 10:33:42 +04:00
|
|
|
#include <stdio.h> // for printf
|
|
|
|
|
1999-02-24 20:24:37 +03:00
|
|
|
#include "InsertElementTxn.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsAString.h"
|
|
|
|
#include "nsDebug.h" // for NS_ENSURE_TRUE, etc
|
|
|
|
#include "nsError.h" // for NS_ERROR_NULL_POINTER, etc
|
|
|
|
#include "nsIContent.h" // for nsIContent
|
|
|
|
#include "nsIEditor.h" // for nsIEditor
|
|
|
|
#include "nsINode.h" // for nsINode
|
|
|
|
#include "nsISelection.h" // for nsISelection
|
|
|
|
#include "nsMemory.h" // for nsMemory
|
|
|
|
#include "nsReadableUtils.h" // for ToNewCString
|
|
|
|
#include "nsString.h" // for nsString
|
1999-02-24 20:24:37 +03:00
|
|
|
|
2012-06-25 23:59:42 +04:00
|
|
|
#ifdef DEBUG
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool gNoisy = false;
|
2000-10-29 02:17:53 +04:00
|
|
|
#endif
|
|
|
|
|
1999-02-24 20:24:37 +03:00
|
|
|
|
|
|
|
InsertElementTxn::InsertElementTxn()
|
|
|
|
: EditTxn()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-05-09 08:59:25 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(InsertElementTxn)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(InsertElementTxn, EditTxn)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mNode)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mParent)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(InsertElementTxn, EditTxn)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mNode)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mParent)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2011-02-20 01:18:37 +03:00
|
|
|
NS_IMPL_ADDREF_INHERITED(InsertElementTxn, EditTxn)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(InsertElementTxn, EditTxn)
|
2009-05-09 08:59:25 +04:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(InsertElementTxn)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(EditTxn)
|
|
|
|
|
1999-03-02 08:30:53 +03:00
|
|
|
NS_IMETHODIMP InsertElementTxn::Init(nsIDOMNode *aNode,
|
|
|
|
nsIDOMNode *aParent,
|
As a reminder, we decided to do this based strictly content. Some support for style-based text properties is written, but not used
anywhere any more.
* Cleaned up split and join undo/redo.
* Added TypeInState, a data struct that remembers things about text properties for collapsed selections, so you can type
* Ctrl-B with an insertion point and the next character will be bold.
* Added all the logic to handle inline vs. block elements when setting text properties.
* Added some support for italic and underline as well. Adding these things is pretty easy now. Ctrl-B, Ctrl-I, Ctrl-U for testing bold, italic, underline.
* Added all the logic to make sure we only add style tags where they're needed, so you should never get the same style tag nested within itself, except as needed for block elements.
* Added methods for testing a node to see if a particular style is set. This isn't 100% done yet, but with very little work we could have toolbar buttons that respond to selection changed notification that show the state of bold, italic, underline, etc. in real time. Supports tri-state: whole selection is bold, some of selection is bold, none of selection is bold, ...
* Fully undoable and redoable.
* Added some debug printfs to transactions and editors. all controlled by a gNoisy static in each module. helps me track down undo/redo problems. if the output bugs people enough, I'll shut it off and re-enable it in my local tree.
Noticably missing: make un-bold, make un-italic, etc. This is coming soon.
1999-04-01 21:58:07 +04:00
|
|
|
PRInt32 aOffset,
|
|
|
|
nsIEditor *aEditor)
|
1999-02-24 20:24:37 +03:00
|
|
|
{
|
As a reminder, we decided to do this based strictly content. Some support for style-based text properties is written, but not used
anywhere any more.
* Cleaned up split and join undo/redo.
* Added TypeInState, a data struct that remembers things about text properties for collapsed selections, so you can type
* Ctrl-B with an insertion point and the next character will be bold.
* Added all the logic to handle inline vs. block elements when setting text properties.
* Added some support for italic and underline as well. Adding these things is pretty easy now. Ctrl-B, Ctrl-I, Ctrl-U for testing bold, italic, underline.
* Added all the logic to make sure we only add style tags where they're needed, so you should never get the same style tag nested within itself, except as needed for block elements.
* Added methods for testing a node to see if a particular style is set. This isn't 100% done yet, but with very little work we could have toolbar buttons that respond to selection changed notification that show the state of bold, italic, underline, etc. in real time. Supports tri-state: whole selection is bold, some of selection is bold, none of selection is bold, ...
* Fully undoable and redoable.
* Added some debug printfs to transactions and editors. all controlled by a gNoisy static in each module. helps me track down undo/redo problems. if the output bugs people enough, I'll shut it off and re-enable it in my local tree.
Noticably missing: make un-bold, make un-italic, etc. This is coming soon.
1999-04-01 21:58:07 +04:00
|
|
|
NS_ASSERTION(aNode && aParent && aEditor, "bad arg");
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(aNode && aParent && aEditor, NS_ERROR_NULL_POINTER);
|
1999-02-24 20:24:37 +03:00
|
|
|
|
|
|
|
mNode = do_QueryInterface(aNode);
|
|
|
|
mParent = do_QueryInterface(aParent);
|
1999-02-25 19:09:18 +03:00
|
|
|
mOffset = aOffset;
|
1999-05-28 04:13:02 +04:00
|
|
|
mEditor = aEditor;
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(mNode && mParent && mEditor, NS_ERROR_INVALID_ARG);
|
1999-02-24 20:24:37 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-03-09 17:23:59 +03:00
|
|
|
NS_IMETHODIMP InsertElementTxn::DoTransaction(void)
|
1999-02-24 20:24:37 +03:00
|
|
|
{
|
2012-06-25 23:59:42 +04:00
|
|
|
#ifdef DEBUG
|
2000-10-29 02:17:53 +04:00
|
|
|
if (gNoisy)
|
1999-04-12 02:53:17 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIContent>nodeAsContent = do_QueryInterface(mNode);
|
|
|
|
nsCOMPtr<nsIContent>parentAsContent = do_QueryInterface(mParent);
|
1999-09-11 02:34:18 +04:00
|
|
|
nsString namestr;
|
|
|
|
mNode->GetNodeName(namestr);
|
2001-09-29 12:28:41 +04:00
|
|
|
char* nodename = ToNewCString(namestr);
|
2000-10-29 02:17:53 +04:00
|
|
|
printf("%p Do Insert Element of %p <%s> into parent %p at offset %d\n",
|
2010-01-18 02:11:04 +03:00
|
|
|
static_cast<void*>(this),
|
|
|
|
static_cast<void*>(nodeAsContent.get()),
|
|
|
|
nodename,
|
|
|
|
static_cast<void*>(parentAsContent.get()),
|
|
|
|
mOffset);
|
2000-06-03 13:46:12 +04:00
|
|
|
nsMemory::Free(nodename);
|
1999-04-12 02:53:17 +04:00
|
|
|
}
|
2003-05-24 01:43:10 +04:00
|
|
|
#endif
|
1999-09-11 02:34:18 +04:00
|
|
|
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(mNode && mParent, NS_ERROR_NOT_INITIALIZED);
|
1999-02-24 20:24:37 +03:00
|
|
|
|
2012-06-06 11:41:30 +04:00
|
|
|
nsCOMPtr<nsINode> parent = do_QueryInterface(mParent);
|
|
|
|
NS_ENSURE_STATE(parent);
|
|
|
|
|
|
|
|
PRUint32 count = parent->GetChildCount();
|
|
|
|
if (mOffset > PRInt32(count) || mOffset == -1) {
|
2002-10-09 03:06:38 +04:00
|
|
|
// -1 is sentinel value meaning "append at end"
|
2012-06-06 11:41:30 +04:00
|
|
|
mOffset = count;
|
1999-02-24 20:24:37 +03:00
|
|
|
}
|
|
|
|
|
2012-06-06 11:41:30 +04:00
|
|
|
nsIContent* refContent = parent->GetChildAt(mOffset);
|
|
|
|
// note, it's ok for refNode to be null. that means append
|
|
|
|
nsCOMPtr<nsIDOMNode> refNode = refContent ? refContent->AsDOMNode() : nsnull;
|
|
|
|
|
2000-03-24 03:13:30 +03:00
|
|
|
mEditor->MarkNodeDirty(mNode);
|
|
|
|
|
1999-02-24 20:24:37 +03:00
|
|
|
nsCOMPtr<nsIDOMNode> resultNode;
|
2012-06-06 11:41:30 +04:00
|
|
|
nsresult result = mParent->InsertBefore(mNode, refNode, getter_AddRefs(resultNode));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(resultNode, NS_ERROR_NULL_POINTER);
|
2000-01-04 06:09:41 +03:00
|
|
|
|
|
|
|
// only set selection to insertion point if editor gives permission
|
2011-09-29 10:19:26 +04:00
|
|
|
bool bAdjustSelection;
|
2000-01-04 06:09:41 +03:00
|
|
|
mEditor->ShouldTxnSetSelection(&bAdjustSelection);
|
|
|
|
if (bAdjustSelection)
|
|
|
|
{
|
2000-09-14 15:45:01 +04:00
|
|
|
nsCOMPtr<nsISelection> selection;
|
2000-01-04 06:09:41 +03:00
|
|
|
result = mEditor->GetSelection(getter_AddRefs(selection));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2000-01-04 06:09:41 +03:00
|
|
|
// place the selection just after the inserted element
|
|
|
|
selection->Collapse(mParent, mOffset+1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// do nothing - dom range gravity will adjust selection
|
|
|
|
}
|
1999-02-24 20:24:37 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2001-03-09 17:23:59 +03:00
|
|
|
NS_IMETHODIMP InsertElementTxn::UndoTransaction(void)
|
1999-02-24 20:24:37 +03:00
|
|
|
{
|
2012-06-25 23:59:42 +04:00
|
|
|
#ifdef DEBUG
|
2010-01-18 02:11:04 +03:00
|
|
|
if (gNoisy)
|
|
|
|
{
|
|
|
|
printf("%p Undo Insert Element of %p into parent %p at offset %d\n",
|
|
|
|
static_cast<void*>(this),
|
|
|
|
static_cast<void*>(mNode.get()),
|
|
|
|
static_cast<void*>(mParent.get()),
|
|
|
|
mOffset);
|
|
|
|
}
|
2003-05-24 01:43:10 +04:00
|
|
|
#endif
|
|
|
|
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(mNode && mParent, NS_ERROR_NOT_INITIALIZED);
|
1999-02-24 20:24:37 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> resultNode;
|
2003-06-30 18:33:23 +04:00
|
|
|
return mParent->RemoveChild(mNode, getter_AddRefs(resultNode));
|
1999-02-24 20:24:37 +03:00
|
|
|
}
|
|
|
|
|
2002-03-24 01:16:54 +03:00
|
|
|
NS_IMETHODIMP InsertElementTxn::GetTxnDescription(nsAString& aString)
|
1999-02-24 20:24:37 +03:00
|
|
|
{
|
2004-06-17 04:13:25 +04:00
|
|
|
aString.AssignLiteral("InsertElementTxn");
|
1999-02-24 20:24:37 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|