LibWeb: Factor out an 'initialize a URL' AO

This is a small refactor in the URL spec to avoid duplication as part of
the introduction of URL.parse, see:

https://github.com/whatwg/url/commit/58acb0
This commit is contained in:
Shannon Booth 2024-05-13 16:09:58 +12:00 committed by Andreas Kling
commit 67ea56da59
Notes: sideshowbarker 2024-07-17 08:45:34 +09:00
2 changed files with 25 additions and 14 deletions

View file

@ -2,6 +2,7 @@
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org> * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
* Copyright (c) 2021, the SerenityOS developers. * Copyright (c) 2021, the SerenityOS developers.
* Copyright (c) 2023, networkException <networkexception@serenityos.org> * Copyright (c) 2023, networkException <networkexception@serenityos.org>
* Copyright (c) 2024, Shannon Booth <shannon@serenityos.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -50,6 +51,25 @@ static Optional<URL::URL> parse_api_url(String const& url, Optional<String> cons
return parsed.is_valid() ? parsed : Optional<URL::URL> {}; return parsed.is_valid() ? parsed : Optional<URL::URL> {};
} }
// https://url.spec.whatwg.org/#url-initialize
JS::NonnullGCPtr<DOMURL> DOMURL::initialize_a_url(JS::Realm& realm, URL::URL const& url_record)
{
// 1. Let query be urlRecords query, if that is non-null; otherwise the empty string.
auto query = url_record.query().value_or(String {});
// 2. Set urls URL to urlRecord.
// 3. Set urls query object to a new URLSearchParams object.
auto query_object = MUST(URLSearchParams::construct_impl(realm, query));
// 4. Initialize urls query object with query.
auto result_url = DOMURL::create(realm, url_record, move(query_object));
// 5. Set urls query objects URL object to url.
result_url->m_query->m_url = result_url;
return result_url;
}
// https://url.spec.whatwg.org/#dom-url-url // https://url.spec.whatwg.org/#dom-url-url
WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMURL>> DOMURL::construct_impl(JS::Realm& realm, String const& url, Optional<String> const& base) WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMURL>> DOMURL::construct_impl(JS::Realm& realm, String const& url, Optional<String> const& base)
{ {
@ -60,20 +80,8 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMURL>> DOMURL::construct_impl(JS::Realm&
if (!parsed_url.has_value()) if (!parsed_url.has_value())
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid URL"sv }; return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid URL"sv };
// 3. Let query be parsedURLs query, if that is non-null, and the empty string otherwise. // 3. Initialize this with parsedURL.
auto query = parsed_url->query().value_or(String {}); return initialize_a_url(realm, parsed_url.value());
// 4. Set thiss URL to parsedURL.
// 5. Set thiss query object to a new URLSearchParams object.
auto query_object = MUST(URLSearchParams::construct_impl(realm, query));
// 6. Initialize thiss query object with query.
auto result_url = DOMURL::create(realm, parsed_url.release_value(), move(query_object));
// 7. Set thiss query objects URL object to this.
result_url->m_query->m_url = result_url;
return result_url;
} }
DOMURL::DOMURL(JS::Realm& realm, URL::URL url, JS::NonnullGCPtr<URLSearchParams> query) DOMURL::DOMURL(JS::Realm& realm, URL::URL url, JS::NonnullGCPtr<URLSearchParams> query)

View file

@ -2,6 +2,7 @@
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org> * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
* Copyright (c) 2021, the SerenityOS developers. * Copyright (c) 2021, the SerenityOS developers.
* Copyright (c) 2023, networkException <networkexception@serenityos.org> * Copyright (c) 2023, networkException <networkexception@serenityos.org>
* Copyright (c) 2024, Shannon Booth <shannon@serenityos.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -81,6 +82,8 @@ public:
private: private:
DOMURL(JS::Realm&, URL::URL, JS::NonnullGCPtr<URLSearchParams> query); DOMURL(JS::Realm&, URL::URL, JS::NonnullGCPtr<URLSearchParams> query);
static JS::NonnullGCPtr<DOMURL> initialize_a_url(JS::Realm&, URL::URL const&);
virtual void initialize(JS::Realm&) override; virtual void initialize(JS::Realm&) override;
virtual void visit_edges(Cell::Visitor&) override; virtual void visit_edges(Cell::Visitor&) override;