1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsTXTToHTMLConv.h"
#include "nsEscape.h"
#include "nsStringStream.h"
#include "nsAutoPtr.h"
#include "nsIChannel.h"
#include <algorithm>
#include "mozilla/UniquePtrExtensions.h"
#define TOKEN_DELIMITERS u"\t\r\n "
using namespace mozilla;
// nsISupports methods
NS_IMPL_ISUPPORTS(nsTXTToHTMLConv,
nsIStreamConverter,
nsITXTToHTMLConv,
nsIRequestObserver,
nsIStreamListener)
// nsIStreamConverter methods
NS_IMETHODIMP
nsTXTToHTMLConv::Convert(nsIInputStream *aFromStream,
const char *aFromType, const char *aToType,
nsISupports *aCtxt, nsIInputStream * *_retval)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsTXTToHTMLConv::AsyncConvertData(const char *aFromType,
const char *aToType,
nsIStreamListener *aListener,
nsISupports *aCtxt)
{
NS_ASSERTION(aListener, "null pointer");
mListener = aListener;
return NS_OK;
}
// nsIRequestObserver methods
NS_IMETHODIMP
nsTXTToHTMLConv::OnStartRequest(nsIRequest* request, nsISupports *aContext)
{
mBuffer.AssignLiteral("<html>\n<head><title>");
mBuffer.Append(mPageTitle);
mBuffer.AppendLiteral("</title></head>\n<body>\n");
if (mPreFormatHTML) { // Use <pre> tags
mBuffer.AppendLiteral("<pre>\n");
}
// Push mBuffer to the listener now, so the initial HTML will not
// be parsed in OnDataAvailable().
nsCOMPtr<nsIChannel> channel = do_QueryInterface(request);
if (channel)
channel->SetContentType(NS_LITERAL_CSTRING("text/html"));
// else, assume there is a channel somewhere that knows what it is doing!
nsresult rv = mListener->OnStartRequest(request, aContext);
if (NS_FAILED(rv)) return rv;
// The request may have been canceled, and if that happens, we want to
// suppress calls to OnDataAvailable.
request->GetStatus(&rv);
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsIInputStream> inputData;
NS_LossyConvertUTF16toASCII asciiData(mBuffer);
rv = NS_NewCStringInputStream(getter_AddRefs(inputData), asciiData);
if (NS_FAILED(rv)) return rv;
rv = mListener->OnDataAvailable(request, aContext,
inputData, 0, mBuffer.Length());
if (NS_FAILED(rv)) return rv;
mBuffer.Truncate();
return rv;
}
NS_IMETHODIMP
nsTXTToHTMLConv::OnStopRequest(nsIRequest* request, nsISupports *aContext,
nsresult aStatus)
{
nsresult rv = NS_OK;
if (mToken) {
// we still have an outstanding token
NS_ASSERTION(mToken->prepend,
"Non prepending tokens should be handled in "
"OnDataAvailable. There should only be a single "
"prepending token left to be processed.");
(void)CatHTML(0, mBuffer.Length());
}
if (mPreFormatHTML) {
mBuffer.AppendLiteral("</pre>\n");
}
mBuffer.AppendLiteral("\n</body></html>");
nsCOMPtr<nsIInputStream> inputData;
NS_LossyConvertUTF16toASCII asciiData(mBuffer);
rv = NS_NewCStringInputStream(getter_AddRefs(inputData), asciiData);
if (NS_FAILED(rv)) return rv;
rv = mListener->OnDataAvailable(request, aContext,
inputData, 0, mBuffer.Length());
if (NS_FAILED(rv)) return rv;
return mListener->OnStopRequest(request, aContext, aStatus);
}
// nsITXTToHTMLConv methods
NS_IMETHODIMP
nsTXTToHTMLConv::SetTitle(const char16_t *aTitle)
{
mPageTitle.Assign(aTitle);
return NS_OK;
}
NS_IMETHODIMP
nsTXTToHTMLConv::PreFormatHTML(bool value)
{
mPreFormatHTML = value;
return NS_OK;
}
// nsIStreamListener method
NS_IMETHODIMP
nsTXTToHTMLConv::OnDataAvailable(nsIRequest* request, nsISupports *aContext,
nsIInputStream *aInStream,
uint64_t aOffset, uint32_t aCount)
{
nsresult rv = NS_OK;
nsString pushBuffer;
uint32_t amtRead = 0;
auto buffer = MakeUniqueFallible<char[]>(aCount+1);
if (!buffer) return NS_ERROR_OUT_OF_MEMORY;
do {
uint32_t read = 0;
// XXX readSegments, to avoid the first copy?
rv = aInStream->Read(buffer.get(), aCount-amtRead, &read);
if (NS_FAILED(rv)) return rv;
buffer[read] = '\0';
// XXX charsets?? non-latin1 characters?? utf-16??
AppendASCIItoUTF16(buffer.get(), mBuffer);
amtRead += read;
int32_t front = -1, back = -1, tokenLoc = -1, cursor = 0;
while ( (tokenLoc = FindToken(cursor, &mToken)) > -1) {
if (mToken->prepend) {
front = mBuffer.RFindCharInSet(TOKEN_DELIMITERS, tokenLoc);
front++;
back = mBuffer.FindCharInSet(TOKEN_DELIMITERS, tokenLoc);
} else {
front = tokenLoc;
back = front + mToken->token.Length();
}
if (back == -1) {
// didn't find an ending, buffer up.
mBuffer.Left(pushBuffer, front);
cursor = front;
break;
}
// found the end of the token.
cursor = CatHTML(front, back);
}
int32_t end = mBuffer.RFind(TOKEN_DELIMITERS, mBuffer.Length());
mBuffer.Left(pushBuffer, std::max(cursor, end));
mBuffer.Cut(0, std::max(cursor, end));
cursor = 0;
if (!pushBuffer.IsEmpty()) {
nsCOMPtr<nsIInputStream> inputData;
NS_LossyConvertUTF16toASCII asciiData(pushBuffer);
rv = NS_NewCStringInputStream(getter_AddRefs(inputData), asciiData);
if (NS_FAILED(rv))
return rv;
rv = mListener->OnDataAvailable(request, aContext,
inputData, 0, pushBuffer.Length());
if (NS_FAILED(rv))
return rv;
}
} while (amtRead < aCount);
return rv;
}
// nsTXTToHTMLConv methods
nsTXTToHTMLConv::nsTXTToHTMLConv()
{
mToken = nullptr;
mPreFormatHTML = false;
}
nsTXTToHTMLConv::~nsTXTToHTMLConv()
{
mTokens.Clear();
}
nsresult
nsTXTToHTMLConv::Init()
{
nsresult rv = NS_OK;
// build up the list of tokens to handle
convToken *token = new convToken;
if (!token) return NS_ERROR_OUT_OF_MEMORY;
token->prepend = false;
token->token.Assign(char16_t('<'));
token->modText.AssignLiteral("<");
mTokens.AppendElement(token);
token = new convToken;
if (!token) return NS_ERROR_OUT_OF_MEMORY;
token->prepend = false;
token->token.Assign(char16_t('>'));
token->modText.AssignLiteral(">");
mTokens.AppendElement(token);
token = new convToken;
if (!token) return NS_ERROR_OUT_OF_MEMORY;
token->prepend = false;
token->token.Assign(char16_t('&'));
token->modText.AssignLiteral("&");
mTokens.AppendElement(token);
token = new convToken;
if (!token) return NS_ERROR_OUT_OF_MEMORY;
token->prepend = true;
token->token.AssignLiteral("http://"); // XXX need to iterate through all protos
mTokens.AppendElement(token);
token = new convToken;
if (!token) return NS_ERROR_OUT_OF_MEMORY;
token->prepend = true;
token->token.Assign(char16_t('@'));
token->modText.AssignLiteral("mailto:");
mTokens.AppendElement(token);
return rv;
}
int32_t
nsTXTToHTMLConv::FindToken(int32_t cursor, convToken* *_retval)
{
int32_t loc = -1, firstToken = mBuffer.Length();
int8_t token = -1;
for (uint8_t i=0; i < mTokens.Length(); i++) {
loc = mBuffer.Find(mTokens[i]->token, cursor);
if (loc != -1)
if (loc < firstToken) {
firstToken = loc;
token = i;
}
}
if (token == -1)
return -1;
*_retval = mTokens[token];
return firstToken;
}
int32_t
nsTXTToHTMLConv::CatHTML(int32_t front, int32_t back)
{
int32_t cursor = 0;
int32_t modLen = mToken->modText.Length();
if (!mToken->prepend) {
// replace the entire token (from delimiter to delimiter)
mBuffer.Cut(front, back - front);
mBuffer.Insert(mToken->modText, front);
cursor = front+modLen;
} else {
nsString linkText;
// href is implied
mBuffer.Mid(linkText, front, back-front);
mBuffer.Insert(NS_LITERAL_STRING("<a href=\""), front);
cursor += front+9;
if (modLen) {
mBuffer.Insert(mToken->modText, cursor);
cursor += modLen;
}
NS_ConvertUTF16toUTF8 linkTextUTF8(linkText);
nsCString escaped;
if (NS_EscapeURL(linkTextUTF8.Data(), linkTextUTF8.Length(), esc_Minimal, escaped)) {
mBuffer.Cut(cursor, back - front);
CopyUTF8toUTF16(escaped, linkText);
mBuffer.Insert(linkText, cursor);
back = front + linkText.Length();
}
cursor += back-front;
mBuffer.Insert(NS_LITERAL_STRING("\">"), cursor);
cursor += 2;
mBuffer.Insert(linkText, cursor);
cursor += linkText.Length();
mBuffer.Insert(NS_LITERAL_STRING("</a>"), cursor);
cursor += 4;
}
mToken = nullptr; // indicates completeness
return cursor;
}
|