1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 22:14:58 +00:00

LibWeb: Add support for type module in HTMLScriptElement

This patch adds support for script elements with the type attribute set
to "module". As a first cut the changes are mainly focused around inline
scripts.

Co-authored-by: davidot <davidot@serenityos.org>
This commit is contained in:
networkException 2022-10-03 21:08:02 +02:00 committed by Andreas Kling
parent c51cf66347
commit f92d95224e

View file

@ -1,5 +1,6 @@
/* /*
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org> * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2022, networkException <networkexception@serenityos.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -15,6 +16,7 @@
#include <LibWeb/HTML/EventNames.h> #include <LibWeb/HTML/EventNames.h>
#include <LibWeb/HTML/HTMLScriptElement.h> #include <LibWeb/HTML/HTMLScriptElement.h>
#include <LibWeb/HTML/Scripting/ClassicScript.h> #include <LibWeb/HTML/Scripting/ClassicScript.h>
#include <LibWeb/HTML/Scripting/Fetching.h>
#include <LibWeb/Infra/CharacterTypes.h> #include <LibWeb/Infra/CharacterTypes.h>
#include <LibWeb/Loader/ResourceLoader.h> #include <LibWeb/Loader/ResourceLoader.h>
#include <LibWeb/MimeSniff/MimeType.h> #include <LibWeb/MimeSniff/MimeType.h>
@ -47,6 +49,8 @@ void HTMLScriptElement::begin_delaying_document_load_event(DOM::Document& docume
// https://html.spec.whatwg.org/multipage/scripting.html#execute-the-script-block // https://html.spec.whatwg.org/multipage/scripting.html#execute-the-script-block
void HTMLScriptElement::execute_script() void HTMLScriptElement::execute_script()
{ {
// FIXME: Update the steps to match current spec.
// 1. Let document be scriptElement's node document. // 1. Let document be scriptElement's node document.
JS::NonnullGCPtr<DOM::Document> node_document = document(); JS::NonnullGCPtr<DOM::Document> node_document = document();
@ -96,8 +100,8 @@ void HTMLScriptElement::execute_script()
// 1. Assert: document's currentScript attribute is null. // 1. Assert: document's currentScript attribute is null.
VERIFY(!document().current_script()); VERIFY(!document().current_script());
// FIXME: 2. Run the module script given by the script's script for scriptElement. // 2. Run the module script given by the script's script for scriptElement.
TODO(); (void)verify_cast<JavaScriptModuleScript>(*m_script).run();
} }
// 6. Decrement the ignore-destructive-writes counter of document, if it was incremented in the earlier step. // 6. Decrement the ignore-destructive-writes counter of document, if it was incremented in the earlier step.
@ -112,6 +116,8 @@ void HTMLScriptElement::execute_script()
// https://html.spec.whatwg.org/multipage/scripting.html#prepare-a-script // https://html.spec.whatwg.org/multipage/scripting.html#prepare-a-script
void HTMLScriptElement::prepare_script() void HTMLScriptElement::prepare_script()
{ {
// FIXME: Update the steps to match current spec.
// 1. If the script element is marked as having "already started", then return. The script is not executed. // 1. If the script element is marked as having "already started", then return. The script is not executed.
if (m_already_started) { if (m_already_started) {
dbgln("HTMLScriptElement: Refusing to run script because it has already started."); dbgln("HTMLScriptElement: Refusing to run script because it has already started.");
@ -293,21 +299,26 @@ void HTMLScriptElement::prepare_script()
auto resource = ResourceLoader::the().load_resource(Resource::Type::Generic, request); auto resource = ResourceLoader::the().load_resource(Resource::Type::Generic, request);
set_resource(resource); set_resource(resource);
} else if (m_script_type == ScriptType::Module) { } else if (m_script_type == ScriptType::Module) {
// FIXME: -> "module" // Fetch an external module script graph given url, settings object, options, and onComplete.
// Fetch an external module script graph given url, settings object, and options. // FIXME: Pass options.
fetch_external_module_script_graph(url, document().relevant_settings_object(), [this](auto const* result) {
// 1. Mark as ready el given result.
m_script = result;
script_became_ready();
});
} }
} else { } else {
// 27. If the element does not have a src content attribute, run these substeps: // 27. If the element does not have a src content attribute, run these substeps:
// FIXME: 1. Let base URL be the script element's node document's document base URL. // 1. Let base URL be the script element's node document's document base URL.
auto base_url = m_document->base_url();
// 2. Switch on the script's type: // 2. Switch on the script's type:
if (m_script_type == ScriptType::Classic) { if (m_script_type == ScriptType::Classic) {
// -> "classic" // -> "classic"
// 1. Let script be the result of creating a classic script using source text, settings object, base URL, and options. // 1. Let script be the result of creating a classic script using source text, settings object, base URL, and options.
// FIXME: Pass options.
// FIXME: Pass settings, base URL and options. auto script = ClassicScript::create(m_document->url().to_string(), source_text, document().relevant_settings_object(), base_url, m_source_line_number);
auto script = ClassicScript::create(m_document->url().to_string(), source_text, document().relevant_settings_object(), AK::URL(), m_source_line_number);
// 2. Set the script's script to script. // 2. Set the script's script to script.
m_script = script; m_script = script;
@ -315,10 +326,15 @@ void HTMLScriptElement::prepare_script()
// 3. The script is ready. // 3. The script is ready.
script_became_ready(); script_became_ready();
} else if (m_script_type == ScriptType::Module) { } else if (m_script_type == ScriptType::Module) {
// FIXME: -> "module" // FIXME: 1. Set el's delaying the load event to true.
// 1. Fetch an inline module script graph, given source text, base URL, settings object, and options.
// When this asynchronously completes, set the script's script to the result. At that time, the script is ready. // 2. Fetch an inline module script graph, given source text, base URL, settings object, options, and with the following steps given result:
TODO(); // FIXME: Pass options
fetch_inline_module_script_graph(m_document->url().to_string(), source_text, base_url, document().relevant_settings_object(), [this](auto const* result) {
// 1. Mark as ready el given result.
m_script = result;
script_became_ready();
});
} }
} }