-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathencoding_parser.dart
387 lines (353 loc) · 9.75 KB
/
encoding_parser.dart
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#library('encoding_parser');
#import('constants.dart');
#import('inputstream.dart');
#import('utils.dart');
// TODO(jmesserly): I converted StopIteration to NoMoreElementsException. Seems
// strange to throw this from outside of an iterator though.
/**
* String-like object with an associated position and various extra methods
* If the position is ever greater than the string length then an exception is
* raised.
*/
class EncodingBytes implements Iterable<String> {
final String _bytes;
int _position;
EncodingBytes(String bytes) : _bytes = bytes, _position = -1;
Iterator<String> iterator() => _bytes.splitChars().iterator();
int get length() => _bytes.length;
String next() {
var p = _position = _position + 1;
if (p >= length) {
throw const NoMoreElementsException();
} else if (p < 0) {
throw new IndexOutOfRangeException(p);
}
return _bytes[p];
}
String previous() {
var p = _position;
if (p >= length) {
throw const NoMoreElementsException();
} else if (p < 0) {
throw new IndexOutOfRangeException(p);
}
_position = p = p - 1;
return _bytes[p];
}
set position(int value) {
if (_position >= length) {
throw const NoMoreElementsException();
}
_position = value;
}
int get position() {
if (_position >= length) {
throw const NoMoreElementsException();
}
if (_position >= 0) {
return _position;
} else {
return 0;
}
}
String get currentByte() => _bytes[position];
/** Skip past a list of characters. Defaults to skipping [isWhitespace]. */
String skip([CharPreciate skipChars]) {
if (skipChars == null) skipChars = isWhitespace;
var p = position; // use property for the error-checking
while (p < length) {
var c = _bytes[p];
if (!skipChars(c)) {
_position = p;
return c;
}
p += 1;
}
_position = p;
return null;
}
String skipUntil(CharPreciate untilChars) {
var p = position;
while (p < length) {
var c = _bytes[p];
if (untilChars(c)) {
_position = p;
return c;
}
p += 1;
}
return null;
}
/**
* Look for a sequence of bytes at the start of a string. If the bytes
* are found return true and advance the position to the byte after the
* match. Otherwise return false and leave the position alone.
*/
bool matchBytes(String bytes) {
var p = position;
if (_bytes.length < p + bytes.length) {
return false;
}
var data = _bytes.substring(p, p + bytes.length);
if (data == bytes) {
position += bytes.length;
return true;
}
return false;
}
/**
* Look for the next sequence of bytes matching a given sequence. If
* a match is found advance the position to the last byte of the match
*/
bool jumpTo(String bytes) {
var newPosition = _bytes.indexOf(bytes, position);
if (newPosition >= 0) {
// XXX: This is ugly, but I can't see a nicer way to fix this.
if (_position == -1) {
_position = 0;
}
_position += newPosition + bytes.length - 1;
return true;
} else {
throw const NoMoreElementsException();
}
}
String slice(int start, [int end]) {
if (end == null) end = length;
if (end < 0) end += length;
return _bytes.substring(start, end - start);
}
}
/** Mini parser for detecting character encoding from meta elements. */
class EncodingParser {
final EncodingBytes data;
String encoding;
/** [bytes] - the data to work on for encoding detection. */
EncodingParser(List<int> bytes)
// Note: this is intentionally interpreting bytes as codepoints.
: data = new EncodingBytes(new String.fromCharCodes(bytes).toLowerCase());
String getEncoding() {
final methodDispatch = [
["<!--", handleComment],
["<meta", handleMeta],
["</", handlePossibleEndTag],
["<!", handleOther],
["<?", handleOther],
["<", handlePossibleStartTag]];
try {
for (var byte in data) {
var keepParsing = true;
for (var dispatch in methodDispatch) {
if (data.matchBytes(dispatch[0])) {
try {
keepParsing = dispatch[1]();
break;
} catch (NoMoreElementsException e) {
keepParsing = false;
break;
}
}
}
if (!keepParsing) {
break;
}
}
} catch (NoMoreElementsException e) {
// Catch this here to match behavior of Python's StopIteration
}
return encoding;
}
/** Skip over comments. */
bool handleComment() => data.jumpTo("-->");
bool handleMeta() {
if (!isWhitespace(data.currentByte)) {
// if we have <meta not followed by a space so just keep going
return true;
}
// We have a valid meta element we want to search for attributes
while (true) {
// Try to find the next attribute after the current position
var attr = getAttribute();
if (attr === null) return true;
if (attr[0] == "charset") {
var tentativeEncoding = attr[1];
var codec = codecName(tentativeEncoding);
if (codec != null) {
encoding = codec;
return false;
}
} else if (attr[0] == "content") {
var contentParser = new ContentAttrParser(new EncodingBytes(attr[1]));
var tentativeEncoding = contentParser.parse();
var codec = codecName(tentativeEncoding);
if (codec != null) {
encoding = codec;
return false;
}
}
}
}
bool handlePossibleStartTag() => handlePossibleTag(false);
bool handlePossibleEndTag() {
data.next();
return handlePossibleTag(true);
}
bool handlePossibleTag(bool endTag) {
if (!isLetter(data.currentByte)) {
//If the next byte is not an ascii letter either ignore this
//fragment (possible start tag case) or treat it according to
//handleOther
if (endTag) {
data.previous();
handleOther();
}
return true;
}
var c = data.skipUntil(isSpaceOrAngleBracket);
if (c == "<") {
// return to the first step in the overall "two step" algorithm
// reprocessing the < byte
data.previous();
} else {
//Read all attributes
var attr = getAttribute();
while (attr != null) {
attr = getAttribute();
}
}
return true;
}
bool handleOther() => data.jumpTo(">");
/**
* Return a name,value pair for the next attribute in the stream,
* if one is found, or null
*/
List<String> getAttribute() {
// Step 1 (skip chars)
var c = data.skip((x) => x == "/" || isWhitespace(x));
// Step 2
if (c == ">" || c == null) {
return null;
}
// Step 3
var attrName = [];
var attrValue = [];
// Step 4 attribute name
while (true) {
if (c == null) {
return null;
} else if (c == "=" && attrName.length > 0) {
break;
} else if (isWhitespace(c)) {
// Step 6!
c = data.skip();
c = data.next();
break;
} else if (c == "/" || c == ">") {
return [joinStr(attrName), ""];
} else if (isLetter(c)) {
attrName.add(c.toLowerCase());
} else {
attrName.add(c);
}
// Step 5
c = data.next();
}
// Step 7
if (c != "=") {
data.previous();
return [joinStr(attrName), ""];
}
// Step 8
data.next();
// Step 9
c = data.skip();
// Step 10
if (c == "'" || c == '"') {
// 10.1
var quoteChar = c;
while (true) {
// 10.2
c = data.next();
if (c == quoteChar) {
// 10.3
data.next();
return [joinStr(attrName), joinStr(attrValue)];
} else if (isLetter(c)) {
// 10.4
attrValue.add(c.toLowerCase());
} else {
// 10.5
attrValue.add(c);
}
}
} else if (c == ">") {
return [joinStr(attrName), ""];
} else if (c === null) {
return null;
} else if (isLetter(c)) {
attrValue.add(c.toLowerCase());
} else {
attrValue.add(c);
}
// Step 11
while (true) {
c = data.next();
if (isSpaceOrAngleBracket(c)) {
return [joinStr(attrName), joinStr(attrValue)];
} else if (c === null) {
return null;
} else if (isLetter(c)) {
attrValue.add(c.toLowerCase());
} else {
attrValue.add(c);
}
}
}
}
class ContentAttrParser {
final EncodingBytes data;
ContentAttrParser(this.data);
String parse() {
try {
// Check if the attr name is charset
// otherwise return
data.jumpTo("charset");
data.position += 1;
data.skip();
if (data.currentByte != "=") {
// If there is no = sign keep looking for attrs
return null;
}
data.position += 1;
data.skip();
// Look for an encoding between matching quote marks
if (data.currentByte == '"' || data.currentByte == "'") {
var quoteMark = data.currentByte;
data.position += 1;
var oldPosition = data.position;
if (data.jumpTo(quoteMark)) {
return data.slice(oldPosition, data.position);
} else {
return null;
}
} else {
// Unquoted value
var oldPosition = data.position;
try {
data.skipUntil(isWhitespace);
return data.slice(oldPosition, data.position);
} catch (NoMoreElementsException e) {
//Return the whole remaining value
return data.slice(oldPosition);
}
}
} catch (NoMoreElementsException e) {
return null;
}
}
}
bool isSpaceOrAngleBracket(String char) {
return char == ">" || char == "<" || isWhitespace(char);
}
typedef bool CharPreciate(String char);