LibWeb: Port EnvironmentSettings URL Parsing to return Optional<URL>

This commit is contained in:
Shannon Booth 2025-02-16 14:01:09 +13:00 committed by Tim Flynn
parent 5f5d18d719
commit 2c8dab36f3
Notes: github-actions[bot] 2025-02-19 13:03:33 +00:00
8 changed files with 25 additions and 25 deletions

View file

@ -46,11 +46,11 @@ WebIDL::ExceptionOr<GC::Ref<EventSource>> EventSource::construct_impl(JS::Realm&
auto url_record = settings.encoding_parse_url(url);
// 4. If urlRecord is failure, then throw a "SyntaxError" DOMException.
if (!url_record.is_valid())
if (!url_record.has_value())
return WebIDL::SyntaxError::create(realm, MUST(String::formatted("Invalid URL '{}'", url)));
// 5. Set ev's url to urlRecord.
event_source->m_url = move(url_record);
event_source->m_url = url_record.release_value();
// 6. Let corsAttributeState be Anonymous.
auto cors_attribute_state = CORSSettingAttribute::Anonymous;

View file

@ -199,11 +199,11 @@ WebIDL::ExceptionOr<void> History::shared_history_push_replace_state(JS::Value d
auto parsed_url = relevant_settings_object(*this).parse_url(url->to_byte_string());
// 2. If that fails, then throw a "SecurityError" DOMException.
if (!parsed_url.is_valid())
if (!parsed_url.has_value())
return WebIDL::SecurityError::create(realm(), "Cannot pushState or replaceState to incompatible URL"_string);
// 3. Set newURL to the resulting URL record.
new_url = parsed_url;
new_url = parsed_url.release_value();
// 4. If document cannot have its URL rewritten to newURL, then throw a "SecurityError" DOMException.
if (!can_have_its_url_rewritten(document, new_url))

View file

@ -141,11 +141,11 @@ WebIDL::ExceptionOr<void> Location::set_href(String const& new_href)
auto url = entry_settings_object().encoding_parse_url(new_href.to_byte_string());
// 3. If url is failure, then throw a "SyntaxError" DOMException.
if (!url.is_valid())
if (!url.has_value())
return WebIDL::SyntaxError::create(realm, MUST(String::formatted("Invalid URL '{}'", new_href)));
// 4. Location-object navigate this to url.
TRY(navigate(url));
TRY(navigate(url.release_value()));
return {};
}
@ -450,11 +450,11 @@ WebIDL::ExceptionOr<void> Location::replace(String const& url)
// 2. Parse url relative to the entry settings object. If that failed, throw a "SyntaxError" DOMException.
auto replace_url = entry_settings_object().parse_url(url);
if (!replace_url.is_valid())
if (!replace_url.has_value())
return WebIDL::SyntaxError::create(realm(), MUST(String::formatted("Invalid URL '{}'", url)));
// 3. Location-object navigate this to the resulting URL record given "replace".
TRY(navigate(replace_url, Bindings::NavigationHistoryBehavior::Replace));
TRY(navigate(replace_url.release_value(), Bindings::NavigationHistoryBehavior::Replace));
return {};
}
@ -473,11 +473,11 @@ WebIDL::ExceptionOr<void> Location::assign(String const& url)
// 3. Parse url relative to the entry settings object. If that failed, throw a "SyntaxError" DOMException.
auto assign_url = entry_settings_object().parse_url(url);
if (!assign_url.is_valid())
if (!assign_url.has_value())
return WebIDL::SyntaxError::create(realm(), MUST(String::formatted("Invalid URL '{}'", url)));
// 4. Location-object navigate this to the resulting URL record.
TRY(navigate(assign_url));
TRY(navigate(assign_url.release_value()));
return {};
}

View file

@ -229,7 +229,7 @@ WebIDL::ExceptionOr<NavigationResult> Navigation::navigate(String url, Navigatio
// If that returns failure, then return an early error result for a "SyntaxError" DOMException.
// Otherwise, let urlRecord be the resulting URL record.
auto url_record = relevant_settings_object(*this).parse_url(url);
if (!url_record.is_valid())
if (!url_record.has_value())
return early_error_result(WebIDL::SyntaxError::create(realm, "Cannot navigate to Invalid URL"_string));
// 2. Let document be this's relevant global object's associated Document.
@ -237,7 +237,7 @@ WebIDL::ExceptionOr<NavigationResult> Navigation::navigate(String url, Navigatio
// 3. If options["history"] is "push", and the navigation must be a replace given urlRecord and document,
// then return an early error result for a "NotSupportedError" DOMException.
if (options.history == Bindings::NavigationHistoryBehavior::Push && navigation_must_be_a_replace(url_record, document))
if (options.history == Bindings::NavigationHistoryBehavior::Push && navigation_must_be_a_replace(url_record.value(), document))
return early_error_result(WebIDL::NotSupportedError::create(realm, "Navigation must be a replace, but push was requested"_string));
// 4. Let state be options["state"], if it exists; otherwise, undefined.
@ -278,7 +278,7 @@ WebIDL::ExceptionOr<NavigationResult> Navigation::navigate(String url, Navigatio
// of the navigation, and we don't need to deal with the allowed by sandboxing to navigate check and its
// acccompanying exceptionsEnabled flag. We just treat all navigations as if they come from the Document
// corresponding to this Navigation object itself (i.e., document).
TRY(document.navigable()->navigate({ .url = url_record, .source_document = document, .history_handling = options.history, .navigation_api_state = move(serialized_state) }));
TRY(document.navigable()->navigate({ .url = url_record.release_value(), .source_document = document, .history_handling = options.history, .navigation_api_state = move(serialized_state) }));
// 11. If this's upcoming non-traverse API method tracker is apiMethodTracker, then:
// NOTE: If the upcoming non-traverse API method tracker is still apiMethodTracker, this means that the navigate

View file

@ -203,17 +203,17 @@ void prepare_to_run_callback(JS::Realm& realm)
}
// https://html.spec.whatwg.org/multipage/urls-and-fetching.html#parse-a-url
URL::URL EnvironmentSettingsObject::parse_url(StringView url)
Optional<URL::URL> EnvironmentSettingsObject::parse_url(StringView url)
{
// 1. Let baseURL be environment's base URL, if environment is a Document object; otherwise environment's API base URL.
auto base_url = api_base_url();
// 2. Return the result of applying the URL parser to url, with baseURL.
return DOMURL::parse(url, base_url).value_or(URL::URL {});
return DOMURL::parse(url, base_url);
}
// https://html.spec.whatwg.org/multipage/urls-and-fetching.html#encoding-parsing-a-url
URL::URL EnvironmentSettingsObject::encoding_parse_url(StringView url)
Optional<URL::URL> EnvironmentSettingsObject::encoding_parse_url(StringView url)
{
// 1. Let encoding be UTF-8.
auto encoding = "UTF-8"_string;
@ -229,7 +229,7 @@ URL::URL EnvironmentSettingsObject::encoding_parse_url(StringView url)
auto base_url = api_base_url();
// 5. Return the result of applying the URL parser to url, with baseURL and encoding.
return DOMURL::parse(url, base_url, encoding).value_or(URL::URL {});
return DOMURL::parse(url, base_url, encoding);
}
// https://html.spec.whatwg.org/multipage/urls-and-fetching.html#encoding-parsing-and-serializing-a-url
@ -239,11 +239,11 @@ Optional<String> EnvironmentSettingsObject::encoding_parse_and_serialize_url(Str
auto parsed_url = encoding_parse_url(url);
// 2. If url is failure, then return failure.
if (!parsed_url.is_valid())
if (!parsed_url.has_value())
return {};
// 3. Return the result of applying the URL serializer to url.
return parsed_url.serialize();
return parsed_url->serialize();
}
// https://html.spec.whatwg.org/multipage/webappapis.html#clean-up-after-running-a-callback

View file

@ -91,8 +91,8 @@ public:
// https://html.spec.whatwg.org/multipage/webappapis.html#concept-settings-object-time-origin
virtual double time_origin() const = 0;
URL::URL parse_url(StringView);
URL::URL encoding_parse_url(StringView);
Optional<URL::URL> parse_url(StringView);
Optional<URL::URL> encoding_parse_url(StringView);
Optional<String> encoding_parse_and_serialize_url(StringView);
JS::Realm& realm();

View file

@ -63,7 +63,7 @@ WebIDL::ExceptionOr<GC::Ref<Worker>> Worker::create(String const& script_url, Wo
auto url = outside_settings.parse_url(script_url);
// 4. If this fails, throw a "SyntaxError" DOMException.
if (!url.is_valid()) {
if (!url.has_value()) {
dbgln_if(WEB_WORKER_DEBUG, "WebWorker: Invalid URL loaded '{}'.", script_url);
return WebIDL::SyntaxError::create(document.realm(), "url is not valid"_string);
}
@ -82,7 +82,7 @@ WebIDL::ExceptionOr<GC::Ref<Worker>> Worker::create(String const& script_url, Wo
// 9. Run this step in parallel:
// 1. Run a worker given worker, worker URL, outside settings, outside port, and options.
worker->run_a_worker(url, outside_settings, *outside_port, options);
worker->run_a_worker(url.value(), outside_settings, *outside_port, options);
// 10. Return worker
return worker;

View file

@ -106,11 +106,11 @@ WebIDL::ExceptionOr<void> WorkerGlobalScope::import_scripts(Vector<String> const
auto url_record = settings_object.encoding_parse_url(url);
// 2. If urlRecord is failure, then throw a "SyntaxError" DOMException.
if (!url_record.is_valid())
if (!url_record.has_value())
return WebIDL::SyntaxError::create(realm(), "Invalid URL"_string);
// 3. Append urlRecord to urlRecords.
url_records.unchecked_append(url_record);
url_records.unchecked_append(url_record.release_value());
}
// 6. For each urlRecord of urlRecords: