Skip to content

Commit

Permalink
Remove wrapping slashes from regexps (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrmod authored Oct 16, 2024
1 parent a574de9 commit 37dbda4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
8 changes: 3 additions & 5 deletions src/converters/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,10 @@ export function normalizeRule(
if (
newRule.condition &&
newRule.condition.regexFilter &&
!(
newRule.condition.regexFilter.startsWith("/") &&
newRule.condition.regexFilter.endsWith("/")
)
newRule.condition.regexFilter.startsWith("/") &&
newRule.condition.regexFilter.endsWith("/")
) {
newRule.condition.regexFilter = `/${newRule.condition.regexFilter}/`;
newRule.condition.regexFilter = newRule.condition.regexFilter.slice(1,-1)
}

if (newRule.condition && newRule.condition.excludedDomains) {
Expand Down
28 changes: 23 additions & 5 deletions test/unit/converters/adguard.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,51 @@ describe('adguard converter', () => {
});

// to be fixed with https://github.com/AdguardTeam/tsurlfilter/pull/109
it.skip("/baynote(-observer)?([0-9]+)\.js/", async () => {
const { rules } = await convertWithAdguard(['/baynote(-observer)?([0-9]+)\.js/']);
it("/baynote(-observer)?([0-9]+)\.js/", async () => {
const { rules } = await convertWithAdguard([String.raw`/baynote(-observer)?([0-9]+)\.js/`]);
expect(rules[0]).toEqual({
action: {
type: "block"
},
condition: {
isUrlFilterCaseSensitive: false,
regexFilter: "/baynote(-observer)?([0-9]+)\.js/"
regexFilter: String.raw`baynote(-observer)?([0-9]+)\.js`
},
id: 1,
priority: 1
});
});

it.skip("handles regexp with ?", async () => {
it("handles regexp with ?", async () => {
const { rules } = await convertWithAdguard(['/a?/']);
expect(rules[0]).toEqual({
action: {
type: "block"
},
condition: {
isUrlFilterCaseSensitive: false,
regexFilter: "/a?/"
regexFilter: "a?"
},
id: 1,
priority: 1
});
});

it("handles regexp escaping", async () => {
const { rules } = await convertWithAdguard([String.raw`/\\d/$doc`]);
expect(rules[0]).toEqual({
action: {
type: "block"
},
condition: {
isUrlFilterCaseSensitive: false,
regexFilter: String.raw`\\d`,
resourceTypes: [
"main_frame"
]
},
id: 1,
priority: 101
});
});
});
4 changes: 2 additions & 2 deletions test/unit/converters/helpers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ describe('normalizeRule', () => {
})
})

it('wraps regex rules in //', () => {
it('does not wraps regex rules in //', () => {
expect(normalizeRule({
condition: {
regexFilter: 'test',
},
})).toEqual({
condition: {
regexFilter: '/test/',
regexFilter: 'test',
},
});
});
Expand Down

0 comments on commit 37dbda4

Please sign in to comment.