Skip to content

Commit f110e9a

Browse files
committed
Fix matched values for :name* patterns. (kenchris#126)
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
1 parent 68d7179 commit f110e9a

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

src/path-to-regex-modified.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,11 @@ export function tokensToRegexp(
655655
route += `(?:${prefix}(${token.pattern})${suffix})${token.modifier}`;
656656
}
657657
} else {
658-
route += `(${token.pattern})${token.modifier}`;
658+
if (token.modifier === "+" || token.modifier === "*") {
659+
route += `((?:${token.pattern})${token.modifier})`;
660+
} else {
661+
route += `(${token.pattern})${token.modifier}`;
662+
}
659663
}
660664
} else {
661665
route += `(?:${prefix}${suffix})${token.modifier}`;

urlpatterntestdata.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2253,6 +2253,48 @@
22532253
"pattern": [{ "pathname": "/foo" }, "https://example.com" ],
22542254
"expected_obj": "error"
22552255
},
2256+
{
2257+
"pattern": [{ "pathname": ":name*" }],
2258+
"inputs": [{ "pathname": "foobar" }],
2259+
"expected_match": {
2260+
"pathname": { "input": "foobar", "groups": { "name": "foobar" }}
2261+
}
2262+
},
2263+
{
2264+
"pattern": [{ "pathname": ":name+" }],
2265+
"inputs": [{ "pathname": "foobar" }],
2266+
"expected_match": {
2267+
"pathname": { "input": "foobar", "groups": { "name": "foobar" }}
2268+
}
2269+
},
2270+
{
2271+
"pattern": [{ "pathname": ":name" }],
2272+
"inputs": [{ "pathname": "foobar" }],
2273+
"expected_match": {
2274+
"pathname": { "input": "foobar", "groups": { "name": "foobar" }}
2275+
}
2276+
},
2277+
{
2278+
"pattern": [{ "protocol": ":name*" }],
2279+
"inputs": [{ "protocol": "foobar" }],
2280+
"expected_match": {
2281+
"protocol": { "input": "foobar", "groups": { "name": "foobar" }}
2282+
}
2283+
},
2284+
{
2285+
"pattern": [{ "protocol": ":name+" }],
2286+
"inputs": [{ "protocol": "foobar" }],
2287+
"expected_match": {
2288+
"protocol": { "input": "foobar", "groups": { "name": "foobar" }}
2289+
}
2290+
},
2291+
{
2292+
"pattern": [{ "protocol": ":name" }],
2293+
"inputs": [{ "protocol": "foobar" }],
2294+
"expected_match": {
2295+
"protocol": { "input": "foobar", "groups": { "name": "foobar" }}
2296+
}
2297+
},
22562298
{
22572299
"pattern": [{}],
22582300
"inputs": ["https://example.com/"],

0 commit comments

Comments
 (0)