1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 06:58:11 +00:00
serenity/Userland/Libraries/LibWeb/URL/URLSearchParams.cpp
Brian Gianforcaro 7852b0fbc3 LibWeb: Identical sub-expression bug in URLSearchParams::set
Flagged by sonarcloud, we appear to want to make sure the entry.name
matches the name we are searching for duplicates of.

Link:
https://sonarcloud.io/project/issues?fileUuids=AXvdC606QRvsO1F91p5C&id=SerenityOS_serenity&open=AXvdC_NEQRvsO1F91p6j
2021-09-13 14:05:22 +00:00

137 lines
4.6 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/QuickSort.h>
#include <AK/Utf8View.h>
#include <LibWeb/URL/URLSearchParams.h>
namespace Web::URL {
DOM::ExceptionOr<NonnullRefPtr<URLSearchParams>> URLSearchParams::create_with_global_object(Bindings::WindowObject&, String const& init)
{
// 1. If init is a string and starts with U+003F (?), then remove the first code point from init.
StringView stripped_init = init.substring_view(init.starts_with('?'));
// 2. Initialize this with init.
// URLSearchParams init from this point forward
// TODO
// 1. If init is a sequence, then for each pair in init:
// a. If pair does not contain exactly two items, then throw a TypeError.
// b. Append a new name-value pair whose name is pairs first item, and value is pairs second item, to querys list.
// TODO
// 2. Otherwise, if init is a record, then for each name → value of init, append a new name-value pair whose name is name and value is value, to querys list.
// 3. Otherwise:
// a. Assert: init is a string.
// b. Set querys list to the result of parsing init.
return URLSearchParams::create(url_decode(stripped_init));
}
void URLSearchParams::append(String const& name, String const& value)
{
// 1. Append a new name-value pair whose name is name and value is value, to list.
m_list.empend(name, value);
// 2. Update this.
update();
}
void URLSearchParams::update()
{
// TODO
// 1. If querys URL object is null, then return.
// 2. Let serializedQuery be the serialization of querys list.
// 3. If serializedQuery is the empty string, then set serializedQuery to null.
// 4. Set querys URL objects URLs query to serializedQuery.
}
void URLSearchParams::delete_(String const& name)
{
// 1. Remove all name-value pairs whose name is name from list.
m_list.remove_all_matching([&name](auto& entry) {
return entry.name == name;
});
// 2. Update this.
update();
}
String URLSearchParams::get(String const& name)
{
// return the value of the first name-value pair whose name is name in thiss list, if there is such a pair, and null otherwise.
auto result = m_list.find_if([&name](auto& entry) {
return entry.name == name;
});
if (result.is_end())
return {};
return result->value;
}
bool URLSearchParams::has(String const& name)
{
// return true if there is a name-value pair whose name is name in thiss list, and false otherwise.
return !m_list.find_if([&name](auto& entry) {
return entry.name == name;
})
.is_end();
}
void URLSearchParams::set(const String& name, const String& value)
{
// 1. If thiss list contains any name-value pairs whose name is name, then set the value of the first such name-value pair to value and remove the others.
auto existing = m_list.find_if([&name](auto& entry) {
return entry.name == name;
});
if (!existing.is_end()) {
existing->value = value;
m_list.remove_all_matching([&name, &existing](auto& entry) {
return &entry != &*existing && entry.name == name;
});
}
// 2. Otherwise, append a new name-value pair whose name is name and value is value, to thiss list.
else {
m_list.empend(name, value);
}
// 3. Update this.
update();
}
void URLSearchParams::sort()
{
// 1. Sort all name-value pairs, if any, by their names. Sorting must be done by comparison of code units. The relative order between name-value pairs with equal names must be preserved.
quick_sort(m_list.begin(), m_list.end(), [](auto& a, auto& b) {
Utf8View a_code_points { a.name };
Utf8View b_code_points { b.name };
if (a_code_points.starts_with(b_code_points))
return false;
if (b_code_points.starts_with(a_code_points))
return true;
for (auto k = a_code_points.begin(), l = b_code_points.begin();
k != a_code_points.end() && l != b_code_points.end();
++k, ++l) {
if (*k != *l) {
if (*k < *l) {
return true;
} else {
return false;
}
}
}
VERIFY_NOT_REACHED();
});
// 2. Update this.
update();
}
String URLSearchParams::to_string()
{
// return the serialization of thiss list.
return url_encode(m_list, AK::URL::PercentEncodeSet::ApplicationXWWWFormUrlencoded);
}
}