From 67ea56da59f29861eef0bb980b90a9e24fd61cac Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Mon, 13 May 2024 16:09:58 +1200 Subject: [PATCH] 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 --- Userland/Libraries/LibWeb/DOMURL/DOMURL.cpp | 36 +++++++++++++-------- Userland/Libraries/LibWeb/DOMURL/DOMURL.h | 3 ++ 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/Userland/Libraries/LibWeb/DOMURL/DOMURL.cpp b/Userland/Libraries/LibWeb/DOMURL/DOMURL.cpp index 14fe78fca60..5d4049e4d05 100644 --- a/Userland/Libraries/LibWeb/DOMURL/DOMURL.cpp +++ b/Userland/Libraries/LibWeb/DOMURL/DOMURL.cpp @@ -2,6 +2,7 @@ * Copyright (c) 2021, Idan Horowitz * Copyright (c) 2021, the SerenityOS developers. * Copyright (c) 2023, networkException + * Copyright (c) 2024, Shannon Booth * * SPDX-License-Identifier: BSD-2-Clause */ @@ -50,6 +51,25 @@ static Optional parse_api_url(String const& url, Optional cons return parsed.is_valid() ? parsed : Optional {}; } +// https://url.spec.whatwg.org/#url-initialize +JS::NonnullGCPtr DOMURL::initialize_a_url(JS::Realm& realm, URL::URL const& url_record) +{ + // 1. Let query be urlRecord’s query, if that is non-null; otherwise the empty string. + auto query = url_record.query().value_or(String {}); + + // 2. Set url’s URL to urlRecord. + // 3. Set url’s query object to a new URLSearchParams object. + auto query_object = MUST(URLSearchParams::construct_impl(realm, query)); + + // 4. Initialize url’s query object with query. + auto result_url = DOMURL::create(realm, url_record, move(query_object)); + + // 5. Set url’s query object’s URL object to url. + result_url->m_query->m_url = result_url; + + return result_url; +} + // https://url.spec.whatwg.org/#dom-url-url WebIDL::ExceptionOr> DOMURL::construct_impl(JS::Realm& realm, String const& url, Optional const& base) { @@ -60,20 +80,8 @@ WebIDL::ExceptionOr> DOMURL::construct_impl(JS::Realm& if (!parsed_url.has_value()) return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid URL"sv }; - // 3. Let query be parsedURL’s query, if that is non-null, and the empty string otherwise. - auto query = parsed_url->query().value_or(String {}); - - // 4. Set this’s URL to parsedURL. - // 5. Set this’s query object to a new URLSearchParams object. - auto query_object = MUST(URLSearchParams::construct_impl(realm, query)); - - // 6. Initialize this’s query object with query. - auto result_url = DOMURL::create(realm, parsed_url.release_value(), move(query_object)); - - // 7. Set this’s query object’s URL object to this. - result_url->m_query->m_url = result_url; - - return result_url; + // 3. Initialize this with parsedURL. + return initialize_a_url(realm, parsed_url.value()); } DOMURL::DOMURL(JS::Realm& realm, URL::URL url, JS::NonnullGCPtr query) diff --git a/Userland/Libraries/LibWeb/DOMURL/DOMURL.h b/Userland/Libraries/LibWeb/DOMURL/DOMURL.h index b69ce044160..671ed0aba36 100644 --- a/Userland/Libraries/LibWeb/DOMURL/DOMURL.h +++ b/Userland/Libraries/LibWeb/DOMURL/DOMURL.h @@ -2,6 +2,7 @@ * Copyright (c) 2021, Idan Horowitz * Copyright (c) 2021, the SerenityOS developers. * Copyright (c) 2023, networkException + * Copyright (c) 2024, Shannon Booth * * SPDX-License-Identifier: BSD-2-Clause */ @@ -81,6 +82,8 @@ public: private: DOMURL(JS::Realm&, URL::URL, JS::NonnullGCPtr query); + static JS::NonnullGCPtr initialize_a_url(JS::Realm&, URL::URL const&); + virtual void initialize(JS::Realm&) override; virtual void visit_edges(Cell::Visitor&) override;