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
This commit is contained in:
Khaled Lakehal 2024-12-25 20:32:25 +01:00 committed by Tim Ledbetter
parent 9585aeafda
commit b0e061b943
Notes: github-actions[bot] 2024-12-28 10:51:36 +00:00
3 changed files with 43 additions and 1 deletions

View file

@ -413,7 +413,10 @@ WebIDL::ExceptionOr<void> Location::set_hash(String const& value)
(void)URL::Parser::basic_parse(input, {}, &copy_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.