From 8a19fb26bef6da3964ce2ef9e0356eac03a5985c Mon Sep 17 00:00:00 2001 From: "buster%netscape.com" Date: Mon, 11 Jan 1999 22:47:23 +0000 Subject: [PATCH] shuffled around the responsibilities of core editing objects. The key listener is now mostly a mapper of key events to editor actions. Soon, this should be 100% true. The editor builds transactions that are themselves intelligent. The editor makes very few DOM calls itself. It relies on the transactions to do most of the actual editing. git-svn-id: svn://10.0.0.236/trunk@17518 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/editor/base/ChangeAttributeTxn.cpp | 17 +- mozilla/editor/base/CreateElementTxn.cpp | 2 +- mozilla/editor/base/CreateElementTxn.h | 2 +- mozilla/editor/base/EditTxn.cpp | 105 ---------- mozilla/editor/base/EditTxn.h | 13 -- mozilla/editor/base/SplitElementTxn.cpp | 7 +- mozilla/editor/base/editor.cpp | 182 +++++++++++++++++- mozilla/editor/base/editor.h | 26 +++ mozilla/editor/base/editorInterfaces.cpp | 44 ++--- mozilla/editor/core/ChangeAttributeTxn.cpp | 17 +- mozilla/editor/core/CreateElementTxn.cpp | 2 +- mozilla/editor/core/CreateElementTxn.h | 2 +- mozilla/editor/core/EditTxn.cpp | 105 ---------- mozilla/editor/core/EditTxn.h | 13 -- mozilla/editor/core/SplitElementTxn.cpp | 7 +- mozilla/editor/core/editor.cpp | 182 +++++++++++++++++- mozilla/editor/core/editor.h | 26 +++ mozilla/editor/core/editorInterfaces.cpp | 44 ++--- .../libeditor/base/ChangeAttributeTxn.cpp | 17 +- .../libeditor/base/CreateElementTxn.cpp | 2 +- .../editor/libeditor/base/CreateElementTxn.h | 2 +- mozilla/editor/libeditor/base/EditTxn.cpp | 105 ---------- mozilla/editor/libeditor/base/EditTxn.h | 13 -- .../editor/libeditor/base/SplitElementTxn.cpp | 7 +- 24 files changed, 470 insertions(+), 472 deletions(-) diff --git a/mozilla/editor/base/ChangeAttributeTxn.cpp b/mozilla/editor/base/ChangeAttributeTxn.cpp index c727ca73e36..9ee4b42a380 100644 --- a/mozilla/editor/base/ChangeAttributeTxn.cpp +++ b/mozilla/editor/base/ChangeAttributeTxn.cpp @@ -18,6 +18,7 @@ #include "ChangeAttributeTxn.h" #include "editor.h" +#include "nsIDOMElement.h" // note that aEditor is not refcounted ChangeAttributeTxn::ChangeAttributeTxn(nsEditor *aEditor, @@ -54,9 +55,9 @@ nsresult ChangeAttributeTxn::Do(void) // now set the attribute to the new value if (PR_FALSE==mRemoveAttribute) - result = mEditor->SetAttribute(mElement, mAttribute, mValue); + result = mElement->SetDOMAttribute(mAttribute, mValue); else - result = mEditor->RemoveAttribute(mElement, mAttribute); + result = mElement->RemoveAttribute(mAttribute); return result; } @@ -65,13 +66,9 @@ nsresult ChangeAttributeTxn::Undo(void) { nsresult result=NS_OK; if (PR_TRUE==mAttributeWasSet) - { - result = mEditor->SetAttribute(mElement, mAttribute, mUndoValue); - } + result = mElement->SetDOMAttribute(mAttribute, mUndoValue); else - { - result = mEditor->RemoveAttribute(mElement, mAttribute); - } + result = mElement->RemoveAttribute(mAttribute); return result; } @@ -81,9 +78,9 @@ nsresult ChangeAttributeTxn::Redo(void) nsresult result; if (PR_FALSE==mRemoveAttribute) - result = mEditor->SetAttribute(mElement, mAttribute, mValue); + result = mElement->SetDOMAttribute(mAttribute, mValue); else - result = mEditor->RemoveAttribute(mElement, mAttribute); + result = mElement->RemoveAttribute(mAttribute); return result; } diff --git a/mozilla/editor/base/CreateElementTxn.cpp b/mozilla/editor/base/CreateElementTxn.cpp index 17fbe253454..fd42eec3f9f 100644 --- a/mozilla/editor/base/CreateElementTxn.cpp +++ b/mozilla/editor/base/CreateElementTxn.cpp @@ -26,7 +26,7 @@ // note that aEditor is not refcounted CreateElementTxn::CreateElementTxn(nsEditor *aEditor, nsIDOMDocument *aDoc, - nsString& aTag, + const nsString& aTag, nsIDOMNode *aParent, PRUint32 aOffsetInParent) : EditTxn(aEditor) diff --git a/mozilla/editor/base/CreateElementTxn.h b/mozilla/editor/base/CreateElementTxn.h index d9a3b9f8bd9..269de9c3f5a 100644 --- a/mozilla/editor/base/CreateElementTxn.h +++ b/mozilla/editor/base/CreateElementTxn.h @@ -36,7 +36,7 @@ public: CreateElementTxn(nsEditor *aEditor, nsIDOMDocument *aDoc, - nsString& aTag, + const nsString& aTag, nsIDOMNode *aParent, PRUint32 aOffsetInParent); diff --git a/mozilla/editor/base/EditTxn.cpp b/mozilla/editor/base/EditTxn.cpp index a220a664dce..768bef6604e 100644 --- a/mozilla/editor/base/EditTxn.cpp +++ b/mozilla/editor/base/EditTxn.cpp @@ -101,109 +101,4 @@ EditTxn::QueryInterface(REFNSIID aIID, void** aInstancePtr) } -/* =============================== Static helper methods ================================== */ -nsresult -EditTxn::SplitNode(nsIDOMNode * aNode, - PRInt32 aOffset, - nsIDOMNode* aNewNode, - nsIDOMNode* aParent) -{ - nsresult result; - NS_ASSERTION(((nsnull!=aNode) && - (nsnull!=aNewNode) && - (nsnull!=aParent)), - "null arg"); - if ((nsnull!=aNode) && - (nsnull!=aNewNode) && - (nsnull!=aParent)) - { - nsCOMPtr resultNode; - result = aParent->InsertBefore(aNewNode, aNode, getter_AddRefs(resultNode)); - if (NS_SUCCEEDED(result)) - { - // split the children between the 2 nodes - // at this point, nNode has all the children - if (0<=aOffset) // don't bother unless we're going to move at least one child - { - nsCOMPtr childNodes; - result = aParent->GetChildNodes(getter_AddRefs(childNodes)); - if ((NS_SUCCEEDED(result)) && (childNodes)) - { - PRInt32 i=0; - for ( ; ((NS_SUCCEEDED(result)) && (i childNode; - result = childNodes->Item(i, getter_AddRefs(childNode)); - if ((NS_SUCCEEDED(result)) && (childNode)) - { - result = aNode->RemoveChild(childNode, getter_AddRefs(resultNode)); - if (NS_SUCCEEDED(result)) - { - result = aNewNode->AppendChild(childNode, getter_AddRefs(resultNode)); - } - } - } - } - } - } - else result = NS_ERROR_NULL_POINTER; - } - return result; -} - -nsresult -EditTxn::JoinNodes(nsIDOMNode * aNodeToKeep, - nsIDOMNode * aNodeToJoin, - nsIDOMNode * aParent, - PRBool aNodeToKeepIsFirst) -{ - nsresult result; - NS_ASSERTION(((nsnull!=aNodeToKeep) && - (nsnull!=aNodeToJoin) && - (nsnull!=aParent)), - "null arg"); - if ((nsnull!=aNodeToKeep) && - (nsnull!=aNodeToJoin) && - (nsnull!=aParent)) - { - nsCOMPtr childNodes; - result = aNodeToJoin->GetChildNodes(getter_AddRefs(childNodes)); - if ((NS_SUCCEEDED(result)) && (childNodes)) - { - PRUint32 i; - PRUint32 childCount=0; - childNodes->GetLength(&childCount); - nsCOMPtr firstNode; //only used if aNodeToKeepIsFirst is false - if (PR_FALSE==aNodeToKeepIsFirst) - { // remember the first child in aNodeToKeep, we'll insert all the children of aNodeToJoin in front of it - result = aNodeToKeep->GetFirstChild(getter_AddRefs(firstNode)); - // GetFirstChild returns nsnull firstNode if aNodeToKeep has no children, that's ok. - } - nsCOMPtr resultNode; - for (i=0; ((NS_SUCCEEDED(result)) && (i childNode; - result = childNodes->Item(i, getter_AddRefs(childNode)); - if ((NS_SUCCEEDED(result)) && (childNode)) - { - if (PR_TRUE==aNodeToKeepIsFirst) - { // append children of aNodeToJoin - result = aNodeToKeep->AppendChild(childNode, getter_AddRefs(resultNode)); - } - else - { // prepend children of aNodeToJoin - result = aNodeToKeep->InsertBefore(childNode, firstNode, getter_AddRefs(resultNode)); - } - } - } - if (NS_SUCCEEDED(result)) - { // delete the extra node - result = aParent->RemoveChild(aNodeToJoin, getter_AddRefs(resultNode)); - } - } - } - else - result = NS_ERROR_NULL_POINTER; - return result; -} \ No newline at end of file diff --git a/mozilla/editor/base/EditTxn.h b/mozilla/editor/base/EditTxn.h index 83b3b96d2e6..afd99fbed46 100644 --- a/mozilla/editor/base/EditTxn.h +++ b/mozilla/editor/base/EditTxn.h @@ -52,19 +52,6 @@ public: virtual nsresult GetRedoString(nsString **aString); - // helper static methods. maybe move these into editor class. - - /** */ - static nsresult SplitNode(nsIDOMNode * aNode, - PRInt32 aOffset, - nsIDOMNode * aNewNode, - nsIDOMNode * aParent); - - static nsresult JoinNodes(nsIDOMNode * aNodeToKeep, - nsIDOMNode * aNodeToJoin, - nsIDOMNode * aParent, - PRBool aNodeToKeepIsFirst); - protected: nsEditor *mEditor; diff --git a/mozilla/editor/base/SplitElementTxn.cpp b/mozilla/editor/base/SplitElementTxn.cpp index ee040d801dc..26523978d22 100644 --- a/mozilla/editor/base/SplitElementTxn.cpp +++ b/mozilla/editor/base/SplitElementTxn.cpp @@ -19,7 +19,6 @@ #include "SplitElementTxn.h" #include "editor.h" #include "nsIDOMNode.h" -#include "nsIDOMNodeList.h" #include "nsIDOMElement.h" // note that aEditor is not refcounted @@ -55,7 +54,7 @@ nsresult SplitElementTxn::Do(void) // insert the new node if ((NS_SUCCEEDED(result)) && (nsnull!=mParent)) { - result = EditTxn::SplitNode(mNode, mOffset, mNewNode, mParent); + result = mEditor->SplitNode(mNode, mOffset, mNewNode, mParent); } } return result; @@ -64,13 +63,13 @@ nsresult SplitElementTxn::Do(void) nsresult SplitElementTxn::Undo(void) { // this assumes Do inserted the new node in front of the prior existing node - nsresult result = EditTxn::JoinNodes(mNode, mNewNode, mParent, PR_FALSE); + nsresult result = mEditor->JoinNodes(mNode, mNewNode, mParent, PR_FALSE); return result; } nsresult SplitElementTxn::Redo(void) { - nsresult result = EditTxn::SplitNode(mNode, mOffset, mNewNode, mParent); + nsresult result = mEditor->SplitNode(mNode, mOffset, mNewNode, mParent); return result; } diff --git a/mozilla/editor/base/editor.cpp b/mozilla/editor/base/editor.cpp index e646ef1cb36..5402e223188 100644 --- a/mozilla/editor/base/editor.cpp +++ b/mozilla/editor/base/editor.cpp @@ -23,6 +23,8 @@ #include "nsIDOMText.h" #include "nsIDOMElement.h" #include "nsIDOMAttr.h" +#include "nsIDOMNode.h" +#include "nsIDOMNodeList.h" #include "nsIDocument.h" #include "nsRepository.h" #include "nsIServiceManager.h" @@ -32,6 +34,12 @@ #include "nsITransactionManager.h" #include "nsIAtom.h" +// transactions the editor knows how to build +#include "ChangeAttributeTxn.h" +#include "CreateElementTxn.h" +#include "InsertTextTxn.h" +#include "DeleteTextTxn.h" + static NS_DEFINE_IID(kIDOMEventReceiverIID, NS_IDOMEVENTRECEIVER_IID); static NS_DEFINE_IID(kIDOMMouseListenerIID, NS_IDOMMOUSELISTENER_IID); @@ -307,9 +315,17 @@ nsEditor::GetProperties(PROPERTIES **) nsresult nsEditor::SetAttribute(nsIDOMElement *aElement, const nsString& aAttribute, const nsString& aValue) { - return aElement->SetDOMAttribute(aAttribute, aValue); + nsresult result; + if (nsnull != aElement) + { + ChangeAttributeTxn *txn = new ChangeAttributeTxn(this, aElement, aAttribute, aValue, PR_FALSE); + if (nsnull!=txn) + result = ExecuteTransaction(txn); + } + return result; } + nsresult nsEditor::GetAttributeValue(nsIDOMElement *aElement, const nsString& aAttribute, @@ -334,11 +350,17 @@ nsEditor::GetAttributeValue(nsIDOMElement *aElement, nsresult nsEditor::RemoveAttribute(nsIDOMElement *aElement, const nsString& aAttribute) { - return aElement->RemoveAttribute(aAttribute); + nsresult result; + if (nsnull != aElement) + { + nsString value; + ChangeAttributeTxn *txn = new ChangeAttributeTxn(this, aElement, aAttribute, value, PR_TRUE); + if (nsnull!=txn) + result = ExecuteTransaction(txn); + } + return result; } - - nsresult nsEditor::Commit(PRBool aCtrlKey) { @@ -560,6 +582,158 @@ nsEditor::Redo() return result; } +nsresult nsEditor::Delete(PRBool aForward, PRUint32 aCount) +{ + return NS_OK; +} + +nsresult nsEditor::CreateElement(const nsString& aTag, + nsIDOMNode * aParent, + PRInt32 aPosition) +{ + nsresult result; + if (nsnull != aParent) + { + CreateElementTxn *txn = new CreateElementTxn(this, mDomInterfaceP, aTag, aParent, aPosition); + if (nsnull!=txn) + result = ExecuteTransaction(txn); + } + return result; +} + +nsresult nsEditor::InsertText(nsIDOMCharacterData *aElement, + PRUint32 aOffset, + const nsString& aStringToInsert) +{ + nsresult result; + if (nsnull != aElement) + { + InsertTextTxn *txn = new InsertTextTxn(this, aElement, aOffset, aStringToInsert); + if (nsnull!=txn) + result = ExecuteTransaction(txn); + } + return result; +} + +nsresult nsEditor::DeleteText(nsIDOMCharacterData *aElement, + PRUint32 aOffset, + PRUint32 aLength) +{ + nsresult result; + if (nsnull != aElement) + { + DeleteTextTxn *txn = new DeleteTextTxn(this, aElement, aOffset, aLength); + if (nsnull!=txn) + result = ExecuteTransaction(txn); + } + return result; +} + +nsresult +nsEditor::SplitNode(nsIDOMNode * aNode, + PRInt32 aOffset, + nsIDOMNode* aNewNode, + nsIDOMNode* aParent) +{ + nsresult result; + NS_ASSERTION(((nsnull!=aNode) && + (nsnull!=aNewNode) && + (nsnull!=aParent)), + "null arg"); + if ((nsnull!=aNode) && + (nsnull!=aNewNode) && + (nsnull!=aParent)) + { + nsCOMPtr resultNode; + result = aParent->InsertBefore(aNewNode, aNode, getter_AddRefs(resultNode)); + if (NS_SUCCEEDED(result)) + { + // split the children between the 2 nodes + // at this point, nNode has all the children + if (0<=aOffset) // don't bother unless we're going to move at least one child + { + nsCOMPtr childNodes; + result = aParent->GetChildNodes(getter_AddRefs(childNodes)); + if ((NS_SUCCEEDED(result)) && (childNodes)) + { + PRInt32 i=0; + for ( ; ((NS_SUCCEEDED(result)) && (i childNode; + result = childNodes->Item(i, getter_AddRefs(childNode)); + if ((NS_SUCCEEDED(result)) && (childNode)) + { + result = aNode->RemoveChild(childNode, getter_AddRefs(resultNode)); + if (NS_SUCCEEDED(result)) + { + result = aNewNode->AppendChild(childNode, getter_AddRefs(resultNode)); + } + } + } + } + } + } + else result = NS_ERROR_NULL_POINTER; + } + return result; +} + +nsresult +nsEditor::JoinNodes(nsIDOMNode * aNodeToKeep, + nsIDOMNode * aNodeToJoin, + nsIDOMNode * aParent, + PRBool aNodeToKeepIsFirst) +{ + nsresult result; + NS_ASSERTION(((nsnull!=aNodeToKeep) && + (nsnull!=aNodeToJoin) && + (nsnull!=aParent)), + "null arg"); + if ((nsnull!=aNodeToKeep) && + (nsnull!=aNodeToJoin) && + (nsnull!=aParent)) + { + nsCOMPtr childNodes; + result = aNodeToJoin->GetChildNodes(getter_AddRefs(childNodes)); + if ((NS_SUCCEEDED(result)) && (childNodes)) + { + PRUint32 i; + PRUint32 childCount=0; + childNodes->GetLength(&childCount); + nsCOMPtr firstNode; //only used if aNodeToKeepIsFirst is false + if (PR_FALSE==aNodeToKeepIsFirst) + { // remember the first child in aNodeToKeep, we'll insert all the children of aNodeToJoin in front of it + result = aNodeToKeep->GetFirstChild(getter_AddRefs(firstNode)); + // GetFirstChild returns nsnull firstNode if aNodeToKeep has no children, that's ok. + } + nsCOMPtr resultNode; + for (i=0; ((NS_SUCCEEDED(result)) && (i childNode; + result = childNodes->Item(i, getter_AddRefs(childNode)); + if ((NS_SUCCEEDED(result)) && (childNode)) + { + if (PR_TRUE==aNodeToKeepIsFirst) + { // append children of aNodeToJoin + result = aNodeToKeep->AppendChild(childNode, getter_AddRefs(resultNode)); + } + else + { // prepend children of aNodeToJoin + result = aNodeToKeep->InsertBefore(childNode, firstNode, getter_AddRefs(resultNode)); + } + } + } + if (NS_SUCCEEDED(result)) + { // delete the extra node + result = aParent->RemoveChild(aNodeToJoin, getter_AddRefs(resultNode)); + } + } + } + else + result = NS_ERROR_NULL_POINTER; + return result; +} + //END nsEditor Private methods diff --git a/mozilla/editor/base/editor.h b/mozilla/editor/base/editor.h index 1ebec7a593d..3195e9000ef 100644 --- a/mozilla/editor/base/editor.h +++ b/mozilla/editor/base/editor.h @@ -26,6 +26,8 @@ #include "nsITransactionManager.h" //#include "nsISelection.h" +class nsIDOMCharacterData; + //This is the monitor for the editor. PRMonitor *getEditorMonitor(); @@ -165,6 +167,30 @@ public: */ nsresult Redo(); + nsresult CreateElement(const nsString& aTag, + nsIDOMNode * aParent, + PRInt32 aPosition); + + nsresult InsertText(nsIDOMCharacterData *aElement, + PRUint32 aOffset, + const nsString& aStringToInsert); + + nsresult DeleteText(nsIDOMCharacterData *aElement, + PRUint32 aOffset, + PRUint32 aLength); + + static nsresult SplitNode(nsIDOMNode * aNode, + PRInt32 aOffset, + nsIDOMNode * aNewNode, + nsIDOMNode * aParent); + + static nsresult JoinNodes(nsIDOMNode * aNodeToKeep, + nsIDOMNode * aNodeToJoin, + nsIDOMNode * aParent, + PRBool aNodeToKeepIsFirst); + + nsresult Delete(PRBool aForward, PRUint32 aCount); + /*END private methods of nsEditor*/ }; diff --git a/mozilla/editor/base/editorInterfaces.cpp b/mozilla/editor/base/editorInterfaces.cpp index c3a037af361..eb0acceda1f 100644 --- a/mozilla/editor/base/editorInterfaces.cpp +++ b/mozilla/editor/base/editorInterfaces.cpp @@ -18,9 +18,6 @@ #include "editorInterfaces.h" #include "editor.h" -#include "ChangeAttributeTxn.h" -#include "InsertTextTxn.h" -#include "DeleteTextTxn.h" #include "CreateElementTxn.h" #include "SplitElementTxn.h" @@ -152,9 +149,7 @@ nsEditorKeyListener::KeyDown(nsIDOMEvent* aKeyEvent) // XXX: for now, just append the text PRUint32 offset; text->GetLength(&offset); - DeleteTextTxn *txn; - txn = new DeleteTextTxn(mEditor, text, offset-1, 1); - mEditor->ExecuteTransaction(txn); + nsresult result = mEditor->DeleteText(text, offset-1, 1); } } break; @@ -169,10 +164,7 @@ nsEditorKeyListener::KeyDown(nsIDOMEvent* aKeyEvent) NS_SUCCEEDED(mEditor->GetFirstTextNode(currentNode,getter_AddRefs(textNode))) && NS_SUCCEEDED(textNode->QueryInterface(kIDOMCharacterDataIID, getter_AddRefs(text)))) { - // XXX: for now, just append the text - DeleteTextTxn *txn; - txn = new DeleteTextTxn(mEditor, text, 0, 1); - mEditor->ExecuteTransaction(txn); + nsresult result = mEditor->DeleteText(text, 0, 1); } } break; @@ -202,9 +194,7 @@ nsEditorKeyListener::KeyDown(nsIDOMEvent* aKeyEvent) // XXX: for now, just append the text PRUint32 offset; text->GetLength(&offset); - InsertTextTxn *txn; - txn = new InsertTextTxn(mEditor, text, offset, key); - mEditor->ExecuteTransaction(txn); + mEditor->InsertText(text, offset, key); } } } @@ -279,7 +269,7 @@ nsEditorKeyListener::ProcessShortCutKeys(nsIDOMEvent* aKeyEvent, PRBool& aProces SplitElementTxn *txn; if (PR_FALSE==isShift) // split the element so there are 0 children in the first half txn = new SplitElementTxn(mEditor, currentNode, -1); - else // split the element so there are 0 children in the first half + else // split the element so there are 2 children in the first half txn = new SplitElementTxn(mEditor, currentNode, 1); mEditor->ExecuteTransaction(txn); } @@ -303,12 +293,11 @@ nsEditorKeyListener::ProcessShortCutKeys(nsIDOMEvent* aKeyEvent, PRBool& aProces { if (NS_SUCCEEDED(currentNode->QueryInterface(kIDOMElementIID, getter_AddRefs(element)))) { - ChangeAttributeTxn *txn; + nsresult result; if (PR_TRUE==ctrlKey) // remove the attribute - txn = new ChangeAttributeTxn(mEditor, element, attribute, value, PR_TRUE); + result = mEditor->RemoveAttribute(element, attribute); else // change the attribute - txn = new ChangeAttributeTxn(mEditor, element, attribute, value, PR_FALSE); - mEditor->ExecuteTransaction(txn); + result = mEditor->SetAttribute(element, attribute, value); } } } @@ -329,19 +318,12 @@ nsEditorKeyListener::ProcessShortCutKeys(nsIDOMEvent* aKeyEvent, PRBool& aProces result = mEditor->GetFirstNodeOfType(nsnull, bodyTag, getter_AddRefs(currentNode)); if (NS_SUCCEEDED(result)) { - nsIDOMDocument *doc=nsnull; - result = mEditor->GetDomInterface(&doc); - if (NS_SUCCEEDED(result)) - { - PRUint32 position=0; - if (PR_TRUE==ctrlKey) - position=CreateElementTxn::eAppend; - else - position=0; - CreateElementTxn *txn; - txn = new CreateElementTxn(mEditor, doc, imgTag, currentNode, position); - mEditor->ExecuteTransaction(txn); - } + PRInt32 position; + if (PR_TRUE==ctrlKey) + position=CreateElementTxn::eAppend; + else + position=0; + result = mEditor->CreateElement(imgTag, currentNode, position); } /* //for building a composite transaction... diff --git a/mozilla/editor/core/ChangeAttributeTxn.cpp b/mozilla/editor/core/ChangeAttributeTxn.cpp index c727ca73e36..9ee4b42a380 100644 --- a/mozilla/editor/core/ChangeAttributeTxn.cpp +++ b/mozilla/editor/core/ChangeAttributeTxn.cpp @@ -18,6 +18,7 @@ #include "ChangeAttributeTxn.h" #include "editor.h" +#include "nsIDOMElement.h" // note that aEditor is not refcounted ChangeAttributeTxn::ChangeAttributeTxn(nsEditor *aEditor, @@ -54,9 +55,9 @@ nsresult ChangeAttributeTxn::Do(void) // now set the attribute to the new value if (PR_FALSE==mRemoveAttribute) - result = mEditor->SetAttribute(mElement, mAttribute, mValue); + result = mElement->SetDOMAttribute(mAttribute, mValue); else - result = mEditor->RemoveAttribute(mElement, mAttribute); + result = mElement->RemoveAttribute(mAttribute); return result; } @@ -65,13 +66,9 @@ nsresult ChangeAttributeTxn::Undo(void) { nsresult result=NS_OK; if (PR_TRUE==mAttributeWasSet) - { - result = mEditor->SetAttribute(mElement, mAttribute, mUndoValue); - } + result = mElement->SetDOMAttribute(mAttribute, mUndoValue); else - { - result = mEditor->RemoveAttribute(mElement, mAttribute); - } + result = mElement->RemoveAttribute(mAttribute); return result; } @@ -81,9 +78,9 @@ nsresult ChangeAttributeTxn::Redo(void) nsresult result; if (PR_FALSE==mRemoveAttribute) - result = mEditor->SetAttribute(mElement, mAttribute, mValue); + result = mElement->SetDOMAttribute(mAttribute, mValue); else - result = mEditor->RemoveAttribute(mElement, mAttribute); + result = mElement->RemoveAttribute(mAttribute); return result; } diff --git a/mozilla/editor/core/CreateElementTxn.cpp b/mozilla/editor/core/CreateElementTxn.cpp index 17fbe253454..fd42eec3f9f 100644 --- a/mozilla/editor/core/CreateElementTxn.cpp +++ b/mozilla/editor/core/CreateElementTxn.cpp @@ -26,7 +26,7 @@ // note that aEditor is not refcounted CreateElementTxn::CreateElementTxn(nsEditor *aEditor, nsIDOMDocument *aDoc, - nsString& aTag, + const nsString& aTag, nsIDOMNode *aParent, PRUint32 aOffsetInParent) : EditTxn(aEditor) diff --git a/mozilla/editor/core/CreateElementTxn.h b/mozilla/editor/core/CreateElementTxn.h index d9a3b9f8bd9..269de9c3f5a 100644 --- a/mozilla/editor/core/CreateElementTxn.h +++ b/mozilla/editor/core/CreateElementTxn.h @@ -36,7 +36,7 @@ public: CreateElementTxn(nsEditor *aEditor, nsIDOMDocument *aDoc, - nsString& aTag, + const nsString& aTag, nsIDOMNode *aParent, PRUint32 aOffsetInParent); diff --git a/mozilla/editor/core/EditTxn.cpp b/mozilla/editor/core/EditTxn.cpp index a220a664dce..768bef6604e 100644 --- a/mozilla/editor/core/EditTxn.cpp +++ b/mozilla/editor/core/EditTxn.cpp @@ -101,109 +101,4 @@ EditTxn::QueryInterface(REFNSIID aIID, void** aInstancePtr) } -/* =============================== Static helper methods ================================== */ -nsresult -EditTxn::SplitNode(nsIDOMNode * aNode, - PRInt32 aOffset, - nsIDOMNode* aNewNode, - nsIDOMNode* aParent) -{ - nsresult result; - NS_ASSERTION(((nsnull!=aNode) && - (nsnull!=aNewNode) && - (nsnull!=aParent)), - "null arg"); - if ((nsnull!=aNode) && - (nsnull!=aNewNode) && - (nsnull!=aParent)) - { - nsCOMPtr resultNode; - result = aParent->InsertBefore(aNewNode, aNode, getter_AddRefs(resultNode)); - if (NS_SUCCEEDED(result)) - { - // split the children between the 2 nodes - // at this point, nNode has all the children - if (0<=aOffset) // don't bother unless we're going to move at least one child - { - nsCOMPtr childNodes; - result = aParent->GetChildNodes(getter_AddRefs(childNodes)); - if ((NS_SUCCEEDED(result)) && (childNodes)) - { - PRInt32 i=0; - for ( ; ((NS_SUCCEEDED(result)) && (i childNode; - result = childNodes->Item(i, getter_AddRefs(childNode)); - if ((NS_SUCCEEDED(result)) && (childNode)) - { - result = aNode->RemoveChild(childNode, getter_AddRefs(resultNode)); - if (NS_SUCCEEDED(result)) - { - result = aNewNode->AppendChild(childNode, getter_AddRefs(resultNode)); - } - } - } - } - } - } - else result = NS_ERROR_NULL_POINTER; - } - return result; -} - -nsresult -EditTxn::JoinNodes(nsIDOMNode * aNodeToKeep, - nsIDOMNode * aNodeToJoin, - nsIDOMNode * aParent, - PRBool aNodeToKeepIsFirst) -{ - nsresult result; - NS_ASSERTION(((nsnull!=aNodeToKeep) && - (nsnull!=aNodeToJoin) && - (nsnull!=aParent)), - "null arg"); - if ((nsnull!=aNodeToKeep) && - (nsnull!=aNodeToJoin) && - (nsnull!=aParent)) - { - nsCOMPtr childNodes; - result = aNodeToJoin->GetChildNodes(getter_AddRefs(childNodes)); - if ((NS_SUCCEEDED(result)) && (childNodes)) - { - PRUint32 i; - PRUint32 childCount=0; - childNodes->GetLength(&childCount); - nsCOMPtr firstNode; //only used if aNodeToKeepIsFirst is false - if (PR_FALSE==aNodeToKeepIsFirst) - { // remember the first child in aNodeToKeep, we'll insert all the children of aNodeToJoin in front of it - result = aNodeToKeep->GetFirstChild(getter_AddRefs(firstNode)); - // GetFirstChild returns nsnull firstNode if aNodeToKeep has no children, that's ok. - } - nsCOMPtr resultNode; - for (i=0; ((NS_SUCCEEDED(result)) && (i childNode; - result = childNodes->Item(i, getter_AddRefs(childNode)); - if ((NS_SUCCEEDED(result)) && (childNode)) - { - if (PR_TRUE==aNodeToKeepIsFirst) - { // append children of aNodeToJoin - result = aNodeToKeep->AppendChild(childNode, getter_AddRefs(resultNode)); - } - else - { // prepend children of aNodeToJoin - result = aNodeToKeep->InsertBefore(childNode, firstNode, getter_AddRefs(resultNode)); - } - } - } - if (NS_SUCCEEDED(result)) - { // delete the extra node - result = aParent->RemoveChild(aNodeToJoin, getter_AddRefs(resultNode)); - } - } - } - else - result = NS_ERROR_NULL_POINTER; - return result; -} \ No newline at end of file diff --git a/mozilla/editor/core/EditTxn.h b/mozilla/editor/core/EditTxn.h index 83b3b96d2e6..afd99fbed46 100644 --- a/mozilla/editor/core/EditTxn.h +++ b/mozilla/editor/core/EditTxn.h @@ -52,19 +52,6 @@ public: virtual nsresult GetRedoString(nsString **aString); - // helper static methods. maybe move these into editor class. - - /** */ - static nsresult SplitNode(nsIDOMNode * aNode, - PRInt32 aOffset, - nsIDOMNode * aNewNode, - nsIDOMNode * aParent); - - static nsresult JoinNodes(nsIDOMNode * aNodeToKeep, - nsIDOMNode * aNodeToJoin, - nsIDOMNode * aParent, - PRBool aNodeToKeepIsFirst); - protected: nsEditor *mEditor; diff --git a/mozilla/editor/core/SplitElementTxn.cpp b/mozilla/editor/core/SplitElementTxn.cpp index ee040d801dc..26523978d22 100644 --- a/mozilla/editor/core/SplitElementTxn.cpp +++ b/mozilla/editor/core/SplitElementTxn.cpp @@ -19,7 +19,6 @@ #include "SplitElementTxn.h" #include "editor.h" #include "nsIDOMNode.h" -#include "nsIDOMNodeList.h" #include "nsIDOMElement.h" // note that aEditor is not refcounted @@ -55,7 +54,7 @@ nsresult SplitElementTxn::Do(void) // insert the new node if ((NS_SUCCEEDED(result)) && (nsnull!=mParent)) { - result = EditTxn::SplitNode(mNode, mOffset, mNewNode, mParent); + result = mEditor->SplitNode(mNode, mOffset, mNewNode, mParent); } } return result; @@ -64,13 +63,13 @@ nsresult SplitElementTxn::Do(void) nsresult SplitElementTxn::Undo(void) { // this assumes Do inserted the new node in front of the prior existing node - nsresult result = EditTxn::JoinNodes(mNode, mNewNode, mParent, PR_FALSE); + nsresult result = mEditor->JoinNodes(mNode, mNewNode, mParent, PR_FALSE); return result; } nsresult SplitElementTxn::Redo(void) { - nsresult result = EditTxn::SplitNode(mNode, mOffset, mNewNode, mParent); + nsresult result = mEditor->SplitNode(mNode, mOffset, mNewNode, mParent); return result; } diff --git a/mozilla/editor/core/editor.cpp b/mozilla/editor/core/editor.cpp index e646ef1cb36..5402e223188 100644 --- a/mozilla/editor/core/editor.cpp +++ b/mozilla/editor/core/editor.cpp @@ -23,6 +23,8 @@ #include "nsIDOMText.h" #include "nsIDOMElement.h" #include "nsIDOMAttr.h" +#include "nsIDOMNode.h" +#include "nsIDOMNodeList.h" #include "nsIDocument.h" #include "nsRepository.h" #include "nsIServiceManager.h" @@ -32,6 +34,12 @@ #include "nsITransactionManager.h" #include "nsIAtom.h" +// transactions the editor knows how to build +#include "ChangeAttributeTxn.h" +#include "CreateElementTxn.h" +#include "InsertTextTxn.h" +#include "DeleteTextTxn.h" + static NS_DEFINE_IID(kIDOMEventReceiverIID, NS_IDOMEVENTRECEIVER_IID); static NS_DEFINE_IID(kIDOMMouseListenerIID, NS_IDOMMOUSELISTENER_IID); @@ -307,9 +315,17 @@ nsEditor::GetProperties(PROPERTIES **) nsresult nsEditor::SetAttribute(nsIDOMElement *aElement, const nsString& aAttribute, const nsString& aValue) { - return aElement->SetDOMAttribute(aAttribute, aValue); + nsresult result; + if (nsnull != aElement) + { + ChangeAttributeTxn *txn = new ChangeAttributeTxn(this, aElement, aAttribute, aValue, PR_FALSE); + if (nsnull!=txn) + result = ExecuteTransaction(txn); + } + return result; } + nsresult nsEditor::GetAttributeValue(nsIDOMElement *aElement, const nsString& aAttribute, @@ -334,11 +350,17 @@ nsEditor::GetAttributeValue(nsIDOMElement *aElement, nsresult nsEditor::RemoveAttribute(nsIDOMElement *aElement, const nsString& aAttribute) { - return aElement->RemoveAttribute(aAttribute); + nsresult result; + if (nsnull != aElement) + { + nsString value; + ChangeAttributeTxn *txn = new ChangeAttributeTxn(this, aElement, aAttribute, value, PR_TRUE); + if (nsnull!=txn) + result = ExecuteTransaction(txn); + } + return result; } - - nsresult nsEditor::Commit(PRBool aCtrlKey) { @@ -560,6 +582,158 @@ nsEditor::Redo() return result; } +nsresult nsEditor::Delete(PRBool aForward, PRUint32 aCount) +{ + return NS_OK; +} + +nsresult nsEditor::CreateElement(const nsString& aTag, + nsIDOMNode * aParent, + PRInt32 aPosition) +{ + nsresult result; + if (nsnull != aParent) + { + CreateElementTxn *txn = new CreateElementTxn(this, mDomInterfaceP, aTag, aParent, aPosition); + if (nsnull!=txn) + result = ExecuteTransaction(txn); + } + return result; +} + +nsresult nsEditor::InsertText(nsIDOMCharacterData *aElement, + PRUint32 aOffset, + const nsString& aStringToInsert) +{ + nsresult result; + if (nsnull != aElement) + { + InsertTextTxn *txn = new InsertTextTxn(this, aElement, aOffset, aStringToInsert); + if (nsnull!=txn) + result = ExecuteTransaction(txn); + } + return result; +} + +nsresult nsEditor::DeleteText(nsIDOMCharacterData *aElement, + PRUint32 aOffset, + PRUint32 aLength) +{ + nsresult result; + if (nsnull != aElement) + { + DeleteTextTxn *txn = new DeleteTextTxn(this, aElement, aOffset, aLength); + if (nsnull!=txn) + result = ExecuteTransaction(txn); + } + return result; +} + +nsresult +nsEditor::SplitNode(nsIDOMNode * aNode, + PRInt32 aOffset, + nsIDOMNode* aNewNode, + nsIDOMNode* aParent) +{ + nsresult result; + NS_ASSERTION(((nsnull!=aNode) && + (nsnull!=aNewNode) && + (nsnull!=aParent)), + "null arg"); + if ((nsnull!=aNode) && + (nsnull!=aNewNode) && + (nsnull!=aParent)) + { + nsCOMPtr resultNode; + result = aParent->InsertBefore(aNewNode, aNode, getter_AddRefs(resultNode)); + if (NS_SUCCEEDED(result)) + { + // split the children between the 2 nodes + // at this point, nNode has all the children + if (0<=aOffset) // don't bother unless we're going to move at least one child + { + nsCOMPtr childNodes; + result = aParent->GetChildNodes(getter_AddRefs(childNodes)); + if ((NS_SUCCEEDED(result)) && (childNodes)) + { + PRInt32 i=0; + for ( ; ((NS_SUCCEEDED(result)) && (i childNode; + result = childNodes->Item(i, getter_AddRefs(childNode)); + if ((NS_SUCCEEDED(result)) && (childNode)) + { + result = aNode->RemoveChild(childNode, getter_AddRefs(resultNode)); + if (NS_SUCCEEDED(result)) + { + result = aNewNode->AppendChild(childNode, getter_AddRefs(resultNode)); + } + } + } + } + } + } + else result = NS_ERROR_NULL_POINTER; + } + return result; +} + +nsresult +nsEditor::JoinNodes(nsIDOMNode * aNodeToKeep, + nsIDOMNode * aNodeToJoin, + nsIDOMNode * aParent, + PRBool aNodeToKeepIsFirst) +{ + nsresult result; + NS_ASSERTION(((nsnull!=aNodeToKeep) && + (nsnull!=aNodeToJoin) && + (nsnull!=aParent)), + "null arg"); + if ((nsnull!=aNodeToKeep) && + (nsnull!=aNodeToJoin) && + (nsnull!=aParent)) + { + nsCOMPtr childNodes; + result = aNodeToJoin->GetChildNodes(getter_AddRefs(childNodes)); + if ((NS_SUCCEEDED(result)) && (childNodes)) + { + PRUint32 i; + PRUint32 childCount=0; + childNodes->GetLength(&childCount); + nsCOMPtr firstNode; //only used if aNodeToKeepIsFirst is false + if (PR_FALSE==aNodeToKeepIsFirst) + { // remember the first child in aNodeToKeep, we'll insert all the children of aNodeToJoin in front of it + result = aNodeToKeep->GetFirstChild(getter_AddRefs(firstNode)); + // GetFirstChild returns nsnull firstNode if aNodeToKeep has no children, that's ok. + } + nsCOMPtr resultNode; + for (i=0; ((NS_SUCCEEDED(result)) && (i childNode; + result = childNodes->Item(i, getter_AddRefs(childNode)); + if ((NS_SUCCEEDED(result)) && (childNode)) + { + if (PR_TRUE==aNodeToKeepIsFirst) + { // append children of aNodeToJoin + result = aNodeToKeep->AppendChild(childNode, getter_AddRefs(resultNode)); + } + else + { // prepend children of aNodeToJoin + result = aNodeToKeep->InsertBefore(childNode, firstNode, getter_AddRefs(resultNode)); + } + } + } + if (NS_SUCCEEDED(result)) + { // delete the extra node + result = aParent->RemoveChild(aNodeToJoin, getter_AddRefs(resultNode)); + } + } + } + else + result = NS_ERROR_NULL_POINTER; + return result; +} + //END nsEditor Private methods diff --git a/mozilla/editor/core/editor.h b/mozilla/editor/core/editor.h index 1ebec7a593d..3195e9000ef 100644 --- a/mozilla/editor/core/editor.h +++ b/mozilla/editor/core/editor.h @@ -26,6 +26,8 @@ #include "nsITransactionManager.h" //#include "nsISelection.h" +class nsIDOMCharacterData; + //This is the monitor for the editor. PRMonitor *getEditorMonitor(); @@ -165,6 +167,30 @@ public: */ nsresult Redo(); + nsresult CreateElement(const nsString& aTag, + nsIDOMNode * aParent, + PRInt32 aPosition); + + nsresult InsertText(nsIDOMCharacterData *aElement, + PRUint32 aOffset, + const nsString& aStringToInsert); + + nsresult DeleteText(nsIDOMCharacterData *aElement, + PRUint32 aOffset, + PRUint32 aLength); + + static nsresult SplitNode(nsIDOMNode * aNode, + PRInt32 aOffset, + nsIDOMNode * aNewNode, + nsIDOMNode * aParent); + + static nsresult JoinNodes(nsIDOMNode * aNodeToKeep, + nsIDOMNode * aNodeToJoin, + nsIDOMNode * aParent, + PRBool aNodeToKeepIsFirst); + + nsresult Delete(PRBool aForward, PRUint32 aCount); + /*END private methods of nsEditor*/ }; diff --git a/mozilla/editor/core/editorInterfaces.cpp b/mozilla/editor/core/editorInterfaces.cpp index c3a037af361..eb0acceda1f 100644 --- a/mozilla/editor/core/editorInterfaces.cpp +++ b/mozilla/editor/core/editorInterfaces.cpp @@ -18,9 +18,6 @@ #include "editorInterfaces.h" #include "editor.h" -#include "ChangeAttributeTxn.h" -#include "InsertTextTxn.h" -#include "DeleteTextTxn.h" #include "CreateElementTxn.h" #include "SplitElementTxn.h" @@ -152,9 +149,7 @@ nsEditorKeyListener::KeyDown(nsIDOMEvent* aKeyEvent) // XXX: for now, just append the text PRUint32 offset; text->GetLength(&offset); - DeleteTextTxn *txn; - txn = new DeleteTextTxn(mEditor, text, offset-1, 1); - mEditor->ExecuteTransaction(txn); + nsresult result = mEditor->DeleteText(text, offset-1, 1); } } break; @@ -169,10 +164,7 @@ nsEditorKeyListener::KeyDown(nsIDOMEvent* aKeyEvent) NS_SUCCEEDED(mEditor->GetFirstTextNode(currentNode,getter_AddRefs(textNode))) && NS_SUCCEEDED(textNode->QueryInterface(kIDOMCharacterDataIID, getter_AddRefs(text)))) { - // XXX: for now, just append the text - DeleteTextTxn *txn; - txn = new DeleteTextTxn(mEditor, text, 0, 1); - mEditor->ExecuteTransaction(txn); + nsresult result = mEditor->DeleteText(text, 0, 1); } } break; @@ -202,9 +194,7 @@ nsEditorKeyListener::KeyDown(nsIDOMEvent* aKeyEvent) // XXX: for now, just append the text PRUint32 offset; text->GetLength(&offset); - InsertTextTxn *txn; - txn = new InsertTextTxn(mEditor, text, offset, key); - mEditor->ExecuteTransaction(txn); + mEditor->InsertText(text, offset, key); } } } @@ -279,7 +269,7 @@ nsEditorKeyListener::ProcessShortCutKeys(nsIDOMEvent* aKeyEvent, PRBool& aProces SplitElementTxn *txn; if (PR_FALSE==isShift) // split the element so there are 0 children in the first half txn = new SplitElementTxn(mEditor, currentNode, -1); - else // split the element so there are 0 children in the first half + else // split the element so there are 2 children in the first half txn = new SplitElementTxn(mEditor, currentNode, 1); mEditor->ExecuteTransaction(txn); } @@ -303,12 +293,11 @@ nsEditorKeyListener::ProcessShortCutKeys(nsIDOMEvent* aKeyEvent, PRBool& aProces { if (NS_SUCCEEDED(currentNode->QueryInterface(kIDOMElementIID, getter_AddRefs(element)))) { - ChangeAttributeTxn *txn; + nsresult result; if (PR_TRUE==ctrlKey) // remove the attribute - txn = new ChangeAttributeTxn(mEditor, element, attribute, value, PR_TRUE); + result = mEditor->RemoveAttribute(element, attribute); else // change the attribute - txn = new ChangeAttributeTxn(mEditor, element, attribute, value, PR_FALSE); - mEditor->ExecuteTransaction(txn); + result = mEditor->SetAttribute(element, attribute, value); } } } @@ -329,19 +318,12 @@ nsEditorKeyListener::ProcessShortCutKeys(nsIDOMEvent* aKeyEvent, PRBool& aProces result = mEditor->GetFirstNodeOfType(nsnull, bodyTag, getter_AddRefs(currentNode)); if (NS_SUCCEEDED(result)) { - nsIDOMDocument *doc=nsnull; - result = mEditor->GetDomInterface(&doc); - if (NS_SUCCEEDED(result)) - { - PRUint32 position=0; - if (PR_TRUE==ctrlKey) - position=CreateElementTxn::eAppend; - else - position=0; - CreateElementTxn *txn; - txn = new CreateElementTxn(mEditor, doc, imgTag, currentNode, position); - mEditor->ExecuteTransaction(txn); - } + PRInt32 position; + if (PR_TRUE==ctrlKey) + position=CreateElementTxn::eAppend; + else + position=0; + result = mEditor->CreateElement(imgTag, currentNode, position); } /* //for building a composite transaction... diff --git a/mozilla/editor/libeditor/base/ChangeAttributeTxn.cpp b/mozilla/editor/libeditor/base/ChangeAttributeTxn.cpp index c727ca73e36..9ee4b42a380 100644 --- a/mozilla/editor/libeditor/base/ChangeAttributeTxn.cpp +++ b/mozilla/editor/libeditor/base/ChangeAttributeTxn.cpp @@ -18,6 +18,7 @@ #include "ChangeAttributeTxn.h" #include "editor.h" +#include "nsIDOMElement.h" // note that aEditor is not refcounted ChangeAttributeTxn::ChangeAttributeTxn(nsEditor *aEditor, @@ -54,9 +55,9 @@ nsresult ChangeAttributeTxn::Do(void) // now set the attribute to the new value if (PR_FALSE==mRemoveAttribute) - result = mEditor->SetAttribute(mElement, mAttribute, mValue); + result = mElement->SetDOMAttribute(mAttribute, mValue); else - result = mEditor->RemoveAttribute(mElement, mAttribute); + result = mElement->RemoveAttribute(mAttribute); return result; } @@ -65,13 +66,9 @@ nsresult ChangeAttributeTxn::Undo(void) { nsresult result=NS_OK; if (PR_TRUE==mAttributeWasSet) - { - result = mEditor->SetAttribute(mElement, mAttribute, mUndoValue); - } + result = mElement->SetDOMAttribute(mAttribute, mUndoValue); else - { - result = mEditor->RemoveAttribute(mElement, mAttribute); - } + result = mElement->RemoveAttribute(mAttribute); return result; } @@ -81,9 +78,9 @@ nsresult ChangeAttributeTxn::Redo(void) nsresult result; if (PR_FALSE==mRemoveAttribute) - result = mEditor->SetAttribute(mElement, mAttribute, mValue); + result = mElement->SetDOMAttribute(mAttribute, mValue); else - result = mEditor->RemoveAttribute(mElement, mAttribute); + result = mElement->RemoveAttribute(mAttribute); return result; } diff --git a/mozilla/editor/libeditor/base/CreateElementTxn.cpp b/mozilla/editor/libeditor/base/CreateElementTxn.cpp index 17fbe253454..fd42eec3f9f 100644 --- a/mozilla/editor/libeditor/base/CreateElementTxn.cpp +++ b/mozilla/editor/libeditor/base/CreateElementTxn.cpp @@ -26,7 +26,7 @@ // note that aEditor is not refcounted CreateElementTxn::CreateElementTxn(nsEditor *aEditor, nsIDOMDocument *aDoc, - nsString& aTag, + const nsString& aTag, nsIDOMNode *aParent, PRUint32 aOffsetInParent) : EditTxn(aEditor) diff --git a/mozilla/editor/libeditor/base/CreateElementTxn.h b/mozilla/editor/libeditor/base/CreateElementTxn.h index d9a3b9f8bd9..269de9c3f5a 100644 --- a/mozilla/editor/libeditor/base/CreateElementTxn.h +++ b/mozilla/editor/libeditor/base/CreateElementTxn.h @@ -36,7 +36,7 @@ public: CreateElementTxn(nsEditor *aEditor, nsIDOMDocument *aDoc, - nsString& aTag, + const nsString& aTag, nsIDOMNode *aParent, PRUint32 aOffsetInParent); diff --git a/mozilla/editor/libeditor/base/EditTxn.cpp b/mozilla/editor/libeditor/base/EditTxn.cpp index a220a664dce..768bef6604e 100644 --- a/mozilla/editor/libeditor/base/EditTxn.cpp +++ b/mozilla/editor/libeditor/base/EditTxn.cpp @@ -101,109 +101,4 @@ EditTxn::QueryInterface(REFNSIID aIID, void** aInstancePtr) } -/* =============================== Static helper methods ================================== */ -nsresult -EditTxn::SplitNode(nsIDOMNode * aNode, - PRInt32 aOffset, - nsIDOMNode* aNewNode, - nsIDOMNode* aParent) -{ - nsresult result; - NS_ASSERTION(((nsnull!=aNode) && - (nsnull!=aNewNode) && - (nsnull!=aParent)), - "null arg"); - if ((nsnull!=aNode) && - (nsnull!=aNewNode) && - (nsnull!=aParent)) - { - nsCOMPtr resultNode; - result = aParent->InsertBefore(aNewNode, aNode, getter_AddRefs(resultNode)); - if (NS_SUCCEEDED(result)) - { - // split the children between the 2 nodes - // at this point, nNode has all the children - if (0<=aOffset) // don't bother unless we're going to move at least one child - { - nsCOMPtr childNodes; - result = aParent->GetChildNodes(getter_AddRefs(childNodes)); - if ((NS_SUCCEEDED(result)) && (childNodes)) - { - PRInt32 i=0; - for ( ; ((NS_SUCCEEDED(result)) && (i childNode; - result = childNodes->Item(i, getter_AddRefs(childNode)); - if ((NS_SUCCEEDED(result)) && (childNode)) - { - result = aNode->RemoveChild(childNode, getter_AddRefs(resultNode)); - if (NS_SUCCEEDED(result)) - { - result = aNewNode->AppendChild(childNode, getter_AddRefs(resultNode)); - } - } - } - } - } - } - else result = NS_ERROR_NULL_POINTER; - } - return result; -} - -nsresult -EditTxn::JoinNodes(nsIDOMNode * aNodeToKeep, - nsIDOMNode * aNodeToJoin, - nsIDOMNode * aParent, - PRBool aNodeToKeepIsFirst) -{ - nsresult result; - NS_ASSERTION(((nsnull!=aNodeToKeep) && - (nsnull!=aNodeToJoin) && - (nsnull!=aParent)), - "null arg"); - if ((nsnull!=aNodeToKeep) && - (nsnull!=aNodeToJoin) && - (nsnull!=aParent)) - { - nsCOMPtr childNodes; - result = aNodeToJoin->GetChildNodes(getter_AddRefs(childNodes)); - if ((NS_SUCCEEDED(result)) && (childNodes)) - { - PRUint32 i; - PRUint32 childCount=0; - childNodes->GetLength(&childCount); - nsCOMPtr firstNode; //only used if aNodeToKeepIsFirst is false - if (PR_FALSE==aNodeToKeepIsFirst) - { // remember the first child in aNodeToKeep, we'll insert all the children of aNodeToJoin in front of it - result = aNodeToKeep->GetFirstChild(getter_AddRefs(firstNode)); - // GetFirstChild returns nsnull firstNode if aNodeToKeep has no children, that's ok. - } - nsCOMPtr resultNode; - for (i=0; ((NS_SUCCEEDED(result)) && (i childNode; - result = childNodes->Item(i, getter_AddRefs(childNode)); - if ((NS_SUCCEEDED(result)) && (childNode)) - { - if (PR_TRUE==aNodeToKeepIsFirst) - { // append children of aNodeToJoin - result = aNodeToKeep->AppendChild(childNode, getter_AddRefs(resultNode)); - } - else - { // prepend children of aNodeToJoin - result = aNodeToKeep->InsertBefore(childNode, firstNode, getter_AddRefs(resultNode)); - } - } - } - if (NS_SUCCEEDED(result)) - { // delete the extra node - result = aParent->RemoveChild(aNodeToJoin, getter_AddRefs(resultNode)); - } - } - } - else - result = NS_ERROR_NULL_POINTER; - return result; -} \ No newline at end of file diff --git a/mozilla/editor/libeditor/base/EditTxn.h b/mozilla/editor/libeditor/base/EditTxn.h index 83b3b96d2e6..afd99fbed46 100644 --- a/mozilla/editor/libeditor/base/EditTxn.h +++ b/mozilla/editor/libeditor/base/EditTxn.h @@ -52,19 +52,6 @@ public: virtual nsresult GetRedoString(nsString **aString); - // helper static methods. maybe move these into editor class. - - /** */ - static nsresult SplitNode(nsIDOMNode * aNode, - PRInt32 aOffset, - nsIDOMNode * aNewNode, - nsIDOMNode * aParent); - - static nsresult JoinNodes(nsIDOMNode * aNodeToKeep, - nsIDOMNode * aNodeToJoin, - nsIDOMNode * aParent, - PRBool aNodeToKeepIsFirst); - protected: nsEditor *mEditor; diff --git a/mozilla/editor/libeditor/base/SplitElementTxn.cpp b/mozilla/editor/libeditor/base/SplitElementTxn.cpp index ee040d801dc..26523978d22 100644 --- a/mozilla/editor/libeditor/base/SplitElementTxn.cpp +++ b/mozilla/editor/libeditor/base/SplitElementTxn.cpp @@ -19,7 +19,6 @@ #include "SplitElementTxn.h" #include "editor.h" #include "nsIDOMNode.h" -#include "nsIDOMNodeList.h" #include "nsIDOMElement.h" // note that aEditor is not refcounted @@ -55,7 +54,7 @@ nsresult SplitElementTxn::Do(void) // insert the new node if ((NS_SUCCEEDED(result)) && (nsnull!=mParent)) { - result = EditTxn::SplitNode(mNode, mOffset, mNewNode, mParent); + result = mEditor->SplitNode(mNode, mOffset, mNewNode, mParent); } } return result; @@ -64,13 +63,13 @@ nsresult SplitElementTxn::Do(void) nsresult SplitElementTxn::Undo(void) { // this assumes Do inserted the new node in front of the prior existing node - nsresult result = EditTxn::JoinNodes(mNode, mNewNode, mParent, PR_FALSE); + nsresult result = mEditor->JoinNodes(mNode, mNewNode, mParent, PR_FALSE); return result; } nsresult SplitElementTxn::Redo(void) { - nsresult result = EditTxn::SplitNode(mNode, mOffset, mNewNode, mParent); + nsresult result = mEditor->SplitNode(mNode, mOffset, mNewNode, mParent); return result; }