Skip to content

Commit

Permalink
fix: correctly escape special characters while generating regex for s…
Browse files Browse the repository at this point in the history
…horteners
  • Loading branch information
double-beep authored May 22, 2024
1 parent e5f66d2 commit a734a11
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
3 changes: 2 additions & 1 deletion dist/fire_extra.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,8 @@
// (?-i:) - case sensitive
// (?#) - the shortener domain
getRegexForPathShortener: (path, domain) => {
const mainPart = `(?-i:${path})`;
const escaped = path.replace(/[/\-\\^$*+?.()|[\]{}]/g, "\\$&");
const mainPart = `(?-i:${escaped})`;
const comment = `(?#${domain || ""})`;
return `${mainPart}${domain ? comment : ""}`;
}
Expand Down
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ export const helpers = {
// (?-i:) - case sensitive
// (?#) - the shortener domain
getRegexForPathShortener: (path: string, domain?: string): string => {
const mainPart = `(?-i:${path})`;
// https://stackoverflow.com/a/3561711
// https://chat.stackexchange.com/transcript/message/65665204
const escaped = path.replace(/[/\-\\^$*+?.()|[\]{}]/g, '\\$&');
const mainPart = `(?-i:${escaped})`;
const comment = `(?#${domain || ''})`;

return `${mainPart}${domain ? comment : ''}`;
Expand Down
5 changes: 4 additions & 1 deletion test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ describe('index helpers', () => {
{
'3vcWir3': ['bit.ly', '(?-i:3vcWir3)(?#bit.ly)'],
'FNEuyd': ['goo.gl', '(?-i:FNEuyd)(?#goo.gl)'],
'KdxEAt91D7k': ['youtu.be', '(?-i:KdxEAt91D7k)(?#youtu.be)']
'KdxEAt91D7k': ['youtu.be', '(?-i:KdxEAt91D7k)(?#youtu.be)'],
'+jJyLwSpqLeAzNmFi': ['t.me', String.raw`(?-i:\+jJyLwSpqLeAzNmFi)(?#t.me)`],
'davitacols/dataDisk': ['github repository', String.raw`(?-i:davitacols\/dataDisk)(?#github repository)`],
'arjun.muralidharan2': ['facebook', String.raw`(?-i:arjun\.muralidharan2)(?#facebook)`]
}
).forEach(([path, info]) => {
const [domain, expectedValue] = info;
Expand Down

0 comments on commit a734a11

Please sign in to comment.