2001-09-25 22:53:13 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 11:12:37 +00: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-01-08 22:16:22 +00:00
|
|
|
|
2012-07-13 06:33:42 +00:00
|
|
|
#include <stdio.h> // for printf
|
|
|
|
|
1999-01-08 22:16:22 +00:00
|
|
|
#include "SplitElementTxn.h"
|
2012-07-13 06:33:42 +00: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 23:05:08 +00:00
|
|
|
#include "nsIContent.h" // for nsIContent
|
2012-07-13 06:33:42 +00:00
|
|
|
#include "nsIDOMCharacterData.h" // for nsIDOMCharacterData
|
|
|
|
#include "nsIEditor.h" // for nsEditor::DebugDumpContent, etc
|
|
|
|
#include "nsISelection.h" // for nsISelection
|
|
|
|
#include "nsISupportsUtils.h" // for NS_ADDREF
|
1999-02-17 19:42:29 +00:00
|
|
|
|
2013-01-09 23:05:08 +00:00
|
|
|
using namespace mozilla;
|
|
|
|
|
1999-01-08 22:16:22 +00:00
|
|
|
// note that aEditor is not refcounted
|
1999-01-21 01:51:09 +00:00
|
|
|
SplitElementTxn::SplitElementTxn()
|
|
|
|
: EditTxn()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-07-08 21:23:18 +00:00
|
|
|
SplitElementTxn::~SplitElementTxn()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-04-25 16:49:00 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(SplitElementTxn, EditTxn,
|
|
|
|
mParent,
|
|
|
|
mNewLeftNode)
|
2009-05-09 04:59:25 +00:00
|
|
|
|
2011-02-19 22:18:37 +00:00
|
|
|
NS_IMPL_ADDREF_INHERITED(SplitElementTxn, EditTxn)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(SplitElementTxn, EditTxn)
|
2009-05-09 04:59:25 +00:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(SplitElementTxn)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(EditTxn)
|
|
|
|
|
2000-01-04 03:09:41 +00:00
|
|
|
NS_IMETHODIMP SplitElementTxn::Init(nsEditor *aEditor,
|
2014-06-05 03:51:34 +00:00
|
|
|
nsIDOMNode *aNode,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t aOffset)
|
1999-01-08 22:16:22 +00:00
|
|
|
{
|
2000-01-04 03:09:41 +00:00
|
|
|
NS_ASSERTION(aEditor && aNode, "bad args");
|
|
|
|
if (!aEditor || !aNode) { return NS_ERROR_NOT_INITIALIZED; }
|
1999-08-19 13:30:48 +00:00
|
|
|
|
1999-05-28 00:16:55 +00:00
|
|
|
mEditor = aEditor;
|
1999-02-19 11:30:31 +00:00
|
|
|
mExistingRightNode = do_QueryInterface(aNode);
|
1999-01-08 22:16:22 +00:00
|
|
|
mOffset = aOffset;
|
1999-01-21 01:51:09 +00:00
|
|
|
return NS_OK;
|
1999-01-08 22:16:22 +00:00
|
|
|
}
|
|
|
|
|
2001-03-09 14:23:59 +00:00
|
|
|
NS_IMETHODIMP SplitElementTxn::DoTransaction(void)
|
1999-01-08 22:16:22 +00:00
|
|
|
{
|
1999-08-19 13:30:48 +00:00
|
|
|
NS_ASSERTION(mExistingRightNode && mEditor, "bad state");
|
|
|
|
if (!mExistingRightNode || !mEditor) { return NS_ERROR_NOT_INITIALIZED; }
|
|
|
|
|
1999-01-08 22:16:22 +00:00
|
|
|
// create a new node
|
2014-06-05 03:51:34 +00:00
|
|
|
nsresult result = mExistingRightNode->CloneNode(false, 1, getter_AddRefs(mNewLeftNode));
|
|
|
|
NS_ASSERTION(((NS_SUCCEEDED(result)) && (mNewLeftNode)), "could not create element.");
|
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2000-03-24 00:26:47 +00:00
|
|
|
mEditor->MarkNodeDirty(mExistingRightNode);
|
|
|
|
|
1999-08-19 13:30:48 +00:00
|
|
|
// get the parent node
|
2014-06-05 03:51:34 +00:00
|
|
|
result = mExistingRightNode->GetParentNode(getter_AddRefs(mParent));
|
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(mParent, NS_ERROR_NULL_POINTER);
|
1999-01-08 22:16:22 +00:00
|
|
|
|
1999-08-19 13:30:48 +00:00
|
|
|
// insert the new node
|
2014-06-05 03:51:34 +00:00
|
|
|
result = mEditor->SplitNodeImpl(mExistingRightNode, mOffset, mNewLeftNode, mParent);
|
2010-12-01 05:24:09 +00:00
|
|
|
if (mNewLeftNode) {
|
2011-09-29 06:19:26 +00:00
|
|
|
bool bAdjustSelection;
|
2010-12-01 05:24:09 +00: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);
|
2014-06-05 03:51:34 +00:00
|
|
|
result = selection->Collapse(mNewLeftNode, mOffset);
|
2010-12-01 05:24:09 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// do nothing - dom range gravity will adjust selection
|
|
|
|
}
|
1999-01-08 22:16:22 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2001-03-09 14:23:59 +00:00
|
|
|
NS_IMETHODIMP SplitElementTxn::UndoTransaction(void)
|
1999-01-08 22:16:22 +00:00
|
|
|
{
|
1999-08-19 13:30:48 +00:00
|
|
|
NS_ASSERTION(mEditor && mExistingRightNode && mNewLeftNode && mParent, "bad state");
|
|
|
|
if (!mEditor || !mExistingRightNode || !mNewLeftNode || !mParent) {
|
1999-03-17 20:56:10 +00:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
1999-01-09 00:24:48 +00:00
|
|
|
// this assumes Do inserted the new node in front of the prior existing node
|
2014-06-05 03:51:34 +00:00
|
|
|
nsCOMPtr<nsINode> right = do_QueryInterface(mExistingRightNode);
|
|
|
|
nsCOMPtr<nsINode> left = do_QueryInterface(mNewLeftNode);
|
|
|
|
nsCOMPtr<nsINode> parent = do_QueryInterface(mParent);
|
|
|
|
return mEditor->JoinNodesImpl(right, left, parent);
|
1999-01-08 22:16:22 +00: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 17:58:07 +00: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.
|
|
|
|
*/
|
2001-03-09 14:23:59 +00:00
|
|
|
NS_IMETHODIMP SplitElementTxn::RedoTransaction(void)
|
1999-01-08 22:16:22 +00:00
|
|
|
{
|
1999-08-19 13:30:48 +00:00
|
|
|
NS_ASSERTION(mEditor && mExistingRightNode && mNewLeftNode && mParent, "bad state");
|
|
|
|
if (!mEditor || !mExistingRightNode || !mNewLeftNode || !mParent) {
|
1999-03-17 20:56:10 +00:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
2003-05-23 21:43:10 +00:00
|
|
|
|
2014-06-05 03:51:34 +00:00
|
|
|
nsresult result;
|
|
|
|
nsCOMPtr<nsIDOMNode>resultNode;
|
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 17:58:07 +00:00
|
|
|
// first, massage the existing node so it is in its post-split state
|
2003-06-30 14:57:59 +00:00
|
|
|
nsCOMPtr<nsIDOMCharacterData>rightNodeAsText = do_QueryInterface(mExistingRightNode);
|
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 17:58:07 +00:00
|
|
|
if (rightNodeAsText)
|
|
|
|
{
|
2013-01-10 00:09:34 +00:00
|
|
|
nsresult result = rightNodeAsText->DeleteData(0, mOffset);
|
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
1999-02-17 19:42:29 +00: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 17:58:07 +00:00
|
|
|
else
|
|
|
|
{
|
2014-06-05 03:51:34 +00:00
|
|
|
nsCOMPtr<nsIDOMNode>child;
|
|
|
|
nsCOMPtr<nsIDOMNode>nextSibling;
|
|
|
|
result = mExistingRightNode->GetFirstChild(getter_AddRefs(child));
|
|
|
|
int32_t i;
|
|
|
|
for (i=0; i<mOffset; i++)
|
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 17:58:07 +00:00
|
|
|
{
|
2014-06-05 03:51:34 +00:00
|
|
|
if (NS_FAILED(result)) {return result;}
|
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 17:58:07 +00:00
|
|
|
if (!child) {return NS_ERROR_NULL_POINTER;}
|
2014-06-05 03:51:34 +00:00
|
|
|
child->GetNextSibling(getter_AddRefs(nextSibling));
|
|
|
|
result = mExistingRightNode->RemoveChild(child, getter_AddRefs(resultNode));
|
|
|
|
if (NS_SUCCEEDED(result))
|
1999-04-04 18:01:35 +00:00
|
|
|
{
|
2014-06-05 03:51:34 +00:00
|
|
|
result = mNewLeftNode->AppendChild(child, getter_AddRefs(resultNode));
|
1999-04-04 18:01:35 +00:00
|
|
|
}
|
2014-06-05 03:51:34 +00:00
|
|
|
child = do_QueryInterface(nextSibling);
|
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 17:58:07 +00:00
|
|
|
}
|
1999-02-17 19:42:29 +00:00
|
|
|
}
|
2013-01-09 23:05:08 +00:00
|
|
|
// second, re-insert the left node into the tree
|
2014-06-05 03:51:34 +00:00
|
|
|
result = mParent->InsertBefore(mNewLeftNode, mExistingRightNode, getter_AddRefs(resultNode));
|
|
|
|
return result;
|
1999-01-08 22:16:22 +00: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 17:58:07 +00:00
|
|
|
|
1999-01-08 22:16:22 +00:00
|
|
|
|
2002-03-23 22:16:54 +00:00
|
|
|
NS_IMETHODIMP SplitElementTxn::GetTxnDescription(nsAString& aString)
|
1999-01-08 22:16:22 +00:00
|
|
|
{
|
2004-06-17 00:13:25 +00:00
|
|
|
aString.AssignLiteral("SplitElementTxn");
|
1999-01-08 22:16:22 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-02-24 17:24:37 +00:00
|
|
|
|
2014-06-05 03:51:34 +00:00
|
|
|
NS_IMETHODIMP SplitElementTxn::GetNewNode(nsIDOMNode **aNewNode)
|
1999-02-24 17:24:37 +00:00
|
|
|
{
|
2010-06-17 20:40:48 +00:00
|
|
|
NS_ENSURE_TRUE(aNewNode, NS_ERROR_NULL_POINTER);
|
|
|
|
NS_ENSURE_TRUE(mNewLeftNode, NS_ERROR_NOT_INITIALIZED);
|
1999-02-24 17:24:37 +00:00
|
|
|
*aNewNode = mNewLeftNode;
|
|
|
|
NS_ADDREF(*aNewNode);
|
|
|
|
return NS_OK;
|
|
|
|
}
|