From 3ee55b4626d8e2ef3838f915fa4119de9487cf8f Mon Sep 17 00:00:00 2001 From: "buster%netscape.com" Date: Wed, 17 Mar 1999 20:56:10 +0000 Subject: [PATCH] split and join now properly remember which content gets deleted, and which content remains in the tree split and join now properly set selection to the remaining node, at the proper offset. fixes bugs 3910 3881, with approval from chofmann git-svn-id: svn://10.0.0.236/trunk@24272 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/editor/base/JoinElementTxn.cpp | 50 +++++++++++++++- mozilla/editor/base/SplitElementTxn.cpp | 60 +++++++++++++++++-- mozilla/editor/base/SplitElementTxn.h | 2 +- .../editor/libeditor/base/JoinElementTxn.cpp | 50 +++++++++++++++- .../editor/libeditor/base/SplitElementTxn.cpp | 60 +++++++++++++++++-- .../editor/libeditor/base/SplitElementTxn.h | 2 +- 6 files changed, 206 insertions(+), 18 deletions(-) diff --git a/mozilla/editor/base/JoinElementTxn.cpp b/mozilla/editor/base/JoinElementTxn.cpp index 33eba6a1473..3d6c0d662f9 100644 --- a/mozilla/editor/base/JoinElementTxn.cpp +++ b/mozilla/editor/base/JoinElementTxn.cpp @@ -18,6 +18,8 @@ #include "JoinElementTxn.h" #include "nsIDOMNodeList.h" +#include "nsIDOMCharacterData.h" +#include "nsIDOMSelection.h" #include "nsIEditorSupport.h" static NS_DEFINE_IID(kIEditorSupportIID, NS_IEDITORSUPPORT_IID); @@ -66,12 +68,34 @@ NS_IMETHODIMP JoinElementTxn::Do(void) if ((NS_SUCCEEDED(result)) && (childNodes)) { childNodes->GetLength(&mOffset); } + else + { + nsCOMPtr leftNodeAsText; + leftNodeAsText = do_QueryInterface(mLeftNode); + if (leftNodeAsText) { + leftNodeAsText->GetLength(&mOffset); + } + } nsCOMPtr editor; result = mEditor->QueryInterface(kIEditorSupportIID, getter_AddRefs(editor)); if (NS_SUCCEEDED(result) && editor) { - result = editor->JoinNodesImpl(mLeftNode, mRightNode, mParent, PR_TRUE); + result = editor->JoinNodesImpl(mRightNode, mLeftNode, mParent, PR_FALSE); + if (NS_SUCCEEDED(result)) + { + nsCOMPtrselection; + mEditor->GetSelection(getter_AddRefs(selection)); + if (selection) + { + selection->Collapse(mRightNode, mOffset); + } + } } } + else + { + NS_ASSERTION(PR_FALSE, "2 nodes do not have same parent"); + return NS_ERROR_INVALID_ARG; + } } } } @@ -86,6 +110,15 @@ NS_IMETHODIMP JoinElementTxn::Undo(void) result = mEditor->QueryInterface(kIEditorSupportIID, getter_AddRefs(editor)); if (NS_SUCCEEDED(result) && editor) { result = editor->SplitNodeImpl(mRightNode, mOffset, mLeftNode, mParent); + if (NS_SUCCEEDED(result) && mLeftNode) + { + nsCOMPtrselection; + mEditor->GetSelection(getter_AddRefs(selection)); + if (selection) + { + selection->Collapse(mLeftNode, mOffset); + } + } } else { result = NS_ERROR_NOT_IMPLEMENTED; @@ -98,8 +131,18 @@ NS_IMETHODIMP JoinElementTxn::Redo(void) nsresult result; nsCOMPtr editor; result = mEditor->QueryInterface(kIEditorSupportIID, getter_AddRefs(editor)); - if (NS_SUCCEEDED(result) && editor) { - result = editor->JoinNodesImpl(mLeftNode, mRightNode, mParent, PR_TRUE); + if (NS_SUCCEEDED(result) && editor) + { + result = editor->JoinNodesImpl(mRightNode, mLeftNode, mParent, PR_FALSE); + if (NS_SUCCEEDED(result)) + { + nsCOMPtrselection; + mEditor->GetSelection(getter_AddRefs(selection)); + if (selection) + { + selection->Collapse(mRightNode, mOffset); + } + } } else { result = NS_ERROR_NOT_IMPLEMENTED; @@ -107,6 +150,7 @@ NS_IMETHODIMP JoinElementTxn::Redo(void) return result; } + NS_IMETHODIMP JoinElementTxn::GetIsTransient(PRBool *aIsTransient) { if (nsnull!=aIsTransient) diff --git a/mozilla/editor/base/SplitElementTxn.cpp b/mozilla/editor/base/SplitElementTxn.cpp index 3143de2472d..bfb5f09f188 100644 --- a/mozilla/editor/base/SplitElementTxn.cpp +++ b/mozilla/editor/base/SplitElementTxn.cpp @@ -18,7 +18,7 @@ #include "SplitElementTxn.h" #include "nsIDOMNode.h" -#include "nsIDOMElement.h" +#include "nsIDOMSelection.h" #include "nsIEditorSupport.h" static NS_DEFINE_IID(kIEditorSupportIID, NS_IEDITORSUPPORT_IID); @@ -30,8 +30,8 @@ SplitElementTxn::SplitElementTxn() } NS_IMETHODIMP SplitElementTxn::Init(nsIEditor *aEditor, - nsIDOMNode *aNode, - PRInt32 aOffset) + nsIDOMNode *aNode, + PRInt32 aOffset) { mEditor = do_QueryInterface(aEditor); mExistingRightNode = do_QueryInterface(aNode); @@ -45,6 +45,10 @@ SplitElementTxn::~SplitElementTxn() NS_IMETHODIMP SplitElementTxn::Do(void) { + NS_ASSERTION(mExistingRightNode, "bad state"); + if (!mExistingRightNode) { + return NS_ERROR_NOT_INITIALIZED; + } // create a new node nsresult result = mExistingRightNode->CloneNode(PR_FALSE, getter_AddRefs(mNewLeftNode)); NS_ASSERTION(((NS_SUCCEEDED(result)) && (mNewLeftNode)), "could not create element."); @@ -58,8 +62,18 @@ NS_IMETHODIMP SplitElementTxn::Do(void) { nsCOMPtr editor; result = mEditor->QueryInterface(kIEditorSupportIID, getter_AddRefs(editor)); - if (NS_SUCCEEDED(result) && editor) { + if (NS_SUCCEEDED(result) && editor) + { result = editor->SplitNodeImpl(mExistingRightNode, mOffset, mNewLeftNode, mParent); + if (NS_SUCCEEDED(result) && mNewLeftNode) + { + nsCOMPtrselection; + mEditor->GetSelection(getter_AddRefs(selection)); + if (selection) + { + selection->Collapse(mNewLeftNode, mOffset); + } + } } else { result = NS_ERROR_NOT_IMPLEMENTED; @@ -71,12 +85,42 @@ NS_IMETHODIMP SplitElementTxn::Do(void) NS_IMETHODIMP SplitElementTxn::Undo(void) { + NS_ASSERTION(mExistingRightNode && mNewLeftNode && mParent, "bad state"); + if (!mExistingRightNode || !mNewLeftNode || !mParent) { + return NS_ERROR_NOT_INITIALIZED; + } + +#ifdef NS_DEBUG + // sanity check + nsCOMPtrparent; + nsresult debugResult = mExistingRightNode->GetParentNode(getter_AddRefs(parent)); + NS_ASSERTION((NS_SUCCEEDED(debugResult)), "bad GetParentNode result for right child"); + NS_ASSERTION(parent, "bad GetParentNode for right child"); + NS_ASSERTION(parent==mParent, "bad GetParentNode for right child, parents don't match"); + + debugResult = mNewLeftNode->GetParentNode(getter_AddRefs(parent)); + NS_ASSERTION((NS_SUCCEEDED(debugResult)), "bad GetParentNode result for left child"); + NS_ASSERTION(parent, "bad GetParentNode for left child"); + NS_ASSERTION(parent==mParent, "bad GetParentNode for right child, left don't match"); + +#endif + // this assumes Do inserted the new node in front of the prior existing node nsresult result; nsCOMPtr editor; result = mEditor->QueryInterface(kIEditorSupportIID, getter_AddRefs(editor)); - if (NS_SUCCEEDED(result) && editor) { + if (NS_SUCCEEDED(result) && editor) + { result = editor->JoinNodesImpl(mExistingRightNode, mNewLeftNode, mParent, PR_FALSE); + if (NS_SUCCEEDED(result) && mNewLeftNode) + { + nsCOMPtrselection; + mEditor->GetSelection(getter_AddRefs(selection)); + if (selection) + { + selection->Collapse(mExistingRightNode, mOffset); + } + } } else { result = NS_ERROR_NOT_IMPLEMENTED; @@ -84,8 +128,13 @@ NS_IMETHODIMP SplitElementTxn::Undo(void) return result; } +/* NS_IMETHODIMP SplitElementTxn::Redo(void) { + NS_ASSERTION(mExistingRightNode && mNewLeftNode && mParent, "bad state"); + if (!mExistingRightNode || !mNewLeftNode || !mParent) { + return NS_ERROR_NOT_INITIALIZED; + } nsresult result; nsCOMPtr editor; result = mEditor->QueryInterface(kIEditorSupportIID, getter_AddRefs(editor)); @@ -97,6 +146,7 @@ NS_IMETHODIMP SplitElementTxn::Redo(void) } return result; } +*/ NS_IMETHODIMP SplitElementTxn::Merge(PRBool *aDidMerge, nsITransaction *aTransaction) { diff --git a/mozilla/editor/base/SplitElementTxn.h b/mozilla/editor/base/SplitElementTxn.h index a2c399bcc58..3d39e6f326c 100644 --- a/mozilla/editor/base/SplitElementTxn.h +++ b/mozilla/editor/base/SplitElementTxn.h @@ -57,7 +57,7 @@ public: NS_IMETHOD Undo(void); - NS_IMETHOD Redo(void); + //NS_IMETHOD Redo(void); NS_IMETHOD Merge(PRBool *aDidMerge, nsITransaction *aTransaction); diff --git a/mozilla/editor/libeditor/base/JoinElementTxn.cpp b/mozilla/editor/libeditor/base/JoinElementTxn.cpp index 33eba6a1473..3d6c0d662f9 100644 --- a/mozilla/editor/libeditor/base/JoinElementTxn.cpp +++ b/mozilla/editor/libeditor/base/JoinElementTxn.cpp @@ -18,6 +18,8 @@ #include "JoinElementTxn.h" #include "nsIDOMNodeList.h" +#include "nsIDOMCharacterData.h" +#include "nsIDOMSelection.h" #include "nsIEditorSupport.h" static NS_DEFINE_IID(kIEditorSupportIID, NS_IEDITORSUPPORT_IID); @@ -66,12 +68,34 @@ NS_IMETHODIMP JoinElementTxn::Do(void) if ((NS_SUCCEEDED(result)) && (childNodes)) { childNodes->GetLength(&mOffset); } + else + { + nsCOMPtr leftNodeAsText; + leftNodeAsText = do_QueryInterface(mLeftNode); + if (leftNodeAsText) { + leftNodeAsText->GetLength(&mOffset); + } + } nsCOMPtr editor; result = mEditor->QueryInterface(kIEditorSupportIID, getter_AddRefs(editor)); if (NS_SUCCEEDED(result) && editor) { - result = editor->JoinNodesImpl(mLeftNode, mRightNode, mParent, PR_TRUE); + result = editor->JoinNodesImpl(mRightNode, mLeftNode, mParent, PR_FALSE); + if (NS_SUCCEEDED(result)) + { + nsCOMPtrselection; + mEditor->GetSelection(getter_AddRefs(selection)); + if (selection) + { + selection->Collapse(mRightNode, mOffset); + } + } } } + else + { + NS_ASSERTION(PR_FALSE, "2 nodes do not have same parent"); + return NS_ERROR_INVALID_ARG; + } } } } @@ -86,6 +110,15 @@ NS_IMETHODIMP JoinElementTxn::Undo(void) result = mEditor->QueryInterface(kIEditorSupportIID, getter_AddRefs(editor)); if (NS_SUCCEEDED(result) && editor) { result = editor->SplitNodeImpl(mRightNode, mOffset, mLeftNode, mParent); + if (NS_SUCCEEDED(result) && mLeftNode) + { + nsCOMPtrselection; + mEditor->GetSelection(getter_AddRefs(selection)); + if (selection) + { + selection->Collapse(mLeftNode, mOffset); + } + } } else { result = NS_ERROR_NOT_IMPLEMENTED; @@ -98,8 +131,18 @@ NS_IMETHODIMP JoinElementTxn::Redo(void) nsresult result; nsCOMPtr editor; result = mEditor->QueryInterface(kIEditorSupportIID, getter_AddRefs(editor)); - if (NS_SUCCEEDED(result) && editor) { - result = editor->JoinNodesImpl(mLeftNode, mRightNode, mParent, PR_TRUE); + if (NS_SUCCEEDED(result) && editor) + { + result = editor->JoinNodesImpl(mRightNode, mLeftNode, mParent, PR_FALSE); + if (NS_SUCCEEDED(result)) + { + nsCOMPtrselection; + mEditor->GetSelection(getter_AddRefs(selection)); + if (selection) + { + selection->Collapse(mRightNode, mOffset); + } + } } else { result = NS_ERROR_NOT_IMPLEMENTED; @@ -107,6 +150,7 @@ NS_IMETHODIMP JoinElementTxn::Redo(void) return result; } + NS_IMETHODIMP JoinElementTxn::GetIsTransient(PRBool *aIsTransient) { if (nsnull!=aIsTransient) diff --git a/mozilla/editor/libeditor/base/SplitElementTxn.cpp b/mozilla/editor/libeditor/base/SplitElementTxn.cpp index 3143de2472d..bfb5f09f188 100644 --- a/mozilla/editor/libeditor/base/SplitElementTxn.cpp +++ b/mozilla/editor/libeditor/base/SplitElementTxn.cpp @@ -18,7 +18,7 @@ #include "SplitElementTxn.h" #include "nsIDOMNode.h" -#include "nsIDOMElement.h" +#include "nsIDOMSelection.h" #include "nsIEditorSupport.h" static NS_DEFINE_IID(kIEditorSupportIID, NS_IEDITORSUPPORT_IID); @@ -30,8 +30,8 @@ SplitElementTxn::SplitElementTxn() } NS_IMETHODIMP SplitElementTxn::Init(nsIEditor *aEditor, - nsIDOMNode *aNode, - PRInt32 aOffset) + nsIDOMNode *aNode, + PRInt32 aOffset) { mEditor = do_QueryInterface(aEditor); mExistingRightNode = do_QueryInterface(aNode); @@ -45,6 +45,10 @@ SplitElementTxn::~SplitElementTxn() NS_IMETHODIMP SplitElementTxn::Do(void) { + NS_ASSERTION(mExistingRightNode, "bad state"); + if (!mExistingRightNode) { + return NS_ERROR_NOT_INITIALIZED; + } // create a new node nsresult result = mExistingRightNode->CloneNode(PR_FALSE, getter_AddRefs(mNewLeftNode)); NS_ASSERTION(((NS_SUCCEEDED(result)) && (mNewLeftNode)), "could not create element."); @@ -58,8 +62,18 @@ NS_IMETHODIMP SplitElementTxn::Do(void) { nsCOMPtr editor; result = mEditor->QueryInterface(kIEditorSupportIID, getter_AddRefs(editor)); - if (NS_SUCCEEDED(result) && editor) { + if (NS_SUCCEEDED(result) && editor) + { result = editor->SplitNodeImpl(mExistingRightNode, mOffset, mNewLeftNode, mParent); + if (NS_SUCCEEDED(result) && mNewLeftNode) + { + nsCOMPtrselection; + mEditor->GetSelection(getter_AddRefs(selection)); + if (selection) + { + selection->Collapse(mNewLeftNode, mOffset); + } + } } else { result = NS_ERROR_NOT_IMPLEMENTED; @@ -71,12 +85,42 @@ NS_IMETHODIMP SplitElementTxn::Do(void) NS_IMETHODIMP SplitElementTxn::Undo(void) { + NS_ASSERTION(mExistingRightNode && mNewLeftNode && mParent, "bad state"); + if (!mExistingRightNode || !mNewLeftNode || !mParent) { + return NS_ERROR_NOT_INITIALIZED; + } + +#ifdef NS_DEBUG + // sanity check + nsCOMPtrparent; + nsresult debugResult = mExistingRightNode->GetParentNode(getter_AddRefs(parent)); + NS_ASSERTION((NS_SUCCEEDED(debugResult)), "bad GetParentNode result for right child"); + NS_ASSERTION(parent, "bad GetParentNode for right child"); + NS_ASSERTION(parent==mParent, "bad GetParentNode for right child, parents don't match"); + + debugResult = mNewLeftNode->GetParentNode(getter_AddRefs(parent)); + NS_ASSERTION((NS_SUCCEEDED(debugResult)), "bad GetParentNode result for left child"); + NS_ASSERTION(parent, "bad GetParentNode for left child"); + NS_ASSERTION(parent==mParent, "bad GetParentNode for right child, left don't match"); + +#endif + // this assumes Do inserted the new node in front of the prior existing node nsresult result; nsCOMPtr editor; result = mEditor->QueryInterface(kIEditorSupportIID, getter_AddRefs(editor)); - if (NS_SUCCEEDED(result) && editor) { + if (NS_SUCCEEDED(result) && editor) + { result = editor->JoinNodesImpl(mExistingRightNode, mNewLeftNode, mParent, PR_FALSE); + if (NS_SUCCEEDED(result) && mNewLeftNode) + { + nsCOMPtrselection; + mEditor->GetSelection(getter_AddRefs(selection)); + if (selection) + { + selection->Collapse(mExistingRightNode, mOffset); + } + } } else { result = NS_ERROR_NOT_IMPLEMENTED; @@ -84,8 +128,13 @@ NS_IMETHODIMP SplitElementTxn::Undo(void) return result; } +/* NS_IMETHODIMP SplitElementTxn::Redo(void) { + NS_ASSERTION(mExistingRightNode && mNewLeftNode && mParent, "bad state"); + if (!mExistingRightNode || !mNewLeftNode || !mParent) { + return NS_ERROR_NOT_INITIALIZED; + } nsresult result; nsCOMPtr editor; result = mEditor->QueryInterface(kIEditorSupportIID, getter_AddRefs(editor)); @@ -97,6 +146,7 @@ NS_IMETHODIMP SplitElementTxn::Redo(void) } return result; } +*/ NS_IMETHODIMP SplitElementTxn::Merge(PRBool *aDidMerge, nsITransaction *aTransaction) { diff --git a/mozilla/editor/libeditor/base/SplitElementTxn.h b/mozilla/editor/libeditor/base/SplitElementTxn.h index a2c399bcc58..3d39e6f326c 100644 --- a/mozilla/editor/libeditor/base/SplitElementTxn.h +++ b/mozilla/editor/libeditor/base/SplitElementTxn.h @@ -57,7 +57,7 @@ public: NS_IMETHOD Undo(void); - NS_IMETHOD Redo(void); + //NS_IMETHOD Redo(void); NS_IMETHOD Merge(PRBool *aDidMerge, nsITransaction *aTransaction);