Skip to content

Commit 2d770a5

Browse files
authored
fix(glob): /**/ should match single / (#37996)
1 parent 2686c09 commit 2d770a5

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

packages/playwright-core/src/utils/isomorphic/urlMatch.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,27 @@ export function globToRegexPattern(glob: string): string {
3030
continue;
3131
}
3232
if (c === '*') {
33+
const charBefore = glob[i - 1];
3334
let starCount = 1;
3435
while (glob[i + 1] === '*') {
3536
starCount++;
3637
i++;
3738
}
38-
if (starCount > 1)
39-
tokens.push('(.*)');
40-
else
39+
if (starCount > 1) {
40+
const charAfter = glob[i + 1];
41+
// Match either /..something../ or /.
42+
if (charAfter === '/') {
43+
if (charBefore === '/')
44+
tokens.push('((.+/)|)');
45+
else
46+
tokens.push('(.*/)');
47+
++i;
48+
} else {
49+
tokens.push('(.*)');
50+
}
51+
} else {
4152
tokens.push('([^/]*)');
53+
}
4254
continue;
4355
}
4456

tests/page/interception.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ it('should work with glob', async () => {
9292
expect(globToRegex('http://localhost:3000/signin-oidc*').test('http://localhost:3000/signin-oidc/foo')).toBeFalsy();
9393
expect(globToRegex('http://localhost:3000/signin-oidc*').test('http://localhost:3000/signin-oidcnice')).toBeTruthy();
9494

95+
expect(globToRegex('**/*.js').test('/foo.js')).toBeTruthy();
96+
expect(globToRegex('asd/**.js').test('/foo.js')).toBeFalsy();
97+
expect(globToRegex('**/*.js').test('bar_foo.js')).toBeFalsy();
98+
9599
// range [] is NOT supported
96100
expect(globToRegex('**/api/v[0-9]').test('http://example.com/api/v[0-9]')).toBeTruthy();
97101
expect(globToRegex('**/api/v[0-9]').test('http://example.com/api/version')).toBeFalsy();
@@ -146,6 +150,10 @@ it('should work with glob', async () => {
146150
expect(urlMatches('http://first.host/', 'http://second.host/foo', '**/foo')).toBeTruthy();
147151
expect(urlMatches('http://playwright.dev/', 'http://localhost/', '*//localhost/')).toBeTruthy();
148152

153+
// /**/ should match /.
154+
expect(urlMatches(undefined, 'https://foo/bar.js', 'https://foo/**/bar.js')).toBeTruthy();
155+
expect(urlMatches(undefined, 'https://foo/bar.js', 'https://foo/**/**/bar.js')).toBeTruthy();
156+
149157
const customPrefixes = ['about', 'data', 'chrome', 'edge', 'file'];
150158
for (const prefix of customPrefixes) {
151159
expect(urlMatches('http://playwright.dev/', `${prefix}:blank`, `${prefix}:blank`)).toBeTruthy();

0 commit comments

Comments
 (0)