Skip to content

Commit 6b8e2d3

Browse files
committed
improve args splitting. AG-12792 #194
Squashed commit of the following: commit 4105d8e Author: Slava Leleka <[email protected]> Date: Tue Feb 15 23:47:30 2022 +0300 revert tests commit 5ec5887 Author: Slava Leleka <[email protected]> Date: Tue Feb 15 22:21:05 2022 +0300 improve args splitting commit f784026 Author: Slava Leleka <[email protected]> Date: Tue Feb 15 21:23:08 2022 +0300 rebuild
1 parent e01f7bf commit 6b8e2d3

File tree

8 files changed

+124
-48
lines changed

8 files changed

+124
-48
lines changed

dist/build.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version=1.5.14
1+
version=1.5.16

dist/redirects.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
/**
33
* AdGuard Scriptlets
4-
* Version 1.5.14
4+
* Version 1.5.16
55
*/
66

77
var Redirects = (function () {

dist/redirects.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#
22
# AdGuard Scriptlets (Redirects Source)
3-
# Version 1.5.14
3+
# Version 1.5.16
44
#
55
- title: 1x1-transparent.gif
66
description: |-

dist/scriptlets.corelibs.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "1.5.14",
2+
"version": "1.5.16",
33
"scriptlets": [
44
{
55
"names": [

dist/scriptlets.js

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
/**
33
* AdGuard Scriptlets
4-
* Version 1.5.14
4+
* Version 1.5.16
55
*/
66

77
(function () {
@@ -6247,6 +6247,7 @@
62476247
var REMOVE_CLASS_ALIASES = scriptletList[REMOVE_CLASS_METHOD].names;
62486248
var ADG_REMOVE_ATTR_NAME = REMOVE_ATTR_ALIASES[0];
62496249
var ADG_REMOVE_CLASS_NAME = REMOVE_CLASS_ALIASES[0];
6250+
var REMOVE_ATTR_CLASS_APPLYING = ['asap', 'stay', 'complete'];
62506251
/**
62516252
* Returns array of strings separated by space which not in quotes
62526253
* @param {string} str
@@ -6270,8 +6271,26 @@
62706271
return acc;
62716272
}, str);
62726273
};
6274+
6275+
var splitArgs = function splitArgs(str) {
6276+
var args = [];
6277+
var prevArgStart = 0;
6278+
6279+
for (var i = 0; i < str.length; i += 1) {
6280+
// do not split args by escaped comma
6281+
// https://github.com/AdguardTeam/Scriptlets/issues/133
6282+
if (str[i] === COMMA_SEPARATOR && str[i - 1] !== '\\') {
6283+
args.push(str.slice(prevArgStart, i).trim());
6284+
prevArgStart = i + 1;
6285+
}
6286+
} // collect arg after last comma
6287+
6288+
6289+
args.push(str.slice(prevArgStart, str.length).trim());
6290+
return args;
6291+
};
62736292
/**
6274-
* Converts string of UBO scriptlet rule to AdGuard scritlet rule
6293+
* Converts string of UBO scriptlet rule to AdGuard scriptlet rule
62756294
* @param {string} rule - UBO scriptlet rule
62766295
* @returns {Array} - array with one AdGuard scriptlet rule
62776296
*/
@@ -6286,26 +6305,39 @@
62866305
template = ADGUARD_SCRIPTLET_EXCEPTION_TEMPLATE;
62876306
} else {
62886307
template = ADGUARD_SCRIPTLET_TEMPLATE;
6289-
} // do not split args by escaped comma
6290-
// https://github.com/AdguardTeam/Scriptlets/issues/133
6291-
6292-
6293-
var parsedArgs = getStringInBraces(rule).split(/(?<!\\),\s/g);
6294-
6295-
if (parsedArgs.length === 1) {
6296-
// Most probably this is not correct separator, in this case we use ','
6297-
parsedArgs = getStringInBraces(rule).split(/,/g);
62986308
}
62996309

6310+
var argsStr = getStringInBraces(rule);
6311+
var parsedArgs = splitArgs(argsStr);
63006312
var scriptletName = parsedArgs[0].indexOf(UBO_SCRIPTLET_JS_ENDING) > -1 ? "ubo-".concat(parsedArgs[0]) : "ubo-".concat(parsedArgs[0]).concat(UBO_SCRIPTLET_JS_ENDING);
63016313

63026314
if ((REMOVE_ATTR_ALIASES.indexOf(scriptletName) > -1 || REMOVE_CLASS_ALIASES.indexOf(scriptletName) > -1) && parsedArgs.length > MAX_REMOVE_ATTR_CLASS_ARGS_COUNT) {
6303-
parsedArgs = [parsedArgs[0], parsedArgs[1], // if there are more than 3 args for remove-attr/class scriptlet,
6315+
// if there are more than 4 args for remove-attr/class scriptlet,
63046316
// ubo rule has multiple selector separated by comma. so we should:
6305-
// 1. join them into a single string
6306-
// 2. replace escaped commas by regular ones
6317+
// 1. check if last arg is 'applying' parameter
6318+
// 2. join 'selector' into one arg
6319+
// 3. combine all args
63076320
// https://github.com/AdguardTeam/Scriptlets/issues/133
6308-
replaceAll(parsedArgs.slice(2).join("".concat(COMMA_SEPARATOR, " ")), ESCAPED_COMMA_SEPARATOR, COMMA_SEPARATOR)];
6321+
var lastArg = parsedArgs.pop();
6322+
6323+
var _parsedArgs = parsedArgs,
6324+
_parsedArgs2 = toArray(_parsedArgs),
6325+
name = _parsedArgs2[0],
6326+
value = _parsedArgs2[1],
6327+
restArgs = _parsedArgs2.slice(2);
6328+
6329+
var applying; // check the last parsed arg for matching possible 'applying' vale
6330+
6331+
if (REMOVE_ATTR_CLASS_APPLYING.some(function (el) {
6332+
return lastArg.indexOf(el) > -1;
6333+
})) {
6334+
applying = lastArg;
6335+
} else {
6336+
restArgs.push(lastArg);
6337+
}
6338+
6339+
var selector = restArgs.join(', ');
6340+
parsedArgs = applying ? [name, value, selector, applying] : [name, value, selector];
63096341
}
63106342

63116343
var args = parsedArgs.map(function (arg, index) {

dist/umd/scriptlets.umd.js

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
/**
33
* AdGuard Scriptlets
4-
* Version 1.5.14
4+
* Version 1.5.16
55
*/
66

77
(function (factory) {
@@ -6249,6 +6249,7 @@
62496249
var REMOVE_CLASS_ALIASES = scriptletList[REMOVE_CLASS_METHOD].names;
62506250
var ADG_REMOVE_ATTR_NAME = REMOVE_ATTR_ALIASES[0];
62516251
var ADG_REMOVE_CLASS_NAME = REMOVE_CLASS_ALIASES[0];
6252+
var REMOVE_ATTR_CLASS_APPLYING = ['asap', 'stay', 'complete'];
62526253
/**
62536254
* Returns array of strings separated by space which not in quotes
62546255
* @param {string} str
@@ -6272,8 +6273,26 @@
62726273
return acc;
62736274
}, str);
62746275
};
6276+
6277+
var splitArgs = function splitArgs(str) {
6278+
var args = [];
6279+
var prevArgStart = 0;
6280+
6281+
for (var i = 0; i < str.length; i += 1) {
6282+
// do not split args by escaped comma
6283+
// https://github.com/AdguardTeam/Scriptlets/issues/133
6284+
if (str[i] === COMMA_SEPARATOR && str[i - 1] !== '\\') {
6285+
args.push(str.slice(prevArgStart, i).trim());
6286+
prevArgStart = i + 1;
6287+
}
6288+
} // collect arg after last comma
6289+
6290+
6291+
args.push(str.slice(prevArgStart, str.length).trim());
6292+
return args;
6293+
};
62756294
/**
6276-
* Converts string of UBO scriptlet rule to AdGuard scritlet rule
6295+
* Converts string of UBO scriptlet rule to AdGuard scriptlet rule
62776296
* @param {string} rule - UBO scriptlet rule
62786297
* @returns {Array} - array with one AdGuard scriptlet rule
62796298
*/
@@ -6288,26 +6307,39 @@
62886307
template = ADGUARD_SCRIPTLET_EXCEPTION_TEMPLATE;
62896308
} else {
62906309
template = ADGUARD_SCRIPTLET_TEMPLATE;
6291-
} // do not split args by escaped comma
6292-
// https://github.com/AdguardTeam/Scriptlets/issues/133
6293-
6294-
6295-
var parsedArgs = getStringInBraces(rule).split(/(?<!\\),\s/g);
6296-
6297-
if (parsedArgs.length === 1) {
6298-
// Most probably this is not correct separator, in this case we use ','
6299-
parsedArgs = getStringInBraces(rule).split(/,/g);
63006310
}
63016311

6312+
var argsStr = getStringInBraces(rule);
6313+
var parsedArgs = splitArgs(argsStr);
63026314
var scriptletName = parsedArgs[0].indexOf(UBO_SCRIPTLET_JS_ENDING) > -1 ? "ubo-".concat(parsedArgs[0]) : "ubo-".concat(parsedArgs[0]).concat(UBO_SCRIPTLET_JS_ENDING);
63036315

63046316
if ((REMOVE_ATTR_ALIASES.indexOf(scriptletName) > -1 || REMOVE_CLASS_ALIASES.indexOf(scriptletName) > -1) && parsedArgs.length > MAX_REMOVE_ATTR_CLASS_ARGS_COUNT) {
6305-
parsedArgs = [parsedArgs[0], parsedArgs[1], // if there are more than 3 args for remove-attr/class scriptlet,
6317+
// if there are more than 4 args for remove-attr/class scriptlet,
63066318
// ubo rule has multiple selector separated by comma. so we should:
6307-
// 1. join them into a single string
6308-
// 2. replace escaped commas by regular ones
6319+
// 1. check if last arg is 'applying' parameter
6320+
// 2. join 'selector' into one arg
6321+
// 3. combine all args
63096322
// https://github.com/AdguardTeam/Scriptlets/issues/133
6310-
replaceAll(parsedArgs.slice(2).join("".concat(COMMA_SEPARATOR, " ")), ESCAPED_COMMA_SEPARATOR, COMMA_SEPARATOR)];
6323+
var lastArg = parsedArgs.pop();
6324+
6325+
var _parsedArgs = parsedArgs,
6326+
_parsedArgs2 = toArray(_parsedArgs),
6327+
name = _parsedArgs2[0],
6328+
value = _parsedArgs2[1],
6329+
restArgs = _parsedArgs2.slice(2);
6330+
6331+
var applying; // check the last parsed arg for matching possible 'applying' vale
6332+
6333+
if (REMOVE_ATTR_CLASS_APPLYING.some(function (el) {
6334+
return lastArg.indexOf(el) > -1;
6335+
})) {
6336+
applying = lastArg;
6337+
} else {
6338+
restArgs.push(lastArg);
6339+
}
6340+
6341+
var selector = restArgs.join(', ');
6342+
parsedArgs = applying ? [name, value, selector, applying] : [name, value, selector];
63116343
}
63126344

63136345
var args = parsedArgs.map(function (arg, index) {

src/helpers/converter.js

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ const REMOVE_ATTR_ALIASES = scriptletList[REMOVE_ATTR_METHOD].names;
5959
const REMOVE_CLASS_ALIASES = scriptletList[REMOVE_CLASS_METHOD].names;
6060
const ADG_REMOVE_ATTR_NAME = REMOVE_ATTR_ALIASES[0];
6161
const ADG_REMOVE_CLASS_NAME = REMOVE_CLASS_ALIASES[0];
62+
const REMOVE_ATTR_CLASS_APPLYING = ['asap', 'stay', 'complete'];
6263

6364
/**
6465
* Returns array of strings separated by space which not in quotes
@@ -121,20 +122,25 @@ export const convertUboScriptletToAdg = (rule) => {
121122
if (((REMOVE_ATTR_ALIASES.indexOf(scriptletName) > -1)
122123
|| (REMOVE_CLASS_ALIASES.indexOf(scriptletName) > -1))
123124
&& parsedArgs.length > MAX_REMOVE_ATTR_CLASS_ARGS_COUNT) {
124-
parsedArgs = [
125-
parsedArgs[0],
126-
parsedArgs[1],
127-
// if there are more than 3 args for remove-attr/class scriptlet,
128-
// ubo rule has multiple selector separated by comma. so we should:
129-
// 1. join them into a single string
130-
// 2. replace escaped commas by regular ones
131-
// https://github.com/AdguardTeam/Scriptlets/issues/133
132-
replaceAll(
133-
parsedArgs.slice(2).join(`${COMMA_SEPARATOR} `),
134-
ESCAPED_COMMA_SEPARATOR,
135-
COMMA_SEPARATOR,
136-
),
137-
];
125+
// if there are more than 4 args for remove-attr/class scriptlet,
126+
// ubo rule has multiple selector separated by comma. so we should:
127+
// 1. check if last arg is 'applying' parameter
128+
// 2. join 'selector' into one arg
129+
// 3. combine all args
130+
// https://github.com/AdguardTeam/Scriptlets/issues/133
131+
const lastArg = parsedArgs.pop();
132+
const [name, value, ...restArgs] = parsedArgs;
133+
let applying;
134+
// check the last parsed arg for matching possible 'applying' vale
135+
if (REMOVE_ATTR_CLASS_APPLYING.some((el) => lastArg.indexOf(el) > -1)) {
136+
applying = lastArg;
137+
} else {
138+
restArgs.push(lastArg);
139+
}
140+
const selector = restArgs.join(', ');
141+
parsedArgs = applying
142+
? [name, value, selector, applying]
143+
: [name, value, selector];
138144
}
139145

140146
const args = parsedArgs

tests/lib-tests/index.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ test('Test SCRIPTLET converting - UBO -> ADG', (assert) => {
106106
expBlockRule = 'foxracingshox.de#%#//scriptlet(\'ubo-rc.js\', \'cookie--not-set\', \'\', \'stay\')';
107107
assert.strictEqual(convertScriptletToAdg(blockingRule)[0], expBlockRule, 'empty selector and specified applying - OK');
108108

109+
// specified selectors and applying for remove-attr/class
110+
// eslint-disable-next-line no-useless-escape
111+
blockingRule = 'memo-book.pl##+js(rc, .rodo-locked, body\, html, stay)';
112+
expBlockRule = 'memo-book.pl#%#//scriptlet(\'ubo-rc.js\', \'.rodo-locked\', \'body, html\', \'stay\')';
113+
assert.strictEqual(convertScriptletToAdg(blockingRule)[0], expBlockRule, 'specified selectors and applying - OK');
114+
109115
// double quotes in scriptlet parameter
110116
blockingRule = 'example.com#@#+js(remove-attr.js, href, a[data-st-area="Header-back"])';
111117
expBlockRule = 'example.com#@%#//scriptlet(\'ubo-remove-attr.js\', \'href\', \'a[data-st-area="Header-back"]\')';

0 commit comments

Comments
 (0)