bug 564737 - plaintext serializer shouldn't take care of leading spaces in a block. tests by David :Bienvenu. r=laurentj sr+a=jst

This commit is contained in:
Jonathan Kamens 2010-09-21 15:02:55 +02:00
Родитель db0f8312e1
Коммит 9730e0d359
3 изменённых файлов: 58 добавлений и 34 удалений

Просмотреть файл

@ -119,7 +119,7 @@ nsPlainTextSerializer::nsPlainTextSerializer()
// Flow // Flow
mEmptyLines = 1; // The start of the document is an "empty line" in itself, mEmptyLines = 1; // The start of the document is an "empty line" in itself,
mInWhitespace = PR_TRUE; mInWhitespace = PR_FALSE;
mPreFormatted = PR_FALSE; mPreFormatted = PR_FALSE;
mStartedOutput = PR_FALSE; mStartedOutput = PR_FALSE;
@ -638,6 +638,8 @@ nsPlainTextSerializer::DoOpenContainer(const nsIParserNode* aNode, PRInt32 aTag)
} }
} }
else { else {
/* See comment at end of function. */
mInWhitespace = PR_TRUE;
mPreFormatted = PR_FALSE; mPreFormatted = PR_FALSE;
} }
@ -807,6 +809,13 @@ nsPlainTextSerializer::DoOpenContainer(const nsIParserNode* aNode, PRInt32 aTag)
Write(NS_LITERAL_STRING("_")); Write(NS_LITERAL_STRING("_"));
} }
/* Container elements are always block elements, so we shouldn't
output any whitespace immediately after the container tag even if
there's extra whitespace there because the HTML is pretty-printed
or something. To ensure that happens, tell the serializer we're
already in whitespace so it won't output more. */
mInWhitespace = PR_TRUE;
return NS_OK; return NS_OK;
} }
@ -1073,38 +1082,26 @@ nsPlainTextSerializer::DoAddLeaf(const nsIParserNode *aNode, PRInt32 aTag,
EnsureVerticalSpace(mEmptyLines+1); EnsureVerticalSpace(mEmptyLines+1);
} }
} }
else if (type == eHTMLTag_whitespace) { else if (type == eHTMLTag_whitespace || type == eHTMLTag_newline) {
// The only times we want to pass along whitespace from the original // The only times we want to pass along whitespace from the original
// html source are if we're forced into preformatted mode via flags, // html source are if we're forced into preformatted mode via flags,
// or if we're prettyprinting and we're inside a <pre>. // or if we're prettyprinting and we're inside a <pre>.
// Otherwise, either we're collapsing to minimal text, or we're // Otherwise, either we're collapsing to minimal text, or we're
// prettyprinting to mimic the html format, and in neither case // prettyprinting to mimic the html format, and in neither case
// does the formatting of the html source help us. // 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 || if (mFlags & nsIDocumentEncoder::OutputPreformatted ||
(mPreFormatted && !mWrapColumn) || (mPreFormatted && !mWrapColumn) ||
IsInPre()) { IsInPre()) {
Write(aText); if (type == eHTMLTag_newline)
EnsureVerticalSpace(mEmptyLines+1);
else
Write(aText);
} }
else if(!mInWhitespace || else if(!mInWhitespace) {
(!mStartedOutput
&& mFlags | nsIDocumentEncoder::OutputSelectionOnly)) {
mInWhitespace = PR_FALSE;
Write(kSpace); Write(kSpace);
mInWhitespace = PR_TRUE; mInWhitespace = PR_TRUE;
} }
} }
else if (type == eHTMLTag_newline) {
if (mFlags & nsIDocumentEncoder::OutputPreformatted ||
(mPreFormatted && !mWrapColumn) ||
IsInPre()) {
EnsureVerticalSpace(mEmptyLines+1);
}
else {
Write(kSpace);
}
}
else if (type == eHTMLTag_hr && else if (type == eHTMLTag_hr &&
(mFlags & nsIDocumentEncoder::OutputFormatted)) { (mFlags & nsIDocumentEncoder::OutputFormatted)) {
EnsureVerticalSpace(0); EnsureVerticalSpace(0);
@ -1161,10 +1158,12 @@ nsPlainTextSerializer::EnsureVerticalSpace(PRInt32 noOfRows)
// realize that we should start a new line. // realize that we should start a new line.
if(noOfRows >= 0 && !mInIndentString.IsEmpty()) { if(noOfRows >= 0 && !mInIndentString.IsEmpty()) {
EndLine(PR_FALSE); EndLine(PR_FALSE);
mInWhitespace = PR_TRUE;
} }
while(mEmptyLines < noOfRows) { while(mEmptyLines < noOfRows) {
EndLine(PR_FALSE); EndLine(PR_FALSE);
mInWhitespace = PR_TRUE;
} }
mLineBreakDue = PR_FALSE; mLineBreakDue = PR_FALSE;
mFloatingLines = -1; mFloatingLines = -1;

Просмотреть файл

@ -46,6 +46,7 @@
#include "nsStringGlue.h" #include "nsStringGlue.h"
#include "nsParserCIID.h" #include "nsParserCIID.h"
#include "nsIDocumentEncoder.h" #include "nsIDocumentEncoder.h"
#include "nsCRT.h"
static NS_DEFINE_CID(kCParserCID, NS_PARSER_CID); static NS_DEFINE_CID(kCParserCID, NS_PARSER_CID);
@ -143,6 +144,27 @@ TestCJKWithFlowedDelSp()
return NS_OK; return NS_OK;
} }
nsresult
TestPrettyPrintedHtml()
{
nsString test;
test.AppendLiteral(
"<html>" NS_LINEBREAK
"<body>" NS_LINEBREAK
" first<br>" NS_LINEBREAK
" second<br>" NS_LINEBREAK
"</body>" NS_LINEBREAK "</html>");
ConvertBufToPlainText(test, 0);
if (!test.EqualsLiteral("first" NS_LINEBREAK "second" NS_LINEBREAK)) {
fail("Wrong prettyprinted html to text serialization");
return NS_ERROR_FAILURE;
}
passed("prettyprinted HTML to text serialization test");
return NS_OK;
}
nsresult nsresult
TestPlainTextSerializer() TestPlainTextSerializer()
{ {
@ -163,6 +185,9 @@ TestPlainTextSerializer()
rv = TestCJKWithFlowedDelSp(); rv = TestCJKWithFlowedDelSp();
NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_SUCCESS(rv, rv);
rv = TestPrettyPrintedHtml();
NS_ENSURE_SUCCESS(rv, rv);
// Add new tests here... // Add new tests here...
return NS_OK; return NS_OK;
} }

Просмотреть файл

@ -112,36 +112,36 @@ function testCopyPaste () {
testPasteText("This is a draggable bit of text."); testPasteText("This is a draggable bit of text.");
copyChildrenToClipboard("alist"); copyChildrenToClipboard("alist");
testSelectionToString("bla\n\n foo\n bar\n\n"); testSelectionToString(" bla\n\n foo\n bar\n\n");
testClipboardValue("text/unicode", " bla\n\n foo\n bar\n\n"); testClipboardValue("text/unicode", "bla\n\n foo\n bar\n\n");
testClipboardValue("text/html", "<div id=\"alist\">\n bla\n <ul>\n <li>foo</li>\n \n <li>bar</li>\n </ul>\n </div>"); testClipboardValue("text/html", "<div id=\"alist\">\n bla\n <ul>\n <li>foo</li>\n \n <li>bar</li>\n </ul>\n </div>");
testPasteText(" bla\n\n foo\n bar\n\n"); testPasteText("bla\n\n foo\n bar\n\n");
copyChildrenToClipboard("blist"); copyChildrenToClipboard("blist");
testSelectionToString("mozilla\n\n foo\n bar\n\n"); testSelectionToString(" mozilla\n\n foo\n bar\n\n");
testClipboardValue("text/unicode", " mozilla\n\n foo\n bar\n\n"); testClipboardValue("text/unicode", "mozilla\n\n foo\n bar\n\n");
testClipboardValue("text/html", "<div id=\"blist\">\n mozilla\n <ol>\n <li>foo</li>\n \n <li>bar</li>\n </ol>\n </div>"); testClipboardValue("text/html", "<div id=\"blist\">\n mozilla\n <ol>\n <li>foo</li>\n \n <li>bar</li>\n </ol>\n </div>");
testPasteText(" mozilla\n\n foo\n bar\n\n"); testPasteText("mozilla\n\n foo\n bar\n\n");
copyChildrenToClipboard("clist"); copyChildrenToClipboard("clist");
testSelectionToString("mzla\n\n foo\n bazzinga!\n bar\n\n"); testSelectionToString(" mzla\n\n foo\n bazzinga!\n bar\n\n");
testClipboardValue("text/unicode", " mzla\n\n foo\n bazzinga!\n bar\n\n"); testClipboardValue("text/unicode", "mzla\n\n foo\n bazzinga!\n bar\n\n");
testClipboardValue("text/html", "<div id=\"clist\">\n mzla\n <ul>\n <li>foo<ul>\n <li>bazzinga!</li>\n </ul></li>\n \n <li>bar</li>\n </ul>\n </div>"); testClipboardValue("text/html", "<div id=\"clist\">\n mzla\n <ul>\n <li>foo<ul>\n <li>bazzinga!</li>\n </ul></li>\n \n <li>bar</li>\n </ul>\n </div>");
testPasteText(" mzla\n\n foo\n bazzinga!\n bar\n\n"); testPasteText("mzla\n\n foo\n bazzinga!\n bar\n\n");
copyChildrenToClipboard("div4"); copyChildrenToClipboard("div4");
testSelectionToString("Tt t t "); testSelectionToString(" Tt t t ");
testClipboardValue("text/unicode", " Tt t t "); testClipboardValue("text/unicode", "Tt t t ");
testClipboardValue("text/html", "<div id=\"div4\">\n T<textarea>t t t</textarea>\n</div>"); testClipboardValue("text/html", "<div id=\"div4\">\n T<textarea>t t t</textarea>\n</div>");
testInnerHTML("div4", "\n T<textarea>t t t</textarea>\n"); testInnerHTML("div4", "\n T<textarea>t t t</textarea>\n");
testPasteText(" Tt t t "); testPasteText("Tt t t ");
copyChildrenToClipboard("div5"); copyChildrenToClipboard("div5");
testSelectionToString("T "); testSelectionToString(" T ");
testClipboardValue("text/unicode", " T "); testClipboardValue("text/unicode", "T ");
testClipboardValue("text/html", "<div id=\"div5\">\n T<textarea> </textarea>\n</div>"); testClipboardValue("text/html", "<div id=\"div5\">\n T<textarea> </textarea>\n</div>");
testInnerHTML("div5", "\n T<textarea> </textarea>\n"); testInnerHTML("div5", "\n T<textarea> </textarea>\n");
testPasteText(" T "); testPasteText("T ");
copyRangeToClipboard($("div6").childNodes[0],0, $("div6").childNodes[1],1); copyRangeToClipboard($("div6").childNodes[0],0, $("div6").childNodes[1],1);
testSelectionToString(""); testSelectionToString("");