Coverage Report

Created: 2026-12-01 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/proc/self/cwd/cpp/htmlparser/parser.cc
Line
Count
Source
1
#include <algorithm>
2
#include <set>
3
#include <tuple>
4
#ifdef DUMP_NODES
5
#include <iostream>  // For DumpDocument
6
#endif               // DUMP_NODES
7
8
#include "absl/flags/flag.h"
9
#include "absl/status/status.h"
10
#include "cpp/htmlparser/atomutil.h"
11
#include "cpp/htmlparser/comparators.h"
12
#include "cpp/htmlparser/defer.h"
13
#include "cpp/htmlparser/doctype.h"
14
#include "cpp/htmlparser/foreign.h"
15
#include "cpp/htmlparser/logging.h"
16
#include "cpp/htmlparser/parser.h"
17
#include "cpp/htmlparser/strings.h"
18
19
ABSL_RETIRED_FLAG(uint32_t, htmlparser_max_nodes_depth_count, 245, "retired");
20
21
namespace htmlparser {
22
23
namespace {
24
// Internal functions forward declarations.
25
std::string ExtractWhitespace(const std::string& s);
26
27
#ifdef DUMP_NODES
28
void DumpNode(Node* root_node) {
29
  for (Node* c = root_node->FirstChild(); c; c = c->NextSibling()) {
30
    std::cerr << c->NameSpace() << ": " << AtomUtil::ToString(c->DataAtom())
31
              << std::endl;
32
    DumpNode(c);
33
  }
34
}
35
// Dumps the nodes in the DOM in their final order after parsing.
36
void DumpDocument(Document* doc) { DumpNode(doc->RootNode()); }
37
38
#endif  // DUMP_NODES
39
40
}  // namespace.
41
42
0
std::unique_ptr<Document> Parse(std::string_view html) {
43
0
  std::unique_ptr<Parser> parser = std::make_unique<Parser>(
44
0
      html,
45
0
      ParseOptions{.scripting = true,
46
0
                   .frameset_ok = true,
47
0
                   .record_node_offsets = true,
48
0
                   .record_attribute_offsets = true,
49
0
                   .count_num_terms_in_text_node = true});
50
0
  return parser->Parse();
51
0
}
52
53
std::unique_ptr<Document> ParseWithOptions(std::string_view html,
54
0
                                           const ParseOptions& options) {
55
0
  return std::make_unique<Parser>(html, options)->Parse();
56
0
}
57
58
std::unique_ptr<Document> ParseFragmentWithOptions(std::string_view html,
59
                                                   const ParseOptions& options,
60
0
                                                   Node* fragment_parent) {
61
0
  std::unique_ptr<Parser> parser = std::make_unique<Parser>(
62
0
      html, options, fragment_parent);
63
0
  Node* root = parser->document_->NewNode(NodeType::ELEMENT_NODE, Atom::HTML);
64
0
  parser->document_->root_node_->AppendChild(root);
65
0
  parser->open_elements_stack_.Push(root);
66
67
0
  if (fragment_parent && fragment_parent->DataAtom() == Atom::TEMPLATE) {
68
0
    parser->template_stack_.push_back(std::bind(&Parser::InTemplateIM,
69
0
                                                parser.get()));
70
0
  }
71
72
0
  parser->ResetInsertionMode();
73
74
0
  for (Node* node = fragment_parent; node; node = node->Parent()) {
75
0
    if (node->Type() == NodeType::ELEMENT_NODE &&
76
0
        node->DataAtom() == Atom::FORM) {
77
0
      parser->form_ = node;
78
0
      break;
79
0
    }
80
0
  }
81
82
0
  auto doc = parser->Parse();
83
84
0
  if (doc->status().ok()) {
85
0
    Node* parent = fragment_parent ? root : doc->root_node_;
86
0
    for (Node* c = parent->FirstChild(); c;) {
87
0
      Node* next = c->NextSibling();
88
0
      doc->fragment_nodes_.push_back(std::move(c));
89
0
      parent->RemoveChild(c);
90
0
      c = next;
91
0
    }
92
0
  }
93
94
0
  return doc;
95
0
}
96
97
std::unique_ptr<Document> ParseFragment(std::string_view html,
98
0
                                        Node* fragment_parent) {
99
  // Expects clients to update the offsets relative to the parent which
100
  // this fragment belongs.
101
0
  ParseOptions options = {.scripting = true,
102
0
                          .frameset_ok = true,
103
0
                          .record_node_offsets = true,
104
0
                          .record_attribute_offsets = true,
105
0
                          .count_num_terms_in_text_node = true};
106
0
  return ParseFragmentWithOptions(html, options, fragment_parent);
107
0
}
108
109
Parser::Parser(std::string_view html, const ParseOptions& options,
110
               Node* fragment_parent)
111
12.6k
    : tokenizer_(std::make_unique<Tokenizer>(
112
12.6k
          html,
113
12.6k
          fragment_parent ? AtomUtil::ToString(fragment_parent->atom_) : "")),
114
12.6k
      on_node_callback_(options.on_node_callback),
115
12.6k
      document_(new Document),
116
12.6k
      scope_marker_(document_->NewNode(NodeType::SCOPE_MARKER_NODE)),
117
12.6k
      scripting_(options.scripting),
118
12.6k
      frameset_ok_(options.frameset_ok),
119
12.6k
      record_node_offsets_(options.record_node_offsets),
120
12.6k
      record_attribute_offsets_(options.record_attribute_offsets),
121
12.6k
      count_num_terms_in_text_node_(options.count_num_terms_in_text_node),
122
12.6k
      fragment_(fragment_parent != nullptr),
123
12.6k
      context_node_(fragment_parent) {
124
12.6k
  document_->metadata_.html_src_bytes = html.size();
125
12.6k
  insertion_mode_ = std::bind(&Parser::InitialIM, this);
126
12.6k
}
127
128
12.6k
std::unique_ptr<Document> Parser::Parse() {
129
12.6k
  bool eof = tokenizer_->IsEOF();
130
16.7M
  while (!eof) {
131
16.6M
    Node* node = open_elements_stack_.Top();
132
16.6M
    tokenizer_->SetAllowCDATA(node && !node->name_space_.empty());
133
    // Read and parse the next token.
134
16.6M
    TokenType token_type = tokenizer_->Next(!template_stack_.empty());
135
136
    // No end of input, but error token. Parsing failed.
137
16.6M
    if (token_type == TokenType::ERROR_TOKEN) {
138
12.6k
      eof = tokenizer_->IsEOF();
139
12.6k
      if (!eof && tokenizer_->Error()) {
140
0
        document_->status_ = absl::InvalidArgumentError(
141
0
            "htmlparser::Parser tokenizer error.");
142
0
        return std::move(document_);
143
0
      }
144
12.6k
    }
145
16.6M
    token_ = tokenizer_->token();
146
16.6M
    ParseCurrentToken();
147
16.6M
  }
148
149
#ifdef DUMP_NODES
150
  DumpDocument(document_.get());
151
#endif
152
153
12.6k
  document_->metadata_.document_end_location = tokenizer_->CurrentPosition();
154
12.6k
  return std::move(document_);
155
12.6k
}  // End Parser::Parse.
156
157
31.3M
Node* Parser::top() {
158
31.3M
  Node* node = open_elements_stack_.Top();
159
31.3M
  if (node) {
160
31.3M
    return node;
161
31.3M
  }
162
163
12.6k
  return document_->root_node_;
164
31.3M
}  // End Parser::Top.
165
166
template <typename... Args>
167
295k
bool Parser::PopUntil(Scope scope, Args... match_tags) {
168
295k
  std::vector<Atom> argsList{match_tags...};
169
295k
  int i = IndexOfElementInScope(scope, argsList);
170
295k
  if (i != -1) {
171
170k
    open_elements_stack_.Pop(open_elements_stack_.size() - i);
172
170k
    return true;
173
170k
  }
174
125k
  return false;
175
295k
}  // End Parser::PopUntil.
bool htmlparser::Parser::PopUntil<htmlparser::Atom>(htmlparser::Parser::Scope, htmlparser::Atom)
Line
Count
Source
167
200k
bool Parser::PopUntil(Scope scope, Args... match_tags) {
168
200k
  std::vector<Atom> argsList{match_tags...};
169
200k
  int i = IndexOfElementInScope(scope, argsList);
170
200k
  if (i != -1) {
171
76.7k
    open_elements_stack_.Pop(open_elements_stack_.size() - i);
172
76.7k
    return true;
173
76.7k
  }
174
124k
  return false;
175
200k
}  // End Parser::PopUntil.
bool htmlparser::Parser::PopUntil<htmlparser::Atom, htmlparser::Atom, htmlparser::Atom, htmlparser::Atom, htmlparser::Atom, htmlparser::Atom>(htmlparser::Parser::Scope, htmlparser::Atom, htmlparser::Atom, htmlparser::Atom, htmlparser::Atom, htmlparser::Atom, htmlparser::Atom)
Line
Count
Source
167
1.56k
bool Parser::PopUntil(Scope scope, Args... match_tags) {
168
1.56k
  std::vector<Atom> argsList{match_tags...};
169
1.56k
  int i = IndexOfElementInScope(scope, argsList);
170
1.56k
  if (i != -1) {
171
499
    open_elements_stack_.Pop(open_elements_stack_.size() - i);
172
499
    return true;
173
499
  }
174
1.06k
  return false;
175
1.56k
}  // End Parser::PopUntil.
bool htmlparser::Parser::PopUntil<htmlparser::Atom, htmlparser::Atom, htmlparser::Atom>(htmlparser::Parser::Scope, htmlparser::Atom, htmlparser::Atom, htmlparser::Atom)
Line
Count
Source
167
46.1k
bool Parser::PopUntil(Scope scope, Args... match_tags) {
168
46.1k
  std::vector<Atom> argsList{match_tags...};
169
46.1k
  int i = IndexOfElementInScope(scope, argsList);
170
46.1k
  if (i != -1) {
171
46.1k
    open_elements_stack_.Pop(open_elements_stack_.size() - i);
172
46.1k
    return true;
173
46.1k
  }
174
0
  return false;
175
46.1k
}  // End Parser::PopUntil.
bool htmlparser::Parser::PopUntil<htmlparser::Atom, htmlparser::Atom>(htmlparser::Parser::Scope, htmlparser::Atom, htmlparser::Atom)
Line
Count
Source
167
47.4k
bool Parser::PopUntil(Scope scope, Args... match_tags) {
168
47.4k
  std::vector<Atom> argsList{match_tags...};
169
47.4k
  int i = IndexOfElementInScope(scope, argsList);
170
47.4k
  if (i != -1) {
171
47.4k
    open_elements_stack_.Pop(open_elements_stack_.size() - i);
172
47.4k
    return true;
173
47.4k
  }
174
0
  return false;
175
47.4k
}  // End Parser::PopUntil.
176
177
int Parser::IndexOfElementInScope(Scope scope,
178
608k
                                  const std::vector<Atom>& match_tags) const {
179
154M
  for (int i = open_elements_stack_.size() - 1; i >= 0; --i) {
180
154M
    Node* node = open_elements_stack_.at(i);
181
154M
    if (node->name_space_.empty()) {
182
154M
      for (Atom a : match_tags) {
183
154M
        if (a == node->atom_) {
184
472k
          return i;
185
472k
        }
186
154M
      }
187
153M
      switch (scope) {
188
67.4M
        case Scope::DefaultScope:
189
          // No-op.
190
67.4M
          break;
191
980
        case Scope::ListItemScope:
192
980
          if (node->atom_ == Atom::OL || node->atom_ == Atom::UL) return -1;
193
592
          break;
194
85.5M
        case Scope::ButtonScope:
195
85.5M
          if (node->atom_ == Atom::BUTTON) return -1;
196
85.5M
          break;
197
85.5M
        case Scope::TableScope:
198
624k
          if (node->atom_ == Atom::HTML || node->atom_ == Atom::TABLE ||
199
622k
              node->atom_ == Atom::TEMPLATE) {
200
1.20k
            return -1;
201
1.20k
          }
202
622k
          break;
203
622k
        case Scope::SelectScope:
204
0
          if (node->atom_ != Atom::OPTGROUP && node->atom_ != Atom::OPTION) {
205
0
            return -1;
206
0
          }
207
0
          break;
208
0
        default:
209
0
          CHECK(false) << "HTML Parser reached unreachable scope";
210
153M
      }
211
153M
    }
212
213
153M
    switch (scope) {
214
67.4M
      case Scope::DefaultScope:
215
67.4M
      case Scope::ListItemScope:
216
153M
      case Scope::ButtonScope: {
217
458M
        for (auto& scope_stop_tags : kDefaultScopeStopTags) {
218
458M
          if (scope_stop_tags.first == node->name_space_) {
219
1.37G
            for (Atom t : scope_stop_tags.second) {
220
1.37G
              if (t == Atom::UNKNOWN) break;
221
1.37G
              if (t == node->atom_) return -1;
222
1.37G
            }
223
153M
          }
224
458M
        }
225
152M
        break;
226
153M
      }
227
152M
      default:
228
622k
        break;
229
153M
    }
230
153M
  }
231
0
  return -1;
232
608k
}  // Parser::IndexOfElementInScope.
233
234
template <typename... Args>
235
308k
bool Parser::ElementInScope(Scope scope, Args... tags) const {
236
308k
  std::vector<Atom> argsList{tags...};
237
308k
  return IndexOfElementInScope(scope, argsList) != -1;
238
308k
}  // Parser::ElementInScope.
239
240
3.63M
void Parser::ClearStackToContext(Scope scope) {
241
6.24M
  for (int i = open_elements_stack_.size() - 1; i >= 0; --i) {
242
6.24M
    Node* node = open_elements_stack_.at(i);
243
6.24M
    Atom atom = node->atom_;
244
6.24M
    switch (scope) {
245
2.25M
      case Scope::TableScope:
246
2.25M
        if (atom == Atom::HTML || atom == Atom::TABLE ||
247
1.22M
            atom == Atom::TEMPLATE) {
248
1.22M
          open_elements_stack_.Pop(open_elements_stack_.size() - i - 1);
249
1.22M
          return;
250
1.22M
        }
251
1.03M
        break;
252
1.62M
      case Scope::TableRowScope:
253
1.62M
        if (atom == Atom::HTML || atom == Atom::TR || atom == Atom::TEMPLATE) {
254
1.22M
          open_elements_stack_.Pop(open_elements_stack_.size() - i - 1);
255
1.22M
          return;
256
1.22M
        }
257
401k
        break;
258
2.36M
      case Scope::TableBodyScope:
259
2.36M
        if (atom == Atom::HTML || atom == Atom::TBODY || atom == Atom::TFOOT ||
260
1.18M
            atom == Atom::THEAD || atom == Atom::TEMPLATE) {
261
1.18M
          open_elements_stack_.Pop(open_elements_stack_.size() - i - 1);
262
1.18M
          return;
263
1.18M
        }
264
1.17M
        break;
265
1.17M
      default:
266
0
        CHECK(false) << "HTML Parser reached unreachable scope";
267
6.24M
    }
268
6.24M
  }
269
3.63M
}  // Parser::ClearStackToContext.
270
271
void Parser::GenerateImpliedEndTags(
272
3.88k
    const std::initializer_list<Atom>& exceptions) {
273
3.88k
  int i = open_elements_stack_.size() - 1;
274
6.94k
  for (; i >= 0; --i) {
275
6.94k
    Node* node = open_elements_stack_.at(i);
276
6.94k
    if (node->node_type_ == NodeType::ELEMENT_NODE) {
277
6.94k
      switch (node->atom_) {
278
353
        case Atom::DD:
279
593
        case Atom::DT:
280
1.16k
        case Atom::LI:
281
1.16k
        case Atom::OPTGROUP:
282
1.16k
        case Atom::OPTION:
283
1.47k
        case Atom::P:
284
1.89k
        case Atom::RB:
285
2.38k
        case Atom::RP:
286
3.05k
        case Atom::RT:
287
3.25k
        case Atom::RTC:
288
3.25k
          for (auto e : exceptions) {
289
1.55k
            if (node->atom_ == e) {
290
              // Pop nodes and return early.
291
194
              open_elements_stack_.Pop(open_elements_stack_.size() - i - 1);
292
194
              return;
293
194
            }
294
1.55k
          }
295
3.05k
          continue;
296
3.68k
        default:
297
3.68k
          break;
298
6.94k
      }
299
6.94k
    }
300
3.68k
    break;
301
6.94k
  }
302
3.68k
  open_elements_stack_.Pop(open_elements_stack_.size() - i - 1);
303
3.68k
}  // Parser::GenerateImpliedEndTags.
304
305
22.1M
void Parser::AddChild(Node* node) {
306
22.1M
  if (ShouldFosterParent()) {
307
45.5k
    FosterParent(node);
308
22.1M
  } else {
309
22.1M
    top()->AppendChild(node);
310
22.1M
  }
311
312
22.1M
  if (node->node_type_ == NodeType::ELEMENT_NODE) {
313
20.5M
    open_elements_stack_.Push(node);
314
20.5M
  }
315
22.1M
}  // Parser::AddChild.
316
317
22.7M
bool Parser::ShouldFosterParent() {
318
22.7M
  if (!foster_parenting_) return false;
319
3.87M
  Atom a = top()->atom_;
320
3.87M
  return (a == Atom::TABLE || a == Atom::TBODY || a == Atom::TFOOT ||
321
3.82M
          a == Atom::THEAD || a == Atom::TR);
322
22.7M
}  // Parser::ShouldFosterParent.
323
324
82.7k
void Parser::FosterParent(Node* node) {
325
82.7k
  Node* table = nullptr;
326
82.7k
  Node* parent = nullptr;
327
82.7k
  Node* prev = nullptr;
328
82.7k
  Node* tpl = nullptr;
329
82.7k
  int i = -1;
330
498k
  for (i = open_elements_stack_.size() - 1; i >= 0; --i) {
331
498k
    if (open_elements_stack_.at(i)->atom_ == Atom::TABLE) {
332
82.7k
      table = open_elements_stack_.at(i);
333
82.7k
      break;
334
82.7k
    }
335
498k
  }
336
337
82.7k
  int j = -1;
338
435M
  for (j = open_elements_stack_.size() - 1; j >= 0; --j) {
339
435M
    if (open_elements_stack_.at(j)->atom_ == Atom::TEMPLATE) {
340
0
      tpl = open_elements_stack_.at(j);
341
0
      break;
342
0
    }
343
435M
  }
344
345
82.7k
  if (tpl && (!table || j > i)) {
346
0
    tpl->AppendChild(node);
347
0
    return;
348
0
  }
349
350
82.7k
  if (!table) {
351
    // The foster parent is the html element.
352
0
    parent = open_elements_stack_.at(0);
353
82.7k
  } else {
354
82.7k
    parent = table->Parent();
355
82.7k
  }
356
357
82.7k
  if (!parent) {
358
0
    parent = open_elements_stack_.at(i - 1);
359
0
  }
360
361
82.7k
  if (table) {
362
82.7k
    prev = table->PrevSibling();
363
82.7k
  } else {
364
0
    prev = parent->LastChild();
365
0
  }
366
367
82.7k
  if (prev && prev->node_type_ == NodeType::TEXT_NODE &&
368
4.53k
      node->node_type_ == NodeType::TEXT_NODE) {
369
3.79k
    prev->data_.append(node->data_);
370
3.79k
    return;
371
3.79k
  }
372
373
78.9k
  parent->InsertBefore(node, table);
374
78.9k
}  // Parser::FosterParent.
375
376
628k
void Parser::AddText(const std::string& text) {
377
628k
  if (text.empty()) return;
378
379
627k
  auto text_node = document_->NewNode(NodeType::TEXT_NODE);
380
627k
  if (record_node_offsets_) {
381
0
    text_node->line_col_in_html_src_ = token_.line_col_in_html_src;
382
0
  }
383
384
627k
  if (ShouldFosterParent()) {
385
9.33k
    text_node->data_.assign(text, 0, text.size());
386
9.33k
    FosterParent(text_node);
387
9.33k
    return;
388
9.33k
  }
389
390
618k
  Node* top_node = top();
391
618k
  if (top_node->LastChild() &&
392
48.9k
      top_node->LastChild()->node_type_ == NodeType::TEXT_NODE) {
393
2.76k
    top_node->LastChild()->data_.append(text);
394
2.76k
    return;
395
2.76k
  }
396
397
615k
  text_node->data_.assign(text, 0, text.size());
398
615k
  AddChild(text_node);
399
  // Count number of terms in ths text node, except if this is <script>,
400
  // <textarea> or a comment node.
401
615k
  if (count_num_terms_in_text_node_ && text_node->Parent() &&
402
0
      text_node->Parent()->DataAtom() != Atom::SCRIPT &&
403
0
      text_node->Parent()->Type() != NodeType::COMMENT_NODE &&
404
0
      text_node->Parent()->DataAtom() != Atom::TEXTAREA) {
405
0
    text_node->num_terms_ = Strings::CountTerms(text);
406
0
  }
407
615k
}  // Parser::AddText.
408
409
17.2M
void Parser::AddElement() {
410
17.2M
  Node* element_node = document_->NewNode(NodeType::ELEMENT_NODE, token_.atom);
411
17.2M
  if (token_.atom == Atom::UNKNOWN) {
412
10.0M
    element_node->data_ = token_.data;
413
10.0M
  }
414
415
17.2M
  if (record_node_offsets_) {
416
0
    element_node->line_col_in_html_src_ = token_.line_col_in_html_src;
417
0
  }
418
419
17.2M
  switch (token_.atom) {
420
12.8k
    case Atom::HTML: {
421
12.8k
      element_node->SetManufactured(document_->metadata_.has_manufactured_html);
422
12.8k
      break;
423
0
    }
424
12.6k
    case Atom::HEAD: {
425
12.6k
      element_node->SetManufactured(document_->metadata_.has_manufactured_head);
426
12.6k
      break;
427
0
    }
428
12.6k
    case Atom::BODY: {
429
12.6k
      element_node->SetManufactured(document_->metadata_.has_manufactured_body);
430
12.6k
      break;
431
0
    }
432
17.2M
    default:
433
17.2M
      break;
434
17.2M
  }
435
436
17.2M
  std::copy(token_.attributes.begin(), token_.attributes.end(),
437
17.2M
            std::back_inserter(element_node->attributes_));
438
17.2M
  AddChild(element_node);
439
440
17.2M
  if (!record_attribute_offsets_ && !element_node->attributes_.empty()) {
441
115k
    std::transform(
442
115k
        element_node->attributes_.begin(), element_node->attributes_.end(),
443
1.09M
        element_node->attributes_.begin(), [](Attribute attr) -> Attribute {
444
1.09M
          attr.line_col_in_html_src = std::nullopt;
445
1.09M
          return attr;
446
1.09M
        });
447
115k
  }
448
449
17.2M
  if (on_node_callback_) {
450
0
    on_node_callback_(element_node, token_);
451
0
  }
452
17.2M
}  // Parser::AddElement.
453
454
// Section 12.2.4.3.
455
1.93M
void Parser::AddFormattingElement() {
456
1.93M
  Atom tag_atom = token_.atom;
457
1.93M
  AddElement();
458
459
  // Implement the Noah's Ark clause, but with three per family instead of two.
460
1.93M
  int identical_elements = 0;
461
2.56G
  for (int i = active_formatting_elements_stack_.size() - 1; i >= 0; --i) {
462
2.56G
    Node* node = active_formatting_elements_stack_.at(i);
463
2.56G
    if (node->node_type_ == NodeType::SCOPE_MARKER_NODE) break;
464
2.56G
    if (node->node_type_ != NodeType::ELEMENT_NODE) continue;
465
2.56G
    if (node->name_space_ != "") continue;
466
2.56G
    if (node->atom_ != tag_atom) continue;
467
2.35G
    if (node->attributes_.size() != token_.attributes.size()) continue;
468
469
2.35G
    bool attr_matched = false;
470
2.35G
    for (int j = 0; j < node->attributes_.size(); ++j) {
471
1.63M
      for (int k = 0; k < token_.attributes.size(); ++k) {
472
1.20M
        attr_matched = (node->attributes_[j] == token_.attributes[k]);
473
        // Found a match for this attribute, continue with the next attribute.
474
1.20M
        if (attr_matched) break;
475
1.20M
      }
476
477
659k
      if (attr_matched) continue;
478
479
      // If we get here, there is no attribute that matches a.
480
      // Therefore the element is not identical to the new one.
481
      // Stop processing rest of the attributes and proceed to next element.
482
430k
      break;
483
659k
    }
484
485
2.35G
    if (attr_matched) {
486
150k
      identical_elements++;
487
150k
      if (identical_elements >= 3) {
488
49.9k
        active_formatting_elements_stack_.Remove(node);
489
49.9k
      }
490
150k
    }
491
2.35G
  }
492
493
1.93M
  active_formatting_elements_stack_.Push(top());
494
1.93M
}  // Parser::AddFormattingElement.
495
496
// Section 12.2.4.3.
497
47.8k
void Parser::ClearActiveFormattingElements() {
498
66.1k
  while (active_formatting_elements_stack_.size() != 0) {
499
66.1k
    Node* node = active_formatting_elements_stack_.Pop();
500
66.1k
    if (node->node_type_ == NodeType::SCOPE_MARKER_NODE) break;
501
66.1k
  }
502
47.8k
}  // Parser::ClearActiveFormattingElements.
503
504
// Section 12.2.4.3.
505
9.97M
void Parser::ReconstructActiveFormattingElements() {
506
9.97M
  Node* node = active_formatting_elements_stack_.Top();
507
9.97M
  if (!node) return;
508
509
4.92M
  if (node->node_type_ == NodeType::SCOPE_MARKER_NODE ||
510
4.87M
      open_elements_stack_.Index(node) != -1) {
511
4.87M
    return;
512
4.87M
  }
513
514
47.0k
  int i = active_formatting_elements_stack_.size() - 1;
515
3.26M
  while (node->node_type_ != NodeType::SCOPE_MARKER_NODE &&
516
3.26M
         open_elements_stack_.Index(node) == -1) {
517
3.25M
    if (i == 0) {
518
42.0k
      i = -1;
519
42.0k
      break;
520
42.0k
    }
521
3.21M
    i--;
522
3.21M
    node = active_formatting_elements_stack_.at(i);
523
3.21M
  }
524
525
3.25M
  do {
526
3.25M
    i++;
527
3.25M
    auto clone = document_->CloneNode(active_formatting_elements_stack_.at(i));
528
3.25M
    AddChild(clone);
529
3.25M
    active_formatting_elements_stack_.Replace(i, clone);
530
3.25M
  } while (i < active_formatting_elements_stack_.size() - 1);
531
47.0k
}  // Parser::ReconstructActiveFormattingElements.
532
533
// Section 12.2.5.
534
10.7k
void Parser::AcknowledgeSelfClosingTag() {
535
10.7k
  has_self_closing_token_ = false;
536
10.7k
}  // Parser::AcknowledgeSelfClosingTag.
537
538
// Section 12.2.4.1, "using the rules for".
539
23.5k
void Parser::SetOriginalIM() {
540
23.5k
  CHECK(!original_insertion_mode_)
541
23.5k
      << "html: bad parser state: original_insertion_mode was set twice";
542
23.5k
  original_insertion_mode_ = insertion_mode_;
543
23.5k
}  // Parser::SetOriginalIM.
544
545
// Section 12.2.4.1, "reset the insertion mode".
546
49.4k
void Parser::ResetInsertionMode() {
547
359k
  for (int i = open_elements_stack_.size() - 1; i >= 0; --i) {
548
359k
    Node* node = open_elements_stack_.at(i);
549
359k
    bool last = (i == 0);
550
359k
    if (last && context_node_) {
551
0
      node = context_node_;
552
0
    }
553
554
359k
    switch (node->atom_) {
555
0
      case Atom::SELECT:
556
0
        if (!last) {
557
0
          Node* ancestor = node;
558
0
          Node* first = open_elements_stack_.at(0);
559
0
          while (ancestor != first) {
560
0
            ancestor = open_elements_stack_.at(
561
0
                open_elements_stack_.Index(ancestor) - 1);
562
0
            switch (ancestor->atom_) {
563
0
              case Atom::TEMPLATE:
564
0
                insertion_mode_ = std::bind(&Parser::InSelectIM, this);
565
0
                return;
566
0
              case Atom::TABLE:
567
0
                insertion_mode_ = std::bind(&Parser::InSelectInTableIM, this);
568
0
                return;
569
0
              default:
570
0
                break;
571
0
            }
572
0
          }
573
0
        }
574
0
        insertion_mode_ = std::bind(&Parser::InSelectIM, this);
575
0
        break;
576
2.46k
      case Atom::TD:
577
47.5k
      case Atom::TH:
578
        // https://bugs.chromium.org/p/chromium/issues/detail?id=829668
579
47.5k
        insertion_mode_ = std::bind(&Parser::InCellIM, this);
580
47.5k
        break;
581
0
      case Atom::TR:
582
0
        insertion_mode_ = std::bind(&Parser::InRowIM, this);
583
0
        break;
584
0
      case Atom::TBODY:
585
0
      case Atom::THEAD:
586
0
      case Atom::TFOOT:
587
0
        insertion_mode_ = std::bind(&Parser::InTableBodyIM, this);
588
0
        break;
589
0
      case Atom::CAPTION:
590
0
        insertion_mode_ = std::bind(&Parser::InCaptionIM, this);
591
0
        break;
592
0
      case Atom::COLGROUP:
593
0
        insertion_mode_ = std::bind(&Parser::InColumnGroupIM, this);
594
0
        break;
595
0
      case Atom::TABLE:
596
0
        insertion_mode_ = std::bind(&Parser::InTableIM, this);
597
0
        break;
598
0
      case Atom::TEMPLATE:
599
        // TODO: remove this divergence from the HTML5 spec.
600
0
        if (!node->name_space_.empty()) {
601
0
          continue;
602
0
        }
603
0
        insertion_mode_ = template_stack_.back();
604
0
        break;
605
0
      case Atom::HEAD:
606
        // https://bugs.chromium.org/p/chromium/issues/detail?id=829668
607
0
        insertion_mode_ = std::bind(&Parser::InHeadIM, this);
608
0
        break;
609
1.89k
      case Atom::BODY:
610
1.89k
        insertion_mode_ = std::bind(&Parser::InBodyIM, this);
611
1.89k
        break;
612
0
      case Atom::FRAMESET:
613
0
        insertion_mode_ = std::bind(&Parser::InFramesetIM, this);
614
0
        break;
615
0
      case Atom::HTML:
616
0
        if (head_) {
617
0
          insertion_mode_ = std::bind(&Parser::AfterHeadIM, this);
618
0
        } else {
619
0
          insertion_mode_ = std::bind(&Parser::BeforeHeadIM, this);
620
0
        }
621
0
        break;
622
309k
      default:
623
309k
        if (last) {
624
0
          insertion_mode_ = std::bind(&Parser::InBodyIM, this);
625
0
          return;
626
0
        }
627
309k
        continue;
628
359k
    }
629
49.4k
    return;
630
359k
  }
631
49.4k
}  // Parser::ResetInsertionMode.
632
633
// Section 12.2.6.4.1.
634
18.8k
bool Parser::InitialIM() {
635
18.8k
  switch (token_.token_type) {
636
4.00k
    case TokenType::TEXT_TOKEN: {
637
      // https://www.w3.org/TR/2011/WD-html5-20110113/tokenization.html#the-initial-insertion-mode
638
4.00k
      Strings::TrimLeft(&token_.data, Strings::kWhitespace);
639
4.00k
      if (token_.data.empty()) {
640
        // It was all whitespace, so ignore it.
641
1.66k
        return true;
642
1.66k
      }
643
2.34k
      break;
644
4.00k
    }
645
4.58k
    case TokenType::COMMENT_TOKEN: {
646
4.58k
      Node* node = document_->NewNode(NodeType::COMMENT_NODE);
647
4.58k
      node->data_ = std::move(token_.data);
648
4.58k
      if (record_node_offsets_) {
649
0
        node->line_col_in_html_src_ = token_.line_col_in_html_src;
650
0
      }
651
4.58k
      node->SetManufactured(token_.is_manufactured);
652
4.58k
      document_->root_node_->AppendChild(node);
653
4.58k
      return true;
654
4.00k
    }
655
169
    case TokenType::DOCTYPE_TOKEN: {
656
169
      auto doctype_node = document_->NewNode(NodeType::DOCTYPE_NODE);
657
169
      bool quirks_mode = ParseDoctype(token_.data, doctype_node);
658
169
      if (record_node_offsets_) {
659
0
        doctype_node->line_col_in_html_src_ = token_.line_col_in_html_src;
660
0
      }
661
169
      document_->root_node_->AppendChild(doctype_node);
662
169
      document_->metadata_.quirks_mode = quirks_mode;
663
169
      insertion_mode_ = std::bind(&Parser::BeforeHTMLIM, this);
664
665
169
      if (on_node_callback_) {
666
0
        on_node_callback_(doctype_node, token_);
667
0
      }
668
669
169
      return true;
670
4.00k
    }
671
10.1k
    default:
672
10.1k
      break;
673
18.8k
  }
674
675
12.4k
  document_->metadata_.quirks_mode = true;
676
12.4k
  insertion_mode_ = std::bind(&Parser::BeforeHTMLIM, this);
677
12.4k
  return false;
678
18.8k
}  // Parser::InitialIM.
679
680
// Section 12.2.6.4.2.
681
27.4k
bool Parser::BeforeHTMLIM() {
682
27.4k
  switch (token_.token_type) {
683
373
    case TokenType::DOCTYPE_TOKEN: {
684
      // Ignore the token.
685
373
      return true;
686
0
    }
687
3.06k
    case TokenType::TEXT_TOKEN: {
688
      // https://www.w3.org/TR/2011/WD-html5-20110113/tokenization.html#the-before-html-insertion-mode
689
3.06k
      Strings::TrimLeft(&token_.data, Strings::kWhitespace);
690
3.06k
      if (token_.data.empty()) {
691
        // It was all whitespace, so ignore it.
692
703
        return true;
693
703
      }
694
2.36k
      break;
695
3.06k
    }
696
21.5k
    case TokenType::START_TAG_TOKEN: {
697
21.5k
      if (token_.atom == Atom::HTML) {
698
12.6k
        AddElement();
699
12.6k
        insertion_mode_ = std::bind(&Parser::BeforeHeadIM, this);
700
12.6k
        return true;
701
12.6k
      }
702
8.90k
      break;
703
21.5k
    }
704
8.90k
    case TokenType::END_TAG_TOKEN: {
705
574
      switch ((Atom)token_.atom) {
706
101
        case Atom::HEAD:
707
188
        case Atom::BODY:
708
291
        case Atom::HTML:
709
299
        case Atom::BR:
710
299
          ParseImpliedToken(TokenType::START_TAG_TOKEN, Atom::HTML,
711
299
                            AtomUtil::ToString(Atom::HTML));
712
299
          return false;
713
275
        default:
714
          // Ignore the token.
715
275
          return true;
716
574
      }
717
0
      break;
718
574
    }
719
880
    case TokenType::COMMENT_TOKEN: {
720
880
      Node* node = document_->NewNode(NodeType::COMMENT_NODE);
721
880
      node->SetManufactured(token_.is_manufactured);
722
880
      if (record_node_offsets_) {
723
0
        node->line_col_in_html_src_ = token_.line_col_in_html_src;
724
0
      }
725
880
      node->data_ = std::move(token_.data);
726
880
      document_->root_node_->AppendChild(node);
727
880
      return true;
728
574
    }
729
990
    default:
730
990
      break;
731
27.4k
  }
732
12.2k
  ParseImpliedToken(TokenType::START_TAG_TOKEN, Atom::HTML,
733
12.2k
                    AtomUtil::ToString(Atom::HTML));
734
12.2k
  return false;
735
27.4k
}  // Parser::BeforeHTMLIM.
736
737
// Section 12.2.6.4.3.
738
27.0k
bool Parser::BeforeHeadIM() {
739
27.0k
  switch (token_.token_type) {
740
2.94k
    case TokenType::TEXT_TOKEN: {
741
      // https://www.w3.org/TR/2011/WD-html5-20110113/tokenization.html#the-before-head-insertion-mode
742
2.94k
      Strings::TrimLeft(&token_.data, Strings::kWhitespace);
743
2.94k
      if (token_.data.empty()) {
744
        // It was all whitespace, so ignore it.
745
564
        return true;
746
564
      }
747
2.37k
      break;
748
2.94k
    }
749
21.8k
    case TokenType::START_TAG_TOKEN: {
750
21.8k
      switch (token_.atom) {
751
12.6k
        case Atom::HEAD:
752
12.6k
          AddElement();
753
12.6k
          head_ = top();
754
12.6k
          insertion_mode_ = std::bind(&Parser::InHeadIM, this);
755
12.6k
          return true;
756
387
        case Atom::HTML:
757
387
          return InBodyIM();
758
8.79k
        default:
759
8.79k
          break;
760
21.8k
      }
761
8.79k
      break;
762
21.8k
    }
763
8.79k
    case TokenType::END_TAG_TOKEN: {
764
524
      switch (token_.atom) {
765
101
        case Atom::HEAD:
766
188
        case Atom::BODY:
767
292
        case Atom::HTML:
768
300
        case Atom::BR:
769
300
          ParseImpliedToken(TokenType::START_TAG_TOKEN, Atom::HEAD,
770
300
                            AtomUtil::ToString(Atom::HEAD));
771
300
          return false;
772
224
        default:
773
          // Ignore the token.
774
224
          return true;
775
524
      }
776
0
      break;
777
524
    }
778
748
    case TokenType::COMMENT_TOKEN: {
779
748
      Node* node = document_->NewNode(NodeType::COMMENT_NODE);
780
748
      node->SetManufactured(token_.is_manufactured);
781
748
      if (record_node_offsets_) {
782
0
        node->line_col_in_html_src_ = token_.line_col_in_html_src;
783
0
      }
784
748
      node->data_ = std::move(token_.data);
785
748
      AddChild(node);
786
748
      return true;
787
524
    }
788
0
    case TokenType::DOCTYPE_TOKEN: {
789
      // Ignore the token.
790
0
      return true;
791
524
    }
792
1.05k
    default:
793
1.05k
      break;
794
27.0k
  }
795
796
12.2k
  ParseImpliedToken(TokenType::START_TAG_TOKEN, Atom::HEAD,
797
12.2k
                    AtomUtil::ToString(Atom::HEAD));
798
12.2k
  return false;
799
27.0k
}  // Parser::BeforeHeadIM.
800
801
// Section 12.2.6.4.4.
802
54.6k
bool Parser::InHeadIM() {
803
54.6k
  switch (token_.token_type) {
804
3.32k
    case TokenType::TEXT_TOKEN: {
805
3.32k
      std::string s = token_.data;
806
3.32k
      Strings::TrimLeft(&s, Strings::kWhitespace);
807
3.32k
      if (s.size() < token_.data.size()) {
808
        // Add the initial whitespace to the current node.
809
        // https://www.w3.org/TR/2011/WD-html5-20110113/tokenization.html#parsing-main-inhead
810
926
        AddText(token_.data.substr(0, token_.data.size() - s.size()));
811
926
        if (s.empty()) {
812
899
          return true;
813
899
        }
814
27
        token_.data = s;
815
27
      }
816
2.42k
      break;
817
3.32k
    }
818
35.0k
    case TokenType::START_TAG_TOKEN: {
819
35.0k
      switch (token_.atom) {
820
194
        case Atom::HTML:
821
194
          return InBodyIM();
822
998
        case Atom::BASE:
823
998
        case Atom::BASEFONT:
824
998
        case Atom::BGSOUND:
825
2.20k
        case Atom::LINK:
826
2.91k
        case Atom::META: {
827
2.91k
          AddElement();
828
2.91k
          open_elements_stack_.Pop();
829
2.91k
          AcknowledgeSelfClosingTag();
830
2.91k
          if (!top() || !top()->LastChild()) return true;
831
          // Record some extra document url related info.
832
2.72k
          if (token_.atom == Atom::BASE) {
833
998
            auto base_node = top()->LastChild();
834
998
            RecordBaseURLMetadata(base_node);
835
1.72k
          } else if (token_.atom == Atom::LINK) {
836
1.01k
            auto link_node = top()->LastChild();
837
1.01k
            RecordLinkRelCanonical(link_node);
838
1.01k
          }
839
2.72k
          return true;
840
2.91k
        }
841
223
        case Atom::NOSCRIPT: {
842
223
          if (scripting_) {
843
223
            ParseGenericRawTextElement();
844
223
            return true;
845
223
          }
846
0
          AddElement();
847
0
          insertion_mode_ = std::bind(&Parser::InHeadNoscriptIM, this);
848
          // Don't let the tokenizer go into raw text mode when scripting is
849
          // disabled.
850
0
          tokenizer_->NextIsNotRawText();
851
0
          return true;
852
223
        }
853
19.7k
        case Atom::SCRIPT:
854
23.5k
        case Atom::TITLE: {
855
23.5k
          AddElement();
856
23.5k
          SetOriginalIM();
857
23.5k
          insertion_mode_ = std::bind(&Parser::TextIM, this);
858
23.5k
          return true;
859
19.7k
        }
860
0
        case Atom::NOFRAMES:
861
267
        case Atom::STYLE: {
862
267
          ParseGenericRawTextElement();
863
267
          return true;
864
0
        }
865
215
        case Atom::HEAD: {
866
          // Ignore the token.
867
215
          return true;
868
0
        }
869
0
        case Atom::TEMPLATE: {
870
0
          AddElement();
871
0
          active_formatting_elements_stack_.Push(scope_marker_);
872
0
          frameset_ok_ = false;
873
0
          insertion_mode_ = std::bind(&Parser::InTemplateIM, this);
874
0
          template_stack_.push_back(std::bind(&Parser::InTemplateIM, this));
875
0
          return true;
876
0
        }
877
7.71k
        default:
878
          // Ignore remaining tags.
879
7.71k
          break;
880
35.0k
      }
881
7.71k
      break;
882
35.0k
    }
883
13.0k
    case TokenType::END_TAG_TOKEN: {
884
13.0k
      switch (token_.atom) {
885
12.6k
        case Atom::HEAD: {
886
12.6k
          open_elements_stack_.Pop();
887
12.6k
          insertion_mode_ = std::bind(&Parser::AfterHeadIM, this);
888
12.6k
          return true;
889
0
        }
890
87
        case Atom::BODY:
891
191
        case Atom::HTML:
892
199
        case Atom::BR: {
893
199
          ParseImpliedToken(TokenType::END_TAG_TOKEN, Atom::HEAD,
894
199
                            AtomUtil::ToString(Atom::HEAD));
895
199
          return false;
896
191
        }
897
0
        case Atom::TEMPLATE: {
898
0
          if (!open_elements_stack_.Contains(Atom::TEMPLATE)) return true;
899
900
          // See https://bugs.chromium.org/p/chromium/issues/detail?id=829668
901
0
          GenerateImpliedEndTags();
902
0
          for (int i = open_elements_stack_.size() - 1; i >= 0; --i) {
903
0
            Node* node = open_elements_stack_.at(i);
904
0
            if (node->name_space_.empty() && node->atom_ == Atom::TEMPLATE) {
905
0
              open_elements_stack_.Pop(open_elements_stack_.size() - i);
906
0
              break;
907
0
            }
908
0
          }
909
910
0
          ClearActiveFormattingElements();
911
0
          template_stack_.pop_back();
912
0
          ResetInsertionMode();
913
0
          return true;
914
0
        }
915
227
        default:
916
          // Ignore the token.
917
227
          return true;
918
13.0k
      }
919
0
      break;
920
13.0k
    }
921
1.06k
    case TokenType::COMMENT_TOKEN: {
922
1.06k
      Node* node = document_->NewNode(NodeType::COMMENT_NODE);
923
1.06k
      node->SetManufactured(token_.is_manufactured);
924
1.06k
      if (record_node_offsets_) {
925
0
        node->line_col_in_html_src_ = token_.line_col_in_html_src;
926
0
      }
927
1.06k
      node->data_ = std::move(token_.data);
928
1.06k
      AddChild(node);
929
1.06k
      return true;
930
13.0k
    }
931
0
    case TokenType::DOCTYPE_TOKEN: {
932
      // Ignore the token.
933
0
      return true;
934
13.0k
    }
935
2.18k
    default:
936
2.18k
      break;
937
54.6k
  }
938
939
12.3k
  ParseImpliedToken(TokenType::END_TAG_TOKEN, Atom::HEAD,
940
12.3k
                    AtomUtil::ToString(Atom::HEAD));
941
12.3k
  return false;
942
54.6k
}  // Parser::InHeadIM.
943
944
// 12.2.6.4.5.
945
0
bool Parser::InHeadNoscriptIM() {
946
0
  switch (token_.token_type) {
947
0
    case TokenType::DOCTYPE_TOKEN: {
948
      // Ignore the token.
949
0
      return true;
950
0
    }
951
0
    case TokenType::START_TAG_TOKEN: {
952
0
      switch (token_.atom) {
953
0
        case Atom::HTML: {
954
0
          return InBodyIM();
955
0
          break;
956
0
        }
957
0
        case Atom::BASEFONT:
958
0
        case Atom::BGSOUND:
959
0
        case Atom::LINK:
960
0
        case Atom::META:
961
0
        case Atom::NOFRAMES:
962
0
        case Atom::STYLE: {
963
0
          return InHeadIM();
964
0
          break;
965
0
        }
966
0
        case Atom::HEAD:
967
          // Ignore the token.
968
0
          return true;
969
0
        case Atom::NOSCRIPT: {
970
          // Don't let the tokenizer go into raw text mode even when a
971
          // <noscript> tag is in "in head noscript" insertion mode.
972
0
          tokenizer_->NextIsNotRawText();
973
          // Ignore the token.
974
0
          return true;
975
0
        }
976
0
        default:
977
0
          break;
978
0
      }
979
0
      break;
980
0
    }
981
0
    case TokenType::END_TAG_TOKEN: {
982
0
      switch (token_.atom) {
983
0
        case Atom::NOSCRIPT:
984
0
        case Atom::BR: {
985
0
          break;
986
0
        }
987
0
        default:
988
          // Ignore the token.
989
0
          return true;
990
0
      }
991
0
      break;
992
0
    }
993
0
    case TokenType::TEXT_TOKEN: {
994
0
      if (Strings::IsAllWhitespaceChars(token_.data)) {
995
        // It was all whitespace.
996
0
        return InHeadIM();
997
0
      }
998
0
      break;
999
0
    }
1000
0
    case TokenType::COMMENT_TOKEN: {
1001
0
      return InHeadIM();
1002
0
      break;
1003
0
    }
1004
0
    default:
1005
0
      break;
1006
0
  }
1007
0
  open_elements_stack_.Pop();
1008
0
  CHECK(top()->atom_ == Atom::HEAD)
1009
0
      << "html: the new current node will be a head element.";
1010
1011
0
  insertion_mode_ = std::bind(&Parser::InHeadIM, this);
1012
0
  if (token_.atom == Atom::NOSCRIPT) {
1013
0
    return true;
1014
0
  }
1015
1016
0
  return false;
1017
0
}  // Parser::InHeadNoscriptIM.
1018
1019
// Section 12.2.6.4.6.
1020
27.5k
bool Parser::AfterHeadIM() {
1021
27.5k
  switch (token_.token_type) {
1022
3.44k
    case TokenType::TEXT_TOKEN: {
1023
3.44k
      std::string s = token_.data;
1024
3.44k
      Strings::TrimLeft(&s);
1025
3.44k
      if (s.size() < token_.data.size()) {
1026
        // Add the initial whitespace to the current node.
1027
1.00k
        AddText(token_.data.substr(0, token_.data.size() - s.size()));
1028
1.00k
        if (s.empty()) return true;
1029
20
        token_.data = s;
1030
20
      }
1031
2.45k
      break;
1032
3.44k
    }
1033
20.2k
    case TokenType::START_TAG_TOKEN: {
1034
20.2k
      switch (token_.atom) {
1035
0
        case Atom::HTML:
1036
0
          return InBodyIM();
1037
12.6k
        case Atom::BODY: {
1038
12.6k
          AddElement();
1039
12.6k
          frameset_ok_ = false;
1040
12.6k
          insertion_mode_ = std::bind(&Parser::InBodyIM, this);
1041
12.6k
          return true;
1042
0
        }
1043
0
        case Atom::FRAMESET: {
1044
0
          AddElement();
1045
0
          insertion_mode_ = std::bind(&Parser::InFramesetIM, this);
1046
0
          return true;
1047
0
        }
1048
0
        case Atom::BASE:
1049
0
        case Atom::BASEFONT:
1050
0
        case Atom::BGSOUND:
1051
0
        case Atom::LINK:
1052
194
        case Atom::META:
1053
194
        case Atom::NOFRAMES:
1054
194
        case Atom::SCRIPT:
1055
194
        case Atom::STYLE:
1056
194
        case Atom::TEMPLATE:
1057
194
        case Atom::TITLE: {
1058
194
          open_elements_stack_.Push(head_);
1059
194
          defer(open_elements_stack_.Remove(head_));
1060
194
          return InHeadIM();
1061
194
        }
1062
323
        case Atom::HEAD:
1063
          // Ignore the token.
1064
323
          return true;
1065
7.14k
        default:
1066
7.14k
          break;
1067
20.2k
      }
1068
7.14k
      break;
1069
20.2k
    }
1070
7.14k
    case TokenType::END_TAG_TOKEN: {
1071
429
      switch (token_.atom) {
1072
87
        case Atom::BODY:
1073
191
        case Atom::HTML:
1074
199
        case Atom::BR: {
1075
          // Drop down to creating an implied <body> tag.
1076
199
          break;
1077
191
        }
1078
0
        case Atom::TEMPLATE: {
1079
0
          return InHeadIM();
1080
191
        }
1081
230
        default:
1082
          // Ignore the token.
1083
230
          return true;
1084
429
      }
1085
199
      break;
1086
429
    }
1087
1.15k
    case TokenType::COMMENT_TOKEN: {
1088
1.15k
      Node* node = document_->NewNode(NodeType::COMMENT_NODE);
1089
1.15k
      node->SetManufactured(token_.is_manufactured);
1090
1.15k
      if (record_node_offsets_) {
1091
0
        node->line_col_in_html_src_ = token_.line_col_in_html_src;
1092
0
      }
1093
1.15k
      node->data_ = std::move(token_.data);
1094
1.15k
      AddChild(node);
1095
1.15k
      return true;
1096
429
    }
1097
0
    case TokenType::DOCTYPE_TOKEN:
1098
      // Ignore the token.
1099
0
      return true;
1100
2.26k
    default:
1101
2.26k
      break;
1102
27.5k
  }
1103
1104
12.0k
  ParseImpliedToken(TokenType::START_TAG_TOKEN, Atom::BODY,
1105
12.0k
                    AtomUtil::ToString(Atom::BODY));
1106
12.0k
  frameset_ok_ = true;
1107
12.0k
  return false;
1108
27.5k
}  // Parser::AfterHeadIM.
1109
1110
// Section 12.2.6.4.7.
1111
12.4M
bool Parser::InBodyIM() {  // NOLINT
1112
12.4M
  switch (token_.token_type) {
1113
518k
    case TokenType::TEXT_TOKEN: {
1114
518k
      std::string d = token_.data;
1115
518k
      Node* node = open_elements_stack_.Top();
1116
518k
      switch (node->atom_) {
1117
2.34k
        case Atom::PRE:
1118
2.34k
        case Atom::LISTING: {
1119
2.34k
          if (!node->FirstChild()) {
1120
            // Ignore a new line at the start of a <pre> block.
1121
2.09k
            if (!d.empty() && d.front() == '\r') {
1122
773
              d = d.substr(1);
1123
773
            }
1124
2.09k
            if (!d.empty() && d.front() == '\n') {
1125
410
              d = d.substr(1);
1126
410
            }
1127
2.09k
          }
1128
2.34k
          break;
1129
2.34k
        }
1130
515k
        default:
1131
515k
          break;
1132
518k
      }
1133
1134
518k
      Strings::ReplaceAny(&d, Strings::kNullChar, "");
1135
      // Checks if data empty or all null characters.
1136
518k
      if (d.empty()) {
1137
904
        return true;
1138
904
      }
1139
1140
517k
      ReconstructActiveFormattingElements();
1141
517k
      AddText(d);
1142
517k
      if (frameset_ok_ && !Strings::IsAllWhitespaceChars(d)) {
1143
        // There were non-whitespace chracters inserted.
1144
3.24k
        frameset_ok_ = false;
1145
3.24k
      }
1146
517k
      break;
1147
518k
    }
1148
10.8M
    case TokenType::START_TAG_TOKEN: {
1149
10.8M
      switch (token_.atom) {
1150
1.31k
        case Atom::HTML: {
1151
1.31k
          num_html_tags_++;
1152
1.31k
          if (open_elements_stack_.Contains(Atom::TEMPLATE)) {
1153
0
            return true;
1154
0
          }
1155
1.31k
          CopyAttributes(open_elements_stack_.at(0), token_);
1156
1.31k
          if (!document_->metadata_.has_manufactured_html ||
1157
1.26k
              num_html_tags_ > 1) {
1158
1.26k
            document_->metadata_.duplicate_html_elements = true;
1159
1.26k
            document_->metadata_.duplicate_html_element_location =
1160
1.26k
                token_.line_col_in_html_src;
1161
1.26k
          }
1162
1.31k
          break;
1163
1.31k
        }
1164
697
        case Atom::BASE:
1165
697
        case Atom::BASEFONT:
1166
697
        case Atom::BGSOUND:
1167
1.47k
        case Atom::LINK:
1168
1.66k
        case Atom::META:
1169
1.66k
        case Atom::NOFRAMES:
1170
20.2k
        case Atom::SCRIPT:
1171
20.3k
        case Atom::STYLE:
1172
20.3k
        case Atom::TEMPLATE:
1173
23.9k
        case Atom::TITLE: {
1174
23.9k
          return InHeadIM();
1175
20.3k
        }
1176
5.79k
        case Atom::BODY: {
1177
5.79k
          num_body_tags_++;
1178
5.79k
          if (open_elements_stack_.Contains(Atom::TEMPLATE)) {
1179
0
            return true;
1180
0
          }
1181
5.79k
          if (open_elements_stack_.size() >= 2) {
1182
5.79k
            Node* body = open_elements_stack_.at(1);
1183
5.79k
            if (body->node_type_ == NodeType::ELEMENT_NODE &&
1184
5.79k
                body->atom_ == Atom::BODY) {
1185
5.79k
              frameset_ok_ = false;
1186
5.79k
              CopyAttributes(body, token_);
1187
5.79k
              if (!document_->metadata_.has_manufactured_body ||
1188
5.34k
                  num_body_tags_ > 1) {
1189
5.34k
                document_->metadata_.duplicate_body_elements = true;
1190
5.34k
                document_->metadata_.duplicate_body_element_location =
1191
5.34k
                    token_.line_col_in_html_src;
1192
5.34k
              }
1193
5.79k
            }
1194
5.79k
          }
1195
5.79k
          break;
1196
5.79k
        }
1197
0
        case Atom::FRAMESET: {
1198
0
          if (!frameset_ok_ || open_elements_stack_.size() < 2 ||
1199
0
              open_elements_stack_.at(1)->atom_ != Atom::BODY) {
1200
            // Ignore the token.
1201
0
            return true;
1202
0
          }
1203
0
          auto body = open_elements_stack_.at(1);
1204
0
          if (body->Parent()) {
1205
0
            auto removed_body = body->Parent()->RemoveChild(body);
1206
0
            open_elements_stack_.Remove(removed_body);
1207
0
          }
1208
          // Remove all nodes except one, the last in the stack.
1209
0
          open_elements_stack_.Pop(open_elements_stack_.size() - 1);
1210
0
          AddElement();
1211
0
          insertion_mode_ = std::bind(&Parser::InFramesetIM, this);
1212
0
          return true;
1213
0
        }
1214
0
        case Atom::ADDRESS:
1215
0
        case Atom::ARTICLE:
1216
195
        case Atom::ASIDE:
1217
195
        case Atom::BLOCKQUOTE:
1218
195
        case Atom::CENTER:
1219
195
        case Atom::DETAILS:
1220
195
        case Atom::DIALOG:
1221
403
        case Atom::DIR:
1222
829
        case Atom::DIV:
1223
11.3k
        case Atom::DL:
1224
11.3k
        case Atom::FIELDSET:
1225
11.3k
        case Atom::FIGCAPTION:
1226
11.3k
        case Atom::FIGURE:
1227
11.5k
        case Atom::FOOTER:
1228
11.8k
        case Atom::HEADER:
1229
11.8k
        case Atom::HGROUP:
1230
12.0k
        case Atom::MAIN:
1231
12.2k
        case Atom::MENU:
1232
12.4k
        case Atom::NAV:
1233
12.7k
        case Atom::OL:
1234
59.8k
        case Atom::P:
1235
59.8k
        case Atom::SECTION:
1236
59.8k
        case Atom::SUMMARY:
1237
66.4k
        case Atom::UL: {
1238
66.4k
          PopUntil(Scope::ButtonScope, Atom::P);
1239
66.4k
          AddElement();
1240
66.4k
          break;
1241
59.8k
        }
1242
1.91k
        case Atom::H1:
1243
2.62k
        case Atom::H2:
1244
3.96k
        case Atom::H3:
1245
5.07k
        case Atom::H4:
1246
5.89k
        case Atom::H5:
1247
6.61k
        case Atom::H6: {
1248
6.61k
          PopUntil(Scope::ButtonScope, Atom::P);
1249
6.61k
          Node* top_node = top();
1250
6.61k
          if (top_node) {
1251
6.61k
            switch (top_node->atom_) {
1252
492
              case Atom::H1:
1253
870
              case Atom::H2:
1254
1.31k
              case Atom::H3:
1255
1.72k
              case Atom::H4:
1256
2.06k
              case Atom::H5:
1257
2.45k
              case Atom::H6:
1258
2.45k
                open_elements_stack_.Pop();
1259
2.45k
                break;
1260
4.16k
              default:
1261
4.16k
                break;
1262
6.61k
            }
1263
6.61k
          }
1264
6.61k
          AddElement();
1265
6.61k
          break;
1266
6.61k
        }
1267
2.49k
        case Atom::PRE:
1268
2.49k
        case Atom::LISTING: {
1269
2.49k
          PopUntil(Scope::ButtonScope, Atom::P);
1270
2.49k
          AddElement();
1271
          // The newline, if any, will be dealth with by the TEXT_TOKEN case.
1272
2.49k
          frameset_ok_ = false;
1273
2.49k
          break;
1274
2.49k
        }
1275
3.55k
        case Atom::FORM: {
1276
3.55k
          if (form_ && !open_elements_stack_.Contains(Atom::TEMPLATE)) {
1277
            // Ignore the token.
1278
1.19k
            return true;
1279
1.19k
          }
1280
2.36k
          PopUntil(Scope::ButtonScope, Atom::P);
1281
2.36k
          AddElement();
1282
2.36k
          if (!open_elements_stack_.Contains(Atom::TEMPLATE)) {
1283
2.36k
            form_ = top();
1284
2.36k
          }
1285
2.36k
          break;
1286
3.55k
        }
1287
5.63k
        case Atom::LI: {
1288
5.63k
          frameset_ok_ = false;
1289
247k
          for (int i = open_elements_stack_.size() - 1; i >= 0; --i) {
1290
247k
            Node* node = open_elements_stack_.at(i);
1291
247k
            switch (node->atom_) {
1292
694
              case Atom::LI:
1293
                // Remove all except last in stack.
1294
694
                open_elements_stack_.Pop(open_elements_stack_.size() - i);
1295
694
                break;
1296
0
              case Atom::ADDRESS:
1297
194
              case Atom::DIV:
1298
412
              case Atom::P:
1299
412
                continue;
1300
246k
              default:
1301
246k
                if (!node->IsSpecialElement()) continue;
1302
247k
            }
1303
5.63k
            break;
1304
247k
          }
1305
5.63k
          PopUntil(Scope::ButtonScope, Atom::P);
1306
5.63k
          AddElement();
1307
5.63k
          break;
1308
5.63k
        }
1309
5.32k
        case Atom::DD:
1310
6.42k
        case Atom::DT: {
1311
6.42k
          frameset_ok_ = false;
1312
145k
          for (int i = open_elements_stack_.size() - 1; i >= 0; --i) {
1313
145k
            Node* node = open_elements_stack_.at(i);
1314
145k
            switch (node->atom_) {
1315
500
              case Atom::DD:
1316
1.30k
              case Atom::DT:
1317
                // Remove all except last in stack.
1318
1.30k
                open_elements_stack_.Pop(open_elements_stack_.size() - i);
1319
1.30k
                break;
1320
0
              case Atom::ADDRESS:
1321
196
              case Atom::DIV:
1322
456
              case Atom::P:
1323
456
                continue;
1324
143k
              default:
1325
143k
                if (!node->IsSpecialElement()) continue;
1326
145k
            }
1327
6.42k
            break;
1328
145k
          }
1329
6.42k
          PopUntil(Scope::ButtonScope, Atom::P);
1330
6.42k
          AddElement();
1331
6.42k
          break;
1332
6.42k
        }
1333
0
        case Atom::PLAINTEXT: {
1334
0
          PopUntil(Scope::ButtonScope, Atom::P);
1335
0
          AddElement();
1336
0
          break;
1337
6.42k
        }
1338
202
        case Atom::BUTTON: {
1339
202
          PopUntil(Scope::DefaultScope, Atom::BUTTON);
1340
202
          ReconstructActiveFormattingElements();
1341
202
          AddElement();
1342
202
          frameset_ok_ = false;
1343
202
          break;
1344
6.42k
        }
1345
185k
        case Atom::A: {
1346
731k
          for (int i = active_formatting_elements_stack_.size() - 1; i >= 0;
1347
730k
               --i) {
1348
730k
            Node* node = active_formatting_elements_stack_.at(i);
1349
730k
            if (node->node_type_ == NodeType::SCOPE_MARKER_NODE) break;
1350
730k
            if (node->node_type_ == NodeType::ELEMENT_NODE &&
1351
730k
                node->atom_ == Atom::A) {
1352
184k
              InBodyEndTagFormatting(Atom::A, "a");
1353
184k
              open_elements_stack_.Remove(node);
1354
184k
              active_formatting_elements_stack_.Remove(node);
1355
184k
              break;
1356
184k
            }
1357
730k
          }
1358
185k
          ReconstructActiveFormattingElements();
1359
185k
          AddFormattingElement();
1360
185k
          break;
1361
6.42k
        }
1362
249k
        case Atom::B:
1363
249k
        case Atom::BIG:
1364
249k
        case Atom::CODE:
1365
250k
        case Atom::EM:
1366
250k
        case Atom::FONT:
1367
606k
        case Atom::I:
1368
1.42M
        case Atom::S:
1369
1.42M
        case Atom::SMALL:
1370
1.42M
        case Atom::STRIKE:
1371
1.42M
        case Atom::STRONG:
1372
1.42M
        case Atom::TT:
1373
1.75M
        case Atom::U: {
1374
1.75M
          ReconstructActiveFormattingElements();
1375
1.75M
          AddFormattingElement();
1376
1.75M
          break;
1377
1.42M
        }
1378
398
        case Atom::NOBR: {
1379
398
          ReconstructActiveFormattingElements();
1380
398
          if (ElementInScope(Scope::DefaultScope, Atom::NOBR)) {
1381
195
            InBodyEndTagFormatting(Atom::NOBR, "nobr");
1382
195
            ReconstructActiveFormattingElements();
1383
195
          }
1384
398
          AddFormattingElement();
1385
398
          break;
1386
1.42M
        }
1387
449
        case Atom::APPLET:
1388
449
        case Atom::MARQUEE:
1389
449
        case Atom::OBJECT: {
1390
449
          ReconstructActiveFormattingElements();
1391
449
          AddElement();
1392
449
          active_formatting_elements_stack_.Push(scope_marker_);
1393
449
          frameset_ok_ = false;
1394
449
          break;
1395
449
        }
1396
1.22M
        case Atom::TABLE: {
1397
1.22M
          if (!document_->metadata_.quirks_mode) {
1398
0
            PopUntil(Scope::ButtonScope, Atom::P);
1399
0
          }
1400
1.22M
          AddElement();
1401
1.22M
          frameset_ok_ = false;
1402
1.22M
          insertion_mode_ = std::bind(&Parser::InTableIM, this);
1403
1.22M
          return true;
1404
449
        }
1405
194
        case Atom::AREA:
1406
774
        case Atom::BR:
1407
968
        case Atom::EMBED:
1408
1.35k
        case Atom::IMG:
1409
2.45k
        case Atom::INPUT:
1410
2.45k
        case Atom::KEYGEN:
1411
2.65k
        case Atom::WBR: {
1412
2.65k
          ReconstructActiveFormattingElements();
1413
2.65k
          AddElement();
1414
2.65k
          open_elements_stack_.Pop();
1415
2.65k
          AcknowledgeSelfClosingTag();
1416
2.65k
          if (token_.atom == Atom::INPUT) {
1417
5.57k
            for (auto& attr : token_.attributes) {
1418
5.57k
              if (attr.key == "type" &&
1419
4.63k
                  Strings::EqualFold(attr.value, "hidden")) {
1420
                  // Skip setting frameset_ok_ = false;
1421
0
                  return true;
1422
0
              }
1423
5.57k
            }
1424
1.09k
          }
1425
2.65k
          frameset_ok_ = false;
1426
2.65k
          break;
1427
2.65k
        }
1428
194
        case Atom::PARAM:
1429
194
        case Atom::SOURCE:
1430
388
        case Atom::TRACK: {
1431
388
          AddElement();
1432
388
          open_elements_stack_.Pop();
1433
388
          AcknowledgeSelfClosingTag();
1434
388
          break;
1435
194
        }
1436
198
        case Atom::HR: {
1437
198
          PopUntil(Scope::ButtonScope, Atom::P);
1438
198
          AddElement();
1439
198
          open_elements_stack_.Pop();
1440
198
          AcknowledgeSelfClosingTag();
1441
198
          frameset_ok_ = false;
1442
198
          break;
1443
194
        }
1444
194
        case Atom::IMAGE: {
1445
194
          token_.atom = Atom::IMG;
1446
194
          token_.data = AtomUtil::ToString(Atom::IMG);
1447
194
          return false;
1448
194
        }
1449
15
        case Atom::TEXTAREA: {
1450
15
          AddElement();
1451
15
          SetOriginalIM();
1452
15
          frameset_ok_ = false;
1453
15
          insertion_mode_ = std::bind(&Parser::TextIM, this);
1454
15
          break;
1455
194
        }
1456
39.1k
        case Atom::XMP: {
1457
39.1k
          PopUntil(Scope::ButtonScope, Atom::P);
1458
39.1k
          ReconstructActiveFormattingElements();
1459
39.1k
          frameset_ok_ = false;
1460
39.1k
          ParseGenericRawTextElement();
1461
39.1k
          break;
1462
194
        }
1463
200
        case Atom::IFRAME: {
1464
200
          frameset_ok_ = false;
1465
200
          ParseGenericRawTextElement();
1466
200
          break;
1467
194
        }
1468
194
        case Atom::NOEMBED: {
1469
194
          ParseGenericRawTextElement();
1470
194
          break;
1471
194
        }
1472
268
        case Atom::NOSCRIPT: {
1473
268
          if (scripting_) {
1474
268
            ParseGenericRawTextElement();
1475
268
            return true;
1476
268
          }
1477
0
          ReconstructActiveFormattingElements();
1478
0
          AddElement();
1479
          // Don't let the tokenizer go into raw text mode when scripting is
1480
          // disabled.
1481
0
          tokenizer_->NextIsNotRawText();
1482
0
          break;
1483
268
        }
1484
0
        case Atom::SELECT: {
1485
0
          ReconstructActiveFormattingElements();
1486
0
          AddElement();
1487
0
          frameset_ok_ = false;
1488
0
          insertion_mode_ = std::bind(&Parser::InSelectIM, this);
1489
0
          return true;
1490
0
          break;
1491
268
        }
1492
0
        case Atom::OPTGROUP:
1493
0
        case Atom::OPTION: {
1494
0
          if (top()->atom_ == Atom::OPTION) {
1495
0
            open_elements_stack_.Pop();
1496
0
          }
1497
0
          ReconstructActiveFormattingElements();
1498
0
          AddElement();
1499
0
          break;
1500
0
        }
1501
644
        case Atom::RB:
1502
847
        case Atom::RTC: {
1503
847
          if (ElementInScope(Scope::DefaultScope, Atom::RUBY)) {
1504
444
            GenerateImpliedEndTags();
1505
444
          }
1506
847
          AddElement();
1507
847
          break;
1508
644
        }
1509
999
        case Atom::RP:
1510
6.89k
        case Atom::RT: {
1511
6.89k
          if (ElementInScope(Scope::DefaultScope, Atom::RUBY)) {
1512
1.12k
            GenerateImpliedEndTags({Atom::RTC});
1513
1.12k
          }
1514
6.89k
          AddElement();
1515
6.89k
          break;
1516
999
        }
1517
878
        case Atom::MATH:
1518
2.85k
        case Atom::SVG: {
1519
2.85k
          ReconstructActiveFormattingElements();
1520
2.85k
          if (token_.atom == Atom::MATH) {
1521
878
            AdjustMathMLAttributeNames(&token_.attributes);
1522
1.97k
          } else {
1523
1.97k
            AdjustSVGAttributeNames(&token_.attributes);
1524
1.97k
          }
1525
2.85k
          AdjustForeignAttributes(&token_.attributes);
1526
2.85k
          AddElement();
1527
2.85k
          top()->name_space_ = AtomUtil::ToString(token_.atom);
1528
2.85k
          if (has_self_closing_token_) {
1529
398
            open_elements_stack_.Pop();
1530
398
            AcknowledgeSelfClosingTag();
1531
398
          }
1532
2.85k
          return true;
1533
0
          break;
1534
878
        }
1535
0
        case Atom::CAPTION:
1536
376
        case Atom::COL:
1537
376
        case Atom::COLGROUP:
1538
570
        case Atom::FRAME:
1539
770
        case Atom::HEAD:
1540
964
        case Atom::TBODY:
1541
1.25k
        case Atom::TD:
1542
1.52k
        case Atom::TFOOT:
1543
1.94k
        case Atom::TH:
1544
2.13k
        case Atom::THEAD:
1545
2.35k
        case Atom::TR: {
1546
          // Ignore the token.
1547
2.35k
          break;
1548
2.13k
        }
1549
7.47M
        default:
1550
7.47M
          ReconstructActiveFormattingElements();
1551
7.47M
          AddElement();
1552
10.8M
      }
1553
9.56M
      break;
1554
10.8M
    }
1555
9.56M
    case TokenType::END_TAG_TOKEN: {
1556
78.9k
      switch (token_.atom) {
1557
5.77k
        case Atom::BODY:
1558
5.77k
          if (ElementInScope(Scope::DefaultScope, Atom::BODY)) {
1559
5.57k
            insertion_mode_ = std::bind(&Parser::AfterBodyIM, this);
1560
5.57k
          }
1561
5.77k
          break;
1562
2.08k
        case Atom::HTML: {
1563
2.08k
          if (ElementInScope(Scope::DefaultScope, Atom::BODY)) {
1564
2.08k
            ParseImpliedToken(TokenType::END_TAG_TOKEN, Atom::BODY,
1565
2.08k
                              AtomUtil::ToString(Atom::BODY));
1566
2.08k
            return false;
1567
2.08k
          }
1568
0
          return true;
1569
0
          break;
1570
2.08k
        }
1571
0
        case Atom::ADDRESS:
1572
0
        case Atom::ARTICLE:
1573
194
        case Atom::ASIDE:
1574
194
        case Atom::BLOCKQUOTE:
1575
401
        case Atom::BUTTON:
1576
401
        case Atom::CENTER:
1577
401
        case Atom::DETAILS:
1578
401
        case Atom::DIALOG:
1579
1.37k
        case Atom::DIR:
1580
1.57k
        case Atom::DIV:
1581
1.77k
        case Atom::DL:
1582
1.77k
        case Atom::FIELDSET:
1583
1.77k
        case Atom::FIGCAPTION:
1584
1.77k
        case Atom::FIGURE:
1585
1.96k
        case Atom::FOOTER:
1586
2.16k
        case Atom::HEADER:
1587
2.16k
        case Atom::HGROUP:
1588
2.16k
        case Atom::LISTING:
1589
2.35k
        case Atom::MAIN:
1590
2.55k
        case Atom::MENU:
1591
2.74k
        case Atom::NAV:
1592
2.94k
        case Atom::OL:
1593
3.13k
        case Atom::PRE:
1594
3.13k
        case Atom::SECTION:
1595
3.13k
        case Atom::SUMMARY:
1596
3.33k
        case Atom::UL: {
1597
3.33k
          PopUntil(Scope::DefaultScope, token_.atom);
1598
3.33k
          break;
1599
3.13k
        }
1600
3.58k
        case Atom::FORM: {
1601
3.58k
          if (open_elements_stack_.Contains(Atom::TEMPLATE)) {
1602
0
            int i = IndexOfElementInScope(Scope::DefaultScope, {Atom::FORM});
1603
0
            if (i == -1) {
1604
              // Ignore the token.
1605
0
              return true;
1606
0
            }
1607
0
            GenerateImpliedEndTags();
1608
0
            if (open_elements_stack_.at(i)->atom_ != Atom::FORM) {
1609
              // Ignore the token.
1610
0
              return true;
1611
0
            }
1612
0
            PopUntil(Scope::DefaultScope, Atom::FORM);
1613
3.58k
          } else {
1614
3.58k
            Node* node = form_;
1615
3.58k
            form_ = nullptr;
1616
3.58k
            int i = IndexOfElementInScope(Scope::DefaultScope, {Atom::FORM});
1617
3.58k
            if (!node || i == -1 || open_elements_stack_.at(i) != node) {
1618
              // Ignore the token.
1619
1.27k
              return true;
1620
1.27k
            }
1621
2.31k
            GenerateImpliedEndTags();
1622
2.31k
            open_elements_stack_.Remove(node);
1623
2.31k
          }
1624
2.31k
          break;
1625
3.58k
        }
1626
2.31k
        case Atom::P: {
1627
1.66k
          if (!ElementInScope(Scope::ButtonScope, Atom::P)) {
1628
1.44k
            ParseImpliedToken(TokenType::START_TAG_TOKEN, Atom::P,
1629
1.44k
                              AtomUtil::ToString(Atom::P));
1630
1.44k
          }
1631
1.66k
          PopUntil(Scope::ButtonScope, Atom::P);
1632
1.66k
          break;
1633
3.58k
        }
1634
608
        case Atom::LI: {
1635
608
          PopUntil(Scope::ListItemScope, Atom::LI);
1636
608
          break;
1637
3.58k
        }
1638
213
        case Atom::DD:
1639
409
        case Atom::DT: {
1640
409
          PopUntil(Scope::DefaultScope, token_.atom);
1641
409
          break;
1642
213
        }
1643
214
        case Atom::H1:
1644
439
        case Atom::H2:
1645
650
        case Atom::H3:
1646
935
        case Atom::H4:
1647
1.18k
        case Atom::H5:
1648
1.56k
        case Atom::H6: {
1649
1.56k
          PopUntil(Scope::DefaultScope, Atom::H1, Atom::H2, Atom::H3, Atom::H4,
1650
1.56k
                   Atom::H5, Atom::H6);
1651
1.56k
          break;
1652
1.18k
        }
1653
886
        case Atom::A:
1654
6.51k
        case Atom::B:
1655
6.71k
        case Atom::BIG:
1656
6.90k
        case Atom::CODE:
1657
7.10k
        case Atom::EM:
1658
7.30k
        case Atom::FONT:
1659
8.43k
        case Atom::I:
1660
8.81k
        case Atom::NOBR:
1661
16.7k
        case Atom::S:
1662
16.9k
        case Atom::SMALL:
1663
17.1k
        case Atom::STRIKE:
1664
17.3k
        case Atom::STRONG:
1665
17.5k
        case Atom::TT:
1666
18.5k
        case Atom::U: {
1667
18.5k
          InBodyEndTagFormatting(token_.atom,
1668
18.5k
                                 token_.atom != Atom::UNKNOWN
1669
18.5k
                                     ? AtomUtil::ToString(token_.atom)
1670
18.5k
                                     : token_.data);
1671
18.5k
          break;
1672
17.5k
        }
1673
536
        case Atom::APPLET:
1674
536
        case Atom::MARQUEE:
1675
536
        case Atom::OBJECT: {
1676
536
          if (PopUntil(Scope::DefaultScope, token_.atom)) {
1677
222
            ClearActiveFormattingElements();
1678
222
          }
1679
536
          break;
1680
536
        }
1681
199
        case Atom::BR: {
1682
199
          token_.token_type = TokenType::START_TAG_TOKEN;
1683
199
          return false;
1684
0
          break;
1685
536
        }
1686
0
        case Atom::TEMPLATE: {
1687
0
          return InHeadIM();
1688
0
          break;
1689
536
        }
1690
40.6k
        default:
1691
40.6k
          InBodyEndTagOther(token_.atom, token_.data);
1692
78.9k
      }
1693
75.4k
      break;
1694
78.9k
    }
1695
984k
    case TokenType::COMMENT_TOKEN: {
1696
984k
      Node* node = document_->NewNode(NodeType::COMMENT_NODE);
1697
984k
      node->SetManufactured(token_.is_manufactured);
1698
984k
      if (record_node_offsets_) {
1699
0
        node->line_col_in_html_src_ = token_.line_col_in_html_src;
1700
0
      }
1701
984k
      node->data_ = token_.data;
1702
984k
      AddChild(node);
1703
984k
      break;
1704
78.9k
    }
1705
12.5k
    case TokenType::ERROR_TOKEN: {
1706
12.5k
      if (template_stack_.size() > 0) {
1707
0
        insertion_mode_ = std::bind(&Parser::InTemplateIM, this);
1708
0
        return false;
1709
12.5k
      } else {
1710
21.9k
        for (Node* n : open_elements_stack_) {
1711
21.9k
          switch (n->atom_) {
1712
167
            case Atom::DD:
1713
341
            case Atom::LI:
1714
341
            case Atom::OPTGROUP:
1715
341
            case Atom::OPTION:
1716
656
            case Atom::P:
1717
886
            case Atom::RB:
1718
1.30k
            case Atom::RP:
1719
1.54k
            case Atom::RT:
1720
1.74k
            case Atom::RTC:
1721
2.36k
            case Atom::TBODY:
1722
2.49k
            case Atom::TD:
1723
2.57k
            case Atom::TFOOT:
1724
2.77k
            case Atom::TH:
1725
2.84k
            case Atom::THEAD:
1726
3.37k
            case Atom::TR:
1727
9.24k
            case Atom::BODY:
1728
15.3k
            case Atom::HTML:
1729
              // Ignore.
1730
15.3k
              break;
1731
6.65k
            default:
1732
6.65k
              return true;
1733
21.9k
          }
1734
21.9k
        }
1735
12.5k
      }
1736
5.86k
      break;
1737
12.5k
    }
1738
5.86k
    default:
1739
194
      break;
1740
12.4M
  }
1741
1742
11.1M
  return true;
1743
12.4M
}  // NOLINT(readability/fn_size)
1744
// Parser::InBodyIM end.
1745
1746
202k
void Parser::InBodyEndTagFormatting(Atom tag_atom, std::string_view tag_name) {
1747
  // This is the "adoption agency" algorithm, described at
1748
  // https://html.spec.whatwg.org/multipage/syntax.html#adoptionAgency
1749
1750
  // TODO: this is a fairly literal line-by-line translation of that algorithm.
1751
  // Once the code successfully parses the comprehensive test suite, we should
1752
  // refactor this code to be more idiomatic.
1753
1754
  // Steps 1-2
1755
202k
  if (auto current = open_elements_stack_.Top();
1756
202k
      current->data_ == tag_name &&
1757
0
      active_formatting_elements_stack_.Index(current) == -1) {
1758
0
    open_elements_stack_.Pop();
1759
0
    return;
1760
0
  }
1761
1762
  // Steps 3-5. The outer loop.
1763
288k
  for (int i = 0; i < 8; ++i) {
1764
    // Step 6. Find the formatting element.
1765
284k
    Node* formatting_element = nullptr;
1766
16.1M
    for (int j = active_formatting_elements_stack_.size() - 1; j >= 0; --j) {
1767
16.1M
      if (active_formatting_elements_stack_.at(j)->node_type_ ==
1768
16.1M
          NodeType::SCOPE_MARKER_NODE) {
1769
215
        break;
1770
215
      }
1771
16.1M
      if (active_formatting_elements_stack_.at(j)->atom_ == tag_atom) {
1772
280k
        formatting_element = active_formatting_elements_stack_.at(j);
1773
280k
        break;
1774
280k
      }
1775
16.1M
    }
1776
1777
284k
    if (!formatting_element) {
1778
4.28k
      InBodyEndTagOther(tag_atom, tag_name);
1779
4.28k
      return;
1780
4.28k
    }
1781
1782
    // Step 7. Ignore the tag if formatting element is not in the stack of open
1783
    // elements.
1784
280k
    int fe_index = open_elements_stack_.Index(formatting_element);
1785
280k
    if (fe_index == -1) {
1786
2.27k
      active_formatting_elements_stack_.Remove(formatting_element);
1787
2.27k
      return;
1788
2.27k
    }
1789
1790
    // Step 8. Ignore the tag if formatting element is not in the scope.
1791
278k
    if (!ElementInScope(Scope::DefaultScope, tag_atom)) {
1792
      // Ignore the tag.
1793
208
      return;
1794
208
    }
1795
1796
    // Step 9. This step is omitted because it's just a parse error but no
1797
    // need to return.
1798
1799
    // Steps 10-11. Find the furthest block.
1800
278k
    Node* furthest_block = nullptr;
1801
2.87M
    for (int k = fe_index; k < open_elements_stack_.size(); ++k) {
1802
2.67M
      if (open_elements_stack_.at(k)->IsSpecialElement()) {
1803
86.0k
        furthest_block = open_elements_stack_.at(k);
1804
86.0k
        break;
1805
86.0k
      }
1806
2.67M
    }
1807
1808
278k
    if (!furthest_block) {
1809
192k
      Node* e = open_elements_stack_.Pop();
1810
1.33M
      while (e != formatting_element) {
1811
1.14M
        e = open_elements_stack_.Pop();
1812
1.14M
      }
1813
192k
      active_formatting_elements_stack_.Remove(e);
1814
192k
      return;
1815
192k
    }
1816
1817
    // Steps 12-13. Find the common ancestor and bookmark node.
1818
86.0k
    Node* common_ancestor = open_elements_stack_.at(fe_index - 1);
1819
86.0k
    auto bookmark = active_formatting_elements_stack_.Index(formatting_element);
1820
1821
    // Step 14. The inner loop. Find the last_node to reparent.
1822
86.0k
    Node* last_node = furthest_block;
1823
86.0k
    Node* node = furthest_block;
1824
86.0k
    int x = open_elements_stack_.Index(node);
1825
    // Step 14.1.
1826
86.0k
    int j = 0;
1827
1.25M
    while (true) {
1828
      // Step 14.2.
1829
1.25M
      j++;
1830
      // Step 14.3.
1831
1.25M
      x--;
1832
1.25M
      node = open_elements_stack_.at(x);
1833
      // Step 14.4. Go to the next step if node is formatting element.
1834
1.25M
      if (node == formatting_element) break;
1835
1836
      // Step 14.5. Remove node from the list of active formatting elements if
1837
      // inner loop counter is greater than three and node is in the list of
1838
      // active formatting elements.
1839
1.16M
      if (int ni = active_formatting_elements_stack_.Index(node);
1840
1.16M
          j > 3 && ni > -1) {
1841
430k
        active_formatting_elements_stack_.Remove(node);
1842
        // If any element of the list of active formatting elements is removed,
1843
        // we need to take care whether bookmark should be decremented or not.
1844
        // This is because the value of bookmark may exceed the size of the
1845
        // list by removing elements from the list.
1846
430k
        if (ni <= bookmark) {
1847
424k
          bookmark--;
1848
424k
        }
1849
430k
        continue;
1850
430k
      }
1851
1852
      // Step 14.6. Continue the next inner loop if node is not in the list of
1853
      // active formatting elements.
1854
739k
      if (active_formatting_elements_stack_.Index(node) == -1) {
1855
679k
        open_elements_stack_.Remove(node);
1856
679k
        continue;
1857
679k
      }
1858
1859
      // Step 14.7.
1860
59.8k
      Node* clone = document_->CloneNode(node);
1861
59.8k
      active_formatting_elements_stack_.Replace(
1862
59.8k
          active_formatting_elements_stack_.Index(node), clone);
1863
59.8k
      open_elements_stack_.Replace(open_elements_stack_.Index(node), clone);
1864
59.8k
      node = clone;
1865
1866
      // Step 14.8.
1867
59.8k
      if (last_node == furthest_block) {
1868
24.4k
        bookmark = active_formatting_elements_stack_.Index(node) + 1;
1869
24.4k
      }
1870
      // Step 14.9.
1871
59.8k
      if (last_node->Parent()) {
1872
24.4k
        last_node = last_node->Parent()->RemoveChild(last_node);
1873
24.4k
      }
1874
59.8k
      node->AppendChild(last_node);
1875
1876
      // Step 14.10.
1877
59.8k
      last_node = node;
1878
59.8k
    }
1879
1880
    // Step 15. Reparent lastNode to the common ancestor,
1881
    // or for misnested table nodes, to the foster parent.
1882
86.0k
    if (last_node->Parent()) {
1883
61.6k
      last_node = last_node->Parent()->RemoveChild(last_node);
1884
61.6k
    }
1885
1886
86.0k
    switch (common_ancestor->atom_) {
1887
364
      case Atom::TABLE:
1888
678
      case Atom::TBODY:
1889
26.5k
      case Atom::TFOOT:
1890
26.6k
      case Atom::THEAD:
1891
27.9k
      case Atom::TR:
1892
27.9k
        FosterParent(last_node);
1893
27.9k
        break;
1894
58.1k
      default:
1895
58.1k
        common_ancestor->AppendChild(last_node);
1896
86.0k
    }
1897
1898
    // Steps 16-18. Reparent nodes from the furthest block's children
1899
    // to a clone of the formatting element.
1900
86.0k
    Node* clone = document_->CloneNode(formatting_element);
1901
86.0k
    furthest_block->ReparentChildrenTo(clone);
1902
86.0k
    furthest_block->AppendChild(clone);
1903
1904
    // Step 19. Fix up the list of active formatting elements.
1905
86.0k
    int old_loc = active_formatting_elements_stack_.Index(formatting_element);
1906
86.0k
    if (old_loc != -1 && old_loc < bookmark) {
1907
      // Move the bookmark with the rest of the list.
1908
24.4k
      bookmark--;
1909
24.4k
    }
1910
1911
86.0k
    active_formatting_elements_stack_.Remove(formatting_element);
1912
86.0k
    active_formatting_elements_stack_.Insert(bookmark, clone);
1913
1914
    // Step 20. Fix up the stack of open elements.
1915
86.0k
    open_elements_stack_.Remove(formatting_element);
1916
86.0k
    open_elements_stack_.Insert(open_elements_stack_.Index(furthest_block) + 1,
1917
86.0k
                                clone);
1918
86.0k
  }
1919
202k
}  // Parser::InBodyEndTagFormatting.
1920
1921
44.9k
void Parser::InBodyEndTagOther(Atom tag_atom, std::string_view tag_name) {
1922
25.1M
  for (int i = open_elements_stack_.size() - 1; i >= 0; --i) {
1923
    // Two element nodes have the same tag if they have the same Data (a
1924
    // string-typed field). As an optimization, for common HTML tags, each
1925
    // Data string is assigned a unique, non-zero Atom (a uint32-typed
1926
    // field), since integer comparison is faster than string comparison.
1927
    // Uncommon (custom) tags get a zero Atom.
1928
    //
1929
    // The if condition here is equivalent to (node->data_ == tag_name).
1930
25.1M
    if (open_elements_stack_.at(i)->atom_ == tag_atom &&
1931
17.6M
        ((tag_atom != Atom::UNKNOWN) ||
1932
17.6M
         (open_elements_stack_.at(i)->data_ == tag_name))) {
1933
887
      open_elements_stack_.Pop(open_elements_stack_.size() - i);
1934
887
      break;
1935
887
    }
1936
1937
25.1M
    if (open_elements_stack_.at(i)->IsSpecialElement()) break;
1938
25.1M
  }
1939
44.9k
}  // Parser::InBodyEndTagOther.
1940
1941
// Section 12.2.6.4.8.
1942
118k
bool Parser::TextIM() {
1943
118k
  switch (token_.token_type) {
1944
1.19k
    case TokenType::ERROR_TOKEN:
1945
1.19k
      open_elements_stack_.Pop();
1946
1.19k
      break;
1947
54.6k
    case TokenType::TEXT_TOKEN: {
1948
54.6k
      std::string_view data_view(token_.data);
1949
54.6k
      Node* node = open_elements_stack_.Top();
1950
54.6k
      if ((node->atom_ == Atom::TEXTAREA) && !node->FirstChild()) {
1951
        // Ignore a newline at the start of a <textarea> block.
1952
14
        if (!data_view.empty() && data_view.front() == '\r') {
1953
0
          data_view.remove_prefix(1);
1954
0
        }
1955
14
        if (!data_view.empty() && data_view.front() == '\n') {
1956
1
          data_view.remove_prefix(1);
1957
1
        }
1958
14
      }
1959
54.6k
      if (data_view.empty()) return true;
1960
54.6k
      AddText(data_view.data());
1961
54.6k
      return true;
1962
54.6k
    }
1963
62.6k
    case TokenType::END_TAG_TOKEN:
1964
62.6k
      open_elements_stack_.Pop();
1965
62.6k
      break;
1966
0
    default:
1967
0
      break;
1968
118k
  }
1969
63.8k
  insertion_mode_ = original_insertion_mode_;
1970
63.8k
  original_insertion_mode_ = nullptr;
1971
63.8k
  return token_.token_type == TokenType::END_TAG_TOKEN;
1972
118k
}  // Parser::TextIM.
1973
1974
// Section 12.2.6.4.9.
1975
5.46M
bool Parser::InTableIM() {
1976
5.46M
  switch (token_.token_type) {
1977
98.1k
    case TokenType::TEXT_TOKEN: {
1978
98.1k
      Strings::ReplaceAny(&token_.data, Strings::kNullChar, "");
1979
98.1k
      switch (open_elements_stack_.Top()->atom_) {
1980
8.65k
        case Atom::TABLE:
1981
8.92k
        case Atom::TBODY:
1982
43.2k
        case Atom::TFOOT:
1983
43.6k
        case Atom::THEAD:
1984
44.8k
        case Atom::TR: {
1985
          // All whitespace including \x00.
1986
44.8k
          if (Strings::IsAllWhitespaceChars(token_.data,
1987
44.8k
                                            Strings::kWhitespaceOrNull)) {
1988
603
            AddText(token_.data);
1989
603
            return true;
1990
603
          }
1991
44.2k
          break;
1992
44.8k
        }
1993
53.3k
        default:
1994
53.3k
          break;
1995
98.1k
      }
1996
97.5k
      break;
1997
98.1k
    }
1998
5.35M
    case TokenType::START_TAG_TOKEN: {
1999
5.35M
      switch (token_.atom) {
2000
0
        case Atom::CAPTION: {
2001
0
          ClearStackToContext(Scope::TableScope);
2002
0
          active_formatting_elements_stack_.Push(scope_marker_);
2003
0
          AddElement();
2004
0
          insertion_mode_ = std::bind(&Parser::InCaptionIM, this);
2005
0
          return true;
2006
0
        }
2007
3.72k
        case Atom::COLGROUP: {
2008
3.72k
          ClearStackToContext(Scope::TableScope);
2009
3.72k
          AddElement();
2010
3.72k
          insertion_mode_ = std::bind(&Parser::InColumnGroupIM, this);
2011
3.72k
          return true;
2012
0
        }
2013
3.72k
        case Atom::COL: {
2014
3.72k
          ParseImpliedToken(TokenType::START_TAG_TOKEN, Atom::COLGROUP,
2015
3.72k
                            AtomUtil::ToString(Atom::COLGROUP));
2016
3.72k
          return false;
2017
0
        }
2018
1.18M
        case Atom::TBODY:
2019
1.22M
        case Atom::TFOOT:
2020
1.22M
        case Atom::THEAD: {
2021
1.22M
          ClearStackToContext(Scope::TableScope);
2022
1.22M
          AddElement();
2023
1.22M
          insertion_mode_ = std::bind(&Parser::InTableBodyIM, this);
2024
1.22M
          return true;
2025
1.22M
        }
2026
767k
        case Atom::TD:
2027
1.18M
        case Atom::TH:
2028
1.18M
        case Atom::TR: {
2029
1.18M
          ParseImpliedToken(TokenType::START_TAG_TOKEN, Atom::TBODY,
2030
1.18M
                            AtomUtil::ToString(Atom::TBODY));
2031
1.18M
          return false;
2032
1.18M
        }
2033
40.4k
        case Atom::TABLE: {
2034
40.4k
          if (PopUntil(Scope::TableScope, Atom::TABLE)) {
2035
40.4k
            ResetInsertionMode();
2036
40.4k
            return false;
2037
40.4k
          }
2038
          // Ignore the token.
2039
0
          return true;
2040
40.4k
        }
2041
1
        case Atom::STYLE:
2042
1
        case Atom::SCRIPT:
2043
1
        case Atom::TEMPLATE: {
2044
1
          return InHeadIM();
2045
1
        }
2046
0
        case Atom::INPUT: {
2047
0
          for (auto& attr : token_.attributes) {
2048
0
            if (attr.key == "type" &&
2049
0
                Strings::EqualFold(attr.value, "hidden")) {
2050
0
              AddElement();
2051
0
              open_elements_stack_.Pop();
2052
0
              return true;
2053
0
            }
2054
0
          }
2055
0
          break;
2056
          // Otherwise drop down to the default action.
2057
0
        }
2058
734
        case Atom::FORM: {
2059
734
          if (open_elements_stack_.Contains(Atom::TEMPLATE) || form_) {
2060
            // Ignore the token.
2061
198
            return true;
2062
198
          }
2063
536
          AddElement();
2064
536
          form_ = open_elements_stack_.Pop();
2065
536
          break;
2066
734
        }
2067
0
        case Atom::SELECT: {
2068
0
          ReconstructActiveFormattingElements();
2069
0
          switch (top()->atom_) {
2070
0
            case Atom::TABLE:
2071
0
            case Atom::TBODY:
2072
0
            case Atom::TFOOT:
2073
0
            case Atom::THEAD:
2074
0
            case Atom::TR:
2075
0
              foster_parenting_ = true;
2076
0
              break;
2077
0
            default:
2078
              // Ignore remaining tags.
2079
0
              break;
2080
0
          }
2081
0
          AddElement();
2082
0
          foster_parenting_ = false;
2083
0
          frameset_ok_ = false;
2084
0
          insertion_mode_ = std::bind(&Parser::InSelectInTableIM, this);
2085
0
          return true;
2086
0
        }
2087
2.89M
        default:
2088
          // Ignore remaining tags.
2089
2.89M
          break;
2090
5.35M
      }
2091
2.89M
      break;
2092
5.35M
    }
2093
2.89M
    case TokenType::END_TAG_TOKEN: {
2094
15.9k
      switch (token_.atom) {
2095
8.96k
        case Atom::TABLE:
2096
8.96k
          if (PopUntil(Scope::TableScope, Atom::TABLE)) {
2097
8.96k
            ResetInsertionMode();
2098
8.96k
            return true;
2099
8.96k
          }
2100
          // Ignore the token.
2101
0
          return true;
2102
194
        case Atom::BODY:
2103
194
        case Atom::CAPTION:
2104
388
        case Atom::COL:
2105
388
        case Atom::COLGROUP:
2106
582
        case Atom::HTML:
2107
776
        case Atom::TBODY:
2108
970
        case Atom::TD:
2109
1.97k
        case Atom::TFOOT:
2110
2.16k
        case Atom::TH:
2111
2.35k
        case Atom::THEAD:
2112
2.58k
        case Atom::TR:
2113
          // Ignore the token.
2114
2.58k
          return true;
2115
0
        case Atom::TEMPLATE:
2116
0
          return InHeadIM();
2117
4.43k
        default:
2118
          // Ignore.
2119
4.43k
          break;
2120
15.9k
      }
2121
4.43k
      break;
2122
15.9k
    }
2123
4.43k
    case TokenType::COMMENT_TOKEN: {
2124
2.03k
      Node* node = document_->NewNode(NodeType::COMMENT_NODE);
2125
2.03k
      node->SetManufactured(token_.is_manufactured);
2126
2.03k
      if (record_node_offsets_) {
2127
0
        node->line_col_in_html_src_ = token_.line_col_in_html_src;
2128
0
      }
2129
2.03k
      node->data_ = token_.data;
2130
2.03k
      AddChild(node);
2131
2.03k
      return true;
2132
15.9k
    }
2133
0
    case TokenType::DOCTYPE_TOKEN: {
2134
      // Ignore the token.
2135
0
      return true;
2136
15.9k
    }
2137
1.44k
    case TokenType::ERROR_TOKEN: {
2138
1.44k
      return InBodyIM();
2139
15.9k
    }
2140
0
    default:
2141
0
      break;
2142
5.46M
  }
2143
2144
2.99M
  foster_parenting_ = true;
2145
2.99M
  defer(foster_parenting_ = false;);
2146
2.99M
  return InBodyIM();
2147
5.46M
}  // Parser::InTableIM.
2148
2149
// Section 12.2.6.4.11.
2150
0
bool Parser::InCaptionIM() {
2151
0
  switch (token_.token_type) {
2152
0
    case TokenType::START_TAG_TOKEN: {
2153
0
      switch (token_.atom) {
2154
0
        case Atom::CAPTION:
2155
0
        case Atom::COL:
2156
0
        case Atom::COLGROUP:
2157
0
        case Atom::TBODY:
2158
0
        case Atom::TD:
2159
0
        case Atom::TFOOT:
2160
0
        case Atom::THEAD:
2161
0
        case Atom::TR: {
2162
0
          if (PopUntil(Scope::TableScope, Atom::CAPTION)) {
2163
0
            ClearActiveFormattingElements();
2164
0
            insertion_mode_ = std::bind(&Parser::InTableIM, this);
2165
0
            return false;
2166
0
          }
2167
          // Ignore the token.
2168
0
          return true;
2169
0
        }
2170
0
        case Atom::SELECT: {
2171
0
          ReconstructActiveFormattingElements();
2172
0
          AddElement();
2173
0
          frameset_ok_ = false;
2174
0
          insertion_mode_ = std::bind(&Parser::InSelectInTableIM, this);
2175
0
          return true;
2176
0
        }
2177
0
        default:
2178
0
          break;
2179
0
      }
2180
0
      break;
2181
0
    }
2182
0
    case TokenType::END_TAG_TOKEN: {
2183
0
      switch (token_.atom) {
2184
0
        case Atom::CAPTION: {
2185
0
          if (PopUntil(Scope::TableScope, Atom::CAPTION)) {
2186
0
            ClearActiveFormattingElements();
2187
0
            insertion_mode_ = std::bind(&Parser::InTableIM, this);
2188
0
          }
2189
0
          return true;
2190
0
        }
2191
0
        case Atom::TABLE: {
2192
0
          if (PopUntil(Scope::TableScope, Atom::CAPTION)) {
2193
0
            ClearActiveFormattingElements();
2194
0
            insertion_mode_ = std::bind(&Parser::InTableIM, this);
2195
0
            return false;
2196
0
          }
2197
          // Ignore the token.
2198
0
          return true;
2199
0
        }
2200
0
        case Atom::BODY:
2201
0
        case Atom::COL:
2202
0
        case Atom::COLGROUP:
2203
0
        case Atom::HTML:
2204
0
        case Atom::TBODY:
2205
0
        case Atom::TD:
2206
0
        case Atom::TFOOT:
2207
0
        case Atom::TH:
2208
0
        case Atom::THEAD:
2209
0
        case Atom::TR: {
2210
          // Ignore the token.
2211
0
          return true;
2212
0
        }
2213
0
        default:
2214
0
          break;
2215
0
      }
2216
0
      break;
2217
0
    }
2218
0
    default:
2219
0
      break;
2220
0
  }
2221
2222
0
  return InBodyIM();
2223
0
}  // Parser::InCaptionIM.
2224
2225
// Section 12.2.6.4.12.
2226
10.9k
bool Parser::InColumnGroupIM() {
2227
10.9k
  switch (token_.token_type) {
2228
3.21k
    case TokenType::TEXT_TOKEN: {
2229
3.21k
      std::string s = token_.data;
2230
3.21k
      Strings::TrimLeft(&s);
2231
3.21k
      if (s.size() < token_.data.size()) {
2232
        // Add the initial whitespace to the current node.
2233
2.28k
        AddText(token_.data.substr(0, token_.data.size() - s.size()));
2234
2.28k
        if (s.empty()) return true;
2235
944
        token_.data = s;
2236
944
      }
2237
1.87k
      break;
2238
3.21k
    }
2239
1.87k
    case TokenType::COMMENT_TOKEN: {
2240
1.50k
      Node* node = document_->NewNode(NodeType::COMMENT_NODE);
2241
1.50k
      node->SetManufactured(token_.is_manufactured);
2242
1.50k
      if (record_node_offsets_) {
2243
0
        node->line_col_in_html_src_ = token_.line_col_in_html_src;
2244
0
      }
2245
1.50k
      node->data_ = token_.data;
2246
1.50k
      AddChild(node);
2247
1.50k
      return true;
2248
3.21k
    }
2249
0
    case TokenType::DOCTYPE_TOKEN: {
2250
      // Ignore the token.
2251
0
      return true;
2252
3.21k
    }
2253
5.75k
    case TokenType::START_TAG_TOKEN: {
2254
5.75k
      switch (token_.atom) {
2255
194
        case Atom::HTML: {
2256
194
          return InBodyIM();
2257
0
        }
2258
4.03k
        case Atom::COL: {
2259
4.03k
          AddElement();
2260
4.03k
          open_elements_stack_.Pop();
2261
4.03k
          AcknowledgeSelfClosingTag();
2262
4.03k
          return true;
2263
0
        }
2264
0
        case Atom::TEMPLATE: {
2265
0
          return InHeadIM();
2266
0
        }
2267
1.52k
        default:
2268
1.52k
          break;
2269
5.75k
      }
2270
1.52k
      break;
2271
5.75k
    }
2272
1.52k
    case TokenType::END_TAG_TOKEN: {
2273
412
      switch (token_.atom) {
2274
0
        case Atom::COLGROUP:
2275
0
          if (open_elements_stack_.Top()->atom_ == Atom::COLGROUP) {
2276
0
            open_elements_stack_.Pop();
2277
0
            insertion_mode_ = std::bind(&Parser::InTableIM, this);
2278
0
          }
2279
0
          return true;
2280
194
        case Atom::COL:
2281
          // Ignore the token.
2282
194
          return true;
2283
0
        case Atom::TEMPLATE:
2284
0
          return InHeadIM();
2285
218
        default:
2286
218
          break;
2287
412
      }
2288
218
      break;
2289
412
    }
2290
218
    case TokenType::ERROR_TOKEN: {
2291
112
      return InBodyIM();
2292
412
    }
2293
0
    default:
2294
0
      break;
2295
10.9k
  }
2296
2297
3.61k
  if (open_elements_stack_.Top()->atom_ != Atom::COLGROUP) {
2298
0
    return true;
2299
0
  }
2300
3.61k
  open_elements_stack_.Pop();
2301
3.61k
  insertion_mode_ = std::bind(&Parser::InTableIM, this);
2302
3.61k
  return false;
2303
3.61k
}  // Parser::InColumnGroupIM.
2304
2305
// Section 12.2.6.4.13.
2306
3.35M
bool Parser::InTableBodyIM() {
2307
3.35M
  switch (token_.token_type) {
2308
3.29M
    case TokenType::START_TAG_TOKEN: {
2309
3.29M
      switch (token_.atom) {
2310
1.18M
        case Atom::TR: {
2311
1.18M
          ClearStackToContext(Scope::TableBodyScope);
2312
1.18M
          AddElement();
2313
1.18M
          insertion_mode_ = std::bind(&Parser::InRowIM, this);
2314
1.18M
          return true;
2315
0
        }
2316
768k
        case Atom::TD:
2317
1.18M
        case Atom::TH: {
2318
1.18M
          ParseImpliedToken(TokenType::START_TAG_TOKEN, Atom::TR,
2319
1.18M
                            AtomUtil::ToString(Atom::TR));
2320
1.18M
          return false;
2321
768k
        }
2322
0
        case Atom::CAPTION:
2323
1.45k
        case Atom::COL:
2324
1.45k
        case Atom::COLGROUP:
2325
2.03k
        case Atom::TBODY:
2326
36.7k
        case Atom::TFOOT:
2327
37.6k
        case Atom::THEAD: {
2328
37.6k
          if (PopUntil(Scope::TableScope, Atom::TBODY, Atom::THEAD,
2329
37.6k
                       Atom::TFOOT)) {
2330
37.6k
            insertion_mode_ = std::bind(&Parser::InTableIM, this);
2331
37.6k
            return false;
2332
37.6k
          }
2333
          // Ignore the token.
2334
0
          return true;
2335
37.6k
        }
2336
886k
        default:
2337
886k
          break;
2338
3.29M
      }
2339
886k
      break;
2340
3.29M
    }
2341
886k
    case TokenType::END_TAG_TOKEN: {
2342
11.9k
      switch (token_.atom) {
2343
336
        case Atom::TBODY:
2344
1.68k
        case Atom::TFOOT:
2345
1.91k
        case Atom::THEAD: {
2346
1.91k
          if (ElementInScope(Scope::TableScope, token_.atom)) {
2347
1.69k
            ClearStackToContext(Scope::TableBodyScope);
2348
1.69k
            open_elements_stack_.Pop();
2349
1.69k
            insertion_mode_ = std::bind(&Parser::InTableIM, this);
2350
1.69k
          }
2351
1.91k
          return true;
2352
1.68k
        }
2353
8.46k
        case Atom::TABLE: {
2354
8.46k
          if (PopUntil(Scope::TableScope, Atom::TBODY, Atom::THEAD,
2355
8.46k
                       Atom::TFOOT)) {
2356
8.46k
            insertion_mode_ = std::bind(&Parser::InTableIM, this);
2357
8.46k
            return false;
2358
8.46k
          }
2359
          // Ignore the token.
2360
0
          return true;
2361
8.46k
        }
2362
194
        case Atom::BODY:
2363
194
        case Atom::CAPTION:
2364
388
        case Atom::COL:
2365
388
        case Atom::COLGROUP:
2366
582
        case Atom::HTML:
2367
776
        case Atom::TD:
2368
970
        case Atom::TH:
2369
1.19k
        case Atom::TR: {
2370
          // Ignore the token.
2371
1.19k
          return true;
2372
970
        }
2373
329
        default:
2374
329
          break;
2375
11.9k
      }
2376
329
      break;
2377
11.9k
    }
2378
836
    case TokenType::COMMENT_TOKEN: {
2379
836
      Node* node = document_->NewNode(NodeType::COMMENT_NODE);
2380
836
      node->SetManufactured(token_.is_manufactured);
2381
836
      if (record_node_offsets_) {
2382
0
        node->line_col_in_html_src_ = token_.line_col_in_html_src;
2383
0
      }
2384
836
      node->data_ = token_.data;
2385
836
      AddChild(node);
2386
836
      return true;
2387
11.9k
    }
2388
44.2k
    default:
2389
44.2k
      break;
2390
3.35M
  }
2391
2392
931k
  return InTableIM();
2393
3.35M
}  // Parser::InTableBodyIM.
2394
2395
// Section 12.2.6.4.14.
2396
2.38M
bool Parser::InRowIM() {
2397
2.38M
  switch (token_.token_type) {
2398
2.36M
    case TokenType::START_TAG_TOKEN: {
2399
2.36M
      switch (token_.atom) {
2400
785k
        case Atom::TD:
2401
1.22M
        case Atom::TH: {
2402
1.22M
          ClearStackToContext(Scope::TableRowScope);
2403
1.22M
          AddElement();
2404
1.22M
          active_formatting_elements_stack_.Push(scope_marker_);
2405
1.22M
          insertion_mode_ = std::bind(&Parser::InCellIM, this);
2406
1.22M
          return true;
2407
785k
        }
2408
0
        case Atom::CAPTION:
2409
1.45k
        case Atom::COL:
2410
1.45k
        case Atom::COLGROUP:
2411
1.65k
        case Atom::TBODY:
2412
2.04k
        case Atom::TFOOT:
2413
2.24k
        case Atom::THEAD:
2414
5.20k
        case Atom::TR: {
2415
5.20k
          if (PopUntil(Scope::TableScope, Atom::TR)) {
2416
5.20k
            insertion_mode_ = std::bind(&Parser::InTableBodyIM, this);
2417
5.20k
            return false;
2418
5.20k
          }
2419
          // Ignore the token.
2420
0
          return true;
2421
0
          break;
2422
5.20k
        }
2423
1.13M
        default:
2424
1.13M
          break;
2425
2.36M
      }
2426
1.13M
      break;
2427
2.36M
    }
2428
1.13M
    case TokenType::END_TAG_TOKEN: {
2429
13.6k
      switch (token_.atom) {
2430
1.07k
        case Atom::TR: {
2431
1.07k
          if (PopUntil(Scope::TableScope, Atom::TR)) {
2432
1.07k
            insertion_mode_ = std::bind(&Parser::InTableBodyIM, this);
2433
1.07k
          }
2434
          // Ignore the token.
2435
1.07k
          return true;
2436
0
        }
2437
8.46k
        case Atom::TABLE: {
2438
8.46k
          if (PopUntil(Scope::TableScope, Atom::TR)) {
2439
8.46k
            insertion_mode_ = std::bind(&Parser::InTableBodyIM, this);
2440
8.46k
            return false;
2441
8.46k
          }
2442
          // Ignore the token.
2443
0
          return true;
2444
8.46k
        }
2445
329
        case Atom::TBODY:
2446
523
        case Atom::TFOOT:
2447
717
        case Atom::THEAD: {
2448
717
          if (ElementInScope(Scope::TableScope, token_.atom)) {
2449
457
            ParseImpliedToken(TokenType::END_TAG_TOKEN, Atom::TR,
2450
457
                              AtomUtil::ToString(Atom::TR));
2451
457
            return false;
2452
457
          }
2453
          // Ignore the token.
2454
260
          return true;
2455
717
        }
2456
194
        case Atom::BODY:
2457
194
        case Atom::CAPTION:
2458
388
        case Atom::COL:
2459
388
        case Atom::COLGROUP:
2460
582
        case Atom::HTML:
2461
790
        case Atom::TD:
2462
984
        case Atom::TH: {
2463
          // Ignore the token.
2464
984
          return true;
2465
790
        }
2466
2.44k
        default:
2467
2.44k
          break;
2468
13.6k
      }
2469
2.44k
      break;
2470
13.6k
    }
2471
11.0k
    default:
2472
11.0k
      break;
2473
2.38M
  }
2474
2475
1.15M
  return InTableIM();
2476
2.38M
}  // Parser::InRowIM.
2477
2478
// Section 12.2.6.4.15.
2479
2.03M
bool Parser::InCellIM() {
2480
2.03M
  switch (token_.token_type) {
2481
2.01M
    case TokenType::START_TAG_TOKEN: {
2482
2.01M
      switch (token_.atom) {
2483
0
        case Atom::CAPTION:
2484
1.32k
        case Atom::COL:
2485
1.32k
        case Atom::COLGROUP:
2486
1.52k
        case Atom::TBODY:
2487
18.7k
        case Atom::TD:
2488
18.9k
        case Atom::TFOOT:
2489
37.5k
        case Atom::TH:
2490
37.7k
        case Atom::THEAD:
2491
37.9k
        case Atom::TR: {
2492
37.9k
          if (PopUntil(Scope::TableScope, Atom::TD, Atom::TH)) {
2493
            // Close the cell and reprocess.
2494
37.9k
            ClearActiveFormattingElements();
2495
37.9k
            insertion_mode_ = std::bind(&Parser::InRowIM, this);
2496
37.9k
            return false;
2497
37.9k
          }
2498
          // Ignore the token.
2499
0
          return true;
2500
37.9k
        }
2501
0
        case Atom::SELECT: {
2502
0
          ReconstructActiveFormattingElements();
2503
0
          AddElement();
2504
0
          frameset_ok_ = false;
2505
0
          insertion_mode_ = std::bind(&Parser::InSelectInTableIM, this);
2506
0
          return true;
2507
37.9k
        }
2508
1.97M
        default:
2509
1.97M
          break;
2510
2.01M
      }
2511
1.97M
      break;
2512
2.01M
    }
2513
1.97M
    case TokenType::END_TAG_TOKEN: {
2514
11.6k
      switch (token_.atom) {
2515
266
        case Atom::TD:
2516
531
        case Atom::TH: {
2517
531
          if (!PopUntil(Scope::TableScope, token_.atom)) {
2518
            // Ignore the token.
2519
278
            return true;
2520
278
          }
2521
253
          ClearActiveFormattingElements();
2522
253
          insertion_mode_ = std::bind(&Parser::InRowIM, this);
2523
253
          return true;
2524
531
        }
2525
194
        case Atom::BODY:
2526
194
        case Atom::CAPTION:
2527
388
        case Atom::COL:
2528
388
        case Atom::COLGROUP:
2529
582
        case Atom::HTML: {
2530
          // Ignore the token.
2531
582
          return true;
2532
388
        }
2533
8.46k
        case Atom::TABLE:
2534
8.74k
        case Atom::TBODY:
2535
9.12k
        case Atom::TFOOT:
2536
9.31k
        case Atom::THEAD:
2537
9.86k
        case Atom::TR: {
2538
9.86k
          if (!ElementInScope(Scope::TableScope, token_.atom)) {
2539
            // Ignore the token.
2540
442
            return true;
2541
442
          }
2542
          // Close the cell and reprocess.
2543
9.42k
          if (PopUntil(Scope::TableScope, Atom::TD, Atom::TH)) {
2544
9.42k
            ClearActiveFormattingElements();
2545
9.42k
          }
2546
9.42k
          insertion_mode_ = std::bind(&Parser::InRowIM, this);
2547
9.42k
          return false;
2548
9.86k
        }
2549
662
        default:
2550
662
          break;
2551
11.6k
      }
2552
662
      break;
2553
11.6k
    }
2554
9.40k
    default:
2555
9.40k
      break;
2556
2.03M
  }
2557
1.98M
  return InBodyIM();
2558
2.03M
}  // Parser::InCellIM.
2559
2560
// Section 12.2.6.4.16.
2561
0
bool Parser::InSelectIM() {
2562
0
  switch (token_.token_type) {
2563
0
    case TokenType::TEXT_TOKEN: {
2564
0
      Strings::ReplaceAny(&token_.data, Strings::kNullChar, "");
2565
0
      AddText(token_.data);
2566
0
      break;
2567
0
    }
2568
0
    case TokenType::START_TAG_TOKEN: {
2569
0
      switch (token_.atom) {
2570
0
        case Atom::HTML: {
2571
0
          return InBodyIM();
2572
0
        }
2573
0
        case Atom::OPTION: {
2574
0
          if (top()->atom_ == Atom::OPTION) {
2575
0
            open_elements_stack_.Pop();
2576
0
          }
2577
0
          AddElement();
2578
0
          break;
2579
0
        }
2580
0
        case Atom::OPTGROUP: {
2581
0
          if (top()->atom_ == Atom::OPTION) {
2582
0
            open_elements_stack_.Pop();
2583
0
          }
2584
0
          if (top()->atom_ == Atom::OPTGROUP) {
2585
0
            open_elements_stack_.Pop();
2586
0
          }
2587
0
          AddElement();
2588
0
          break;
2589
0
        }
2590
0
        case Atom::SELECT: {
2591
0
          if (PopUntil(Scope::SelectScope, Atom::SELECT)) {
2592
0
            ResetInsertionMode();
2593
0
          }
2594
          // Ignore the token.
2595
0
          return true;
2596
0
        }
2597
0
        case Atom::INPUT:
2598
0
        case Atom::KEYGEN:
2599
0
        case Atom::TEXTAREA: {
2600
0
          if (ElementInScope(Scope::SelectScope, Atom::SELECT)) {
2601
0
            ParseImpliedToken(TokenType::END_TAG_TOKEN, Atom::SELECT,
2602
0
                              AtomUtil::ToString(Atom::SELECT));
2603
0
            return false;
2604
0
          }
2605
          // In order to properly ignore <textarea>, we need to change the
2606
          // tokenizer mode.
2607
0
          tokenizer_->NextIsNotRawText();
2608
          // Ignore the token.
2609
0
          return true;
2610
0
        }
2611
0
        case Atom::SCRIPT:
2612
0
        case Atom::TEMPLATE: {
2613
0
          return InHeadIM();
2614
0
        }
2615
0
        case Atom::IFRAME:
2616
0
        case Atom::NOEMBED:
2617
0
        case Atom::NOFRAMES:
2618
0
        case Atom::NOSCRIPT:
2619
0
        case Atom::PLAINTEXT:
2620
0
        case Atom::STYLE:
2621
0
        case Atom::TITLE:
2622
0
        case Atom::XMP: {
2623
          // Don't let the tokenizer go into raw text mode when there are raw
2624
          // tags to be ignored. These tags should be ignored from the tokenizer
2625
          // properly.
2626
0
          tokenizer_->NextIsNotRawText();
2627
          // Ignore the token.
2628
0
          return true;
2629
0
        }
2630
0
        default:
2631
0
          break;
2632
0
      }
2633
0
      break;
2634
0
    }
2635
0
    case TokenType::END_TAG_TOKEN: {
2636
0
      switch (token_.atom) {
2637
0
        case Atom::OPTION: {
2638
0
          if (top()->atom_ == Atom::OPTION) {
2639
0
            open_elements_stack_.Pop();
2640
0
          }
2641
0
          break;
2642
0
        }
2643
0
        case Atom::OPTGROUP: {
2644
0
          int i = open_elements_stack_.size() - 1;
2645
0
          Node* node = open_elements_stack_.at(i);
2646
0
          if (node && node->atom_ == Atom::OPTION) {
2647
0
            i--;
2648
0
          }
2649
0
          node = open_elements_stack_.at(i);
2650
0
          if (node && node->atom_ == Atom::OPTGROUP) {
2651
0
            open_elements_stack_.Pop(open_elements_stack_.size() - i);
2652
0
          }
2653
0
          break;
2654
0
        }
2655
0
        case Atom::SELECT: {
2656
0
          if (!PopUntil(Scope::SelectScope, Atom::SELECT)) {
2657
            // Ignore the token.
2658
0
            return true;
2659
0
          }
2660
0
          ResetInsertionMode();
2661
0
          break;
2662
0
        }
2663
0
        case Atom::TEMPLATE: {
2664
0
          return InHeadIM();
2665
0
        }
2666
0
        default:
2667
0
          break;
2668
0
      }
2669
0
      break;
2670
0
    }
2671
0
    case TokenType::COMMENT_TOKEN: {
2672
0
      Node* node = document_->NewNode(NodeType::COMMENT_NODE);
2673
0
      node->SetManufactured(token_.is_manufactured);
2674
0
      if (record_node_offsets_) {
2675
0
        node->line_col_in_html_src_ = token_.line_col_in_html_src;
2676
0
      }
2677
0
      node->data_ = token_.data;
2678
0
      AddChild(node);
2679
0
      break;
2680
0
    }
2681
0
    case TokenType::DOCTYPE_TOKEN: {
2682
      // Ignore the token.
2683
0
      return true;
2684
0
    }
2685
0
    case TokenType::ERROR_TOKEN: {
2686
0
      return InBodyIM();
2687
0
    }
2688
0
    default:
2689
0
      break;
2690
0
  }
2691
2692
0
  return true;
2693
0
}  // Parser::InSelectIM.
2694
2695
// Section 12.2.6.4.17.
2696
0
bool Parser::InSelectInTableIM() {
2697
0
  switch (token_.token_type) {
2698
0
    case TokenType::START_TAG_TOKEN:
2699
0
    case TokenType::END_TAG_TOKEN: {
2700
0
      switch (token_.atom) {
2701
0
        case Atom::CAPTION:
2702
0
        case Atom::TABLE:
2703
0
        case Atom::TBODY:
2704
0
        case Atom::TFOOT:
2705
0
        case Atom::THEAD:
2706
0
        case Atom::TR:
2707
0
        case Atom::TD:
2708
0
        case Atom::TH: {
2709
0
          if (token_.token_type == TokenType::END_TAG_TOKEN &&
2710
0
              !ElementInScope(Scope::TableScope, token_.atom)) {
2711
            // Ignore the token.
2712
0
            return true;
2713
0
          }
2714
          // This is like p.popUntil(selectScope, a.Select), but it also
2715
          // matches <math select>, not just <select>. Matching the MathML
2716
          // tag is arguably incorrect (conceptually), but it mimics what
2717
          // Chromium does.
2718
0
          for (int i = open_elements_stack_.size() - 1; i >= 0; --i) {
2719
0
            if (open_elements_stack_.at(i)->atom_ == Atom::SELECT) {
2720
0
              open_elements_stack_.Pop(open_elements_stack_.size() - i);
2721
0
              break;
2722
0
            }
2723
0
          }
2724
0
          ResetInsertionMode();
2725
0
          return false;
2726
0
        }
2727
0
        default:
2728
0
          break;
2729
0
      }
2730
0
      break;
2731
0
    }
2732
0
    default:
2733
0
      break;
2734
0
  }
2735
2736
0
  return InSelectIM();
2737
0
}  // Parser::InSelectInTableIM.
2738
2739
// Section 12.2.6.4.18.
2740
0
bool Parser::InTemplateIM() {
2741
0
  switch (token_.token_type) {
2742
0
    case TokenType::TEXT_TOKEN:
2743
0
    case TokenType::COMMENT_TOKEN:
2744
0
    case TokenType::DOCTYPE_TOKEN:
2745
0
      return InBodyIM();
2746
0
    case TokenType::START_TAG_TOKEN: {
2747
0
      switch (token_.atom) {
2748
0
        case Atom::BASE:
2749
0
        case Atom::BASEFONT:
2750
0
        case Atom::BGSOUND:
2751
0
        case Atom::LINK:
2752
0
        case Atom::META:
2753
0
        case Atom::NOFRAMES:
2754
0
        case Atom::SCRIPT:
2755
0
        case Atom::STYLE:
2756
0
        case Atom::TEMPLATE:
2757
0
        case Atom::TITLE:
2758
0
          return InHeadIM();
2759
0
        case Atom::CAPTION:
2760
0
        case Atom::COLGROUP:
2761
0
        case Atom::TBODY:
2762
0
        case Atom::TFOOT:
2763
0
        case Atom::THEAD: {
2764
0
          template_stack_.pop_back();
2765
0
          template_stack_.push_back(std::bind(&Parser::InTableIM, this));
2766
0
          insertion_mode_ = std::bind(&Parser::InTableIM, this);
2767
0
          return false;
2768
0
        }
2769
0
        case Atom::COL: {
2770
0
          template_stack_.pop_back();
2771
0
          template_stack_.push_back(std::bind(&Parser::InColumnGroupIM, this));
2772
0
          insertion_mode_ = std::bind(&Parser::InColumnGroupIM, this);
2773
0
          return false;
2774
0
        }
2775
0
        case Atom::TR: {
2776
0
          template_stack_.pop_back();
2777
0
          template_stack_.push_back(std::bind(&Parser::InTableBodyIM, this));
2778
0
          insertion_mode_ = std::bind(&Parser::InTableBodyIM, this);
2779
0
          return false;
2780
0
        }
2781
0
        case Atom::TD:
2782
0
        case Atom::TH: {
2783
0
          template_stack_.pop_back();
2784
0
          template_stack_.push_back(std::bind(&Parser::InRowIM, this));
2785
0
          insertion_mode_ = std::bind(&Parser::InRowIM, this);
2786
0
          return false;
2787
0
        }
2788
0
        default:
2789
0
          template_stack_.pop_back();
2790
0
          template_stack_.push_back(std::bind(&Parser::InBodyIM, this));
2791
0
          insertion_mode_ = std::bind(&Parser::InBodyIM, this);
2792
0
          return false;
2793
0
      }
2794
0
    }
2795
0
    case TokenType::END_TAG_TOKEN: {
2796
0
      switch (token_.atom) {
2797
0
        case Atom::TEMPLATE:
2798
0
          return InHeadIM();
2799
0
        default:
2800
          // Ignore the token.
2801
0
          return true;
2802
0
      }
2803
0
    }
2804
0
    case TokenType::ERROR_TOKEN: {
2805
0
      if (!open_elements_stack_.Contains(Atom::TEMPLATE)) {
2806
        // Ignore the token.
2807
0
        return true;
2808
0
      }
2809
      // TODO: remove this divergence from the HTML5 spec.
2810
      //
2811
      // See https://bugs.chromium.org/p/chromium/issues/detail?id=829668
2812
0
      GenerateImpliedEndTags();
2813
0
      for (int i = open_elements_stack_.size() - 1; i >= 0; --i) {
2814
0
        Node* node = open_elements_stack_.at(i);
2815
0
        if (node->name_space_.empty() && node->atom_ == Atom::TEMPLATE) {
2816
0
          open_elements_stack_.Pop(open_elements_stack_.size() - i);
2817
0
          break;
2818
0
        }
2819
0
      }
2820
0
      ClearActiveFormattingElements();
2821
0
      template_stack_.pop_back();
2822
0
      ResetInsertionMode();
2823
0
      return false;
2824
0
    }
2825
0
    default:
2826
0
      break;
2827
0
  }
2828
0
  return false;
2829
0
}  // Parser::InTemplateIM.
2830
2831
// Section 12.2.6.4.19.
2832
8.62k
bool Parser::AfterBodyIM() {
2833
8.62k
  switch (token_.token_type) {
2834
20
    case TokenType::ERROR_TOKEN:
2835
      // Stop parsing.
2836
20
      return true;
2837
2.89k
    case TokenType::TEXT_TOKEN:
2838
      // https://www.w3.org/TR/2011/WD-html5-20110113/tokenization.html#parsing-main-afterbody
2839
2.89k
      if (token_.data.find_first_not_of(Strings::kWhitespace) ==
2840
2.89k
          std::string::npos) {
2841
        // It was all whitesapce.
2842
263
        return InBodyIM();
2843
263
      }
2844
2.63k
      break;
2845
2.63k
    case TokenType::START_TAG_TOKEN:
2846
494
      if (token_.atom == Atom::HTML) {
2847
0
        return InBodyIM();
2848
0
      }
2849
494
      break;
2850
2.43k
    case TokenType::END_TAG_TOKEN:
2851
2.43k
      if (token_.atom == Atom::HTML) {
2852
2.08k
        if (!fragment_) {
2853
2.08k
          insertion_mode_ = std::bind(&Parser::AfterAfterBodyIM, this);
2854
2.08k
        }
2855
2.08k
        return true;
2856
2.08k
      }
2857
350
      break;
2858
2.78k
    case TokenType::COMMENT_TOKEN: {
2859
      // The comment is attached to the <html> element.
2860
2.78k
      CHECK(open_elements_stack_.size() > 0 &&
2861
2.78k
            open_elements_stack_.at(0)->atom_ == Atom::HTML)
2862
2.78k
          << "html: bad parser state: <html> element not found, in the "
2863
2.78k
             "after-body insertion mode";
2864
2.78k
      Node* node = document_->NewNode(NodeType::COMMENT_NODE);
2865
2.78k
      node->SetManufactured(token_.is_manufactured);
2866
2.78k
      if (record_node_offsets_) {
2867
0
        node->line_col_in_html_src_ = token_.line_col_in_html_src;
2868
0
      }
2869
2.78k
      node->data_ = token_.data;
2870
2.78k
      open_elements_stack_.at(0)->AppendChild(node);
2871
2.78k
      return true;
2872
2.43k
    }
2873
0
    default:
2874
0
      break;
2875
8.62k
  }
2876
2877
3.47k
  insertion_mode_ = std::bind(&Parser::InBodyIM, this);
2878
3.47k
  return false;
2879
8.62k
}  // Parser::AfterBodyIM.
2880
2881
// Section 12.2.6.4.20.
2882
0
bool Parser::InFramesetIM() {
2883
0
  switch (token_.token_type) {
2884
0
    case TokenType::COMMENT_TOKEN: {
2885
0
      Node* node = document_->NewNode(NodeType::COMMENT_NODE);
2886
0
      node->SetManufactured(token_.is_manufactured);
2887
0
      if (record_node_offsets_) {
2888
0
        node->line_col_in_html_src_ = token_.line_col_in_html_src;
2889
0
      }
2890
0
      node->data_ = token_.data;
2891
0
      AddChild(node);
2892
0
      break;
2893
0
    }
2894
0
    case TokenType::TEXT_TOKEN: {
2895
0
      std::string whitespace_only = ExtractWhitespace(token_.data);
2896
0
      if (!whitespace_only.empty()) AddText(whitespace_only);
2897
0
      break;
2898
0
    }
2899
0
    case TokenType::START_TAG_TOKEN:
2900
0
      switch (token_.atom) {
2901
0
        case Atom::HTML:
2902
0
          return InBodyIM();
2903
0
        case Atom::FRAMESET:
2904
0
          AddElement();
2905
0
          break;
2906
0
        case Atom::FRAME:
2907
0
          AddElement();
2908
0
          open_elements_stack_.Pop();
2909
0
          AcknowledgeSelfClosingTag();
2910
0
          break;
2911
0
        case Atom::NOFRAMES:
2912
0
          return InHeadIM();
2913
0
        default:
2914
0
          break;
2915
0
      }
2916
0
      break;
2917
0
    case TokenType::END_TAG_TOKEN:
2918
0
      switch (token_.atom) {
2919
0
        case Atom::FRAMESET:
2920
0
          if (open_elements_stack_.Top()->atom_ != Atom::HTML) {
2921
0
            open_elements_stack_.Pop();
2922
0
            if (open_elements_stack_.Top()->atom_ != Atom::FRAMESET) {
2923
0
              insertion_mode_ = std::bind(&Parser::AfterFramesetIM, this);
2924
0
              return true;
2925
0
            }
2926
0
          }
2927
0
          break;
2928
0
        default:
2929
0
          break;
2930
0
      }
2931
0
      break;
2932
0
    default:
2933
      // Ignore the token.
2934
0
      break;
2935
0
  }
2936
0
  return true;
2937
0
}  // Parser::InFramesetIM.
2938
2939
// Section 12.2.6.4.21.
2940
0
bool Parser::AfterFramesetIM() {
2941
0
  switch (token_.token_type) {
2942
0
    case TokenType::COMMENT_TOKEN: {
2943
0
      Node* node = document_->NewNode(NodeType::COMMENT_NODE);
2944
0
      node->SetManufactured(token_.is_manufactured);
2945
0
      if (record_node_offsets_) {
2946
0
        node->line_col_in_html_src_ = token_.line_col_in_html_src;
2947
0
      }
2948
0
      node->data_ = token_.data;
2949
0
      AddChild(node);
2950
0
      break;
2951
0
    }
2952
0
    case TokenType::TEXT_TOKEN: {
2953
0
      std::string whitespace_only = ExtractWhitespace(token_.data);
2954
0
      if (!whitespace_only.empty()) AddText(whitespace_only);
2955
0
      break;
2956
0
    }
2957
0
    case TokenType::START_TAG_TOKEN:
2958
0
      switch (token_.atom) {
2959
0
        case Atom::HTML:
2960
0
          return InBodyIM();
2961
0
        case Atom::NOFRAMES:
2962
0
          return InHeadIM();
2963
0
        default:
2964
0
          break;
2965
0
      }
2966
0
      break;
2967
0
    case TokenType::END_TAG_TOKEN:
2968
0
      switch (token_.atom) {
2969
0
        case Atom::HTML:
2970
0
          insertion_mode_ = std::bind(&Parser::AfterAfterFramesetIM, this);
2971
0
          return true;
2972
0
        default:
2973
0
          break;
2974
0
      }
2975
0
      break;
2976
0
    default:
2977
      // Ignore the token.
2978
0
      break;
2979
0
  }
2980
0
  return true;
2981
0
}  // Parser::AfterFramesetIM.
2982
2983
// Section 12.2.6.4.22.
2984
3.59k
bool Parser::AfterAfterBodyIM() {
2985
3.59k
  switch (token_.token_type) {
2986
98
    case TokenType::ERROR_TOKEN:
2987
      // Stop parsing.
2988
98
      return true;
2989
920
    case TokenType::TEXT_TOKEN: {
2990
920
      if (token_.data.find_first_not_of(Strings::kWhitespace) ==
2991
920
          std::string::npos) {
2992
504
        return InBodyIM();
2993
504
      }
2994
416
      break;
2995
920
    }
2996
530
    case TokenType::START_TAG_TOKEN:
2997
530
      if (token_.atom == Atom::HTML) {
2998
324
        return InBodyIM();
2999
324
      }
3000
206
      break;
3001
681
    case TokenType::COMMENT_TOKEN: {
3002
681
      Node* node = document_->NewNode(NodeType::COMMENT_NODE);
3003
681
      node->SetManufactured(token_.is_manufactured);
3004
681
      if (record_node_offsets_) {
3005
0
        node->line_col_in_html_src_ = token_.line_col_in_html_src;
3006
0
      }
3007
681
      node->data_ = token_.data;
3008
681
      document_->root_node_->AppendChild(node);
3009
681
      return true;
3010
530
    }
3011
0
    case TokenType::DOCTYPE_TOKEN:
3012
0
      return InBodyIM();
3013
1.36k
    default:
3014
1.36k
      break;
3015
3.59k
  }
3016
3017
1.98k
  insertion_mode_ = std::bind(&Parser::InBodyIM, this);
3018
1.98k
  return false;
3019
3.59k
}  // Parser::AfterAfterBodyIM.
3020
3021
// Section 12.2.6.4.23.
3022
0
bool Parser::AfterAfterFramesetIM() {
3023
0
  switch (token_.token_type) {
3024
0
    case TokenType::COMMENT_TOKEN: {
3025
0
      Node* node = document_->NewNode(NodeType::COMMENT_NODE);
3026
0
      node->SetManufactured(token_.is_manufactured);
3027
0
      if (record_node_offsets_) {
3028
0
        node->line_col_in_html_src_ = token_.line_col_in_html_src;
3029
0
      }
3030
0
      node->data_ = token_.data;
3031
0
      document_->root_node_->AppendChild(node);
3032
0
      break;
3033
0
    }
3034
0
    case TokenType::TEXT_TOKEN: {
3035
0
      std::string whitespace_only = ExtractWhitespace(token_.data);
3036
0
      if (!whitespace_only.empty()) {
3037
0
        token_.data = whitespace_only;
3038
0
        return InBodyIM();
3039
0
      }
3040
0
      break;
3041
0
    }
3042
0
    case TokenType::START_TAG_TOKEN:
3043
0
      switch (token_.atom) {
3044
0
        case Atom::HTML:
3045
0
          return InBodyIM();
3046
0
        case Atom::NOFRAMES:
3047
0
          return InHeadIM();
3048
0
        default:
3049
0
          break;
3050
0
      }
3051
0
      break;
3052
0
    case TokenType::DOCTYPE_TOKEN:
3053
0
      return InBodyIM();
3054
0
    default:
3055
0
      break;
3056
0
  }
3057
0
  return true;
3058
0
}  // Parser::AfterAfterFramesetIM.
3059
3060
24.4M
Node* Parser::AdjustedCurrentNode() {
3061
24.4M
  if (open_elements_stack_.size() == 1 && fragment_ && context_node_)
3062
0
    return context_node_;
3063
24.4M
  return open_elements_stack_.Top();
3064
24.4M
}
3065
3066
// Section 12.2.6.5.
3067
2.86M
bool Parser::ParseForeignContent() {
3068
2.86M
  switch (token_.token_type) {
3069
51.8k
    case TokenType::TEXT_TOKEN: {
3070
51.8k
      if (frameset_ok_) {
3071
870
        frameset_ok_ = (token_.data.find_first_not_of(
3072
870
                            Strings::kWhitespaceOrNull) == std::string::npos);
3073
870
      }
3074
      // Replaces null char with \ufffd replacement character.
3075
51.8k
      Strings::ReplaceAny(&token_.data, Strings::kNullChar,
3076
51.8k
                          Strings::kNullReplacementChar);
3077
51.8k
      AddText(token_.data);
3078
51.8k
      break;
3079
0
    }
3080
1.64k
    case TokenType::COMMENT_TOKEN: {
3081
1.64k
      Node* node = document_->NewNode(NodeType::COMMENT_NODE);
3082
1.64k
      node->SetManufactured(token_.is_manufactured);
3083
1.64k
      if (record_node_offsets_) {
3084
0
        node->line_col_in_html_src_ = token_.line_col_in_html_src;
3085
0
      }
3086
1.64k
      node->data_ = token_.data;
3087
1.64k
      AddChild(node);
3088
1.64k
      break;
3089
0
    }
3090
2.80M
    case TokenType::START_TAG_TOKEN: {
3091
2.80M
      if (!fragment_) {
3092
2.80M
        auto breaktout_tag = std::find(std::begin(kBreakoutTags),
3093
2.80M
                                       std::end(kBreakoutTags), token_.atom);
3094
2.80M
        bool is_breakout_tag = breaktout_tag != std::end(kBreakoutTags);
3095
3096
2.80M
        if (token_.atom == Atom::FONT) {
3097
2.20k
          for (auto& attr : token_.attributes) {
3098
2.20k
            std::string key = attr.key;
3099
2.20k
            if (key == "color" || key == "face" || key == "size") {
3100
0
              is_breakout_tag = true;
3101
0
              break;
3102
0
            }
3103
2.20k
          }
3104
480
        }
3105
2.80M
        if (is_breakout_tag) {
3106
1.85M
          for (int i = open_elements_stack_.size() - 1; i >= 0; --i) {
3107
1.85M
            Node* node = open_elements_stack_.at(i);
3108
1.85M
            if (node->name_space_.empty() || HtmlIntegrationPoint(*node) ||
3109
1.85M
                MathMLTextIntegrationPoint(*node)) {
3110
1.03k
              open_elements_stack_.Pop(open_elements_stack_.size() - i - 1);
3111
1.03k
              break;
3112
1.03k
            }
3113
1.85M
          }
3114
1.03k
          return false;
3115
1.03k
        }
3116
2.80M
      }
3117
3118
2.80M
      Node* current = AdjustedCurrentNode();
3119
2.80M
      if (current->name_space_ == "math") {
3120
1.15M
        AdjustMathMLAttributeNames(&token_.attributes);
3121
1.64M
      } else if (current->name_space_ == "svg") {
3122
59.3M
        for (auto [name, adjusted] : kSvgTagNameAdjustments) {
3123
59.3M
          if (name == token_.atom) {
3124
0
            token_.atom = adjusted;
3125
0
          }
3126
59.3M
        }
3127
1.64M
        AdjustSVGAttributeNames(&token_.attributes);
3128
1.64M
      } else {
3129
0
        CHECK(false) << "html: bad parser state: unexpected namespace";
3130
0
      }
3131
3132
2.80M
      AdjustForeignAttributes(&token_.attributes);
3133
2.80M
      auto& ns = current->name_space_;
3134
2.80M
      AddElement();
3135
2.80M
      top()->name_space_ = ns;
3136
2.80M
      if (!ns.empty()) {
3137
        // Don't let the tokenizer go into raw text mode in foreign content.
3138
        // (e.g. in an SVG <title> tag).
3139
2.80M
        tokenizer_->NextIsNotRawText();
3140
2.80M
      }
3141
2.80M
      if (has_self_closing_token_) {
3142
196
        open_elements_stack_.Pop();
3143
196
        AcknowledgeSelfClosingTag();
3144
196
      }
3145
2.80M
      break;
3146
2.80M
    }
3147
5.05k
    case TokenType::END_TAG_TOKEN:
3148
58.4k
      for (int i = open_elements_stack_.size() - 1; i >= 0; --i) {
3149
58.4k
        if (open_elements_stack_.at(i)->name_space_.empty()) {
3150
4.84k
          return insertion_mode_();
3151
4.84k
        }
3152
3153
53.5k
        auto sn = open_elements_stack_.at(i);
3154
53.5k
        auto node_data = sn->atom_ != Atom::UNKNOWN
3155
53.5k
                             ? AtomUtil::ToString(sn->atom_)
3156
53.5k
                             : sn->data_;
3157
53.5k
        auto token_data = token_.atom != Atom::UNKNOWN
3158
53.5k
                              ? AtomUtil::ToString(token_.atom)
3159
53.5k
                              : token_.data;
3160
3161
53.5k
        if (Strings::EqualFold(node_data, token_data)) {
3162
206
          open_elements_stack_.Pop(open_elements_stack_.size() - i);
3163
206
          break;
3164
206
        }
3165
53.5k
      }
3166
206
      return true;
3167
0
    default:
3168
      // Ignore the token.
3169
0
      break;
3170
2.86M
  }
3171
2.85M
  return true;
3172
2.86M
}  // Parser::ParseForeignContent.
3173
3174
// Section 12.2.6.
3175
21.7M
bool Parser::InForeignContent() {
3176
21.7M
  if (open_elements_stack_.size() == 0) return false;
3177
3178
21.6M
  Node* node = AdjustedCurrentNode();
3179
21.6M
  if (node->name_space_.empty()) return false;
3180
2.86M
  Atom token_atom = token_.atom;
3181
2.86M
  TokenType token_type = token_.token_type;
3182
2.86M
  if (MathMLTextIntegrationPoint(*node)) {
3183
2.66k
    if (token_type == TokenType::START_TAG_TOKEN &&
3184
760
        token_atom != Atom::MGLYPH && token_atom != Atom::MALIGNMARK) {
3185
760
      return false;
3186
760
    }
3187
1.90k
    if (token_type == TokenType::TEXT_TOKEN) {
3188
387
      return false;
3189
387
    }
3190
1.90k
  }
3191
3192
2.86M
  if (node->name_space_ == "math" && node->atom_ == Atom::ANNOTATION_XML &&
3193
0
      token_type == TokenType::START_TAG_TOKEN && token_atom == Atom::SVG) {
3194
0
    return false;
3195
0
  }
3196
3197
2.86M
  if (HtmlIntegrationPoint(*node) &&
3198
1.98k
      (token_type == TokenType::START_TAG_TOKEN ||
3199
1.22k
       token_type == TokenType::TEXT_TOKEN)) {
3200
1.18k
    return false;
3201
1.18k
  }
3202
3203
2.86M
  if (token_type == TokenType::ERROR_TOKEN) {
3204
1.05k
    return false;
3205
1.05k
  }
3206
3207
2.86M
  return true;
3208
2.86M
}  // Parser::InForeignContent.
3209
3210
// Section 12.2.6.2.
3211
40.2k
void Parser::ParseGenericRawTextElement() {
3212
40.2k
  AddElement();
3213
40.2k
  original_insertion_mode_ = insertion_mode_;
3214
40.2k
  insertion_mode_ = std::bind(&Parser::TextIM, this);
3215
40.2k
}
3216
3217
void Parser::ParseImpliedToken(TokenType token_type, Atom atom,
3218
2.42M
                               const std::string& data) {
3219
  // Copy original token.
3220
2.42M
  Token real_token = {.token_type = token_.token_type,
3221
2.42M
                      .atom = token_.atom,
3222
2.42M
                      .data = token_.data,
3223
2.42M
                      .line_col_in_html_src = token_.line_col_in_html_src,
3224
2.42M
                      .attributes = token_.attributes};
3225
2.42M
  bool self_closing = has_self_closing_token_;
3226
  // Create implied tokens.
3227
2.42M
  token_ = {.token_type = token_type,
3228
2.42M
            .atom = atom,
3229
2.42M
            .data = data,
3230
            // For reporting purposes implied tokens are assumed to be parsed at
3231
            // the current tag location.
3232
2.42M
            .line_col_in_html_src = token_.line_col_in_html_src,
3233
2.42M
            .attributes = {}};
3234
2.42M
  has_self_closing_token_ = false;
3235
3236
  // Accounting for manufactured tags.
3237
2.42M
  if (token_type == TokenType::START_TAG_TOKEN) {
3238
2.41M
    switch (atom) {
3239
12.5k
      case Atom::HTML:
3240
12.5k
        document_->metadata_.has_manufactured_html = true;
3241
12.5k
        break;
3242
12.5k
      case Atom::HEAD:
3243
12.5k
        document_->metadata_.has_manufactured_head = true;
3244
12.5k
        break;
3245
12.0k
      case Atom::BODY:
3246
12.0k
        document_->metadata_.has_manufactured_body = true;
3247
12.0k
        break;
3248
2.37M
      default:
3249
2.37M
        break;
3250
2.41M
    }
3251
2.41M
  }
3252
3253
2.42M
  ParseCurrentToken();
3254
  // Restore original token.
3255
2.42M
  token_ = {.token_type = real_token.token_type,
3256
2.42M
            .atom = real_token.atom,
3257
2.42M
            .data = real_token.data,
3258
2.42M
            .line_col_in_html_src = token_.line_col_in_html_src,
3259
2.42M
            .attributes = real_token.attributes};
3260
2.42M
  has_self_closing_token_ = self_closing;
3261
2.42M
}  // Parser::ParseImpliedToken.
3262
3263
19.1M
void Parser::ParseCurrentToken() {
3264
19.1M
  if (token_.token_type == TokenType::SELF_CLOSING_TAG_TOKEN) {
3265
1.03k
    has_self_closing_token_ = true;
3266
1.03k
    token_.token_type = TokenType::START_TAG_TOKEN;
3267
1.03k
  }
3268
3269
19.1M
  bool consumed = false;
3270
3271
40.8M
  while (!consumed) {
3272
21.7M
    if (InForeignContent()) {
3273
2.86M
      consumed = ParseForeignContent();
3274
18.8M
    } else {
3275
18.8M
      consumed = insertion_mode_();
3276
18.8M
    }
3277
21.7M
  }
3278
3279
19.1M
  if (has_self_closing_token_) {
3280
    // This is a parse error, but ignore it.
3281
437
    has_self_closing_token_ = false;
3282
437
  }
3283
19.1M
}  // Parser::ParseCurrentToken.
3284
3285
7.10k
void Parser::CopyAttributes(Node* node, Token token) const {
3286
7.10k
  if (token.attributes.empty()) return;
3287
4.19k
  std::set<std::string> attr_keys;
3288
4.19k
  std::transform(node->attributes_.begin(), node->attributes_.end(),
3289
4.19k
                 std::inserter(attr_keys, attr_keys.begin()),
3290
34.8k
                 [](const Attribute& attr) -> std::string { return attr.key; });
3291
8.06k
  for (const Attribute& attr : token.attributes) {
3292
8.06k
    if (attr_keys.find(attr.key) == attr_keys.end()) {
3293
4.21k
      node->attributes_.push_back(attr);
3294
4.21k
      attr_keys.insert(attr.key);
3295
4.21k
    }
3296
8.06k
  }
3297
4.19k
}  // Parser::CopyAttributes.
3298
3299
998
void Parser::RecordBaseURLMetadata(Node* base_node) {
3300
998
  if (base_node->Type() != NodeType::ELEMENT_NODE ||
3301
804
      base_node->DataAtom() != Atom::BASE) return;
3302
3303
1.04k
  for (auto& attr : base_node->Attributes()) {
3304
1.04k
    if (Strings::EqualFold(attr.key, "href")) {
3305
195
      document_->metadata_.base_url.first = attr.value;
3306
849
    } else if (Strings::EqualFold(attr.key, "target")) {
3307
0
      document_->metadata_.base_url.second = attr.value;
3308
0
    }
3309
1.04k
  }
3310
610
}
3311
3312
1.01k
void Parser::RecordLinkRelCanonical(Node* link_node) {
3313
1.01k
  if (link_node->Type() != NodeType::ELEMENT_NODE ||
3314
816
      link_node->DataAtom() != Atom::LINK) return;
3315
3316
622
  bool canonical = false;
3317
622
  std::string canonical_url;
3318
1.62k
  for (auto& attr : link_node->Attributes()) {
3319
1.62k
    if (Strings::EqualFold(attr.key, "rel") &&
3320
528
        Strings::EqualFold(attr.value, "canonical")) {
3321
0
      canonical = true;
3322
1.62k
    } else if (Strings::EqualFold(attr.key, "href")) {
3323
195
      canonical_url = attr.value;
3324
195
    }
3325
1.62k
  }
3326
622
  if (canonical && !canonical_url.empty()) {
3327
0
    document_->metadata_.canonical_url = canonical_url;
3328
0
  }
3329
622
}
3330
3331
namespace {
3332
// Returns only whitespace characters in s.
3333
// <space><space>foo<space>bar<space> returns 4 spaces.
3334
0
std::string ExtractWhitespace(const std::string& s) {
3335
0
  std::string only_whitespaces;
3336
0
  std::copy_if(
3337
0
      s.begin(), s.end(),
3338
0
      only_whitespaces.begin(),  // Unused, populated directly in predicate.
3339
0
      [&only_whitespaces](char c) -> bool {
3340
0
        if (Strings::kWhitespace.find(c) != std::string::npos) {
3341
0
          only_whitespaces.push_back(c);
3342
0
        }
3343
0
        return false;
3344
0
      });
3345
0
  return only_whitespaces;
3346
0
}
3347
3348
}  // namespace
3349
3350
}  // namespace htmlparser