From 1ba6dbd86c0a94e5f068e6586199866f7de6354e Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Sun, 11 Aug 2024 11:27:18 +1200 Subject: [PATCH] LibWeb: Use a stable sort for searching URLSearchParams Quick sort is not a stable sort. This meant we had a subtle issue implementing this portion of the spec comment: > The relative order between name-value pairs with equal names must > be preserved. Switch to insertion sort which is a stable sort, and properly handles keys which are the same. Fixes 8 tests on https://wpt.live/url/urlsearchparams-sort.any.html --- .../URL/url-search-params-sort-order-stable.txt | 5 +++++ .../URL/url-search-params-sort-order-stable.html | 11 +++++++++++ Userland/Libraries/LibWeb/DOMURL/URLSearchParams.cpp | 2 +- 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 Tests/LibWeb/Text/expected/URL/url-search-params-sort-order-stable.txt create mode 100644 Tests/LibWeb/Text/input/URL/url-search-params-sort-order-stable.html diff --git a/Tests/LibWeb/Text/expected/URL/url-search-params-sort-order-stable.txt b/Tests/LibWeb/Text/expected/URL/url-search-params-sort-order-stable.txt new file mode 100644 index 00000000000..26c616c3ac7 --- /dev/null +++ b/Tests/LibWeb/Text/expected/URL/url-search-params-sort-order-stable.txt @@ -0,0 +1,5 @@ +=f&=t&=x&z=z +'' => 'f' +'' => 't' +'' => 'x' +'z' => 'z' diff --git a/Tests/LibWeb/Text/input/URL/url-search-params-sort-order-stable.html b/Tests/LibWeb/Text/input/URL/url-search-params-sort-order-stable.html new file mode 100644 index 00000000000..8598b8dfb7e --- /dev/null +++ b/Tests/LibWeb/Text/input/URL/url-search-params-sort-order-stable.html @@ -0,0 +1,11 @@ + + diff --git a/Userland/Libraries/LibWeb/DOMURL/URLSearchParams.cpp b/Userland/Libraries/LibWeb/DOMURL/URLSearchParams.cpp index a15e9dcdf23..f424ada69a5 100644 --- a/Userland/Libraries/LibWeb/DOMURL/URLSearchParams.cpp +++ b/Userland/Libraries/LibWeb/DOMURL/URLSearchParams.cpp @@ -323,7 +323,7 @@ void URLSearchParams::set(String const& name, String const& value) 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) { + insertion_sort(m_list, [](auto& a, auto& b) { Utf8View a_code_points { a.name }; Utf8View b_code_points { b.name };