diff --git a/mozilla/editor/base/InsertElementTxn.h b/mozilla/editor/base/InsertElementTxn.h index 4f04676a3d6..b3e0421ede5 100644 --- a/mozilla/editor/base/InsertElementTxn.h +++ b/mozilla/editor/base/InsertElementTxn.h @@ -30,7 +30,7 @@ {0x86, 0xdb, 0x0, 0x0, 0x64, 0x65, 0x73, 0x74} } /** - * A transaction that deletes a single element + * A transaction that inserts a single element */ class InsertElementTxn : public EditTxn { diff --git a/mozilla/editor/base/Makefile.in b/mozilla/editor/base/Makefile.in index 31c12c00532..6fd1b19be19 100644 --- a/mozilla/editor/base/Makefile.in +++ b/mozilla/editor/base/Makefile.in @@ -51,6 +51,7 @@ CPPSRCS = \ DeleteTableRowTxn.cpp \ JoinTableCellsTxn.cpp \ InsertTextTxn.cpp \ + nsInsertHTMLTxn.cpp \ PlaceholderTxn.cpp \ DeleteTextTxn.cpp \ CreateElementTxn.cpp \ diff --git a/mozilla/editor/base/TransactionFactory.cpp b/mozilla/editor/base/TransactionFactory.cpp index d1f51396266..abe35f159f2 100644 --- a/mozilla/editor/base/TransactionFactory.cpp +++ b/mozilla/editor/base/TransactionFactory.cpp @@ -24,6 +24,7 @@ #include "DeleteTextTxn.h" #include "CreateElementTxn.h" #include "InsertElementTxn.h" +#include "nsInsertHTMLTxn.h" #include "DeleteElementTxn.h" #include "DeleteRangeTxn.h" #include "ChangeAttributeTxn.h" @@ -45,6 +46,7 @@ static NS_DEFINE_IID(kInsertTextTxnIID, INSERT_TEXT_TXN_IID); static NS_DEFINE_IID(kDeleteTextTxnIID, DELETE_TEXT_TXN_IID); static NS_DEFINE_IID(kCreateElementTxnIID, CREATE_ELEMENT_TXN_IID); static NS_DEFINE_IID(kInsertElementTxnIID, INSERT_ELEMENT_TXN_IID); +static NS_DEFINE_IID(kInsertHTMLTxnIID, NS_INSERT_HTML_TXN_IID); static NS_DEFINE_IID(kDeleteElementTxnIID, DELETE_ELEMENT_TXN_IID); static NS_DEFINE_IID(kDeleteRangeTxnIID, DELETE_RANGE_TXN_IID); static NS_DEFINE_IID(kChangeAttributeTxnIID,CHANGE_ATTRIBUTE_TXN_IID); @@ -81,6 +83,8 @@ TransactionFactory::GetNewTransaction(REFNSIID aTxnType, EditTxn **aResult) *aResult = new CreateElementTxn(); else if (aTxnType.Equals(kInsertElementTxnIID)) *aResult = new InsertElementTxn(); + else if (aTxnType.Equals(kInsertHTMLTxnIID)) + *aResult = new nsInsertHTMLTxn(); else if (aTxnType.Equals(kDeleteElementTxnIID)) *aResult = new DeleteElementTxn(); else if (aTxnType.Equals(kDeleteRangeTxnIID)) diff --git a/mozilla/editor/base/nsHTMLEditor.cpp b/mozilla/editor/base/nsHTMLEditor.cpp index 795cacf459d..f357ccdc3c5 100644 --- a/mozilla/editor/base/nsHTMLEditor.cpp +++ b/mozilla/editor/base/nsHTMLEditor.cpp @@ -20,6 +20,7 @@ #include "nsHTMLEditor.h" #include "nsHTMLEditRules.h" #include "nsEditorEventListeners.h" +#include "nsInsertHTMLTxn.h" #include "nsIDOMNodeList.h" #include "nsIDOMNSRange.h" #include "nsIDOMDocument.h" @@ -46,6 +47,8 @@ static NS_DEFINE_IID(kIDOMEventReceiverIID, NS_IDOMEVENTRECEIVER_IID); static NS_DEFINE_IID(kIDOMMouseListenerIID, NS_IDOMMOUSELISTENER_IID); static NS_DEFINE_IID(kIDOMKeyListenerIID, NS_IDOMKEYLISTENER_IID); +static NS_DEFINE_IID(kInsertHTMLTxnIID, NS_INSERT_HTML_TXN_IID); + static NS_DEFINE_CID(kEditorCID, NS_EDITOR_CID); static NS_DEFINE_IID(kIEditorIID, NS_IEDITOR_IID); static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID); @@ -376,37 +379,30 @@ NS_IMETHODIMP nsHTMLEditor::Paste() NS_IMETHODIMP nsHTMLEditor::Insert(nsString& aInputString) { + nsresult res; + nsEditor::BeginTransaction(); nsEditor::DeleteSelection(nsIEditor::eDoNothing); - nsCOMPtrselection; - nsresult res = nsEditor::GetSelection(getter_AddRefs(selection)); - if (NS_SUCCEEDED(res) && selection) + // Make the transaction for insert html: + nsInsertHTMLTxn* insertHTMLTxn = 0; + res = TransactionFactory::GetNewTransaction(kInsertHTMLTxnIID, + (EditTxn **)&insertHTMLTxn); + if (NS_SUCCEEDED(res)) { - // Get the first NSRange in the selection: - nsCOMPtr domrange; - res = selection->GetRangeAt(0, getter_AddRefs(domrange)); + res = insertHTMLTxn->Init(aInputString, this); if (NS_SUCCEEDED(res)) - { - nsCOMPtr nsrange (do_QueryInterface(domrange)); - if (nsrange) - { -#ifdef DEBUG_akkana - char* str = aInputString.ToNewCString(); - printf("Calling nsIDOMNSRange::InsertFragment(%s)\n", str); - delete[] str; -#endif /* DEBUG_akkana */ - res = nsrange->InsertFragment(aInputString); - // XXX NOTE: this part isn't going through the transaction manager! - } - } + res = Do(insertHTMLTxn); + + // XXX How is it that we don't have to release the transaction? } nsEditor::EndTransaction(); if (!NS_SUCCEEDED(res)) printf("Couldn't insert html: error was %d\n", res); + return res; } diff --git a/mozilla/editor/base/nsInsertHTMLTxn.cpp b/mozilla/editor/base/nsInsertHTMLTxn.cpp new file mode 100644 index 00000000000..766aaec1d18 --- /dev/null +++ b/mozilla/editor/base/nsInsertHTMLTxn.cpp @@ -0,0 +1,115 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * The contents of this file are subject to the Netscape Public License + * Version 1.0 (the "NPL"); you may not use this file except in + * compliance with the NPL. You may obtain a copy of the NPL at + * http://www.mozilla.org/NPL/ + * + * Software distributed under the NPL is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL + * for the specific language governing rights and limitations under the + * NPL. + * + * The Initial Developer of this code under the NPL is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998 Netscape Communications Corporation. All Rights + * Reserved. + */ + +#include "nsInsertHTMLTxn.h" +#include "nsIDOMSelection.h" +#include "nsIContent.h" +#include "nsIDOMNSRange.h" + + +nsInsertHTMLTxn::nsInsertHTMLTxn() : EditTxn(), mSrc("") +{ +} + +NS_IMETHODIMP nsInsertHTMLTxn::Init(nsString& aSrc, nsIEditor* aEditor) +{ + if (!aEditor) + return NS_ERROR_NULL_POINTER; + + mSrc = aSrc; + mEditor = do_QueryInterface(aEditor); + return NS_OK; +} + + +nsInsertHTMLTxn::~nsInsertHTMLTxn() +{ + //NS_RELEASE(mStr); // do nsStrings have to be released? +} + +NS_IMETHODIMP nsInsertHTMLTxn::Do(void) +{ + nsCOMPtrselection; + nsresult res = mEditor->GetSelection(getter_AddRefs(selection)); + if (NS_SUCCEEDED(res) && selection) + { + // Get the first range in the selection, and save it in mRange: + res = selection->GetRangeAt(0, getter_AddRefs(mRange)); + if (NS_SUCCEEDED(res)) + { + nsCOMPtr nsrange (do_QueryInterface(mRange)); + if (nsrange) + { +#ifdef DEBUG_akkana + char* str = mSrc.ToNewCString(); + printf("Calling nsInsertHTMLTxn::Do(%s)\n", str); + delete[] str; +#endif /* DEBUG_akkana */ + res = nsrange->InsertFragment(mSrc); + // XXX NOTE: this part isn't going through the transaction manager! + } + } +#ifdef DEBUG_akkana + else printf("nsInsertHTMLTxn::Do: Couldn't get selection range!\n"); +#endif + } + + return res; +} + +NS_IMETHODIMP nsInsertHTMLTxn::Undo(void) +{ +#ifdef DEBUG_akkana + printf("%p Undo Insert HTML\n", this); +#endif /* DEBUG_akkana */ + + if (!mRange) + return NS_ERROR_NULL_POINTER; + + return mRange->DeleteContents(); +} + +NS_IMETHODIMP nsInsertHTMLTxn::Merge(PRBool *aDidMerge, nsITransaction *aTransaction) +{ + if (nsnull != aDidMerge) + *aDidMerge=PR_FALSE; + return NS_OK; +} + +NS_IMETHODIMP nsInsertHTMLTxn::Write(nsIOutputStream *aOutputStream) +{ + return NS_ERROR_NOT_IMPLEMENTED; +} + +NS_IMETHODIMP nsInsertHTMLTxn::GetUndoString(nsString **aString) +{ + if (nsnull!=aString) + { + **aString="Remove HTML: "; + } + return NS_OK; +} + +NS_IMETHODIMP nsInsertHTMLTxn::GetRedoString(nsString **aString) +{ + if (nsnull!=aString) + { + **aString="Insert HTML: "; + } + return NS_OK; +} diff --git a/mozilla/editor/base/nsInsertHTMLTxn.h b/mozilla/editor/base/nsInsertHTMLTxn.h new file mode 100644 index 00000000000..de0c9194128 --- /dev/null +++ b/mozilla/editor/base/nsInsertHTMLTxn.h @@ -0,0 +1,80 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * The contents of this file are subject to the Netscape Public License + * Version 1.0 (the "NPL"); you may not use this file except in + * compliance with the NPL. You may obtain a copy of the NPL at + * http://www.mozilla.org/NPL/ + * + * Software distributed under the NPL is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL + * for the specific language governing rights and limitations under the + * NPL. + * + * The Initial Developer of this code under the NPL is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998 Netscape Communications Corporation. All Rights + * Reserved. + */ + +#ifndef nsInsertHTMLTxn_h__ +#define nsInsertHTMLTxn_h__ + +#include "EditTxn.h" +#include "nsIEditor.h" +#include "nsIDOMRange.h" +#include "nsCOMPtr.h" + +#define NS_INSERT_HTML_TXN_IID \ +{/* a6cf90fd-15b3-11d2-932e-00805f8add3 */ \ +0xa6cf90fc, 0x15b3, 0x11d2, \ +{0x93, 0x2e, 0x00, 0x80, 0x5f, 0x8a, 0xdd, 0x32} }; + +/** + * A transaction that inserts a string of html source + */ +class nsInsertHTMLTxn : public EditTxn +{ +public: + + /** initialize the transaction. + * @param aSrc the source for the HTML to insert + * @param aEditor the editor in which to do the work + */ + NS_IMETHOD Init(nsString& aSrc, + nsIEditor *aEditor); + +private: + nsInsertHTMLTxn(); + +public: + + virtual ~nsInsertHTMLTxn(); + + NS_IMETHOD Do(void); + + NS_IMETHOD Undo(void); + + NS_IMETHOD Merge(PRBool *aDidMerge, nsITransaction *aTransaction); + + NS_IMETHOD Write(nsIOutputStream *aOutputStream); + + NS_IMETHOD GetUndoString(nsString **aString); + + NS_IMETHOD GetRedoString(nsString **aString); + +protected: + + /** the html to insert */ + nsString mSrc; + + /** the range representing the inserted fragment */ + nsCOMPtr mRange; + + /** the editor for this transaction */ + nsCOMPtr mEditor; + + friend class TransactionFactory; + +}; + +#endif diff --git a/mozilla/editor/libeditor/base/InsertElementTxn.h b/mozilla/editor/libeditor/base/InsertElementTxn.h index 4f04676a3d6..b3e0421ede5 100644 --- a/mozilla/editor/libeditor/base/InsertElementTxn.h +++ b/mozilla/editor/libeditor/base/InsertElementTxn.h @@ -30,7 +30,7 @@ {0x86, 0xdb, 0x0, 0x0, 0x64, 0x65, 0x73, 0x74} } /** - * A transaction that deletes a single element + * A transaction that inserts a single element */ class InsertElementTxn : public EditTxn { diff --git a/mozilla/editor/libeditor/base/TransactionFactory.cpp b/mozilla/editor/libeditor/base/TransactionFactory.cpp index d1f51396266..abe35f159f2 100644 --- a/mozilla/editor/libeditor/base/TransactionFactory.cpp +++ b/mozilla/editor/libeditor/base/TransactionFactory.cpp @@ -24,6 +24,7 @@ #include "DeleteTextTxn.h" #include "CreateElementTxn.h" #include "InsertElementTxn.h" +#include "nsInsertHTMLTxn.h" #include "DeleteElementTxn.h" #include "DeleteRangeTxn.h" #include "ChangeAttributeTxn.h" @@ -45,6 +46,7 @@ static NS_DEFINE_IID(kInsertTextTxnIID, INSERT_TEXT_TXN_IID); static NS_DEFINE_IID(kDeleteTextTxnIID, DELETE_TEXT_TXN_IID); static NS_DEFINE_IID(kCreateElementTxnIID, CREATE_ELEMENT_TXN_IID); static NS_DEFINE_IID(kInsertElementTxnIID, INSERT_ELEMENT_TXN_IID); +static NS_DEFINE_IID(kInsertHTMLTxnIID, NS_INSERT_HTML_TXN_IID); static NS_DEFINE_IID(kDeleteElementTxnIID, DELETE_ELEMENT_TXN_IID); static NS_DEFINE_IID(kDeleteRangeTxnIID, DELETE_RANGE_TXN_IID); static NS_DEFINE_IID(kChangeAttributeTxnIID,CHANGE_ATTRIBUTE_TXN_IID); @@ -81,6 +83,8 @@ TransactionFactory::GetNewTransaction(REFNSIID aTxnType, EditTxn **aResult) *aResult = new CreateElementTxn(); else if (aTxnType.Equals(kInsertElementTxnIID)) *aResult = new InsertElementTxn(); + else if (aTxnType.Equals(kInsertHTMLTxnIID)) + *aResult = new nsInsertHTMLTxn(); else if (aTxnType.Equals(kDeleteElementTxnIID)) *aResult = new DeleteElementTxn(); else if (aTxnType.Equals(kDeleteRangeTxnIID)) diff --git a/mozilla/editor/libeditor/html/nsHTMLEditor.cpp b/mozilla/editor/libeditor/html/nsHTMLEditor.cpp index 795cacf459d..f357ccdc3c5 100644 --- a/mozilla/editor/libeditor/html/nsHTMLEditor.cpp +++ b/mozilla/editor/libeditor/html/nsHTMLEditor.cpp @@ -20,6 +20,7 @@ #include "nsHTMLEditor.h" #include "nsHTMLEditRules.h" #include "nsEditorEventListeners.h" +#include "nsInsertHTMLTxn.h" #include "nsIDOMNodeList.h" #include "nsIDOMNSRange.h" #include "nsIDOMDocument.h" @@ -46,6 +47,8 @@ static NS_DEFINE_IID(kIDOMEventReceiverIID, NS_IDOMEVENTRECEIVER_IID); static NS_DEFINE_IID(kIDOMMouseListenerIID, NS_IDOMMOUSELISTENER_IID); static NS_DEFINE_IID(kIDOMKeyListenerIID, NS_IDOMKEYLISTENER_IID); +static NS_DEFINE_IID(kInsertHTMLTxnIID, NS_INSERT_HTML_TXN_IID); + static NS_DEFINE_CID(kEditorCID, NS_EDITOR_CID); static NS_DEFINE_IID(kIEditorIID, NS_IEDITOR_IID); static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID); @@ -376,37 +379,30 @@ NS_IMETHODIMP nsHTMLEditor::Paste() NS_IMETHODIMP nsHTMLEditor::Insert(nsString& aInputString) { + nsresult res; + nsEditor::BeginTransaction(); nsEditor::DeleteSelection(nsIEditor::eDoNothing); - nsCOMPtrselection; - nsresult res = nsEditor::GetSelection(getter_AddRefs(selection)); - if (NS_SUCCEEDED(res) && selection) + // Make the transaction for insert html: + nsInsertHTMLTxn* insertHTMLTxn = 0; + res = TransactionFactory::GetNewTransaction(kInsertHTMLTxnIID, + (EditTxn **)&insertHTMLTxn); + if (NS_SUCCEEDED(res)) { - // Get the first NSRange in the selection: - nsCOMPtr domrange; - res = selection->GetRangeAt(0, getter_AddRefs(domrange)); + res = insertHTMLTxn->Init(aInputString, this); if (NS_SUCCEEDED(res)) - { - nsCOMPtr nsrange (do_QueryInterface(domrange)); - if (nsrange) - { -#ifdef DEBUG_akkana - char* str = aInputString.ToNewCString(); - printf("Calling nsIDOMNSRange::InsertFragment(%s)\n", str); - delete[] str; -#endif /* DEBUG_akkana */ - res = nsrange->InsertFragment(aInputString); - // XXX NOTE: this part isn't going through the transaction manager! - } - } + res = Do(insertHTMLTxn); + + // XXX How is it that we don't have to release the transaction? } nsEditor::EndTransaction(); if (!NS_SUCCEEDED(res)) printf("Couldn't insert html: error was %d\n", res); + return res; }