From b0e061b943192227a1efe18b79fa2ae70a7569a7 Mon Sep 17 00:00:00 2001 From: Khaled Lakehal Date: Wed, 25 Dec 2024 20:32:25 +0100 Subject: [PATCH] LibWeb/HTML: Fix URL fragment comparison triggering unwanted events This update ensures consistent handling of URL fragments, treating null fragments as empty strings in `Location::set_hash` method. This fixes all tests in https://wpt.live/html/browsers/browsing-the-web/history-traversal/hash-empty-string.html --- Libraries/LibWeb/HTML/Location.cpp | 5 ++- .../history-traversal/hash-empty-string.txt | 8 +++++ .../history-traversal/hash-empty-string.html | 31 +++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 Tests/LibWeb/Text/expected/wpt-import/html/browsers/browsing-the-web/history-traversal/hash-empty-string.txt create mode 100644 Tests/LibWeb/Text/input/wpt-import/html/browsers/browsing-the-web/history-traversal/hash-empty-string.html diff --git a/Libraries/LibWeb/HTML/Location.cpp b/Libraries/LibWeb/HTML/Location.cpp index d561cb3a6ee..788f1eb8a20 100644 --- a/Libraries/LibWeb/HTML/Location.cpp +++ b/Libraries/LibWeb/HTML/Location.cpp @@ -413,7 +413,10 @@ WebIDL::ExceptionOr Location::set_hash(String const& value) (void)URL::Parser::basic_parse(input, {}, ©_url, URL::Parser::State::Fragment); // 7. If copyURL's fragment is this's url's fragment, then return. - if (copy_url.fragment() == this->url().fragment()) + // NOTE: Ignore null values when comparing fragments. This behavior is not explicitly mentioned in the specs, potential bug? + auto copy_url_fragment = copy_url.fragment().has_value() ? copy_url.fragment() : String {}; + auto this_url_fragment = this->url().fragment().has_value() ? this->url().fragment() : String {}; + if (copy_url_fragment == this_url_fragment) return {}; // 8. Location-object navigate this to copyURL. diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/browsers/browsing-the-web/history-traversal/hash-empty-string.txt b/Tests/LibWeb/Text/expected/wpt-import/html/browsers/browsing-the-web/history-traversal/hash-empty-string.txt new file mode 100644 index 00000000000..77f26e17bcc --- /dev/null +++ b/Tests/LibWeb/Text/expected/wpt-import/html/browsers/browsing-the-web/history-traversal/hash-empty-string.txt @@ -0,0 +1,8 @@ +Harness status: OK + +Found 3 tests + +3 Pass +Pass URL has no hash +Pass changing the hash from an empty string to an empty string does not trigger a popstate event +Pass changing the hash from an empty string to an empty string does not trigger a hashchange event \ No newline at end of file diff --git a/Tests/LibWeb/Text/input/wpt-import/html/browsers/browsing-the-web/history-traversal/hash-empty-string.html b/Tests/LibWeb/Text/input/wpt-import/html/browsers/browsing-the-web/history-traversal/hash-empty-string.html new file mode 100644 index 00000000000..4670740cf6f --- /dev/null +++ b/Tests/LibWeb/Text/input/wpt-import/html/browsers/browsing-the-web/history-traversal/hash-empty-string.html @@ -0,0 +1,31 @@ + + + + + + + +