Skip to content

Commit

Permalink
LibWeb: Actually set empty serialized query to OptionalNone
Browse files Browse the repository at this point in the history
Because the type returned by to_string is a String, _not_ an
Optional<String>, the following code:

if (serialized_query.is_empty())
    serialized_query = {};

Was achieving nothing at all! Make sure that the type is an
Optional<String> so that we're not just setting an empty string to an
empty string.
  • Loading branch information
shannonbooth authored and tcl3 committed Aug 12, 2024
1 parent 1ba6dbd commit d755a83
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
https://www.birdoftheyear.org.nz/?
https://www.birdoftheyear.org.nz/
9 changes: 9 additions & 0 deletions Tests/LibWeb/Text/input/URL/search-params-with-no-query.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script src="../include.js"></script>
<script>
test(() => {
let url = new URL("https://www.birdoftheyear.org.nz/?")
println(url.href);
url.searchParams.sort()
println(url.href);
});
</script>
7 changes: 4 additions & 3 deletions Userland/Libraries/LibWeb/DOMURL/URLSearchParams.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
* Copyright (c) 2023, Shannon Booth <shannon@serenityos.org>
* Copyright (c) 2023-2024, Shannon Booth <shannon@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
Expand Down Expand Up @@ -213,17 +213,18 @@ void URLSearchParams::append(String const& name, String const& value)
update();
}

// https://url.spec.whatwg.org/#concept-urlsearchparams-update
void URLSearchParams::update()
{
// 1. If query’s URL object is null, then return.
if (!m_url)
return;

// 2. Let serializedQuery be the serialization of query’s list.
auto serialized_query = to_string();
Optional<String> serialized_query = to_string();

// 3. If serializedQuery is the empty string, then set serializedQuery to null.
if (serialized_query.is_empty())
if (serialized_query == String {})
serialized_query = {};

// 4. Set query’s URL object’s URL’s query to serializedQuery.
Expand Down

0 comments on commit d755a83

Please sign in to comment.