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 "nsStringGlue.h"
11 #include "nsIDocumentEncoder.h"
12 #include "nsCRT.h"
13 #include "nsIParserUtils.h"
14 
15 void
ConvertBufToPlainText(nsString & aConBuf,int aFlag)16 ConvertBufToPlainText(nsString &aConBuf, int aFlag)
17 {
18   nsCOMPtr<nsIParserUtils> utils = do_GetService(NS_PARSERUTILS_CONTRACTID);
19   utils->ConvertToPlainText(aConBuf, aFlag, 72, 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("<html><body>"
29                      "Firefox Firefox Firefox Firefox "
30                      "Firefox Firefox Firefox Firefox "
31                      "Firefox Firefox Firefox Firefox"
32                      "</body></html>");
33 
34   ConvertBufToPlainText(test, nsIDocumentEncoder::OutputFormatted |
35                               nsIDocumentEncoder::OutputCRLineBreak |
36                               nsIDocumentEncoder::OutputLFLineBreak |
37                               nsIDocumentEncoder::OutputFormatFlowed |
38                               nsIDocumentEncoder::OutputFormatDelSp);
39 
40   // create result case
41   result.AssignLiteral("Firefox Firefox Firefox Firefox "
42                        "Firefox Firefox Firefox Firefox "
43                        "Firefox  \r\nFirefox Firefox Firefox\r\n");
44 
45   ASSERT_TRUE(test.Equals(result)) <<
46     "Wrong HTML to ASCII text serialization with format=flowed; delsp=yes";
47 }
48 
49 // Test for CJK with format=flowed; delsp=yes
TEST(PlainTextSerializer,CJKWithFlowedDelSp)50 TEST(PlainTextSerializer, CJKWithFlowedDelSp)
51 {
52   nsString test;
53   nsString result;
54 
55   test.AssignLiteral("<html><body>");
56   for (uint32_t i = 0; i < 40; i++) {
57     // Insert Kanji (U+5341)
58     test.Append(0x5341);
59   }
60   test.AppendLiteral("</body></html>");
61 
62   ConvertBufToPlainText(test, nsIDocumentEncoder::OutputFormatted |
63                               nsIDocumentEncoder::OutputCRLineBreak |
64                               nsIDocumentEncoder::OutputLFLineBreak |
65                               nsIDocumentEncoder::OutputFormatFlowed |
66                               nsIDocumentEncoder::OutputFormatDelSp);
67 
68   // create result case
69   for (uint32_t i = 0; i < 36; i++) {
70     result.Append(0x5341);
71   }
72   result.AppendLiteral(" \r\n");
73   for (uint32_t i = 0; i < 4; i++) {
74     result.Append(0x5341);
75   }
76   result.AppendLiteral("\r\n");
77 
78   ASSERT_TRUE(test.Equals(result)) <<
79     "Wrong HTML to CJK text serialization with format=flowed; delsp=yes";
80 }
81 
82 // Test for CJK with DisallowLineBreaking
TEST(PlainTextSerializer,CJKWithDisallowLineBreaking)83 TEST(PlainTextSerializer, CJKWithDisallowLineBreaking)
84 {
85   nsString test;
86   nsString result;
87 
88   test.AssignLiteral("<html><body>");
89   for (uint32_t i = 0; i < 400; i++) {
90     // Insert Kanji (U+5341)
91     test.Append(0x5341);
92   }
93   test.AppendLiteral("</body></html>");
94 
95   ConvertBufToPlainText(test, nsIDocumentEncoder::OutputFormatted |
96                               nsIDocumentEncoder::OutputCRLineBreak |
97                               nsIDocumentEncoder::OutputLFLineBreak |
98                               nsIDocumentEncoder::OutputFormatFlowed |
99                               nsIDocumentEncoder::OutputDisallowLineBreaking);
100 
101   // create result case
102   for (uint32_t i = 0; i < 400; i++) {
103     result.Append(0x5341);
104   }
105   result.AppendLiteral("\r\n");
106 
107   ASSERT_TRUE(test.Equals(result)) <<
108     "Wrong HTML to CJK text serialization with OutputDisallowLineBreaking";
109 }
110 
111 // Test for ASCII with format=flowed; and quoted lines in preformatted span.
TEST(PlainTextSerializer,PreformatFlowedQuotes)112 TEST(PlainTextSerializer, PreformatFlowedQuotes)
113 {
114   nsString test;
115   nsString result;
116 
117   test.AssignLiteral("<html><body>"
118                      "<span style=\"white-space: pre-wrap;\">"
119                      "&gt; Firefox Firefox Firefox Firefox <br>"
120                      "&gt; Firefox Firefox Firefox Firefox<br>"
121                      "&gt;<br>"
122                      "&gt;&gt; Firefox Firefox Firefox Firefox <br>"
123                      "&gt;&gt; Firefox Firefox Firefox Firefox<br>"
124                      "</span></body></html>");
125 
126   ConvertBufToPlainText(test, nsIDocumentEncoder::OutputFormatted |
127                               nsIDocumentEncoder::OutputCRLineBreak |
128                               nsIDocumentEncoder::OutputLFLineBreak |
129                               nsIDocumentEncoder::OutputFormatFlowed);
130 
131   // create result case
132   result.AssignLiteral("> Firefox Firefox Firefox Firefox \r\n"
133                        "> Firefox Firefox Firefox Firefox\r\n"
134                        ">\r\n"
135                        ">> Firefox Firefox Firefox Firefox \r\n"
136                        ">> Firefox Firefox Firefox Firefox\r\n");
137 
138   ASSERT_TRUE(test.Equals(result)) <<
139     "Wrong HTML to ASCII text serialization with format=flowed; and quoted "
140     "lines";
141 }
142 
TEST(PlainTextSerializer,PrettyPrintedHtml)143 TEST(PlainTextSerializer, PrettyPrintedHtml)
144 {
145   nsString test;
146   test.AppendLiteral(
147     "<html>" NS_LINEBREAK
148     "<body>" NS_LINEBREAK
149     "  first<br>" NS_LINEBREAK
150     "  second<br>" NS_LINEBREAK
151     "</body>" NS_LINEBREAK "</html>");
152 
153   ConvertBufToPlainText(test, 0);
154   ASSERT_TRUE(test.EqualsLiteral("first" NS_LINEBREAK "second" NS_LINEBREAK)) <<
155     "Wrong prettyprinted html to text serialization";
156 }
157 
TEST(PlainTextSerializer,PreElement)158 TEST(PlainTextSerializer, PreElement)
159 {
160   nsString test;
161   test.AppendLiteral(
162     "<html>" NS_LINEBREAK
163     "<body>" NS_LINEBREAK
164     "<pre>" NS_LINEBREAK
165     "  first" NS_LINEBREAK
166     "  second" NS_LINEBREAK
167     "</pre>" NS_LINEBREAK
168     "</body>" NS_LINEBREAK "</html>");
169 
170   ConvertBufToPlainText(test, 0);
171   ASSERT_TRUE(test.EqualsLiteral("  first" NS_LINEBREAK
172                                  "  second" NS_LINEBREAK NS_LINEBREAK)) <<
173     "Wrong prettyprinted html to text serialization";
174 }
175 
TEST(PlainTextSerializer,BlockElement)176 TEST(PlainTextSerializer, BlockElement)
177 {
178   nsString test;
179   test.AppendLiteral(
180     "<html>" NS_LINEBREAK
181     "<body>" NS_LINEBREAK
182     "<div>" NS_LINEBREAK
183     "  first" NS_LINEBREAK
184     "</div>" NS_LINEBREAK
185     "<div>" NS_LINEBREAK
186     "  second" NS_LINEBREAK
187     "</div>" NS_LINEBREAK
188     "</body>" NS_LINEBREAK "</html>");
189 
190   ConvertBufToPlainText(test, 0);
191   ASSERT_TRUE(test.EqualsLiteral("first" NS_LINEBREAK "second" NS_LINEBREAK)) <<
192     "Wrong prettyprinted html to text serialization";
193 }
194 
TEST(PlainTextSerializer,PreWrapElementForThunderbird)195 TEST(PlainTextSerializer, PreWrapElementForThunderbird)
196 {
197   // This test examines the magic pre-wrap setup that Thunderbird relies on.
198   nsString test;
199   test.AppendLiteral(
200     "<html>" NS_LINEBREAK
201     "<body style=\"white-space: pre-wrap; width: 10ch;\">" 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
206     "</body>" NS_LINEBREAK "</html>");
207 
208   ConvertBufToPlainText(test, nsIDocumentEncoder::OutputWrap);
209   // "\n\n  first\nline is\ntoo long\n  second\nline is\neven\nloooonger\n\n\n"
210   ASSERT_TRUE(test.EqualsLiteral(NS_LINEBREAK NS_LINEBREAK
211                                  "  first" NS_LINEBREAK
212                                  "line is" NS_LINEBREAK
213                                  "too long" NS_LINEBREAK
214                                  "  second" NS_LINEBREAK
215                                  "line is" NS_LINEBREAK
216                                  "even" NS_LINEBREAK
217                                  "loooonger" NS_LINEBREAK
218                                  NS_LINEBREAK NS_LINEBREAK)) <<
219     "Wrong prettyprinted html to text serialization";
220 }
221 
TEST(PlainTextSerializer,Simple)222 TEST(PlainTextSerializer, Simple)
223 {
224   nsString test;
225   test.AppendLiteral("<html><base>base</base><head><span>span</span></head>"
226                      "<body>body</body></html>");
227   ConvertBufToPlainText(test, 0);
228   ASSERT_TRUE(test.EqualsLiteral("basespanbody")) <<
229     "Wrong html to text serialization";
230 }
231 
232