LibWeb: Implement the setter for location.protocol

This commit is contained in:
Tim Ledbetter 2024-08-16 22:00:29 +01:00 committed by Andreas Kling
commit 1365289d98
Notes: github-actions[bot] 2024-08-17 05:40:53 +00:00
3 changed files with 42 additions and 3 deletions

View file

@ -0,0 +1 @@
Setting location.protocol to an invalid value throws SyntaxError

View file

@ -0,0 +1,11 @@
<!DOCTYPE html>
<script src="../include.js"></script>
<script>
test(() => {
try {
window.location.protocol = "\xff";
} catch (e) {
println(`Setting location.protocol to an invalid value throws ${e.name}`);
}
});
</script>

View file

@ -156,10 +156,37 @@ WebIDL::ExceptionOr<String> Location::protocol() const
return TRY_OR_THROW_OOM(vm, String::formatted("{}:", url().scheme())); return TRY_OR_THROW_OOM(vm, String::formatted("{}:", url().scheme()));
} }
WebIDL::ExceptionOr<void> Location::set_protocol(String const&) // https://html.spec.whatwg.org/multipage/history.html#dom-location-protocol
WebIDL::ExceptionOr<void> Location::set_protocol(String const& value)
{ {
auto& vm = this->vm(); auto relevant_document = this->relevant_document();
return vm.throw_completion<JS::InternalError>(JS::ErrorType::NotImplemented, "Location.protocol setter");
// 1. If this's relevant Document is null, then return.
if (!relevant_document)
return {};
// 2. If this's relevant Document's origin is not same origin-domain with the entry settings object's origin, then throw a "SecurityError" DOMException.
if (!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"_fly_string);
// 3. Let copyURL be a copy of this's url.
auto copy_url = this->url();
// 4. Let possibleFailure be the result of basic URL parsing the given value, followed by ":", with copyURL as url and scheme start state as state override.
auto possible_failure = URL::Parser::basic_parse(value, {}, &copy_url, URL::Parser::State::SchemeStart);
// 5. If possibleFailure is failure, then throw a "SyntaxError" DOMException.
if (!possible_failure.is_valid())
return WebIDL::SyntaxError::create(realm(), MUST(String::formatted("Failed to set protocol. '{}' is an invalid protocol", value)));
// 6. if copyURL's scheme is not an HTTP(S) scheme, then terminate these steps.
if (!(copy_url.scheme() == "http"sv || copy_url.scheme() == "https"sv))
return {};
// 7. Location-object navigate this to copyURL.
TRY(navigate(copy_url));
return {};
} }
// https://html.spec.whatwg.org/multipage/history.html#dom-location-host // https://html.spec.whatwg.org/multipage/history.html#dom-location-host