Skip to content

Commit 3624b83

Browse files
committed
Merge branch 'use-exact-match-for-regular-expresson' into 'master'
Use exact match for regular expresson See merge request clear-code/browserselector!88
2 parents bf534ad + 2a702ae commit 3624b83

3 files changed

Lines changed: 21 additions & 6 deletions

File tree

webextensions/chrome/background.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ function wildmat(text, pat) {
6666
function regexMatch(text, pattern) {
6767
let regExp;
6868
try {
69-
regExp = new RegExp(pattern);
69+
regExp = new RegExp(`^${pattern}$`);
7070
}
7171
catch(_error) {
7272
console.log('failed to compile a regex pattern: ', pattern);

webextensions/edge/background.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ function wildmat(text, pat) {
6666
function regexMatch(url, pattern) {
6767
let regExp;
6868
try {
69-
regExp = new RegExp(pattern);
69+
regExp = new RegExp(`^${pattern}$`);
7070
}
7171
catch(_error) {
7272
console.log('failed to compile a regex pattern: ', pattern);

webextensions/test/test-is-redirect-url.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ describe('isRedirectURL', () => {
3838
});
3939
it(`Match redirect pattern with regex: partial match`, () => {
4040
const url = 'http://www.example.com/';
41-
const conf = config([['http://www\.example\.com/', 'firefox']], [], { UseRegex: 1 })
41+
const conf = config([['http://www\.example\..*', 'firefox']], [], { UseRegex: 1 })
4242
assert.equal(redirector.isRedirectURL(conf, url), true);
4343
});
44-
it(`Match redirect pattern with regex: exact match`, () => {
44+
it(`Match redirect pattern with regex: extra caret and dollar`, () => {
4545
const url = 'http://www.example.com/';
4646
const conf = config([['^http://www\.example\.com/$', 'firefox']], [], { UseRegex: 1 })
4747
assert.equal(redirector.isRedirectURL(conf, url), true);
@@ -114,6 +114,11 @@ describe('isRedirectURL', () => {
114114
const conf = config([['http://www\.example\.com/', 'firefox']], [], { UseRegex: 1 })
115115
assert.equal(redirector.isRedirectURL(conf, url), false);
116116
});
117+
it(`Unmatch redirect pattern with regex: not exact match`, () => {
118+
const url = 'http://www.example.com/';
119+
const conf = config([['\.example\.', 'firefox']], [], { UseRegex: 1 })
120+
assert.equal(redirector.isRedirectURL(conf, url), false);
121+
});
117122
});
118123
describe('HostName patterns', () => {
119124
it(`Match redirect pattern without wildcard`, () => {
@@ -126,16 +131,21 @@ describe('isRedirectURL', () => {
126131
const conf = config([], [['*.example.com', 'firefox']])
127132
assert.equal(redirector.isRedirectURL(conf, url), true);
128133
});
129-
it(`Match redirect pattern with regex: partial match`, () => {
134+
it(`Match redirect pattern with regex`, () => {
130135
const url = 'http://www.example.com/';
131136
const conf = config([], [['www\.example\.com', 'firefox']], { UseRegex: 1 })
132137
assert.equal(redirector.isRedirectURL(conf, url), true);
133138
});
134-
it(`Match redirect pattern with regex: exact match`, () => {
139+
it(`Match redirect pattern with regex: extra caret and dollar`, () => {
135140
const url = 'http://www.example.com/';
136141
const conf = config([], [['^www\.example\.com$', 'firefox']], { UseRegex: 1 })
137142
assert.equal(redirector.isRedirectURL(conf, url), true);
138143
});
144+
it(`Match redirect pattern with regex: partial match`, () => {
145+
const url = 'http://www.example.com/';
146+
const conf = config([], [['.*\.example\..*', 'firefox']], { UseRegex: 1 })
147+
assert.equal(redirector.isRedirectURL(conf, url), true);
148+
});
139149
it(`Match first pattern: redirection (matches firefox)`, () => {
140150
const url = 'http://www.example.com/';
141151
const conf = config(
@@ -215,6 +225,11 @@ describe('isRedirectURL', () => {
215225
const conf = config([], [['http://www.example.com', 'firefox']], { UseRegex: 1 })
216226
assert.equal(redirector.isRedirectURL(conf, url), false);
217227
});
228+
it(`Unmatch redirect pattern with regex: not exact match`, () => {
229+
const url = 'http://www.example.com/';
230+
const conf = config([], [['\.example\.', 'firefox']], { UseRegex: 1 })
231+
assert.equal(redirector.isRedirectURL(conf, url), false);
232+
});
218233
});
219234
});
220235
});

0 commit comments

Comments
 (0)