LibURL+LibWeb: Ensure opaque paths always roundtrip

Corresponds to: 6c782003
This commit is contained in:
Shannon Booth 2025-03-15 15:38:09 +13:00 committed by Sam Atkins
commit ec3c545426
Notes: github-actions[bot] 2025-03-18 12:18:21 +00:00
17 changed files with 280 additions and 99 deletions

View file

@ -388,18 +388,10 @@ void DOMURL::set_search(String const& search)
// 1. Let url be thiss URL.
auto& url = m_url;
// 2. If the given value is the empty string:
// 2. If the given value is the empty string, then set urls query to null, empty thiss query objects list, and return.
if (search.is_empty()) {
// 1. Set urls query to null.
url.set_query({});
// 2. Empty thiss query objects list.
m_query->m_list.clear();
// 3. Potentially strip trailing spaces from an opaque path with this.
strip_trailing_spaces_from_an_opaque_path(*this);
// 4. Return.
return;
}
@ -438,15 +430,9 @@ String DOMURL::hash() const
// https://url.spec.whatwg.org/#ref-for-dom-url-hash%E2%91%A0
void DOMURL::set_hash(String const& hash)
{
// 1. If the given value is the empty string:
// 1. If the given value is the empty string, then set thiss URLs fragment to null and return.
if (hash.is_empty()) {
// 1. Set thiss URLs fragment to null.
m_url.set_fragment({});
// 2. Potentially strip trailing spaces from an opaque path with this.
strip_trailing_spaces_from_an_opaque_path(*this);
// 3. Return.
return;
}
@ -461,29 +447,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/#potentially-strip-trailing-spaces-from-an-opaque-path
void strip_trailing_spaces_from_an_opaque_path(DOMURL& url)
{
// 1. If urls URL does not have an opaque path, then return.
// FIXME: Reimplement this step once we modernize the URL implementation to meet the spec.
if (!url.cannot_be_a_base_url())
return;
// 2. If urls URLs fragment is non-null, then return.
if (url.fragment().has_value())
return;
// 3. If urls URLs query is non-null, then return.
if (url.query().has_value())
return;
// 4. Remove all trailing U+0020 SPACE code points from urls URLs path.
// NOTE: At index 0 since the first step tells us that the URL only has one path segment.
auto opaque_path = url.path_segment_at_index(0);
auto trimmed_path = opaque_path.trim(" "sv, TrimMode::Right);
url.set_paths({ trimmed_path });
}
// https://url.spec.whatwg.org/#concept-url-parser
Optional<URL::URL> parse(StringView input, Optional<URL::URL const&> base_url, Optional<StringView> encoding)
{

View file

@ -2,7 +2,7 @@
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
* Copyright (c) 2021, the SerenityOS developers.
* Copyright (c) 2023, networkException <networkexception@serenityos.org>
* Copyright (c) 2024, Shannon Booth <shannon@serenityos.org>
* Copyright (c) 2024-2025, Shannon Booth <shannon@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -92,9 +92,6 @@ private:
GC::Ref<URLSearchParams> m_query;
};
// https://url.spec.whatwg.org/#potentially-strip-trailing-spaces-from-an-opaque-path
void strip_trailing_spaces_from_an_opaque_path(DOMURL& url);
// https://url.spec.whatwg.org/#concept-url-parser
Optional<URL::URL> parse(StringView input, Optional<URL::URL const&> base_url = {}, Optional<StringView> encoding = {});

View file

@ -1,6 +1,6 @@
/*
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
* Copyright (c) 2023-2024, Shannon Booth <shannon@serenityos.org>
* Copyright (c) 2023-2025, Shannon Booth <shannon@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -229,10 +229,6 @@ void URLSearchParams::update()
// 4. Set querys URL objects URLs query to serializedQuery.
m_url->set_query({}, serialized_query);
// 5. If serializedQuery is null, then potentially strip trailing spaces from an opaque path with querys URL object.
if (!serialized_query.has_value())
strip_trailing_spaces_from_an_opaque_path(*m_url);
}
// https://url.spec.whatwg.org/#dom-urlsearchparams-delete