diff --git a/mozilla/editor/base/Makefile.in b/mozilla/editor/base/Makefile.in index 4ccea61c08a..ff3e97e51ed 100644 --- a/mozilla/editor/base/Makefile.in +++ b/mozilla/editor/base/Makefile.in @@ -62,19 +62,22 @@ CPPSRCS = \ IMECommitTxn.cpp \ $(NULL) -ifdef ENABLE_JS_EDITOR_LOG -DEFINES += -DENABLE_JS_EDITOR_LOG -CPPSRCS += \ - nsJSEditorLog.cpp \ - nsJSTxnLog.cpp \ +# Enable Editor API Logging! +ENABLE_EDITOR_API_LOG = 1 + +ifdef ENABLE_EDITOR_API_LOG +DEFINES += -DENABLE_EDITOR_API_LOG +CPPSRCS += \ + nsHTMLEditorLog.cpp \ + nsEditorTxnLog.cpp \ $(NULL) endif MODULE = editor -EXTRA_DSO_LDOPTS =\ - -L$(DIST)/lib -lneckoutil_s \ - $(NULL) +EXTRA_DSO_LDOPTS = -L$(DIST)/lib \ + -lneckoutil_s \ + $(NULL) include $(topsrcdir)/config/rules.mk diff --git a/mozilla/editor/base/makefile.win b/mozilla/editor/base/makefile.win index 95555dadb23..fb3e360d803 100644 --- a/mozilla/editor/base/makefile.win +++ b/mozilla/editor/base/makefile.win @@ -101,18 +101,21 @@ MAKE_OBJ_TYPE = DLL DLLNAME = ender DLL=.\$(OBJDIR)\$(DLLNAME).dll -!if defined(ENABLE_JS_EDITOR_LOG) -CPPSRCS += \ - nsJSEditorLog.cpp \ - nsJSTxnLog.cpp \ - $(NULL) +# Enable Editor API Logging! +ENABLE_EDITOR_API_LOG=1 -CPP_OBJS += \ - .\$(OBJDIR)\nsJSEditorLog.obj \ - .\$(OBJDIR)\nsJSTxnLog.obj \ - $(NULL) +!if defined(ENABLE_EDITOR_API_LOG) +CPPSRCS = $(CPPSRCS) \ + nsHTMLEditorLog.cpp \ + nsEditorTxnLog.cpp \ + $(NULL) -DEFINES = -DENABLE_JS_EDITOR_LOG $(DEFINES) +CPP_OBJS = $(CPP_OBJS) \ + .\$(OBJDIR)\nsHTMLEditorLog.obj \ + .\$(OBJDIR)\nsEditorTxnLog.obj \ + $(NULL) + +DEFINES = -DENABLE_EDITOR_API_LOG $(DEFINES) !endif diff --git a/mozilla/editor/base/nsEditor.cpp b/mozilla/editor/base/nsEditor.cpp index 9196164a4ba..01adf94ad49 100644 --- a/mozilla/editor/base/nsEditor.cpp +++ b/mozilla/editor/base/nsEditor.cpp @@ -16,7 +16,6 @@ * Reserved. */ -#include "nsVector.h" #include "nsVoidArray.h" #include "nsIDOMDocument.h" @@ -65,11 +64,6 @@ #include "nsIContentIterator.h" #include "nsLayoutCID.h" -#ifdef ENABLE_JS_EDITOR_LOG -#include "nsJSEditorLog.h" -#include "nsJSTxnLog.h" -#endif // ENABLE_JS_EDITOR_LOG - // transactions the editor knows how to build #include "TransactionFactory.h" #include "EditAggregateTxn.h" @@ -156,10 +150,6 @@ nsEditor::nsEditor() , mDoc(nsnull) , mPrefs(nsnull) , mDocCharset("ISO-8859-1") -#ifdef ENABLE_JS_EDITOR_LOG -, mJSEditorLog(nsnull) -, mJSTxnLog(nsnull) -#endif // ENABLE_JS_EDITOR_LOG { //initialize member variables here NS_INIT_REFCNT(); @@ -188,12 +178,6 @@ nsEditor::~nsEditor() mActionListeners = 0; } -#ifdef ENABLE_JS_EDITOR_LOG - - StopLogging(); - -#endif // ENABLE_JS_EDITOR_LOG - // Release service pointers if (mPrefs) nsServiceManager::ReleaseService(kPrefCID, mPrefs); @@ -231,11 +215,6 @@ nsEditor::QueryInterface(REFNSIID aIID, void** aInstancePtr) NS_ADDREF_THIS(); return NS_OK; } - if (aIID.Equals(nsIEditorLogging::GetIID())) { - *aInstancePtr = (void*)(nsIEditorLogging*)this; - NS_ADDREF_THIS(); - return NS_OK; - } return NS_NOINTERFACE; } @@ -444,13 +423,6 @@ NS_IMETHODIMP nsEditor::SaveDocument(PRBool saveAs, PRBool saveCopy) NS_IMETHODIMP nsEditor::Save() { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->Save(); -#endif // ENABLE_JS_EDITOR_LOG - nsresult rv = SaveDocument(PR_FALSE, PR_FALSE); if (NS_FAILED(rv)) return rv; @@ -461,13 +433,6 @@ NS_IMETHODIMP nsEditor::Save() NS_IMETHODIMP nsEditor::SaveAs(PRBool aSavingCopy) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->SaveAs(aSavingCopy); -#endif // ENABLE_JS_EDITOR_LOG - return SaveDocument(PR_TRUE, aSavingCopy); } @@ -536,13 +501,6 @@ nsEditor::EnableUndo(PRBool aEnable) NS_IMETHODIMP nsEditor::Undo(PRUint32 aCount) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->Undo(aCount); -#endif // ENABLE_JS_EDITOR_LOG - if (gNoisy) { printf("Editor::Undo ----------\n"); } nsresult result = NS_OK; @@ -588,13 +546,6 @@ NS_IMETHODIMP nsEditor::CanUndo(PRBool &aIsEnabled, PRBool &aCanUndo) NS_IMETHODIMP nsEditor::Redo(PRUint32 aCount) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->Redo(aCount); -#endif // ENABLE_JS_EDITOR_LOG - if (gNoisy) { printf("Editor::Redo ----------\n"); } nsresult result = NS_OK; @@ -640,13 +591,6 @@ NS_IMETHODIMP nsEditor::CanRedo(PRBool &aIsEnabled, PRBool &aCanRedo) NS_IMETHODIMP nsEditor::BeginTransaction() { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->BeginTransaction(); -#endif // ENABLE_JS_EDITOR_LOG - BeginUpdateViewBatch(); if ((nsITransactionManager *)nsnull!=mTxnMgr.get()) @@ -660,13 +604,6 @@ nsEditor::BeginTransaction() NS_IMETHODIMP nsEditor::EndTransaction() { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->EndTransaction(); -#endif // ENABLE_JS_EDITOR_LOG - if ((nsITransactionManager *)nsnull!=mTxnMgr.get()) { mTxnMgr->EndBatch(); @@ -680,13 +617,6 @@ nsEditor::EndTransaction() // XXX: the rule system should tell us which node to select all on (ie, the root, or the body) NS_IMETHODIMP nsEditor::SelectAll() { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->SelectAll(); -#endif // ENABLE_JS_EDITOR_LOG - if (!mDoc || !mPresShell) { return NS_ERROR_NOT_INITIALIZED; } nsCOMPtr selection; @@ -700,13 +630,6 @@ NS_IMETHODIMP nsEditor::SelectAll() NS_IMETHODIMP nsEditor::BeginningOfDocument() { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->BeginningOfDocument(); -#endif // ENABLE_JS_EDITOR_LOG - if (!mDoc || !mPresShell) { return NS_ERROR_NOT_INITIALIZED; } nsCOMPtr selection; @@ -740,13 +663,6 @@ NS_IMETHODIMP nsEditor::BeginningOfDocument() NS_IMETHODIMP nsEditor::EndOfDocument() { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->EndOfDocument(); -#endif // ENABLE_JS_EDITOR_LOG - if (!mDoc || !mPresShell) { return NS_ERROR_NOT_INITIALIZED; } nsCOMPtr selection; @@ -1322,67 +1238,6 @@ nsEditor::SetCompositionString(const nsString& aCompositionString, nsIPrivateTex return result; } -#ifdef XP_MAC -#pragma mark - -#pragma mark --- nsIEditorLogging --- -#pragma mark - -#endif - - -NS_IMETHODIMP -nsEditor::StartLogging(nsIFileSpec *aLogFile) -{ -#ifdef ENABLE_JS_EDITOR_LOG - - mJSEditorLog = new nsJSEditorLog(this, aLogFile); - - if (!mJSEditorLog) - return NS_ERROR_OUT_OF_MEMORY; - - if (mTxnMgr) - { - mJSTxnLog = new nsJSTxnLog(mJSEditorLog); - - if (mJSTxnLog) - { - NS_ADDREF(mJSTxnLog); - mTxnMgr->AddListener(mJSTxnLog); - } - else - return NS_ERROR_OUT_OF_MEMORY; - } - -#endif // ENABLE_JS_EDITOR_LOG - - return NS_OK; -} - -NS_IMETHODIMP -nsEditor::StopLogging() -{ -#ifdef ENABLE_JS_EDITOR_LOG - - if (mTxnMgr && mJSTxnLog) - mTxnMgr->RemoveListener(mJSTxnLog); - - if (mJSTxnLog) - { - NS_RELEASE(mJSTxnLog); - mJSTxnLog = 0; - } - - if (mJSEditorLog) - { - delete mJSEditorLog; - mJSEditorLog = 0; - } - -#endif // ENABLE_JS_EDITOR_LOG - - return NS_OK; -} - - #ifdef XP_MAC #pragma mark - @@ -1541,14 +1396,6 @@ nsString& nsEditor::GetTextNodeTag() NS_IMETHODIMP nsEditor::InsertTextImpl(const nsString& aStringToInsert) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->InsertText(aStringToInsert); - -#endif // ENABLE_JS_EDITOR_LOG - EditAggregateTxn *aggTxn = nsnull; // Create the "delete current selection" txn nsresult result = CreateAggregateTxnForDeleteSelection(InsertTextTxn::gInsertTextTxnName, &aggTxn); @@ -3844,13 +3691,6 @@ nsresult nsEditor::EndUpdateViewBatch() NS_IMETHODIMP nsEditor::DeleteSelectionImpl(ESelectionCollapseDirection aAction) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->DeleteSelection(aAction); -#endif // ENABLE_JS_EDITOR_LOG - nsresult result; EditAggregateTxn *txn; diff --git a/mozilla/editor/base/nsEditor.h b/mozilla/editor/base/nsEditor.h index 1bd8e48b70c..82420966c53 100644 --- a/mozilla/editor/base/nsEditor.h +++ b/mozilla/editor/base/nsEditor.h @@ -24,7 +24,6 @@ #include "nsIEditor.h" #include "nsIEditorIMESupport.h" -#include "nsIEditorLogging.h" #include "nsIDOMDocument.h" #include "nsIDOMSelection.h" @@ -69,11 +68,6 @@ class IMETextTxn; class AddStyleSheetTxn; class RemoveStyleSheetTxn; -#ifdef ENABLE_JS_EDITOR_LOG -class nsJSEditorLog; -class nsJSTxnLog; -#endif // ENABLE_JS_EDITOR_LOG - //This is the monitor for the editor. PRMonitor *GetEditorMonitor(); @@ -84,8 +78,7 @@ PRMonitor *GetEditorMonitor(); * delegate the actual commands to the editor independent of the XPFE implementation. */ class nsEditor : public nsIEditor, - public nsIEditorIMESupport, - public nsIEditorLogging + public nsIEditorIMESupport { public: @@ -208,11 +201,6 @@ public: NS_IMETHOD SetCompositionString(const nsString& aCompositionString, nsIPrivateTextRangeList* aTextRangeList,nsTextEventReply* aReply); NS_IMETHOD EndComposition(void); - /* ------------ nsIEditorLogging methods -------------- */ - - NS_IMETHOD StartLogging(nsIFileSpec *aLogFile); - NS_IMETHOD StopLogging(); - public: @@ -623,12 +611,6 @@ protected: //Document 'SaveAs' charset nsString mDocCharset; - -#ifdef ENABLE_JS_EDITOR_LOG - nsJSEditorLog *mJSEditorLog; - nsJSTxnLog *mJSTxnLog; -#endif // ENABLE_JS_EDITOR_LOG - static PRInt32 gInstanceCount; friend PRBool NSCanUnload(nsISupports* serviceMgr); diff --git a/mozilla/editor/base/nsEditorFactory.cpp b/mozilla/editor/base/nsEditorFactory.cpp index 2735585f7a2..423226235b6 100644 --- a/mozilla/editor/base/nsEditorFactory.cpp +++ b/mozilla/editor/base/nsEditorFactory.cpp @@ -22,9 +22,13 @@ #include "nsIEditor.h" #include "nsIHTMLEditor.h" +#include "nsEditorShell.h" #include "nsEditorShell.h" #include "nsEditorShellFactory.h" +#ifdef ENABLE_EDITOR_API_LOG +#include "nsHTMLEditorLog.h" +#endif // ENABLE_EDITOR_API_LOG #include "nsHTMLEditor.h" #include "nsEditor.h" @@ -115,7 +119,12 @@ nsEditorFactory::CreateInstance(nsISupports *aOuter, REFNSIID aIID, void **aResu { //Need to cast to interface first to avoid "ambiguous conversion..." error // because of multiple nsISupports in the class hierarchy + +#ifdef ENABLE_EDITOR_API_LOG + obj = (nsISupports *)(nsIHTMLEditor*)new nsHTMLEditorLog(); +#else obj = (nsISupports *)(nsIHTMLEditor*)new nsHTMLEditor(); +#endif // ENABLE_EDITOR_API_LOG } if (obj && NS_FAILED(obj->QueryInterface(aIID, (void**)aResult)) ) diff --git a/mozilla/editor/base/nsEditorTxnLog.cpp b/mozilla/editor/base/nsEditorTxnLog.cpp new file mode 100644 index 00000000000..0e8f63535fe --- /dev/null +++ b/mozilla/editor/base/nsEditorTxnLog.cpp @@ -0,0 +1,406 @@ +/* -*- 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 +#include "nsHTMLEditorLog.h" +#include "nsEditorTxnLog.h" + +#define LOCK_LOG(doc) +#define UNLOCK_LOG(doc) + +static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID); + +nsEditorTxnLog::nsEditorTxnLog(nsHTMLEditorLog *aEditorLog) +{ + mRefCnt = 0; + mIndentLevel = 0; + mBatchCount = 0; + mEditorLog = aEditorLog; +} + +nsEditorTxnLog::~nsEditorTxnLog() +{ +} + +// #define DEBUG_EDITOR_TXN_LOG_REFCNT 1 + +#ifdef DEBUG_EDITOR_TXN_LOG_REFCNT + +nsrefcnt nsEditorTxnLog::AddRef(void) +{ + return ++mRefCnt; +} + +nsrefcnt nsEditorTxnLog::Release(void) +{ + NS_PRECONDITION(0 != mRefCnt, "dup release"); + if (--mRefCnt == 0) { + NS_DELETEXPCOM(this); + return 0; + } + return mRefCnt; +} + +#else + +NS_IMPL_ADDREF(nsEditorTxnLog) +NS_IMPL_RELEASE(nsEditorTxnLog) + +#endif + +NS_IMETHODIMP +nsEditorTxnLog::QueryInterface(REFNSIID aIID, void** aInstancePtr) +{ + if (nsnull == aInstancePtr) { + return NS_ERROR_NULL_POINTER; + } + if (aIID.Equals(kISupportsIID)) { + *aInstancePtr = (void*)(nsISupports*)this; + NS_ADDREF_THIS(); + return NS_OK; + } + if (aIID.Equals(nsITransactionListener::GetIID())) { + *aInstancePtr = (void*)(nsITransactionListener*)this; + NS_ADDREF_THIS(); + return NS_OK; + } + *aInstancePtr = 0; + return NS_NOINTERFACE; +} + +NS_IMETHODIMP +nsEditorTxnLog::WillDo(nsITransactionManager *aTxMgr, nsITransaction *aTransaction) +{ + LOCK_LOG(this); + + PrintIndent(mIndentLevel++); + Write("WillDo: "); + Write(GetString(aTransaction)); + Write("\n"); + Flush(); + + UNLOCK_LOG(this); + + return NS_OK; +} + +NS_IMETHODIMP +nsEditorTxnLog::DidDo(nsITransactionManager *aTxMgr, nsITransaction *aTransaction, nsresult aDoResult) +{ + LOCK_LOG(this); + + PrintIndent(--mIndentLevel); + Write("DidDo: "); + Write(GetString(aTransaction)); + Write("("); + WriteInt("%d", aDoResult); + Write(")\n"); + Flush(); + + UNLOCK_LOG(this); + + return NS_OK; +} + +NS_IMETHODIMP +nsEditorTxnLog::WillUndo(nsITransactionManager *aTxMgr, nsITransaction *aTransaction) +{ + LOCK_LOG(this); + + PrintIndent(mIndentLevel++); + + if (aTransaction) + { + Write("WillUndo: "); + Write(GetString(aTransaction)); + Write("\n"); + } + else + Write("WillUndoBatch\n"); + + Flush(); + + UNLOCK_LOG(this); + + return NS_OK; +} + +NS_IMETHODIMP +nsEditorTxnLog::DidUndo(nsITransactionManager *aTxMgr, nsITransaction *aTransaction, nsresult aUndoResult) +{ + LOCK_LOG(this); + + PrintIndent(--mIndentLevel); + + if (aTransaction) + { + Write("DidUndo: "); + Write(GetString(aTransaction)); + Write("("); + WriteInt("%d", aUndoResult); + Write(")\n"); + } + else + { + Write("EndUndoBatch ("); + WriteInt("%d", aUndoResult); + Write(")\n"); + } + + Flush(); + + UNLOCK_LOG(this); + + return NS_OK; +} + +NS_IMETHODIMP +nsEditorTxnLog::WillRedo(nsITransactionManager *aTxMgr, nsITransaction *aTransaction) +{ + LOCK_LOG(this); + + PrintIndent(mIndentLevel++); + + if (aTransaction) + { + Write("WillRedo: "); + Write(GetString(aTransaction)); + Write("\n"); + } + else + Write("WillRedoBatch\n"); + + Flush(); + + UNLOCK_LOG(this); + + return NS_OK; +} + +NS_IMETHODIMP +nsEditorTxnLog::DidRedo(nsITransactionManager *aTxMgr, nsITransaction *aTransaction, nsresult aRedoResult) +{ + LOCK_LOG(this); + + PrintIndent(--mIndentLevel); + + if (aTransaction) + { + Write("DidRedo: "); + Write(GetString(aTransaction)); + Write(" ("); + WriteInt("%d", aRedoResult); + Write(")\n"); + } + else + { + Write("DidRedoBatch ("); + WriteInt("%d", aRedoResult); + Write(")\n"); + } + + Flush(); + + UNLOCK_LOG(this); + + return NS_OK; +} + +NS_IMETHODIMP +nsEditorTxnLog::WillBeginBatch(nsITransactionManager *aTxMgr) +{ + LOCK_LOG(this); + + PrintIndent(mIndentLevel); + Write("WillBeginBatch: "); + WriteInt("%d", mBatchCount); + Write("\n"); + Flush(); + + UNLOCK_LOG(this); + + return NS_OK; +} + +NS_IMETHODIMP +nsEditorTxnLog::DidBeginBatch(nsITransactionManager *aTxMgr, nsresult aResult) +{ + LOCK_LOG(this); + + PrintIndent(mIndentLevel++); + Write("DidBeginBatch: "); + WriteInt("%d", mBatchCount++); + Write(" ("); + WriteInt("%d", aResult); + Write(")\n"); + Flush(); + + UNLOCK_LOG(this); + + return NS_OK; +} + +NS_IMETHODIMP +nsEditorTxnLog::WillEndBatch(nsITransactionManager *aTxMgr) +{ + LOCK_LOG(this); + + PrintIndent(--mIndentLevel); + Write("WillEndBatch: "); + WriteInt("%d", --mBatchCount); + Write("\n"); + Flush(); + + UNLOCK_LOG(this); + + return NS_OK; +} + +NS_IMETHODIMP +nsEditorTxnLog::DidEndBatch(nsITransactionManager *aTxMgr, nsresult aResult) +{ + LOCK_LOG(this); + + PrintIndent(mIndentLevel); + Write("DidEndBatch: "); + WriteInt("%d", mBatchCount); + Write(" ("); + WriteInt("%d", aResult); + Write(")\n"); + Flush(); + + UNLOCK_LOG(this); + + return NS_OK; +} + +NS_IMETHODIMP +nsEditorTxnLog::WillMerge(nsITransactionManager *aTxMgr, nsITransaction *aTopTransaction, nsITransaction *aTransaction) +{ + LOCK_LOG(this); + + PrintIndent(mIndentLevel); + Write("WillMerge: "); + Write(GetString(aTopTransaction)); + Write(" <-- "); + Write(GetString(aTransaction)); + Write("\n"); + Flush(); + + UNLOCK_LOG(this); + + return NS_OK; +} + +NS_IMETHODIMP +nsEditorTxnLog::DidMerge(nsITransactionManager *aTxMgr, nsITransaction *aTopTransaction, nsITransaction *aTransaction, PRBool aDidMerge, nsresult aMergeResult) +{ + LOCK_LOG(this); + + PrintIndent(mIndentLevel); + Write("DidMerge: "); + Write(GetString(aTopTransaction)); + Write(" <-- "); + Write(GetString(aTransaction)); + Write(" ("); + Write(aDidMerge ? "TRUE" : "FALSE"); + Write(", "); + WriteInt("%d", aMergeResult); + Write(")\n"); + Flush(); + + UNLOCK_LOG(this); + + return NS_OK; +} + +const char * +nsEditorTxnLog::GetString(nsITransaction *aTransaction) +{ + static char buf[256]; + + nsString str = ""; + + aTransaction->GetRedoString(&str); + + if (str.Length() == 0) + str = ""; + + buf[0] = '\0'; + str.ToCString(buf, 256); + + return buf; +} + +nsresult +nsEditorTxnLog::PrintIndent(PRInt32 aIndentLevel) +{ + PRInt32 i; + + Write(" // "); + + for (i = 0; i < aIndentLevel; i++) + Write(" "); + + return NS_OK; +} + +nsresult +nsEditorTxnLog::Write(const char *aBuffer) +{ + if (!aBuffer) + return NS_ERROR_NULL_POINTER; + + if (mEditorLog) + mEditorLog->Write(aBuffer); + else + printf(aBuffer); + + return NS_OK; +} + +nsresult +nsEditorTxnLog::WriteInt(const char *aFormat, PRInt32 aInt) +{ + if (!aFormat) + return NS_ERROR_NULL_POINTER; + + if (mEditorLog) + mEditorLog->WriteInt(aFormat, aInt); + else + printf(aFormat, aInt); + + return NS_OK; +} + +nsresult +nsEditorTxnLog::Flush() +{ + nsresult result = NS_OK; + +#ifdef SLOWS_THINGS_WAY_DOWN + + if (mEditorLog) + result = mEditorLog->Flush(); + else + fflush(stdout); + +#endif // SLOWS_THINGS_WAY_DOWN + + return result; +} diff --git a/mozilla/editor/base/nsEditorTxnLog.h b/mozilla/editor/base/nsEditorTxnLog.h new file mode 100644 index 00000000000..f5be21ccfbe --- /dev/null +++ b/mozilla/editor/base/nsEditorTxnLog.h @@ -0,0 +1,77 @@ +/* -*- 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://wwwt.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 nsEditorTxnLog_h__ +#define nsEditorTxnLog_h__ + +#include "nsITransaction.h" +#include "nsITransactionManager.h" +#include "nsITransactionListener.h" + +class nsHTMLEditorLog; + +/** implementation of a transaction listener object. + * + */ +class nsEditorTxnLog : public nsITransactionListener +{ +private: + + nsHTMLEditorLog *mEditorLog; + PRInt32 mIndentLevel; + PRInt32 mBatchCount; + +public: + + /** The default constructor. + */ + nsEditorTxnLog(nsHTMLEditorLog *aEditorLog=0); + + /** The default destructor. + */ + virtual ~nsEditorTxnLog(); + + /* Macro for AddRef(), Release(), and QueryInterface() */ + NS_DECL_ISUPPORTS + + /* nsITransactionListener method implementations. */ + NS_IMETHOD WillDo(nsITransactionManager *aTxMgr, nsITransaction *aTransaction); + NS_IMETHOD DidDo(nsITransactionManager *aTxMgr, nsITransaction *aTransaction, nsresult aDoResult); + NS_IMETHOD WillUndo(nsITransactionManager *aTxMgr, nsITransaction *aTransaction); + NS_IMETHOD DidUndo(nsITransactionManager *aTxMgr, nsITransaction *aTransaction, nsresult aUndoResult); + NS_IMETHOD WillRedo(nsITransactionManager *aTxMgr, nsITransaction *aTransaction); + NS_IMETHOD DidRedo(nsITransactionManager *aTxMgr, nsITransaction *aTransaction, nsresult aRedoResult); + NS_IMETHOD WillBeginBatch(nsITransactionManager *aTxMgr); + NS_IMETHOD DidBeginBatch(nsITransactionManager *aTxMgr, nsresult aResult); + NS_IMETHOD WillEndBatch(nsITransactionManager *aTxMgr); + NS_IMETHOD DidEndBatch(nsITransactionManager *aTxMgr, nsresult aResult); + NS_IMETHOD WillMerge(nsITransactionManager *aTxMgr, nsITransaction *aTopTransaction, nsITransaction *aTransaction); + NS_IMETHOD DidMerge(nsITransactionManager *aTxMgr, nsITransaction *aTopTransaction, nsITransaction *aTransaction, PRBool aDidMerge, nsresult aMergeResult); + + +private: + + /* nsEditorTxnLog private methods. */ + const char *GetString(nsITransaction *aTransaction); + nsresult PrintIndent(PRInt32 aIndentLevel); + nsresult Write(const char *aBuffer); + nsresult WriteInt(const char *aFormat, PRInt32 aInt); + nsresult Flush(); +}; + +#endif // nsEditorTxnLog_h__ diff --git a/mozilla/editor/base/nsHTMLEditor.cpp b/mozilla/editor/base/nsHTMLEditor.cpp index 9243672a879..a1ca754660f 100644 --- a/mozilla/editor/base/nsHTMLEditor.cpp +++ b/mozilla/editor/base/nsHTMLEditor.cpp @@ -85,10 +85,6 @@ const unsigned char nbsp = 160; -#ifdef ENABLE_JS_EDITOR_LOG -#include "nsJSEditorLog.h" -#endif // ENABLE_JS_EDITOR_LOG - // HACK - CID for NavDTD until we can get at dtd via the document // {a6cf9107-15b3-11d2-932e-00805f8add32} #define NS_CNAVDTD_CID \ @@ -408,13 +404,6 @@ NS_IMETHODIMP nsHTMLEditor::SetInlineProperty(nsIAtom *aProperty, const nsString *aAttribute, const nsString *aValue) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->SetTextProperty(aProperty, aAttribute, aValue); -#endif // ENABLE_JS_EDITOR_LOG - if (!aProperty) { return NS_ERROR_NULL_POINTER; } if (!mRules) { return NS_ERROR_NOT_INITIALIZED; } @@ -667,14 +656,6 @@ NS_IMETHODIMP nsHTMLEditor::GetInlineProperty(nsIAtom *aProperty, NS_IMETHODIMP nsHTMLEditor::RemoveInlineProperty(nsIAtom *aProperty, const nsString *aAttribute) { - -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->RemoveTextProperty(aProperty, aAttribute); -#endif // ENABLE_JS_EDITOR_LOG - if (!aProperty) { return NS_ERROR_NULL_POINTER; } if (!mRules) { return NS_ERROR_NOT_INITIALIZED; } @@ -783,13 +764,6 @@ NS_IMETHODIMP nsHTMLEditor::RemoveInlineProperty(nsIAtom *aProperty, const nsStr NS_IMETHODIMP nsHTMLEditor::DeleteSelection(nsIEditor::ESelectionCollapseDirection aAction) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->DeleteSelection(aAction); -#endif // ENABLE_JS_EDITOR_LOG - if (!mRules) { return NS_ERROR_NOT_INITIALIZED; } nsCOMPtr selection; @@ -818,13 +792,6 @@ NS_IMETHODIMP nsHTMLEditor::DeleteSelection(nsIEditor::ESelectionCollapseDirecti NS_IMETHODIMP nsHTMLEditor::InsertText(const nsString& aStringToInsert) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->InsertText(aStringToInsert); -#endif // ENABLE_JS_EDITOR_LOG - if (!mRules) { return NS_ERROR_NOT_INITIALIZED; } nsCOMPtr selection; @@ -857,13 +824,6 @@ NS_IMETHODIMP nsHTMLEditor::InsertText(const nsString& aStringToInsert) NS_IMETHODIMP nsHTMLEditor::InsertHTML(const nsString& aInputString) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->InsertHTML(aInputString); -#endif // ENABLE_JS_EDITOR_LOG - nsAutoEditBatch beginBatching(this); nsresult res; @@ -964,13 +924,6 @@ NS_IMETHODIMP nsHTMLEditor::InsertHTML(const nsString& aInputString) NS_IMETHODIMP nsHTMLEditor::InsertBreak() { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->InsertBreak(); -#endif // ENABLE_JS_EDITOR_LOG - nsresult res; if (!mRules) { return NS_ERROR_NOT_INITIALIZED; } @@ -1042,13 +995,6 @@ NS_IMETHODIMP nsHTMLEditor::InsertBreak() NS_IMETHODIMP nsHTMLEditor::InsertElement(nsIDOMElement* aElement, PRBool aDeleteSelection) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->InsertElement(aElement, aDeleteSelection); -#endif // ENABLE_JS_EDITOR_LOG - nsresult res = NS_ERROR_NOT_INITIALIZED; if (!aElement) @@ -1211,13 +1157,6 @@ nsHTMLEditor::DeleteSelectionAndCreateNode(const nsString& aTag, NS_IMETHODIMP nsHTMLEditor::SelectElement(nsIDOMElement* aElement) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->SelectElement(aElement); -#endif // ENABLE_JS_EDITOR_LOG - nsresult res = NS_ERROR_NULL_POINTER; // Must be sure that element is contained in the document body @@ -1250,13 +1189,6 @@ nsHTMLEditor::SelectElement(nsIDOMElement* aElement) NS_IMETHODIMP nsHTMLEditor::SetCaretAfterElement(nsIDOMElement* aElement) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->SetCaretAfterElement(aElement); -#endif // ENABLE_JS_EDITOR_LOG - nsresult res = NS_ERROR_NULL_POINTER; // Be sure the element is contained in the document body @@ -1306,13 +1238,6 @@ NS_IMETHODIMP nsHTMLEditor::GetParagraphFormat(nsString& aParagraphFormat) NS_IMETHODIMP nsHTMLEditor::SetParagraphFormat(const nsString& aParagraphFormat) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->SetParagraphFormat(aParagraphFormat); -#endif // ENABLE_JS_EDITOR_LOG - nsresult res = NS_ERROR_NOT_INITIALIZED; //Kinda sad to waste memory just to force lower case nsAutoString tag = aParagraphFormat; @@ -1403,13 +1328,6 @@ nsHTMLEditor::GetParagraphStyle(nsStringArray *aTagList) NS_IMETHODIMP nsHTMLEditor::AddBlockParent(nsString& aParentTag) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->AddBlockParent(aParentTag); -#endif // ENABLE_JS_EDITOR_LOG - if (gNoisy) { char *tag = aParentTag.ToNewCString(); @@ -1454,13 +1372,6 @@ nsHTMLEditor::AddBlockParent(nsString& aParentTag) NS_IMETHODIMP nsHTMLEditor::ReplaceBlockParent(nsString& aParentTag) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->ReplaceBlockParent(aParentTag); -#endif // ENABLE_JS_EDITOR_LOG - if (gNoisy) { char *tag = aParentTag.ToNewCString(); @@ -1501,13 +1412,6 @@ nsHTMLEditor::ReplaceBlockParent(nsString& aParentTag) NS_IMETHODIMP nsHTMLEditor::RemoveParagraphStyle() { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->RemoveParagraphStyle(); -#endif // ENABLE_JS_EDITOR_LOG - if (gNoisy) { printf("---------- nsHTMLEditor::RemoveParagraphStyle ----------\n"); } @@ -1539,13 +1443,6 @@ nsHTMLEditor::RemoveParagraphStyle() NS_IMETHODIMP nsHTMLEditor::RemoveParent(const nsString &aParentTag) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->RemoveParent(aParentTag); -#endif // ENABLE_JS_EDITOR_LOG - if (gNoisy) { printf("---------- nsHTMLEditor::RemoveParent ----------\n"); } @@ -1577,13 +1474,6 @@ nsHTMLEditor::RemoveParent(const nsString &aParentTag) NS_IMETHODIMP nsHTMLEditor::InsertList(const nsString& aListType) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->InsertList(aListType); -#endif // ENABLE_JS_EDITOR_LOG - nsresult res; if (!mRules) { return NS_ERROR_NOT_INITIALIZED; } @@ -1668,13 +1558,6 @@ nsHTMLEditor::InsertList(const nsString& aListType) NS_IMETHODIMP nsHTMLEditor::InsertBasicBlock(const nsString& aBlockType) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->InsertBasicBlock(aBlockType); -#endif // ENABLE_JS_EDITOR_LOG - nsresult res; if (!mRules) { return NS_ERROR_NOT_INITIALIZED; } @@ -1753,13 +1636,6 @@ nsHTMLEditor::InsertBasicBlock(const nsString& aBlockType) NS_IMETHODIMP nsHTMLEditor::Indent(const nsString& aIndent) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->Indent(aIndent); -#endif // ENABLE_JS_EDITOR_LOG - nsresult res; if (!mRules) { return NS_ERROR_NOT_INITIALIZED; } @@ -1839,13 +1715,6 @@ nsHTMLEditor::Indent(const nsString& aIndent) NS_IMETHODIMP nsHTMLEditor::Align(const nsString& aAlignType) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->Align(aAlignType); -#endif // ENABLE_JS_EDITOR_LOG - nsAutoEditBatch beginBatching(this); nsCOMPtr node; PRBool cancel= PR_FALSE; @@ -2312,13 +2181,6 @@ nsHTMLEditor::SaveHLineSettings(nsIDOMElement* aElement) NS_IMETHODIMP nsHTMLEditor::InsertLinkAroundSelection(nsIDOMElement* aAnchorElement) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->InsertLinkAroundSelection(aAnchorElement); -#endif // ENABLE_JS_EDITOR_LOG - nsresult res=NS_ERROR_NULL_POINTER; nsCOMPtr selection; @@ -2375,15 +2237,6 @@ NS_IMETHODIMP nsHTMLEditor::SetBackgroundColor(const nsString& aColor) // TODO: Check selection for Cell, Row, Column or table and do color on appropriate level // For initial testing, just set the background on the BODY tag (the document's background) -// Do this only if setting a table or cell background -// It will be called in nsTextEditor::SetBackgroundColor for the page background -#if 0 //def ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->SetBackgroundColor(aColor); -#endif // ENABLE_JS_EDITOR_LOG - // TODO: Check selection for Cell, Row, Column or table and do color on appropriate level // For initial testing, just set the background on the BODY tag (the document's background) @@ -2401,14 +2254,6 @@ NS_IMETHODIMP nsHTMLEditor::SetBackgroundColor(const nsString& aColor) NS_IMETHODIMP nsHTMLEditor::SetBodyAttribute(const nsString& aAttribute, const nsString& aValue) { - -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->SetBodyAttribute(aAttribute, aValue); -#endif // ENABLE_JS_EDITOR_LOG - nsresult res; // TODO: Check selection for Cell, Row, Column or table and do color on appropriate level @@ -2564,13 +2409,6 @@ nsHTMLEditor::RemoveStyleSheet(nsICSSStyleSheet* aSheet) NS_IMETHODIMP nsHTMLEditor::ApplyStyleSheet(const nsString& aURL) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->ApplyStyleSheet(aURL); -#endif // ENABLE_JS_EDITOR_LOG - // XXX: Note that this is not an undo-able action yet! nsresult rv = NS_OK; @@ -2740,26 +2578,12 @@ NS_IMETHODIMP nsHTMLEditor::SetBodyWrapWidth(PRInt32 aWrapColumn) // NS_IMETHODIMP nsHTMLEditor::PasteAsQuotation() { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->PasteAsQuotation(); -#endif // ENABLE_JS_EDITOR_LOG - nsAutoString citation(""); return PasteAsCitedQuotation(citation); } NS_IMETHODIMP nsHTMLEditor::PasteAsCitedQuotation(const nsString& aCitation) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->PasteAsCitedQuotation(aCitation); -#endif // ENABLE_JS_EDITOR_LOG - nsAutoEditBatch beginBatching(this); nsCOMPtr newNode; nsAutoString tag("blockquote"); @@ -2803,13 +2627,6 @@ NS_IMETHODIMP nsHTMLEditor::PasteAsCitedQuotation(const nsString& aCitation) // NS_IMETHODIMP nsHTMLEditor::PasteAsPlaintextQuotation() { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->PasteAsQuotation(); -#endif // ENABLE_JS_EDITOR_LOG - // Get Clipboard Service nsIClipboard* clipboard; nsresult rv = nsServiceManager::GetService(kCClipboardCID, @@ -2850,13 +2667,6 @@ NS_IMETHODIMP nsHTMLEditor::PasteAsPlaintextQuotation() NS_IMETHODIMP nsHTMLEditor::InsertAsQuotation(const nsString& aQuotedText) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->InsertAsQuotation(aQuotedText); -#endif // ENABLE_JS_EDITOR_LOG - nsAutoString citation (""); return InsertAsCitedQuotation(aQuotedText, citation); } @@ -2864,13 +2674,6 @@ NS_IMETHODIMP nsHTMLEditor::InsertAsQuotation(const nsString& aQuotedText) // text insert. NS_IMETHODIMP nsHTMLEditor::InsertAsPlaintextQuotation(const nsString& aQuotedText) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->InsertAsQuotation(aQuotedText); -#endif // ENABLE_JS_EDITOR_LOG - // Now we have the text. Cite it appropriately: nsCOMPtr citer; nsCOMPtr prefs; @@ -2908,13 +2711,6 @@ NS_IMETHODIMP nsHTMLEditor::InsertAsPlaintextQuotation(const nsString& aQuotedTe NS_IMETHODIMP nsHTMLEditor::InsertAsCitedQuotation(const nsString& aQuotedText, const nsString& aCitation) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->InsertAsCitedQuotation(aQuotedText, aCitation); -#endif // ENABLE_JS_EDITOR_LOG - nsAutoEditBatch beginBatching(this); nsCOMPtr newNode; nsAutoString tag("blockquote"); @@ -3020,13 +2816,6 @@ nsHTMLEditor::GetEmbeddedObjects(nsISupportsArray** aNodeList) NS_IMETHODIMP nsHTMLEditor::Cut() { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->Cut(); -#endif // ENABLE_JS_EDITOR_LOG - nsCOMPtr selection; nsresult res = mPresShell->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); if (!NS_SUCCEEDED(res)) @@ -3044,13 +2833,6 @@ NS_IMETHODIMP nsHTMLEditor::Cut() NS_IMETHODIMP nsHTMLEditor::Copy() { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->Copy(); -#endif // ENABLE_JS_EDITOR_LOG - //printf("nsEditor::Copy\n"); return mPresShell->DoCopy(); @@ -3058,13 +2840,6 @@ NS_IMETHODIMP nsHTMLEditor::Copy() NS_IMETHODIMP nsHTMLEditor::Paste() { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->Paste(); -#endif // ENABLE_JS_EDITOR_LOG - nsString stuffToPaste; // Get Clipboard Service diff --git a/mozilla/editor/base/nsHTMLEditorLog.cpp b/mozilla/editor/base/nsHTMLEditorLog.cpp new file mode 100644 index 00000000000..26dc9a0b21d --- /dev/null +++ b/mozilla/editor/base/nsHTMLEditorLog.cpp @@ -0,0 +1,1211 @@ +/* -*- 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 +#include "nsIPresShell.h" +#include "nsIDOMElement.h" +#include "nsIDOMAttr.h" +#include "nsIDOMNode.h" +#include "nsIDOMNodeList.h" +#include "nsIDOMCharacterData.h" +#include "nsIDOMNamedNodeMap.h" +#include "nsIDOMSelection.h" +#include "nsIDOMRange.h" +#include "nsHTMLEditorLog.h" +#include "nsCOMPtr.h" + +#include "nsEditorTxnLog.h" + +#define LOCK_LOG(doc) +#define UNLOCK_LOG(doc) + +static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID); + +nsHTMLEditorLog::nsHTMLEditorLog() +{ + mRefCnt = 0; + mLocked = -1; + mEditorTxnLog = 0; +} + +nsHTMLEditorLog::~nsHTMLEditorLog() +{ + StopLogging(); +} + +NS_IMPL_ADDREF_INHERITED(nsHTMLEditorLog, nsHTMLEditor); +NS_IMPL_RELEASE_INHERITED(nsHTMLEditorLog, nsHTMLEditor); + +NS_IMETHODIMP +nsHTMLEditorLog::QueryInterface(REFNSIID aIID, void** aInstancePtr) +{ + if (!aInstancePtr) + return NS_ERROR_NULL_POINTER; + + *aInstancePtr = nsnull; + + if (aIID.Equals(nsIEditorLogging::GetIID())) { + *aInstancePtr = (void*)(nsIEditorLogging*)this; + NS_ADDREF_THIS(); + return NS_OK; + } + + return nsHTMLEditor::QueryInterface(aIID, aInstancePtr); +} + +NS_IMETHODIMP +nsHTMLEditorLog::SetInlineProperty(nsIAtom *aProperty, const nsString *aAttribute, const nsString *aValue) +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + nsAutoString propStr; + + aProperty->ToString(propStr); + + PrintSelection(); + Write("window.editorShell.SetTextProperty(\""); + PrintUnicode(propStr); + Write("\", \""); + if (aAttribute) + PrintUnicode(*aAttribute); + Write("\", \""); + if (aValue) + PrintUnicode(*aValue); + Write("\");\n"); + + Flush(); + } + + return nsHTMLEditor::SetInlineProperty(aProperty, aAttribute, aValue); +} + +NS_IMETHODIMP +nsHTMLEditorLog::SetParagraphFormat(const nsString& aParagraphFormat) +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + PrintSelection(); + Write("window.editorShell.paragraphFormat = \""); + PrintUnicode(aParagraphFormat); + Write("\";\n"); + + Flush(); + } + + return nsHTMLEditor::SetParagraphFormat(aParagraphFormat); +} + +NS_IMETHODIMP +nsHTMLEditorLog::RemoveInlineProperty(nsIAtom *aProperty, const nsString *aAttribute) +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + nsAutoString propStr; + + aProperty->ToString(propStr); + + PrintSelection(); + Write("window.editorShell.RemoveTextProperty(\""); + PrintUnicode(propStr); + Write("\", \""); + if (aAttribute) + PrintUnicode(*aAttribute); + Write("\");\n"); + + Flush(); + } + + return nsHTMLEditor::RemoveInlineProperty(aProperty, aAttribute); +} + +NS_IMETHODIMP +nsHTMLEditorLog::DeleteSelection(nsIEditor::ESelectionCollapseDirection aAction) +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + PrintSelection(); + Write("window.editorShell.DeleteSelection("); + WriteInt("%d", aAction); + Write(");\n"); + + Flush(); + } + + return nsHTMLEditor::DeleteSelection(aAction); +} + +NS_IMETHODIMP +nsHTMLEditorLog::InsertText(const nsString& aStringToInsert) +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + PrintSelection(); + + Write("window.editorShell.InsertText(\""); + PrintUnicode(aStringToInsert); + Write("\");\n"); + + Flush(); + } + + return nsHTMLEditor::InsertText(aStringToInsert); +} + +NS_IMETHODIMP +nsHTMLEditorLog::InsertBreak() +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + PrintSelection(); + Write("window.editorShell.InsertBreak();\n"); + Flush(); + } + + return nsHTMLEditor::InsertBreak(); +} + +NS_IMETHODIMP +nsHTMLEditorLog::Undo(PRUint32 aCount) +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + Write("window.editorShell.Undo();\n"); + Flush(); + } + + return nsHTMLEditor::Undo(aCount); +} + +NS_IMETHODIMP +nsHTMLEditorLog::Redo(PRUint32 aCount) +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + Write("window.editorShell.Redo();\n"); + Flush(); + } + + return nsHTMLEditor::Redo(aCount); +} + +NS_IMETHODIMP +nsHTMLEditorLog::BeginTransaction() +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + Write("window.editorShell.BeginBatchChanges();\n"); + Flush(); + } + + return nsHTMLEditor::BeginTransaction(); +} + +NS_IMETHODIMP +nsHTMLEditorLog::EndTransaction() +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + Write("window.editorShell.EndBatchChanges();\n"); + Flush(); + } + + return nsHTMLEditor::EndTransaction(); +} + +NS_IMETHODIMP +nsHTMLEditorLog::SelectAll() +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + Write("window.editorShell.SelectAll();\n"); + Flush(); + } + + return nsHTMLEditor::SelectAll(); +} + +NS_IMETHODIMP +nsHTMLEditorLog::BeginningOfDocument() +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + Write("window.editorShell.BeginningOfDocument();\n"); + Flush(); + } + + return nsHTMLEditor::BeginningOfDocument(); +} + +NS_IMETHODIMP +nsHTMLEditorLog::EndOfDocument() +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + Write("window.editorShell.EndOfDocument();\n"); + Flush(); + } + + return nsHTMLEditor::EndOfDocument(); +} + +NS_IMETHODIMP +nsHTMLEditorLog::Cut() +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + PrintSelection(); + Write("window.editorShell.Cut();\n"); + Flush(); + } + + return nsHTMLEditor::Cut(); +} + +NS_IMETHODIMP +nsHTMLEditorLog::Copy() +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + PrintSelection(); + Write("window.editorShell.Copy();\n"); + Flush(); + } + + return nsHTMLEditor::Copy(); +} + +NS_IMETHODIMP +nsHTMLEditorLog::Paste() +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + PrintSelection(); + Write("window.editorShell.Paste();\n"); + Flush(); + } + + return nsHTMLEditor::Paste(); +} + +NS_IMETHODIMP +nsHTMLEditorLog::PasteAsQuotation() +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + PrintSelection(); + Write("window.editorShell.PasteAsQuotation();\n"); + Flush(); + } + + return nsHTMLEditor::PasteAsQuotation(); +} + +NS_IMETHODIMP +nsHTMLEditorLog::PasteAsPlaintextQuotation() +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + PrintSelection(); + Write("window.editorShell.PasteAsQuotation();\n"); + Flush(); + } + + return nsHTMLEditor::PasteAsPlaintextQuotation(); +} + +NS_IMETHODIMP +nsHTMLEditorLog::PasteAsCitedQuotation(const nsString& aCitation) +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + PrintSelection(); + Write("window.editorShell.PasteAsCitedQuotation(\""); + PrintUnicode(aCitation); + Write("\");\n"); + Flush(); + } + + return nsHTMLEditor::PasteAsCitedQuotation(aCitation); +} + +NS_IMETHODIMP +nsHTMLEditorLog::InsertAsQuotation(const nsString& aQuotedText) +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + PrintSelection(); + Write("window.editorShell.InsertAsQuotation(\""); + PrintUnicode(aQuotedText); + Write("\");\n"); + Flush(); + } + + return nsHTMLEditor::InsertAsQuotation(aQuotedText); +} + +NS_IMETHODIMP +nsHTMLEditorLog::InsertAsPlaintextQuotation(const nsString& aQuotedText) +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + PrintSelection(); + Write("window.editorShell.InsertAsQuotation(\""); + PrintUnicode(aQuotedText); + Write("\");\n"); + Flush(); + } + + return nsHTMLEditor::InsertAsPlaintextQuotation(aQuotedText); +} + +NS_IMETHODIMP +nsHTMLEditorLog::InsertAsCitedQuotation(const nsString& aQuotedText, const nsString& aCitation) +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + PrintSelection(); + Write("window.editorShell.InsertAsCitedQuotation(\""); + PrintUnicode(aQuotedText); + Write("\", \""); + PrintUnicode(aCitation); + Write("\");\n"); + Flush(); + } + + return nsHTMLEditor::InsertAsCitedQuotation(aQuotedText, aCitation); +} + +NS_IMETHODIMP +nsHTMLEditorLog::SetBackgroundColor(const nsString& aColor) +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + Write("window.editorShell.SetBackgroundColor(\""); + PrintUnicode(aColor); + Write("\");\n"); + Flush(); + } + + return nsHTMLEditor::SetBackgroundColor(aColor); +} + +NS_IMETHODIMP +nsHTMLEditorLog::SetBodyAttribute(const nsString& aAttr, const nsString& aValue) +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + Write("window.editorShell.SetBodyAttribute(\""); + PrintUnicode(aAttr); + Write("\", \""); + PrintUnicode(aValue); + Write("\");\n"); + Flush(); + } + + return nsHTMLEditor::SetBodyAttribute(aAttr, aValue); +} + +NS_IMETHODIMP +nsHTMLEditorLog::InsertList(const nsString& aListType) +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + PrintSelection(); + + Write("window.editorShell.InsertList(\""); + PrintUnicode(aListType); + Write("\");\n"); + Flush(); + } + + return nsHTMLEditor::InsertList(aListType); +} + +NS_IMETHODIMP +nsHTMLEditorLog::Indent(const nsString& aIndent) +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + PrintSelection(); + + Write("window.editorShell.Indent(\""); + PrintUnicode(aIndent); + Write("\");\n"); + Flush(); + } + + return nsHTMLEditor::Indent(aIndent); +} + +NS_IMETHODIMP +nsHTMLEditorLog::Align(const nsString& aAlign) +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + PrintSelection(); + + Write("window.editorShell.Align(\""); + PrintUnicode(aAlign); + Write("\");\n"); + Flush(); + } + + return nsHTMLEditor::Align(aAlign); +} + +NS_IMETHODIMP +nsHTMLEditorLog::InsertElement(nsIDOMElement* aElement, PRBool aDeleteSelection) +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + if (!aElement) + return NS_ERROR_NULL_POINTER; + + nsCOMPtr node = do_QueryInterface(aElement); + + if (!node) + return NS_ERROR_NULL_POINTER; + + PrintSelection(); + PrintNode(node, 0); + Write("window.editorShell.InsertElement(n0, "); + Write(aDeleteSelection ? "true" : "false"); + Write(");\n"); + Flush(); + } + + return nsHTMLEditor::InsertElement(aElement, aDeleteSelection); +} + +NS_IMETHODIMP +nsHTMLEditorLog::InsertLinkAroundSelection(nsIDOMElement* aAnchorElement) +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + if (!aAnchorElement) + return NS_ERROR_NULL_POINTER; + + nsCOMPtr node = do_QueryInterface(aAnchorElement); + + if (!node) + return NS_ERROR_NULL_POINTER; + + PrintSelection(); + PrintNode(node, 0); + Write("window.editorShell.InsertLinkAroundSelection(n0);\n"); + Flush(); + } + + return nsHTMLEditor::InsertLinkAroundSelection(aAnchorElement); +} + +NS_IMETHODIMP +nsHTMLEditorLog::ApplyStyleSheet(const nsString& aURL) +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + Write("window.editorShell.ApplyStyleSheet(\""); + PrintUnicode(aURL); + Write("\");\n"); + + Flush(); + } + + return nsHTMLEditor::ApplyStyleSheet(aURL); +} + +NS_IMETHODIMP +nsHTMLEditorLog::StartLogging(nsIFileSpec *aLogFile) +{ + nsresult result = NS_ERROR_FAILURE; + + if (!aLogFile) + return NS_ERROR_NULL_POINTER; + + if (mFileSpec) + { + result = StopLogging(); + + if (NS_FAILED(result)) + return result; + } + + mFileSpec = do_QueryInterface(aLogFile); + + if (!mFileSpec) + result = NS_ERROR_NULL_POINTER; + + result = mFileSpec->openStreamForWriting(); + + if (NS_FAILED(result)) + { + mFileSpec = nsCOMPtr(); + return result; + } + + if (mTxnMgr) + { + mEditorTxnLog = new nsEditorTxnLog(this); + + if (mEditorTxnLog) + { + NS_ADDREF(mEditorTxnLog); + mTxnMgr->AddListener(mEditorTxnLog); + } + else + return NS_ERROR_OUT_OF_MEMORY; + } + + return NS_OK; +} + +NS_IMETHODIMP +nsHTMLEditorLog::StopLogging() +{ + if (mTxnMgr && mEditorTxnLog) + mTxnMgr->RemoveListener(mEditorTxnLog); + + if (mEditorTxnLog) + { + NS_RELEASE(mEditorTxnLog); + mEditorTxnLog = 0; + } + + if (mFileSpec) + { + mFileSpec->closeStream(); + mFileSpec = nsCOMPtr(); + } + + return NS_OK; +} + + +nsresult +nsHTMLEditorLog::Write(const char *aBuffer) +{ + nsresult result; + + if (!aBuffer) + return NS_ERROR_NULL_POINTER; + + PRInt32 len = strlen(aBuffer); + + if (mFileSpec) + { + PRInt32 retval; + + result = mFileSpec->write(aBuffer, len, &retval); + + if (NS_FAILED(result)) + return result; + +#ifdef VERY_SLOW + + result = mFileSpec->flush(); + + if (NS_FAILED(result)) + return result; + +#endif // VERY_SLOW + } + else + fwrite(aBuffer, 1, len, stdout); + + return NS_OK; +} + +nsresult +nsHTMLEditorLog::WriteInt(const char *aFormat, PRInt32 aInt) +{ + if (!aFormat) + return NS_ERROR_NULL_POINTER; + + char buf[256]; + + sprintf(buf, aFormat, aInt); + + return Write(buf); +} + +nsresult +nsHTMLEditorLog::Flush() +{ + nsresult result = NS_OK; + + if (mFileSpec) + result = mFileSpec->flush(); + else + fflush(stdout); + + return result; +} + +nsresult +nsHTMLEditorLog::PrintUnicode(const nsString &aString) +{ + const PRUnichar *uc = aString.GetUnicode(); + PRInt32 i, len = aString.Length(); + char buf[10]; + + for (i = 0; i < len; i++) + { + if (nsString::IsAlpha(uc[i]) || nsString::IsDigit(uc[i]) || uc[i] == ' ') + sprintf(buf, "%c", uc[i]); + else + sprintf(buf, "\\u%.4x", uc[i]); + + nsresult result = Write(buf); + + if (NS_FAILED(result)) + return result; + } + + return NS_OK; +} + +nsresult +nsHTMLEditorLog::PrintSelection() +{ + nsCOMPtr selection; + nsresult result; + PRInt32 rangeCount; + + result = GetSelection(getter_AddRefs(selection)); + + if (NS_FAILED(result)) + return result; + + result = selection->GetRangeCount(&rangeCount); + + if (NS_FAILED(result)) + return result; + + Write("selRanges = [ "); + + PRInt32 i, j; + nsCOMPtr range; + nsCOMPtr startNode; + nsCOMPtr endNode; + PRInt32 startOffset, endOffset; + + for (i = 0; i < rangeCount; i++) + { + result = selection->GetRangeAt(i, getter_AddRefs(range)); + + if (NS_FAILED(result)) + return result; + + result = range->GetStartParent(getter_AddRefs(startNode)); + + if (NS_FAILED(result)) + return result; + + if (!startNode) + return NS_ERROR_NULL_POINTER; + + result = range->GetStartOffset(&startOffset); + + if (NS_FAILED(result)) + return result; + + result = range->GetEndParent(getter_AddRefs(endNode)); + + if (NS_FAILED(result)) + return result; + + if (!endNode) + return NS_ERROR_NULL_POINTER; + + result = range->GetEndOffset(&endOffset); + + if (NS_FAILED(result)) + return result; + + PRInt32 *offsetArray = 0; + PRInt32 arrayLength = 0; + + result = GetNodeTreeOffsets(startNode, &offsetArray, &arrayLength); + + if (NS_FAILED(result)) + return result; + + if (i != 0) + Write(",\n "); + + Write("[ [["); + + for (j = 0; j < arrayLength; j++) + { + if (j != 0) + Write(", "); + WriteInt("%d", offsetArray[j]); + } + + Write("], "); + WriteInt("%3d", startOffset); + Write("], "); + + if (startNode != endNode) + { + delete []offsetArray; + offsetArray = 0; + arrayLength = 0; + + result = GetNodeTreeOffsets(endNode, &offsetArray, &arrayLength); + + if (NS_FAILED(result)) + return result; + } + + Write("[["); + + for (j = 0; j < arrayLength; j++) + { + if (j != 0) + Write(", "); + WriteInt("%d", offsetArray[j]); + } + + delete []offsetArray; + + Write("], "); + WriteInt("%3d", endOffset); + Write("] ]"); + } + + Write(" ];\nEditorSetSelectionFromOffsets(selRanges);\n"); + + Flush(); + + return NS_OK; +} + +nsresult +nsHTMLEditorLog::PrintElementNode(nsIDOMNode *aNode, PRInt32 aDepth) +{ + nsresult result; + nsAutoString tag, name, value; + nsCOMPtr ele = do_QueryInterface(aNode); + nsCOMPtr map; + + if (!ele) + return NS_ERROR_NULL_POINTER; + + result = ele->GetTagName(tag); + + if (NS_FAILED(result)) + return result; + + Write("n"); + WriteInt("%d", aDepth); + Write(" = window.editorShell.editorDocument.createElement(\""); + PrintUnicode(tag); + Write("\");\n"); + + result = aNode->GetAttributes(getter_AddRefs(map)); + + if (NS_FAILED(result)) + return result; + + if (!map) + return NS_ERROR_NULL_POINTER; + + PRUint32 i, len; + nsCOMPtr attr; + + result = map->GetLength(&len); + + if (NS_FAILED(result)) + return result; + + for (i = 0; i < len; i++) + { + result = map->Item(i, getter_AddRefs(attr)); + + if (NS_FAILED(result)) + return result; + + if (!attr) + return NS_ERROR_NULL_POINTER; + + result = PrintAttributeNode(attr, aDepth); + + if (NS_FAILED(result)) + return result; + } + + result = PrintNodeChildren(aNode, aDepth); + + return result; +} + +nsresult +nsHTMLEditorLog::PrintAttributeNode(nsIDOMNode *aNode, PRInt32 aDepth) +{ + nsresult result; + nsCOMPtr attr = do_QueryInterface(aNode); + + if (!attr) + return NS_ERROR_NULL_POINTER; + + nsAutoString str; + + result = attr->GetName(str); + + if (NS_FAILED(result)) + return result; + + Write("a"); + WriteInt("%d", aDepth); + Write(" = window.editorShell.editorDocument.createAttribute(\""); + PrintUnicode(str); + Write("\");\n"); + + result = attr->GetValue(str); + + if (NS_FAILED(result)) + return result; + + Write("a"); + WriteInt("%d", aDepth); + Write(".value = \""); + PrintUnicode(str); + Write("\";\n"); + + Write("n"); + WriteInt("%d", aDepth); + Write(".setAttributeNode(a"); + WriteInt("%d", aDepth); + Write(");\n"); + + return NS_OK; +} + +nsresult +nsHTMLEditorLog::PrintNodeChildren(nsIDOMNode *aNode, PRInt32 aDepth) +{ + nsresult result; + + if (!aNode) + return NS_ERROR_NULL_POINTER; + + nsCOMPtr list; + + result = aNode->GetChildNodes(getter_AddRefs(list)); + + if (NS_FAILED(result)) + return result; + + if (!list) + { + // Must not have any children! + return NS_OK; + } + + PRUint32 i, len; + nsCOMPtr node; + + result = list->GetLength(&len); + + if (NS_FAILED(result)) + return result; + + for (i = 0; i < len; i++) + { + result = list->Item(i, getter_AddRefs(node)); + + if (NS_FAILED(result)) + return result; + + result = PrintNode(node, aDepth + 1); + + if (NS_FAILED(result)) + return result; + + Write("n"); + WriteInt("%d", aDepth); + Write(".appendChild(n"); + WriteInt("%d", aDepth+1); + Write(");\n"); + } + + return NS_OK; +} + +nsresult +nsHTMLEditorLog::PrintTextNode(nsIDOMNode *aNode, PRInt32 aDepth) +{ + nsresult result; + + nsCOMPtr cd = do_QueryInterface(aNode); + + if (!cd) + return NS_ERROR_NULL_POINTER; + + nsAutoString str; + + result = cd->GetData(str); + + if (NS_FAILED(result)) + return result; + + Write("n"); + WriteInt("%d", aDepth); + Write(" = window.editorShell.editorDocument.createTextNode(\""); + PrintUnicode(str); + Write("\");\n"); + + return NS_OK; +} + +nsresult +nsHTMLEditorLog::PrintNode(nsIDOMNode *aNode, PRInt32 aDepth) +{ + nsresult result = NS_OK; + + if (!aNode) + return NS_ERROR_NULL_POINTER; + + PRUint16 nodeType; + + result = aNode->GetNodeType(&nodeType); + + switch (nodeType) + { + case nsIDOMNode::ELEMENT_NODE: + result = PrintElementNode(aNode, aDepth); + break; + case nsIDOMNode::TEXT_NODE: + result = PrintTextNode(aNode, aDepth); + break; + case nsIDOMNode::ATTRIBUTE_NODE: + result = PrintAttributeNode(aNode, aDepth); + break; + case nsIDOMNode::CDATA_SECTION_NODE: + case nsIDOMNode::ENTITY_REFERENCE_NODE: + case nsIDOMNode::ENTITY_NODE: + case nsIDOMNode::PROCESSING_INSTRUCTION_NODE: + case nsIDOMNode::COMMENT_NODE: + case nsIDOMNode::DOCUMENT_NODE: + case nsIDOMNode::DOCUMENT_TYPE_NODE: + case nsIDOMNode::DOCUMENT_FRAGMENT_NODE: + case nsIDOMNode::NOTATION_NODE: + default: + break; + } + + Flush(); + + return result; +} + +nsresult +nsHTMLEditorLog::GetNodeTreeOffsets(nsIDOMNode *aNode, PRInt32 **aResult, PRInt32 *aLength) +{ + nsresult result; + + if (!aNode || !aResult || !aLength) + return NS_ERROR_NULL_POINTER; + + *aResult = 0; + *aLength = 0; + + nsIDOMNode *parent = aNode; + PRInt32 i = 0; + + // Count the number of parent nodes above aNode. + + while (parent) + { + result = parent->GetParentNode(&parent); + + if (NS_FAILED(result)) + return result; + + if (parent) + ++i; + } + + // Allocate an array big enough to hold all the offsets. + + *aResult = new PRInt32[i]; + + if (!aResult) + return NS_ERROR_OUT_OF_MEMORY; + + *aLength = i; + + while (aNode && i > 0) + { + PRInt32 offset = 0; + + result = aNode->GetParentNode(&parent); + + if (NS_FAILED(result)) + { + delete [](*aResult); + *aResult = 0; + *aLength = 0; + return result; + } + + while (aNode) + { + result = aNode->GetPreviousSibling(&aNode); + + if (NS_FAILED(result)) + { + delete [](*aResult); + *aResult = 0; + *aLength = 0; + return result; + } + + if (aNode) + ++offset; + } + + (*aResult)[--i] = offset; + aNode = parent; + } + + return NS_OK; +} + +nsresult +nsHTMLEditorLog::Lock() +{ + mLocked++; + return NS_OK; +} + +nsresult +nsHTMLEditorLog::Unlock() +{ + --mLocked; + return NS_OK; +} + +#ifdef NEVER_ENABLE_THIS_JAVASCRIPT + +function EditorGetNodeAtOffsets(offsets) +{ + var node = null; + var i; + + node = window.editorShell.editorDocument; + + for (i = 0; i < offsets.length; i++) + { + node = node.childNodes[offsets[i]]; + } + + return node; +} + +function EditorSetSelectionFromOffsets(selRanges) +{ + var rangeArr, start, end, i, node, offset; + var selection = window.editorShell.editorSelection; + + selection.clearSelection(); + + for (i = 0; i < selRanges.length; i++) + { + rangeArr = selRanges[i]; + start = rangeArr[0]; + end = rangeArr[1]; + + var range = window.editorShell.editorDocument.createRange(); + + node = EditorGetNodeAtOffsets(start[0]); + offset = start[1]; + + range.setStart(node, offset); + + node = EditorGetNodeAtOffsets(end[0]); + offset = end[1]; + + range.setEnd(node, offset); + + selection.addRange(range); + } +} + +#endif diff --git a/mozilla/editor/base/nsHTMLEditorLog.h b/mozilla/editor/base/nsHTMLEditorLog.h new file mode 100644 index 00000000000..772354609c4 --- /dev/null +++ b/mozilla/editor/base/nsHTMLEditorLog.h @@ -0,0 +1,127 @@ +/* -*- 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://wwwt.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 nsHTMLEditorLog_h__ +#define nsHTMLEditorLog_h__ + +#include "nsHTMLEditor.h" +#include "nsIEditorLogging.h" +#include "nsIFileSpec.h" +#include "nsCOMPtr.h" + +class nsEditorTxnLog; + +/** implementation of a transaction listener object. + * + */ +class nsHTMLEditorLog : public nsHTMLEditor, + public nsIEditorLogging +{ +private: + + nsCOMPtr mFileSpec; + nsEditorTxnLog *mEditorTxnLog; + PRInt32 mLocked; + PRInt32 mDepth; + +public: + + // Interfaces for AddRef, Release, and QueryInterface. + NS_DECL_ISUPPORTS_INHERITED + + nsHTMLEditorLog(); + virtual ~nsHTMLEditorLog(); + + /* nsIHTMLEditor method implementations. */ + NS_IMETHOD SetInlineProperty(nsIAtom *aProperty, + const nsString *aAttribute, + const nsString *aValue); + NS_IMETHOD SetParagraphFormat(const nsString& aParagraphFormat); + NS_IMETHOD RemoveInlineProperty(nsIAtom *aProperty, const nsString *aAttribute); + NS_IMETHOD DeleteSelection(nsIEditor::ESelectionCollapseDirection aAction); + NS_IMETHOD InsertText(const nsString& aStringToInsert); + NS_IMETHOD InsertBreak(); + NS_IMETHOD Undo(PRUint32 aCount); + NS_IMETHOD Redo(PRUint32 aCount); + NS_IMETHOD BeginTransaction(); + NS_IMETHOD EndTransaction(); + NS_IMETHOD SelectAll(); + NS_IMETHOD BeginningOfDocument(); + NS_IMETHOD EndOfDocument(); + + NS_IMETHOD Cut(); + NS_IMETHOD Copy(); + NS_IMETHOD Paste(); + NS_IMETHOD PasteAsQuotation(); + NS_IMETHOD PasteAsPlaintextQuotation(); + NS_IMETHOD PasteAsCitedQuotation(const nsString& aCitation); + NS_IMETHOD InsertAsQuotation(const nsString& aQuotedText); + NS_IMETHOD InsertAsPlaintextQuotation(const nsString& aQuotedText); + NS_IMETHOD InsertAsCitedQuotation(const nsString& aQuotedText, const nsString& aCitation); + + NS_IMETHOD ApplyStyleSheet(const nsString& aURL); + + NS_IMETHOD SetBackgroundColor(const nsString& aColor); + NS_IMETHOD SetBodyAttribute(const nsString& aAttr, const nsString& aValue); + NS_IMETHOD InsertList(const nsString& aListType); + NS_IMETHOD Indent(const nsString& aIndent); + NS_IMETHOD Align(const nsString& aAlign); + NS_IMETHOD InsertElement(nsIDOMElement* aElement, PRBool aDeleteSelection); + NS_IMETHOD InsertLinkAroundSelection(nsIDOMElement* aAnchorElement); + + NS_IMETHOD StartLogging(nsIFileSpec *aLogFile); + NS_IMETHOD StopLogging(); + + /* nsHTMLEditorLog public methods. */ + nsresult Write(const char *aBuffer); + nsresult WriteInt(const char *aFormat, PRInt32 aInt); + nsresult Flush(); + nsresult PrintUnicode(const nsString &aString); + nsresult PrintSelection(); + nsresult PrintNode(nsIDOMNode *aNode, PRInt32 aDepth=0); + nsresult PrintElementNode(nsIDOMNode *aNode, PRInt32 aDepth); + nsresult PrintTextNode(nsIDOMNode *aNode, PRInt32 aDepth); + nsresult PrintAttributeNode(nsIDOMNode *aNode, PRInt32 aDepth=0); + nsresult PrintNodeChildren(nsIDOMNode *aNode, PRInt32 aDepth=0); + nsresult GetNodeTreeOffsets(nsIDOMNode *aNode, PRInt32 **aResult, PRInt32 *aLength); + nsresult Lock(); + nsresult Unlock(); +}; + +class nsAutoHTMLEditorLogLock +{ + nsHTMLEditorLog *mLog; + +public: + + nsAutoHTMLEditorLogLock(nsHTMLEditorLog *aLog) + { + mLog = aLog; + + if (mLog) + mLog->Lock(); + } + + ~nsAutoHTMLEditorLogLock() + { + if (mLog) + mLog->Unlock(); + } +}; + +#endif // nsHTMLEditorLog_h__ diff --git a/mozilla/editor/libeditor/base/nsEditor.cpp b/mozilla/editor/libeditor/base/nsEditor.cpp index 9196164a4ba..01adf94ad49 100644 --- a/mozilla/editor/libeditor/base/nsEditor.cpp +++ b/mozilla/editor/libeditor/base/nsEditor.cpp @@ -16,7 +16,6 @@ * Reserved. */ -#include "nsVector.h" #include "nsVoidArray.h" #include "nsIDOMDocument.h" @@ -65,11 +64,6 @@ #include "nsIContentIterator.h" #include "nsLayoutCID.h" -#ifdef ENABLE_JS_EDITOR_LOG -#include "nsJSEditorLog.h" -#include "nsJSTxnLog.h" -#endif // ENABLE_JS_EDITOR_LOG - // transactions the editor knows how to build #include "TransactionFactory.h" #include "EditAggregateTxn.h" @@ -156,10 +150,6 @@ nsEditor::nsEditor() , mDoc(nsnull) , mPrefs(nsnull) , mDocCharset("ISO-8859-1") -#ifdef ENABLE_JS_EDITOR_LOG -, mJSEditorLog(nsnull) -, mJSTxnLog(nsnull) -#endif // ENABLE_JS_EDITOR_LOG { //initialize member variables here NS_INIT_REFCNT(); @@ -188,12 +178,6 @@ nsEditor::~nsEditor() mActionListeners = 0; } -#ifdef ENABLE_JS_EDITOR_LOG - - StopLogging(); - -#endif // ENABLE_JS_EDITOR_LOG - // Release service pointers if (mPrefs) nsServiceManager::ReleaseService(kPrefCID, mPrefs); @@ -231,11 +215,6 @@ nsEditor::QueryInterface(REFNSIID aIID, void** aInstancePtr) NS_ADDREF_THIS(); return NS_OK; } - if (aIID.Equals(nsIEditorLogging::GetIID())) { - *aInstancePtr = (void*)(nsIEditorLogging*)this; - NS_ADDREF_THIS(); - return NS_OK; - } return NS_NOINTERFACE; } @@ -444,13 +423,6 @@ NS_IMETHODIMP nsEditor::SaveDocument(PRBool saveAs, PRBool saveCopy) NS_IMETHODIMP nsEditor::Save() { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->Save(); -#endif // ENABLE_JS_EDITOR_LOG - nsresult rv = SaveDocument(PR_FALSE, PR_FALSE); if (NS_FAILED(rv)) return rv; @@ -461,13 +433,6 @@ NS_IMETHODIMP nsEditor::Save() NS_IMETHODIMP nsEditor::SaveAs(PRBool aSavingCopy) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->SaveAs(aSavingCopy); -#endif // ENABLE_JS_EDITOR_LOG - return SaveDocument(PR_TRUE, aSavingCopy); } @@ -536,13 +501,6 @@ nsEditor::EnableUndo(PRBool aEnable) NS_IMETHODIMP nsEditor::Undo(PRUint32 aCount) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->Undo(aCount); -#endif // ENABLE_JS_EDITOR_LOG - if (gNoisy) { printf("Editor::Undo ----------\n"); } nsresult result = NS_OK; @@ -588,13 +546,6 @@ NS_IMETHODIMP nsEditor::CanUndo(PRBool &aIsEnabled, PRBool &aCanUndo) NS_IMETHODIMP nsEditor::Redo(PRUint32 aCount) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->Redo(aCount); -#endif // ENABLE_JS_EDITOR_LOG - if (gNoisy) { printf("Editor::Redo ----------\n"); } nsresult result = NS_OK; @@ -640,13 +591,6 @@ NS_IMETHODIMP nsEditor::CanRedo(PRBool &aIsEnabled, PRBool &aCanRedo) NS_IMETHODIMP nsEditor::BeginTransaction() { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->BeginTransaction(); -#endif // ENABLE_JS_EDITOR_LOG - BeginUpdateViewBatch(); if ((nsITransactionManager *)nsnull!=mTxnMgr.get()) @@ -660,13 +604,6 @@ nsEditor::BeginTransaction() NS_IMETHODIMP nsEditor::EndTransaction() { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->EndTransaction(); -#endif // ENABLE_JS_EDITOR_LOG - if ((nsITransactionManager *)nsnull!=mTxnMgr.get()) { mTxnMgr->EndBatch(); @@ -680,13 +617,6 @@ nsEditor::EndTransaction() // XXX: the rule system should tell us which node to select all on (ie, the root, or the body) NS_IMETHODIMP nsEditor::SelectAll() { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->SelectAll(); -#endif // ENABLE_JS_EDITOR_LOG - if (!mDoc || !mPresShell) { return NS_ERROR_NOT_INITIALIZED; } nsCOMPtr selection; @@ -700,13 +630,6 @@ NS_IMETHODIMP nsEditor::SelectAll() NS_IMETHODIMP nsEditor::BeginningOfDocument() { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->BeginningOfDocument(); -#endif // ENABLE_JS_EDITOR_LOG - if (!mDoc || !mPresShell) { return NS_ERROR_NOT_INITIALIZED; } nsCOMPtr selection; @@ -740,13 +663,6 @@ NS_IMETHODIMP nsEditor::BeginningOfDocument() NS_IMETHODIMP nsEditor::EndOfDocument() { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->EndOfDocument(); -#endif // ENABLE_JS_EDITOR_LOG - if (!mDoc || !mPresShell) { return NS_ERROR_NOT_INITIALIZED; } nsCOMPtr selection; @@ -1322,67 +1238,6 @@ nsEditor::SetCompositionString(const nsString& aCompositionString, nsIPrivateTex return result; } -#ifdef XP_MAC -#pragma mark - -#pragma mark --- nsIEditorLogging --- -#pragma mark - -#endif - - -NS_IMETHODIMP -nsEditor::StartLogging(nsIFileSpec *aLogFile) -{ -#ifdef ENABLE_JS_EDITOR_LOG - - mJSEditorLog = new nsJSEditorLog(this, aLogFile); - - if (!mJSEditorLog) - return NS_ERROR_OUT_OF_MEMORY; - - if (mTxnMgr) - { - mJSTxnLog = new nsJSTxnLog(mJSEditorLog); - - if (mJSTxnLog) - { - NS_ADDREF(mJSTxnLog); - mTxnMgr->AddListener(mJSTxnLog); - } - else - return NS_ERROR_OUT_OF_MEMORY; - } - -#endif // ENABLE_JS_EDITOR_LOG - - return NS_OK; -} - -NS_IMETHODIMP -nsEditor::StopLogging() -{ -#ifdef ENABLE_JS_EDITOR_LOG - - if (mTxnMgr && mJSTxnLog) - mTxnMgr->RemoveListener(mJSTxnLog); - - if (mJSTxnLog) - { - NS_RELEASE(mJSTxnLog); - mJSTxnLog = 0; - } - - if (mJSEditorLog) - { - delete mJSEditorLog; - mJSEditorLog = 0; - } - -#endif // ENABLE_JS_EDITOR_LOG - - return NS_OK; -} - - #ifdef XP_MAC #pragma mark - @@ -1541,14 +1396,6 @@ nsString& nsEditor::GetTextNodeTag() NS_IMETHODIMP nsEditor::InsertTextImpl(const nsString& aStringToInsert) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->InsertText(aStringToInsert); - -#endif // ENABLE_JS_EDITOR_LOG - EditAggregateTxn *aggTxn = nsnull; // Create the "delete current selection" txn nsresult result = CreateAggregateTxnForDeleteSelection(InsertTextTxn::gInsertTextTxnName, &aggTxn); @@ -3844,13 +3691,6 @@ nsresult nsEditor::EndUpdateViewBatch() NS_IMETHODIMP nsEditor::DeleteSelectionImpl(ESelectionCollapseDirection aAction) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->DeleteSelection(aAction); -#endif // ENABLE_JS_EDITOR_LOG - nsresult result; EditAggregateTxn *txn; diff --git a/mozilla/editor/libeditor/base/nsEditor.h b/mozilla/editor/libeditor/base/nsEditor.h index 1bd8e48b70c..82420966c53 100644 --- a/mozilla/editor/libeditor/base/nsEditor.h +++ b/mozilla/editor/libeditor/base/nsEditor.h @@ -24,7 +24,6 @@ #include "nsIEditor.h" #include "nsIEditorIMESupport.h" -#include "nsIEditorLogging.h" #include "nsIDOMDocument.h" #include "nsIDOMSelection.h" @@ -69,11 +68,6 @@ class IMETextTxn; class AddStyleSheetTxn; class RemoveStyleSheetTxn; -#ifdef ENABLE_JS_EDITOR_LOG -class nsJSEditorLog; -class nsJSTxnLog; -#endif // ENABLE_JS_EDITOR_LOG - //This is the monitor for the editor. PRMonitor *GetEditorMonitor(); @@ -84,8 +78,7 @@ PRMonitor *GetEditorMonitor(); * delegate the actual commands to the editor independent of the XPFE implementation. */ class nsEditor : public nsIEditor, - public nsIEditorIMESupport, - public nsIEditorLogging + public nsIEditorIMESupport { public: @@ -208,11 +201,6 @@ public: NS_IMETHOD SetCompositionString(const nsString& aCompositionString, nsIPrivateTextRangeList* aTextRangeList,nsTextEventReply* aReply); NS_IMETHOD EndComposition(void); - /* ------------ nsIEditorLogging methods -------------- */ - - NS_IMETHOD StartLogging(nsIFileSpec *aLogFile); - NS_IMETHOD StopLogging(); - public: @@ -623,12 +611,6 @@ protected: //Document 'SaveAs' charset nsString mDocCharset; - -#ifdef ENABLE_JS_EDITOR_LOG - nsJSEditorLog *mJSEditorLog; - nsJSTxnLog *mJSTxnLog; -#endif // ENABLE_JS_EDITOR_LOG - static PRInt32 gInstanceCount; friend PRBool NSCanUnload(nsISupports* serviceMgr); diff --git a/mozilla/editor/libeditor/html/nsEditorTxnLog.cpp b/mozilla/editor/libeditor/html/nsEditorTxnLog.cpp new file mode 100644 index 00000000000..0e8f63535fe --- /dev/null +++ b/mozilla/editor/libeditor/html/nsEditorTxnLog.cpp @@ -0,0 +1,406 @@ +/* -*- 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 +#include "nsHTMLEditorLog.h" +#include "nsEditorTxnLog.h" + +#define LOCK_LOG(doc) +#define UNLOCK_LOG(doc) + +static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID); + +nsEditorTxnLog::nsEditorTxnLog(nsHTMLEditorLog *aEditorLog) +{ + mRefCnt = 0; + mIndentLevel = 0; + mBatchCount = 0; + mEditorLog = aEditorLog; +} + +nsEditorTxnLog::~nsEditorTxnLog() +{ +} + +// #define DEBUG_EDITOR_TXN_LOG_REFCNT 1 + +#ifdef DEBUG_EDITOR_TXN_LOG_REFCNT + +nsrefcnt nsEditorTxnLog::AddRef(void) +{ + return ++mRefCnt; +} + +nsrefcnt nsEditorTxnLog::Release(void) +{ + NS_PRECONDITION(0 != mRefCnt, "dup release"); + if (--mRefCnt == 0) { + NS_DELETEXPCOM(this); + return 0; + } + return mRefCnt; +} + +#else + +NS_IMPL_ADDREF(nsEditorTxnLog) +NS_IMPL_RELEASE(nsEditorTxnLog) + +#endif + +NS_IMETHODIMP +nsEditorTxnLog::QueryInterface(REFNSIID aIID, void** aInstancePtr) +{ + if (nsnull == aInstancePtr) { + return NS_ERROR_NULL_POINTER; + } + if (aIID.Equals(kISupportsIID)) { + *aInstancePtr = (void*)(nsISupports*)this; + NS_ADDREF_THIS(); + return NS_OK; + } + if (aIID.Equals(nsITransactionListener::GetIID())) { + *aInstancePtr = (void*)(nsITransactionListener*)this; + NS_ADDREF_THIS(); + return NS_OK; + } + *aInstancePtr = 0; + return NS_NOINTERFACE; +} + +NS_IMETHODIMP +nsEditorTxnLog::WillDo(nsITransactionManager *aTxMgr, nsITransaction *aTransaction) +{ + LOCK_LOG(this); + + PrintIndent(mIndentLevel++); + Write("WillDo: "); + Write(GetString(aTransaction)); + Write("\n"); + Flush(); + + UNLOCK_LOG(this); + + return NS_OK; +} + +NS_IMETHODIMP +nsEditorTxnLog::DidDo(nsITransactionManager *aTxMgr, nsITransaction *aTransaction, nsresult aDoResult) +{ + LOCK_LOG(this); + + PrintIndent(--mIndentLevel); + Write("DidDo: "); + Write(GetString(aTransaction)); + Write("("); + WriteInt("%d", aDoResult); + Write(")\n"); + Flush(); + + UNLOCK_LOG(this); + + return NS_OK; +} + +NS_IMETHODIMP +nsEditorTxnLog::WillUndo(nsITransactionManager *aTxMgr, nsITransaction *aTransaction) +{ + LOCK_LOG(this); + + PrintIndent(mIndentLevel++); + + if (aTransaction) + { + Write("WillUndo: "); + Write(GetString(aTransaction)); + Write("\n"); + } + else + Write("WillUndoBatch\n"); + + Flush(); + + UNLOCK_LOG(this); + + return NS_OK; +} + +NS_IMETHODIMP +nsEditorTxnLog::DidUndo(nsITransactionManager *aTxMgr, nsITransaction *aTransaction, nsresult aUndoResult) +{ + LOCK_LOG(this); + + PrintIndent(--mIndentLevel); + + if (aTransaction) + { + Write("DidUndo: "); + Write(GetString(aTransaction)); + Write("("); + WriteInt("%d", aUndoResult); + Write(")\n"); + } + else + { + Write("EndUndoBatch ("); + WriteInt("%d", aUndoResult); + Write(")\n"); + } + + Flush(); + + UNLOCK_LOG(this); + + return NS_OK; +} + +NS_IMETHODIMP +nsEditorTxnLog::WillRedo(nsITransactionManager *aTxMgr, nsITransaction *aTransaction) +{ + LOCK_LOG(this); + + PrintIndent(mIndentLevel++); + + if (aTransaction) + { + Write("WillRedo: "); + Write(GetString(aTransaction)); + Write("\n"); + } + else + Write("WillRedoBatch\n"); + + Flush(); + + UNLOCK_LOG(this); + + return NS_OK; +} + +NS_IMETHODIMP +nsEditorTxnLog::DidRedo(nsITransactionManager *aTxMgr, nsITransaction *aTransaction, nsresult aRedoResult) +{ + LOCK_LOG(this); + + PrintIndent(--mIndentLevel); + + if (aTransaction) + { + Write("DidRedo: "); + Write(GetString(aTransaction)); + Write(" ("); + WriteInt("%d", aRedoResult); + Write(")\n"); + } + else + { + Write("DidRedoBatch ("); + WriteInt("%d", aRedoResult); + Write(")\n"); + } + + Flush(); + + UNLOCK_LOG(this); + + return NS_OK; +} + +NS_IMETHODIMP +nsEditorTxnLog::WillBeginBatch(nsITransactionManager *aTxMgr) +{ + LOCK_LOG(this); + + PrintIndent(mIndentLevel); + Write("WillBeginBatch: "); + WriteInt("%d", mBatchCount); + Write("\n"); + Flush(); + + UNLOCK_LOG(this); + + return NS_OK; +} + +NS_IMETHODIMP +nsEditorTxnLog::DidBeginBatch(nsITransactionManager *aTxMgr, nsresult aResult) +{ + LOCK_LOG(this); + + PrintIndent(mIndentLevel++); + Write("DidBeginBatch: "); + WriteInt("%d", mBatchCount++); + Write(" ("); + WriteInt("%d", aResult); + Write(")\n"); + Flush(); + + UNLOCK_LOG(this); + + return NS_OK; +} + +NS_IMETHODIMP +nsEditorTxnLog::WillEndBatch(nsITransactionManager *aTxMgr) +{ + LOCK_LOG(this); + + PrintIndent(--mIndentLevel); + Write("WillEndBatch: "); + WriteInt("%d", --mBatchCount); + Write("\n"); + Flush(); + + UNLOCK_LOG(this); + + return NS_OK; +} + +NS_IMETHODIMP +nsEditorTxnLog::DidEndBatch(nsITransactionManager *aTxMgr, nsresult aResult) +{ + LOCK_LOG(this); + + PrintIndent(mIndentLevel); + Write("DidEndBatch: "); + WriteInt("%d", mBatchCount); + Write(" ("); + WriteInt("%d", aResult); + Write(")\n"); + Flush(); + + UNLOCK_LOG(this); + + return NS_OK; +} + +NS_IMETHODIMP +nsEditorTxnLog::WillMerge(nsITransactionManager *aTxMgr, nsITransaction *aTopTransaction, nsITransaction *aTransaction) +{ + LOCK_LOG(this); + + PrintIndent(mIndentLevel); + Write("WillMerge: "); + Write(GetString(aTopTransaction)); + Write(" <-- "); + Write(GetString(aTransaction)); + Write("\n"); + Flush(); + + UNLOCK_LOG(this); + + return NS_OK; +} + +NS_IMETHODIMP +nsEditorTxnLog::DidMerge(nsITransactionManager *aTxMgr, nsITransaction *aTopTransaction, nsITransaction *aTransaction, PRBool aDidMerge, nsresult aMergeResult) +{ + LOCK_LOG(this); + + PrintIndent(mIndentLevel); + Write("DidMerge: "); + Write(GetString(aTopTransaction)); + Write(" <-- "); + Write(GetString(aTransaction)); + Write(" ("); + Write(aDidMerge ? "TRUE" : "FALSE"); + Write(", "); + WriteInt("%d", aMergeResult); + Write(")\n"); + Flush(); + + UNLOCK_LOG(this); + + return NS_OK; +} + +const char * +nsEditorTxnLog::GetString(nsITransaction *aTransaction) +{ + static char buf[256]; + + nsString str = ""; + + aTransaction->GetRedoString(&str); + + if (str.Length() == 0) + str = ""; + + buf[0] = '\0'; + str.ToCString(buf, 256); + + return buf; +} + +nsresult +nsEditorTxnLog::PrintIndent(PRInt32 aIndentLevel) +{ + PRInt32 i; + + Write(" // "); + + for (i = 0; i < aIndentLevel; i++) + Write(" "); + + return NS_OK; +} + +nsresult +nsEditorTxnLog::Write(const char *aBuffer) +{ + if (!aBuffer) + return NS_ERROR_NULL_POINTER; + + if (mEditorLog) + mEditorLog->Write(aBuffer); + else + printf(aBuffer); + + return NS_OK; +} + +nsresult +nsEditorTxnLog::WriteInt(const char *aFormat, PRInt32 aInt) +{ + if (!aFormat) + return NS_ERROR_NULL_POINTER; + + if (mEditorLog) + mEditorLog->WriteInt(aFormat, aInt); + else + printf(aFormat, aInt); + + return NS_OK; +} + +nsresult +nsEditorTxnLog::Flush() +{ + nsresult result = NS_OK; + +#ifdef SLOWS_THINGS_WAY_DOWN + + if (mEditorLog) + result = mEditorLog->Flush(); + else + fflush(stdout); + +#endif // SLOWS_THINGS_WAY_DOWN + + return result; +} diff --git a/mozilla/editor/libeditor/html/nsEditorTxnLog.h b/mozilla/editor/libeditor/html/nsEditorTxnLog.h new file mode 100644 index 00000000000..f5be21ccfbe --- /dev/null +++ b/mozilla/editor/libeditor/html/nsEditorTxnLog.h @@ -0,0 +1,77 @@ +/* -*- 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://wwwt.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 nsEditorTxnLog_h__ +#define nsEditorTxnLog_h__ + +#include "nsITransaction.h" +#include "nsITransactionManager.h" +#include "nsITransactionListener.h" + +class nsHTMLEditorLog; + +/** implementation of a transaction listener object. + * + */ +class nsEditorTxnLog : public nsITransactionListener +{ +private: + + nsHTMLEditorLog *mEditorLog; + PRInt32 mIndentLevel; + PRInt32 mBatchCount; + +public: + + /** The default constructor. + */ + nsEditorTxnLog(nsHTMLEditorLog *aEditorLog=0); + + /** The default destructor. + */ + virtual ~nsEditorTxnLog(); + + /* Macro for AddRef(), Release(), and QueryInterface() */ + NS_DECL_ISUPPORTS + + /* nsITransactionListener method implementations. */ + NS_IMETHOD WillDo(nsITransactionManager *aTxMgr, nsITransaction *aTransaction); + NS_IMETHOD DidDo(nsITransactionManager *aTxMgr, nsITransaction *aTransaction, nsresult aDoResult); + NS_IMETHOD WillUndo(nsITransactionManager *aTxMgr, nsITransaction *aTransaction); + NS_IMETHOD DidUndo(nsITransactionManager *aTxMgr, nsITransaction *aTransaction, nsresult aUndoResult); + NS_IMETHOD WillRedo(nsITransactionManager *aTxMgr, nsITransaction *aTransaction); + NS_IMETHOD DidRedo(nsITransactionManager *aTxMgr, nsITransaction *aTransaction, nsresult aRedoResult); + NS_IMETHOD WillBeginBatch(nsITransactionManager *aTxMgr); + NS_IMETHOD DidBeginBatch(nsITransactionManager *aTxMgr, nsresult aResult); + NS_IMETHOD WillEndBatch(nsITransactionManager *aTxMgr); + NS_IMETHOD DidEndBatch(nsITransactionManager *aTxMgr, nsresult aResult); + NS_IMETHOD WillMerge(nsITransactionManager *aTxMgr, nsITransaction *aTopTransaction, nsITransaction *aTransaction); + NS_IMETHOD DidMerge(nsITransactionManager *aTxMgr, nsITransaction *aTopTransaction, nsITransaction *aTransaction, PRBool aDidMerge, nsresult aMergeResult); + + +private: + + /* nsEditorTxnLog private methods. */ + const char *GetString(nsITransaction *aTransaction); + nsresult PrintIndent(PRInt32 aIndentLevel); + nsresult Write(const char *aBuffer); + nsresult WriteInt(const char *aFormat, PRInt32 aInt); + nsresult Flush(); +}; + +#endif // nsEditorTxnLog_h__ diff --git a/mozilla/editor/libeditor/html/nsHTMLEditor.cpp b/mozilla/editor/libeditor/html/nsHTMLEditor.cpp index 9243672a879..a1ca754660f 100644 --- a/mozilla/editor/libeditor/html/nsHTMLEditor.cpp +++ b/mozilla/editor/libeditor/html/nsHTMLEditor.cpp @@ -85,10 +85,6 @@ const unsigned char nbsp = 160; -#ifdef ENABLE_JS_EDITOR_LOG -#include "nsJSEditorLog.h" -#endif // ENABLE_JS_EDITOR_LOG - // HACK - CID for NavDTD until we can get at dtd via the document // {a6cf9107-15b3-11d2-932e-00805f8add32} #define NS_CNAVDTD_CID \ @@ -408,13 +404,6 @@ NS_IMETHODIMP nsHTMLEditor::SetInlineProperty(nsIAtom *aProperty, const nsString *aAttribute, const nsString *aValue) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->SetTextProperty(aProperty, aAttribute, aValue); -#endif // ENABLE_JS_EDITOR_LOG - if (!aProperty) { return NS_ERROR_NULL_POINTER; } if (!mRules) { return NS_ERROR_NOT_INITIALIZED; } @@ -667,14 +656,6 @@ NS_IMETHODIMP nsHTMLEditor::GetInlineProperty(nsIAtom *aProperty, NS_IMETHODIMP nsHTMLEditor::RemoveInlineProperty(nsIAtom *aProperty, const nsString *aAttribute) { - -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->RemoveTextProperty(aProperty, aAttribute); -#endif // ENABLE_JS_EDITOR_LOG - if (!aProperty) { return NS_ERROR_NULL_POINTER; } if (!mRules) { return NS_ERROR_NOT_INITIALIZED; } @@ -783,13 +764,6 @@ NS_IMETHODIMP nsHTMLEditor::RemoveInlineProperty(nsIAtom *aProperty, const nsStr NS_IMETHODIMP nsHTMLEditor::DeleteSelection(nsIEditor::ESelectionCollapseDirection aAction) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->DeleteSelection(aAction); -#endif // ENABLE_JS_EDITOR_LOG - if (!mRules) { return NS_ERROR_NOT_INITIALIZED; } nsCOMPtr selection; @@ -818,13 +792,6 @@ NS_IMETHODIMP nsHTMLEditor::DeleteSelection(nsIEditor::ESelectionCollapseDirecti NS_IMETHODIMP nsHTMLEditor::InsertText(const nsString& aStringToInsert) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->InsertText(aStringToInsert); -#endif // ENABLE_JS_EDITOR_LOG - if (!mRules) { return NS_ERROR_NOT_INITIALIZED; } nsCOMPtr selection; @@ -857,13 +824,6 @@ NS_IMETHODIMP nsHTMLEditor::InsertText(const nsString& aStringToInsert) NS_IMETHODIMP nsHTMLEditor::InsertHTML(const nsString& aInputString) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->InsertHTML(aInputString); -#endif // ENABLE_JS_EDITOR_LOG - nsAutoEditBatch beginBatching(this); nsresult res; @@ -964,13 +924,6 @@ NS_IMETHODIMP nsHTMLEditor::InsertHTML(const nsString& aInputString) NS_IMETHODIMP nsHTMLEditor::InsertBreak() { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->InsertBreak(); -#endif // ENABLE_JS_EDITOR_LOG - nsresult res; if (!mRules) { return NS_ERROR_NOT_INITIALIZED; } @@ -1042,13 +995,6 @@ NS_IMETHODIMP nsHTMLEditor::InsertBreak() NS_IMETHODIMP nsHTMLEditor::InsertElement(nsIDOMElement* aElement, PRBool aDeleteSelection) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->InsertElement(aElement, aDeleteSelection); -#endif // ENABLE_JS_EDITOR_LOG - nsresult res = NS_ERROR_NOT_INITIALIZED; if (!aElement) @@ -1211,13 +1157,6 @@ nsHTMLEditor::DeleteSelectionAndCreateNode(const nsString& aTag, NS_IMETHODIMP nsHTMLEditor::SelectElement(nsIDOMElement* aElement) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->SelectElement(aElement); -#endif // ENABLE_JS_EDITOR_LOG - nsresult res = NS_ERROR_NULL_POINTER; // Must be sure that element is contained in the document body @@ -1250,13 +1189,6 @@ nsHTMLEditor::SelectElement(nsIDOMElement* aElement) NS_IMETHODIMP nsHTMLEditor::SetCaretAfterElement(nsIDOMElement* aElement) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->SetCaretAfterElement(aElement); -#endif // ENABLE_JS_EDITOR_LOG - nsresult res = NS_ERROR_NULL_POINTER; // Be sure the element is contained in the document body @@ -1306,13 +1238,6 @@ NS_IMETHODIMP nsHTMLEditor::GetParagraphFormat(nsString& aParagraphFormat) NS_IMETHODIMP nsHTMLEditor::SetParagraphFormat(const nsString& aParagraphFormat) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->SetParagraphFormat(aParagraphFormat); -#endif // ENABLE_JS_EDITOR_LOG - nsresult res = NS_ERROR_NOT_INITIALIZED; //Kinda sad to waste memory just to force lower case nsAutoString tag = aParagraphFormat; @@ -1403,13 +1328,6 @@ nsHTMLEditor::GetParagraphStyle(nsStringArray *aTagList) NS_IMETHODIMP nsHTMLEditor::AddBlockParent(nsString& aParentTag) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->AddBlockParent(aParentTag); -#endif // ENABLE_JS_EDITOR_LOG - if (gNoisy) { char *tag = aParentTag.ToNewCString(); @@ -1454,13 +1372,6 @@ nsHTMLEditor::AddBlockParent(nsString& aParentTag) NS_IMETHODIMP nsHTMLEditor::ReplaceBlockParent(nsString& aParentTag) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->ReplaceBlockParent(aParentTag); -#endif // ENABLE_JS_EDITOR_LOG - if (gNoisy) { char *tag = aParentTag.ToNewCString(); @@ -1501,13 +1412,6 @@ nsHTMLEditor::ReplaceBlockParent(nsString& aParentTag) NS_IMETHODIMP nsHTMLEditor::RemoveParagraphStyle() { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->RemoveParagraphStyle(); -#endif // ENABLE_JS_EDITOR_LOG - if (gNoisy) { printf("---------- nsHTMLEditor::RemoveParagraphStyle ----------\n"); } @@ -1539,13 +1443,6 @@ nsHTMLEditor::RemoveParagraphStyle() NS_IMETHODIMP nsHTMLEditor::RemoveParent(const nsString &aParentTag) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->RemoveParent(aParentTag); -#endif // ENABLE_JS_EDITOR_LOG - if (gNoisy) { printf("---------- nsHTMLEditor::RemoveParent ----------\n"); } @@ -1577,13 +1474,6 @@ nsHTMLEditor::RemoveParent(const nsString &aParentTag) NS_IMETHODIMP nsHTMLEditor::InsertList(const nsString& aListType) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->InsertList(aListType); -#endif // ENABLE_JS_EDITOR_LOG - nsresult res; if (!mRules) { return NS_ERROR_NOT_INITIALIZED; } @@ -1668,13 +1558,6 @@ nsHTMLEditor::InsertList(const nsString& aListType) NS_IMETHODIMP nsHTMLEditor::InsertBasicBlock(const nsString& aBlockType) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->InsertBasicBlock(aBlockType); -#endif // ENABLE_JS_EDITOR_LOG - nsresult res; if (!mRules) { return NS_ERROR_NOT_INITIALIZED; } @@ -1753,13 +1636,6 @@ nsHTMLEditor::InsertBasicBlock(const nsString& aBlockType) NS_IMETHODIMP nsHTMLEditor::Indent(const nsString& aIndent) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->Indent(aIndent); -#endif // ENABLE_JS_EDITOR_LOG - nsresult res; if (!mRules) { return NS_ERROR_NOT_INITIALIZED; } @@ -1839,13 +1715,6 @@ nsHTMLEditor::Indent(const nsString& aIndent) NS_IMETHODIMP nsHTMLEditor::Align(const nsString& aAlignType) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->Align(aAlignType); -#endif // ENABLE_JS_EDITOR_LOG - nsAutoEditBatch beginBatching(this); nsCOMPtr node; PRBool cancel= PR_FALSE; @@ -2312,13 +2181,6 @@ nsHTMLEditor::SaveHLineSettings(nsIDOMElement* aElement) NS_IMETHODIMP nsHTMLEditor::InsertLinkAroundSelection(nsIDOMElement* aAnchorElement) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->InsertLinkAroundSelection(aAnchorElement); -#endif // ENABLE_JS_EDITOR_LOG - nsresult res=NS_ERROR_NULL_POINTER; nsCOMPtr selection; @@ -2375,15 +2237,6 @@ NS_IMETHODIMP nsHTMLEditor::SetBackgroundColor(const nsString& aColor) // TODO: Check selection for Cell, Row, Column or table and do color on appropriate level // For initial testing, just set the background on the BODY tag (the document's background) -// Do this only if setting a table or cell background -// It will be called in nsTextEditor::SetBackgroundColor for the page background -#if 0 //def ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->SetBackgroundColor(aColor); -#endif // ENABLE_JS_EDITOR_LOG - // TODO: Check selection for Cell, Row, Column or table and do color on appropriate level // For initial testing, just set the background on the BODY tag (the document's background) @@ -2401,14 +2254,6 @@ NS_IMETHODIMP nsHTMLEditor::SetBackgroundColor(const nsString& aColor) NS_IMETHODIMP nsHTMLEditor::SetBodyAttribute(const nsString& aAttribute, const nsString& aValue) { - -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->SetBodyAttribute(aAttribute, aValue); -#endif // ENABLE_JS_EDITOR_LOG - nsresult res; // TODO: Check selection for Cell, Row, Column or table and do color on appropriate level @@ -2564,13 +2409,6 @@ nsHTMLEditor::RemoveStyleSheet(nsICSSStyleSheet* aSheet) NS_IMETHODIMP nsHTMLEditor::ApplyStyleSheet(const nsString& aURL) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->ApplyStyleSheet(aURL); -#endif // ENABLE_JS_EDITOR_LOG - // XXX: Note that this is not an undo-able action yet! nsresult rv = NS_OK; @@ -2740,26 +2578,12 @@ NS_IMETHODIMP nsHTMLEditor::SetBodyWrapWidth(PRInt32 aWrapColumn) // NS_IMETHODIMP nsHTMLEditor::PasteAsQuotation() { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->PasteAsQuotation(); -#endif // ENABLE_JS_EDITOR_LOG - nsAutoString citation(""); return PasteAsCitedQuotation(citation); } NS_IMETHODIMP nsHTMLEditor::PasteAsCitedQuotation(const nsString& aCitation) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->PasteAsCitedQuotation(aCitation); -#endif // ENABLE_JS_EDITOR_LOG - nsAutoEditBatch beginBatching(this); nsCOMPtr newNode; nsAutoString tag("blockquote"); @@ -2803,13 +2627,6 @@ NS_IMETHODIMP nsHTMLEditor::PasteAsCitedQuotation(const nsString& aCitation) // NS_IMETHODIMP nsHTMLEditor::PasteAsPlaintextQuotation() { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->PasteAsQuotation(); -#endif // ENABLE_JS_EDITOR_LOG - // Get Clipboard Service nsIClipboard* clipboard; nsresult rv = nsServiceManager::GetService(kCClipboardCID, @@ -2850,13 +2667,6 @@ NS_IMETHODIMP nsHTMLEditor::PasteAsPlaintextQuotation() NS_IMETHODIMP nsHTMLEditor::InsertAsQuotation(const nsString& aQuotedText) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->InsertAsQuotation(aQuotedText); -#endif // ENABLE_JS_EDITOR_LOG - nsAutoString citation (""); return InsertAsCitedQuotation(aQuotedText, citation); } @@ -2864,13 +2674,6 @@ NS_IMETHODIMP nsHTMLEditor::InsertAsQuotation(const nsString& aQuotedText) // text insert. NS_IMETHODIMP nsHTMLEditor::InsertAsPlaintextQuotation(const nsString& aQuotedText) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->InsertAsQuotation(aQuotedText); -#endif // ENABLE_JS_EDITOR_LOG - // Now we have the text. Cite it appropriately: nsCOMPtr citer; nsCOMPtr prefs; @@ -2908,13 +2711,6 @@ NS_IMETHODIMP nsHTMLEditor::InsertAsPlaintextQuotation(const nsString& aQuotedTe NS_IMETHODIMP nsHTMLEditor::InsertAsCitedQuotation(const nsString& aQuotedText, const nsString& aCitation) { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->InsertAsCitedQuotation(aQuotedText, aCitation); -#endif // ENABLE_JS_EDITOR_LOG - nsAutoEditBatch beginBatching(this); nsCOMPtr newNode; nsAutoString tag("blockquote"); @@ -3020,13 +2816,6 @@ nsHTMLEditor::GetEmbeddedObjects(nsISupportsArray** aNodeList) NS_IMETHODIMP nsHTMLEditor::Cut() { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->Cut(); -#endif // ENABLE_JS_EDITOR_LOG - nsCOMPtr selection; nsresult res = mPresShell->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection)); if (!NS_SUCCEEDED(res)) @@ -3044,13 +2833,6 @@ NS_IMETHODIMP nsHTMLEditor::Cut() NS_IMETHODIMP nsHTMLEditor::Copy() { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->Copy(); -#endif // ENABLE_JS_EDITOR_LOG - //printf("nsEditor::Copy\n"); return mPresShell->DoCopy(); @@ -3058,13 +2840,6 @@ NS_IMETHODIMP nsHTMLEditor::Copy() NS_IMETHODIMP nsHTMLEditor::Paste() { -#ifdef ENABLE_JS_EDITOR_LOG - nsAutoJSEditorLogLock logLock(mJSEditorLog); - - if (mJSEditorLog) - mJSEditorLog->Paste(); -#endif // ENABLE_JS_EDITOR_LOG - nsString stuffToPaste; // Get Clipboard Service diff --git a/mozilla/editor/libeditor/html/nsHTMLEditorLog.cpp b/mozilla/editor/libeditor/html/nsHTMLEditorLog.cpp new file mode 100644 index 00000000000..26dc9a0b21d --- /dev/null +++ b/mozilla/editor/libeditor/html/nsHTMLEditorLog.cpp @@ -0,0 +1,1211 @@ +/* -*- 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 +#include "nsIPresShell.h" +#include "nsIDOMElement.h" +#include "nsIDOMAttr.h" +#include "nsIDOMNode.h" +#include "nsIDOMNodeList.h" +#include "nsIDOMCharacterData.h" +#include "nsIDOMNamedNodeMap.h" +#include "nsIDOMSelection.h" +#include "nsIDOMRange.h" +#include "nsHTMLEditorLog.h" +#include "nsCOMPtr.h" + +#include "nsEditorTxnLog.h" + +#define LOCK_LOG(doc) +#define UNLOCK_LOG(doc) + +static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID); + +nsHTMLEditorLog::nsHTMLEditorLog() +{ + mRefCnt = 0; + mLocked = -1; + mEditorTxnLog = 0; +} + +nsHTMLEditorLog::~nsHTMLEditorLog() +{ + StopLogging(); +} + +NS_IMPL_ADDREF_INHERITED(nsHTMLEditorLog, nsHTMLEditor); +NS_IMPL_RELEASE_INHERITED(nsHTMLEditorLog, nsHTMLEditor); + +NS_IMETHODIMP +nsHTMLEditorLog::QueryInterface(REFNSIID aIID, void** aInstancePtr) +{ + if (!aInstancePtr) + return NS_ERROR_NULL_POINTER; + + *aInstancePtr = nsnull; + + if (aIID.Equals(nsIEditorLogging::GetIID())) { + *aInstancePtr = (void*)(nsIEditorLogging*)this; + NS_ADDREF_THIS(); + return NS_OK; + } + + return nsHTMLEditor::QueryInterface(aIID, aInstancePtr); +} + +NS_IMETHODIMP +nsHTMLEditorLog::SetInlineProperty(nsIAtom *aProperty, const nsString *aAttribute, const nsString *aValue) +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + nsAutoString propStr; + + aProperty->ToString(propStr); + + PrintSelection(); + Write("window.editorShell.SetTextProperty(\""); + PrintUnicode(propStr); + Write("\", \""); + if (aAttribute) + PrintUnicode(*aAttribute); + Write("\", \""); + if (aValue) + PrintUnicode(*aValue); + Write("\");\n"); + + Flush(); + } + + return nsHTMLEditor::SetInlineProperty(aProperty, aAttribute, aValue); +} + +NS_IMETHODIMP +nsHTMLEditorLog::SetParagraphFormat(const nsString& aParagraphFormat) +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + PrintSelection(); + Write("window.editorShell.paragraphFormat = \""); + PrintUnicode(aParagraphFormat); + Write("\";\n"); + + Flush(); + } + + return nsHTMLEditor::SetParagraphFormat(aParagraphFormat); +} + +NS_IMETHODIMP +nsHTMLEditorLog::RemoveInlineProperty(nsIAtom *aProperty, const nsString *aAttribute) +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + nsAutoString propStr; + + aProperty->ToString(propStr); + + PrintSelection(); + Write("window.editorShell.RemoveTextProperty(\""); + PrintUnicode(propStr); + Write("\", \""); + if (aAttribute) + PrintUnicode(*aAttribute); + Write("\");\n"); + + Flush(); + } + + return nsHTMLEditor::RemoveInlineProperty(aProperty, aAttribute); +} + +NS_IMETHODIMP +nsHTMLEditorLog::DeleteSelection(nsIEditor::ESelectionCollapseDirection aAction) +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + PrintSelection(); + Write("window.editorShell.DeleteSelection("); + WriteInt("%d", aAction); + Write(");\n"); + + Flush(); + } + + return nsHTMLEditor::DeleteSelection(aAction); +} + +NS_IMETHODIMP +nsHTMLEditorLog::InsertText(const nsString& aStringToInsert) +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + PrintSelection(); + + Write("window.editorShell.InsertText(\""); + PrintUnicode(aStringToInsert); + Write("\");\n"); + + Flush(); + } + + return nsHTMLEditor::InsertText(aStringToInsert); +} + +NS_IMETHODIMP +nsHTMLEditorLog::InsertBreak() +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + PrintSelection(); + Write("window.editorShell.InsertBreak();\n"); + Flush(); + } + + return nsHTMLEditor::InsertBreak(); +} + +NS_IMETHODIMP +nsHTMLEditorLog::Undo(PRUint32 aCount) +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + Write("window.editorShell.Undo();\n"); + Flush(); + } + + return nsHTMLEditor::Undo(aCount); +} + +NS_IMETHODIMP +nsHTMLEditorLog::Redo(PRUint32 aCount) +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + Write("window.editorShell.Redo();\n"); + Flush(); + } + + return nsHTMLEditor::Redo(aCount); +} + +NS_IMETHODIMP +nsHTMLEditorLog::BeginTransaction() +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + Write("window.editorShell.BeginBatchChanges();\n"); + Flush(); + } + + return nsHTMLEditor::BeginTransaction(); +} + +NS_IMETHODIMP +nsHTMLEditorLog::EndTransaction() +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + Write("window.editorShell.EndBatchChanges();\n"); + Flush(); + } + + return nsHTMLEditor::EndTransaction(); +} + +NS_IMETHODIMP +nsHTMLEditorLog::SelectAll() +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + Write("window.editorShell.SelectAll();\n"); + Flush(); + } + + return nsHTMLEditor::SelectAll(); +} + +NS_IMETHODIMP +nsHTMLEditorLog::BeginningOfDocument() +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + Write("window.editorShell.BeginningOfDocument();\n"); + Flush(); + } + + return nsHTMLEditor::BeginningOfDocument(); +} + +NS_IMETHODIMP +nsHTMLEditorLog::EndOfDocument() +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + Write("window.editorShell.EndOfDocument();\n"); + Flush(); + } + + return nsHTMLEditor::EndOfDocument(); +} + +NS_IMETHODIMP +nsHTMLEditorLog::Cut() +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + PrintSelection(); + Write("window.editorShell.Cut();\n"); + Flush(); + } + + return nsHTMLEditor::Cut(); +} + +NS_IMETHODIMP +nsHTMLEditorLog::Copy() +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + PrintSelection(); + Write("window.editorShell.Copy();\n"); + Flush(); + } + + return nsHTMLEditor::Copy(); +} + +NS_IMETHODIMP +nsHTMLEditorLog::Paste() +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + PrintSelection(); + Write("window.editorShell.Paste();\n"); + Flush(); + } + + return nsHTMLEditor::Paste(); +} + +NS_IMETHODIMP +nsHTMLEditorLog::PasteAsQuotation() +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + PrintSelection(); + Write("window.editorShell.PasteAsQuotation();\n"); + Flush(); + } + + return nsHTMLEditor::PasteAsQuotation(); +} + +NS_IMETHODIMP +nsHTMLEditorLog::PasteAsPlaintextQuotation() +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + PrintSelection(); + Write("window.editorShell.PasteAsQuotation();\n"); + Flush(); + } + + return nsHTMLEditor::PasteAsPlaintextQuotation(); +} + +NS_IMETHODIMP +nsHTMLEditorLog::PasteAsCitedQuotation(const nsString& aCitation) +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + PrintSelection(); + Write("window.editorShell.PasteAsCitedQuotation(\""); + PrintUnicode(aCitation); + Write("\");\n"); + Flush(); + } + + return nsHTMLEditor::PasteAsCitedQuotation(aCitation); +} + +NS_IMETHODIMP +nsHTMLEditorLog::InsertAsQuotation(const nsString& aQuotedText) +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + PrintSelection(); + Write("window.editorShell.InsertAsQuotation(\""); + PrintUnicode(aQuotedText); + Write("\");\n"); + Flush(); + } + + return nsHTMLEditor::InsertAsQuotation(aQuotedText); +} + +NS_IMETHODIMP +nsHTMLEditorLog::InsertAsPlaintextQuotation(const nsString& aQuotedText) +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + PrintSelection(); + Write("window.editorShell.InsertAsQuotation(\""); + PrintUnicode(aQuotedText); + Write("\");\n"); + Flush(); + } + + return nsHTMLEditor::InsertAsPlaintextQuotation(aQuotedText); +} + +NS_IMETHODIMP +nsHTMLEditorLog::InsertAsCitedQuotation(const nsString& aQuotedText, const nsString& aCitation) +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + PrintSelection(); + Write("window.editorShell.InsertAsCitedQuotation(\""); + PrintUnicode(aQuotedText); + Write("\", \""); + PrintUnicode(aCitation); + Write("\");\n"); + Flush(); + } + + return nsHTMLEditor::InsertAsCitedQuotation(aQuotedText, aCitation); +} + +NS_IMETHODIMP +nsHTMLEditorLog::SetBackgroundColor(const nsString& aColor) +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + Write("window.editorShell.SetBackgroundColor(\""); + PrintUnicode(aColor); + Write("\");\n"); + Flush(); + } + + return nsHTMLEditor::SetBackgroundColor(aColor); +} + +NS_IMETHODIMP +nsHTMLEditorLog::SetBodyAttribute(const nsString& aAttr, const nsString& aValue) +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + Write("window.editorShell.SetBodyAttribute(\""); + PrintUnicode(aAttr); + Write("\", \""); + PrintUnicode(aValue); + Write("\");\n"); + Flush(); + } + + return nsHTMLEditor::SetBodyAttribute(aAttr, aValue); +} + +NS_IMETHODIMP +nsHTMLEditorLog::InsertList(const nsString& aListType) +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + PrintSelection(); + + Write("window.editorShell.InsertList(\""); + PrintUnicode(aListType); + Write("\");\n"); + Flush(); + } + + return nsHTMLEditor::InsertList(aListType); +} + +NS_IMETHODIMP +nsHTMLEditorLog::Indent(const nsString& aIndent) +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + PrintSelection(); + + Write("window.editorShell.Indent(\""); + PrintUnicode(aIndent); + Write("\");\n"); + Flush(); + } + + return nsHTMLEditor::Indent(aIndent); +} + +NS_IMETHODIMP +nsHTMLEditorLog::Align(const nsString& aAlign) +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + PrintSelection(); + + Write("window.editorShell.Align(\""); + PrintUnicode(aAlign); + Write("\");\n"); + Flush(); + } + + return nsHTMLEditor::Align(aAlign); +} + +NS_IMETHODIMP +nsHTMLEditorLog::InsertElement(nsIDOMElement* aElement, PRBool aDeleteSelection) +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + if (!aElement) + return NS_ERROR_NULL_POINTER; + + nsCOMPtr node = do_QueryInterface(aElement); + + if (!node) + return NS_ERROR_NULL_POINTER; + + PrintSelection(); + PrintNode(node, 0); + Write("window.editorShell.InsertElement(n0, "); + Write(aDeleteSelection ? "true" : "false"); + Write(");\n"); + Flush(); + } + + return nsHTMLEditor::InsertElement(aElement, aDeleteSelection); +} + +NS_IMETHODIMP +nsHTMLEditorLog::InsertLinkAroundSelection(nsIDOMElement* aAnchorElement) +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + if (!aAnchorElement) + return NS_ERROR_NULL_POINTER; + + nsCOMPtr node = do_QueryInterface(aAnchorElement); + + if (!node) + return NS_ERROR_NULL_POINTER; + + PrintSelection(); + PrintNode(node, 0); + Write("window.editorShell.InsertLinkAroundSelection(n0);\n"); + Flush(); + } + + return nsHTMLEditor::InsertLinkAroundSelection(aAnchorElement); +} + +NS_IMETHODIMP +nsHTMLEditorLog::ApplyStyleSheet(const nsString& aURL) +{ + nsAutoHTMLEditorLogLock logLock(this); + + if (!mLocked && mFileSpec) + { + Write("window.editorShell.ApplyStyleSheet(\""); + PrintUnicode(aURL); + Write("\");\n"); + + Flush(); + } + + return nsHTMLEditor::ApplyStyleSheet(aURL); +} + +NS_IMETHODIMP +nsHTMLEditorLog::StartLogging(nsIFileSpec *aLogFile) +{ + nsresult result = NS_ERROR_FAILURE; + + if (!aLogFile) + return NS_ERROR_NULL_POINTER; + + if (mFileSpec) + { + result = StopLogging(); + + if (NS_FAILED(result)) + return result; + } + + mFileSpec = do_QueryInterface(aLogFile); + + if (!mFileSpec) + result = NS_ERROR_NULL_POINTER; + + result = mFileSpec->openStreamForWriting(); + + if (NS_FAILED(result)) + { + mFileSpec = nsCOMPtr(); + return result; + } + + if (mTxnMgr) + { + mEditorTxnLog = new nsEditorTxnLog(this); + + if (mEditorTxnLog) + { + NS_ADDREF(mEditorTxnLog); + mTxnMgr->AddListener(mEditorTxnLog); + } + else + return NS_ERROR_OUT_OF_MEMORY; + } + + return NS_OK; +} + +NS_IMETHODIMP +nsHTMLEditorLog::StopLogging() +{ + if (mTxnMgr && mEditorTxnLog) + mTxnMgr->RemoveListener(mEditorTxnLog); + + if (mEditorTxnLog) + { + NS_RELEASE(mEditorTxnLog); + mEditorTxnLog = 0; + } + + if (mFileSpec) + { + mFileSpec->closeStream(); + mFileSpec = nsCOMPtr(); + } + + return NS_OK; +} + + +nsresult +nsHTMLEditorLog::Write(const char *aBuffer) +{ + nsresult result; + + if (!aBuffer) + return NS_ERROR_NULL_POINTER; + + PRInt32 len = strlen(aBuffer); + + if (mFileSpec) + { + PRInt32 retval; + + result = mFileSpec->write(aBuffer, len, &retval); + + if (NS_FAILED(result)) + return result; + +#ifdef VERY_SLOW + + result = mFileSpec->flush(); + + if (NS_FAILED(result)) + return result; + +#endif // VERY_SLOW + } + else + fwrite(aBuffer, 1, len, stdout); + + return NS_OK; +} + +nsresult +nsHTMLEditorLog::WriteInt(const char *aFormat, PRInt32 aInt) +{ + if (!aFormat) + return NS_ERROR_NULL_POINTER; + + char buf[256]; + + sprintf(buf, aFormat, aInt); + + return Write(buf); +} + +nsresult +nsHTMLEditorLog::Flush() +{ + nsresult result = NS_OK; + + if (mFileSpec) + result = mFileSpec->flush(); + else + fflush(stdout); + + return result; +} + +nsresult +nsHTMLEditorLog::PrintUnicode(const nsString &aString) +{ + const PRUnichar *uc = aString.GetUnicode(); + PRInt32 i, len = aString.Length(); + char buf[10]; + + for (i = 0; i < len; i++) + { + if (nsString::IsAlpha(uc[i]) || nsString::IsDigit(uc[i]) || uc[i] == ' ') + sprintf(buf, "%c", uc[i]); + else + sprintf(buf, "\\u%.4x", uc[i]); + + nsresult result = Write(buf); + + if (NS_FAILED(result)) + return result; + } + + return NS_OK; +} + +nsresult +nsHTMLEditorLog::PrintSelection() +{ + nsCOMPtr selection; + nsresult result; + PRInt32 rangeCount; + + result = GetSelection(getter_AddRefs(selection)); + + if (NS_FAILED(result)) + return result; + + result = selection->GetRangeCount(&rangeCount); + + if (NS_FAILED(result)) + return result; + + Write("selRanges = [ "); + + PRInt32 i, j; + nsCOMPtr range; + nsCOMPtr startNode; + nsCOMPtr endNode; + PRInt32 startOffset, endOffset; + + for (i = 0; i < rangeCount; i++) + { + result = selection->GetRangeAt(i, getter_AddRefs(range)); + + if (NS_FAILED(result)) + return result; + + result = range->GetStartParent(getter_AddRefs(startNode)); + + if (NS_FAILED(result)) + return result; + + if (!startNode) + return NS_ERROR_NULL_POINTER; + + result = range->GetStartOffset(&startOffset); + + if (NS_FAILED(result)) + return result; + + result = range->GetEndParent(getter_AddRefs(endNode)); + + if (NS_FAILED(result)) + return result; + + if (!endNode) + return NS_ERROR_NULL_POINTER; + + result = range->GetEndOffset(&endOffset); + + if (NS_FAILED(result)) + return result; + + PRInt32 *offsetArray = 0; + PRInt32 arrayLength = 0; + + result = GetNodeTreeOffsets(startNode, &offsetArray, &arrayLength); + + if (NS_FAILED(result)) + return result; + + if (i != 0) + Write(",\n "); + + Write("[ [["); + + for (j = 0; j < arrayLength; j++) + { + if (j != 0) + Write(", "); + WriteInt("%d", offsetArray[j]); + } + + Write("], "); + WriteInt("%3d", startOffset); + Write("], "); + + if (startNode != endNode) + { + delete []offsetArray; + offsetArray = 0; + arrayLength = 0; + + result = GetNodeTreeOffsets(endNode, &offsetArray, &arrayLength); + + if (NS_FAILED(result)) + return result; + } + + Write("[["); + + for (j = 0; j < arrayLength; j++) + { + if (j != 0) + Write(", "); + WriteInt("%d", offsetArray[j]); + } + + delete []offsetArray; + + Write("], "); + WriteInt("%3d", endOffset); + Write("] ]"); + } + + Write(" ];\nEditorSetSelectionFromOffsets(selRanges);\n"); + + Flush(); + + return NS_OK; +} + +nsresult +nsHTMLEditorLog::PrintElementNode(nsIDOMNode *aNode, PRInt32 aDepth) +{ + nsresult result; + nsAutoString tag, name, value; + nsCOMPtr ele = do_QueryInterface(aNode); + nsCOMPtr map; + + if (!ele) + return NS_ERROR_NULL_POINTER; + + result = ele->GetTagName(tag); + + if (NS_FAILED(result)) + return result; + + Write("n"); + WriteInt("%d", aDepth); + Write(" = window.editorShell.editorDocument.createElement(\""); + PrintUnicode(tag); + Write("\");\n"); + + result = aNode->GetAttributes(getter_AddRefs(map)); + + if (NS_FAILED(result)) + return result; + + if (!map) + return NS_ERROR_NULL_POINTER; + + PRUint32 i, len; + nsCOMPtr attr; + + result = map->GetLength(&len); + + if (NS_FAILED(result)) + return result; + + for (i = 0; i < len; i++) + { + result = map->Item(i, getter_AddRefs(attr)); + + if (NS_FAILED(result)) + return result; + + if (!attr) + return NS_ERROR_NULL_POINTER; + + result = PrintAttributeNode(attr, aDepth); + + if (NS_FAILED(result)) + return result; + } + + result = PrintNodeChildren(aNode, aDepth); + + return result; +} + +nsresult +nsHTMLEditorLog::PrintAttributeNode(nsIDOMNode *aNode, PRInt32 aDepth) +{ + nsresult result; + nsCOMPtr attr = do_QueryInterface(aNode); + + if (!attr) + return NS_ERROR_NULL_POINTER; + + nsAutoString str; + + result = attr->GetName(str); + + if (NS_FAILED(result)) + return result; + + Write("a"); + WriteInt("%d", aDepth); + Write(" = window.editorShell.editorDocument.createAttribute(\""); + PrintUnicode(str); + Write("\");\n"); + + result = attr->GetValue(str); + + if (NS_FAILED(result)) + return result; + + Write("a"); + WriteInt("%d", aDepth); + Write(".value = \""); + PrintUnicode(str); + Write("\";\n"); + + Write("n"); + WriteInt("%d", aDepth); + Write(".setAttributeNode(a"); + WriteInt("%d", aDepth); + Write(");\n"); + + return NS_OK; +} + +nsresult +nsHTMLEditorLog::PrintNodeChildren(nsIDOMNode *aNode, PRInt32 aDepth) +{ + nsresult result; + + if (!aNode) + return NS_ERROR_NULL_POINTER; + + nsCOMPtr list; + + result = aNode->GetChildNodes(getter_AddRefs(list)); + + if (NS_FAILED(result)) + return result; + + if (!list) + { + // Must not have any children! + return NS_OK; + } + + PRUint32 i, len; + nsCOMPtr node; + + result = list->GetLength(&len); + + if (NS_FAILED(result)) + return result; + + for (i = 0; i < len; i++) + { + result = list->Item(i, getter_AddRefs(node)); + + if (NS_FAILED(result)) + return result; + + result = PrintNode(node, aDepth + 1); + + if (NS_FAILED(result)) + return result; + + Write("n"); + WriteInt("%d", aDepth); + Write(".appendChild(n"); + WriteInt("%d", aDepth+1); + Write(");\n"); + } + + return NS_OK; +} + +nsresult +nsHTMLEditorLog::PrintTextNode(nsIDOMNode *aNode, PRInt32 aDepth) +{ + nsresult result; + + nsCOMPtr cd = do_QueryInterface(aNode); + + if (!cd) + return NS_ERROR_NULL_POINTER; + + nsAutoString str; + + result = cd->GetData(str); + + if (NS_FAILED(result)) + return result; + + Write("n"); + WriteInt("%d", aDepth); + Write(" = window.editorShell.editorDocument.createTextNode(\""); + PrintUnicode(str); + Write("\");\n"); + + return NS_OK; +} + +nsresult +nsHTMLEditorLog::PrintNode(nsIDOMNode *aNode, PRInt32 aDepth) +{ + nsresult result = NS_OK; + + if (!aNode) + return NS_ERROR_NULL_POINTER; + + PRUint16 nodeType; + + result = aNode->GetNodeType(&nodeType); + + switch (nodeType) + { + case nsIDOMNode::ELEMENT_NODE: + result = PrintElementNode(aNode, aDepth); + break; + case nsIDOMNode::TEXT_NODE: + result = PrintTextNode(aNode, aDepth); + break; + case nsIDOMNode::ATTRIBUTE_NODE: + result = PrintAttributeNode(aNode, aDepth); + break; + case nsIDOMNode::CDATA_SECTION_NODE: + case nsIDOMNode::ENTITY_REFERENCE_NODE: + case nsIDOMNode::ENTITY_NODE: + case nsIDOMNode::PROCESSING_INSTRUCTION_NODE: + case nsIDOMNode::COMMENT_NODE: + case nsIDOMNode::DOCUMENT_NODE: + case nsIDOMNode::DOCUMENT_TYPE_NODE: + case nsIDOMNode::DOCUMENT_FRAGMENT_NODE: + case nsIDOMNode::NOTATION_NODE: + default: + break; + } + + Flush(); + + return result; +} + +nsresult +nsHTMLEditorLog::GetNodeTreeOffsets(nsIDOMNode *aNode, PRInt32 **aResult, PRInt32 *aLength) +{ + nsresult result; + + if (!aNode || !aResult || !aLength) + return NS_ERROR_NULL_POINTER; + + *aResult = 0; + *aLength = 0; + + nsIDOMNode *parent = aNode; + PRInt32 i = 0; + + // Count the number of parent nodes above aNode. + + while (parent) + { + result = parent->GetParentNode(&parent); + + if (NS_FAILED(result)) + return result; + + if (parent) + ++i; + } + + // Allocate an array big enough to hold all the offsets. + + *aResult = new PRInt32[i]; + + if (!aResult) + return NS_ERROR_OUT_OF_MEMORY; + + *aLength = i; + + while (aNode && i > 0) + { + PRInt32 offset = 0; + + result = aNode->GetParentNode(&parent); + + if (NS_FAILED(result)) + { + delete [](*aResult); + *aResult = 0; + *aLength = 0; + return result; + } + + while (aNode) + { + result = aNode->GetPreviousSibling(&aNode); + + if (NS_FAILED(result)) + { + delete [](*aResult); + *aResult = 0; + *aLength = 0; + return result; + } + + if (aNode) + ++offset; + } + + (*aResult)[--i] = offset; + aNode = parent; + } + + return NS_OK; +} + +nsresult +nsHTMLEditorLog::Lock() +{ + mLocked++; + return NS_OK; +} + +nsresult +nsHTMLEditorLog::Unlock() +{ + --mLocked; + return NS_OK; +} + +#ifdef NEVER_ENABLE_THIS_JAVASCRIPT + +function EditorGetNodeAtOffsets(offsets) +{ + var node = null; + var i; + + node = window.editorShell.editorDocument; + + for (i = 0; i < offsets.length; i++) + { + node = node.childNodes[offsets[i]]; + } + + return node; +} + +function EditorSetSelectionFromOffsets(selRanges) +{ + var rangeArr, start, end, i, node, offset; + var selection = window.editorShell.editorSelection; + + selection.clearSelection(); + + for (i = 0; i < selRanges.length; i++) + { + rangeArr = selRanges[i]; + start = rangeArr[0]; + end = rangeArr[1]; + + var range = window.editorShell.editorDocument.createRange(); + + node = EditorGetNodeAtOffsets(start[0]); + offset = start[1]; + + range.setStart(node, offset); + + node = EditorGetNodeAtOffsets(end[0]); + offset = end[1]; + + range.setEnd(node, offset); + + selection.addRange(range); + } +} + +#endif diff --git a/mozilla/editor/libeditor/html/nsHTMLEditorLog.h b/mozilla/editor/libeditor/html/nsHTMLEditorLog.h new file mode 100644 index 00000000000..772354609c4 --- /dev/null +++ b/mozilla/editor/libeditor/html/nsHTMLEditorLog.h @@ -0,0 +1,127 @@ +/* -*- 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://wwwt.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 nsHTMLEditorLog_h__ +#define nsHTMLEditorLog_h__ + +#include "nsHTMLEditor.h" +#include "nsIEditorLogging.h" +#include "nsIFileSpec.h" +#include "nsCOMPtr.h" + +class nsEditorTxnLog; + +/** implementation of a transaction listener object. + * + */ +class nsHTMLEditorLog : public nsHTMLEditor, + public nsIEditorLogging +{ +private: + + nsCOMPtr mFileSpec; + nsEditorTxnLog *mEditorTxnLog; + PRInt32 mLocked; + PRInt32 mDepth; + +public: + + // Interfaces for AddRef, Release, and QueryInterface. + NS_DECL_ISUPPORTS_INHERITED + + nsHTMLEditorLog(); + virtual ~nsHTMLEditorLog(); + + /* nsIHTMLEditor method implementations. */ + NS_IMETHOD SetInlineProperty(nsIAtom *aProperty, + const nsString *aAttribute, + const nsString *aValue); + NS_IMETHOD SetParagraphFormat(const nsString& aParagraphFormat); + NS_IMETHOD RemoveInlineProperty(nsIAtom *aProperty, const nsString *aAttribute); + NS_IMETHOD DeleteSelection(nsIEditor::ESelectionCollapseDirection aAction); + NS_IMETHOD InsertText(const nsString& aStringToInsert); + NS_IMETHOD InsertBreak(); + NS_IMETHOD Undo(PRUint32 aCount); + NS_IMETHOD Redo(PRUint32 aCount); + NS_IMETHOD BeginTransaction(); + NS_IMETHOD EndTransaction(); + NS_IMETHOD SelectAll(); + NS_IMETHOD BeginningOfDocument(); + NS_IMETHOD EndOfDocument(); + + NS_IMETHOD Cut(); + NS_IMETHOD Copy(); + NS_IMETHOD Paste(); + NS_IMETHOD PasteAsQuotation(); + NS_IMETHOD PasteAsPlaintextQuotation(); + NS_IMETHOD PasteAsCitedQuotation(const nsString& aCitation); + NS_IMETHOD InsertAsQuotation(const nsString& aQuotedText); + NS_IMETHOD InsertAsPlaintextQuotation(const nsString& aQuotedText); + NS_IMETHOD InsertAsCitedQuotation(const nsString& aQuotedText, const nsString& aCitation); + + NS_IMETHOD ApplyStyleSheet(const nsString& aURL); + + NS_IMETHOD SetBackgroundColor(const nsString& aColor); + NS_IMETHOD SetBodyAttribute(const nsString& aAttr, const nsString& aValue); + NS_IMETHOD InsertList(const nsString& aListType); + NS_IMETHOD Indent(const nsString& aIndent); + NS_IMETHOD Align(const nsString& aAlign); + NS_IMETHOD InsertElement(nsIDOMElement* aElement, PRBool aDeleteSelection); + NS_IMETHOD InsertLinkAroundSelection(nsIDOMElement* aAnchorElement); + + NS_IMETHOD StartLogging(nsIFileSpec *aLogFile); + NS_IMETHOD StopLogging(); + + /* nsHTMLEditorLog public methods. */ + nsresult Write(const char *aBuffer); + nsresult WriteInt(const char *aFormat, PRInt32 aInt); + nsresult Flush(); + nsresult PrintUnicode(const nsString &aString); + nsresult PrintSelection(); + nsresult PrintNode(nsIDOMNode *aNode, PRInt32 aDepth=0); + nsresult PrintElementNode(nsIDOMNode *aNode, PRInt32 aDepth); + nsresult PrintTextNode(nsIDOMNode *aNode, PRInt32 aDepth); + nsresult PrintAttributeNode(nsIDOMNode *aNode, PRInt32 aDepth=0); + nsresult PrintNodeChildren(nsIDOMNode *aNode, PRInt32 aDepth=0); + nsresult GetNodeTreeOffsets(nsIDOMNode *aNode, PRInt32 **aResult, PRInt32 *aLength); + nsresult Lock(); + nsresult Unlock(); +}; + +class nsAutoHTMLEditorLogLock +{ + nsHTMLEditorLog *mLog; + +public: + + nsAutoHTMLEditorLogLock(nsHTMLEditorLog *aLog) + { + mLog = aLog; + + if (mLog) + mLog->Lock(); + } + + ~nsAutoHTMLEditorLogLock() + { + if (mLog) + mLog->Unlock(); + } +}; + +#endif // nsHTMLEditorLog_h__