Skip to content

Commit

Permalink
update url_pattern to pass more wpt on node.js
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Jan 27, 2025
1 parent aec5864 commit f30abe2
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
2 changes: 0 additions & 2 deletions include/ada/implementation.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ bool can_parse(std::string_view input,
* @param input valid UTF-8 string or URLPatternInit struct
* @param base_url an optional valid UTF-8 string
* @param options an optional url_pattern_options struct
* @param provider an optional regex provider. if not provided, it will
* use ada::url_pattern_regex::std_regex_provider
* @return url_pattern instance
*/
template <url_pattern_regex::regex_concept regex_provider =
Expand Down
5 changes: 0 additions & 5 deletions include/ada/url_pattern-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@ url_pattern_component<regex_provider>::create_component_match_result(
auto result =
url_pattern_component_result{.input = std::string(input), .groups = {}};

// If input is empty, then groups will always be empty.
if (input.empty()) {
return result;
}

// Optimization: Let's reserve the size.
result.groups.reserve(exec_result.size());

Expand Down
3 changes: 2 additions & 1 deletion src/url_pattern_regex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ std_regex_provider::regex_search(std::string_view input,
return std::nullopt;
}
std::vector<std::optional<std::string>> matches;
if (match_result.empty()) {
// If input is empty, let's assume the result will be empty as well.
if (input.empty() || match_result.empty()) {
return matches;
}
matches.reserve(match_result.size());
Expand Down
26 changes: 26 additions & 0 deletions tests/wpt_urlpattern_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,32 @@ using namespace simdjson;
constexpr std::string_view URL_PATTERN_TEST_DATA =
"wpt/urlpatterntestdata.json";

TEST(wpt_urlpattern_tests, test_regex_difference) {
// {
// "pattern": [{ "pathname": "/foo/bar" }],
// "inputs": [{ "pathname": "/foo/bar" }],
// "expected_match": {
// "pathname": { "input": "/foo/bar", "groups": {} }
// }
// }
auto init = ada::url_pattern_init{};
init.pathname = "/foo/bar";
auto u =
ada::parse_url_pattern<ada::url_pattern_regex::std_regex_provider>(init);
ASSERT_TRUE(u);
auto match = u->exec(init, nullptr);
ASSERT_TRUE(match);
ASSERT_TRUE(match->has_value());

std::unordered_map<std::string, std::optional<std::string>> empty_object{};

ASSERT_EQ(match->value().protocol.input, "");
ASSERT_EQ(match->value().protocol.groups, empty_object);
ASSERT_EQ(match->value().pathname.input, "/foo/bar");
ASSERT_EQ(match->value().pathname.groups, empty_object);
SUCCEED();
}

TEST(wpt_urlpattern_tests, parser_tokenize_basic_tests) {
auto tokenize_result =
tokenize("*", ada::url_pattern_helpers::token_policy::STRICT);
Expand Down

0 comments on commit f30abe2

Please sign in to comment.