From 404c0dae7b651e1a4f5dee053ed261678d16254c Mon Sep 17 00:00:00 2001 From: Mirko Brodesser Date: Mon, 23 Sep 2019 11:43:53 +0000 Subject: [PATCH] Bug 1581523: part 1) Remove hack for Thunderbird which parses `nsPlainTextSerializer::mWrapColumn` from ``'s style. r=hsivonen,jorgk `TextEditor::SetWrapWidth` already sets `nsIDocumentEncoder`'s wrap column which is propagated to `nsPlainTextSerializer`. So this should be a safe change. Differential Revision: https://phabricator.services.mozilla.com/D46008 --HG-- extra : moz-landing-system : lando --- dom/base/nsPlainTextSerializer.cpp | 24 ------- .../test/gtest/TestPlainTextSerializer.cpp | 66 +++++++++++-------- 2 files changed, 38 insertions(+), 52 deletions(-) diff --git a/dom/base/nsPlainTextSerializer.cpp b/dom/base/nsPlainTextSerializer.cpp index 198e82d1eace..8dbb840162b2 100644 --- a/dom/base/nsPlainTextSerializer.cpp +++ b/dom/base/nsPlainTextSerializer.cpp @@ -684,8 +684,6 @@ nsresult nsPlainTextSerializer::DoOpenContainer(const nsAtom* aTag) { // Trigger on the presence of a "pre-wrap" in the // style attribute. That's a very simplistic way to do // it, but better than nothing. - // Also set mWrapColumn to the value given there - // (which arguably we should only do if told to do so). nsAutoString style; int32_t whitespace; if (NS_SUCCEEDED(GetAttributeValue(nsGkAtoms::style, style)) && @@ -695,28 +693,6 @@ nsresult nsPlainTextSerializer::DoOpenContainer(const nsAtom* aTag) { printf("Set mPreFormattedMail based on style pre-wrap\n"); #endif mPreFormattedMail = true; - int32_t widthOffset = style.Find("width:"); - if (widthOffset >= 0) { - // We have to search for the ch before the semicolon, - // not for the semicolon itself, because nsString::ToInteger() - // considers 'c' to be a valid numeric char (even if radix=10) - // but then gets confused if it sees it next to the number - // when the radix specified was 10, and returns an error code. - int32_t semiOffset = style.Find("ch", false, widthOffset + 6); - int32_t length = (semiOffset > 0 ? semiOffset - widthOffset - 6 - : style.Length() - widthOffset); - nsAutoString widthstr; - style.Mid(widthstr, widthOffset + 6, length); - nsresult err; - int32_t col = widthstr.ToInteger(&err); - - if (NS_SUCCEEDED(err)) { - mWrapColumn = (uint32_t)col; -#ifdef DEBUG_preformatted - printf("Set wrap column to %d based on style\n", mWrapColumn); -#endif - } - } } else if (kNotFound != style.Find("pre", true, whitespace)) { #ifdef DEBUG_preformatted printf("Set mPreFormattedMail based on style pre\n"); diff --git a/dom/base/test/gtest/TestPlainTextSerializer.cpp b/dom/base/test/gtest/TestPlainTextSerializer.cpp index ef2e37e26397..8ae8ec7c2cfb 100644 --- a/dom/base/test/gtest/TestPlainTextSerializer.cpp +++ b/dom/base/test/gtest/TestPlainTextSerializer.cpp @@ -12,9 +12,11 @@ #include "nsCRT.h" #include "nsIParserUtils.h" -void ConvertBufToPlainText(nsString& aConBuf, int aFlag) { +const uint32_t kDefaultWrapColumn = 72; + +void ConvertBufToPlainText(nsString& aConBuf, int aFlag, uint32_t aWrapColumn) { nsCOMPtr utils = do_GetService(NS_PARSERUTILS_CONTRACTID); - utils->ConvertToPlainText(aConBuf, aFlag, 72, aConBuf); + utils->ConvertToPlainText(aConBuf, aFlag, aWrapColumn, aConBuf); } // Test for ASCII with format=flowed; delsp=yes @@ -30,11 +32,13 @@ TEST(PlainTextSerializer, ASCIIWithFlowedDelSp) "Firefox Firefox Firefox Firefox" ""); - ConvertBufToPlainText(test, nsIDocumentEncoder::OutputFormatted | - nsIDocumentEncoder::OutputCRLineBreak | - nsIDocumentEncoder::OutputLFLineBreak | - nsIDocumentEncoder::OutputFormatFlowed | - nsIDocumentEncoder::OutputFormatDelSp); + ConvertBufToPlainText(test, + nsIDocumentEncoder::OutputFormatted | + nsIDocumentEncoder::OutputCRLineBreak | + nsIDocumentEncoder::OutputLFLineBreak | + nsIDocumentEncoder::OutputFormatFlowed | + nsIDocumentEncoder::OutputFormatDelSp, + kDefaultWrapColumn); // create result case result.AssignLiteral( @@ -59,11 +63,13 @@ TEST(PlainTextSerializer, CJKWithFlowedDelSp) } test.AppendLiteral(""); - ConvertBufToPlainText(test, nsIDocumentEncoder::OutputFormatted | - nsIDocumentEncoder::OutputCRLineBreak | - nsIDocumentEncoder::OutputLFLineBreak | - nsIDocumentEncoder::OutputFormatFlowed | - nsIDocumentEncoder::OutputFormatDelSp); + ConvertBufToPlainText(test, + nsIDocumentEncoder::OutputFormatted | + nsIDocumentEncoder::OutputCRLineBreak | + nsIDocumentEncoder::OutputLFLineBreak | + nsIDocumentEncoder::OutputFormatFlowed | + nsIDocumentEncoder::OutputFormatDelSp, + kDefaultWrapColumn); // create result case for (uint32_t i = 0; i < 36; i++) { @@ -97,7 +103,8 @@ TEST(PlainTextSerializer, CJKWithDisallowLineBreaking) nsIDocumentEncoder::OutputCRLineBreak | nsIDocumentEncoder::OutputLFLineBreak | nsIDocumentEncoder::OutputFormatFlowed | - nsIDocumentEncoder::OutputDisallowLineBreaking); + nsIDocumentEncoder::OutputDisallowLineBreaking, + kDefaultWrapColumn); // create result case for (uint32_t i = 0; i < 400; i++) { @@ -125,10 +132,12 @@ TEST(PlainTextSerializer, PreformatFlowedQuotes) ">> Firefox Firefox Firefox Firefox
" ""); - ConvertBufToPlainText(test, nsIDocumentEncoder::OutputFormatted | - nsIDocumentEncoder::OutputCRLineBreak | - nsIDocumentEncoder::OutputLFLineBreak | - nsIDocumentEncoder::OutputFormatFlowed); + ConvertBufToPlainText(test, + nsIDocumentEncoder::OutputFormatted | + nsIDocumentEncoder::OutputCRLineBreak | + nsIDocumentEncoder::OutputLFLineBreak | + nsIDocumentEncoder::OutputFormatFlowed, + kDefaultWrapColumn); // create result case result.AssignLiteral( @@ -151,7 +160,7 @@ TEST(PlainTextSerializer, PrettyPrintedHtml) " first
" NS_LINEBREAK " second
" NS_LINEBREAK "" NS_LINEBREAK ""); - ConvertBufToPlainText(test, 0); + ConvertBufToPlainText(test, 0, kDefaultWrapColumn); ASSERT_TRUE(test.EqualsLiteral("first" NS_LINEBREAK "second" NS_LINEBREAK)) << "Wrong prettyprinted html to text serialization"; } @@ -164,7 +173,7 @@ TEST(PlainTextSerializer, PreElement) " second" NS_LINEBREAK "" NS_LINEBREAK "" NS_LINEBREAK ""); - ConvertBufToPlainText(test, 0); + ConvertBufToPlainText(test, 0, kDefaultWrapColumn); ASSERT_TRUE(test.EqualsLiteral(" first" NS_LINEBREAK " second" NS_LINEBREAK NS_LINEBREAK)) << "Wrong prettyprinted html to text serialization"; @@ -179,7 +188,7 @@ TEST(PlainTextSerializer, BlockElement) " second" NS_LINEBREAK "" NS_LINEBREAK "" NS_LINEBREAK ""); - ConvertBufToPlainText(test, 0); + ConvertBufToPlainText(test, 0, kDefaultWrapColumn); ASSERT_TRUE(test.EqualsLiteral("first" NS_LINEBREAK "second" NS_LINEBREAK)) << "Wrong prettyprinted html to text serialization"; } @@ -188,14 +197,15 @@ TEST(PlainTextSerializer, PreWrapElementForThunderbird) { // This test examines the magic pre-wrap setup that Thunderbird relies on. nsString test; - test.AppendLiteral( - "" NS_LINEBREAK - "" NS_LINEBREAK - "
" NS_LINEBREAK "  first line is too long" NS_LINEBREAK
-      "  second line is even loooonger  " NS_LINEBREAK "
" NS_LINEBREAK - "" NS_LINEBREAK ""); + test.AppendLiteral("" NS_LINEBREAK + "" NS_LINEBREAK + "
" NS_LINEBREAK
+                     "  first line is too long" NS_LINEBREAK
+                     "  second line is even loooonger  " NS_LINEBREAK
+                     "
" NS_LINEBREAK "" NS_LINEBREAK ""); - ConvertBufToPlainText(test, nsIDocumentEncoder::OutputWrap); + const uint32_t wrapColumn = 10; + ConvertBufToPlainText(test, nsIDocumentEncoder::OutputWrap, wrapColumn); // "\n\n first\nline is\ntoo long\n second\nline is\neven\nloooonger\n\n\n" ASSERT_TRUE(test.EqualsLiteral( NS_LINEBREAK NS_LINEBREAK @@ -211,7 +221,7 @@ TEST(PlainTextSerializer, Simple) test.AppendLiteral( "basespan" "body"); - ConvertBufToPlainText(test, 0); + ConvertBufToPlainText(test, 0, kDefaultWrapColumn); ASSERT_TRUE(test.EqualsLiteral("basespanbody")) << "Wrong html to text serialization"; }