LibWeb: Use correct URL parsing methods throughout LibWeb

There are essentially 3 URL parsing AOs defined by the spec:
1. Parse a URL
2. Encoding parse a URL
3. Encoding parse a URL and serialize the result

Further, these are replicated between the Document and the ESO.

This patch defines these methods in accordance with the spec and updates
existing users to invoke the correct method. In places where the correct
method is ambiguous, we use the encoding parser to preserve existing ad-
hoc behavior.
This commit is contained in:
Timothy Flynn 2024-12-06 16:24:08 -05:00 committed by Tim Flynn
parent 0b2fe008a3
commit fe891727dc
Notes: github-actions[bot] 2024-12-10 18:38:02 +00:00
25 changed files with 119 additions and 57 deletions

View file

@ -1081,7 +1081,38 @@ URL::URL Document::parse_url(StringView url) const
auto base_url = this->base_url();
// 2. Return the result of applying the URL parser to url, with baseURL.
return DOMURL::parse(url, base_url, Optional<StringView> { m_encoding });
return DOMURL::parse(url, base_url);
}
// https://html.spec.whatwg.org/multipage/urls-and-fetching.html#encoding-parsing-a-url
URL::URL Document::encoding_parse_url(StringView url) const
{
// 1. Let encoding be UTF-8.
// 2. If environment is a Document object, then set encoding to environment's character encoding.
auto encoding = encoding_or_default();
// 3. Otherwise, if environment's relevant global object is a Window object, set encoding to environment's relevant
// global object's associated Document's character encoding.
// 4. Let baseURL be environment's base URL, if environment is a Document object; otherwise environment's API base URL.
auto base_url = this->base_url();
// 5. Return the result of applying the URL parser to url, with baseURL and encoding.
return DOMURL::parse(url, base_url, encoding);
}
// https://html.spec.whatwg.org/multipage/urls-and-fetching.html#encoding-parsing-and-serializing-a-url
Optional<String> Document::encoding_parse_and_serialize_url(StringView url) const
{
// 1. Let url be the result of encoding-parsing a URL given url, relative to environment.
auto parsed_url = encoding_parse_url(url);
// 2. If url is failure, then return failure.
if (!parsed_url.is_valid())
return {};
// 3. Return the result of applying the URL serializer to url.
return parsed_url.serialize();
}
void Document::set_needs_layout()