Skip to content

Commit 906c315

Browse files
committed
Improve set-constant adg->ubo conversion
fix by @gorhill uBlockOrigin/uBlock-issues#2411 * commit '993c7648279c7c6c7eb7ee8f3ef92724fe584651': add related issue link to changelog update changelog add few simple tests fix converter comments fix readme Fix converter for uBO's `[]`/`{}` in `set-constant` scriptlet
2 parents a9d916c + 993c764 commit 906c315

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

CHANGELOG.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
# Scriptlets and Redirect Resources Changelog
22

3+
4+
## v1.7.14
5+
6+
### Added
7+
8+
* `set-constant` ADG→UBO conversion for [`emptyArr` and `emptyObj`](https://github.com/uBlockOrigin/uBlock-issues/issues/2411)
9+
10+
311
## v1.7.13
412

5-
### Fixed
13+
### Fixed
614

715
* `isEmptyObject` helper not counting `prototype` as an object property
816

17+
918
## v1.7.10
1019

1120
### Added
@@ -19,6 +28,7 @@
1928
* spread of args bug at `getXhrData` call for `trusted-replace-xhr-response`
2029
* request properties array not being served to `getRequestData` and `parseMatchProps` helpers
2130

31+
2232
## v1.7.3
2333

2434
### Added

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# AdGuard Scriptlets and Redirect resources
1+
# AdGuard Scriptlets and Redirect Resources
22

33
AdGuard's Scriptlets and Redirect resources library which provides extended capabilities for content blocking.
44

src/helpers/converter.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ const ADG_XHR_TYPE = 'xmlhttprequest';
4242

4343
const ADG_SET_CONSTANT_NAME = 'set-constant';
4444
const ADG_SET_CONSTANT_EMPTY_STRING = '';
45+
const ADG_SET_CONSTANT_EMPTY_ARRAY = 'emptyArr';
46+
const ADG_SET_CONSTANT_EMPTY_OBJECT = 'emptyObj';
4547
const UBO_SET_CONSTANT_EMPTY_STRING = '\'\'';
48+
const UBO_SET_CONSTANT_EMPTY_ARRAY = '[]';
49+
const UBO_SET_CONSTANT_EMPTY_OBJECT = '{}';
4650

4751
const ADG_PREVENT_FETCH_NAME = 'prevent-fetch';
4852
const ADG_PREVENT_FETCH_EMPTY_STRING = '';
@@ -246,10 +250,17 @@ export const convertAdgScriptletToUbo = (rule) => {
246250
const { name: parsedName, args: parsedParams } = parseRule(rule);
247251

248252
let preparedParams;
249-
// https://github.com/AdguardTeam/FiltersCompiler/issues/102
250253
if (parsedName === ADG_SET_CONSTANT_NAME
254+
// https://github.com/AdguardTeam/FiltersCompiler/issues/102
251255
&& parsedParams[1] === ADG_SET_CONSTANT_EMPTY_STRING) {
252256
preparedParams = [parsedParams[0], UBO_SET_CONSTANT_EMPTY_STRING];
257+
} else if (parsedName === ADG_SET_CONSTANT_NAME
258+
// https://github.com/uBlockOrigin/uBlock-issues/issues/2411
259+
&& parsedParams[1] === ADG_SET_CONSTANT_EMPTY_ARRAY) {
260+
preparedParams = [parsedParams[0], UBO_SET_CONSTANT_EMPTY_ARRAY];
261+
} else if (parsedName === ADG_SET_CONSTANT_NAME
262+
&& parsedParams[1] === ADG_SET_CONSTANT_EMPTY_OBJECT) {
263+
preparedParams = [parsedParams[0], UBO_SET_CONSTANT_EMPTY_OBJECT];
253264
} else if (parsedName === ADG_PREVENT_FETCH_NAME
254265
// https://github.com/AdguardTeam/Scriptlets/issues/109
255266
&& (parsedParams[0] === ADG_PREVENT_FETCH_WILDCARD

tests/lib-tests/index.test.js

+10
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,16 @@ test('Test SCRIPTLET converting - ADG -> UBO', (assert) => {
246246
inputAdg = 'example.com#%#//scriptlet(\'close-window\')';
247247
expectedUbo = 'example.com##+js(window-close-if)';
248248
assert.strictEqual(convertAdgScriptletToUbo(inputAdg), expectedUbo);
249+
250+
// emptyArr as set-constant parameter
251+
inputAdg = "example.org#%#//scriptlet('set-constant', 'adUnits', 'emptyArr')";
252+
expectedUbo = 'example.org##+js(set-constant, adUnits, [])';
253+
assert.strictEqual(convertAdgScriptletToUbo(inputAdg), expectedUbo);
254+
255+
// emptyObj as set-constant parameter
256+
inputAdg = "example.org#%#//scriptlet('set-constant', 'adUnits', 'emptyObj')";
257+
expectedUbo = 'example.org##+js(set-constant, adUnits, {})';
258+
assert.strictEqual(convertAdgScriptletToUbo(inputAdg), expectedUbo);
249259
});
250260

251261
test('Test $redirect validation', (assert) => {

0 commit comments

Comments
 (0)