Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
chrmod committed Nov 9, 2023
1 parent 0d7eee6 commit cb90d99
Showing 1 changed file with 4 additions and 17 deletions.
21 changes: 4 additions & 17 deletions packages/adblocker/src/filters/cosmetic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,6 @@ function isSimpleHrefSelector(selector: string, start: number): boolean {
);
}

// source https://stackoverflow.com/a/1144788
function escapeRegExp(string: string): string {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}

function replaceAll(str: string, find: string, replace: string): string {
return str.replace(new RegExp(escapeRegExp(find), 'g'), replace);
}

/**
* Validate CSS selector. There is a fast path for simple selectors (e.g.: #foo
* or .bar) which are the most common case. For complex ones, we rely on
Expand Down Expand Up @@ -769,13 +760,9 @@ export default class CosmeticFilter implements IFilter {
return part;
})
.map((part: string) => {
const withoutUnicodeCommas = replaceAll(part, String.raw`\u002C`, ',');
const withoutUnicodeBackslashed = replaceAll(
withoutUnicodeCommas,
String.raw`\u005C`,
'\\',
);
return withoutUnicodeBackslashed;
return part
.replace(new RegExp(String.raw`\u002C`, 'g'), ',')
.replace(new RegExp(String.raw`\u005C`, 'g'), '\\');
});
return { name: parts[0], args };
}
Expand All @@ -791,7 +778,7 @@ export default class CosmeticFilter implements IFilter {
let script = js.get(name);
if (script !== undefined) {
for (let i = 0; i < args.length; i += 1) {
// escape some characters so they wont get evaluated with escape during script injection
// escape some characters so they wont get evaluated with escape characters during script injection
const arg = args[i].replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
script = script.replace(`{{${i + 1}}}`, arg);
}
Expand Down

0 comments on commit cb90d99

Please sign in to comment.