Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions packages/isomorphic/urlMatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,29 @@ export function globToRegexPattern(glob: string): string {
}

switch (c) {
case '{':
inGroup = true;
tokens.push('(');
case '{': {
// Only treat '{' as a group opener when a matching '}' exists ahead;
// otherwise emit it as a literal to avoid producing an invalid regex.
let hasMatchingClose = false;
for (let j = i + 1; j < glob.length; j++) {
if (glob[j] === '\\' && j + 1 < glob.length) { j++; continue; }
if (glob[j] === '}') { hasMatchingClose = true; break; }
}
if (hasMatchingClose) {
inGroup = true;
tokens.push('(');
} else {
tokens.push('\\{');
}
break;
}
case '}':
inGroup = false;
tokens.push(')');
if (inGroup) {
inGroup = false;
tokens.push(')');
} else {
tokens.push('\\}');
}
break;
case ',':
if (inGroup) {
Expand Down
9 changes: 9 additions & 0 deletions tests/page/interception.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@ it('should work with glob', async () => {
expect(globToRegex('[a-z]')).toEqual(/^\[a-z\]$/);
expect(globToRegex('$^+.\\*()|\\?\\{\\}\\[\\]')).toEqual(/^\$\^\+\.\*\(\)\|\?\{\}\[\]$/);

// unbalanced braces must be treated as literals, not produce invalid regexes
expect(globToRegex('{foo').test('{foo')).toBeTruthy();
expect(globToRegex('{foo').test('foo')).toBeFalsy();
expect(globToRegex('foo}').test('foo}')).toBeTruthy();
expect(globToRegex('foo}').test('foo')).toBeFalsy();
expect(globToRegex('**/*.png?{').test('https://localhost:8080/c.png?{')).toBeTruthy();
expect(globToRegex('**/*.png?{').test('https://localhost:8080/c.png?')).toBeFalsy();
expect(globToRegex('}foo{').test('}foo{')).toBeTruthy();

expect(urlMatches(undefined, 'http://playwright.dev/', 'http://playwright.dev')).toBeTruthy();
expect(urlMatches(undefined, 'http://playwright.dev/?a=b', 'http://playwright.dev?a=b')).toBeTruthy();
expect(urlMatches(undefined, 'http://playwright.dev/', 'h*://playwright.dev')).toBeTruthy();
Expand Down