Skip to content

Truly independent web browser

LibWeb/HTML: Build a script element's fetch options in a function

Luke Wilde7e8ff7d535eb
diff --git a/Libraries/LibWeb/HTML/HTMLScriptElement.cpp b/Libraries/LibWeb/HTML/HTMLScriptElement.cpp
index 7f29b6d94c6..d84a5261be5 100644
--- a/Libraries/LibWeb/HTML/HTMLScriptElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLScriptElement.cpp
@@ -214,6 +214,44 @@ void HTMLScriptElement::execute_script()
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(ScriptElementFetchAttributes const& element)
+{
+ // 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(element.crossorigin);
+
+ // 26. Let cryptographic nonce be el's [[CryptographicNonce]] internal slot's value.
+ auto cryptographic_nonce = element.cryptographic_nonce;
+
+ // 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 = element.integrity.value_or({});
+
+ // 28. Let referrer policy be the current state of el's referrerpolicy content attribute.
+ auto referrer_policy = element.referrer_policy;
+
+ // 29. Let fetch priority be the current state of el's fetchpriority content attribute.
+ auto fetch_priority = element.fetch_priority;
+
+ // 30. Let parser metadata be "parser-inserted" if el is parser-inserted, and "not-parser-inserted" otherwise.
+ auto parser_metadata = element.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()
@@ -386,42 +424,16 @@ void HTMLScriptElement::prepare_script()
// 24. Let classic script CORS setting be the current state of el's crossorigin content attribute.
auto classic_script_cors_setting = m_crossorigin;
- // 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(m_crossorigin);
-
- // 26. Let cryptographic nonce be el's [[CryptographicNonce]] internal slot's value.
- auto cryptographic_nonce = nonce();
-
- // 27. If el has an integrity attribute, then let integrity metadata be that attribute's value.
- // Otherwise, let integrity metadata be the empty string.
- Utf16String integrity_metadata;
- if (auto maybe_integrity = get_attribute_ns({}, HTML::AttributeNames::integrity); maybe_integrity.has_value()) {
- integrity_metadata = *maybe_integrity;
- }
-
- // 28. Let referrer policy be the current state of el's referrerpolicy content attribute.
- auto referrer_policy = m_referrer_policy;
-
- // 29. Let fetch priority be the current state of el's fetchpriority content attribute.
+ // 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);
-
- // 30. Let parser metadata be "parser-inserted" if el is parser-inserted, and "not-parser-inserted" otherwise.
- auto parser_metadata = is_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.
- ScriptFetchOptions options {
- .cryptographic_nonce = move(cryptographic_nonce),
- .integrity_metadata = move(integrity_metadata),
- .parser_metadata = parser_metadata,
- .credentials_mode = module_script_credential_mode,
- .referrer_policy = move(referrer_policy),
- .fetch_priority = move(fetch_priority),
- };
+ auto options = script_element_fetch_options({
+ .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(),
+ });
// 32. Let settings object be el's node document's relevant settings object.
auto& settings_object = document().relevant_settings_object();
diff --git a/Libraries/LibWeb/HTML/HTMLScriptElement.h b/Libraries/LibWeb/HTML/HTMLScriptElement.h
index c9fe38c4d51..e695a3de5a1 100644
--- a/Libraries/LibWeb/HTML/HTMLScriptElement.h
+++ b/Libraries/LibWeb/HTML/HTMLScriptElement.h
@@ -13,6 +13,7 @@
#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/TrustedTypes/TrustedScript.h>
#include <LibWeb/TrustedTypes/TrustedScriptURL.h>
#include <LibWebCommon/ReferrerPolicy/ReferrerPolicy.h>
@@ -162,6 +163,17 @@ private:
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(ScriptElementFetchAttributes const&);
+
}
namespace Web::DOM {