LibURL+LibWeb: Make URL::serialize return a String

Simplifying a bunch of uneeded error handling around the place.
This commit is contained in:
Shannon Booth 2024-12-03 22:31:33 +13:00 committed by Sam Atkins
commit 0fa54c2327
Notes: github-actions[bot] 2024-12-04 16:48:13 +00:00
52 changed files with 88 additions and 103 deletions

View file

@ -256,7 +256,7 @@ WebIDL::ExceptionOr<BrowsingContext::BrowsingContextAndDocument> BrowsingContext
// 16. If creator is non-null, then:
if (creator) {
// 1. Set document's referrer to the serialization of creator's URL.
document->set_referrer(MUST(String::from_byte_string(creator->url().serialize())));
document->set_referrer(creator->url().serialize());
// 2. Set document's policy container to a clone of creator's policy container.
document->set_policy_container(creator->policy_container());

View file

@ -34,7 +34,7 @@ public:
static WebIDL::ExceptionOr<GC::Ref<EventSource>> construct_impl(JS::Realm&, StringView url, EventSourceInit event_source_init_dict = {});
// https://html.spec.whatwg.org/multipage/server-sent-events.html#dom-eventsource-url
String url() const { return MUST(String::from_byte_string(m_url.serialize())); }
String url() const { return m_url.serialize(); }
// https://html.spec.whatwg.org/multipage/server-sent-events.html#dom-eventsource-withcredentials
bool with_credentials() const { return m_with_credentials; }

View file

@ -191,7 +191,7 @@ String FormAssociatedElement::form_action() const
}
auto document_base_url = html_element.document().base_url();
return MUST(document_base_url.complete_url(form_action_attribute.value()).to_string());
return document_base_url.complete_url(form_action_attribute.value()).to_string();
}
WebIDL::ExceptionOr<void> FormAssociatedElement::set_form_action(String const& value)

View file

@ -100,7 +100,7 @@ String HTMLBaseElement::href() const
return url;
// 5. Return the serialization of urlRecord.
return MUST(url_record.to_string());
return url_record.to_string();
}
// https://html.spec.whatwg.org/multipage/semantics.html#dom-base-href

View file

@ -312,7 +312,7 @@ String HTMLCanvasElement::to_data_url(StringView type, JS::Value quality)
if (base64_encoded_or_error.is_error()) {
return "data:,"_string;
}
return MUST(URL::create_with_data(file.value().mime_type, base64_encoded_or_error.release_value(), true).to_string());
return URL::create_with_data(file.value().mime_type, base64_encoded_or_error.release_value(), true).to_string();
}
// https://html.spec.whatwg.org/multipage/canvas.html#dom-canvas-toblob

View file

@ -593,7 +593,7 @@ String HTMLFormElement::action() const
return document().url_string();
}
return MUST(document().base_url().complete_url(form_action_attribute.value()).to_string());
return document().base_url().complete_url(form_action_attribute.value()).to_string();
}
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-fs-action

View file

@ -424,7 +424,7 @@ String HTMLHyperlinkElementUtils::href() const
return href_content_attribute.release_value();
// 5. Return url, serialized.
return MUST(String::from_byte_string(url->serialize()));
return url->serialize();
}
// https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-href
@ -438,7 +438,7 @@ WebIDL::ExceptionOr<void> HTMLHyperlinkElementUtils::set_href(String href)
void HTMLHyperlinkElementUtils::update_href()
{
// To update href, set the element's href content attribute's value to the element's url, serialized.
MUST(set_hyperlink_element_utils_href(MUST(String::from_byte_string(m_url->serialize()))));
MUST(set_hyperlink_element_utils_href(m_url->serialize()));
}
bool HTMLHyperlinkElementUtils::cannot_navigate() const
@ -492,7 +492,7 @@ void HTMLHyperlinkElementUtils::follow_the_hyperlink(Optional<String> hyperlink_
if (!url.is_valid())
return;
auto url_string = MUST(url.to_string());
auto url_string = url.to_string();
// 10. If hyperlinkSuffix is non-null, then append it to urlString.
if (hyperlink_suffix.has_value()) {

View file

@ -306,7 +306,7 @@ String HTMLImageElement::current_src() const
auto current_url = m_current_request->current_url();
if (!current_url.is_valid())
return {};
return MUST(current_url.to_string());
return current_url.to_string();
}
// https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-decode

View file

@ -433,7 +433,7 @@ void HTMLLinkElement::process_stylesheet_resource(bool success, Fetch::Infrastru
if (m_loaded_style_sheet) {
Optional<String> location;
if (!response.url_list().is_empty())
location = MUST(response.url_list().first().to_string());
location = response.url_list().first().to_string();
document().style_sheets().create_a_css_style_sheet(
"text/css"_string,

View file

@ -628,7 +628,7 @@ public:
// would have resulted from parsing the URL specified by candidate's src attribute's value relative to the
// candidate's node document when the src attribute was last changed.
auto url_record = m_candidate->document().parse_url(candiate_src);
auto url_string = MUST(url_record.to_string());
auto url_string = url_record.to_string();
// 4. ⌛ If urlString was not obtained successfully, then end the synchronous section, and jump down to the failed
// with elements step below.
@ -875,7 +875,7 @@ WebIDL::ExceptionOr<void> HTMLMediaElement::select_resource()
// 3. ⌛ If urlString was obtained successfully, set the currentSrc attribute to urlString.
if (url_record.is_valid())
m_current_src = MUST(url_record.to_string());
m_current_src = url_record.to_string();
// 4. End the synchronous section, continuing the remaining steps in parallel.

View file

@ -137,7 +137,7 @@ String HTMLObjectElement::data() const
if (!data.has_value())
return {};
return MUST(document().parse_url(*data).to_string());
return document().parse_url(*data).to_string();
}
GC::Ptr<Layout::Node> HTMLObjectElement::create_layout_node(CSS::StyleProperties style)

View file

@ -95,15 +95,13 @@ URL::URL Location::url() const
// https://html.spec.whatwg.org/multipage/history.html#dom-location-href
WebIDL::ExceptionOr<String> Location::href() const
{
auto& vm = this->vm();
// 1. If this's relevant Document is non-null and its origin is not same origin-domain with the entry settings object's origin, then throw a "SecurityError" DOMException.
auto const relevant_document = this->relevant_document();
if (relevant_document && !relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_string);
// 2. Return this's url, serialized.
return TRY_OR_THROW_OOM(vm, String::from_byte_string(url().serialize()));
return url().serialize();
}
// https://html.spec.whatwg.org/multipage/history.html#the-location-interface:dom-location-href-2

View file

@ -1600,7 +1600,7 @@ WebIDL::ExceptionOr<GC::Ptr<DOM::Document>> Navigable::evaluate_javascript_url(U
auto url_string = url.serialize();
// 2. Let encodedScriptSource be the result of removing the leading "javascript:" from urlString.
auto encoded_script_source = url_string.substring_view(11, url_string.length() - 11);
auto encoded_script_source = url_string.bytes_as_string_view().substring_view(11);
// 3. Let scriptSource be the UTF-8 decoding of the percent-decoding of encodedScriptSource.
auto script_source = URL::percent_decode(encoded_script_source);

View file

@ -40,10 +40,10 @@ void NavigationDestination::visit_edges(JS::Cell::Visitor& visitor)
}
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigationdestination-url
WebIDL::ExceptionOr<String> NavigationDestination::url() const
String NavigationDestination::url() const
{
// The url getter steps are to return this's URL, serialized.
return TRY_OR_THROW_OOM(vm(), String::from_byte_string(m_url.serialize()));
return m_url.serialize();
}
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigationdestination-key

View file

@ -20,7 +20,7 @@ class NavigationDestination : public Bindings::PlatformObject {
public:
[[nodiscard]] static GC::Ref<NavigationDestination> create(JS::Realm&);
WebIDL::ExceptionOr<String> url() const;
String url() const;
String key() const;
String id() const;
i64 index() const;

View file

@ -44,7 +44,7 @@ void NavigationHistoryEntry::visit_edges(JS::Cell::Visitor& visitor)
}
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigationhistoryentry-url
WebIDL::ExceptionOr<Optional<String>> NavigationHistoryEntry::url() const
Optional<String> NavigationHistoryEntry::url() const
{
// The url getter steps are:
// 1. Let document be this's relevant global object's associated Document.
@ -65,7 +65,7 @@ WebIDL::ExceptionOr<Optional<String>> NavigationHistoryEntry::url() const
return OptionalNone {};
// 5. Return she's URL, serialized.
return TRY_OR_THROW_OOM(vm(), String::from_byte_string(she->url().serialize()));
return she->url().serialize();
}
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#concept-navigationhistoryentry-key

View file

@ -18,7 +18,7 @@ class NavigationHistoryEntry : public DOM::EventTarget {
public:
[[nodiscard]] static GC::Ref<NavigationHistoryEntry> create(JS::Realm&, GC::Ref<SessionHistoryEntry>);
WebIDL::ExceptionOr<Optional<String>> url() const;
Optional<String> url() const;
String key() const;
String id() const;
i64 index() const;

View file

@ -127,7 +127,7 @@ WebIDL::ExceptionOr<URL::URL> resolve_module_specifier(Optional<Script&> referri
auto as_url = resolve_url_like_module_specifier(specifier, *base_url);
// 8. Let normalizedSpecifier be the serialization of asURL, if asURL is non-null; otherwise, specifier.
auto normalized_specifier = as_url.has_value() ? as_url->serialize() : specifier;
auto normalized_specifier = as_url.has_value() ? as_url->serialize().to_byte_string() : specifier;
// 9. For each scopePrefix → scopeImports of importMap's scopes:
for (auto const& entry : import_map.scopes()) {
@ -137,7 +137,7 @@ WebIDL::ExceptionOr<URL::URL> resolve_module_specifier(Optional<Script&> referri
auto const& scope_imports = entry.value;
// 1. If scopePrefix is baseURLString, or if scopePrefix ends with U+002F (/) and scopePrefix is a code unit prefix of baseURLString, then:
if (scope_prefix == base_url_string || (scope_prefix.ends_with("/"sv) && Infra::is_code_unit_prefix(scope_prefix, base_url_string))) {
if (scope_prefix == base_url_string || (scope_prefix.ends_with('/') && Infra::is_code_unit_prefix(scope_prefix, base_url_string))) {
// 1. Let scopeImportsMatch be the result of resolving an imports match given normalizedSpecifier, asURL, and scopeImports.
auto scope_imports_match = TRY(resolve_imports_match(normalized_specifier, as_url, scope_imports));
@ -202,7 +202,7 @@ WebIDL::ExceptionOr<Optional<URL::URL>> resolve_imports_match(ByteString const&
auto after_prefix = normalized_specifier.substring(specifier_key.length());
// 4. Assert: resolutionResult, serialized, ends with U+002F (/), as enforced during parsing.
VERIFY(resolution_result->serialize().ends_with("/"sv));
VERIFY(resolution_result->serialize().ends_with('/'));
// 5. Let url be the result of URL parsing afterPrefix with resolutionResult.
auto url = DOMURL::parse(after_prefix, *resolution_result);

View file

@ -107,7 +107,7 @@ Optional<DeprecatedFlyString> normalise_specifier_key(JS::Realm& realm, Deprecat
// 3. If url is not null, then return the serialization of url.
if (url.has_value())
return url->serialize();
return url->serialize().to_byte_string();
// 4. Return specifierKey.
return specifier_key;
@ -160,7 +160,7 @@ WebIDL::ExceptionOr<ModuleSpecifierMap> sort_and_normalise_module_specifier_map(
}
// 6. If specifierKey ends with U+002F (/), and the serialization of addressURL does not end with U+002F (/), then:
if (specifier_key.as_string().ends_with("/"sv) && !address_url->serialize().ends_with("/"sv)) {
if (specifier_key.as_string().ends_with("/"sv) && !address_url->serialize().ends_with('/')) {
// 1. The user agent may report a warning to the console indicating that an invalid address was given for the specifier key specifierKey; since specifierKey ends with a slash, the address needs to as well.
auto& console = realm.intrinsics().console_object()->console();
console.output_debug_message(JS::Console::LogLevel::Warn,

View file

@ -17,7 +17,7 @@ GC_DEFINE_ALLOCATOR(WorkerLocation);
String WorkerLocation::href() const
{
// The href getter steps are to return this's WorkerGlobalScope object's url, serialized.
return MUST(String::from_byte_string(m_global_scope->url().serialize()));
return m_global_scope->url().serialize();
}
// https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-origin