Skip to content

Commit

Permalink
Fix matched values for :name* patterns. (#126)
Browse files Browse the repository at this point in the history
This fixes the problem where:

  const p = new URLPattern({ pathname: ':name*' });
  const r = p.exec('foobar');
  console.log(r.pathname.groups.name);

Would log 'r' instead of 'foobar'.

This is an upstream bug in path-to-regexp:

  pillarjs/path-to-regexp#260

This commit essentially applies the proposed fix in:

  pillarjs/path-to-regexp#261

And adds the WPT tests from:

  https://chromium-review.googlesource.com/c/chromium/src/+/3123654
  • Loading branch information
wanderview authored and kenchris committed Jan 11, 2022
1 parent 6e65e81 commit 7a5adfe
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/path-to-regex-modified.ts
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,11 @@ export function tokensToRegexp(
route += `(?:${prefix}(${token.pattern})${suffix})${token.modifier}`;
}
} else {
route += `(${token.pattern})${token.modifier}`;
if (token.modifier === "+" || token.modifier === "*") {
route += `((?:${token.pattern})${token.modifier})`;
} else {
route += `(${token.pattern})${token.modifier}`;
}
}
} else {
route += `(?:${prefix}${suffix})${token.modifier}`;
Expand Down
42 changes: 42 additions & 0 deletions urlpatterntestdata.json
Original file line number Diff line number Diff line change
Expand Up @@ -2253,6 +2253,48 @@
"pattern": [{ "pathname": "/foo" }, "https://example.com" ],
"expected_obj": "error"
},
{
"pattern": [{ "pathname": ":name*" }],
"inputs": [{ "pathname": "foobar" }],
"expected_match": {
"pathname": { "input": "foobar", "groups": { "name": "foobar" }}
}
},
{
"pattern": [{ "pathname": ":name+" }],
"inputs": [{ "pathname": "foobar" }],
"expected_match": {
"pathname": { "input": "foobar", "groups": { "name": "foobar" }}
}
},
{
"pattern": [{ "pathname": ":name" }],
"inputs": [{ "pathname": "foobar" }],
"expected_match": {
"pathname": { "input": "foobar", "groups": { "name": "foobar" }}
}
},
{
"pattern": [{ "protocol": ":name*" }],
"inputs": [{ "protocol": "foobar" }],
"expected_match": {
"protocol": { "input": "foobar", "groups": { "name": "foobar" }}
}
},
{
"pattern": [{ "protocol": ":name+" }],
"inputs": [{ "protocol": "foobar" }],
"expected_match": {
"protocol": { "input": "foobar", "groups": { "name": "foobar" }}
}
},
{
"pattern": [{ "protocol": ":name" }],
"inputs": [{ "protocol": "foobar" }],
"expected_match": {
"protocol": { "input": "foobar", "groups": { "name": "foobar" }}
}
},
{
"pattern": [{}],
"inputs": ["https://example.com/"],
Expand Down

0 comments on commit 7a5adfe

Please sign in to comment.