Skip to content

Commit

Permalink
feat: modifier replacer for NetworkFilter.toString (#3699)
Browse files Browse the repository at this point in the history
* feat: modifier replacer for NetworkFilter.toString

To make minimal performance impact on current codes using adblocker library, I chose to use Array.prototype.map function instead of putting `if` statements or using empty proxying function: `(str: text) => str` as default value.

refs #3693

* chore: fix types

use `as const` keyword

* chore(test): fix test output

refs #3681

* fix(test): test expectation to match latest commit

refs #3681
  • Loading branch information
seia-soto authored Jan 10, 2024
1 parent bc9718c commit 97b3069
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
8 changes: 6 additions & 2 deletions packages/adblocker/src/filters/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1087,7 +1087,7 @@ export default class NetworkFilter implements IFilter {
* there are things which cannot be recovered though, like domains options
* of which only hashes are stored.
*/
public toString() {
public toString(modifierReplacer?: (modifier: string) => string) {
if (this.rawLine !== undefined) {
return this.rawLine;
}
Expand Down Expand Up @@ -1201,7 +1201,11 @@ export default class NetworkFilter implements IFilter {
}

if (options.length > 0) {
filter += `$${options.join(',')}`;
if (typeof modifierReplacer === 'function') {
filter += `$${options.map(modifierReplacer).join(',')}`;
} else {
filter += `$${options.join(',')}`;
}
}

return filter;
Expand Down
23 changes: 19 additions & 4 deletions packages/adblocker/test/parsing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,16 @@ const DEFAULT_NETWORK_FILTER = {

describe('Network filters', () => {
describe('toString', () => {
const checkToString = (line: string, expected: string, debug: boolean = false) => {
const checkToString = (
line: string,
expected: string,
debug: boolean = false,
modifierReplacer = (modifier: string) => modifier,
) => {
const parsed = NetworkFilter.parse(line, debug);
expect(parsed).not.to.be.null;
if (parsed !== null) {
expect(parsed.toString()).to.equal(expected);
expect(parsed.toString(modifierReplacer)).to.equal(expected);
}
};

Expand Down Expand Up @@ -178,6 +183,16 @@ describe('Network filters', () => {
true,
);
});

it('pprint longer form of modifiers', () => {
checkToString('||foo.com^$3p', '||foo.com^$third-party', false, (modifier) => {
if (modifier === '3p') {
return 'third-party';
}

return modifier;
});
});
});

it('parses pattern', () => {
Expand Down Expand Up @@ -1365,9 +1380,9 @@ describe('Network filters', () => {
[new Uint32Array([NORMALIZED_TYPE_TOKEN.document])],
],
['@@/wp-content/themes/$script', [hashStrings(['content'])]],
]) {
] as const) {
it(`get tokens for ${filter}`, () => {
const parsed = NetworkFilter.parse(filter as string, true);
const parsed = NetworkFilter.parse(filter, true);
expect(parsed).not.to.be.null;
if (parsed !== null) {
expect(parsed.getTokens()).to.eql(regexTokens);
Expand Down

0 comments on commit 97b3069

Please sign in to comment.