LibURL/Pattern: Complete the implementation of the constructor

This commit is contained in:
Shannon Booth 2025-03-18 19:36:35 +13:00 committed by Tim Flynn
parent c9e6ad562c
commit e35555f00e
Notes: github-actions[bot] 2025-04-06 12:27:22 +00:00
4 changed files with 208 additions and 135 deletions

View file

@ -4,21 +4,49 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibURL/Pattern/Canonicalization.h>
#include <LibURL/Pattern/ConstructorStringParser.h>
#include <LibURL/Pattern/Pattern.h>
#include <LibURL/URL.h>
namespace URL::Pattern {
// https://urlpattern.spec.whatwg.org/#hostname-pattern-is-an-ipv6-address
static bool hostname_pattern_is_an_ipv6_address(String const& input)
{
// 1. If inputs code point length is less than 2, then return false.
if (input.bytes().size() < 2)
return false;
// 2. Let input code points be input interpreted as a list of code points.
auto input_code_points = input.bytes();
// 3. If input code points[0] is U+005B ([), then return true.
if (input_code_points[0] == '[')
return true;
// 4. If input code points[0] is U+007B ({) and input code points[1] is U+005B ([), then return true.
if (input_code_points[0] == '{' && input_code_points[1] == '[')
return true;
// 5. If input code points[0] is U+005C (\) and input code points[1] is U+005B ([), then return true.
if (input_code_points[0] == '\\' && input_code_points[1] == '[')
return true;
// 6. Return false.
return false;
}
// https://urlpattern.spec.whatwg.org/#url-pattern-create
PatternErrorOr<Pattern> Pattern::create(Input const& input, Optional<String> const& base_url, IgnoreCase)
PatternErrorOr<Pattern> Pattern::create(Input const& input, Optional<String> const& base_url, IgnoreCase ignore_case)
{
// 1. Let init be null.
Init init;
// 2. If input is a scalar value string then:
if (input.has<String>()) {
if (auto const* input_string = input.get_pointer<String>()) {
// 1. Set init to the result of running parse a constructor string given input.
init = TRY(ConstructorStringParser::parse(input.get<String>().code_points()));
init = TRY(ConstructorStringParser::parse(input_string->code_points()));
// 2. If baseURL is null and init["protocol"] does not exist, then throw a TypeError.
if (!base_url.has_value() && !init.protocol.has_value())
@ -63,48 +91,72 @@ PatternErrorOr<Pattern> Pattern::create(Input const& input, Optional<String> con
if (!processed_init.hash.has_value())
processed_init.hash = "*"_string;
// FIXME: 6. If processedInit["protocol"] is a special scheme and processedInit["port"] is a string which represents its
// 6. If processedInit["protocol"] is a special scheme and processedInit["port"] is a string which represents its
// corresponding default port in radix-10 using ASCII digits then set processedInit["port"] to the empty string.
if (is_special_scheme(processed_init.protocol.value())) {
auto maybe_port = processed_init.port->to_number<u16>();
if (maybe_port.has_value() && *maybe_port == default_port_for_scheme(*processed_init.protocol).value())
processed_init.port = String {};
}
// 7. Let urlPattern be a new URL pattern.
Pattern url_pattern;
// FIXME: 8. Set urlPatterns protocol component to the result of compiling a component given processedInit["protocol"],
// 8. Set urlPatterns protocol component to the result of compiling a component given processedInit["protocol"],
// canonicalize a protocol, and default options.
url_pattern.m_protocol_component = TRY(Component::compile(processed_init.protocol->code_points(), canonicalize_a_protocol, Options::default_()));
// FIXME: 9. Set urlPatterns username component to the result of compiling a component given processedInit["username"],
// 9. Set urlPatterns username component to the result of compiling a component given processedInit["username"],
// canonicalize a username, and default options.
url_pattern.m_username_component = TRY(Component::compile(processed_init.username->code_points(), canonicalize_a_username, Options::default_()));
// FIXME: 10. Set urlPatterns password component to the result of compiling a component given processedInit["password"],
// 10. Set urlPatterns password component to the result of compiling a component given processedInit["password"],
// canonicalize a password, and default options.
url_pattern.m_password_component = TRY(Component::compile(processed_init.password->code_points(), canonicalize_a_password, Options::default_()));
// FIXME: 11. If the result running hostname pattern is an IPv6 address given processedInit["hostname"] is true, then set
// 11. If the result running hostname pattern is an IPv6 address given processedInit["hostname"] is true, then set
// urlPatterns hostname component to the result of compiling a component given processedInit["hostname"],
// canonicalize an IPv6 hostname, and hostname options.
// FIXME: 12. Otherwise, set urlPatterns hostname component to the result of compiling a component given
// processedInit["hostname"], canonicalize a hostname, and hostname options.
// FIXME: 13. Set urlPatterns port component to the result of compiling a component given processedInit["port"],
// canonicalize a port, and default options.
// FIXME: 14. Let compileOptions be a copy of the default options with the ignore case property set to options["ignoreCase"].
// FIXME: 15. If the result of running protocol component matches a special scheme given urlPatterns protocol component is true, then:
if (false) {
// FIXME: 1. Let pathCompileOptions be copy of the pathname options with the ignore case property set to options["ignoreCase"].
// FIXME: 2. Set urlPatterns pathname component to the result of compiling a component given processedInit["pathname"],
// canonicalize a pathname, and pathCompileOptions.
if (hostname_pattern_is_an_ipv6_address(processed_init.hostname.value())) {
url_pattern.m_hostname_component = TRY(Component::compile(processed_init.hostname->code_points(), canonicalize_an_ipv6_hostname, Options::hostname()));
}
// FIXME: 16. Otherwise set urlPatterns pathname component to the result of compiling a component given
// 12. Otherwise, set urlPatterns hostname component to the result of compiling a component given
// processedInit["hostname"], canonicalize a hostname, and hostname options.
else {
url_pattern.m_hostname_component = TRY(Component::compile(processed_init.hostname->code_points(), canonicalize_a_hostname, Options::hostname()));
}
// 13. Set urlPatterns port component to the result of compiling a component given processedInit["port"],
// canonicalize a port, and default options.
url_pattern.m_port_component = TRY(Component::compile(processed_init.port->code_points(), [](String const& value) { return canonicalize_a_port(value); }, Options::default_()));
// 14. Let compileOptions be a copy of the default options with the ignore case property set to options["ignoreCase"].
auto compile_options = Options::default_();
compile_options.ignore_case = ignore_case == IgnoreCase::Yes;
// 15. If the result of running protocol component matches a special scheme given urlPatterns protocol component is true, then:
if (protocol_component_matches_a_special_scheme(url_pattern.m_protocol_component)) {
// 1. Let pathCompileOptions be copy of the pathname options with the ignore case property set to options["ignoreCase"].
auto path_compile_options = Options::pathname();
path_compile_options.ignore_case = ignore_case == IgnoreCase::Yes;
// 2. Set urlPatterns pathname component to the result of compiling a component given processedInit["pathname"],
// canonicalize a pathname, and pathCompileOptions.
url_pattern.m_pathname_component = TRY(Component::compile(processed_init.pathname->code_points(), canonicalize_a_pathname, path_compile_options));
}
// 16. Otherwise set urlPatterns pathname component to the result of compiling a component given
// processedInit["pathname"], canonicalize an opaque pathname, and compileOptions.
else {
url_pattern.m_pathname_component = TRY(Component::compile(processed_init.pathname->code_points(), canonicalize_an_opaque_pathname, compile_options));
}
// FIXME: 17. Set urlPatterns search component to the result of compiling a component given processedInit["search"],
// 17. Set urlPatterns search component to the result of compiling a component given processedInit["search"],
// canonicalize a search, and compileOptions.
url_pattern.m_search_component = TRY(Component::compile(processed_init.search->code_points(), canonicalize_a_search, compile_options));
// FIXME: 18. Set urlPatterns hash component to the result of compiling a component given processedInit["hash"],
// 18. Set urlPatterns hash component to the result of compiling a component given processedInit["hash"],
// canonicalize a hash, and compileOptions.
url_pattern.m_hash_component = TRY(Component::compile(processed_init.hash->code_points(), canonicalize_a_hash, compile_options));
// 19. Return urlPattern.
return url_pattern;