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 13560b6 commit 86d9dc6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
32 changes: 22 additions & 10 deletions packages/adblocker/src/filters/cosmetic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ 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 @@ -759,16 +768,19 @@ export default class CosmeticFilter implements IFilter {
}
return part;
})
.map((part: string) =>
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
part
.replaceAll(String.raw`\u002C`, ',')
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
.replaceAll(String.raw`\u005C`, '\\')
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
.replaceAll(/\\([^bfnrtvxu0])/g, String.raw`\\$1`),
);

.map((part: string) => {
const withoutUnicodeCommas = replaceAll(part, String.raw`\u002C`, ',');
const withoutUnicodeBackslashed = replaceAll(
withoutUnicodeCommas,
String.raw`\u005C`,
'\\',
);
const withDoubleEscaping = withoutUnicodeBackslashed.replace(
/\\([^bfnrtvxu0])/g,
String.raw`\\$1`,
);
return withDoubleEscaping;
});
return { name: parts[0], args };
}

Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"target": "es2021",
"target": "es2017",
"module": "commonjs",
"moduleResolution": "node",
"strict": true,
Expand Down

0 comments on commit 86d9dc6

Please sign in to comment.