Skip to content

Commit

Permalink
fix(match-patterns): Ignore unimplemented protocols and return true w…
Browse files Browse the repository at this point in the history
…hen checking `<all_urls>` patterns
  • Loading branch information
aklinker1 committed Sep 29, 2023
1 parent 827fb15 commit 7bac5ca
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/match-patterns/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe('MatchPattern', () => {
it.each([
[true, 'http://google.com'],
[true, new URL('https://youtube.com')],
[true, new URL('file:///home/aklinker1')],
[true, { hostname: 'test.com', pathname: '/', protocol: 'http:' } as Location],
])('should parse "%s", when "%s" is checked, return %s', (exepcted, url) => {
expect(new MatchPattern('<all_urls>').includes(url)).toBe(exepcted);
Expand Down
4 changes: 4 additions & 0 deletions packages/match-patterns/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export class MatchPattern {
private protocolMatches: string[];
private hostnameMatch: string | undefined;
private pathnameMatch: string | undefined;
private isAllUrls?: boolean;

/**
* Parse a match pattern string. If it is invalid, the constructor will throw an
Expand All @@ -22,6 +23,7 @@ export class MatchPattern {
*/
constructor(matchPattern: string) {
if (matchPattern === '<all_urls>') {
this.isAllUrls = true;
this.protocolMatches = [...MatchPattern.PROTOCOLS];
this.hostnameMatch = '*';
this.pathnameMatch = '*';
Expand All @@ -44,6 +46,8 @@ export class MatchPattern {
* Check if a URL is included in a pattern.
*/
includes(url: string | URL | Location): boolean {
if (this.isAllUrls) return true;

const u: URL =
typeof url === 'string' ? new URL(url) : url instanceof Location ? new URL(url.href) : url;

Expand Down

0 comments on commit 7bac5ca

Please sign in to comment.