From ea971792b5da5f10cadb4f88f49c9722d88b32f6 Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Sun, 6 Oct 2024 09:29:49 +1300 Subject: [PATCH] LibWeb: Throw a SyntaxError on invalid URL for Location href setter Aligning with a spec update, fixing 195 tests for: https://wpt.live/url/failure.html --- .../expected/HTML/Location-set-invalid-href-url.txt | 1 + .../input/HTML/Location-set-invalid-href-url.html | 10 ++++++++++ Userland/Libraries/LibWeb/HTML/Location.cpp | 12 +++++++----- 3 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 Tests/LibWeb/Text/expected/HTML/Location-set-invalid-href-url.txt create mode 100644 Tests/LibWeb/Text/input/HTML/Location-set-invalid-href-url.html diff --git a/Tests/LibWeb/Text/expected/HTML/Location-set-invalid-href-url.txt b/Tests/LibWeb/Text/expected/HTML/Location-set-invalid-href-url.txt new file mode 100644 index 00000000000..68cfb8c5b70 --- /dev/null +++ b/Tests/LibWeb/Text/expected/HTML/Location-set-invalid-href-url.txt @@ -0,0 +1 @@ +Error setting href: SyntaxError: Invalid URL 'http://@:www.invalid-url.com' diff --git a/Tests/LibWeb/Text/input/HTML/Location-set-invalid-href-url.html b/Tests/LibWeb/Text/input/HTML/Location-set-invalid-href-url.html new file mode 100644 index 00000000000..a9c61b0c675 --- /dev/null +++ b/Tests/LibWeb/Text/input/HTML/Location-set-invalid-href-url.html @@ -0,0 +1,10 @@ + + diff --git a/Userland/Libraries/LibWeb/HTML/Location.cpp b/Userland/Libraries/LibWeb/HTML/Location.cpp index 3b64dad0edb..b71f76e09f1 100644 --- a/Userland/Libraries/LibWeb/HTML/Location.cpp +++ b/Userland/Libraries/LibWeb/HTML/Location.cpp @@ -109,7 +109,7 @@ WebIDL::ExceptionOr Location::href() const // https://html.spec.whatwg.org/multipage/history.html#the-location-interface:dom-location-href-2 WebIDL::ExceptionOr Location::set_href(String const& new_href) { - auto& vm = this->vm(); + auto& realm = this->realm(); auto& window = verify_cast(HTML::current_global_object()); // 1. If this's relevant Document is null, then return. @@ -117,12 +117,14 @@ WebIDL::ExceptionOr Location::set_href(String const& new_href) if (!relevant_document) return {}; - // 2. Parse the given value relative to the entry settings object. If that failed, throw a TypeError exception. + // FIXME: 2. Let url be the result of encoding-parsing a URL given the given value, relative to the entry settings object. auto href_url = window.associated_document().parse_url(new_href.to_byte_string()); - if (!href_url.is_valid()) - return vm.throw_completion(TRY_OR_THROW_OOM(vm, String::formatted("Invalid URL '{}'", new_href))); - // 3. Location-object navigate given the resulting URL record. + // 3. If url is failure, then throw a "SyntaxError" DOMException. + if (!href_url.is_valid()) + return WebIDL::SyntaxError::create(realm, MUST(String::formatted("Invalid URL '{}'", new_href))); + + // 4. Location-object navigate this to url. TRY(navigate(href_url)); return {};