Truly independent web browser
LibWeb+Meta: Move script element processing into a shared base class
Tim Ledbetterb8c37c86b9f8
diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txtindex 1f57801dded..50804f155e8 100644--- a/Libraries/LibWeb/CMakeLists.txt+++ b/Libraries/LibWeb/CMakeLists.txt@@ -482,6 +482,7 @@ set(SOURCESHTML/HTMLOptionElement.cppHTML/HTMLOptionsCollection.cppHTML/HTMLOrSVGOrMathMLElement.cpp+ HTML/HTMLOrSVGScriptElement.cppHTML/HTMLOutputElement.cppHTML/HTMLParagraphElement.cppHTML/HTMLParamElement.cppdiff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cppindex 3fce33625d5..d73806180fa 100644--- a/Libraries/LibWeb/DOM/Document.cpp+++ b/Libraries/LibWeb/DOM/Document.cpp@@ -1654,12 +1654,12 @@ WebIDL::ExceptionOr<Document*> Document::open(Optional<Utf16String> const&, Opti// parser script runner's pending parsing-blocking and deferred scripts when replacing the parser, and// Gecko cancels pending parser script loads when terminating the parser. Stop discarded scripts from// delaying the replacement document's load event before removing them from the parser's queues.- if (m_pending_parsing_blocking_script)- m_pending_parsing_blocking_script->stop_delaying_document_load_event({});+ if (auto* pending_parsing_blocking_script = this->pending_parsing_blocking_script())+ pending_parsing_blocking_script->stop_delaying_document_load_event({});if (m_pending_parsing_blocking_svg_script)m_pending_parsing_blocking_svg_script->stop_delaying_document_load_event({});for (auto& script : m_scripts_to_execute_when_parsing_has_finished)- script->stop_delaying_document_load_event({});+ as<HTML::HTMLOrSVGScriptElement>(*script).stop_delaying_document_load_event({});m_pending_parsing_blocking_script = nullptr;m_pending_parsing_blocking_svg_script = nullptr;m_scripts_to_execute_when_parsing_has_finished.clear();@@ -4098,17 +4098,24 @@ WebIDL::ExceptionOr<GC::Ref<Event>> Document::create_event(Utf16FlyString const&return GC::Ref(*event);}-void Document::set_pending_parsing_blocking_script(HTML::HTMLScriptElement* script)+void Document::set_pending_parsing_blocking_script(HTML::HTMLOrSVGScriptElement* script){- m_pending_parsing_blocking_script = script;+ m_pending_parsing_blocking_script = script ? &script->as_element() : nullptr;}-GC::Ref<HTML::HTMLScriptElement> Document::take_pending_parsing_blocking_script(Badge<HTML::HTMLParser>)+HTML::HTMLOrSVGScriptElement* Document::pending_parsing_blocking_script()+{+ if (!m_pending_parsing_blocking_script)+ return nullptr;+ return &as<HTML::HTMLOrSVGScriptElement>(*m_pending_parsing_blocking_script);+}++HTML::HTMLOrSVGScriptElement& Document::take_pending_parsing_blocking_script(Badge<HTML::HTMLParser>){VERIFY(m_pending_parsing_blocking_script);auto script = m_pending_parsing_blocking_script;m_pending_parsing_blocking_script = nullptr;- return *script;+ return as<HTML::HTMLOrSVGScriptElement>(*script);}void Document::set_pending_parsing_blocking_svg_script(SVG::SVGScriptElement* script)@@ -4124,9 +4131,18 @@ GC::Ref<SVG::SVGScriptElement> Document::take_pending_parsing_blocking_svg_scripreturn *script;}-void Document::add_script_to_execute_when_parsing_has_finished(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement& script)+Variant<GC::Ref<HTML::HTMLScriptElement>, GC::Ref<SVG::SVGScriptElement>, Empty> Document::current_script_for_bindings() const+{+ if (auto* html_script_element = as_if<HTML::HTMLScriptElement>(m_current_script.ptr()))+ return GC::Ref { *html_script_element };+ if (auto* svg_script_element = as_if<SVG::SVGScriptElement>(m_current_script.ptr()))+ return GC::Ref { *svg_script_element };+ return Empty {};+}++void Document::add_script_to_execute_when_parsing_has_finished(Badge<HTML::HTMLOrSVGScriptElement>, HTML::HTMLOrSVGScriptElement& script){- m_scripts_to_execute_when_parsing_has_finished.append(script);+ m_scripts_to_execute_when_parsing_has_finished.append(script.as_element());}// https://dom.spec.whatwg.org/#dom-document-importnodediff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.hindex 118ee5812b5..38efd101ab5 100644--- a/Libraries/LibWeb/DOM/Document.h+++ b/Libraries/LibWeb/DOM/Document.h@@ -735,9 +735,9 @@ public:WebIDL::ExceptionOr<GC::Ref<Event>> create_event(Utf16FlyString const& interface);GC::Ref<Range> create_range();- void set_pending_parsing_blocking_script(HTML::HTMLScriptElement*);- HTML::HTMLScriptElement* pending_parsing_blocking_script() { return m_pending_parsing_blocking_script.ptr(); }- GC::Ref<HTML::HTMLScriptElement> take_pending_parsing_blocking_script(Badge<HTML::HTMLParser>);+ void set_pending_parsing_blocking_script(HTML::HTMLOrSVGScriptElement*);+ HTML::HTMLOrSVGScriptElement* pending_parsing_blocking_script();+ HTML::HTMLOrSVGScriptElement& take_pending_parsing_blocking_script(Badge<HTML::HTMLParser>);void set_pending_parsing_blocking_svg_script(SVG::SVGScriptElement*);SVG::SVGScriptElement* pending_parsing_blocking_svg_script() { return m_pending_parsing_blocking_svg_script.ptr(); }@@ -747,12 +747,12 @@ public:return m_pending_parsing_blocking_script.ptr() || m_pending_parsing_blocking_svg_script.ptr();}- void add_script_to_execute_when_parsing_has_finished(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement&);- Vector<GC::Ref<HTML::HTMLScriptElement>>& scripts_to_execute_when_parsing_has_finished() { return m_scripts_to_execute_when_parsing_has_finished; }+ void add_script_to_execute_when_parsing_has_finished(Badge<HTML::HTMLOrSVGScriptElement>, HTML::HTMLOrSVGScriptElement&);+ Vector<GC::Ref<Element>>& scripts_to_execute_when_parsing_has_finished() { return m_scripts_to_execute_when_parsing_has_finished; }- Vector<GC::Ref<HTML::HTMLScriptElement>>& scripts_to_execute_as_soon_as_possible() { return m_scripts_to_execute_as_soon_as_possible; }+ Vector<GC::Ref<Element>>& scripts_to_execute_as_soon_as_possible() { return m_scripts_to_execute_as_soon_as_possible; }- Vector<GC::Ref<HTML::HTMLScriptElement>>& scripts_to_execute_in_order_as_soon_as_possible() { return m_scripts_to_execute_in_order_as_soon_as_possible; }+ Vector<GC::Ref<Element>>& scripts_to_execute_in_order_as_soon_as_possible() { return m_scripts_to_execute_in_order_as_soon_as_possible; }HTML::ScriptRegistry& script_registry() { return m_script_registry; }HTML::ScriptRegistry const& script_registry() const { return m_script_registry; }@@ -946,8 +946,9 @@ public:// has completely loaded, the requests it makes without a cache mode of their own bypass the cache too.bool is_loading_bypassing_cache() const { return m_navigation_bypassed_cache && !is_completely_loaded(); }- GC::Ptr<HTML::HTMLScriptElement> current_script() const { return m_current_script.ptr(); }- void set_current_script(Badge<HTML::HTMLScriptElement>, GC::Ptr<HTML::HTMLScriptElement> script) { m_current_script = move(script); }+ GC::Ptr<Element> current_script() const { return m_current_script; }+ Variant<GC::Ref<HTML::HTMLScriptElement>, GC::Ref<SVG::SVGScriptElement>, Empty> current_script_for_bindings() const;+ void set_current_script(Badge<HTML::HTMLOrSVGScriptElement>, GC::Ptr<Element> script) { m_current_script = move(script); }static constexpr size_t current_script_offset() { return offsetof(Document, m_current_script); }u32 ignore_destructive_writes_counter() const { return m_ignore_destructive_writes_counter; }@@ -2064,16 +2065,16 @@ private:Utf16String m_source;- GC::Ptr<HTML::HTMLScriptElement> m_pending_parsing_blocking_script;+ GC::Ptr<Element> m_pending_parsing_blocking_script;GC::Ptr<SVG::SVGScriptElement> m_pending_parsing_blocking_svg_script;- Vector<GC::Ref<HTML::HTMLScriptElement>> m_scripts_to_execute_when_parsing_has_finished;+ Vector<GC::Ref<Element>> m_scripts_to_execute_when_parsing_has_finished;// https://html.spec.whatwg.org/multipage/scripting.html#list-of-scripts-that-will-execute-in-order-as-soon-as-possible- Vector<GC::Ref<HTML::HTMLScriptElement>> m_scripts_to_execute_in_order_as_soon_as_possible;+ Vector<GC::Ref<Element>> m_scripts_to_execute_in_order_as_soon_as_possible;// https://html.spec.whatwg.org/multipage/scripting.html#set-of-scripts-that-will-execute-as-soon-as-possible- Vector<GC::Ref<HTML::HTMLScriptElement>> m_scripts_to_execute_as_soon_as_possible;+ Vector<GC::Ref<Element>> m_scripts_to_execute_as_soon_as_possible;HTML::ScriptRegistry m_script_registry;@@ -2166,7 +2167,7 @@ private:bool m_text_directive_user_activation { false };bool m_browsing_context_group_has_remote_contexts { false };bool m_navigation_bypassed_cache { false };- GC::Ptr<HTML::HTMLScriptElement> m_current_script;+ GC::Ptr<Element> m_current_script;u32 m_ignore_destructive_writes_counter { 0 };diff --git a/Libraries/LibWeb/DOM/Document.idl b/Libraries/LibWeb/DOM/Document.idlindex 96e8eb6c409..b02af3ca3ee 100644--- a/Libraries/LibWeb/DOM/Document.idl+++ b/Libraries/LibWeb/DOM/Document.idl@@ -83,8 +83,7 @@ partial interface Document {[SameObject] readonly attribute HTMLCollection forms;[SameObject] readonly attribute HTMLCollection scripts;NodeList getElementsByName(DOMString elementName);- // FIXME: currentScript should be a HTMLOrSVGScriptElement.- [DirectGetter] readonly attribute HTMLScriptElement? currentScript; // classic scripts in a document tree only+ [DirectGetter, ImplementedAs=current_script_for_bindings] readonly attribute HTMLOrSVGScriptElement? currentScript; // classic scripts in a document tree only// dynamic markup insertion[CEReactions] Document open(optional DOMString unused1, optional DOMString unused2); // both arguments are ignored@@ -115,6 +114,7 @@ partial interface Document {enum DocumentReadyState { "loading", "interactive", "complete" };enum DocumentVisibilityState { "visible", "hidden" };+typedef (HTMLScriptElement or SVGScriptElement) HTMLOrSVGScriptElement;// https://html.spec.whatwg.org/multipage/obsolete.html#Document-partialpartial interface Document {diff --git a/Libraries/LibWeb/Forward.h b/Libraries/LibWeb/Forward.hindex baee4975648..256ed4f463f 100644--- a/Libraries/LibWeb/Forward.h+++ b/Libraries/LibWeb/Forward.h@@ -723,6 +723,7 @@ class HTMLOListElement;class HTMLOptGroupElement;class HTMLOptionElement;class HTMLOptionsCollection;+class HTMLOrSVGScriptElement;class HTMLOutputElement;class HTMLParagraphElement;class HTMLParamElement;diff --git a/Libraries/LibWeb/HTML/HTMLOrSVGScriptElement.cpp b/Libraries/LibWeb/HTML/HTMLOrSVGScriptElement.cppnew file mode 100644index 00000000000..6d230599a68--- /dev/null+++ b/Libraries/LibWeb/HTML/HTMLOrSVGScriptElement.cpp@@ -0,0 +1,721 @@+/*+ * Copyright (c) 2018-2021, Andreas Kling <[email protected]>+ * Copyright (c) 2022, networkException <[email protected]>+ * Copyright (c) 2026, Tim Ledbetter <[email protected]>+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <AK/Debug.h>+#include <AK/Utf16StringBuilder.h>+#include <LibTextCodec/Decoder.h>+#include <LibWeb/ContentSecurityPolicy/BlockingAlgorithms.h>+#include <LibWeb/ContentSecurityPolicy/Directives/HTMLIntegration.h>+#include <LibWeb/DOM/Document.h>+#include <LibWeb/DOM/Event.h>+#include <LibWeb/DOM/ShadowRoot.h>+#include <LibWeb/HTML/AttributeNames.h>+#include <LibWeb/HTML/EventNames.h>+#include <LibWeb/HTML/HTMLOrSVGScriptElement.h>+#include <LibWeb/HTML/HTMLScriptElement.h>+#include <LibWeb/HTML/Scripting/ClassicScript.h>+#include <LibWeb/HTML/Scripting/Environments.h>+#include <LibWeb/HTML/Scripting/Fetching.h>+#include <LibWeb/HTML/Scripting/ImportMapParseResult.h>+#include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>+#include <LibWeb/HTML/Window.h>+#include <LibWeb/HighResolutionTime/TimeOrigin.h>+#include <LibWeb/TrustedTypes/RequireTrustedTypesForDirective.h>+#include <LibWeb/TrustedTypes/TrustedTypePolicy.h>+#include <LibWebCommon/Infra/CharacterTypes.h>+#include <LibWebCommon/Infra/Strings.h>+#include <LibWebCommon/MimeSniff/MimeType.h>++namespace Web::HTML {++static GC::Ref<DOM::Event> create_event_for_element(DOM::Element& element, Utf16FlyString const& event_name)+{+ return DOM::Event::create(event_name, HighResolutionTime::current_high_resolution_time(relevant_global_object(element)));+}++void HTMLOrSVGScriptElement::visit_script_element_edges(GC::Cell::Visitor& visitor)+{+ m_result.visit(+ [](ResultState::Uninitialized) { },+ [](ResultState::Null) { },+ [&]<typename T>(GC::Ref<T> result) { visitor.visit(result); });+ visitor.visit(m_parser_document);+ visitor.visit(m_preparation_time_document);+}++void HTMLOrSVGScriptElement::script_element_attribute_changed(Utf16FlyString const& name)+{+ // https://html.spec.whatwg.org/multipage/scripting.html#script-processing-model:script-force-async+ // When an async attribute is added to a script element el, the user agent must set el's force async to false.+ if (name == HTML::AttributeNames::async)+ m_force_async = false;+}++// https://www.w3.org/TR/trusted-types/#prepare-script-text+WebIDL::ExceptionOr<void> HTMLOrSVGScriptElement::prepare_script_text()+{+ auto& element = as_element();++ // 1. Let sink be "HTMLScriptElement text" if script is an HTMLScriptElement; otherwise "SVGScriptElement text".+ constexpr auto sink = TrustedTypes::InjectionSink::HTMLScriptElement_text;++ // 2. If script’s script text value is not equal to its child text content,+ // set script’s script text to the result of executing get Trusted Type compliant string, with the following arguments:+ // TrustedScriptURL as expectedType,+ // script’s Document’s relevant global object as global,+ // script’s child text content attribute value as input,+ // sink,+ // 'script' as sinkGroup.+ if (m_script_text != element.child_text_content()) {+ // INTEROP: Step 2 currently names TrustedScriptURL, but script bodies use the createScript policy callback+ // in other engines, consistently with the script text setters.+ m_script_text = TRY(TrustedTypes::get_trusted_type_compliant_string(+ TrustedTypes::TrustedTypeName::TrustedScript,+ HTML::relevant_global_object(element.document()),+ element.child_text_content(),+ sink,+ TrustedTypes::Script.view()));+ }++ return {};+}++void HTMLOrSVGScriptElement::begin_delaying_document_load_event(DOM::Document& document)+{+ // https://html.spec.whatwg.org/multipage/scripting.html#concept-script-script+ // The user agent must delay the load event of the element's node document until the script is ready.+ m_document_load_event_delayer.emplace(document);+}++// https://html.spec.whatwg.org/multipage/scripting.html#execute-the-script-block+void HTMLOrSVGScriptElement::execute_script()+{+ auto& element = as_element();++ // 1. Let document be el's node document.+ GC::Ref<DOM::Document> document = element.document();++ // 2. If el's preparation-time document is not equal to document, then return.+ if (m_preparation_time_document.ptr() != document.ptr()) {+ dbgln("HTMLScriptElement: Refusing to run script because the preparation time document is not the same as the node document.");+ return;+ }++ // https://html.spec.whatwg.org/multipage/document-lifecycle.html#read-html+ // Before any script execution occurs, the user agent must wait for scripts may run for the newly-created document to be true for document.+ VERIFY(document->ready_to_run_scripts());++ // 3. Unblock rendering on el.+ element.unblock_rendering();++ // 3. If el's result is null, then fire an event named error at el, and return.+ if (m_result.has<ResultState::Null>()) {+ dbgln("HTMLScriptElement: Refusing to run script because the element's result is null.");+ element.dispatch_event(create_event_for_element(element, HTML::EventNames::error));+ return;+ }++ // 5. If el's from an external file is true, or el's type is "module", then increment document's ignore-destructive-writes counter.+ 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;+ }++ // 5. Switch on el's type:+ // -> "classic"+ if (m_script_type == ScriptType::Classic) {+ // 1. Let oldCurrentScript be the value to which document's currentScript object was most recently set.+ auto old_current_script = document->current_script();+ // 2. If el's root is not a shadow root, then set document's currentScript attribute to el. Otherwise, set it to null.+ if (!is<DOM::ShadowRoot>(element.root()))+ document->set_current_script({}, &element);+ else+ document->set_current_script({}, nullptr);++ if (m_from_an_external_file)+ dbgln_if(HTML_SCRIPT_DEBUG, "HTMLScriptElement: Running script {}", source_attribute_value());+ else+ dbgln_if(HTML_SCRIPT_DEBUG, "HTMLScriptElement: Running inline script");++ // 3. Run the classic script given by el's result.+ (void)as<ClassicScript>(*m_result.get<GC::Ref<Script>>()).run();++ // 4. Set document's currentScript attribute to oldCurrentScript.+ document->set_current_script({}, old_current_script);+ }+ // -> "module"+ else if (m_script_type == ScriptType::Module) {+ // 1. Assert: document's currentScript attribute is null.+ VERIFY(document->current_script() == nullptr);++ // 2. Run the module script given by el's result.+ (void)as<ModuleScript>(*m_result.get<GC::Ref<Script>>()).run();+ }+ // -> "importmap"+ else if (m_script_type == ScriptType::ImportMap) {+ HTML::TemporaryExecutionContext execution_context { document->relevant_settings_object() };++ // 1. Register an import map given el's relevant global object and el's result.+ m_result.get<GC::Ref<ImportMapParseResult>>()->register_import_map(relevant_window(element));+ }++ // 7. Decrement the ignore-destructive-writes counter of document, if it was incremented in the earlier step.+ if (incremented_destructive_writes_counter)+ document->decrement_ignore_destructive_writes_counter();++ // 8. If el's from an external file is true, then fire an event named load at el.+ if (m_from_an_external_file)+ element.dispatch_event(create_event_for_element(element, HTML::EventNames::load));+}++// https://html.spec.whatwg.org/multipage/scripting.html#prepare-the-script-element+ScriptFetchOptions script_element_fetch_options(DOM::Element const& element, ScriptElementFetchAttributes const& attributes)+{+ // 25. Let module script credentials mode be the CORS settings attribute credentials mode for el's crossorigin content attribute.+ auto module_script_credential_mode = cors_settings_attribute_credentials_mode(attributes.crossorigin);++ // 26. Let cryptographic nonce be el's [[CryptographicNonce]] internal slot's value.+ // AD-HOC: A script element that is not nonceable gets no nonce. Content Security Policy only checks this for inline+ // scripts, which lets dangling markup take the nonce of a following element to load an external script.+ auto cryptographic_nonce = ContentSecurityPolicy::Directives::is_element_nonceable(element) == ContentSecurityPolicy::Directives::Nonceable::Yes+ ? attributes.cryptographic_nonce+ : Utf16String {};++ // 27. If el has an integrity attribute, then let integrity metadata be that attribute's value.+ // Otherwise, let integrity metadata be the empty string.+ auto integrity_metadata = attributes.integrity.value_or({});++ // 28. Let referrer policy be the current state of el's referrerpolicy content attribute.+ auto referrer_policy = attributes.referrer_policy;++ // 29. Let fetch priority be the current state of el's fetchpriority content attribute.+ auto fetch_priority = attributes.fetch_priority;++ // 30. Let parser metadata be "parser-inserted" if el is parser-inserted, and "not-parser-inserted" otherwise.+ auto parser_metadata = attributes.parser_inserted+ ? Fetch::Infrastructure::Request::ParserMetadata::ParserInserted+ : Fetch::Infrastructure::Request::ParserMetadata::NotParserInserted;++ // 31. Let options be a script fetch options whose cryptographic nonce is cryptographic nonce,+ // integrity metadata is integrity metadata, parser metadata is parser metadata,+ // credentials mode is module script credentials mode, referrer policy is referrer policy,+ // and fetch priority is fetch priority.+ return {+ .cryptographic_nonce = move(cryptographic_nonce),+ .integrity_metadata = move(integrity_metadata),+ .parser_metadata = parser_metadata,+ .credentials_mode = module_script_credential_mode,+ .referrer_policy = referrer_policy,+ .fetch_priority = fetch_priority,+ };+}++// https://w3c.github.io/trusted-types/dist/spec/#slot-value-verification+// https://html.spec.whatwg.org/multipage/scripting.html#prepare-a-script+void HTMLOrSVGScriptElement::prepare_script()+{+ auto& element = as_element();++ // 1. If el's already started is true, then return.+ if (m_already_started)+ return;++ // 2. Let parser document be el's parser document.+ GC::Ptr<DOM::Document> parser_document = m_parser_document;++ // 3. Set el's parser document to null.+ m_parser_document = nullptr;++ // 4. If parser document is non-null and el does not have an async attribute, then set el's force async to true.+ if (parser_document && !element.has_attribute(HTML::AttributeNames::async)) {+ m_force_async = true;+ }++ // NB: The Trusted Types integration prepares the cached script text value before the HTML source-text step.+ if (prepare_script_text().is_exception())+ return;++ // 5. Let source text be el's child text content.+ // NB: The Trusted Types integration supplies this through the cached script text value.+ auto source_text = m_script_text;++ // 6. If el has no src attribute, and source text is the empty string, then return.+ if (!has_source_attribute() && source_text.is_empty()) {+ return;+ }++ // 7. If el is not connected, then return.+ if (!element.is_connected())+ return;++ // 8. If any of the following are true:+ // - el has a type attribute whose value is the empty string;+ // - el has no type attribute but it has a language attribute and that attribute's value is the empty string; or+ // - el has neither a type attribute nor a language attribute+ Utf16String script_block_type;+ auto maybe_type_attribute = element.get_attribute_ns({}, HTML::AttributeNames::type);+ auto maybe_language_attribute = language_attribute_value();+ if ((maybe_type_attribute.has_value() && maybe_type_attribute->is_empty())+ || (!maybe_type_attribute.has_value() && maybe_language_attribute.has_value() && maybe_language_attribute->is_empty())+ || (!maybe_type_attribute.has_value() && !maybe_language_attribute.has_value())) {+ // then let the script block's type string for this script element be "text/javascript".+ script_block_type = "text/javascript"_utf16;+ }+ // Otherwise, if el has a type attribute,+ else if (maybe_type_attribute.has_value()) {+ // then let the script block's type string be the value of that attribute.+ script_block_type = *maybe_type_attribute;+ }+ // Otherwise, el has a non-empty language attribute;+ else if (maybe_language_attribute.has_value() && !maybe_language_attribute->is_empty()) {+ // let the script block's type string be the concatenation of "text/" and the value of el's language attribute.+ Utf16StringBuilder builder;+ builder.append("text/"_utf16);+ builder.append(*maybe_language_attribute);+ script_block_type = builder.to_string();+ }++ // 9. If the script block's type string is a JavaScript MIME type essence match, or el has a type attribute whose+ // value with leading and trailing ASCII whitespace stripped is a JavaScript MIME type essence match,+ if (MimeSniff::is_javascript_mime_type_essence_match(script_block_type)+ || (maybe_type_attribute.has_value() && MimeSniff::is_javascript_mime_type_essence_match(maybe_type_attribute->trim(Infra::ASCII_WHITESPACE)))) {+ // then set el's type to "classic".+ m_script_type = ScriptType::Classic;+ }+ // 10. Otherwise, if the script block's type string is an ASCII case-insensitive match for the string "module",+ else if (script_block_type.equals_ignoring_ascii_case(u"module"sv)) {+ // then set el's type to "module".+ m_script_type = ScriptType::Module;+ }+ // 11. Otherwise, if the script block's type string is an ASCII case-insensitive match for the string "importmap",+ else if (script_block_type.equals_ignoring_ascii_case(u"importmap"sv)) {+ // then set el's type to "importmap".+ m_script_type = ScriptType::ImportMap;+ }+ // FIXME: 12. Otherwise, if the script block's type string is an ASCII case-insensitive match for the string "speculationrules", then set el's type to "speculationrules".+ // 13. Otherwise, return. (No script is executed, and el's type is left as null.)+ else {+ VERIFY(m_script_type == ScriptType::Null);+ return;+ }++ // 14. If parser document is non-null, then set el's parser document back to parser document and set el's force async to false.+ if (parser_document) {+ m_parser_document = parser_document;+ m_force_async = false;+ }++ // 15. Set el's already started to true.+ m_already_started = true;++ // 16. Set el's preparation-time document to its node document.+ m_preparation_time_document = &element.document();++ // 17. If parser document is non-null, and parser document is not equal to el's preparation-time document, then return.+ if (parser_document != nullptr && parser_document != m_preparation_time_document) {+ dbgln("HTMLScriptElement: Refusing to run script because the parser document is not the same as the preparation time document.");+ return;+ }++ // 18. If scripting is disabled for el, then return.+ if (element.is_scripting_disabled()) {+ dbgln("HTMLScriptElement: Refusing to run script because scripting is disabled.");+ return;+ }++ // 19. If el has a nomodule content attribute and its type is "classic", then return.+ if (m_script_type == ScriptType::Classic && has_nomodule_attribute()) {+ dbgln("HTMLScriptElement: Refusing to run classic script because it has the nomodule attribute.");+ return;+ }++ // FIXME: 20. Let cspType be "script speculationrules" if el's type is "speculationrules"; otherwise, "script".++ // 21. If el does not have a src content attribute, and the Should element's inline behavior be blocked by Content+ // Security Policy? algorithm returns "Blocked" when given el, cspType, and source text, then return [CSP]+ if (!has_source_attribute()+ && ContentSecurityPolicy::should_elements_inline_type_behavior_be_blocked_by_content_security_policy(element, ContentSecurityPolicy::Directives::Directive::InlineType::Script, source_text.utf16_view()) == ContentSecurityPolicy::Directives::Directive::Result::Blocked) {+ dbgln("HTMLScriptElement: Refusing to run inline script because it violates the Content Security Policy.");+ return;+ }++ // 22. If el has an event attribute and a for attribute, and el's type is "classic", then:+ if (m_script_type == ScriptType::Classic && event_attribute_value().has_value() && for_attribute_value().has_value()) {+ // 1. Let for be the value of el's' for attribute.+ auto for_ = for_attribute_value().value_or({});++ // 2. Let event be the value of el's event attribute.+ auto event = event_attribute_value().value_or({});++ // 3. Strip leading and trailing ASCII whitespace from event and for.+ for_ = for_.trim_ascii_whitespace();+ event = event.trim_ascii_whitespace();++ // 4. If for is not an ASCII case-insensitive match for the string "window", then return.+ if (!for_.equals_ignoring_ascii_case(u"window"sv)) {+ dbgln("HTMLScriptElement: Refusing to run classic script because the provided 'for' attribute is not equal to 'window'");+ return;+ }++ // 5. If event is not an ASCII case-insensitive match for either the string "onload" or the string "onload()", then return.+ if (!event.equals_ignoring_ascii_case(u"onload"sv)+ && !event.equals_ignoring_ascii_case(u"onload()"sv)) {+ dbgln("HTMLScriptElement: Refusing to run classic script because the provided 'event' attribute is not equal to 'onload' or 'onload()'");+ return;+ }+ }++ // 23. If el has a charset attribute, then let encoding be the result of getting an encoding from the value of the charset attribute.+ // If el does not have a charset attribute, or if getting an encoding failed, then let encoding be el's node document's the encoding.+ Optional<Utf16String> encoding;++ if (auto charset_attribute = charset_attribute_value(); charset_attribute.has_value()) {+ auto charset = TextCodec::get_standardized_encoding(*charset_attribute);+ if (charset.has_value())+ encoding = Utf16String::from_ascii_without_validation(charset->bytes());+ }++ if (!encoding.has_value()) {+ encoding = element.document().encoding_or_default();+ }++ VERIFY(encoding.has_value());++ auto fetch_attributes = this->fetch_attributes();++ // 24. Let classic script CORS setting be the current state of el's crossorigin content attribute.+ auto classic_script_cors_setting = fetch_attributes.crossorigin;++ // 25-31. Let options be a script fetch options for el.+ auto options = script_element_fetch_options(element, fetch_attributes);++ // 32. Let settings object be el's node document's relevant settings object.+ auto& settings_object = element.document().relevant_settings_object();++ // 33. If el has a src content attribute, then:+ if (has_source_attribute()) {+ // 1. If el's type is "importmap" or "speculationrules", then:+ // FIXME: Add "speculationrules" support.+ if (m_script_type == ScriptType::ImportMap) {+ // then queue an element task on the DOM manipulation task source given el to fire an event named error at el, and return.+ element.queue_an_element_task(HTML::Task::Source::DOMManipulation, [&element] {+ element.dispatch_event(create_event_for_element(element, HTML::EventNames::error));+ });+ return;+ }++ // 2. Let src be the value of el's src attribute.+ auto src = source_attribute_value();++ // 3. If src is the empty string, then queue an element task on the DOM manipulation task source given el to fire an event named error at el, and return.+ if (src.is_empty()) {+ dbgln("HTMLScriptElement: Refusing to run script because the src attribute is empty.");+ element.queue_an_element_task(HTML::Task::Source::DOMManipulation, [&element] {+ element.dispatch_event(create_event_for_element(element, HTML::EventNames::error));+ });+ return;+ }++ // 4. Set el's from an external file to true.+ m_from_an_external_file = true;++ // 5. Let url be the result of encoding-parsing a URL given src, relative to el's node document.+ auto url = element.document().encoding_parse_url(src);++ // 6. If url is failure, then queue an element task on the DOM manipulation task source given el to fire an event named error at el, and return.+ if (!url.has_value()) {+ dbgln("HTMLScriptElement: Refusing to run script because the src URL '{}' is invalid.", url);+ element.queue_an_element_task(HTML::Task::Source::DOMManipulation, [&element] {+ element.dispatch_event(create_event_for_element(element, HTML::EventNames::error));+ });+ return;+ }++ // 7. If el is potentially render-blocking, then block rendering on el.+ if (element.is_potentially_render_blocking()) {+ element.block_rendering();+ }++ // 8. Set el's delaying the load event to true.+ begin_delaying_document_load_event(*m_preparation_time_document);++ // 9. If el is currently render-blocking, then set options's render-blocking to true.+ if (element.document().is_render_blocking_element(element))+ options.render_blocking = true;++ // 10. Let onComplete given result be the following steps:+ OnFetchScriptComplete on_complete = create_on_fetch_script_complete(element.heap(), [this](auto result) {+ // 1. Mark as ready el given result.+ if (result)+ mark_as_ready(Result { *result });+ else+ mark_as_ready(ResultState::Null {});+ });++ // 11. Switch on el's type:+ // -> "classic"+ if (m_script_type == ScriptType::Classic) {+ // Fetch a classic script given url, settings object, options, classic script CORS setting, encoding, and onComplete.+ fetch_classic_script(relevant_realm(element), *url, settings_object, move(options), classic_script_cors_setting, encoding.release_value(), on_complete);+ }+ // -> "module"+ else if (m_script_type == ScriptType::Module) {+ // If el does not have an integrity attribute, then set options's integrity metadata to the result of resolving a module integrity metadata with url and settings object.+ if (!fetch_attributes.integrity.has_value())+ options.integrity_metadata = resolve_a_module_integrity_metadata(*url, settings_object);++ // AD-HOC: Queue an element task on the networking task source to run the onComplete steps+ // This resolves an edge case where mark_as_ready() can execute before we set+ // m_steps_to_run_when_the_result_is_ready+ // See https://github.com/whatwg/html/issues/12073+ auto on_complete = create_on_fetch_script_complete(element.heap(), [this, &element](auto result) {+ element.queue_an_element_task(Task::Source::Networking, [this, result = move(result)] {+ if (!result)+ mark_as_ready(ResultState::Null {});+ else+ mark_as_ready(Result(*result));+ });+ });++ // Fetch an external module script graph given url, settings object, options, and onComplete.+ fetch_external_module_script_graph(HTML::relevant_realm(element), *url, settings_object, options, on_complete);+ }+ }++ // 34. If el does not have a src content attribute:+ if (!has_source_attribute()) {+ // 1. Let base URL be el's node document's document base URL.+ auto base_url = element.document().base_url();++ // 2. Switch on el's type:+ // -> "classic"+ if (m_script_type == ScriptType::Classic) {+ // 1. Let script be the result of creating a classic script using source text, settings object, base URL, and options.+ auto script = ClassicScript::create(element.document().url().to_byte_string(), source_text, settings_object, base_url, m_source_line_number, ClassicScript::MutedErrors::No, ScriptRegistry::IsInlineSource::Yes);+ script->set_fetch_options(options);++ // 2. Mark as ready el given script.+ mark_as_ready(Result(move(script)));+ }+ // -> "module"+ else if (m_script_type == ScriptType::Module) {+ // 1. Set el's delaying the load event to true.+ begin_delaying_document_load_event(*m_preparation_time_document);++ // 2. If el is potentially render-blocking:+ if (element.is_potentially_render_blocking()) {+ // 1. Block rendering on el.+ element.block_rendering();+ // 2. Set options's render-blocking to true.+ options.render_blocking = true;+ }++ auto steps = create_on_fetch_script_complete(element.heap(), [this, &element](auto result) {+ // 1. Queue an element task on the networking task source given el to perform the following steps:+ element.queue_an_element_task(Task::Source::Networking, [this, result = move(result)] {+ // 1. Mark as ready el given result.+ if (!result)+ mark_as_ready(ResultState::Null {});+ else+ mark_as_ready(Result(*result));+ });+ });++ // 3. Fetch an inline module script graph, given source text, base URL, settings object, options, and with the following steps given result:+ fetch_inline_module_script_graph(HTML::relevant_realm(element), element.document().url().to_byte_string(), source_text.utf16_view(), base_url, element.document().relevant_settings_object(), options, m_source_line_number, steps);+ }+ // -> "importmap"+ else if (m_script_type == ScriptType::ImportMap) {+ // 1. Let result be the result of creating an import map parse result given source text and base URL.+ auto result = ImportMapParseResult::parse(HTML::relevant_realm(element), source_text.utf16_view(), base_url);++ // 2. Mark as ready el given result.+ mark_as_ready(Result(move(result)));+ }+ // FIXME: -> "speculationrules"+ }++ // 35. If el's type is "classic" and el has a src attribute, or el's type is "module":+ if ((m_script_type == ScriptType::Classic && has_source_attribute()) || m_script_type == ScriptType::Module) {+ // 1. Assert: el's result is "uninitialized".+ // FIXME: I believe this step to be a spec bug, and it should be removed: https://github.com/whatwg/html/issues/8534++ // 2. If el has an async attribute or el's force async is true:+ if (element.has_attribute(HTML::AttributeNames::async) || m_force_async) {+ // 1. Let scripts be el's preparation-time document's set of scripts that will execute as soon as possible.+ // 2. Append el to scripts.+ m_preparation_time_document->scripts_to_execute_as_soon_as_possible().append(element);++ // 3. Set el's steps to run when the result is ready to the following:+ m_steps_to_run_when_the_result_is_ready = [this] {+ // 1. Execute the script element el.+ execute_script();++ // 2. Remove el from scripts.+ m_preparation_time_document->scripts_to_execute_as_soon_as_possible().remove_first_matching([this](auto& entry) {+ return entry.ptr() == &as_element();+ });+ };+ }++ // 3. Otherwise, if el is not parser-inserted:+ else if (!is_parser_inserted()) {+ // 1. Let scripts be el's preparation-time document's list of scripts that will execute in order as soon as possible.+ // 2. Append el to scripts.+ m_preparation_time_document->scripts_to_execute_in_order_as_soon_as_possible().append(element);++ // 3. Set el's steps to run when the result is ready to the following:+ m_steps_to_run_when_the_result_is_ready = [this] {+ auto& scripts = m_preparation_time_document->scripts_to_execute_in_order_as_soon_as_possible();+ // 1. If scripts[0] is not el, then abort these steps.+ if (scripts[0].ptr() != &as_element())+ return;++ // 2. While scripts is not empty, and scripts[0]'s result is not "uninitialized":+ while (!scripts.is_empty() && !as<HTMLOrSVGScriptElement>(*scripts[0]).m_result.has<ResultState::Uninitialized>()) {+ // 1. Execute the script element scripts[0].+ as<HTMLOrSVGScriptElement>(*scripts[0]).execute_script();++ // 2. Remove scripts[0].+ scripts.take_first();+ }+ };+ }++ // 4. Otherwise, if el has a defer attribute or el's type is "module":+ else if (has_defer_attribute() || m_script_type == ScriptType::Module) {+ // 1. Append el to its parser document's list of scripts that will execute when the document has finished parsing.+ m_parser_document->add_script_to_execute_when_parsing_has_finished({}, *this);++ // 2. Set el's steps to run when the result is ready to the following:+ m_steps_to_run_when_the_result_is_ready = [this] {+ // set el's ready to be parser-executed to true. (The parser will handle executing the script.)+ m_ready_to_be_parser_executed = true;+ };+ }++ // 5. Otherwise:+ else {+ // 1. Set el's parser document's pending parsing-blocking script to el.+ m_parser_document->set_pending_parsing_blocking_script(this);++ // 2. Block rendering on el.+ element.block_rendering();++ // 3. Set el's steps to run when the result is ready to the following:+ m_steps_to_run_when_the_result_is_ready = [this] {+ // set el's ready to be parser-executed to true. (The parser will handle executing the script.)+ m_ready_to_be_parser_executed = true;+ };+ }+ }++ // 36. Otherwise:+ else {+ // 1. Assert: el's result is not "uninitialized".+ VERIFY(!m_result.has<ResultState::Uninitialized>());++ // 2. If all of the following are true:+ // - el's type is "classic";+ // - el is parser-inserted;+ // - el's parser document has a style sheet that is blocking scripts; and+ // FIXME: - either the parser that created el is an XML parser, or it's an HTML parser whose script nesting level is not greater than one,+ // then:+ if (m_script_type == ScriptType::Classic+ && is_parser_inserted()+ && m_parser_document->has_a_style_sheet_that_is_blocking_scripts()) {+ // 1. Set el's parser document's pending parsing-blocking script to el.+ m_parser_document->set_pending_parsing_blocking_script(this);++ // 2. Set el's ready to be parser-executed to true. (The parser will handle executing the script.)+ m_ready_to_be_parser_executed = true;+ }++ // 3. Otherwise,+ else {+ // immediately execute the script element el, even if other scripts are already executing.+ execute_script();+ }+ }+}++// https://html.spec.whatwg.org/multipage/scripting.html#script-processing-model:children-changed-steps+void HTMLOrSVGScriptElement::script_element_children_changed(DOM::Node::ChildrenChangedMetadata const& metadata)+{+ // AD-HOC: Avoid running script on child removal, matching the behaviour of other browsers.+ // We also do not run script on child mutation, matching the behaviour of chromium and webkit.+ // See: https://github.com/whatwg/html/issues/12279+ if (metadata.type != DOM::Node::ChildrenChangedMetadata::Type::Inserted)+ return;++ // 1. If the script element is not connected, then return.+ if (!as_element().is_connected())+ return;++ // 2. Run the script HTML element post-connection steps, given the script element.+ script_element_post_connection();+}++// https://html.spec.whatwg.org/multipage/scripting.html#script-processing-model:html-element-post-connection-steps+void HTMLOrSVGScriptElement::script_element_post_connection()+{+ // 1. If insertedNode is parser-inserted, then return.+ if (is_parser_inserted())+ return;++ // 2. Prepare the script element given insertedNode.+ prepare_script();+}++// https://html.spec.whatwg.org/multipage/scripting.html#mark-as-ready+void HTMLOrSVGScriptElement::mark_as_ready(Result result)+{+ // 1. Set el's result to result.+ m_result = move(result);++ // 2. If el's steps to run when the result is ready are not null, then run them.+ if (m_steps_to_run_when_the_result_is_ready)+ m_steps_to_run_when_the_result_is_ready();++ // 3. Set el's steps to run when the result is ready to null.+ m_steps_to_run_when_the_result_is_ready = nullptr;++ // 4. Set el's delaying the load event to false.+ m_document_load_event_delayer.clear();++ if (m_preparation_time_document)+ m_preparation_time_document->schedule_html_parser_end_check();+}++// https://html.spec.whatwg.org/multipage/scripting.html#script-processing-model:concept-node-clone-ext+void HTMLOrSVGScriptElement::script_element_cloned(HTMLOrSVGScriptElement& copy) const+{+ // The cloning steps for script elements given node, copy, and subtree are to set copy's already started to node's already started.+ copy.m_already_started = m_already_started;+}++}++namespace Web::DOM {++template<>+HTML::HTMLOrSVGScriptElement* Node::fast_as<HTML::HTMLOrSVGScriptElement>()+{+ if (auto* html_script_element = as_if<HTML::HTMLScriptElement>(*this))+ return html_script_element;+ return nullptr;+}++template<>+HTML::HTMLOrSVGScriptElement const* Node::fast_as<HTML::HTMLOrSVGScriptElement>() const+{+ return const_cast<Node&>(*this).fast_as<HTML::HTMLOrSVGScriptElement>();+}++}diff --git a/Libraries/LibWeb/HTML/HTMLOrSVGScriptElement.h b/Libraries/LibWeb/HTML/HTMLOrSVGScriptElement.hnew file mode 100644index 00000000000..51af3b00d3b--- /dev/null+++ b/Libraries/LibWeb/HTML/HTMLOrSVGScriptElement.h@@ -0,0 +1,159 @@+/*+ * Copyright (c) 2018-2021, Andreas Kling <[email protected]>+ * Copyright (c) 2026, Tim Ledbetter <[email protected]>+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#pragma once++#include <AK/Badge.h>+#include <AK/Function.h>+#include <AK/Utf16String.h>+#include <LibWeb/DOM/DocumentLoadEventDelayer.h>+#include <LibWeb/DOM/Node.h>+#include <LibWeb/Forward.h>+#include <LibWeb/HTML/CORSSettingAttribute.h>+#include <LibWeb/HTML/Scripting/ImportMapParseResult.h>+#include <LibWeb/HTML/Scripting/Script.h>+#include <LibWeb/HTML/Scripting/ScriptFetchOptions.h>+#include <LibWeb/WebIDL/ExceptionOr.h>+#include <LibWebCommon/ReferrerPolicy/ReferrerPolicy.h>++namespace Web::HTML {++struct ScriptElementFetchAttributes {+ CORSSettingAttribute crossorigin { CORSSettingAttribute::NoCORS };+ Utf16String cryptographic_nonce {};+ Optional<Utf16String> integrity {};+ ReferrerPolicy::ReferrerPolicy referrer_policy { ReferrerPolicy::ReferrerPolicy::EmptyString };+ Fetch::Infrastructure::Request::Priority fetch_priority { Fetch::Infrastructure::Request::Priority::Auto };+ bool parser_inserted { false };+};++ScriptFetchOptions script_element_fetch_options(DOM::Element const&, ScriptElementFetchAttributes const&);++class HTMLOrSVGScriptElement {+public:+ virtual ~HTMLOrSVGScriptElement() = default;++ bool is_force_async() const { return m_force_async; }+ bool is_ready_to_be_parser_executed() const { return m_ready_to_be_parser_executed; }+ bool is_parser_inserted() const { return !!m_parser_document; }++ void set_parser_document(Badge<XMLDocumentBuilder, HTMLParser>, DOM::Document& document) { m_parser_document = &document; }+ void set_force_async(Badge<XMLDocumentBuilder, HTMLParser>, bool force_async) { m_force_async = force_async; }+ void set_already_started(Badge<XMLDocumentBuilder, HTMLParser>, bool already_started) { m_already_started = already_started; }+ void prepare_script(Badge<XMLDocumentBuilder, HTMLParser>) { prepare_script(); }++ void execute_script();+ void stop_delaying_document_load_event(Badge<DOM::Document>) { m_document_load_event_delayer.clear(); }++ void set_source_line_number(Badge<HTMLParser>, size_t source_line_number) { m_source_line_number = source_line_number; }+ void set_string_text(Utf16View value) { m_script_text = Utf16String::from_utf16(value); }++ virtual DOM::Element& as_element() = 0;+ virtual DOM::Element const& as_element() const = 0;++protected:+ enum class ScriptType {+ Null,+ Classic,+ Module,+ ImportMap,+ };++ ScriptType script_type() const { return m_script_type; }+ void set_force_async(bool force_async) { m_force_async = force_async; }++ void script_element_attribute_changed(Utf16FlyString const& name);+ void script_element_children_changed(DOM::Node::ChildrenChangedMetadata const&);+ void script_element_post_connection();+ void script_element_cloned(HTMLOrSVGScriptElement& copy) const;++ void visit_script_element_edges(GC::Cell::Visitor&);++ void retarget_script_load_event_delayer(DOM::Document& new_document)+ {+ if (m_document_load_event_delayer.has_value())+ m_document_load_event_delayer.emplace(new_document);+ }++ virtual bool has_source_attribute() const = 0;+ virtual Utf16String source_attribute_value() = 0;+ virtual Optional<Utf16String> language_attribute_value() const = 0;+ virtual Optional<Utf16String> charset_attribute_value() const = 0;+ virtual Optional<Utf16String> event_attribute_value() const = 0;+ virtual Optional<Utf16String> for_attribute_value() const = 0;+ virtual bool has_defer_attribute() const = 0;+ virtual bool has_nomodule_attribute() const = 0;+ virtual ScriptElementFetchAttributes fetch_attributes() const = 0;++private:+ // https://www.w3.org/TR/trusted-types/#prepare-script-text+ WebIDL::ExceptionOr<void> prepare_script_text();++ // https://html.spec.whatwg.org/multipage/scripting.html#prepare-the-script-element+ void prepare_script();++ void begin_delaying_document_load_event(DOM::Document&);++ struct ResultState {+ struct Uninitialized { };+ struct Null { };+ };++ using Result = Variant<ResultState::Uninitialized, ResultState::Null, GC::Ref<HTML::Script>, GC::Ref<HTML::ImportMapParseResult>>;++ // https://html.spec.whatwg.org/multipage/scripting.html#mark-as-ready+ void mark_as_ready(Result);++ // https://html.spec.whatwg.org/multipage/scripting.html#parser-document+ GC::Ptr<DOM::Document> m_parser_document;++ // https://html.spec.whatwg.org/multipage/scripting.html#preparation-time-document+ GC::Ptr<DOM::Document> m_preparation_time_document;++ // https://html.spec.whatwg.org/multipage/scripting.html#script-force-async+ bool m_force_async { true };++ // https://html.spec.whatwg.org/multipage/scripting.html#already-started+ bool m_already_started { false };++ // https://html.spec.whatwg.org/multipage/scripting.html#concept-script-external+ bool m_from_an_external_file { false };++ // https://html.spec.whatwg.org/multipage/scripting.html#ready-to-be-parser-executed+ bool m_ready_to_be_parser_executed { false };++ // https://html.spec.whatwg.org/multipage/scripting.html#concept-script-type+ ScriptType m_script_type { ScriptType::Null };++ // https://html.spec.whatwg.org/multipage/scripting.html#steps-to-run-when-the-result-is-ready+ Function<void()> m_steps_to_run_when_the_result_is_ready;++ // https://html.spec.whatwg.org/multipage/scripting.html#concept-script-result+ Result m_result { ResultState::Uninitialized {} };++ // https://html.spec.whatwg.org/multipage/scripting.html#concept-script-delay-load+ Optional<DOM::DocumentLoadEventDelayer> m_document_load_event_delayer;++ size_t m_source_line_number { 1 };++ // https://www.w3.org/TR/trusted-types/#htmlscriptelement-script-text+ Utf16String m_script_text;+};++}++namespace Web::DOM {++template<>+inline bool Node::fast_is<HTML::HTMLOrSVGScriptElement>() const { return is_html_script_element(); }++template<>+WEB_API HTML::HTMLOrSVGScriptElement* Node::fast_as<HTML::HTMLOrSVGScriptElement>();+template<>+WEB_API HTML::HTMLOrSVGScriptElement const* Node::fast_as<HTML::HTMLOrSVGScriptElement>() const;++}diff --git a/Libraries/LibWeb/HTML/HTMLScriptElement.cpp b/Libraries/LibWeb/HTML/HTMLScriptElement.cppindex 4d9db7e0a77..adb6c707db7 100644--- a/Libraries/LibWeb/HTML/HTMLScriptElement.cpp+++ b/Libraries/LibWeb/HTML/HTMLScriptElement.cpp@@ -5,40 +5,17 @@* SPDX-License-Identifier: BSD-2-Clause*/-#include <AK/Debug.h>-#include <AK/Utf16StringBuilder.h>-#include <LibTextCodec/Decoder.h>-#include <LibWeb/ContentSecurityPolicy/BlockingAlgorithms.h>-#include <LibWeb/ContentSecurityPolicy/Directives/HTMLIntegration.h>#include <LibWeb/DOM/DOMTokenList.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/HTML/Scripting/ClassicScript.h>#include <LibWeb/HTML/Scripting/Environments.h>-#include <LibWeb/HTML/Scripting/Fetching.h>-#include <LibWeb/HTML/Scripting/ImportMapParseResult.h>-#include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>-#include <LibWeb/HTML/Window.h>-#include <LibWeb/HighResolutionTime/TimeOrigin.h>#include <LibWeb/TrustedTypes/RequireTrustedTypesForDirective.h>#include <LibWeb/TrustedTypes/TrustedTypePolicy.h>-#include <LibWebCommon/Infra/CharacterTypes.h>-#include <LibWebCommon/Infra/Strings.h>-#include <LibWebCommon/MimeSniff/MimeType.h>namespace Web::HTML {GC_DEFINE_ALLOCATOR(HTMLScriptElement);-static GC::Ref<DOM::Event> create_event_for_element(HTMLElement& element, Utf16FlyString const& event_name)-{- return DOM::Event::create(event_name, HighResolutionTime::current_high_resolution_time(relevant_global_object(element)));-}-HTMLScriptElement::HTMLScriptElement(DOM::Document& document, DOM::QualifiedName qualified_name): HTMLElement(document, move(qualified_name)){@@ -58,20 +35,13 @@ void HTMLScriptElement::visit_edges(Cell::Visitor& visitor){Base::visit_edges(visitor);visitor.visit(m_blocking);- m_result.visit(- [](ResultState::Uninitialized) { },- [](ResultState::Null) { },- [&]<typename T>(GC::Ref<T> result) { visitor.visit(result); });- visitor.visit(m_parser_document);- visitor.visit(m_preparation_time_document);+ visit_script_element_edges(visitor);}void HTMLScriptElement::adopted_from(DOM::Document& old_document){Base::adopted_from(old_document);-- if (m_document_load_event_delayer.has_value())- m_document_load_event_delayer.emplace(document());+ retarget_script_load_event_delayer(document());}void HTMLScriptElement::attribute_changed(Utf16FlyString const& name, Optional<Utf16String> const& old_value, Optional<Utf16String> const& value, Optional<Utf16FlyString> const& namespace_)@@ -94,651 +64,20 @@ void HTMLScriptElement::attribute_changed(Utf16FlyString const& name, Optional<U// post-connection steps, given element.if (value.has_value() && is_connected())post_connection();- } else if (name == HTML::AttributeNames::async) {- // https://html.spec.whatwg.org/multipage/scripting.html#script-processing-model:script-force-async- // When an async attribute is added to a script element el, the user agent must set el's force async to false.- m_force_async = false;- }-}--// https://www.w3.org/TR/trusted-types/#prepare-script-text-WebIDL::ExceptionOr<void> HTMLScriptElement::prepare_script_text()-{- // 1. Let sink be "HTMLScriptElement text" if script is an HTMLScriptElement; otherwise "SVGScriptElement text".- constexpr auto sink = TrustedTypes::InjectionSink::HTMLScriptElement_text;-- // 2. If script’s script text value is not equal to its child text content,- // set script’s script text to the result of executing get Trusted Type compliant string, with the following arguments:- // TrustedScriptURL as expectedType,- // script’s Document’s relevant global object as global,- // script’s child text content attribute value as input,- // sink,- // 'script' as sinkGroup.- if (m_script_text != child_text_content()) {- // INTEROP: Step 2 currently names TrustedScriptURL, but script bodies use the createScript policy callback- // in other engines, consistently with the script text setters.- m_script_text = TRY(TrustedTypes::get_trusted_type_compliant_string(- TrustedTypes::TrustedTypeName::TrustedScript,- HTML::relevant_global_object(document()),- child_text_content(),- sink,- TrustedTypes::Script.view()));}- return {};-}--void HTMLScriptElement::begin_delaying_document_load_event(DOM::Document& document)-{- // https://html.spec.whatwg.org/multipage/scripting.html#concept-script-script- // The user agent must delay the load event of the element's node document until the script is ready.- m_document_load_event_delayer.emplace(document);+ script_element_attribute_changed(name);}-// https://html.spec.whatwg.org/multipage/scripting.html#execute-the-script-block-void HTMLScriptElement::execute_script()-{- // 1. Let document be el's node document.- GC::Ref<DOM::Document> document = this->document();-- // 2. If el's preparation-time document is not equal to document, then return.- if (m_preparation_time_document.ptr() != document.ptr()) {- dbgln("HTMLScriptElement: Refusing to run script because the preparation time document is not the same as the node document.");- return;- }-- // https://html.spec.whatwg.org/multipage/document-lifecycle.html#read-html- // Before any script execution occurs, the user agent must wait for scripts may run for the newly-created document to be true for document.- VERIFY(document->ready_to_run_scripts());-- // 3. Unblock rendering on el.- unblock_rendering();-- // 3. If el's result is null, then fire an event named error at el, and return.- if (m_result.has<ResultState::Null>()) {- dbgln("HTMLScriptElement: Refusing to run script because the element's result is null.");- dispatch_event(create_event_for_element(*this, HTML::EventNames::error));- return;- }-- // 5. If el's from an external file is true, or el's type is "module", then increment document's ignore-destructive-writes counter.- 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;- }-- // 5. Switch on el's type:- // -> "classic"- if (m_script_type == ScriptType::Classic) {- // 1. Let oldCurrentScript be the value to which document's currentScript object was most recently set.- auto old_current_script = document->current_script();- // 2. If el's root is not a shadow root, then set document's currentScript attribute to el. Otherwise, set it to null.- 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).value_or({}));- else- dbgln_if(HTML_SCRIPT_DEBUG, "HTMLScriptElement: Running inline script");-- // 3. Run the classic script given by el's result.- (void)as<ClassicScript>(*m_result.get<GC::Ref<Script>>()).run();-- // 4. Set document's currentScript attribute to oldCurrentScript.- document->set_current_script({}, old_current_script);- }- // -> "module"- else if (m_script_type == ScriptType::Module) {- // 1. Assert: document's currentScript attribute is null.- VERIFY(document->current_script() == nullptr);-- // 2. Run the module script given by el's result.- (void)as<ModuleScript>(*m_result.get<GC::Ref<Script>>()).run();- }- // -> "importmap"- else if (m_script_type == ScriptType::ImportMap) {- HTML::TemporaryExecutionContext execution_context { document->relevant_settings_object() };-- // 1. Register an import map given el's relevant global object and el's result.- m_result.get<GC::Ref<ImportMapParseResult>>()->register_import_map(relevant_window(*this));- }-- // 7. Decrement the ignore-destructive-writes counter of document, if it was incremented in the earlier step.- if (incremented_destructive_writes_counter)- document->decrement_ignore_destructive_writes_counter();-- // 8. If el's from an external file is true, then fire an event named load at el.- if (m_from_an_external_file)- dispatch_event(create_event_for_element(*this, HTML::EventNames::load));-}--// https://html.spec.whatwg.org/multipage/scripting.html#prepare-the-script-element-ScriptFetchOptions script_element_fetch_options(DOM::Element const& element, ScriptElementFetchAttributes const& attributes)-{- // 25. Let module script credentials mode be the CORS settings attribute credentials mode for el's crossorigin content attribute.- auto module_script_credential_mode = cors_settings_attribute_credentials_mode(attributes.crossorigin);-- // 26. Let cryptographic nonce be el's [[CryptographicNonce]] internal slot's value.- // AD-HOC: A script element that is not nonceable gets no nonce. Content Security Policy only checks this for inline- // scripts, which lets dangling markup take the nonce of a following element to load an external script.- auto cryptographic_nonce = ContentSecurityPolicy::Directives::is_element_nonceable(element) == ContentSecurityPolicy::Directives::Nonceable::Yes- ? attributes.cryptographic_nonce- : Utf16String {};-- // 27. If el has an integrity attribute, then let integrity metadata be that attribute's value.- // Otherwise, let integrity metadata be the empty string.- auto integrity_metadata = attributes.integrity.value_or({});-- // 28. Let referrer policy be the current state of el's referrerpolicy content attribute.- auto referrer_policy = attributes.referrer_policy;-- // 29. Let fetch priority be the current state of el's fetchpriority content attribute.- auto fetch_priority = attributes.fetch_priority;-- // 30. Let parser metadata be "parser-inserted" if el is parser-inserted, and "not-parser-inserted" otherwise.- auto parser_metadata = attributes.parser_inserted- ? Fetch::Infrastructure::Request::ParserMetadata::ParserInserted- : Fetch::Infrastructure::Request::ParserMetadata::NotParserInserted;-- // 31. Let options be a script fetch options whose cryptographic nonce is cryptographic nonce,- // integrity metadata is integrity metadata, parser metadata is parser metadata,- // credentials mode is module script credentials mode, referrer policy is referrer policy,- // and fetch priority is fetch priority.- return {- .cryptographic_nonce = move(cryptographic_nonce),- .integrity_metadata = move(integrity_metadata),- .parser_metadata = parser_metadata,- .credentials_mode = module_script_credential_mode,- .referrer_policy = referrer_policy,- .fetch_priority = fetch_priority,- };-}--// https://w3c.github.io/trusted-types/dist/spec/#slot-value-verification-// https://html.spec.whatwg.org/multipage/scripting.html#prepare-a-script-void HTMLScriptElement::prepare_script()-{- // 1. If el's already started is true, then return.- if (m_already_started)- return;-- // 2. Let parser document be el's parser document.- GC::Ptr<DOM::Document> parser_document = m_parser_document;-- // 3. Set el's parser document to null.- m_parser_document = nullptr;-- // 4. If parser document is non-null and el does not have an async attribute, then set el's force async to true.- if (parser_document && !has_attribute(HTML::AttributeNames::async)) {- m_force_async = true;- }-- // NB: The Trusted Types integration prepares the cached script text value before the HTML source-text step.- if (prepare_script_text().is_exception())- return;-- // 5. Let source text be el's child text content.- // NB: The Trusted Types integration supplies this through the cached script text value.- auto source_text = m_script_text;-- // 6. If el has no src attribute, and source text is the empty string, then return.- if (!has_attribute_ns({}, HTML::AttributeNames::src) && source_text.is_empty()) {- return;- }-- // 7. If el is not connected, then return.- if (!is_connected())- return;-- // 8. If any of the following are true:- // - el has a type attribute whose value is the empty string;- // - el has no type attribute but it has a language attribute and that attribute's value is the empty string; or- // - el has neither a type attribute nor a language attribute- Utf16String script_block_type;- auto maybe_type_attribute = get_attribute_ns({}, HTML::AttributeNames::type);- auto maybe_language_attribute = attribute(HTML::AttributeNames::language);- if ((maybe_type_attribute.has_value() && maybe_type_attribute->is_empty())- || (!maybe_type_attribute.has_value() && maybe_language_attribute.has_value() && maybe_language_attribute->is_empty())- || (!maybe_type_attribute.has_value() && !maybe_language_attribute.has_value())) {- // then let the script block's type string for this script element be "text/javascript".- script_block_type = "text/javascript"_utf16;- }- // Otherwise, if el has a type attribute,- else if (maybe_type_attribute.has_value()) {- // then let the script block's type string be the value of that attribute.- script_block_type = *maybe_type_attribute;- }- // Otherwise, el has a non-empty language attribute;- else if (maybe_language_attribute.has_value() && !maybe_language_attribute->is_empty()) {- // let the script block's type string be the concatenation of "text/" and the value of el's language attribute.- Utf16StringBuilder builder;- builder.append("text/"_utf16);- builder.append(*maybe_language_attribute);- script_block_type = builder.to_string();- }-- // 9. If the script block's type string is a JavaScript MIME type essence match, or el has a type attribute whose- // value with leading and trailing ASCII whitespace stripped is a JavaScript MIME type essence match,- if (MimeSniff::is_javascript_mime_type_essence_match(script_block_type)- || (maybe_type_attribute.has_value() && MimeSniff::is_javascript_mime_type_essence_match(maybe_type_attribute->trim(Infra::ASCII_WHITESPACE)))) {- // then set el's type to "classic".- m_script_type = ScriptType::Classic;- }- // 10. Otherwise, if the script block's type string is an ASCII case-insensitive match for the string "module",- else if (script_block_type.equals_ignoring_ascii_case(u"module"sv)) {- // then set el's type to "module".- m_script_type = ScriptType::Module;- }- // 11. Otherwise, if the script block's type string is an ASCII case-insensitive match for the string "importmap",- else if (script_block_type.equals_ignoring_ascii_case(u"importmap"sv)) {- // then set el's type to "importmap".- m_script_type = ScriptType::ImportMap;- }- // FIXME: 12. Otherwise, if the script block's type string is an ASCII case-insensitive match for the string "speculationrules", then set el's type to "speculationrules".- // 13. Otherwise, return. (No script is executed, and el's type is left as null.)- else {- VERIFY(m_script_type == ScriptType::Null);- return;- }-- // 14. If parser document is non-null, then set el's parser document back to parser document and set el's force async to false.- if (parser_document) {- m_parser_document = parser_document;- m_force_async = false;- }-- // 15. Set el's already started to true.- m_already_started = true;-- // 16. Set el's preparation-time document to its node document.- m_preparation_time_document = &document();-- // 17. If parser document is non-null, and parser document is not equal to el's preparation-time document, then return.- if (parser_document != nullptr && parser_document != m_preparation_time_document) {- dbgln("HTMLScriptElement: Refusing to run script because the parser document is not the same as the preparation time document.");- return;- }-- // 18. If scripting is disabled for el, then return.- if (is_scripting_disabled()) {- dbgln("HTMLScriptElement: Refusing to run script because scripting is disabled.");- return;- }-- // 19. If el has a nomodule content attribute and its type is "classic", then 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: 20. Let cspType be "script speculationrules" if el's type is "speculationrules"; otherwise, "script".-- // 21. If el does not have a src content attribute, and the Should element's inline behavior be blocked by Content- // Security Policy? algorithm returns "Blocked" when given el, cspType, and source text, then return [CSP]- if (!has_attribute_ns({}, AttributeNames::src)- && ContentSecurityPolicy::should_elements_inline_type_behavior_be_blocked_by_content_security_policy(*this, ContentSecurityPolicy::Directives::Directive::InlineType::Script, source_text.utf16_view()) == ContentSecurityPolicy::Directives::Directive::Result::Blocked) {- dbgln("HTMLScriptElement: Refusing to run inline script because it violates the Content Security Policy.");- return;- }-- // 22. If el has an event attribute and a for attribute, and el's type is "classic", then:- if (m_script_type == ScriptType::Classic && has_attribute(HTML::AttributeNames::event) && has_attribute(HTML::AttributeNames::for_)) {- // 1. Let for be the value of el's' for attribute.- auto for_ = attribute(HTML::AttributeNames::for_).value_or({});-- // 2. Let event be the value of el's event attribute.- auto event = attribute(HTML::AttributeNames::event).value_or({});-- // 3. Strip leading and trailing ASCII whitespace from event and for.- for_ = for_.trim_ascii_whitespace();- event = event.trim_ascii_whitespace();-- // 4. If for is not an ASCII case-insensitive match for the string "window", then return.- if (!for_.equals_ignoring_ascii_case(u"window"sv)) {- dbgln("HTMLScriptElement: Refusing to run classic script because the provided 'for' attribute is not equal to 'window'");- return;- }-- // 5. If event is not an ASCII case-insensitive match for either the string "onload" or the string "onload()", then return.- if (!event.equals_ignoring_ascii_case(u"onload"sv)- && !event.equals_ignoring_ascii_case(u"onload()"sv)) {- dbgln("HTMLScriptElement: Refusing to run classic script because the provided 'event' attribute is not equal to 'onload' or 'onload()'");- return;- }- }-- // 23. If el has a charset attribute, then let encoding be the result of getting an encoding from the value of the charset attribute.- // If el does not have a charset attribute, or if getting an encoding failed, then let encoding be el's node document's the encoding.- Optional<Utf16String> encoding;-- if (has_attribute(HTML::AttributeNames::charset)) {- auto charset = TextCodec::get_standardized_encoding(attribute(HTML::AttributeNames::charset).value_or({}));- if (charset.has_value())- encoding = Utf16String::from_ascii_without_validation(charset->bytes());- }-- if (!encoding.has_value()) {- encoding = document().encoding_or_default();- }-- VERIFY(encoding.has_value());-- // 24. Let classic script CORS setting be the current state of el's crossorigin content attribute.- auto classic_script_cors_setting = m_crossorigin;-- // 25-31. Let options be a script fetch options for el.- auto fetch_priority = Fetch::Infrastructure::request_priority_from_string(attribute(HTML::AttributeNames::fetchpriority).value_or({})).value_or(Fetch::Infrastructure::Request::Priority::Auto);- ScriptElementFetchAttributes fetch_attributes {- .crossorigin = m_crossorigin,- .cryptographic_nonce = nonce(),- .integrity = get_attribute_ns({}, HTML::AttributeNames::integrity),- .referrer_policy = m_referrer_policy,- .fetch_priority = fetch_priority,- .parser_inserted = is_parser_inserted(),- };- auto options = script_element_fetch_options(*this, fetch_attributes);-- // 32. Let settings object be el's node document's relevant settings object.- auto& settings_object = document().relevant_settings_object();-- // 33. If el has a src content attribute, then:- if (has_attribute_ns({}, HTML::AttributeNames::src)) {- // 1. If el's type is "importmap" or "speculationrules", then:- // FIXME: Add "speculationrules" support.- if (m_script_type == ScriptType::ImportMap) {- // then queue an element task on the DOM manipulation task source given el to fire an event named error at el, and return.- queue_an_element_task(HTML::Task::Source::DOMManipulation, [this] {- dispatch_event(create_event_for_element(*this, HTML::EventNames::error));- });- return;- }-- // 2. Let src be the value of el's src attribute.- auto src = get_attribute_ns({}, HTML::AttributeNames::src).value_or({});-- // 3. If src is the empty string, then queue an element task on the DOM manipulation task source given el to fire an event named error at el, and return.- if (src.is_empty()) {- dbgln("HTMLScriptElement: Refusing to run script because the src attribute is empty.");- queue_an_element_task(HTML::Task::Source::DOMManipulation, [this] {- dispatch_event(create_event_for_element(*this, HTML::EventNames::error));- });- return;- }-- // 4. Set el's from an external file to true.- m_from_an_external_file = true;-- // 5. Let url be the result of encoding-parsing a URL given src, relative to el's node document.- auto url = document().encoding_parse_url(src);-- // 6. If url is failure, then queue an element task on the DOM manipulation task source given el to fire an event named error at el, and return.- if (!url.has_value()) {- dbgln("HTMLScriptElement: Refusing to run script because the src URL '{}' is invalid.", url);- queue_an_element_task(HTML::Task::Source::DOMManipulation, [this] {- dispatch_event(create_event_for_element(*this, HTML::EventNames::error));- });- return;- }-- // 7. If el is potentially render-blocking, then block rendering on el.- if (is_potentially_render_blocking()) {- block_rendering();- }-- // 8. Set el's delaying the load event to true.- begin_delaying_document_load_event(*m_preparation_time_document);-- // 9. If el is currently render-blocking, then set options's render-blocking to true.- if (document().is_render_blocking_element(*this))- options.render_blocking = true;-- // 10. Let onComplete given result be the following steps:- OnFetchScriptComplete on_complete = create_on_fetch_script_complete(heap(), [this](auto result) {- // 1. Mark as ready el given result.- if (result)- mark_as_ready(Result { *result });- else- mark_as_ready(ResultState::Null {});- });-- // 11. Switch on el's type:- // -> "classic"- if (m_script_type == ScriptType::Classic) {- // Fetch a classic script given url, settings object, options, classic script CORS setting, encoding, and onComplete.- fetch_classic_script(relevant_realm(*this), *url, settings_object, move(options), classic_script_cors_setting, encoding.release_value(), on_complete);- }- // -> "module"- else if (m_script_type == ScriptType::Module) {- // If el does not have an integrity attribute, then set options's integrity metadata to the result of resolving a module integrity metadata with url and settings object.- if (!has_attribute_ns({}, HTML::AttributeNames::integrity))- options.integrity_metadata = resolve_a_module_integrity_metadata(*url, settings_object);-- // AD-HOC: Queue an element task on the networking task source to run the onComplete steps- // This resolves an edge case where mark_as_ready() can execute before we set- // m_steps_to_run_when_the_result_is_ready- // See https://github.com/whatwg/html/issues/12073- auto on_complete = create_on_fetch_script_complete(heap(), [this](auto result) {- queue_an_element_task(Task::Source::Networking, [this, result = move(result)] {- if (!result)- mark_as_ready(ResultState::Null {});- else- mark_as_ready(Result(*result));- });- });-- // Fetch an external module script graph given url, settings object, options, and onComplete.- fetch_external_module_script_graph(HTML::relevant_realm(*this), *url, settings_object, options, on_complete);- }- }-- // 34. If el does not have a src content attribute:- if (!has_attribute_ns({}, HTML::AttributeNames::src)) {- // 1. Let base URL be el's node document's document base URL.- auto base_url = document().base_url();-- // 2. Switch on el's type:- // -> "classic"- if (m_script_type == ScriptType::Classic) {- // 1. Let script be the result of creating a classic script using source text, settings object, base URL, and options.- auto script = ClassicScript::create(m_document->url().to_byte_string(), source_text, settings_object, base_url, m_source_line_number, ClassicScript::MutedErrors::No, ScriptRegistry::IsInlineSource::Yes);- script->set_fetch_options(options);-- // 2. Mark as ready el given script.- mark_as_ready(Result(move(script)));- }- // -> "module"- else if (m_script_type == ScriptType::Module) {- // 1. Set el's delaying the load event to true.- begin_delaying_document_load_event(*m_preparation_time_document);-- // 2. If el is potentially render-blocking:- if (is_potentially_render_blocking()) {- // 1. Block rendering on el.- block_rendering();- // 2. Set options's render-blocking to true.- options.render_blocking = true;- }-- auto steps = create_on_fetch_script_complete(heap(), [this](auto result) {- // 1. Queue an element task on the networking task source given el to perform the following steps:- queue_an_element_task(Task::Source::Networking, [this, result = move(result)] {- // 1. Mark as ready el given result.- if (!result)- mark_as_ready(ResultState::Null {});- else- mark_as_ready(Result(*result));- });- });-- // 3. Fetch an inline module script graph, given source text, base URL, settings object, options, and with the following steps given result:- fetch_inline_module_script_graph(HTML::relevant_realm(*this), m_document->url().to_byte_string(), source_text.utf16_view(), base_url, document().relevant_settings_object(), options, m_source_line_number, steps);- }- // -> "importmap"- else if (m_script_type == ScriptType::ImportMap) {- // 1. Let result be the result of creating an import map parse result given source text and base URL.- auto result = ImportMapParseResult::parse(HTML::relevant_realm(*this), source_text.utf16_view(), base_url);-- // 2. Mark as ready el given result.- mark_as_ready(Result(move(result)));- }- // FIXME: -> "speculationrules"- }-- // 35. If el's type is "classic" and el has a src attribute, or el's type is "module":- if ((m_script_type == ScriptType::Classic && has_attribute_ns({}, HTML::AttributeNames::src)) || m_script_type == ScriptType::Module) {- // 1. Assert: el's result is "uninitialized".- // FIXME: I believe this step to be a spec bug, and it should be removed: https://github.com/whatwg/html/issues/8534-- // 2. If el has an async attribute or el's force async is true:- if (has_attribute(HTML::AttributeNames::async) || m_force_async) {- // 1. Let scripts be el's preparation-time document's set of scripts that will execute as soon as possible.- // 2. Append el to scripts.- m_preparation_time_document->scripts_to_execute_as_soon_as_possible().append(*this);-- // 3. Set el's steps to run when the result is ready to the following:- m_steps_to_run_when_the_result_is_ready = [this] {- // 1. Execute the script element el.- execute_script();-- // 2. Remove el from scripts.- m_preparation_time_document->scripts_to_execute_as_soon_as_possible().remove_first_matching([this](auto& entry) {- return entry.ptr() == this;- });- };- }-- // 3. Otherwise, if el is not parser-inserted:- else if (!is_parser_inserted()) {- // 1. Let scripts be el's preparation-time document's list of scripts that will execute in order as soon as possible.- // 2. Append el to scripts.- m_preparation_time_document->scripts_to_execute_in_order_as_soon_as_possible().append(*this);-- // 3. Set el's steps to run when the result is ready to the following:- m_steps_to_run_when_the_result_is_ready = [this] {- auto& scripts = m_preparation_time_document->scripts_to_execute_in_order_as_soon_as_possible();- // 1. If scripts[0] is not el, then abort these steps.- if (scripts[0].ptr() != this)- return;-- // 2. While scripts is not empty, and scripts[0]'s result is not "uninitialized":- while (!scripts.is_empty() && !scripts[0]->m_result.has<ResultState::Uninitialized>()) {- // 1. Execute the script element scripts[0].- scripts[0]->execute_script();-- // 2. Remove scripts[0].- scripts.take_first();- }- };- }-- // 4. Otherwise, if el has a defer attribute or el's type is "module":- else if (has_attribute(HTML::AttributeNames::defer) || m_script_type == ScriptType::Module) {- // 1. Append el to its parser document's list of scripts that will execute when the document has finished parsing.- m_parser_document->add_script_to_execute_when_parsing_has_finished({}, *this);-- // 2. Set el's steps to run when the result is ready to the following:- m_steps_to_run_when_the_result_is_ready = [this] {- // set el's ready to be parser-executed to true. (The parser will handle executing the script.)- m_ready_to_be_parser_executed = true;- };- }-- // 5. Otherwise:- else {- // 1. Set el's parser document's pending parsing-blocking script to el.- m_parser_document->set_pending_parsing_blocking_script(this);-- // 2. Block rendering on el.- block_rendering();-- // 3. Set el's steps to run when the result is ready to the following:- m_steps_to_run_when_the_result_is_ready = [this] {- // set el's ready to be parser-executed to true. (The parser will handle executing the script.)- m_ready_to_be_parser_executed = true;- };- }- }-- // 36. Otherwise:- else {- // 1. Assert: el's result is not "uninitialized".- VERIFY(!m_result.has<ResultState::Uninitialized>());-- // 2. If all of the following are true:- // - el's type is "classic";- // - el is parser-inserted;- // - el's parser document has a style sheet that is blocking scripts; and- // FIXME: - either the parser that created el is an XML parser, or it's an HTML parser whose script nesting level is not greater than one,- // then:- if (m_script_type == ScriptType::Classic- && is_parser_inserted()- && m_parser_document->has_a_style_sheet_that_is_blocking_scripts()) {- // 1. Set el's parser document's pending parsing-blocking script to el.- m_parser_document->set_pending_parsing_blocking_script(this);-- // 2. Set el's ready to be parser-executed to true. (The parser will handle executing the script.)- m_ready_to_be_parser_executed = true;- }-- // 3. Otherwise,- else {- // immediately execute the script element el, even if other scripts are already executing.- execute_script();- }- }-}--// https://html.spec.whatwg.org/multipage/scripting.html#script-processing-model:children-changed-stepsvoid HTMLScriptElement::children_changed(ChildrenChangedMetadata const& metadata){Base::children_changed(metadata);-- // AD-HOC: Avoid running script on child removal, matching the behaviour of other browsers.- // We also do not run script on child mutation, matching the behaviour of chromium and webkit.- // See: https://github.com/whatwg/html/issues/12279- if (metadata.type != ChildrenChangedMetadata::Type::Inserted)- return;-- // 1. If the script element is not connected, then return.- if (!is_connected())- return;-- // 2. Run the script HTML element post-connection steps, given the script element.- post_connection();+ script_element_children_changed(metadata);}-// https://html.spec.whatwg.org/multipage/scripting.html#script-processing-model:html-element-post-connection-stepsvoid HTMLScriptElement::post_connection(){- // 1. If insertedNode is parser-inserted, then return.- if (is_parser_inserted())- return;-- // 2. Prepare the script element given insertedNode.- prepare_script();-}--// https://html.spec.whatwg.org/multipage/scripting.html#mark-as-ready-void HTMLScriptElement::mark_as_ready(Result result)-{- // 1. Set el's result to result.- m_result = move(result);-- // 2. If el's steps to run when the result is ready are not null, then run them.- if (m_steps_to_run_when_the_result_is_ready)- m_steps_to_run_when_the_result_is_ready();-- // 3. Set el's steps to run when the result is ready to null.- m_steps_to_run_when_the_result_is_ready = nullptr;-- // 4. Set el's delaying the load event to false.- m_document_load_event_delayer.clear();-- if (m_preparation_time_document)- m_preparation_time_document->schedule_html_parser_end_check();+ script_element_post_connection();}// https://github.com/w3c/trusted-types/blob/0f01aee35c3684e8cb20e7d83445ad65938f545d/spec/index.bs#L1341@@ -779,7 +118,7 @@ WebIDL::ExceptionOr<void> HTMLScriptElement::set_text(TrustedTypes::TrustedScripTrustedTypes::Script.view()));// 2. Set this’s script text value to the given value.- m_script_text = value;+ set_string_text(value);// 3. String replace all with the given value within this.string_replace_all(value);@@ -830,7 +169,7 @@ WebIDL::ExceptionOr<void> HTMLScriptElement::set_text_content(TrustedTypes::NullTrustedTypes::Script.view()));// 2. Set this’s script text value to value.- m_script_text = value;+ set_string_text(value);// 3. Run set text content with this and value.string_replace_all(value);@@ -857,7 +196,7 @@ WebIDL::ExceptionOr<void> HTMLScriptElement::set_inner_text(TrustedTypes::TrusteTrustedTypes::Script.view()));// 2. Set this’s script text value to value.- m_script_text = value;+ set_string_text(value);// 3. Run set the inner text steps with this and value.HTMLElement::set_inner_text(value);@@ -868,7 +207,7 @@ WebIDL::ExceptionOr<void> HTMLScriptElement::set_inner_text(TrustedTypes::Trustebool HTMLScriptElement::async() const{// 1. If this's force async is true, then return true.- if (m_force_async)+ if (is_force_async())return true;// 2. If this's async content attribute is present, then return true.@@ -882,7 +221,7 @@ bool HTMLScriptElement::async() constvoid HTMLScriptElement::set_async(bool async){// 1. Set this's force async to false.- m_force_async = false;+ set_force_async(false);// 2. If the given value is true, then set this's async content attribute to the empty string.if (async) {@@ -894,15 +233,10 @@ void HTMLScriptElement::set_async(bool async)}}-// https://html.spec.whatwg.org/multipage/scripting.html#script-processing-model:concept-node-clone-extWebIDL::ExceptionOr<void> HTMLScriptElement::cloned(Node& copy, bool subtree) const{TRY(Base::cloned(copy, subtree));-- // The cloning steps for script elements given node, copy, and subtree are to set copy's already started to node's already started.- auto& script_copy = as<HTMLScriptElement>(copy);- script_copy.m_already_started = m_already_started;-+ script_element_cloned(as<HTMLScriptElement>(copy));return {};}@@ -910,7 +244,59 @@ WebIDL::ExceptionOr<void> HTMLScriptElement::cloned(Node& copy, bool subtree) cobool HTMLScriptElement::is_implicitly_potentially_render_blocking() const{// A script element el is implicitly potentially render-blocking if el's type is "classic", el is parser-inserted, and el does not have an async or defer attribute.- return m_script_type == ScriptType::Classic && is_parser_inserted() && !has_attribute(AttributeNames::async) && !has_attribute(AttributeNames::defer);+ return script_type() == ScriptType::Classic && is_parser_inserted() && !has_attribute(AttributeNames::async) && !has_attribute(AttributeNames::defer);+}++bool HTMLScriptElement::has_source_attribute() const+{+ return has_attribute_ns({}, AttributeNames::src);+}++Utf16String HTMLScriptElement::source_attribute_value()+{+ return get_attribute_ns({}, AttributeNames::src).value_or({});+}++Optional<Utf16String> HTMLScriptElement::language_attribute_value() const+{+ return attribute(AttributeNames::language);+}++Optional<Utf16String> HTMLScriptElement::charset_attribute_value() const+{+ return attribute(AttributeNames::charset);+}++Optional<Utf16String> HTMLScriptElement::event_attribute_value() const+{+ return attribute(AttributeNames::event);+}++Optional<Utf16String> HTMLScriptElement::for_attribute_value() const+{+ return attribute(AttributeNames::for_);+}++bool HTMLScriptElement::has_defer_attribute() const+{+ return has_attribute(AttributeNames::defer);+}++bool HTMLScriptElement::has_nomodule_attribute() const+{+ return has_attribute(AttributeNames::nomodule);+}++ScriptElementFetchAttributes HTMLScriptElement::fetch_attributes() const+{+ return {+ .crossorigin = m_crossorigin,+ .cryptographic_nonce = nonce(),+ .integrity = get_attribute_ns({}, AttributeNames::integrity),+ .referrer_policy = m_referrer_policy,+ .fetch_priority = Fetch::Infrastructure::request_priority_from_string(attribute(AttributeNames::fetchpriority).value_or({})).value_or(Fetch::Infrastructure::Request::Priority::Auto),+ .parser_inserted = is_parser_inserted(),+ };}}diff --git a/Libraries/LibWeb/HTML/HTMLScriptElement.h b/Libraries/LibWeb/HTML/HTMLScriptElement.hindex 63904b8cacc..909fcca2909 100644--- a/Libraries/LibWeb/HTML/HTMLScriptElement.h+++ b/Libraries/LibWeb/HTML/HTMLScriptElement.h@@ -6,21 +6,19 @@#pragma once-#include <AK/Function.h>#include <AK/Utf16View.h>-#include <LibWeb/DOM/DocumentLoadEventDelayer.h>#include <LibWeb/HTML/CORSSettingAttribute.h>#include <LibWeb/HTML/HTMLElement.h>-#include <LibWeb/HTML/Scripting/ImportMapParseResult.h>-#include <LibWeb/HTML/Scripting/Script.h>-#include <LibWeb/HTML/Scripting/ScriptFetchOptions.h>+#include <LibWeb/HTML/HTMLOrSVGScriptElement.h>#include <LibWeb/TrustedTypes/TrustedScript.h>#include <LibWeb/TrustedTypes/TrustedScriptURL.h>#include <LibWebCommon/ReferrerPolicy/ReferrerPolicy.h>namespace Web::HTML {-class HTMLScriptElement final : public HTMLElement {+class HTMLScriptElement final+ : public HTMLElement+ , public HTMLOrSVGScriptElement {WEB_WRAPPABLE(HTMLScriptElement, HTMLElement);GC_DECLARE_ALLOCATOR(HTMLScriptElement);@@ -29,23 +27,6 @@ public:GC::Ref<DOM::DOMTokenList> blocking();- bool is_force_async() const { return m_force_async; }- bool is_ready_to_be_parser_executed() const { return m_ready_to_be_parser_executed; }- bool failed_to_load() const { return m_failed_to_load; }-- void set_parser_document(Badge<XMLDocumentBuilder, HTMLParser>, DOM::Document& document) { m_parser_document = &document; }-- void set_force_async(Badge<XMLDocumentBuilder, HTMLParser>, bool b) { m_force_async = b; }-- void set_already_started(Badge<XMLDocumentBuilder, HTMLParser>, bool b) { m_already_started = b; }-- void prepare_script(Badge<XMLDocumentBuilder, HTMLParser>) { prepare_script(); }-- void execute_script();- void stop_delaying_document_load_event(Badge<DOM::Document>) { m_document_load_event_delayer.clear(); }-- bool is_parser_inserted() const { return !!m_parser_document; }-virtual void children_changed(ChildrenChangedMetadata const&) override;virtual void post_connection() override;virtual bool has_post_connection_steps() const override { return true; }@@ -56,8 +37,6 @@ public:return type.is_one_of("classic"sv, "module"sv, "importmap"sv);}- void set_source_line_number(Badge<HTMLParser>, size_t source_line_number) { m_source_line_number = source_line_number; }-TrustedTypes::TrustedScriptOrString text() const { return child_text_content(); }WebIDL::ExceptionOr<void> set_text(TrustedTypes::TrustedScriptOrString);@@ -75,8 +54,6 @@ public:virtual WebIDL::ExceptionOr<void> cloned(Node&, bool) const override;- void set_string_text(Utf16View value) { m_script_text = Utf16String::from_utf16(value); }-protected:// https://html.spec.whatwg.org/multipage/urls-and-fetching.html#implicitly-potentially-render-blockingvirtual bool is_implicitly_potentially_render_blocking() const override;@@ -92,88 +69,26 @@ private:virtual void attribute_changed(Utf16FlyString const& name, Optional<Utf16String> const& old_value, Optional<Utf16String> const& value, Optional<Utf16FlyString> const& namespace_) override;- // https://www.w3.org/TR/trusted-types/#prepare-script-text- WebIDL::ExceptionOr<void> prepare_script_text();-- // https://html.spec.whatwg.org/multipage/scripting.html#prepare-the-script-element- void prepare_script();-- void begin_delaying_document_load_event(DOM::Document&);-- struct ResultState {- struct Uninitialized { };- struct Null { };- };-- using Result = Variant<ResultState::Uninitialized, ResultState::Null, GC::Ref<HTML::Script>, GC::Ref<HTML::ImportMapParseResult>>;-- // https://html.spec.whatwg.org/multipage/scripting.html#mark-as-ready- void mark_as_ready(Result);-- // https://html.spec.whatwg.org/multipage/scripting.html#parser-document- GC::Ptr<DOM::Document> m_parser_document;-- // https://html.spec.whatwg.org/multipage/scripting.html#preparation-time-document- GC::Ptr<DOM::Document> m_preparation_time_document;-- // https://html.spec.whatwg.org/multipage/scripting.html#script-force-async- bool m_force_async { true };-- // https://html.spec.whatwg.org/multipage/scripting.html#already-started- bool m_already_started { false };-- // https://html.spec.whatwg.org/multipage/scripting.html#concept-script-external- bool m_from_an_external_file { false };-- bool m_script_ready { false };-- // https://html.spec.whatwg.org/multipage/scripting.html#ready-to-be-parser-executed- bool m_ready_to_be_parser_executed { false };+ // ^HTMLOrSVGScriptElement+ virtual DOM::Element& as_element() override { return *this; }+ virtual DOM::Element const& as_element() const override { return *this; }+ virtual bool has_source_attribute() const override;+ virtual Utf16String source_attribute_value() override;+ virtual Optional<Utf16String> language_attribute_value() const override;+ virtual Optional<Utf16String> charset_attribute_value() const override;+ virtual Optional<Utf16String> event_attribute_value() const override;+ virtual Optional<Utf16String> for_attribute_value() const override;+ virtual bool has_defer_attribute() const override;+ virtual bool has_nomodule_attribute() const override;+ virtual ScriptElementFetchAttributes fetch_attributes() const override;// https://html.spec.whatwg.org/multipage/scripting.html#attr-script-crossoriginCORSSettingAttribute m_crossorigin { CORSSettingAttribute::NoCORS };// https://html.spec.whatwg.org/multipage/scripting.html#attr-script-referrerpolicyReferrerPolicy::ReferrerPolicy m_referrer_policy { ReferrerPolicy::ReferrerPolicy::EmptyString };-- bool m_failed_to_load { false };-- enum class ScriptType {- Null,- Classic,- Module,- ImportMap,- };-- // https://html.spec.whatwg.org/multipage/scripting.html#concept-script-type- ScriptType m_script_type { ScriptType::Null };-- // https://html.spec.whatwg.org/multipage/scripting.html#steps-to-run-when-the-result-is-ready- Function<void()> m_steps_to_run_when_the_result_is_ready;-- // https://html.spec.whatwg.org/multipage/scripting.html#concept-script-result- Result m_result { ResultState::Uninitialized {} };-- // https://html.spec.whatwg.org/multipage/scripting.html#concept-script-delay-load- Optional<DOM::DocumentLoadEventDelayer> m_document_load_event_delayer;-- size_t m_source_line_number { 1 };-- // https://www.w3.org/TR/trusted-types/#htmlscriptelement-script-text- Utf16String m_script_text;-};--struct ScriptElementFetchAttributes {- CORSSettingAttribute crossorigin { CORSSettingAttribute::NoCORS };- Utf16String cryptographic_nonce {};- Optional<Utf16String> integrity {};- ReferrerPolicy::ReferrerPolicy referrer_policy { ReferrerPolicy::ReferrerPolicy::EmptyString };- Fetch::Infrastructure::Request::Priority fetch_priority { Fetch::Infrastructure::Request::Priority::Auto };- bool parser_inserted { false };};-ScriptFetchOptions script_element_fetch_options(DOM::Element const&, ScriptElementFetchAttributes const&);-}namespace Web::DOM {diff --git a/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp b/Libraries/LibWeb/HTML/Parser/HTMLParser.cppindex fbd59c77f9d..3621111b995 100644--- a/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp+++ b/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp@@ -805,7 +805,7 @@ void HTMLParserEndState::check_progress()case Phase::WaitingForDeferredScripts:// 5. While the list of scripts that will execute when the document has finished parsing is not empty:while (!m_document->scripts_to_execute_when_parsing_has_finished().is_empty()) {- auto& first_script = *m_document->scripts_to_execute_when_parsing_has_finished().first();+ auto& first_script = as<HTMLOrSVGScriptElement>(*m_document->scripts_to_execute_when_parsing_has_finished().first());// 1. Spin the event loop until the first script in the list of scripts that will execute when the document has finished parsing// has its "ready to be parser-executed" flag set and the parser's Document has no style sheet that is blocking scripts.@@ -1366,7 +1366,7 @@ void HTMLParser::resume_after_parser_blocking_script()// 2. Set the pending parsing-blocking script to null.// 11. Execute the script element the script.if (pending)- document().take_pending_parsing_blocking_script({})->execute_script();+ document().take_pending_parsing_blocking_script({}).execute_script();elsedocument().take_pending_parsing_blocking_svg_script({})->execute_pending_parser_blocking_script({});diff --git a/Meta/Generators/libweb_bindings/attributes.py b/Meta/Generators/libweb_bindings/attributes.pyindex 4188fd09dfe..6c8811fbd44 100644--- a/Meta/Generators/libweb_bindings/attributes.py+++ b/Meta/Generators/libweb_bindings/attributes.py@@ -67,15 +67,16 @@ def validate_direct_getter(context: GenerationContext, interface: Interface, attdescription = f"[DirectGetter] attribute '{attribute.name}' on '{interface.name}'"resolved_type = context.resolve_typedef(attribute.type)- target_interface = context.interface(resolved_type)- if target_interface is None or target_interface.is_callback_interface:- raise RuntimeError(f"{description} must have an interface type")- if target_interface.extended_attributes.get("ImplementedIn") == "Rust":- # NB: The engine reads the wrapper of a direct getter's object as a Wrappable keeps it, and the object of an- # interface implemented in Rust is a cell that keeps its wrapper elsewhere.- raise RuntimeError(f"{description} cannot return {target_interface.name}, which is implemented in Rust")- if not cpp_type_for_idl_type_details(resolved_type, context).gc_ref_target_type:- raise RuntimeError(f"{description} must convert from a GC pointer")+ for target_type in resolved_type.flattened_member_types():+ target_interface = context.interface(target_type)+ if target_interface is None or target_interface.is_callback_interface:+ raise RuntimeError(f"{description} must have an interface type or a union of interface types")+ if target_interface.extended_attributes.get("ImplementedIn") == "Rust":+ # NB: The engine reads the wrapper of a direct getter's object as a Wrappable keeps it, and the object of an+ # interface implemented in Rust is a cell that keeps its wrapper elsewhere.+ raise RuntimeError(f"{description} cannot return {target_interface.name}, which is implemented in Rust")+ if not cpp_type_for_idl_type_details(target_type, context).gc_ref_target_type:+ raise RuntimeError(f"{description} must convert from a GC pointer")incompatible_extended_attributes = ("CachedAttribute",