1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Debug.h>
#include <AK/StringBuilder.h>
#include <LibJS/Parser.h>
#include <LibTextCodec/Decoder.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Event.h>
#include <LibWeb/DOM/ShadowRoot.h>
#include <LibWeb/DOM/Text.h>
#include <LibWeb/HTML/EventNames.h>
#include <LibWeb/HTML/HTMLScriptElement.h>
#include <LibWeb/Loader/ResourceLoader.h>
namespace Web::HTML {
HTMLScriptElement::HTMLScriptElement(DOM::Document& document, QualifiedName qualified_name)
: HTMLElement(document, move(qualified_name))
, m_script_filename("(document)")
{
}
HTMLScriptElement::~HTMLScriptElement()
{
}
void HTMLScriptElement::set_parser_document(Badge<HTMLDocumentParser>, DOM::Document& document)
{
m_parser_document = document;
}
void HTMLScriptElement::set_non_blocking(Badge<HTMLDocumentParser>, bool non_blocking)
{
m_non_blocking = non_blocking;
}
void HTMLScriptElement::execute_script()
{
if (m_preparation_time_document.ptr() != &document()) {
dbgln("HTMLScriptElement: Refusing to run script because the preparation time document is not the same as the node document.");
return;
}
if (m_script_source.is_null()) {
dbgln("HTMLScriptElement: Refusing to run script because the script source is null.");
dispatch_event(DOM::Event::create(HTML::EventNames::error));
return;
}
bool incremented_destructive_writes_counter = false;
if (m_from_an_external_file || m_script_type == ScriptType::Module) {
document().increment_ignore_destructive_writes_counter();
incremented_destructive_writes_counter = true;
}
if (m_script_type == ScriptType::Classic) {
auto old_current_script = document().current_script();
if (!is<DOM::ShadowRoot>(root()))
document().set_current_script({}, this);
else
document().set_current_script({}, nullptr);
if (m_from_an_external_file)
dbgln_if(HTML_SCRIPT_DEBUG, "HTMLScriptElement: Running script {}", attribute(HTML::AttributeNames::src));
else
dbgln_if(HTML_SCRIPT_DEBUG, "HTMLScriptElement: Running inline script");
document().run_javascript(m_script_source, m_script_filename);
document().set_current_script({}, old_current_script);
} else {
VERIFY(!document().current_script());
TODO();
}
if (incremented_destructive_writes_counter)
document().decrement_ignore_destructive_writes_counter();
if (m_from_an_external_file)
dispatch_event(DOM::Event::create(HTML::EventNames::load));
}
// https://mimesniff.spec.whatwg.org/#javascript-mime-type-essence-match
static bool is_javascript_mime_type_essence_match(const String& string)
{
auto lowercase_string = string.to_lowercase();
return lowercase_string.is_one_of("application/ecmascript", "application/javascript", "application/x-ecmascript", "application/x-javascript", "text/ecmascript", "text/javascript", "text/javascript1.0", "text/javascript1.1", "text/javascript1.2", "text/javascript1.3", "text/javascript1.4", "text/javascript1.5", "text/jscript", "text/livescript", "text/x-ecmascript", "text/x-javascript");
}
// https://html.spec.whatwg.org/multipage/scripting.html#prepare-a-script
void HTMLScriptElement::prepare_script()
{
if (m_already_started) {
dbgln("HTMLScriptElement: Refusing to run script because it has already started.");
return;
}
RefPtr<DOM::Document> parser_document = m_parser_document.ptr();
m_parser_document = nullptr;
if (parser_document && !has_attribute(HTML::AttributeNames::async)) {
m_non_blocking = true;
}
auto source_text = child_text_content();
if (!has_attribute(HTML::AttributeNames::src) && source_text.is_empty()) {
dbgln("HTMLScriptElement: Refusing to run empty script.");
return;
}
if (!is_connected()) {
dbgln("HTMLScriptElement: Refusing to run script because the element is not connected.");
return;
}
String script_block_type;
bool has_type = has_attribute(HTML::AttributeNames::type);
bool has_language = has_attribute(HTML::AttributeNames::language);
if ((has_type && attribute(HTML::AttributeNames::type).is_empty())
|| (!has_type && has_language && attribute(HTML::AttributeNames::language).is_empty())
|| (!has_type && !has_language)) {
script_block_type = "text/javascript";
} else if (has_type) {
script_block_type = attribute(HTML::AttributeNames::type).trim_whitespace();
} else if (!attribute(HTML::AttributeNames::language).is_empty()) {
script_block_type = String::formatted("text/{}", attribute(HTML::AttributeNames::language));
}
if (is_javascript_mime_type_essence_match(script_block_type)) {
m_script_type = ScriptType::Classic;
} else if (script_block_type.equals_ignoring_case("module")) {
m_script_type = ScriptType::Module;
} else {
dbgln("HTMLScriptElement: Refusing to run script because the type '{}' is not recognized.", script_block_type);
return;
}
if (parser_document) {
m_parser_document = *parser_document;
m_non_blocking = false;
}
m_already_started = true;
m_preparation_time_document = document();
if (parser_document && parser_document.ptr() != m_preparation_time_document.ptr()) {
dbgln("HTMLScriptElement: Refusing to run script because the parser document is not the same as the preparation time document.");
return;
}
if (is_scripting_disabled()) {
dbgln("HTMLScriptElement: Refusing to run script because scripting is disabled.");
return;
}
if (m_script_type == ScriptType::Classic && has_attribute(HTML::AttributeNames::nomodule)) {
dbgln("HTMLScriptElement: Refusing to run classic script because it has the nomodule attribute.");
return;
}
// FIXME: Check CSP
if (m_script_type == ScriptType::Classic && has_attribute(HTML::AttributeNames::event) && has_attribute(HTML::AttributeNames::for_)) {
auto for_ = attribute(HTML::AttributeNames::for_).trim_whitespace();
auto event = attribute(HTML::AttributeNames::event).trim_whitespace();
if (!for_.equals_ignoring_case("window")) {
dbgln("HTMLScriptElement: Refusing to run classic script because the provided 'for' attribute is not equal to 'window'");
return;
}
if (!event.equals_ignoring_case("onload") && !event.equals_ignoring_case("onload()")) {
dbgln("HTMLScriptElement: Refusing to run classic script because the provided 'event' attribute is not equal to 'onload' or 'onload()'");
return;
}
}
// FIXME: Check "charset" attribute
// FIXME: Check CORS
// FIXME: Module script credentials mode
// FIXME: Cryptographic nonce
// FIXME: Check "integrity" attribute
// FIXME: Check "referrerpolicy" attribute
// FIXME: Check fetch options
if (has_attribute(HTML::AttributeNames::src)) {
auto src = attribute(HTML::AttributeNames::src);
if (src.is_empty()) {
dbgln("HTMLScriptElement: Refusing to run script because the src attribute is empty.");
// FIXME: Queue a task to do this.
dispatch_event(DOM::Event::create(HTML::EventNames::error));
return;
}
m_from_an_external_file = true;
auto url = document().complete_url(src);
if (!url.is_valid()) {
dbgln("HTMLScriptElement: Refusing to run script because the src URL '{}' is invalid.", url);
// FIXME: Queue a task to do this.
dispatch_event(DOM::Event::create(HTML::EventNames::error));
return;
}
if (m_script_type == ScriptType::Classic) {
auto request = LoadRequest::create_for_url_on_page(url, document().page());
// FIXME: This load should be made asynchronous and the parser should spin an event loop etc.
m_script_filename = url.to_string();
ResourceLoader::the().load_sync(
request,
[this, url](auto data, auto&, auto) {
if (data.is_null()) {
dbgln("HTMLScriptElement: Failed to load {}", url);
return;
}
m_script_source = String::copy(data);
script_became_ready();
},
[this](auto&, auto) {
m_failed_to_load = true;
});
} else {
TODO();
}
} else {
if (m_script_type == ScriptType::Classic) {
m_script_source = source_text;
script_became_ready();
} else {
TODO();
}
}
if ((m_script_type == ScriptType::Classic && has_attribute(HTML::AttributeNames::src) && has_attribute(HTML::AttributeNames::defer) && is_parser_inserted() && !has_attribute(HTML::AttributeNames::async))
|| (m_script_type == ScriptType::Module && is_parser_inserted() && !has_attribute(HTML::AttributeNames::async))) {
document().add_script_to_execute_when_parsing_has_finished({}, *this);
when_the_script_is_ready([this] {
m_ready_to_be_parser_executed = true;
});
}
else if (m_script_type == ScriptType::Classic && has_attribute(HTML::AttributeNames::src) && is_parser_inserted() && !has_attribute(HTML::AttributeNames::async)) {
document().set_pending_parsing_blocking_script({}, this);
when_the_script_is_ready([this] {
m_ready_to_be_parser_executed = true;
});
}
else if ((m_script_type == ScriptType::Classic && has_attribute(HTML::AttributeNames::src) && !has_attribute(HTML::AttributeNames::async) && !m_non_blocking)
|| (m_script_type == ScriptType::Module && !has_attribute(HTML::AttributeNames::async) && !m_non_blocking)) {
m_preparation_time_document->add_script_to_execute_as_soon_as_possible({}, *this);
// FIXME: When the script is ready, run the following steps:
//
// If the element is not now the first element in the list of scripts
// that will execute in order as soon as possible to which it was added above,
// then mark the element as ready but return without executing the script yet.
//
// Execution: Execute the script block corresponding to the first script element
// in this list of scripts that will execute in order as soon as possible.
//
// Remove the first element from this list of scripts that will execute in order
// as soon as possible.
//
// If this list of scripts that will execute in order as soon as possible is still
// not empty and the first entry has already been marked as ready, then jump back
// to the step labeled execution.
}
else if ((m_script_type == ScriptType::Classic && has_attribute(HTML::AttributeNames::src)) || m_script_type == ScriptType::Module) {
// FIXME: This should add to a set, not a list.
m_preparation_time_document->add_script_to_execute_as_soon_as_possible({}, *this);
// FIXME: When the script is ready, execute the script block and then remove the element
// from the set of scripts that will execute as soon as possible.
}
// FIXME: If the element does not have a src attribute, and the element is "parser-inserted",
// and either the parser that created the script is an XML parser or it's an HTML parser
// whose script nesting level is not greater than one, and the element's parser document
// has a style sheet that is blocking scripts:
// The element is the pending parsing-blocking script of its parser document.
// (There can only be one such script per Document at a time.)
// Set the element's "ready to be parser-executed" flag. The parser will handle executing the script.
else {
// Immediately execute the script block, even if other scripts are already executing.
execute_script();
}
}
void HTMLScriptElement::script_became_ready()
{
m_script_ready = true;
if (!m_script_ready_callback)
return;
m_script_ready_callback();
m_script_ready_callback = nullptr;
}
void HTMLScriptElement::when_the_script_is_ready(Function<void()> callback)
{
if (m_script_ready) {
callback();
return;
}
m_script_ready_callback = move(callback);
}
void HTMLScriptElement::inserted()
{
if (!is_parser_inserted()) {
// FIXME: Only do this if the element was previously not connected.
if (is_connected()) {
prepare_script();
}
}
HTMLElement::inserted();
}
}
|