From 37dbda48796a556b75398cdc7f06dd95c8464236 Mon Sep 17 00:00:00 2001 From: Krzysztof Modras <1228153+chrmod@users.noreply.github.com> Date: Wed, 16 Oct 2024 15:37:17 +0200 Subject: [PATCH] Remove wrapping slashes from regexps (#20) --- src/converters/helpers.js | 8 +++----- test/unit/converters/adguard.spec.js | 28 +++++++++++++++++++++++----- test/unit/converters/helpers.spec.js | 4 ++-- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/converters/helpers.js b/src/converters/helpers.js index f004bfe..9f8b459 100644 --- a/src/converters/helpers.js +++ b/src/converters/helpers.js @@ -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) { diff --git a/test/unit/converters/adguard.spec.js b/test/unit/converters/adguard.spec.js index 28a939e..f91ef97 100644 --- a/test/unit/converters/adguard.spec.js +++ b/test/unit/converters/adguard.spec.js @@ -19,22 +19,22 @@ 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: { @@ -42,10 +42,28 @@ describe('adguard converter', () => { }, 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 + }); + }); }); diff --git a/test/unit/converters/helpers.spec.js b/test/unit/converters/helpers.spec.js index 55de872..69731b9 100644 --- a/test/unit/converters/helpers.spec.js +++ b/test/unit/converters/helpers.spec.js @@ -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', }, }); });