Explorar el Código

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
Shannon Booth hace 8 meses
padre
commit
ea971792b5

+ 1 - 0
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'

+ 10 - 0
Tests/LibWeb/Text/input/HTML/Location-set-invalid-href-url.html

@@ -0,0 +1,10 @@
+<script src="../include.js"></script>
+<script>
+    test(() => {
+        try {
+            location.href = 'http://@:www.invalid-url.com';
+        } catch (e) {
+            println(`Error setting href: ${e}`);
+        }
+    });
+</script>

+ 6 - 4
Userland/Libraries/LibWeb/HTML/Location.cpp

@@ -109,7 +109,7 @@ WebIDL::ExceptionOr<String> Location::href() const
 // https://html.spec.whatwg.org/multipage/history.html#the-location-interface:dom-location-href-2
 WebIDL::ExceptionOr<void> Location::set_href(String const& new_href)
 {
-    auto& vm = this->vm();
+    auto& realm = this->realm();
     auto& window = verify_cast<HTML::Window>(HTML::current_global_object());
 
     // 1. If this's relevant Document is null, then return.
@@ -117,12 +117,14 @@ WebIDL::ExceptionOr<void> 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());
+
+    // 3. If url is failure, then throw a "SyntaxError" DOMException.
     if (!href_url.is_valid())
-        return vm.throw_completion<JS::URIError>(TRY_OR_THROW_OOM(vm, String::formatted("Invalid URL '{}'", new_href)));
+        return WebIDL::SyntaxError::create(realm, MUST(String::formatted("Invalid URL '{}'", new_href)));
 
-    // 3. Location-object navigate given the resulting URL record.
+    // 4. Location-object navigate this to url.
     TRY(navigate(href_url));
 
     return {};