webext-patterns 1.5.1 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,8 +3,8 @@ export declare const allStarsRegex: RegExp;
3
3
  export declare const allUrlsRegex: RegExp;
4
4
  export declare function assertValidPattern(matchPattern: string): void;
5
5
  export declare function isValidPattern(matchPattern: string): boolean;
6
- export declare function doesUrlMatchPatterns(url: string, ...patterns: string[]): boolean;
7
- export declare function findMatchingPatterns(url: string, ...patterns: string[]): string[];
6
+ export declare function testPatterns(url: string, patterns: string[]): boolean;
7
+ export declare function getMatchingPatterns(url: string, patterns: string[]): string[];
8
8
  export declare function patternToRegex(...matchPatterns: readonly string[]): RegExp;
9
9
  export declare function globToRegex(...globs: readonly string[]): RegExp;
10
- export declare function excludeDuplicatePatterns(matchPatterns: readonly string[]): string[];
10
+ export declare function removeRedundantPatterns(matchPatterns: readonly string[]): string[];
@@ -1,6 +1,5 @@
1
- import escapeStringRegexp from 'escape-string-regexp';
2
- // Copied from https://github.com/mozilla/gecko-dev/blob/073cc24f53d0cf31403121d768812146e597cc9d/toolkit/components/extensions/schemas/manifest.json#L487-L491
3
- export const patternValidationRegex = /^(https?|wss?|file|ftp|\*):\/\/(\*|\*\.[^*/]+|[^*/]+)\/.*$|^file:\/\/\/.*$|^resource:\/\/(\*|\*\.[^*/]+|[^*/]+)\/.*$|^about:/;
1
+ // Copied from https://github.com/mozilla/gecko-dev/blob/5836a062726f715fda621338a17b51aff30d0a8c/toolkit/components/extensions/schemas/manifest.json#L729-L741
2
+ export const patternValidationRegex = /^(https?|wss?|file|ftp|\*):\/\/(\*|\*\.[^*/:]+|[^*/:]+)\/.*$|^file:\/\/\/.*$|^about:/;
4
3
  const isFirefox = globalThis.navigator?.userAgent.includes('Firefox/');
5
4
  export const allStarsRegex = isFirefox
6
5
  ? /^(https?|wss?):[/][/][^/]+([/].*)?$/
@@ -14,7 +13,7 @@ export function assertValidPattern(matchPattern) {
14
13
  export function isValidPattern(matchPattern) {
15
14
  return matchPattern === '<all_urls>' || patternValidationRegex.test(matchPattern);
16
15
  }
17
- export function doesUrlMatchPatterns(url, ...patterns) {
16
+ export function testPatterns(url, patterns) {
18
17
  if (patterns.includes('<all_urls>') && allUrlsRegex.test(url)) {
19
18
  return true;
20
19
  }
@@ -28,8 +27,8 @@ export function doesUrlMatchPatterns(url, ...patterns) {
28
27
  }
29
28
  return false;
30
29
  }
31
- export function findMatchingPatterns(url, ...patterns) {
32
- return patterns.filter(pattern => doesUrlMatchPatterns(url, pattern));
30
+ export function getMatchingPatterns(url, patterns) {
31
+ return patterns.filter(pattern => testPatterns(url, [pattern]));
33
32
  }
34
33
  function getRawPatternRegex(matchPattern) {
35
34
  assertValidPattern(matchPattern);
@@ -73,7 +72,8 @@ function splitReplace(part, index) {
73
72
  }
74
73
  if (index % 2 === 0) {
75
74
  // Raw text, escape it
76
- return escapeStringRegexp(part);
75
+ // eslint-disable-next-line no-use-extend-native/no-use-extend-native -- TODO: Drop after https://github.com/dustinspecker/eslint-plugin-no-use-extend-native/issues/157
76
+ return RegExp.escape(part);
77
77
  }
78
78
  // Else: Symbol
79
79
  if (part.includes('*')) { // Can be more than one and it swallows surrounding question marks
@@ -100,7 +100,7 @@ export function globToRegex(...globs) {
100
100
  }
101
101
  return new RegExp(globs.map(x => getRawGlobRegex(x)).join('|'));
102
102
  }
103
- export function excludeDuplicatePatterns(matchPatterns) {
103
+ export function removeRedundantPatterns(matchPatterns) {
104
104
  if (matchPatterns.includes('<all_urls>')) {
105
105
  return ['<all_urls>'];
106
106
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webext-patterns",
3
- "version": "1.5.1",
3
+ "version": "2.0.1",
4
4
  "description": "Tool to convert the patterns and globs of your WebExtension manifest to regex",
5
5
  "keywords": [
6
6
  "browser",
@@ -21,43 +21,32 @@
21
21
  "license": "MIT",
22
22
  "author": "Federico Brigante <me@fregante.com> (https://fregante.com)",
23
23
  "type": "module",
24
- "exports": "./index.js",
25
- "types": "./index.d.ts",
24
+ "exports": "./distribution/index.js",
25
+ "types": "./distribution/index.d.ts",
26
26
  "files": [
27
- "index.js",
28
- "index.d.ts"
27
+ "distribution/index.js",
28
+ "distribution/index.d.ts"
29
29
  ],
30
30
  "scripts": {
31
31
  "build": "tsc",
32
32
  "fix": "xo --fix",
33
33
  "lint": "xo",
34
34
  "prepare": "tsc --sourceMap false",
35
- "test": "tsc && ava && xo",
35
+ "test": "tsc && vitest run && xo",
36
36
  "watch": "tsc --watch"
37
37
  },
38
- "xo": {
39
- "envs": [
40
- "browser",
41
- "webextensions"
42
- ],
43
- "rules": {
44
- "unicorn/better-regex": "off"
45
- }
46
- },
47
- "dependencies": {
48
- "escape-string-regexp": "^5.0.0"
49
- },
50
38
  "devDependencies": {
51
- "@sindresorhus/tsconfig": "^5.0.0",
52
- "@types/chrome": "0.0.268",
53
- "ava": "^6.1.3",
54
- "sinon": "^18.0.0",
55
- "type-fest": "^4.20.0",
56
- "typescript": "^5.4.5",
57
- "xo": "^0.58.0"
39
+ "@sindresorhus/tsconfig": "^8.1.0",
40
+ "@types/chrome": "^0.1.39",
41
+ "globals": "^17.4.0",
42
+ "sinon": "^21.0.3",
43
+ "type-fest": "^5.5.0",
44
+ "typescript": "^6.0.2",
45
+ "vitest": "^4.1.2",
46
+ "xo": "^2.0.2"
58
47
  },
59
48
  "engines": {
60
- "node": ">=18"
49
+ "node": ">=24"
61
50
  },
62
51
  "webExt": {
63
52
  "sourceDir": "demo-extension",
package/readme.md CHANGED
@@ -1,10 +1,9 @@
1
- # webext-patterns [![][badge-gzip]][link-bundlephobia]
2
-
3
- [badge-gzip]: https://img.shields.io/bundlephobia/minzip/webext-patterns.svg?label=gzipped
4
- [link-bundlephobia]: https://bundlephobia.com/result?p=webext-patterns
1
+ # webext-patterns
5
2
 
6
3
  > Utilities for [patterns](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/content_scripts#matching_url_patterns) and globs for WebExtensions
7
4
 
5
+ **Sponsored by [PixieBrix](https://www.pixiebrix.com)** :tada:
6
+
8
7
  ## Install
9
8
 
10
9
  You can download the [standalone bundle](https://bundle.fregante.com/?pkg=webext-patterns) and include it in your `manifest.json`.
@@ -19,8 +18,8 @@ npm install webext-patterns
19
18
  import {
20
19
  patternToRegex,
21
20
  globToRegex,
22
- excludeDuplicatePatterns
23
- doesUrlMatchPatterns,
21
+ removeRedundantPatterns,
22
+ testPatterns,
24
23
  assertValidPattern,
25
24
  isValidPattern,
26
25
  } from 'webext-patterns';
@@ -35,7 +34,7 @@ patternToRegex('http://*/*');
35
34
  globToRegex('*.example.com');
36
35
  // Returns /\.example\.com$/
37
36
 
38
- excludeDuplicatePatterns(['https://*.google.com/*', 'https://google.com/*']);
37
+ removeRedundantPatterns(['https://*.google.com/*', 'https://google.com/*']);
39
38
  // Returns ['https://*.google.com/*']
40
39
 
41
40
  assertValidPattern('https://google.*/*');
@@ -48,6 +47,9 @@ isValidPattern('https://*.google.com/*');
48
47
  > **Note**
49
48
  > Firefox and Chrome handle patterns very slighly differently. `webext-patterns` defaults to Chrome’s logic, but if it detects a Firefox userAgent it will produce a Firefox-compatible regex.
50
49
 
50
+ > **Note**
51
+ > This package uses `RegExp.escape`, which requires browsers from May 2025 and later.
52
+
51
53
  ## API
52
54
 
53
55
  #### patternToRegex(pattern1, pattern2, etc)
@@ -97,12 +99,12 @@ googleRegex.test('https://google.it/search'); // -> true
97
99
  googleRegex.test('https://google.de/search'); // -> false
98
100
  ```
99
101
 
100
- #### excludeDuplicatePatterns([pattern1, pattern2, etc])
102
+ #### removeRedundantPatterns([pattern1, pattern2, etc])
101
103
 
102
104
  Accepts an array of patterns and returns a filtered array without the patterns that are already covered by others. For example `"https://*/*"` already covers all "https" URLs, so having `"https://google.com/*"` in the array won't make any difference and therefore it's dropped.
103
105
 
104
106
  ```js
105
- excludeDuplicatePatterns([
107
+ removeRedundantPatterns([
106
108
  "https://*/*",
107
109
  "https://google.com/*",
108
110
  "https://*.example.com/*",
@@ -110,18 +112,18 @@ excludeDuplicatePatterns([
110
112
  // Returns ["https://*/*"]
111
113
  ```
112
114
 
113
- #### doesUrlMatchPatterns(url, ...patterns)
115
+ #### testPatterns(url, patterns)
114
116
 
115
- Accepts a URL and any number of patterns and returns `true` if the URL matches any of the patterns. This is a convenience method that wraps `patternToRegex` for single use. If you plan on testing multiple URLs to the same pattern, it's better to convert the patterns to a regex once and reuse that.
117
+ Accepts a URL and an array of patterns and returns `true` if the URL matches any of the patterns. This is a convenience method that wraps `patternToRegex` for single use. If you plan on testing multiple URLs against the same patterns, it's better to convert the patterns to a regex once and reuse that.
116
118
 
117
119
  ```js
118
- doesUrlMatchPatterns('https://google.com/', 'https://*.google.com/*', '*://example.com/*');
120
+ testPatterns('https://google.com/', ['https://*.google.com/*', '*://example.com/*']);
119
121
  // Returns true
120
122
  ```
121
123
 
122
- #### findMatchingPatterns(url, ...patterns)
124
+ #### getMatchingPatterns(url, patterns)
123
125
 
124
- Accepts a URL and any number of patterns and returns an array of the patterns that match the URL. It returns an empty array if none of the patterns match the URL.
126
+ Accepts a URL and an array of patterns and returns an array of the patterns that match the URL. It returns an empty array if none of the patterns match the URL.
125
127
 
126
128
  #### assertValidPattern(pattern)
127
129