From 575a96dca1f555d5b7eca88070c3660d767d2072 Mon Sep 17 00:00:00 2001 From: "kostello%netscape.com" Date: Thu, 4 Mar 1999 21:53:42 +0000 Subject: [PATCH] Added Implementations for OutputHTML and OutputTXT git-svn-id: svn://10.0.0.236/trunk@22785 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/editor/base/Makefile.in | 2 +- mozilla/editor/base/makefile.win | 4 +- mozilla/editor/base/nsTextEditor.cpp | 195 ++++++++++++++++++++++++++- mozilla/editor/base/nsTextEditor.h | 1 + 4 files changed, 194 insertions(+), 8 deletions(-) diff --git a/mozilla/editor/base/Makefile.in b/mozilla/editor/base/Makefile.in index 0419221fa90..1fa63164019 100644 --- a/mozilla/editor/base/Makefile.in +++ b/mozilla/editor/base/Makefile.in @@ -60,7 +60,7 @@ CPPSRCS = \ MODULE = editor -REQUIRES = xpcom raptor dom base +REQUIRES = xpcom raptor dom base netlib include $(topsrcdir)/config/config.mk diff --git a/mozilla/editor/base/makefile.win b/mozilla/editor/base/makefile.win index 24201aebe03..51576d5e0fd 100644 --- a/mozilla/editor/base/makefile.win +++ b/mozilla/editor/base/makefile.win @@ -88,13 +88,14 @@ CPP_OBJS = \ MODULE=editor -REQUIRES=xpcom raptor dom base +REQUIRES=xpcom raptor dom base netlib LINCS=-I$(PUBLIC)\editor \ -I$(PUBLIC)\xpcom \ -I$(PUBLIC)\raptor \ -I$(PUBLIC)\js \ -I$(PUBLIC)\txmgr \ + -I$(PUBLIC)\netlib \ -I$(PUBLIC)\dom MAKE_OBJ_TYPE = DLL @@ -111,6 +112,7 @@ LLIBS= \ $(DIST)\lib\xpcom32.lib \ $(DIST)\lib\libplc21.lib \ $(DIST)\lib\raptorbase.lib \ + $(DIST)\lib\raptorhtmlpars.lib \ $(LIBNSPR) !if "$(MOZ_BITS)"=="32" && defined(MOZ_DEBUG) && defined(GLOWCODE) LLIBS=$(LLIBS) $(GLOWDIR)\glowcode.lib diff --git a/mozilla/editor/base/nsTextEditor.cpp b/mozilla/editor/base/nsTextEditor.cpp index e45d8b636ce..901c2fdf691 100644 --- a/mozilla/editor/base/nsTextEditor.cpp +++ b/mozilla/editor/base/nsTextEditor.cpp @@ -20,6 +20,17 @@ #include "nsIEditorSupport.h" #include "nsEditorEventListeners.h" #include "nsIEditProperty.h" + +#include "nsIStreamListener.h" +#include "nsIParser.h" +#include "nsParserCIID.h" +#include "nsIDocument.h" +#include "nsIHTMLContentSink.h" +#include "nsHTMLContentSinkStream.h" +#include "nsHTMLToTXTSinkStream.h" +#include "nsXIFDTD.h" + + #include "nsIDOMDocument.h" #include "nsIDOMEventReceiver.h" #include "nsIDOMKeyListener.h" @@ -36,8 +47,23 @@ #include "nsLayoutCID.h" #include "nsIPresShell.h" #include "nsIStyleContext.h" + + class nsIFrame; +#ifdef XP_UNIX +#include +#endif + +#ifdef XP_MAC +#include +#include +#endif + +#ifdef XP_PC +#include +#endif + #include "CreateElementTxn.h" #include "nsRepository.h" @@ -543,22 +569,179 @@ NS_IMETHODIMP nsTextEditor::Insert(nsIInputStream *aInputStream) return result; } + +#ifdef XP_MAC +void WriteFromStringstream(stringstream& aIn, nsIOutputStream* aOut) +{ + if (aOut != nsnull) + { + string theString = aIn.str(); + PRInt32 len = theString.length(); + const char* str = theString.data(); + PRUint32 outCount = 0; + + if (len) + { + char * ptr = NS_CONST_CAST(char*,str); + for (PRInt32 plen = len; plen > 0; plen --, ptr ++) + if (*ptr == '\n') + *ptr = '\r'; + aOut->Write(ptr, 0, len, &outCount); + } + } +} +#else +static +void WriteFromOstrstream(ostrstream& aIn, nsIOutputStream* aOut) +{ + if (aOut != nsnull) + { + char* str = aIn.str(); + PRUint32 inCount = aIn.pcount(); + PRUint32 outCount = 0; + if (str != nsnull) + { + aOut->Write(str, 0, inCount, &outCount); + // in ostrstreams if you call the str() function + // then you are responsible for deleting the string + delete str; + } + } +} +#endif + NS_IMETHODIMP nsTextEditor::OutputText(nsIOutputStream *aOutputStream) { - nsresult result=NS_ERROR_NOT_INITIALIZED; +#ifdef XP_MAC + stringstream out; +#else + ostrstream out; +#endif + + nsresult result=NS_ERROR_FAILURE; if (mEditor) { - result = NS_ERROR_NOT_IMPLEMENTED; + nsIPresShell* shell = nsnull; + + mEditor->GetPresShell(&shell); + if (nsnull != shell) { + nsCOMPtr doc; + shell->GetDocument(getter_AddRefs(doc)); + if (doc) { + nsString buffer; + + doc->CreateXIF(buffer); + + nsIParser* parser; + + static NS_DEFINE_IID(kCParserIID, NS_IPARSER_IID); + static NS_DEFINE_IID(kCParserCID, NS_PARSER_IID); + + nsresult rv = nsRepository::CreateInstance(kCParserCID, + nsnull, + kCParserIID, + (void **)&parser); + + if (NS_OK == rv) { + nsIHTMLContentSink* sink = nsnull; + + rv = NS_New_HTMLToTXT_SinkStream(&sink); + + if (aOutputStream != nsnull) + ((nsHTMLContentSinkStream*)sink)->SetOutputStream(out); + + if (NS_OK == rv) { + parser->SetContentSink(sink); + + nsIDTD* dtd = nsnull; + rv = NS_NewXIFDTD(&dtd); + if (NS_OK == rv) { + parser->RegisterDTD(dtd); + parser->Parse(buffer, 0, "text/xif",PR_FALSE,PR_TRUE); + } + #ifdef XP_MAC + WriteFromStringstream(out,aOutputStream); + #else + WriteFromOstrstream(out,aOutputStream); + #endif + + NS_IF_RELEASE(dtd); + NS_IF_RELEASE(sink); + } + NS_RELEASE(parser); + } + } + NS_RELEASE(shell); + } } return result; } + + NS_IMETHODIMP nsTextEditor::OutputHTML(nsIOutputStream *aOutputStream) { - nsresult result=NS_ERROR_NOT_INITIALIZED; +#ifdef XP_MAC + stringstream out; +#else + ostrstream out; +#endif + + + nsresult result=NS_ERROR_FAILURE; if (mEditor) { - result = NS_ERROR_NOT_IMPLEMENTED; + nsIPresShell* shell = nsnull; + + mEditor->GetPresShell(&shell); + if (nsnull != shell) { + nsCOMPtr doc; + shell->GetDocument(getter_AddRefs(doc)); + if (doc) { + nsString buffer; + + doc->CreateXIF(buffer); + + nsIParser* parser; + + static NS_DEFINE_IID(kCParserIID, NS_IPARSER_IID); + static NS_DEFINE_IID(kCParserCID, NS_PARSER_IID); + + nsresult rv = nsRepository::CreateInstance(kCParserCID, + nsnull, + kCParserIID, + (void **)&parser); + + if (NS_OK == rv) { + nsIHTMLContentSink* sink = nsnull; + + rv = NS_New_HTML_ContentSinkStream(&sink); + + if (aOutputStream) + ((nsHTMLContentSinkStream*)sink)->SetOutputStream(out); + + if (NS_OK == rv) { + parser->SetContentSink(sink); + + nsIDTD* dtd = nsnull; + rv = NS_NewXIFDTD(&dtd); + if (NS_OK == rv) { + parser->RegisterDTD(dtd); + parser->Parse(buffer, 0, "text/xif",PR_FALSE,PR_TRUE); + } + #ifdef XP_MAC + WriteFromStringstream(out,aOutputStream); + #else + WriteFromOstrstream(out,aOutputStream); + #endif + NS_IF_RELEASE(dtd); + NS_IF_RELEASE(sink); + } + NS_RELEASE(parser); + } + } + NS_RELEASE(shell); + } } return result; } @@ -617,7 +800,7 @@ NS_IMETHODIMP nsTextEditor::SetTextPropertiesForNode(nsIDOMNode *aNode, } if (NS_SUCCEEDED(result)) { - if (aEndOffset!=count) + if (aEndOffset!=(PRInt32)count) { result = mEditor->SplitNode(aNode, aEndOffset-aStartOffset, getter_AddRefs(newTextNode)); } @@ -673,7 +856,7 @@ nsTextEditor::SetTextPropertiesForNodesWithSameParent(nsIDOMNode *aStartNode, PRUint32 count; endNodeAsChar->GetLength(&count); nsCOMPtrnewRightTextNode; // this will be the middle text node - if (count!=aEndOffset) { + if ((PRInt32)count!=aEndOffset) { result = mEditor->SplitNode(aEndNode, aEndOffset, getter_AddRefs(newRightTextNode)); } else { diff --git a/mozilla/editor/base/nsTextEditor.h b/mozilla/editor/base/nsTextEditor.h index eb9063ffc84..7cc58ecc1ae 100644 --- a/mozilla/editor/base/nsTextEditor.h +++ b/mozilla/editor/base/nsTextEditor.h @@ -109,6 +109,7 @@ protected: NS_IMETHOD SetTagFromProperty(nsAutoString &aTag, nsIAtom *aPropName) const; + // Data members protected: nsCOMPtr mEditor;