1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include "gtest/gtest.h"
8 
9 #include "nsServiceManagerUtils.h"
10 #include "nsString.h"
11 #include "nsIDocumentEncoder.h"
12 #include "nsCRT.h"
13 #include "nsIParserUtils.h"
14 
15 const uint32_t kDefaultWrapColumn = 72;
16 
ConvertBufToPlainText(nsString & aConBuf,int aFlag,uint32_t aWrapColumn)17 void ConvertBufToPlainText(nsString& aConBuf, int aFlag, uint32_t aWrapColumn) {
18   nsCOMPtr<nsIParserUtils> utils = do_GetService(NS_PARSERUTILS_CONTRACTID);
19   utils->ConvertToPlainText(aConBuf, aFlag, aWrapColumn, aConBuf);
20 }
21 
22 // Test for ASCII with format=flowed; delsp=yes
TEST(PlainTextSerializer,ASCIIWithFlowedDelSp)23 TEST(PlainTextSerializer, ASCIIWithFlowedDelSp)
24 {
25   nsString test;
26   nsString result;
27 
28   test.AssignLiteral(
29       "<html><body>"
30       "Firefox Firefox Firefox Firefox "
31       "Firefox Firefox Firefox Firefox "
32       "Firefox Firefox Firefox Firefox"
33       "</body></html>");
34 
35   ConvertBufToPlainText(test,
36                         nsIDocumentEncoder::OutputFormatted |
37                             nsIDocumentEncoder::OutputCRLineBreak |
38                             nsIDocumentEncoder::OutputLFLineBreak |
39                             nsIDocumentEncoder::OutputFormatFlowed |
40                             nsIDocumentEncoder::OutputFormatDelSp,
41                         kDefaultWrapColumn);
42 
43   // create result case
44   result.AssignLiteral(
45       "Firefox Firefox Firefox Firefox "
46       "Firefox Firefox Firefox Firefox "
47       "Firefox  \r\nFirefox Firefox Firefox\r\n");
48 
49   ASSERT_TRUE(test.Equals(result))
50   << "Wrong HTML to ASCII text serialization with format=flowed; delsp=yes";
51 }
52 
53 // Test for CJK with format=flowed; delsp=yes
TEST(PlainTextSerializer,CJKWithFlowedDelSp)54 TEST(PlainTextSerializer, CJKWithFlowedDelSp)
55 {
56   nsString test;
57   nsString result;
58 
59   test.AssignLiteral("<html><body>");
60   for (uint32_t i = 0; i < 40; i++) {
61     // Insert Kanji (U+5341)
62     test.Append(0x5341);
63   }
64   test.AppendLiteral("</body></html>");
65 
66   ConvertBufToPlainText(test,
67                         nsIDocumentEncoder::OutputFormatted |
68                             nsIDocumentEncoder::OutputCRLineBreak |
69                             nsIDocumentEncoder::OutputLFLineBreak |
70                             nsIDocumentEncoder::OutputFormatFlowed |
71                             nsIDocumentEncoder::OutputFormatDelSp,
72                         kDefaultWrapColumn);
73 
74   // create result case
75   for (uint32_t i = 0; i < 36; i++) {
76     result.Append(0x5341);
77   }
78   result.AppendLiteral(" \r\n");
79   for (uint32_t i = 0; i < 4; i++) {
80     result.Append(0x5341);
81   }
82   result.AppendLiteral("\r\n");
83 
84   ASSERT_TRUE(test.Equals(result))
85   << "Wrong HTML to CJK text serialization with format=flowed; delsp=yes";
86 }
87 
88 // Test for CJK with DisallowLineBreaking
TEST(PlainTextSerializer,CJKWithDisallowLineBreaking)89 TEST(PlainTextSerializer, CJKWithDisallowLineBreaking)
90 {
91   nsString test;
92   nsString result;
93 
94   test.AssignLiteral("<html><body>");
95   for (uint32_t i = 0; i < 400; i++) {
96     // Insert Kanji (U+5341)
97     test.Append(0x5341);
98   }
99   test.AppendLiteral("</body></html>");
100 
101   ConvertBufToPlainText(test,
102                         nsIDocumentEncoder::OutputFormatted |
103                             nsIDocumentEncoder::OutputCRLineBreak |
104                             nsIDocumentEncoder::OutputLFLineBreak |
105                             nsIDocumentEncoder::OutputFormatFlowed |
106                             nsIDocumentEncoder::OutputDisallowLineBreaking,
107                         kDefaultWrapColumn);
108 
109   // create result case
110   for (uint32_t i = 0; i < 400; i++) {
111     result.Append(0x5341);
112   }
113   result.AppendLiteral("\r\n");
114 
115   ASSERT_TRUE(test.Equals(result))
116   << "Wrong HTML to CJK text serialization with OutputDisallowLineBreaking";
117 }
118 
119 // Test for ASCII with format=flowed; and quoted lines in preformatted span.
TEST(PlainTextSerializer,PreformatFlowedQuotes)120 TEST(PlainTextSerializer, PreformatFlowedQuotes)
121 {
122   nsString test;
123   nsString result;
124 
125   test.AssignLiteral(
126       "<html><body>"
127       "<span style=\"white-space: pre-wrap;\" _moz_quote=\"true\">"
128       "&gt; Firefox Firefox Firefox Firefox <br>"
129       "&gt; Firefox Firefox Firefox Firefox<br>"
130       "&gt;<br>"
131       "&gt;&gt; Firefox Firefox Firefox Firefox <br>"
132       "&gt;&gt; Firefox Firefox Firefox Firefox<br>"
133       "</span></body></html>");
134 
135   ConvertBufToPlainText(test,
136                         nsIDocumentEncoder::OutputFormatted |
137                             nsIDocumentEncoder::OutputCRLineBreak |
138                             nsIDocumentEncoder::OutputLFLineBreak |
139                             nsIDocumentEncoder::OutputFormatFlowed,
140                         kDefaultWrapColumn);
141 
142   // create result case
143   result.AssignLiteral(
144       "> Firefox Firefox Firefox Firefox \r\n"
145       "> Firefox Firefox Firefox Firefox\r\n"
146       ">\r\n"
147       ">> Firefox Firefox Firefox Firefox \r\n"
148       ">> Firefox Firefox Firefox Firefox\r\n");
149 
150   ASSERT_TRUE(test.Equals(result))
151   << "Wrong HTML to ASCII text serialization "
152      "with format=flowed; and quoted "
153      "lines";
154 }
155 
TEST(PlainTextSerializer,PrettyPrintedHtml)156 TEST(PlainTextSerializer, PrettyPrintedHtml)
157 {
158   nsString test;
159   test.AppendLiteral("<html>" NS_LINEBREAK "<body>" NS_LINEBREAK
160                      "  first<br>" NS_LINEBREAK "  second<br>" NS_LINEBREAK
161                      "</body>" NS_LINEBREAK "</html>");
162 
163   ConvertBufToPlainText(test, 0, kDefaultWrapColumn);
164   ASSERT_TRUE(test.EqualsLiteral("first" NS_LINEBREAK "second" NS_LINEBREAK))
165   << "Wrong prettyprinted html to text serialization";
166 }
167 
TEST(PlainTextSerializer,PreElement)168 TEST(PlainTextSerializer, PreElement)
169 {
170   nsString test;
171   test.AppendLiteral("<html>" NS_LINEBREAK "<body>" NS_LINEBREAK
172                      "<pre>" NS_LINEBREAK "  first" NS_LINEBREAK
173                      "  second" NS_LINEBREAK "</pre>" NS_LINEBREAK
174                      "</body>" NS_LINEBREAK "</html>");
175 
176   ConvertBufToPlainText(test, 0, kDefaultWrapColumn);
177   ASSERT_TRUE(test.EqualsLiteral("  first" NS_LINEBREAK
178                                  "  second" NS_LINEBREAK NS_LINEBREAK))
179   << "Wrong prettyprinted html to text serialization";
180 }
181 
TEST(PlainTextSerializer,BlockElement)182 TEST(PlainTextSerializer, BlockElement)
183 {
184   nsString test;
185   test.AppendLiteral("<html>" NS_LINEBREAK "<body>" NS_LINEBREAK
186                      "<div>" NS_LINEBREAK "  first" NS_LINEBREAK
187                      "</div>" NS_LINEBREAK "<div>" NS_LINEBREAK
188                      "  second" NS_LINEBREAK "</div>" NS_LINEBREAK
189                      "</body>" NS_LINEBREAK "</html>");
190 
191   ConvertBufToPlainText(test, 0, kDefaultWrapColumn);
192   ASSERT_TRUE(test.EqualsLiteral("first" NS_LINEBREAK "second" NS_LINEBREAK))
193   << "Wrong prettyprinted html to text serialization";
194 }
195 
TEST(PlainTextSerializer,PreWrapElementForThunderbird)196 TEST(PlainTextSerializer, PreWrapElementForThunderbird)
197 {
198   // This test examines the magic pre-wrap setup that Thunderbird relies on.
199   nsString test;
200   test.AppendLiteral("<html>" NS_LINEBREAK
201                      "<body style=\"white-space: pre-wrap;\">" NS_LINEBREAK
202                      "<pre>" NS_LINEBREAK
203                      "  first line is too long" NS_LINEBREAK
204                      "  second line is even loooonger  " NS_LINEBREAK
205                      "</pre>" NS_LINEBREAK "</body>" NS_LINEBREAK "</html>");
206 
207   const uint32_t wrapColumn = 10;
208   ConvertBufToPlainText(test, nsIDocumentEncoder::OutputWrap, wrapColumn);
209   // "\n\n  first\nline is\ntoo long\n  second\nline is\neven\nloooonger\n\n\n"
210   ASSERT_TRUE(test.EqualsLiteral(
211       NS_LINEBREAK NS_LINEBREAK
212       "  first" NS_LINEBREAK "line is" NS_LINEBREAK "too long" NS_LINEBREAK
213       "  second" NS_LINEBREAK "line is" NS_LINEBREAK "even" NS_LINEBREAK
214       "loooonger" NS_LINEBREAK NS_LINEBREAK NS_LINEBREAK))
215   << "Wrong prettyprinted html to text serialization";
216 }
217 
TEST(PlainTextSerializer,Simple)218 TEST(PlainTextSerializer, Simple)
219 {
220   nsString test;
221   test.AppendLiteral(
222       "<html><base>base</base><head><span>span</span></head>"
223       "<body>body</body></html>");
224   ConvertBufToPlainText(test, 0, kDefaultWrapColumn);
225   ASSERT_TRUE(test.EqualsLiteral("basespanbody"))
226   << "Wrong html to text serialization";
227 }
228 
TEST(PlainTextSerializer,OneHundredAndOneOL)229 TEST(PlainTextSerializer, OneHundredAndOneOL)
230 {
231   nsAutoString test;
232   test.AppendLiteral(
233       "<html>"
234       "<body>"
235       "<ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><"
236       "ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><"
237       "ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><"
238       "ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><"
239       "ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><"
240       "ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><ol><ol></ol></ol></ol></"
241       "ol></ol></ol></ol></ol></ol></ol></ol></ol></ol></ol></ol></"
242       "ol></ol></ol></ol></ol></ol></ol></ol></ol></ol></ol></ol></ol></ol></"
243       "ol></ol></ol></ol></ol></ol></ol></ol></ol></ol></ol></ol></ol></ol></"
244       "ol></ol></ol></ol></ol></ol></ol></ol></ol></ol></ol></ol></ol></ol></"
245       "ol></ol></ol></ol></ol></ol></ol></ol></ol></ol></ol></ol></ol></ol></"
246       "ol></ol></ol></ol></ol></ol></ol></ol></ol></ol></ol></ol></ol></ol></"
247       "ol></ol></ol></ol></ol></ol></ol></ol></ol></ol></ol></ol></ol></ol></"
248       "ol></ol><li>X</li></ol>"
249       "</body>"
250       "</html>");
251 
252   ConvertBufToPlainText(test, nsIDocumentEncoder::OutputFormatted,
253                         kDefaultWrapColumn);
254 
255   nsAutoString expected;
256   expected.AppendLiteral(" 1. X" NS_LINEBREAK);
257   ASSERT_EQ(test, expected);
258 }
259