Skip to content

Commit

Permalink
Add new '@include' directive option for adding modifiers in filter li…
Browse files Browse the repository at this point in the history
…st rules.
  • Loading branch information
jellizaveta committed Oct 23, 2023
1 parent bf4e1a7 commit f74ee14
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/main/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ module.exports = (function () {
const INCLUDE_OPTION_COMMENTS = '/stripComments';
const INCLUDE_OPTION_NOT_OPTIMIZED = '/notOptimized';
const INCLUDE_OPTION_EXCLUDE = '/exclude=';
const INCLUDE_OPTION_MODIFIERS = '/addModifiers=';

const NOT_OPTIMIZED_HINT = '!+ NOT_OPTIMIZED';

Expand Down Expand Up @@ -130,6 +131,29 @@ module.exports = (function () {

return result;
};
/**
* Adds modifiers to lines of text.
*
* @param {string[]} lines - An array of text lines.
* @param {string} modifier - The modifier to add.
* @returns {string[]} - An array of modified lines.
*/
const addModifiers = (lines, modifier) => {
const result = [];
lines.forEach((line) => {
// Check if the line is empty or null, and skip it.
if (!line || line.length === 0) {
return result;
}
// Check if the line doesn't start with '!' character (comment).
if (!line.startsWith('! ')) {
// Add the specified modifier to the line.
result.push(`${line}$${modifier}`);
}
});
// Return the array of modified lines.
return result;
};

/**
* Checks case when '#%#' rules are excluded to DON'T exclude '#%#//scriptlet' rules
Expand Down Expand Up @@ -241,9 +265,16 @@ module.exports = (function () {
let stripComments = false;
let notOptimized = false;
let exclude = null;
let includedModifiers = null;

for (let i = 1; i < parts.length; i += 1) {
const attribute = parts[i].trim();
if (attribute.startsWith(INCLUDE_OPTION_MODIFIERS)) {
// Extract the value inside quotes, if it exists.
const modifierMatch = attribute.match(/=["'](.*?)["']/);
// Set the 'includedModifiers' variable to the extracted value or to null if no match is found.
includedModifiers = modifierMatch ? modifierMatch[1] : null;
}
if (attribute.startsWith(INCLUDE_OPTION_COMMENTS)) {
stripComments = true;
} else if (attribute.startsWith(INCLUDE_OPTION_NOT_OPTIMIZED)) {
Expand All @@ -256,6 +287,7 @@ module.exports = (function () {

return {
url,
includedModifiers,
stripComments,
notOptimized,
exclude,
Expand Down Expand Up @@ -323,6 +355,11 @@ module.exports = (function () {
if (options.notOptimized) {
result = addNotOptimizedHints(result);
}
const modifier = options.includedModifiers;

if (modifier) {
result = addModifiers(result, modifier);
}

result = workaround.fixVersionComments(result);
} else {
Expand Down
16 changes: 16 additions & 0 deletions src/test/builder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,22 @@ describe('Test builder', () => {
expect(filterLines.includes('regularexpression_not_excluded')).toBeTruthy();
});

it('platforms/test filters 8.txt', async () => {
// Read the content of the file and split it into lines
const filterContent = await readFile(path.join(filtersDir, 'filter_8_Includes', 'filter.txt'));
const filterLines = filterContent.split(/\r?\n/);
// Check that the number of lines in the file is equal to 15
expect(filterLines.length).toEqual(15);
// Check that the array of lines is not empty
expect(filterLines).toBeTruthy();
// Check that each line (excluding comments) ends with '$image,script'
filterLines.forEach((line) => {
if (!line.startsWith('!')) {
expect(line.trim().endsWith('$image,script')).toBeTruthy();
}
});
});

it('platforms/test filters 5.txt', async () => {
const filterContent = await readFile(path.join(__dirname, 'resources/platforms/test', 'filters', '5.txt'));
const filterLines = filterContent.split(/\r?\n/);
Expand Down
15 changes: 15 additions & 0 deletions src/test/resources/filters/cname_trackers.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
a155e09a56.reuters.tv
albany.townsquarenewsletters.com
alerts.dmgt.com
aremedia.e.aremedia.com.au
auto.scissorsscotch.com
aww.e.aremedia.com.au
! comment
aww.e.bauer-media.com.au
babyhorizon.zola.com
! comment
! comment
babylink.zola.com
bauerworks.e.bauer-media.com.au
bhg.e.aremedia.com.au
binghamton.townsquaremedia.info
Empty file.
13 changes: 13 additions & 0 deletions src/test/resources/filters/filter_8_Includes/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"filterId": 88,
"name": "Includes Filter",
"description": "Includes Filter description",
"timeAdded": 1404115015848,
"homepage": "https://example.com/",
"expires": "2 days",
"displayNumber": 101,
"groupId": 2,
"subscriptionUrl": "https://example.com/",
"tags": [],
"trustLevel": "full"
}
4 changes: 4 additions & 0 deletions src/test/resources/filters/filter_8_Includes/template.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
! Homepage: http://adguard.com/filters.html#english
! License: http://creativecommons.org/licenses/by-sa/3.0/
!
@include ../cname_trackers.txt /addModifiers="image,script"

0 comments on commit f74ee14

Please sign in to comment.