Skip to content

Commit 25c0860

Browse files
authored
Expose isPatternValid and assertPatternValid helpers (#15)
1 parent 7775d95 commit 25c0860

File tree

3 files changed

+52
-5
lines changed

3 files changed

+52
-5
lines changed

index.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,25 @@ import escapeStringRegexp from 'escape-string-regexp';
33
// Copied from https://github.com/mozilla/gecko-dev/blob/073cc24f53d0cf31403121d768812146e597cc9d/toolkit/components/extensions/schemas/manifest.json#L487-L491
44
export const patternValidationRegex = /^(https?|wss?|file|ftp|\*):\/\/(\*|\*\.[^*/]+|[^*/]+)\/.*$|^file:\/\/\/.*$|^resource:\/\/(\*|\*\.[^*/]+|[^*/]+)\/.*$|^about:/;
55

6-
const isFirefox = typeof navigator === 'object' && navigator.userAgent.includes('Firefox/');
6+
const isFirefox = globalThis.navigator?.userAgent.includes('Firefox/');
77

88
export const allStarsRegex = isFirefox
99
? /^(https?|wss?):[/][/][^/]+([/].*)?$/
1010
: /^https?:[/][/][^/]+([/].*)?$/;
1111
export const allUrlsRegex = /^(https?|file|ftp):[/]+/;
1212

13-
function getRawPatternRegex(matchPattern: string): string {
14-
if (!patternValidationRegex.test(matchPattern)) {
13+
export function assertValidPattern(matchPattern: string): void {
14+
if (!isValidPattern(matchPattern)) {
1515
throw new Error(matchPattern + ' is an invalid pattern, it must match ' + String(patternValidationRegex));
1616
}
17+
}
18+
19+
export function isValidPattern(matchPattern: string): boolean {
20+
return matchPattern === '<all_urls>' || patternValidationRegex.test(matchPattern);
21+
}
22+
23+
function getRawPatternRegex(matchPattern: string): string {
24+
assertValidPattern(matchPattern);
1725

1826
// Host undefined for file:///
1927
let [, protocol, host = '', pathname] = matchPattern.split(/(^[^:]+:[/][/])([^/]+)?/);

readme.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@ npm install webext-patterns
1919

2020
```js
2121
// This module is only offered as a ES Module
22-
import {patternToRegex} from 'webext-patterns';
22+
import {
23+
patternToRegex,
24+
globToRegex,
25+
excludeDuplicatePatterns
26+
assertValidPattern,
27+
isValidPattern,
28+
} from 'webext-patterns';
2329
```
2430

2531
## Usage
@@ -33,6 +39,12 @@ globToRegex('*.example.com');
3339

3440
excludeDuplicatePatterns(['https://*.google.com/*', 'https://google.com/*']);
3541
// Returns ['https://*.google.com/*']
42+
43+
assertValidPattern('https://google.*/*');
44+
// Throws an error because the pattern is invalid
45+
46+
isValidPattern('https://*.google.com/*');
47+
// Returns true
3648
```
3749

3850
> **Note**
@@ -100,6 +112,24 @@ excludeDuplicatePatterns([
100112
// Returns ["https://*/*"]
101113
```
102114

115+
#### assertValidPattern(pattern)
116+
117+
Accepts a pattern and throws an error if it's invalid.
118+
119+
```js
120+
assertValidPattern('https://google.*/*');
121+
// Throws an error because the pattern is invalid
122+
```
123+
124+
#### isValidPattern(pattern)
125+
126+
Accepts a pattern and returns `true` if it's valid.
127+
128+
```js
129+
isValidPattern('https://google.*/*');
130+
// Returns false
131+
```
132+
103133
## Related
104134

105135
### Permissions

test/patterns.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import test from 'ava';
2-
import {patternToRegex} from '../index.js';
2+
import {patternToRegex, isValidPattern, assertValidPattern} from '../index.js';
33

44
function macro(t, pattern, matching) {
55
const regex = patternToRegex(pattern);
66
for (const url of matching) {
77
t.regex(url, regex);
8+
t.true(isValidPattern(pattern));
9+
t.notThrows(() => assertValidPattern(pattern));
810
}
911
}
1012

@@ -65,6 +67,7 @@ test('Should not match anything if no patterns are passed', t => {
6567
});
6668

6769
const invalidPatterns = [
70+
'https://google.*/*', // The TLD cannot be a wildcard
6871
'https://mozilla.org', // No path
6972
'https://*zilla.org/', // "*" in host must be the only character or be followed by "."
7073
'http*://mozilla.org/', // "*" in scheme must be the only character
@@ -74,9 +77,15 @@ const invalidPatterns = [
7477
];
7578
for (const pattern of invalidPatterns) {
7679
test('Invalid pattern: ' + pattern, t => {
80+
t.false(isValidPattern(pattern));
81+
7782
t.throws(() => patternToRegex(pattern), {
7883
message: /is an invalid pattern, it must match/,
7984
});
85+
86+
t.throws(() => assertValidPattern(pattern), {
87+
message: /is an invalid pattern, it must match/,
88+
});
8089
});
8190
}
8291

0 commit comments

Comments
 (0)