From 938b7c8db0c5371284e91944ac2cf176d581036b Mon Sep 17 00:00:00 2001 From: "pinkerton%netscape.com" Date: Sat, 8 Jan 2000 01:48:39 +0000 Subject: [PATCH] Added a new param to the HTML->Text sink stream to take the charset so that we can convert to plain text with the correct charset for the platform. Enhanced the XIFFormatConverter to take advantage of this, fixing bug 21414. r=akkana, nhotta. --- content/base/src/nsDocumentEncoder.cpp | 2 +- htmlparser/src/nsHTMLToTXTSinkStream.h | 10 ++++--- layout/base/src/nsDocumentEncoder.cpp | 2 +- mailnews/compose/src/nsMsgCompUtils.cpp | 3 ++- parser/htmlparser/src/nsHTMLToTXTSinkStream.h | 10 ++++--- widget/src/xpwidgets/nsXIFFormatConverter.cpp | 27 +++++++++++++++---- 6 files changed, 40 insertions(+), 14 deletions(-) diff --git a/content/base/src/nsDocumentEncoder.cpp b/content/base/src/nsDocumentEncoder.cpp index ca880f78f2c..e61b98b19a3 100644 --- a/content/base/src/nsDocumentEncoder.cpp +++ b/content/base/src/nsDocumentEncoder.cpp @@ -195,7 +195,7 @@ nsTextEncoder::EncodeToString(nsString& aOutputString) rv = NS_New_HTML_ContentSinkStream(&sink, &aOutputString, mFlags); else // default to text/plain - rv = NS_New_HTMLToTXT_SinkStream(&sink, &aOutputString, + rv = NS_New_HTMLToTXT_SinkStream(&sink, &aOutputString, nsnull, mWrapColumn, mFlags); if (sink && NS_SUCCEEDED(rv)) diff --git a/htmlparser/src/nsHTMLToTXTSinkStream.h b/htmlparser/src/nsHTMLToTXTSinkStream.h index 5b281dd7bd9..8eecb84f880 100644 --- a/htmlparser/src/nsHTMLToTXTSinkStream.h +++ b/htmlparser/src/nsHTMLToTXTSinkStream.h @@ -224,6 +224,7 @@ NS_New_HTMLToTXT_SinkStream(nsIHTMLContentSink** aInstancePtrResult, inline nsresult NS_New_HTMLToTXT_SinkStream(nsIHTMLContentSink** aInstancePtrResult, nsString* aOutString, + const nsString* aCharsetOverride=nsnull, PRUint32 aWrapColumn=0, PRUint32 aFlags=0) { nsCOMPtr it; @@ -238,9 +239,12 @@ NS_New_HTMLToTXT_SinkStream(nsIHTMLContentSink** aInstancePtrResult, if (NS_SUCCEEDED(rv)) { it->SetWrapColumn(aWrapColumn); - nsAutoString ucs2("ucs2"); - it->SetCharsetOverride(&ucs2); - + if ( aCharsetOverride ) + it->SetCharsetOverride(aCharsetOverride); + else { + nsAutoString ucs2("ucs2"); + it->SetCharsetOverride(&ucs2); + } rv = it->QueryInterface(nsIHTMLContentSink::GetIID(), (void**)aInstancePtrResult); } diff --git a/layout/base/src/nsDocumentEncoder.cpp b/layout/base/src/nsDocumentEncoder.cpp index ca880f78f2c..e61b98b19a3 100644 --- a/layout/base/src/nsDocumentEncoder.cpp +++ b/layout/base/src/nsDocumentEncoder.cpp @@ -195,7 +195,7 @@ nsTextEncoder::EncodeToString(nsString& aOutputString) rv = NS_New_HTML_ContentSinkStream(&sink, &aOutputString, mFlags); else // default to text/plain - rv = NS_New_HTMLToTXT_SinkStream(&sink, &aOutputString, + rv = NS_New_HTMLToTXT_SinkStream(&sink, &aOutputString, nsnull, mWrapColumn, mFlags); if (sink && NS_SUCCEEDED(rv)) diff --git a/mailnews/compose/src/nsMsgCompUtils.cpp b/mailnews/compose/src/nsMsgCompUtils.cpp index 518cc22b17c..200ecf421cb 100644 --- a/mailnews/compose/src/nsMsgCompUtils.cpp +++ b/mailnews/compose/src/nsMsgCompUtils.cpp @@ -1936,7 +1936,8 @@ ConvertBufToPlainText(nsString &aConBuf, const char *charSet) converterFlags |= nsIDocumentEncoder::OutputFormatFlowed; } - rv = NS_New_HTMLToTXT_SinkStream((nsIHTMLContentSink **)&sink, &convertedText, wrapWidth, converterFlags); + nsAutoString charSetStr(charSet); + rv = NS_New_HTMLToTXT_SinkStream((nsIHTMLContentSink **)&sink, &convertedText, &charSetStr, wrapWidth, converterFlags); if (sink && NS_SUCCEEDED(rv)) { sink->DoFragment(PR_TRUE); diff --git a/parser/htmlparser/src/nsHTMLToTXTSinkStream.h b/parser/htmlparser/src/nsHTMLToTXTSinkStream.h index 5b281dd7bd9..8eecb84f880 100644 --- a/parser/htmlparser/src/nsHTMLToTXTSinkStream.h +++ b/parser/htmlparser/src/nsHTMLToTXTSinkStream.h @@ -224,6 +224,7 @@ NS_New_HTMLToTXT_SinkStream(nsIHTMLContentSink** aInstancePtrResult, inline nsresult NS_New_HTMLToTXT_SinkStream(nsIHTMLContentSink** aInstancePtrResult, nsString* aOutString, + const nsString* aCharsetOverride=nsnull, PRUint32 aWrapColumn=0, PRUint32 aFlags=0) { nsCOMPtr it; @@ -238,9 +239,12 @@ NS_New_HTMLToTXT_SinkStream(nsIHTMLContentSink** aInstancePtrResult, if (NS_SUCCEEDED(rv)) { it->SetWrapColumn(aWrapColumn); - nsAutoString ucs2("ucs2"); - it->SetCharsetOverride(&ucs2); - + if ( aCharsetOverride ) + it->SetCharsetOverride(aCharsetOverride); + else { + nsAutoString ucs2("ucs2"); + it->SetCharsetOverride(&ucs2); + } rv = it->QueryInterface(nsIHTMLContentSink::GetIID(), (void**)aInstancePtrResult); } diff --git a/widget/src/xpwidgets/nsXIFFormatConverter.cpp b/widget/src/xpwidgets/nsXIFFormatConverter.cpp index 0ac58b89250..534796a524e 100644 --- a/widget/src/xpwidgets/nsXIFFormatConverter.cpp +++ b/widget/src/xpwidgets/nsXIFFormatConverter.cpp @@ -50,6 +50,12 @@ #include "nsWidgetsCID.h" #include "nsXIFFormatConverter.h" +// unicode conversion +#define NS_IMPL_IDS +#include "nsIPlatformCharset.h" +#undef NS_IMPL_IDS +#include "nsISaveAsCharset.h" + static NS_DEFINE_IID(kCParserCID, NS_PARSER_IID); @@ -303,17 +309,28 @@ nsXIFFormatConverter::Convert(const char *aFromDataFlavor, nsISupports *aFromDat NS_IMETHODIMP nsXIFFormatConverter::ConvertFromXIFToText(const nsAutoString & aFromStr, nsAutoString & aToStr) { + // we may cache it since the platform charset will not change through application life + nsAutoString aCharset; + nsCOMPtr platformCharset; + nsresult rv = nsComponentManager::CreateInstance(NS_PLATFORMCHARSET_PROGID, nsnull, + NS_GET_IID(nsIPlatformCharset), + getter_AddRefs(platformCharset)); + if (NS_SUCCEEDED(rv)) + rv = platformCharset->GetCharset(kPlatformCharsetSel_PlainTextInClipboard, aCharset); + if (NS_FAILED(rv)) + aCharset.SetString("ISO-8859-1"); + aToStr = ""; nsCOMPtr parser; - nsresult rv = nsComponentManager::CreateInstance(kCParserCID, - nsnull, - nsIParser::GetIID(), - getter_AddRefs(parser)); + rv = nsComponentManager::CreateInstance(kCParserCID, + nsnull, + nsIParser::GetIID(), + getter_AddRefs(parser)); if ( !parser ) return rv; nsCOMPtr sink; - rv = NS_New_HTMLToTXT_SinkStream(getter_AddRefs(sink),&aToStr,0); + rv = NS_New_HTMLToTXT_SinkStream(getter_AddRefs(sink),&aToStr,&aCharset); if ( sink ) { parser->SetContentSink(sink);