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

Corresponds to: 5c979a31
This commit is contained in:
Shannon Booth 2025-03-26 18:52:48 +13:00 committed by Tim Flynn
commit 83a82a027f
Notes: github-actions[bot] 2025-04-06 12:26:10 +00:00
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 // 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. // 1. If value is the empty string, then return value.
if (value.is_empty()) if (value.is_empty())
@ -171,22 +171,17 @@ PatternErrorOr<String> canonicalize_a_pathname(String const& value)
// 5. Let dummyURL be a new URL record. // 5. Let dummyURL be a new URL record.
URL dummy_url; URL dummy_url;
// 6. Let parseResult be the result of running basic URL parser given modified value with dummyURL // 6. Run basic URL parser given modified value with dummyURL as url and path start state as state override.
// as url and path start state as state override. (void)Parser::basic_parse(modified_value.string_view(), {}, &dummy_url, Parser::State::PathStart);
auto parse_result = Parser::basic_parse(modified_value.string_view(), {}, &dummy_url, Parser::State::PathStart);
// 7. If parseResult is failure, then throw a TypeError. // 7. Let result be the result of URL path serializing dummyURL.
if (!parse_result.has_value())
return ErrorInfo { "Failed to canonicalize pathname string"_string };
// 8. Let result be the result of URL path serializing dummyURL.
auto result = dummy_url.serialize_path(); 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) if (!leading_slash)
result = MUST(String::from_utf8(result.code_points().unicode_substring_view(2).as_string())); result = MUST(String::from_utf8(result.code_points().unicode_substring_view(2).as_string()));
// 10. Return result. // 9. Return result.
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 // 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. // 1. If value is the empty string, return value.
if (value.is_empty()) if (value.is_empty())
@ -228,20 +223,16 @@ PatternErrorOr<String> canonicalize_a_search(String const& value)
// 3. Set dummyURLs query to the empty string. // 3. Set dummyURLs query to the empty string.
dummy_url.set_query(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. // 4. Run 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); (void)Parser::basic_parse(value, {}, &dummy_url, Parser::State::Query);
// 5. If parseResult is failure, then throw a TypeError. // 5. Return dummyURLs query.
if (!parse_result.has_value())
return ErrorInfo { "Failed to canonicalize query string"_string };
// 6. Return dummyURLs query.
VERIFY(dummy_url.query().has_value()); VERIFY(dummy_url.query().has_value());
return *dummy_url.query(); return *dummy_url.query();
} }
// https://urlpattern.spec.whatwg.org/#canonicalize-a-hash // 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. // 1. If value is the empty string, return value.
if (value.is_empty()) if (value.is_empty())
@ -253,14 +244,10 @@ PatternErrorOr<String> canonicalize_a_hash(String const& value)
// 3. Set dummyURLs fragment to the empty string. // 3. Set dummyURLs fragment to the empty string.
dummy_url.set_fragment(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. // 4. Run 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); (void)Parser::basic_parse(value, {}, &dummy_url, Parser::State::Fragment);
// 5. If parseResult is failure, then throw a TypeError. // 5. Return dummyURLs fragment.
if (!parse_result.has_value())
return ErrorInfo { "Failed to canonicalize query string"_string };
// 6. Return dummyURLs fragment.
VERIFY(dummy_url.fragment().has_value()); VERIFY(dummy_url.fragment().has_value());
return *dummy_url.fragment(); 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_a_hostname(String const&);
PatternErrorOr<String> canonicalize_an_ipv6_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_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_an_opaque_pathname(String const&);
PatternErrorOr<String> canonicalize_a_search(String const&); String canonicalize_a_search(String const&);
PatternErrorOr<String> canonicalize_a_hash(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 // 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. // 1. Let strippedValue be the given value with a single leading U+003F (?) removed, if any.
auto stripped_value = value; 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 // 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. // 1. Let strippedValue be the given value with a single leading U+0023 (#) removed, if any.
auto stripped_value = value; 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. // 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()) 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. // 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()) 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. // 20. Return result.
return result; return result;