LibURL/Pattern: Implement generating a component match result

This commit is contained in:
Shannon Booth 2025-03-18 19:45:08 +13:00 committed by Tim Flynn
parent e35555f00e
commit 2a44420e52
Notes: github-actions[bot] 2025-04-06 12:27:13 +00:00
3 changed files with 55 additions and 16 deletions

View file

@ -273,4 +273,41 @@ PatternErrorOr<Component> 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<USVString, (USVString or undefined)>.
OrderedHashMap<String, Variant<String, Empty>> 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 components 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;
}
}

View file

@ -15,6 +15,16 @@ namespace URL::Pattern {
// https://urlpattern.spec.whatwg.org/#component
struct Component {
static PatternErrorOr<Component> compile(Utf8View const& input, PatternParser::EncodingCallback, Options const&);
// https://urlpattern.spec.whatwg.org/#dictdef-urlpatterncomponentresult
struct Result {
String input;
OrderedHashMap<String, Variant<String, Empty>> 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<Component> compile(Utf8View const& input, PatternParser::EncodingCallback, Options const&);
};
bool protocol_component_matches_a_special_scheme(Component const& protocol_component);

View file

@ -19,24 +19,18 @@ namespace URL::Pattern {
// https://urlpattern.spec.whatwg.org/#typedefdef-urlpatterninput
using Input = Variant<String, Init>;
// https://urlpattern.spec.whatwg.org/#dictdef-urlpatterncomponentresult
struct ComponentResult {
String input;
OrderedHashMap<String, Variant<String, Empty>> groups;
};
// https://urlpattern.spec.whatwg.org/#dictdef-urlpatternresult
struct Result {
Vector<Input> 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