/proc/self/cwd/cpp/htmlparser/tokenizer.cc
Line | Count | Source |
1 | | #include "cpp/htmlparser/tokenizer.h" |
2 | | |
3 | | #include "absl/flags/flag.h" |
4 | | #include "cpp/htmlparser/atom.h" |
5 | | #include "cpp/htmlparser/atomutil.h" |
6 | | #include "cpp/htmlparser/defer.h" |
7 | | #include "cpp/htmlparser/strings.h" |
8 | | |
9 | | ABSL_FLAG(std::size_t, htmlparser_max_attributes_per_node, |
10 | | 1000, |
11 | | "Protects out of memory errors by dropping insanely large amounts " |
12 | | "of attributes per node."); |
13 | | |
14 | | namespace htmlparser { |
15 | | |
16 | | Tokenizer::Tokenizer(std::string_view html, std::string context_tag) : |
17 | 12.6k | buffer_(html) { |
18 | 12.6k | lines_cols_.push_back(std::make_pair(1, 0)); |
19 | 12.6k | current_line_col_ = std::make_pair(1, 0); |
20 | 12.6k | token_line_col_ = std::make_pair(1, 0); |
21 | 12.6k | if (!context_tag.empty()) { |
22 | 0 | Strings::ToLower(&context_tag); |
23 | 0 | if (std::find(kAllowedFragmentContainers.begin(), |
24 | 0 | kAllowedFragmentContainers.end(), |
25 | 0 | AtomUtil::ToAtom(context_tag)) != |
26 | 0 | kAllowedFragmentContainers.end()) { |
27 | 0 | raw_tag_ = context_tag; |
28 | 0 | } |
29 | 0 | } |
30 | 12.6k | } |
31 | | |
32 | 200M | inline char Tokenizer::ReadByte() { |
33 | 200M | if (raw_.end >= buffer_.size()) { |
34 | 12.6k | eof_ = true; |
35 | 12.6k | return 0; |
36 | 12.6k | } |
37 | | |
38 | 200M | char c = buffer_.at(raw_.end++); |
39 | 200M | current_line_col_.second++; |
40 | 200M | int multi_byte = Strings::CodePointByteSequenceCount(c); |
41 | 200M | if (multi_byte > 1) { |
42 | 22.2M | current_line_col_.second -= (multi_byte - 1); |
43 | 22.2M | } |
44 | | |
45 | 200M | if (c == '\n' || (c == '\r' && |
46 | 17.4M | raw_.end < buffer_.size() && |
47 | 19.7M | buffer_.at(raw_.end) != '\n')) { |
48 | 19.7M | lines_cols_.back() = current_line_col_; |
49 | | // Increment line number and reset column number. |
50 | 19.7M | current_line_col_.first++; |
51 | 19.7M | current_line_col_.second = 0; |
52 | 19.7M | lines_cols_.push_back({current_line_col_.first + 1, 0}); |
53 | 19.7M | } |
54 | | |
55 | 200M | return c; |
56 | 200M | } |
57 | | |
58 | 56.0M | inline void Tokenizer::UnreadByte() { |
59 | 56.0M | raw_.end--; |
60 | 56.0M | if (current_line_col_.first > 1 && current_line_col_.second == 0) { |
61 | 2.87M | if (lines_cols_.size() > 1) { |
62 | 2.87M | lines_cols_.pop_back(); |
63 | 2.87M | } |
64 | 2.87M | current_line_col_ = lines_cols_.back(); |
65 | 2.87M | return; |
66 | 2.87M | } |
67 | | |
68 | 53.1M | current_line_col_.second--; |
69 | 53.1M | } |
70 | | |
71 | 23.7M | void Tokenizer::SkipWhiteSpace() { |
72 | 28.0M | while (!eof_) { |
73 | 28.0M | char c = ReadByte(); |
74 | 28.0M | switch (c) { |
75 | 7.91k | case ' ': |
76 | 1.44M | case '\n': |
77 | 4.23M | case '\r': |
78 | 4.29M | case '\t': |
79 | 4.36M | case '\f': |
80 | 4.36M | break; |
81 | 23.7M | default: |
82 | 23.7M | UnreadByte(); |
83 | 23.7M | return; |
84 | 28.0M | } |
85 | 28.0M | } |
86 | 23.7M | } |
87 | | |
88 | 16.6M | void Tokenizer::SetAllowCDATA(bool allow_cdata) { |
89 | 16.6M | allow_cdata_ = allow_cdata; |
90 | 16.6M | } |
91 | | |
92 | 2.80M | void Tokenizer::NextIsNotRawText() { |
93 | 2.80M | raw_tag_ = ""; |
94 | 2.80M | } |
95 | | |
96 | 63.8k | void Tokenizer::ReadRawOrRCDATA() { |
97 | 63.8k | if (raw_tag_ == "script") { |
98 | 19.7k | ReadScript(); |
99 | 19.7k | text_is_raw_ = true; |
100 | 19.7k | raw_tag_ = ""; |
101 | 19.7k | return; |
102 | 19.7k | } |
103 | | |
104 | 2.16M | while (!eof_) { |
105 | 2.16M | char c = ReadByte(); |
106 | 2.16M | if (eof_) break; |
107 | 2.16M | if (c != '<') continue; |
108 | 359k | c = ReadByte(); |
109 | 359k | if (eof_) break; |
110 | 359k | if (c != '/') continue; |
111 | 324k | if (ReadRawEndTag() || eof_) break; |
112 | 324k | } |
113 | | |
114 | 44.0k | data_.end = raw_.end; |
115 | | // A textarea's or title's RCDATA can contain escaped entities. |
116 | 44.0k | text_is_raw_ = raw_tag_ != "textarea" && raw_tag_ != "title"; |
117 | 44.0k | raw_tag_ = ""; |
118 | 44.0k | } |
119 | | |
120 | 346k | bool Tokenizer::ReadRawEndTag() { |
121 | 1.42M | for (std::size_t i = 0; i < raw_tag_.size(); ++i) { |
122 | 1.23M | char c = ReadByte(); |
123 | 1.23M | if (eof_) return false; |
124 | 1.23M | if (c != raw_tag_.at(i) && c != (raw_tag_.at(i) - ('a' - 'A'))) { |
125 | 158k | UnreadByte(); |
126 | 158k | return false; |
127 | 158k | } |
128 | 1.23M | } |
129 | | |
130 | 188k | char c = ReadByte(); |
131 | 188k | if (eof_) return false; |
132 | 188k | switch (c) { |
133 | 910 | case ' ': |
134 | 1.10k | case '\n': |
135 | 1.29k | case '\t': |
136 | 1.49k | case '\f': |
137 | 1.68k | case '/': |
138 | 63.6k | case '>': |
139 | | // The 3 is 2 for the leading "</" plus 1 for the trailing character c. |
140 | 63.6k | raw_.end -= (3 /* <, /, and > */+ raw_tag_.size()); |
141 | 63.6k | current_line_col_.second -= (3 /* <, /, and > */ + raw_tag_.size()); |
142 | 63.6k | return true; |
143 | 188k | } |
144 | 124k | UnreadByte(); |
145 | 124k | return false; |
146 | 188k | } |
147 | | |
148 | | enum ScriptDataState { |
149 | | DONE = 0, |
150 | | SCRIPT_DATA = 1, |
151 | | SCRIPT_DATA_LESS_THAN_SIGN = 2, |
152 | | SCRIPT_DATA_END_TAG_OPEN = 3, |
153 | | SCRIPT_DATA_ESCAPE_START = 4, |
154 | | SCRIPT_DATA_ESCAPE_START_DASH = 5, |
155 | | SCRIPT_DATA_ESCAPED = 6, |
156 | | SCRIPT_DATA_ESCAPED_DASH = 7, |
157 | | SCRIPT_DATA_ESCAPED_DASH_DASH = 8, |
158 | | SCRIPT_DATA_ESCAPED_LESS_THAN_SIGN = 9, |
159 | | SCRIPT_DATA_ESCAPED_END_TAG_OPEN = 10, |
160 | | SCRIPT_DATA_DOUBLE_ESCAPE_START = 11, |
161 | | SCRIPT_DATA_DOUBLE_ESCAPED = 12, |
162 | | SCRIPT_DATA_DOUBLE_ESCAPED_DASH = 13, |
163 | | SCRIPT_DATA_DOUBLE_ESCAPED_DASH_DASH = 14, |
164 | | SCRIPT_DATA_DOUBLE_ESCAPED_LESS_THAN_SIGN = 15, |
165 | | SCRIPT_DATA_DOUBLE_ESCAPED_END = 16 |
166 | | }; |
167 | | |
168 | 19.7k | void Tokenizer::ReadScript() { |
169 | 19.7k | defer({data_.end = raw_.end;}); |
170 | 19.7k | ScriptDataState state = ScriptDataState::SCRIPT_DATA; |
171 | 16.2M | while (!eof_ && state != ScriptDataState::DONE) { |
172 | 16.2M | switch (state) { |
173 | 4.56M | case ScriptDataState::SCRIPT_DATA: { |
174 | 4.56M | char c = ReadByte(); |
175 | 4.56M | if (eof_) return; |
176 | 4.56M | if (c == '<') { |
177 | 1.80M | state = ScriptDataState::SCRIPT_DATA_LESS_THAN_SIGN; |
178 | 2.76M | } else { |
179 | 2.76M | state = ScriptDataState::SCRIPT_DATA; |
180 | 2.76M | } |
181 | 4.56M | break; |
182 | 4.56M | } |
183 | 1.80M | case ScriptDataState::SCRIPT_DATA_LESS_THAN_SIGN: { |
184 | 1.80M | char c = ReadByte(); |
185 | 1.80M | if (eof_) return; |
186 | 1.80M | if (c == '/') { |
187 | 4.60k | state = ScriptDataState::SCRIPT_DATA_END_TAG_OPEN; |
188 | 1.79M | } else if (c == '!') { |
189 | 1.15M | state = ScriptDataState::SCRIPT_DATA_ESCAPE_START; |
190 | 1.15M | } else { |
191 | 644k | UnreadByte(); |
192 | 644k | state = ScriptDataState::SCRIPT_DATA; |
193 | 644k | } |
194 | 1.80M | break; |
195 | 1.80M | } |
196 | 4.60k | case ScriptDataState::SCRIPT_DATA_END_TAG_OPEN: { |
197 | 4.60k | if (ReadRawEndTag() || eof_) { |
198 | 4.20k | return; |
199 | 4.20k | } |
200 | 399 | state = ScriptDataState::SCRIPT_DATA; |
201 | 399 | break; |
202 | 4.60k | } |
203 | 1.15M | case ScriptDataState::SCRIPT_DATA_ESCAPE_START: { |
204 | 1.15M | char c = ReadByte(); |
205 | 1.15M | if (eof_) return; |
206 | 1.15M | if (c == '-') { |
207 | 1.12M | state = ScriptDataState::SCRIPT_DATA_ESCAPE_START_DASH; |
208 | 1.12M | } else { |
209 | 23.8k | UnreadByte(); |
210 | 23.8k | state = ScriptDataState::SCRIPT_DATA; |
211 | 23.8k | } |
212 | 1.15M | break; |
213 | 1.15M | } |
214 | 1.12M | case ScriptDataState::SCRIPT_DATA_ESCAPE_START_DASH: { |
215 | 1.12M | char c = ReadByte(); |
216 | 1.12M | if (eof_) return; |
217 | 1.12M | if (c == '-') { |
218 | 587k | state = SCRIPT_DATA_ESCAPED_DASH_DASH; |
219 | 587k | } else { |
220 | 540k | UnreadByte(); |
221 | 540k | state = ScriptDataState::SCRIPT_DATA; |
222 | 540k | } |
223 | 1.12M | break; |
224 | 1.12M | } |
225 | 2.74M | case ScriptDataState::SCRIPT_DATA_ESCAPED: { |
226 | 2.74M | char c = ReadByte(); |
227 | 2.74M | if (eof_) return; |
228 | 2.74M | if (c == '-') { |
229 | 1.01M | state = ScriptDataState::SCRIPT_DATA_ESCAPED_DASH; |
230 | 1.73M | } else if (c == '<') { |
231 | 73.9k | state = ScriptDataState::SCRIPT_DATA_ESCAPED_LESS_THAN_SIGN; |
232 | 1.66M | } else { |
233 | 1.66M | state = ScriptDataState::SCRIPT_DATA_ESCAPED; |
234 | 1.66M | } |
235 | 2.74M | break; |
236 | 2.74M | } |
237 | 1.01M | case ScriptDataState::SCRIPT_DATA_ESCAPED_DASH: { |
238 | 1.01M | char c = ReadByte(); |
239 | 1.01M | if (eof_) return; |
240 | 1.01M | if (c == '-') { |
241 | 12.0k | state = ScriptDataState::SCRIPT_DATA_ESCAPED_DASH_DASH; |
242 | 1.00M | } else if (c == '<') { |
243 | 985k | state = ScriptDataState::SCRIPT_DATA_ESCAPED_LESS_THAN_SIGN; |
244 | 985k | } else { |
245 | 14.1k | state = ScriptDataState::SCRIPT_DATA_ESCAPED; |
246 | 14.1k | } |
247 | 1.01M | break; |
248 | 1.01M | } |
249 | 599k | case ScriptDataState::SCRIPT_DATA_ESCAPED_DASH_DASH: { |
250 | 599k | char c = ReadByte(); |
251 | 599k | if (eof_) return; |
252 | 599k | if (c == '-') { |
253 | 290 | state = ScriptDataState::SCRIPT_DATA_ESCAPED_DASH_DASH; |
254 | 599k | } else if (c == '<') { |
255 | 30.5k | state = ScriptDataState::SCRIPT_DATA_ESCAPED_LESS_THAN_SIGN; |
256 | 568k | } else if (c == '>') { |
257 | 274 | state = ScriptDataState::SCRIPT_DATA; |
258 | 568k | } else { |
259 | 568k | state = ScriptDataState::SCRIPT_DATA_ESCAPED; |
260 | 568k | } |
261 | 599k | break; |
262 | 599k | } |
263 | 1.09M | case ScriptDataState::SCRIPT_DATA_ESCAPED_LESS_THAN_SIGN: { |
264 | 1.09M | char c = ReadByte(); |
265 | 1.09M | if (eof_) return; |
266 | 1.09M | if (c == '/') { |
267 | 16.0k | state = ScriptDataState::SCRIPT_DATA_ESCAPED_END_TAG_OPEN; |
268 | 1.07M | } else if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z')) { |
269 | 504k | state = ScriptDataState::SCRIPT_DATA_DOUBLE_ESCAPE_START; |
270 | 570k | } else { |
271 | 570k | UnreadByte(); |
272 | 570k | state = ScriptDataState::SCRIPT_DATA; |
273 | 570k | } |
274 | 1.09M | break; |
275 | 1.09M | } |
276 | 16.0k | case ScriptDataState::SCRIPT_DATA_ESCAPED_END_TAG_OPEN: { |
277 | 16.0k | if (ReadRawEndTag()) { |
278 | 14.7k | state = ScriptDataState::DONE; |
279 | 14.7k | } else { |
280 | 1.32k | state = ScriptDataState::SCRIPT_DATA_ESCAPED; |
281 | 1.32k | } |
282 | 16.0k | break; |
283 | 1.09M | } |
284 | 504k | case ScriptDataState::SCRIPT_DATA_DOUBLE_ESCAPE_START: { |
285 | 504k | UnreadByte(); |
286 | 504k | static std::string script_tag_l = "script"; |
287 | 504k | static std::string script_tag_u = "SCRIPT"; |
288 | 3.52M | for (int8_t i = 0; i < 6 /*script*/; ++i) { |
289 | 3.02M | char c = ReadByte(); |
290 | 3.02M | if (eof_) return; |
291 | 3.02M | if (c != script_tag_l[i] && c != script_tag_u[i]) { |
292 | 3.01M | UnreadByte(); |
293 | 3.01M | state = ScriptDataState::SCRIPT_DATA_ESCAPED; |
294 | 3.01M | } |
295 | 3.02M | } |
296 | 504k | char c = ReadByte(); |
297 | 504k | if (eof_) return; |
298 | 504k | if (c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == '\f' |
299 | 502k | || c == '/' || c == '>') { |
300 | 2.72k | state = ScriptDataState::SCRIPT_DATA_DOUBLE_ESCAPED; |
301 | 501k | } else { |
302 | 501k | UnreadByte(); |
303 | 501k | state = ScriptDataState::SCRIPT_DATA_ESCAPED; |
304 | 501k | } |
305 | 504k | break; |
306 | 504k | } |
307 | 899k | case ScriptDataState::SCRIPT_DATA_DOUBLE_ESCAPED: { |
308 | 899k | char c = ReadByte(); |
309 | 899k | if (eof_) return; |
310 | 899k | if (c == '-') { |
311 | 319k | state = ScriptDataState::SCRIPT_DATA_DOUBLE_ESCAPED_DASH; |
312 | 579k | } else if (c == '<') { |
313 | 134k | state = ScriptDataState::SCRIPT_DATA_DOUBLE_ESCAPED_LESS_THAN_SIGN; |
314 | 445k | } else { |
315 | 445k | state = ScriptDataState::SCRIPT_DATA_DOUBLE_ESCAPED; |
316 | 445k | } |
317 | 899k | break; |
318 | 899k | } |
319 | 319k | case ScriptDataState::SCRIPT_DATA_DOUBLE_ESCAPED_DASH: { |
320 | 319k | char c = ReadByte(); |
321 | 319k | if (eof_) return; |
322 | 319k | if (c == '-') { |
323 | 63.6k | state = ScriptDataState::SCRIPT_DATA_DOUBLE_ESCAPED_DASH_DASH; |
324 | 256k | } else if (c == '<') { |
325 | 183k | state = ScriptDataState::SCRIPT_DATA_DOUBLE_ESCAPED_LESS_THAN_SIGN; |
326 | 183k | } else if (c == '>') { |
327 | 1.31k | state = ScriptDataState::SCRIPT_DATA; |
328 | 71.2k | } else { |
329 | 71.2k | state = SCRIPT_DATA_DOUBLE_ESCAPED; |
330 | 71.2k | } |
331 | 319k | break; |
332 | 319k | } |
333 | 66.1k | case ScriptDataState::SCRIPT_DATA_DOUBLE_ESCAPED_DASH_DASH: { |
334 | 66.1k | char c = ReadByte(); |
335 | 66.1k | if (eof_) return; |
336 | 66.0k | if (c == '-') { |
337 | 2.43k | state = ScriptDataState::SCRIPT_DATA_DOUBLE_ESCAPED_DASH_DASH; |
338 | 63.6k | } else if (c == '<') { |
339 | 1.75k | state = ScriptDataState::SCRIPT_DATA_DOUBLE_ESCAPED_LESS_THAN_SIGN; |
340 | 61.9k | } else if (c == '>') { |
341 | 194 | state = ScriptDataState::SCRIPT_DATA; |
342 | 61.7k | } else { |
343 | 61.7k | state = ScriptDataState::SCRIPT_DATA_DOUBLE_ESCAPED; |
344 | 61.7k | } |
345 | 66.0k | break; |
346 | 66.1k | } |
347 | 319k | case ScriptDataState::SCRIPT_DATA_DOUBLE_ESCAPED_LESS_THAN_SIGN: { |
348 | 319k | char c = ReadByte(); |
349 | 319k | if (eof_) return; |
350 | 319k | if (c == '/') { |
351 | 1.62k | state = ScriptDataState::SCRIPT_DATA_DOUBLE_ESCAPED_END; |
352 | 317k | } else { |
353 | 317k | UnreadByte(); |
354 | 317k | state = ScriptDataState::SCRIPT_DATA_DOUBLE_ESCAPED; |
355 | 317k | } |
356 | 319k | break; |
357 | 319k | } |
358 | 1.62k | case ScriptDataState::SCRIPT_DATA_DOUBLE_ESCAPED_END: { |
359 | 1.62k | if (ReadRawEndTag()) { |
360 | 981 | raw_.end += std::string("</script>").size(); |
361 | 981 | state = ScriptDataState::SCRIPT_DATA_ESCAPED; |
362 | 981 | } else { |
363 | 648 | if (eof_) return; |
364 | 640 | state = ScriptDataState::SCRIPT_DATA_DOUBLE_ESCAPED; |
365 | 640 | } |
366 | 1.62k | break; |
367 | 1.62k | } |
368 | 1.62k | default: |
369 | 0 | break; |
370 | 16.2M | } |
371 | 16.2M | } |
372 | 19.7k | } |
373 | | |
374 | 757 | void Tokenizer::ReadComment() { |
375 | 757 | data_.start = raw_.end; |
376 | 757 | defer({ |
377 | 757 | if (data_.end < data_.start) { |
378 | | // It's a comment with no data, like <!--> |
379 | 757 | data_.end = data_.start; |
380 | 757 | } |
381 | 757 | }); |
382 | 757 | int dash_count = 2; |
383 | 3.13M | while (!eof_) { |
384 | 3.13M | char c = ReadByte(); |
385 | 3.13M | if (eof_) { |
386 | | // Ignore up to two dashes at EOF. |
387 | 92 | if (dash_count > 2) { |
388 | 13 | dash_count = 2; |
389 | 13 | } |
390 | 92 | data_.end = raw_.end - dash_count; |
391 | 92 | return; |
392 | 92 | } |
393 | 3.13M | if (c == '-') { |
394 | 603k | dash_count++; |
395 | 603k | continue; |
396 | 2.52M | } else if (c == '>') { |
397 | 8.68k | if (dash_count >= 2) { |
398 | 400 | data_.end = raw_.end - 3 /* --> */; |
399 | 400 | return; |
400 | 400 | } |
401 | 2.52M | } else if (c == '!') { |
402 | 297k | if (dash_count >= 2) { |
403 | 824 | char c = ReadByte(); |
404 | 824 | if (eof_) { |
405 | 6 | data_.end = raw_.end; |
406 | 6 | return; |
407 | 6 | } |
408 | 818 | if (c == '>') { |
409 | 259 | data_.end = raw_.end - 4 /* --!> */; |
410 | 259 | return; |
411 | 259 | } |
412 | 818 | } |
413 | 297k | } |
414 | 2.52M | dash_count = 0; |
415 | 2.52M | } |
416 | 757 | } |
417 | | |
418 | 990k | void Tokenizer::ReadUntilCloseAngle() { |
419 | 990k | data_.start = raw_.end; |
420 | 3.58M | while (!eof_) { |
421 | 3.58M | char c = ReadByte(); |
422 | 3.58M | if (eof_) { |
423 | 605 | data_.end = raw_.end; |
424 | 605 | return; |
425 | 605 | } |
426 | 3.58M | if (c == '>') { |
427 | 990k | data_.end = raw_.end - 1 /* ">" */; |
428 | 990k | return; |
429 | 990k | } |
430 | 3.58M | } |
431 | 990k | } |
432 | | |
433 | 987k | TokenType Tokenizer::ReadMarkupDeclaration() { |
434 | 987k | data_.start = raw_.end; |
435 | 987k | char c[2]; |
436 | 2.96M | for (int i = 0; i < 2; ++i) { |
437 | 1.97M | c[i] = ReadByte(); |
438 | 1.97M | if (eof_) { |
439 | 46 | data_.end = raw_.end; |
440 | 46 | return TokenType::COMMENT_TOKEN; |
441 | 46 | } |
442 | 1.97M | } |
443 | | |
444 | 987k | if (c[0] == '-' && c[1] == '-') { |
445 | 757 | ReadComment(); |
446 | 757 | return TokenType::COMMENT_TOKEN; |
447 | 757 | } |
448 | | |
449 | 986k | UnreadByte(); |
450 | 986k | UnreadByte(); |
451 | 986k | if (ReadDoctype()) { |
452 | 736 | return TokenType::DOCTYPE_TOKEN; |
453 | 736 | } |
454 | | |
455 | 985k | if (allow_cdata_ && ReadCDATA()) { |
456 | 380 | convert_null_ = true; |
457 | 380 | return TokenType::TEXT_TOKEN; |
458 | 380 | } |
459 | | |
460 | | // It's a bogus comment. |
461 | 985k | ReadUntilCloseAngle(); |
462 | 985k | return TokenType::COMMENT_TOKEN; |
463 | 985k | } |
464 | | |
465 | 986k | bool Tokenizer::ReadDoctype() { |
466 | 986k | token_line_col_ = {current_line_col_.first, |
467 | 986k | current_line_col_.second - 2 /* <! */}; |
468 | | |
469 | 986k | static constexpr std::string_view kDoctype = "DOCTYPE"; |
470 | 992k | for (std::size_t i = 0; i < kDoctype.size(); ++i) { |
471 | 991k | char c = ReadByte(); |
472 | 991k | if (eof_) { |
473 | 13 | data_.end = raw_.end; |
474 | 13 | return false; |
475 | 13 | } |
476 | 991k | if (c != kDoctype.at(i) && c != (kDoctype.at(i) + ('a' - 'A'))) { |
477 | | // Back up to read the fragment of "DOCTYPE" again. |
478 | 985k | raw_.end = data_.start; |
479 | 985k | return false; |
480 | 985k | } |
481 | 991k | } |
482 | | |
483 | 736 | SkipWhiteSpace(); |
484 | 736 | if (eof_) { |
485 | 21 | data_.start = raw_.end; |
486 | 21 | data_.end = raw_.end; |
487 | 21 | return true; |
488 | 21 | } |
489 | | |
490 | 715 | ReadUntilCloseAngle(); |
491 | 715 | return true; |
492 | 736 | } |
493 | | |
494 | 657 | bool Tokenizer::ReadCDATA() { |
495 | 657 | static constexpr std::string_view kCData = "[CDATA["; |
496 | 3.67k | for (std::size_t i = 0; i < kCData.size(); ++i) { |
497 | 3.29k | char c = ReadByte(); |
498 | 3.29k | if (eof_) { |
499 | 7 | data_.end = raw_.end; |
500 | 7 | return false; |
501 | 7 | } |
502 | 3.28k | if (c != kCData[i]) { |
503 | | // Back up to read the fragment of "[CDATA[" again. |
504 | 270 | data_.end = raw_.start; |
505 | 270 | return false; |
506 | 270 | } |
507 | 3.28k | } |
508 | 380 | data_.start = raw_.end; |
509 | 380 | int brackets = 0; |
510 | 1.66k | while (!eof_) { |
511 | 1.66k | char c = ReadByte(); |
512 | 1.66k | if (eof_) { |
513 | 31 | data_.end = raw_.end; |
514 | 31 | return true; |
515 | 31 | } |
516 | 1.62k | switch (c) { |
517 | 892 | case ']': { |
518 | 892 | brackets++; |
519 | 892 | break; |
520 | 0 | } |
521 | 543 | case '>': { |
522 | 543 | if (brackets >= 2) { |
523 | 349 | data_.end = raw_.end - 3 /* "]]>" */; |
524 | 349 | return true; |
525 | 349 | } |
526 | 194 | brackets = 0; |
527 | 194 | break; |
528 | 543 | } |
529 | 194 | default: |
530 | 194 | brackets = 0; |
531 | 1.62k | } |
532 | 1.62k | } |
533 | 0 | return false; |
534 | 380 | } |
535 | | |
536 | | template<typename... Args> |
537 | 4.80M | bool Tokenizer::StartTagIn(Args... ss) { |
538 | 4.80M | std::vector<std::string> argsList{ss...}; |
539 | 9.31M | for (const auto& s : argsList) { |
540 | 9.31M | if (data_.end - data_.start != s.size()) continue; |
541 | 1.59M | bool matched = true; |
542 | 3.80M | for (std::size_t i = 0; i < s.size(); ++i) { |
543 | 3.74M | char c = buffer_.at(data_.start + i); |
544 | 3.74M | if ('A' <= c && c <= 'Z') { |
545 | 3.46M | c += 'a' - 'A'; |
546 | 3.46M | } |
547 | 3.74M | if (c != s[i]) { |
548 | 1.53M | matched = false; |
549 | 1.53M | break; |
550 | 1.53M | } |
551 | 3.74M | } |
552 | 1.59M | if (matched) { |
553 | 64.2k | return true; |
554 | 64.2k | } |
555 | 1.59M | } |
556 | 4.73M | return false; |
557 | 4.80M | } bool htmlparser::Tokenizer::StartTagIn<char const*>(char const*) Line | Count | Source | 537 | 451k | bool Tokenizer::StartTagIn(Args... ss) { | 538 | 451k | std::vector<std::string> argsList{ss...}; | 539 | 451k | for (const auto& s : argsList) { | 540 | 451k | if (data_.end - data_.start != s.size()) continue; | 541 | 39.7k | bool matched = true; | 542 | 159k | for (std::size_t i = 0; i < s.size(); ++i) { | 543 | 120k | char c = buffer_.at(data_.start + i); | 544 | 120k | if ('A' <= c && c <= 'Z') { | 545 | 79.0k | c += 'a' - 'A'; | 546 | 79.0k | } | 547 | 120k | if (c != s[i]) { | 548 | 454 | matched = false; | 549 | 454 | break; | 550 | 454 | } | 551 | 120k | } | 552 | 39.7k | if (matched) { | 553 | 39.3k | return true; | 554 | 39.3k | } | 555 | 39.7k | } | 556 | 411k | return false; | 557 | 451k | } |
bool htmlparser::Tokenizer::StartTagIn<char const*, char const*, char const*>(char const*, char const*, char const*) Line | Count | Source | 537 | 180k | bool Tokenizer::StartTagIn(Args... ss) { | 538 | 180k | std::vector<std::string> argsList{ss...}; | 539 | 541k | for (const auto& s : argsList) { | 540 | 541k | if (data_.end - data_.start != s.size()) continue; | 541 | 207k | bool matched = true; | 542 | 833k | for (std::size_t i = 0; i < s.size(); ++i) { | 543 | 832k | char c = buffer_.at(data_.start + i); | 544 | 832k | if ('A' <= c && c <= 'Z') { | 545 | 826k | c += 'a' - 'A'; | 546 | 826k | } | 547 | 832k | if (c != s[i]) { | 548 | 206k | matched = false; | 549 | 206k | break; | 550 | 206k | } | 551 | 832k | } | 552 | 207k | if (matched) { | 553 | 685 | return true; | 554 | 685 | } | 555 | 207k | } | 556 | 179k | return false; | 557 | 180k | } |
bool htmlparser::Tokenizer::StartTagIn<char const*, char const*>(char const*, char const*) Line | Count | Source | 537 | 4.17M | bool Tokenizer::StartTagIn(Args... ss) { | 538 | 4.17M | std::vector<std::string> argsList{ss...}; | 539 | 8.32M | for (const auto& s : argsList) { | 540 | 8.32M | if (data_.end - data_.start != s.size()) continue; | 541 | 1.34M | bool matched = true; | 542 | 2.81M | for (std::size_t i = 0; i < s.size(); ++i) { | 543 | 2.78M | char c = buffer_.at(data_.start + i); | 544 | 2.78M | if ('A' <= c && c <= 'Z') { | 545 | 2.55M | c += 'a' - 'A'; | 546 | 2.55M | } | 547 | 2.78M | if (c != s[i]) { | 548 | 1.32M | matched = false; | 549 | 1.32M | break; | 550 | 1.32M | } | 551 | 2.78M | } | 552 | 1.34M | if (matched) { | 553 | 24.2k | return true; | 554 | 24.2k | } | 555 | 1.34M | } | 556 | 4.14M | return false; | 557 | 4.17M | } |
|
558 | | |
559 | 14.8M | TokenType Tokenizer::ReadStartTag(bool template_mode) { |
560 | 14.8M | token_line_col_ = {current_line_col_.first, |
561 | 14.8M | current_line_col_.second - 1 /* < */}; |
562 | 14.8M | ReadTag(true, template_mode); |
563 | | |
564 | 14.8M | if (eof_) { |
565 | 442 | return TokenType::ERROR_TOKEN; |
566 | 442 | } |
567 | | |
568 | | // Several tags flag the tokenizer's next token as raw. |
569 | 14.8M | bool raw = false; |
570 | 14.8M | char c = buffer_.at(data_.start); |
571 | | |
572 | | // Lowercase. |
573 | 14.8M | if ('A' <= c && c <= 'Z') { |
574 | 5.91M | c += 'a' - 'A'; |
575 | 5.91M | } |
576 | | |
577 | 14.8M | switch (c) { |
578 | 359k | case 'i': |
579 | 359k | raw = StartTagIn("iframe"); |
580 | 359k | break; |
581 | 180k | case 'n': |
582 | 180k | raw = StartTagIn("noembed", "noframes", "noscript"); |
583 | 180k | break; |
584 | 48.4k | case 'p': |
585 | 48.4k | raw = StartTagIn("plaintext"); |
586 | 48.4k | break; |
587 | 1.02M | case 's': |
588 | 1.02M | raw = StartTagIn("script", "style"); |
589 | 1.02M | break; |
590 | 3.14M | case 't': |
591 | 3.14M | raw = StartTagIn("textarea", "title"); |
592 | 3.14M | break; |
593 | 43.3k | case 'x': |
594 | 43.3k | raw = StartTagIn("xmp"); |
595 | 14.8M | } |
596 | | |
597 | 14.8M | if (raw) { |
598 | 64.2k | int size = data_.end - data_.start; |
599 | 64.2k | raw_tag_ = std::string(buffer_.substr(data_.start, size)); |
600 | 64.2k | Strings::ToLower(&raw_tag_); |
601 | 64.2k | } |
602 | | |
603 | | // Look for a self-closing token like "<br/>". |
604 | 14.8M | if (!eof_ && buffer_[raw_.end - 2] == '/') { |
605 | 1.03k | return TokenType::SELF_CLOSING_TAG_TOKEN; |
606 | 1.03k | } |
607 | | |
608 | 14.8M | return TokenType::START_TAG_TOKEN; |
609 | 14.8M | } |
610 | | |
611 | 15.0M | void Tokenizer::ReadTag(bool save_attr, bool template_mode) { |
612 | 15.0M | attributes_.clear(); |
613 | 15.0M | n_attributes_returned_ = 0; |
614 | | |
615 | | // Read the tag name and attribute key/value pairs. |
616 | 15.0M | ReadTagName(); |
617 | 15.0M | SkipWhiteSpace(); |
618 | | |
619 | 15.0M | if (eof_) { |
620 | 149 | return; |
621 | 149 | } |
622 | | |
623 | 19.3M | while (!eof_) { |
624 | 19.3M | char c = ReadByte(); |
625 | 19.3M | if (eof_ || c == '>') { |
626 | 15.0M | break; |
627 | 15.0M | } |
628 | | |
629 | | // Undo previous > read. |
630 | 4.30M | UnreadByte(); |
631 | | |
632 | 4.30M | ReadTagAttributeKey(template_mode); |
633 | 4.30M | ReadTagAttributeValue(); |
634 | | // Save pending_attribute if save_attr and that attribute has a non-empty |
635 | | // key. |
636 | 4.30M | if (save_attr && |
637 | | // Skip excessive attributes. |
638 | 4.30M | attributes_.size() < ::absl::GetFlag( |
639 | 4.30M | FLAGS_htmlparser_max_attributes_per_node) && |
640 | 1.17M | std::get<0>(pending_attribute_).start != |
641 | 1.17M | std::get<0>(pending_attribute_).end) { |
642 | 1.12M | attributes_.push_back(pending_attribute_); |
643 | 1.12M | } |
644 | 4.30M | SkipWhiteSpace(); |
645 | 4.30M | } |
646 | 15.0M | } |
647 | | |
648 | 15.0M | void Tokenizer::ReadTagName() { |
649 | 15.0M | data_.start = raw_.end - 1; |
650 | 30.2M | while (!eof_) { |
651 | 30.2M | char c = ReadByte(); |
652 | 30.2M | if (eof_) { |
653 | 86 | data_.end = raw_.end; |
654 | 86 | return; |
655 | 86 | } |
656 | 30.2M | switch (c) { |
657 | 8.07k | case ' ': |
658 | 9.00k | case '\n': |
659 | 111k | case '\r': |
660 | 112k | case '\t': |
661 | 112k | case '\f': |
662 | 112k | data_.end = raw_.end - 1; |
663 | 112k | return; |
664 | 10.7k | case '/': |
665 | 14.9M | case '>': |
666 | 14.9M | UnreadByte(); |
667 | 14.9M | data_.end = raw_.end; |
668 | 14.9M | return; |
669 | 30.2M | } |
670 | 30.2M | } |
671 | 15.0M | } |
672 | | |
673 | | // Sets pending_attribute_[0] to the "k" in "<div k=v>". |
674 | | // Precondition: eof_ != true; |
675 | 4.30M | void Tokenizer::ReadTagAttributeKey(bool template_mode) { |
676 | 4.30M | std::get<0>(pending_attribute_).start = raw_.end; |
677 | 4.30M | std::get<LineCol>(pending_attribute_) = |
678 | 4.30M | {current_line_col_.first, current_line_col_.second + 1}; |
679 | | |
680 | | // All mustache_ prefixed variables applies to parsing logic for AMP mustache |
681 | | // templates. See: https://amp.dev/documentation/components/amp-mustache/ |
682 | 4.30M | bool mustache_inside_section_block = false; |
683 | 4.30M | std::string mustache_section_name = ""; |
684 | | |
685 | 26.0M | while (!eof_) { |
686 | 26.0M | char c = ReadByte(); |
687 | 26.0M | if (eof_) { |
688 | 141 | std::get<0>(pending_attribute_).start = raw_.end; |
689 | 141 | return; |
690 | 141 | } |
691 | | |
692 | | // Template attributes processing. |
693 | | // Looks for following special syntax. |
694 | | // {{#section}}...{{/section}} |
695 | | // {{^section}}...{{/section}} |
696 | | // {{variable}} |
697 | 26.0M | if (template_mode) { |
698 | 0 | UnreadByte(); |
699 | 0 | UnreadByte(); |
700 | 0 | UnreadByte(); |
701 | 0 | char c1 = ReadByte(); |
702 | 0 | char c2 = ReadByte(); |
703 | 0 | c = ReadByte(); |
704 | 0 | if (mustache_inside_section_block && c1 == '{' && c2 == '{' && c == '/') { |
705 | | // Look for closing section name. If not resort to default behavior. |
706 | | // Reason for this logic is to differentiate between: |
707 | | // <p {{#mycondition}}class=foo{{/mycondition}} foo=bar> vs. |
708 | | // <img {{#mycondition}}class=foo /> |
709 | 0 | int raw_end = raw_.end; |
710 | 0 | std::string_view close_section = |
711 | 0 | buffer_.substr(raw_.end, mustache_section_name.size()); |
712 | 0 | bool section_name_match = close_section == mustache_section_name; |
713 | 0 | if (section_name_match) { |
714 | 0 | raw_.end += mustache_section_name.size(); |
715 | 0 | char e1 = ReadByte(); |
716 | 0 | char e2 = ReadByte(); |
717 | 0 | if (e1 == '}' && e2 == '}') { |
718 | 0 | mustache_inside_section_block = false; |
719 | 0 | continue; |
720 | 0 | } else { |
721 | 0 | raw_.end = raw_end; |
722 | 0 | } |
723 | 0 | } |
724 | 0 | } |
725 | | |
726 | 0 | if (c1 == '{' && c2 == '{' && (c == '#' || c == '^')) { |
727 | 0 | auto n = buffer_.find("}}", raw_.end); |
728 | 0 | if (n != std::string_view::npos) { |
729 | 0 | mustache_section_name = buffer_.substr(raw_.end, n - raw_.end); |
730 | 0 | mustache_inside_section_block = true; |
731 | 0 | continue; |
732 | 0 | } |
733 | 0 | } |
734 | 0 | } |
735 | | |
736 | 26.0M | switch (c) { |
737 | 463k | case ' ': |
738 | 673k | case '\n': |
739 | 713k | case '\r': |
740 | 713k | case '\t': |
741 | 766k | case '\f': |
742 | 4.13M | case '/': { |
743 | 4.13M | std::get<0>(pending_attribute_).end = raw_.end - 1; |
744 | 4.13M | return; |
745 | 766k | } |
746 | 48.4k | case '=': |
747 | 167k | case '>': { |
748 | 167k | UnreadByte(); |
749 | 167k | std::get<0>(pending_attribute_).end = raw_.end; |
750 | 167k | return; |
751 | 48.4k | } |
752 | 26.0M | } |
753 | 26.0M | } |
754 | 4.30M | } |
755 | | |
756 | | // Sets pending_attribute_.second to the "v" in "<div k=v>". |
757 | 4.30M | void Tokenizer::ReadTagAttributeValue() { |
758 | 4.30M | std::get<1>(pending_attribute_).start = raw_.end; |
759 | 4.30M | std::get<1>(pending_attribute_).end = raw_.end; |
760 | 4.30M | SkipWhiteSpace(); |
761 | 4.30M | if (eof_) { |
762 | 252 | return; |
763 | 252 | } |
764 | 4.30M | char c = ReadByte(); |
765 | 4.30M | if (eof_) { |
766 | 0 | return; |
767 | 0 | } |
768 | | |
769 | 4.30M | if (c != '=') { |
770 | 4.25M | UnreadByte(); |
771 | 4.25M | return; |
772 | 4.25M | } |
773 | | |
774 | 48.5k | SkipWhiteSpace(); |
775 | 48.5k | if (eof_) { |
776 | 22 | return; |
777 | 22 | } |
778 | | |
779 | 48.4k | char quote = ReadByte(); |
780 | 48.4k | if (eof_) { |
781 | 0 | return; |
782 | 0 | } |
783 | | |
784 | 48.4k | switch (quote) { |
785 | 209 | case '>': |
786 | 209 | UnreadByte(); |
787 | 209 | return; |
788 | 288 | case '\'': |
789 | 482 | case '"': |
790 | 482 | std::get<1>(pending_attribute_).start = raw_.end; |
791 | 692 | while (!eof_) { |
792 | 692 | c = ReadByte(); |
793 | 692 | if (eof_) { |
794 | 21 | std::get<1>(pending_attribute_).end = raw_.end; |
795 | 21 | return; |
796 | 21 | } |
797 | 671 | if (c == quote) { |
798 | 461 | std::get<1>(pending_attribute_).end = raw_.end - 1; |
799 | 461 | return; |
800 | 461 | } |
801 | 671 | } |
802 | 0 | break; |
803 | 47.8k | default: { |
804 | 47.8k | std::get<1>(pending_attribute_).start = raw_.end - 1; |
805 | 9.37M | while (!eof_) { |
806 | 9.37M | c = ReadByte(); |
807 | 9.37M | if (eof_) { |
808 | 19 | std::get<1>(pending_attribute_).end = raw_.end; |
809 | 19 | return; |
810 | 19 | } |
811 | 9.37M | switch (c) { |
812 | 6.14k | case ' ': |
813 | 38.9k | case '\n': |
814 | 39.1k | case '\r': |
815 | 39.3k | case '\t': |
816 | 46.5k | case '\f': |
817 | 46.5k | std::get<1>(pending_attribute_).end = raw_.end - 1; |
818 | 46.5k | return; |
819 | 1.21k | case '>': |
820 | 1.21k | UnreadByte(); |
821 | 1.21k | std::get<1>(pending_attribute_).end = raw_.end; |
822 | 1.21k | return; |
823 | 9.37M | } |
824 | 9.37M | } |
825 | 47.8k | } |
826 | 48.4k | } |
827 | 48.4k | } |
828 | | |
829 | 16.6M | TokenType Tokenizer::Next(bool template_mode) { |
830 | 16.6M | raw_.start = raw_.end; |
831 | 16.6M | data_.start = raw_.end; |
832 | 16.6M | data_.end = raw_.end; |
833 | 16.6M | is_token_manufactured_ = false; |
834 | | |
835 | 16.6M | if (eof_) { |
836 | 4.30k | err_ = true; |
837 | 4.30k | token_type_ = TokenType::ERROR_TOKEN; |
838 | 4.30k | return token_type_; |
839 | 4.30k | } |
840 | | |
841 | 16.6M | if (raw_tag_ != "") { |
842 | 63.8k | if (raw_tag_ == "plaintext") { |
843 | | // Read everything up to EOF. |
844 | 0 | while (!eof_) { |
845 | 0 | ReadByte(); |
846 | 0 | } |
847 | 0 | data_.end = raw_.end; |
848 | 0 | text_is_raw_ = true; |
849 | 63.8k | } else { |
850 | 63.8k | ReadRawOrRCDATA(); |
851 | 63.8k | } |
852 | | |
853 | 63.8k | if (data_.end > data_.start) { |
854 | 54.6k | token_type_ = TokenType::TEXT_TOKEN; |
855 | 54.6k | convert_null_ = true; |
856 | 54.6k | return token_type_; |
857 | 54.6k | } |
858 | 63.8k | } |
859 | | |
860 | 16.6M | text_is_raw_ = false; |
861 | 16.6M | convert_null_ = false; |
862 | | |
863 | 32.8M | while (!eof_) { |
864 | 32.8M | char c = ReadByte(); |
865 | | |
866 | 32.8M | if (eof_) { |
867 | 9.85k | break; |
868 | 9.85k | } |
869 | | |
870 | 32.8M | if (c != '<') { |
871 | 15.9M | continue; |
872 | 15.9M | } |
873 | | |
874 | 16.8M | c = ReadByte(); |
875 | 16.8M | if (eof_) break; |
876 | | |
877 | | // Check if the '<' we have just read is part of a tag, comment or |
878 | | // doctype. If not, it's part of the accumulated text token. |
879 | 16.8M | TokenType token_type; |
880 | 16.8M | if (Strings::IsCharAlphabet(c)) { |
881 | 15.4M | token_type = TokenType::START_TAG_TOKEN; |
882 | 15.4M | } else if (c == '/') { |
883 | 191k | token_type = TokenType::END_TAG_TOKEN; |
884 | 1.27M | } else if (c == '!' || c == '?') { |
885 | 1.03M | token_type = TokenType::COMMENT_TOKEN; |
886 | 1.03M | } else { |
887 | 245k | UnreadByte(); |
888 | 245k | continue; |
889 | 245k | } |
890 | | |
891 | | // We have a non-text token, but we might have accumulated some text |
892 | | // before that. If so, we return the text first, and return the non text |
893 | | // token on the subsequent call to Next. |
894 | | // |
895 | | // <space><space><mytag>, returns two spaces before processing the mytag |
896 | | // token in the next call. |
897 | 16.6M | if (int x = raw_.end - 2 /* "<a" */; raw_.start < x) { |
898 | 573k | raw_.end = x; |
899 | 573k | data_.end = x; |
900 | | // We know there is no \n so no line adjustment needed. |
901 | 573k | current_line_col_.second -= 2; |
902 | 573k | token_type_ = TokenType::TEXT_TOKEN; |
903 | 573k | return token_type_; |
904 | 573k | } |
905 | | |
906 | 16.0M | switch (token_type) { |
907 | 14.8M | case TokenType::START_TAG_TOKEN: |
908 | 14.8M | token_type_ = ReadStartTag(template_mode); |
909 | 14.8M | return token_type_; |
910 | 172k | case TokenType::END_TAG_TOKEN: |
911 | 172k | c = ReadByte(); |
912 | 172k | if (eof_) break; |
913 | 171k | if (c == '>') { |
914 | | // "</> does not generate a token at all. Generate an empty comment |
915 | | // to allow passthrough clients to pick up the data using raw_. |
916 | | // Reset the tokenizer state and start again. |
917 | 11.0k | token_type_ = TokenType::COMMENT_TOKEN; |
918 | 11.0k | return token_type_; |
919 | 11.0k | } |
920 | 160k | if (Strings::IsCharAlphabet(c)) { |
921 | 159k | ReadTag(false); |
922 | 159k | if (eof_) { |
923 | 59 | token_type_ = TokenType::ERROR_TOKEN; |
924 | 159k | } else { |
925 | 159k | token_type_ = TokenType::END_TAG_TOKEN; |
926 | 159k | } |
927 | 159k | return token_type_; |
928 | 159k | } |
929 | 1.78k | UnreadByte(); |
930 | 1.78k | ReadUntilCloseAngle(); |
931 | 1.78k | token_type_ = TokenType::COMMENT_TOKEN; |
932 | 1.78k | return token_type_; |
933 | 990k | case TokenType::COMMENT_TOKEN: { |
934 | 990k | if (c == '!') { |
935 | 987k | token_type_ = ReadMarkupDeclaration(); |
936 | 987k | return token_type_; |
937 | 987k | } |
938 | 3.08k | is_token_manufactured_ = true; |
939 | | // <? is part of the comment text. |
940 | 3.08k | UnreadByte(); |
941 | 3.08k | ReadUntilCloseAngle(); |
942 | 3.08k | token_type_ = TokenType::COMMENT_TOKEN; |
943 | 3.08k | return token_type_; |
944 | 990k | } |
945 | 0 | default: |
946 | 0 | break; |
947 | 16.0M | } |
948 | 16.0M | } |
949 | | |
950 | 10.2k | if (raw_.start < raw_.end) { |
951 | 2.43k | data_.end = raw_.end; |
952 | 2.43k | token_type_ = TokenType::TEXT_TOKEN; |
953 | 2.43k | return token_type_; |
954 | 2.43k | } |
955 | | |
956 | 7.83k | token_type_ = TokenType::ERROR_TOKEN; |
957 | 7.83k | return token_type_; |
958 | 10.2k | } |
959 | | |
960 | 0 | std::string_view Tokenizer::Raw() { |
961 | 0 | int size = raw_.end - raw_.start; |
962 | 0 | return buffer_.substr(raw_.start, size); |
963 | 0 | } |
964 | | |
965 | 1.63M | std::string Tokenizer::Text() { |
966 | 1.63M | switch (token_type_) { |
967 | 631k | case TokenType::TEXT_TOKEN: |
968 | 1.63M | case TokenType::COMMENT_TOKEN: |
969 | 1.63M | case TokenType::DOCTYPE_TOKEN: { |
970 | 1.63M | int size = data_.end - data_.start; |
971 | 1.63M | std::string s(buffer_.substr(data_.start, size)); |
972 | 1.63M | data_.start = raw_.end; |
973 | 1.63M | data_.end = raw_.end; |
974 | 1.63M | Strings::ConvertNewLines(&s); |
975 | 1.63M | if (convert_null_ || token_type_ == TokenType::COMMENT_TOKEN) { |
976 | | // Replace \x00 with \ufffd. |
977 | 1.05M | Strings::ReplaceAny(&s, |
978 | 1.05M | Strings::kNullChar, |
979 | 1.05M | Strings::kNullReplacementChar); |
980 | 1.05M | } |
981 | 1.63M | if (!text_is_raw_) Strings::UnescapeString(&s, false); |
982 | 1.63M | return s; |
983 | 1.63M | } |
984 | 0 | default: |
985 | 0 | break; |
986 | 1.63M | } |
987 | | |
988 | 0 | return ""; |
989 | 1.63M | } |
990 | | |
991 | 15.0M | std::optional<std::tuple<std::string, bool>> Tokenizer::TagName() { |
992 | 15.0M | if (data_.start < data_.end) { |
993 | 15.0M | switch (token_type_) { |
994 | 14.8M | case TokenType::START_TAG_TOKEN: |
995 | 15.0M | case TokenType::END_TAG_TOKEN: |
996 | 15.0M | case TokenType::SELF_CLOSING_TAG_TOKEN: { |
997 | 15.0M | int size = data_.end - data_.start; |
998 | 15.0M | std::string s(buffer_.substr(data_.start, size)); |
999 | 15.0M | data_.start = raw_.end; |
1000 | 15.0M | data_.end = raw_.end; |
1001 | 15.0M | Strings::ToLower(&s); |
1002 | 15.0M | return std::make_tuple<std::string, bool>(std::move(s), |
1003 | 15.0M | n_attributes_returned_ < attributes_.size()); |
1004 | 15.0M | } |
1005 | 0 | default: |
1006 | 0 | break; |
1007 | 15.0M | } |
1008 | 15.0M | } |
1009 | | |
1010 | 0 | return std::nullopt; |
1011 | 15.0M | } |
1012 | | |
1013 | 1.10M | std::optional<std::tuple<Attribute, bool>> Tokenizer::TagAttr() { |
1014 | 1.10M | if (n_attributes_returned_ < attributes_.size()) { |
1015 | 1.10M | switch (token_type_) { |
1016 | 1.10M | case TokenType::START_TAG_TOKEN: |
1017 | 1.10M | case TokenType::SELF_CLOSING_TAG_TOKEN: { |
1018 | 1.10M | auto attr = attributes_[n_attributes_returned_]; |
1019 | 1.10M | n_attributes_returned_++; |
1020 | 1.10M | int size = std::get<0>(attr).end - std::get<0>(attr).start; |
1021 | 1.10M | std::string key(buffer_.substr(std::get<0>(attr).start, size)); |
1022 | 1.10M | int value_size = std::get<1>(attr).end - std::get<1>(attr).start; |
1023 | 1.10M | std::string val(buffer_.substr(std::get<1>(attr).start, value_size)); |
1024 | 1.10M | Strings::ToLower(&key); |
1025 | 1.10M | Strings::ConvertNewLines(&val); |
1026 | 1.10M | Strings::UnescapeString(&val, true); |
1027 | 1.10M | return std::make_tuple<Attribute, bool>( |
1028 | 1.10M | {.name_space = "", |
1029 | 1.10M | .key = std::move(key), |
1030 | 1.10M | .value = std::move(val), |
1031 | 1.10M | .line_col_in_html_src = std::get<LineCol>(attr)}, |
1032 | 1.10M | n_attributes_returned_ < attributes_.size()); |
1033 | 1.10M | } |
1034 | 0 | default: |
1035 | 0 | break; |
1036 | 1.10M | } |
1037 | 1.10M | } |
1038 | | |
1039 | 0 | return std::nullopt; |
1040 | 1.10M | } |
1041 | | |
1042 | 16.6M | Token Tokenizer::token() { |
1043 | 16.6M | Token t; |
1044 | 16.6M | t.token_type = token_type_; |
1045 | 16.6M | switch (token_type_) { |
1046 | 631k | case TokenType::TEXT_TOKEN: { |
1047 | 631k | t.data = Text(); |
1048 | 631k | int line_number = current_line_col_.first; |
1049 | 631k | int column_number = current_line_col_.second - t.data.size(); |
1050 | | // Shift to previous line, where this text belongs. |
1051 | 631k | if (column_number < 0) { |
1052 | 257k | if (lines_cols_.size() > 1) { |
1053 | 257k | auto previous_token_linecol = lines_cols_[lines_cols_.size() - 2]; |
1054 | 257k | line_number = previous_token_linecol.first; |
1055 | 257k | column_number = |
1056 | 257k | previous_token_linecol.second - abs(column_number) + 1; |
1057 | 257k | } else { |
1058 | 414 | column_number = 0; |
1059 | 414 | } |
1060 | 257k | } |
1061 | 631k | token_line_col_ = {line_number, column_number}; |
1062 | 631k | break; |
1063 | 0 | } |
1064 | 1.00M | case TokenType::COMMENT_TOKEN: |
1065 | 1.00M | case TokenType::DOCTYPE_TOKEN: |
1066 | 1.00M | t.data = Text(); |
1067 | 1.00M | t.is_manufactured = is_token_manufactured_; |
1068 | 1.00M | token_line_col_ = {current_line_col_.first, |
1069 | 1.00M | current_line_col_.second - t.data.size()}; |
1070 | 1.00M | break; |
1071 | 14.8M | case TokenType::START_TAG_TOKEN: |
1072 | 14.8M | case TokenType::SELF_CLOSING_TAG_TOKEN: |
1073 | 15.0M | case TokenType::END_TAG_TOKEN: { |
1074 | 15.0M | auto tag_name_value = TagName(); |
1075 | 15.0M | if (tag_name_value.has_value()) { |
1076 | 15.0M | std::string tag_name = std::get<0>(tag_name_value.value()); |
1077 | 15.0M | bool has_attributes = std::get<1>(tag_name_value.value()); |
1078 | 15.0M | Atom atom = AtomUtil::ToAtom(tag_name); |
1079 | 15.0M | if (atom != Atom::UNKNOWN) { |
1080 | 4.97M | t.atom = atom; |
1081 | 10.0M | } else { |
1082 | 10.0M | t.atom = Atom::UNKNOWN; |
1083 | 10.0M | t.data = tag_name; |
1084 | 10.0M | } |
1085 | 15.0M | if (has_attributes) { |
1086 | 1.10M | while (true) { |
1087 | 1.10M | auto a = TagAttr(); |
1088 | 1.10M | if (!a.has_value()) break; |
1089 | 1.10M | auto attr = std::get<Attribute>(a.value()); |
1090 | 1.10M | bool more_attributes = std::get<bool>(a.value()); |
1091 | 1.10M | t.attributes.push_back(attr); |
1092 | 1.10M | if (!more_attributes) break; |
1093 | 1.10M | } |
1094 | 119k | } |
1095 | 15.0M | } |
1096 | 15.0M | break; |
1097 | 14.8M | } |
1098 | 12.6k | case TokenType::ERROR_TOKEN: |
1099 | | // Ignore. |
1100 | 12.6k | break; |
1101 | 16.6M | } |
1102 | | |
1103 | 16.6M | t.line_col_in_html_src = token_line_col_; |
1104 | 16.6M | return t; |
1105 | 16.6M | } |
1106 | | |
1107 | | } // namespace htmlparser |