LibURL/Pattern: Do not return errors in some canonicalization steps

Corresponds to: https://github.com/whatwg/urlpattern/commit/5c979a31
This commit is contained in:
Shannon Booth 2025-03-26 18:52:48 +13:00
parent 2d3794fed0
commit 6a48734a76
3 changed files with 21 additions and 34 deletions

View file

@ -151,7 +151,7 @@ PatternErrorOr<String> canonicalize_a_port(String const& port_value, Optional<St
}
// https://urlpattern.spec.whatwg.org/#canonicalize-a-pathname
PatternErrorOr<String> canonicalize_a_pathname(String const& value)
String canonicalize_a_pathname(String const& value)
{
// 1. If value is the empty string, then return value.
if (value.is_empty())
@ -171,22 +171,17 @@ PatternErrorOr<String> canonicalize_a_pathname(String const& value)
// 5. Let dummyURL be a new URL record.
URL dummy_url;
// 6. Let parseResult be the result of running basic URL parser given modified value with dummyURL
// as url and path start state as state override.
auto parse_result = Parser::basic_parse(modified_value.string_view(), {}, &dummy_url, Parser::State::PathStart);
// 6. Run basic URL parser given modified value with dummyURL as url and path start state as state override.
(void)Parser::basic_parse(modified_value.string_view(), {}, &dummy_url, Parser::State::PathStart);
// 7. If parseResult is failure, then throw a TypeError.
if (!parse_result.has_value())
return ErrorInfo { "Failed to canonicalize pathname string"_string };
// 8. Let result be the result of URL path serializing dummyURL.
// 7. Let result be the result of URL path serializing dummyURL.
auto result = dummy_url.serialize_path();
// 9. If leading slash is false, then set result to the code point substring from 2 to the end of the string within result.
// 8. If leading slash is false, then set result to the code point substring from 2 to the end of the string within result.
if (!leading_slash)
result = MUST(String::from_utf8(result.code_points().unicode_substring_view(2).as_string()));
// 10. Return result.
// 9. Return result.
return result;
}
@ -216,7 +211,7 @@ PatternErrorOr<String> canonicalize_an_opaque_pathname(String const& value)
}
// https://urlpattern.spec.whatwg.org/#canonicalize-a-search
PatternErrorOr<String> canonicalize_a_search(String const& value)
String canonicalize_a_search(String const& value)
{
// 1. If value is the empty string, return value.
if (value.is_empty())
@ -228,20 +223,16 @@ PatternErrorOr<String> canonicalize_a_search(String const& value)
// 3. Set dummyURLs query to the empty string.
dummy_url.set_query(String {});
// 4. Let parseResult be the result of running basic URL parser given value with dummyURL as url and query state as state override.
auto parse_result = Parser::basic_parse(value, {}, &dummy_url, Parser::State::Query);
// 4. Run basic URL parser given value with dummyURL as url and query state as state override.
(void)Parser::basic_parse(value, {}, &dummy_url, Parser::State::Query);
// 5. If parseResult is failure, then throw a TypeError.
if (!parse_result.has_value())
return ErrorInfo { "Failed to canonicalize query string"_string };
// 6. Return dummyURLs query.
// 5. Return dummyURLs query.
VERIFY(dummy_url.query().has_value());
return *dummy_url.query();
}
// https://urlpattern.spec.whatwg.org/#canonicalize-a-hash
PatternErrorOr<String> canonicalize_a_hash(String const& value)
String canonicalize_a_hash(String const& value)
{
// 1. If value is the empty string, return value.
if (value.is_empty())
@ -253,14 +244,10 @@ PatternErrorOr<String> canonicalize_a_hash(String const& value)
// 3. Set dummyURLs fragment to the empty string.
dummy_url.set_fragment(String {});
// 4. Let parseResult be the result of running basic URL parser given value with dummyURL as url and fragment state as state override.
auto parse_result = Parser::basic_parse(value, {}, &dummy_url, Parser::State::Fragment);
// 4. Run basic URL parser given value with dummyURL as url and fragment state as state override.
(void)Parser::basic_parse(value, {}, &dummy_url, Parser::State::Fragment);
// 5. If parseResult is failure, then throw a TypeError.
if (!parse_result.has_value())
return ErrorInfo { "Failed to canonicalize query string"_string };
// 6. Return dummyURLs fragment.
// 5. Return dummyURLs fragment.
VERIFY(dummy_url.fragment().has_value());
return *dummy_url.fragment();
}

View file

@ -17,9 +17,9 @@ String canonicalize_a_password(String const&);
PatternErrorOr<String> canonicalize_a_hostname(String const&);
PatternErrorOr<String> canonicalize_an_ipv6_hostname(String const&);
PatternErrorOr<String> canonicalize_a_port(String const&, Optional<String> const& protocol_value = {});
PatternErrorOr<String> canonicalize_a_pathname(String const&);
String canonicalize_a_pathname(String const&);
PatternErrorOr<String> canonicalize_an_opaque_pathname(String const&);
PatternErrorOr<String> canonicalize_a_search(String const&);
PatternErrorOr<String> canonicalize_a_hash(String const&);
String canonicalize_a_search(String const&);
String canonicalize_a_hash(String const&);
}

View file

@ -134,7 +134,7 @@ static PatternErrorOr<String> process_pathname_for_init(String const& pathname_v
}
// https://urlpattern.spec.whatwg.org/#process-search-for-init
static PatternErrorOr<String> process_search_for_init(String const& value, PatternProcessType type)
static String process_search_for_init(String const& value, PatternProcessType type)
{
// 1. Let strippedValue be the given value with a single leading U+003F (?) removed, if any.
auto stripped_value = value;
@ -150,7 +150,7 @@ static PatternErrorOr<String> process_search_for_init(String const& value, Patte
}
// https://urlpattern.spec.whatwg.org/#process-hash-for-init
static PatternErrorOr<String> process_hash_for_init(String const& value, PatternProcessType type)
static String process_hash_for_init(String const& value, PatternProcessType type)
{
// 1. Let strippedValue be the given value with a single leading U+0023 (#) removed, if any.
auto stripped_value = value;
@ -346,11 +346,11 @@ PatternErrorOr<Init> process_a_url_pattern_init(Init const& init, PatternProcess
// 18. If init["search"] exists then set result["search"] to the result of process search for init given init["search"] and type.
if (init.search.has_value())
result.search = TRY(process_search_for_init(init.search.value(), type));
result.search = process_search_for_init(init.search.value(), type);
// 19. If init["hash"] exists then set result["hash"] to the result of process hash for init given init["hash"] and type.
if (init.hash.has_value())
result.hash = TRY(process_hash_for_init(init.hash.value(), type));
result.hash = process_hash_for_init(init.hash.value(), type);
// 20. Return result.
return result;