From 570dcbaa3396cba0c092511c25f3caf7e752a103 Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Tue, 18 Mar 2025 19:45:08 +1300 Subject: [PATCH] LibURL/Pattern: Implement generating a component match result --- Libraries/LibURL/Pattern/Component.cpp | 37 ++++++++++++++++++++++++++ Libraries/LibURL/Pattern/Component.h | 12 +++++++-- Libraries/LibURL/Pattern/Pattern.h | 22 ++++++--------- 3 files changed, 55 insertions(+), 16 deletions(-) diff --git a/Libraries/LibURL/Pattern/Component.cpp b/Libraries/LibURL/Pattern/Component.cpp index 1079af1a18d..2c857975d74 100644 --- a/Libraries/LibURL/Pattern/Component.cpp +++ b/Libraries/LibURL/Pattern/Component.cpp @@ -273,4 +273,41 @@ PatternErrorOr Component::compile(Utf8View const& input, PatternParse }; } +// https://urlpattern.spec.whatwg.org/#create-a-component-match-result +Component::Result Component::create_match_result(String const& input, regex::RegexResult const& exec_result) const +{ + // 1. Let result be a new URLPatternComponentResult. + Component::Result result; + + // 2. Set result["input"] to input. + result.input = input; + + // 3. Let groups be a record. + OrderedHashMap> groups; + + // 4. Let index be 1. + // 5. While index is less than Get(execResult, "length"): + for (size_t index = 1; index <= exec_result.n_capture_groups; ++index) { + auto const& capture = exec_result.capture_group_matches[0][index]; + + // 1. Let name be component’s group name list[index − 1]. + auto name = group_name_list[index - 1]; + + // 2. Let value be Get(execResult, ToString(index)). + // 3. Set groups[name] to value. + if (capture.view.is_null()) + groups.set(name, Empty {}); + else + groups.set(name, MUST(capture.view.to_string())); + + // 4. Increment index by 1. + } + + // 6. Set result["groups"] to groups. + result.groups = move(groups); + + // 7. Return result. + return result; +} + } diff --git a/Libraries/LibURL/Pattern/Component.h b/Libraries/LibURL/Pattern/Component.h index 9ac77ff36e8..ae4edf688cd 100644 --- a/Libraries/LibURL/Pattern/Component.h +++ b/Libraries/LibURL/Pattern/Component.h @@ -15,6 +15,16 @@ namespace URL::Pattern { // https://urlpattern.spec.whatwg.org/#component struct Component { + static PatternErrorOr compile(Utf8View const& input, PatternParser::EncodingCallback, Options const&); + + // https://urlpattern.spec.whatwg.org/#dictdef-urlpatterncomponentresult + struct Result { + String input; + OrderedHashMap> groups; + }; + + Result create_match_result(String const& input, regex::RegexResult const& exec_result) const; + // https://urlpattern.spec.whatwg.org/#component-pattern-string // pattern string, a well formed pattern string String pattern_string; @@ -30,8 +40,6 @@ struct Component { // https://urlpattern.spec.whatwg.org/#component-has-regexp-groups // has regexp groups, a boolean bool has_regexp_groups {}; - - static PatternErrorOr compile(Utf8View const& input, PatternParser::EncodingCallback, Options const&); }; bool protocol_component_matches_a_special_scheme(Component const& protocol_component); diff --git a/Libraries/LibURL/Pattern/Pattern.h b/Libraries/LibURL/Pattern/Pattern.h index e2b1bb59ea6..40d7f6db0cc 100644 --- a/Libraries/LibURL/Pattern/Pattern.h +++ b/Libraries/LibURL/Pattern/Pattern.h @@ -19,24 +19,18 @@ namespace URL::Pattern { // https://urlpattern.spec.whatwg.org/#typedefdef-urlpatterninput using Input = Variant; -// https://urlpattern.spec.whatwg.org/#dictdef-urlpatterncomponentresult -struct ComponentResult { - String input; - OrderedHashMap> groups; -}; - // https://urlpattern.spec.whatwg.org/#dictdef-urlpatternresult struct Result { Vector inputs; - ComponentResult protocol; - ComponentResult username; - ComponentResult password; - ComponentResult hostname; - ComponentResult port; - ComponentResult pathname; - ComponentResult search; - ComponentResult hash; + Component::Result protocol; + Component::Result username; + Component::Result password; + Component::Result hostname; + Component::Result port; + Component::Result pathname; + Component::Result search; + Component::Result hash; }; // https://urlpattern.spec.whatwg.org/#dictdef-urlpatternoptions