diff --git a/editor/base/JoinElementTxn.cpp b/editor/base/JoinElementTxn.cpp index 33eba6a14739..3d6c0d662f9b 100644 --- a/editor/base/JoinElementTxn.cpp +++ b/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/editor/base/SplitElementTxn.cpp b/editor/base/SplitElementTxn.cpp index 3143de2472d6..bfb5f09f188a 100644 --- a/editor/base/SplitElementTxn.cpp +++ b/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/editor/base/SplitElementTxn.h b/editor/base/SplitElementTxn.h index a2c399bcc588..3d39e6f326c5 100644 --- a/editor/base/SplitElementTxn.h +++ b/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/editor/libeditor/base/JoinElementTxn.cpp b/editor/libeditor/base/JoinElementTxn.cpp index 33eba6a14739..3d6c0d662f9b 100644 --- a/editor/libeditor/base/JoinElementTxn.cpp +++ b/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/editor/libeditor/base/SplitElementTxn.cpp b/editor/libeditor/base/SplitElementTxn.cpp index 3143de2472d6..bfb5f09f188a 100644 --- a/editor/libeditor/base/SplitElementTxn.cpp +++ b/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/editor/libeditor/base/SplitElementTxn.h b/editor/libeditor/base/SplitElementTxn.h index a2c399bcc588..3d39e6f326c5 100644 --- a/editor/libeditor/base/SplitElementTxn.h +++ b/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);