LibWeb+LibURL: Consolidate Origin parsing and serialization into LibURL

Because of the previous awkward factoring of Origin we had two
implementations of Origin serializing and creation. Move the
implementation of DOMURL::url_origin into URL::origin, and
instead use the implemenation of URL::Origin::serialize for
serialization (replacing URL::serialize_origin).

This happens to fix 8 URL subtests as the two implemenations had
diverged, and URL::serialize_origin was previously missing the spec
changes of: whatwg/url@eee49fd and whatwg/url@fff33c3
This commit is contained in:
Shannon Booth 2024-10-05 17:03:51 +13:00 committed by Andreas Kling
commit 501f92b54e
Notes: github-actions[bot] 2024-10-05 08:47:31 +00:00
21 changed files with 77 additions and 101 deletions

View file

@ -141,7 +141,7 @@ WebIDL::ExceptionOr<void> DOMURL::revoke_object_url(JS::VM& vm, StringView url)
return {};
// 3. Let origin be the origin of url record.
auto origin = url_origin(url_record);
auto origin = url_record.origin();
// 4. Let settings be the current settings object.
auto& settings = HTML::current_settings_object();
@ -218,7 +218,7 @@ WebIDL::ExceptionOr<String> DOMURL::origin() const
auto& vm = realm().vm();
// The origin getter steps are to return the serialization of thiss URLs origin. [HTML]
return TRY_OR_THROW_OOM(vm, String::from_byte_string(m_url.serialize_origin()));
return TRY_OR_THROW_OOM(vm, String::from_byte_string(m_url.origin().serialize()));
}
// https://url.spec.whatwg.org/#dom-url-protocol
@ -478,58 +478,6 @@ void DOMURL::set_hash(String const& hash)
(void)URL::Parser::basic_parse(input, {}, &m_url, URL::Parser::State::Fragment);
}
// https://url.spec.whatwg.org/#concept-url-origin
URL::Origin url_origin(URL::URL const& url)
{
// FIXME: We should probably have an extended version of URL::URL for LibWeb instead of standalone functions like this.
// The origin of a URL url is the origin returned by running these steps, switching on urls scheme:
// -> "blob"
if (url.scheme() == "blob"sv) {
auto url_string = url.to_string().release_value_but_fixme_should_propagate_errors();
// 1. If urls blob URL entry is non-null, then return urls blob URL entrys environments origin.
if (url.blob_url_entry().has_value())
return url.blob_url_entry()->environment_origin;
// 2. Let pathURL be the result of parsing the result of URL path serializing url.
auto path_url = parse(url.serialize_path());
// 3. If pathURL is failure, then return a new opaque origin.
if (!path_url.is_valid())
return URL::Origin {};
// 4. If pathURLs scheme is "http", "https", or "file", then return pathURLs origin.
if (path_url.scheme().is_one_of("http"sv, "https"sv, "file"sv))
return url_origin(path_url);
// 5. Return a new opaque origin.
return URL::Origin {};
}
// -> "ftp"
// -> "http"
// -> "https"
// -> "ws"
// -> "wss"
if (url.scheme().is_one_of("ftp"sv, "http"sv, "https"sv, "ws"sv, "wss"sv)) {
// Return the tuple origin (urls scheme, urls host, urls port, null).
return URL::Origin(url.scheme().to_byte_string(), url.host(), url.port().value_or(0));
}
// -> "file"
// AD-HOC: Our resource:// is basically an alias to file://
if (url.scheme() == "file"sv || url.scheme() == "resource"sv) {
// Unfortunate as it is, this is left as an exercise to the reader. When in doubt, return a new opaque origin.
// Note: We must return an origin with the `file://' protocol for `file://' iframes to work from `file://' pages.
return URL::Origin(url.scheme().to_byte_string(), String {}, 0);
}
// -> Otherwise
// Return a new opaque origin.
return URL::Origin {};
}
// https://url.spec.whatwg.org/#concept-domain
bool host_is_domain(URL::Host const& host)
{

View file

@ -92,7 +92,6 @@ private:
JS::NonnullGCPtr<URLSearchParams> m_query;
};
URL::Origin url_origin(URL::URL const&);
bool host_is_domain(URL::Host const&);
// https://url.spec.whatwg.org/#potentially-strip-trailing-spaces-from-an-opaque-path