Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose isPatternValid and assertPatternValid helpers #15

Merged
merged 3 commits into from
Jan 18, 2024
Merged
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
14 changes: 11 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,25 @@ import escapeStringRegexp from 'escape-string-regexp';
// Copied from https://github.com/mozilla/gecko-dev/blob/073cc24f53d0cf31403121d768812146e597cc9d/toolkit/components/extensions/schemas/manifest.json#L487-L491
export const patternValidationRegex = /^(https?|wss?|file|ftp|\*):\/\/(\*|\*\.[^*/]+|[^*/]+)\/.*$|^file:\/\/\/.*$|^resource:\/\/(\*|\*\.[^*/]+|[^*/]+)\/.*$|^about:/;

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

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

function getRawPatternRegex(matchPattern: string): string {
if (!patternValidationRegex.test(matchPattern)) {
export function assertValidPattern(matchPattern: string): void {
if (!isValidPattern(matchPattern)) {
throw new Error(matchPattern + ' is an invalid pattern, it must match ' + String(patternValidationRegex));
}
}

export function isValidPattern(matchPattern: string): boolean {
return matchPattern === '<all_urls>' || patternValidationRegex.test(matchPattern);
}

function getRawPatternRegex(matchPattern: string): string {
assertValidPattern(matchPattern);

// Host undefined for file:///
let [, protocol, host = '', pathname] = matchPattern.split(/(^[^:]+:[/][/])([^/]+)?/);
Expand Down
32 changes: 31 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ npm install webext-patterns

```js
// This module is only offered as a ES Module
import {patternToRegex} from 'webext-patterns';
import {
patternToRegex,
globToRegex,
excludeDuplicatePatterns
assertValidPattern,
isValidPattern,
} from 'webext-patterns';
```

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

excludeDuplicatePatterns(['https://*.google.com/*', 'https://google.com/*']);
// Returns ['https://*.google.com/*']

assertValidPattern('https://google.*/*');
// Throws an error because the pattern is invalid

isValidPattern('https://*.google.com/*');
// Returns true
```

> **Note**
Expand Down Expand Up @@ -100,6 +112,24 @@ excludeDuplicatePatterns([
// Returns ["https://*/*"]
```

#### assertValidPattern(pattern)

Accepts a pattern and throws an error if it's invalid.

```js
assertValidPattern('https://google.*/*');
// Throws an error because the pattern is invalid
```

#### isValidPattern(pattern)

Accepts a pattern and returns `true` if it's valid.

```js
isValidPattern('https://google.*/*');
// Returns false
```

## Related

### Permissions
Expand Down
11 changes: 10 additions & 1 deletion test/patterns.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import test from 'ava';
import {patternToRegex} from '../index.js';
import {patternToRegex, isValidPattern, assertValidPattern} from '../index.js';

function macro(t, pattern, matching) {
const regex = patternToRegex(pattern);
for (const url of matching) {
t.regex(url, regex);
t.true(isValidPattern(pattern));
t.notThrows(() => assertValidPattern(pattern));
}
}

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

const invalidPatterns = [
'https://google.*/*', // The TLD cannot be a wildcard
'https://mozilla.org', // No path
'https://*zilla.org/', // "*" in host must be the only character or be followed by "."
'http*://mozilla.org/', // "*" in scheme must be the only character
Expand All @@ -74,9 +77,15 @@ const invalidPatterns = [
];
for (const pattern of invalidPatterns) {
test('Invalid pattern: ' + pattern, t => {
t.false(isValidPattern(pattern));

t.throws(() => patternToRegex(pattern), {
message: /is an invalid pattern, it must match/,
});

t.throws(() => assertValidPattern(pattern), {
message: /is an invalid pattern, it must match/,
});
});
}

Expand Down