This repository was archived by the owner on Feb 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree_constructor.cpp
More file actions
208 lines (171 loc) · 6.38 KB
/
Copy pathtree_constructor.cpp
File metadata and controls
208 lines (171 loc) · 6.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/**
* Copyright (C) 2020 Tristan. All Rights Reserved.
* This file is licensed under the BSD 2-Clause license.
* See the COPYING file for licensing information.
*/
#include "tree_constructor.hpp"
#include <iostream>
#include <memory>
#include <sstream>
#include <vector>
#include "data/text/ustring.hpp"
#include "dom/comment.hpp"
#include "dom/resettable_element.hpp"
#include "logger.hpp"
#include "parser/html/context.hpp"
#include "parser/html/tree/insert_before_head.hpp"
#include "parser/html/tree/insert_before_html.hpp"
#include "parser/html/tree/insert_in_head.hpp"
#include "parser/html/tree/insert_initial.hpp"
namespace HTML {
TreeConstructor::TreeConstructor(Tokenizer::Context &context)
: context(context), currentMode(InsertionModeType::INITIAL),
insertionModes({
{InsertionModeType::INITIAL, std::make_shared<InsertionModes::Initial>(*this)},
{InsertionModeType::BEFORE_HTML, std::make_shared<InsertionModes::BeforeHTML>(*this)},
{InsertionModeType::BEFORE_HEAD, std::make_shared<InsertionModes::BeforeHead>(*this)},
{InsertionModeType::IN_HEAD, std::make_shared<InsertionModes::InHead>(*this)},
}) {
}
void
TreeConstructor::EmitToken(HTML::Tokenizer::Token &inToken) {
bool reprocess = false;
std::size_t reprocessCount = 0;
do {
if (reprocessCount == 10) {
std::stringstream info;
info << "Reprocess loop detected! Reprocess requested 10 times! Quitting emission of token.";
info << " InsertionMode: " << currentMode;
Logger::Warning("TreeConstructor", info.str());
return;
}
auto iterator = insertionModes.find(currentMode);
if (iterator == insertionModes.end()) {
std::stringstream output;
output << "Unknown insertion mode: \033[1;35m";
output << currentMode;
Logger::Warning("TreeConstructor", output.str());
reprocess = false;
} else
reprocess = iterator->second->EmitToken(inToken);
reprocessCount++;
} while (reprocess);
}
void
TreeConstructor::EmitCharacterToken(char character) {
HTML::Tokenizer::CharacterToken token(character);
EmitToken(token);
}
void
TreeConstructor::EmitDoctypeQuirksToken() {
HTML::Tokenizer::DoctypeToken token;
token.forceQuirks = true;
EmitToken(token);
}
void
TreeConstructor::EmitEOFToken() {
HTML::Tokenizer::EOFToken token;
EmitToken(token);
}
/**
* Spec:
* https://dom.spec.whatwg.org/#concept-create-element
*/
std::shared_ptr<DOM::Element>
TreeConstructor::CreateElement(const Unicode::UString &localName, const Unicode::UString &nameSpace,
std::optional<Unicode::UString> prefix, std::optional<Unicode::UString> is,
bool synchronousCustomElementsFlag) {
// TODO Check for custom element definition
auto element = std::make_shared<DOM::Element>();
element->namespaceURI = nameSpace;
if (prefix.has_value()) {
element->namespacePrefix = std::move(prefix.value());
}
element->localName = localName;
element->customElementState = DOM::CustomElementState::UNCUSTOMIZED;
if (is.has_value()) {
element->is = std::move(is.value());
}
// TODO 7.3
(void)synchronousCustomElementsFlag;
return element;
}
/**
* Spec:
* https://html.spec.whatwg.org/multipage/parsing.html#create-an-element-for-the-token
*/
std::shared_ptr<DOM::Element>
TreeConstructor::CreateElementForToken(
HTML::Tokenizer::StartTagToken &tagToken, const Unicode::UString &nameSpace) {
std::optional<Unicode::UString> is;
auto attr = std::find_if(std::begin(tagToken.GetAttributes()), std::end(tagToken.GetAttributes()),
[](const auto &attr) { return attr.first.EqualsIgnoreCaseA(2, "is"); });
if (attr != std::end(tagToken.GetAttributes()))
is = {attr->second};
auto element = CreateElement(tagToken.tagName, nameSpace, {}, is, executeScript);
/* Maybe, because the lifetime of the Token ends here, we can do a swap
* between the tokens attributes and the elements internalAttributes?
*
* Also, the appending of attributes to an element is not
* straightforward:
* https://dom.spec.whatwg.org/#concept-element-attributes-change-ext
*/
element->internalAttributes.insert(std::begin(tagToken.GetAttributes()), std::end(tagToken.GetAttributes()));
/** TODO xmlns attribute check */
auto resettableElement = std::dynamic_pointer_cast<DOM::ResettableElement>(element);
if (resettableElement)
resettableElement->reset();
/** TODO Form-associated element */
return element;
}
// Spec:
// https://html.spec.whatwg.org/multipage/parsing.html#appropriate-place-for-inserting-a-node
void
TreeConstructor::InsertNodeInAppropriateLocation(
const std::shared_ptr<DOM::Node> &node, std::optional<std::shared_ptr<DOM::Node>> overrideTarget) {
/* TODO Foster parenting */
if (overrideTarget.has_value()) {
overrideTarget.value()->childNodes.push_back(node);
} else if (!openElementsStack.empty()) {
openElementsStack.back()->childNodes.push_back(node);
} else {
/* No nodes in the openElementsStack, thus use Document (this
* shouldn't be in the stack of open elements, states the standard implicitly:
* https://html.spec.whatwg.org/multipage/parsing.html#stack-of-open-elements
*/
context.parserContext.documentNode->childNodes.push_back(node);
}
}
/**
* Spec:
* https://html.spec.whatwg.org/multipage/parsing.html#insert-a-foreign-element
*/
std::shared_ptr<DOM::Element>
TreeConstructor::InsertForeignElement(HTML::Tokenizer::StartTagToken &token, const Unicode::UString &nameSpace) {
auto element = CreateElementForToken(token, nameSpace);
InsertNodeInAppropriateLocation(element);
/* ... Element reactions ... */
openElementsStack.push_back(element);
return element;
}
std::shared_ptr<DOM::Element>
TreeConstructor::InsertElement(const Unicode::UString &tagName, const Unicode::UString &nameSpace,
const std::map<Unicode::UString, Unicode::UString> &attributes) {
/* https://html.spec.whatwg.org/multipage/parsing.html#create-an-element-for-the-token */
auto element = std::make_shared<DOM::Element>();
element->namespaceURI = nameSpace;
element->localName = tagName;
context.parserContext.documentNode->children.push_back(element);
openElementsStack.push_back(element);
(void)attributes;
return element;
}
/**
* Spec:
* https://html.spec.whatwg.org/multipage/parsing.html#insert-a-comment
*/
void
TreeConstructor::InsertComment(Unicode::UString &contents) {
InsertNodeInAppropriateLocation(std::make_shared<DOM::Comment>(contents));
}
} // namespace HTML