From 815d7692f5decfe391528e28b70a8ecaa71c1cd3 Mon Sep 17 00:00:00 2001 From: "akkana%netscape.com" Date: Wed, 26 Apr 2000 20:57:03 +0000 Subject: [PATCH] Better fix for 37059: initial space should be retained in selection-only case. --- htmlparser/src/nsHTMLToTXTSinkStream.cpp | 36 ++++++++++--------- htmlparser/src/nsHTMLToTXTSinkStream.h | 1 + .../htmlparser/src/nsHTMLToTXTSinkStream.cpp | 36 ++++++++++--------- parser/htmlparser/src/nsHTMLToTXTSinkStream.h | 1 + 4 files changed, 40 insertions(+), 34 deletions(-) diff --git a/htmlparser/src/nsHTMLToTXTSinkStream.cpp b/htmlparser/src/nsHTMLToTXTSinkStream.cpp index a1880b01937..b874bd28bd9 100644 --- a/htmlparser/src/nsHTMLToTXTSinkStream.cpp +++ b/htmlparser/src/nsHTMLToTXTSinkStream.cpp @@ -185,6 +185,7 @@ nsHTMLToTXTSinkStream::nsHTMLToTXTSinkStream() mInWhitespace = PR_TRUE; mPreFormatted = PR_FALSE; mCacheLine = PR_FALSE; + mStartedOutput = PR_FALSE; // initialize the tag stack to zero: mTagStack = new nsHTMLTag[TagStackSize]; @@ -243,13 +244,6 @@ nsHTMLToTXTSinkStream::Initialize(nsIOutputStream* aOutStream, result = nsServiceManager::ReleaseService(kLWBrkCID, lf); } - // If we're encoding only the selection, then we don't want to risk - // ignoring whitespace which might be significant: -// Unfortunately, this isn't the right solution because then we -// always get a space at the beginning of selection-only output! -// if (mFlags | nsIDocumentEncoder::OutputSelectionOnly) -// mInWhitespace = PR_FALSE; - return result; } @@ -705,6 +699,8 @@ nsHTMLToTXTSinkStream::CloseContainer(const nsIParserNode& aNode) // Fow now, I will only add a SPACE. Could be a TAB or something // else but I'm not sure everything can handle the TAB so SPACE // seems like a better solution. + // XXX This unfortunately means that every selection inside a + // XXX table cell ends up with an extraneous space after it. if(!mInWhitespace) { // Maybe add something else? Several spaces? A TAB? SPACE+TAB? if(mCacheLine) { @@ -826,17 +822,24 @@ nsHTMLToTXTSinkStream::AddLeaf(const nsIParserNode& aNode) // Otherwise, either we're collapsing to minimal text, or we're // prettyprinting to mimic the html format, and in neither case // does the formatting of the html source help us. + // One exception: at the very beginning of a selection, + // we want to preserve whitespace. if (mFlags & nsIDocumentEncoder::OutputPreformatted || ((mFlags & nsIDocumentEncoder::OutputFormatted) && (mTagStackIndex > 0) && (mTagStack[mTagStackIndex-1] == eHTMLTag_pre)) || (mPreFormatted && !mWrapColumn)) - { - Write(text); // XXX: spacestuffing (maybe call AddToLine if mCacheLine==true) - } else if(!mInWhitespace) { - Write( NS_ConvertToString(" ") ); - mInWhitespace = PR_TRUE; - } + { + Write(text); // XXX: spacestuffing (maybe call AddToLine if mCacheLine==true) + } + else if(!mInWhitespace || + (!mStartedOutput + && mFlags | nsIDocumentEncoder::OutputSelectionOnly)) + { + mInWhitespace = PR_FALSE; + Write( NS_ConvertToString(" ") ); + mInWhitespace = PR_TRUE; + } } else if (type == eHTMLTag_newline) { @@ -945,6 +948,9 @@ nsHTMLToTXTSinkStream::FlushLine() */ void nsHTMLToTXTSinkStream::WriteSimple(nsString& aString) { + if (aString.Length() > 0) + mStartedOutput = PR_TRUE; + // First, replace all nbsp characters with spaces, // which the unicode encoder won't do for us. static PRUnichar nbsp = 160; @@ -1167,10 +1173,6 @@ nsHTMLToTXTSinkStream::WriteQuotesAndIndent() } -#ifdef DEBUG_akkana_not -#define DEBUG_wrapping 1 -#endif - // // Write a string, wrapping appropriately to mWrapColumn. // This routine also handles indentation and mail-quoting, diff --git a/htmlparser/src/nsHTMLToTXTSinkStream.h b/htmlparser/src/nsHTMLToTXTSinkStream.h index 49c5e05560e..4f2a844cff9 100644 --- a/htmlparser/src/nsHTMLToTXTSinkStream.h +++ b/htmlparser/src/nsHTMLToTXTSinkStream.h @@ -179,6 +179,7 @@ protected: PRBool mInWhitespace; PRBool mPreFormatted; PRBool mCacheLine; // If the line should be cached before output. This makes it possible to do smarter wrapping. + PRBool mStartedOutput; // we've produced at least a character nsString mURL; diff --git a/parser/htmlparser/src/nsHTMLToTXTSinkStream.cpp b/parser/htmlparser/src/nsHTMLToTXTSinkStream.cpp index a1880b01937..b874bd28bd9 100644 --- a/parser/htmlparser/src/nsHTMLToTXTSinkStream.cpp +++ b/parser/htmlparser/src/nsHTMLToTXTSinkStream.cpp @@ -185,6 +185,7 @@ nsHTMLToTXTSinkStream::nsHTMLToTXTSinkStream() mInWhitespace = PR_TRUE; mPreFormatted = PR_FALSE; mCacheLine = PR_FALSE; + mStartedOutput = PR_FALSE; // initialize the tag stack to zero: mTagStack = new nsHTMLTag[TagStackSize]; @@ -243,13 +244,6 @@ nsHTMLToTXTSinkStream::Initialize(nsIOutputStream* aOutStream, result = nsServiceManager::ReleaseService(kLWBrkCID, lf); } - // If we're encoding only the selection, then we don't want to risk - // ignoring whitespace which might be significant: -// Unfortunately, this isn't the right solution because then we -// always get a space at the beginning of selection-only output! -// if (mFlags | nsIDocumentEncoder::OutputSelectionOnly) -// mInWhitespace = PR_FALSE; - return result; } @@ -705,6 +699,8 @@ nsHTMLToTXTSinkStream::CloseContainer(const nsIParserNode& aNode) // Fow now, I will only add a SPACE. Could be a TAB or something // else but I'm not sure everything can handle the TAB so SPACE // seems like a better solution. + // XXX This unfortunately means that every selection inside a + // XXX table cell ends up with an extraneous space after it. if(!mInWhitespace) { // Maybe add something else? Several spaces? A TAB? SPACE+TAB? if(mCacheLine) { @@ -826,17 +822,24 @@ nsHTMLToTXTSinkStream::AddLeaf(const nsIParserNode& aNode) // Otherwise, either we're collapsing to minimal text, or we're // prettyprinting to mimic the html format, and in neither case // does the formatting of the html source help us. + // One exception: at the very beginning of a selection, + // we want to preserve whitespace. if (mFlags & nsIDocumentEncoder::OutputPreformatted || ((mFlags & nsIDocumentEncoder::OutputFormatted) && (mTagStackIndex > 0) && (mTagStack[mTagStackIndex-1] == eHTMLTag_pre)) || (mPreFormatted && !mWrapColumn)) - { - Write(text); // XXX: spacestuffing (maybe call AddToLine if mCacheLine==true) - } else if(!mInWhitespace) { - Write( NS_ConvertToString(" ") ); - mInWhitespace = PR_TRUE; - } + { + Write(text); // XXX: spacestuffing (maybe call AddToLine if mCacheLine==true) + } + else if(!mInWhitespace || + (!mStartedOutput + && mFlags | nsIDocumentEncoder::OutputSelectionOnly)) + { + mInWhitespace = PR_FALSE; + Write( NS_ConvertToString(" ") ); + mInWhitespace = PR_TRUE; + } } else if (type == eHTMLTag_newline) { @@ -945,6 +948,9 @@ nsHTMLToTXTSinkStream::FlushLine() */ void nsHTMLToTXTSinkStream::WriteSimple(nsString& aString) { + if (aString.Length() > 0) + mStartedOutput = PR_TRUE; + // First, replace all nbsp characters with spaces, // which the unicode encoder won't do for us. static PRUnichar nbsp = 160; @@ -1167,10 +1173,6 @@ nsHTMLToTXTSinkStream::WriteQuotesAndIndent() } -#ifdef DEBUG_akkana_not -#define DEBUG_wrapping 1 -#endif - // // Write a string, wrapping appropriately to mWrapColumn. // This routine also handles indentation and mail-quoting, diff --git a/parser/htmlparser/src/nsHTMLToTXTSinkStream.h b/parser/htmlparser/src/nsHTMLToTXTSinkStream.h index 49c5e05560e..4f2a844cff9 100644 --- a/parser/htmlparser/src/nsHTMLToTXTSinkStream.h +++ b/parser/htmlparser/src/nsHTMLToTXTSinkStream.h @@ -179,6 +179,7 @@ protected: PRBool mInWhitespace; PRBool mPreFormatted; PRBool mCacheLine; // If the line should be cached before output. This makes it possible to do smarter wrapping. + PRBool mStartedOutput; // we've produced at least a character nsString mURL;