Skip to content

Commit

Permalink
Improve set-constant adg->ubo conversion
Browse files Browse the repository at this point in the history
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
  • Loading branch information
slavaleleka committed Dec 16, 2022
2 parents a9d916c + 993c764 commit 906c315
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
# Scriptlets and Redirect Resources Changelog


## v1.7.14

### Added

* `set-constant` ADG→UBO conversion for [`emptyArr` and `emptyObj`](https://github.com/uBlockOrigin/uBlock-issues/issues/2411)


## v1.7.13

### Fixed
### Fixed

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


## v1.7.10

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


## v1.7.3

### Added
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# AdGuard Scriptlets and Redirect resources
# AdGuard Scriptlets and Redirect Resources

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

Expand Down
13 changes: 12 additions & 1 deletion src/helpers/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ const ADG_XHR_TYPE = 'xmlhttprequest';

const ADG_SET_CONSTANT_NAME = 'set-constant';
const ADG_SET_CONSTANT_EMPTY_STRING = '';
const ADG_SET_CONSTANT_EMPTY_ARRAY = 'emptyArr';
const ADG_SET_CONSTANT_EMPTY_OBJECT = 'emptyObj';
const UBO_SET_CONSTANT_EMPTY_STRING = '\'\'';
const UBO_SET_CONSTANT_EMPTY_ARRAY = '[]';
const UBO_SET_CONSTANT_EMPTY_OBJECT = '{}';

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

let preparedParams;
// https://github.com/AdguardTeam/FiltersCompiler/issues/102
if (parsedName === ADG_SET_CONSTANT_NAME
// https://github.com/AdguardTeam/FiltersCompiler/issues/102
&& parsedParams[1] === ADG_SET_CONSTANT_EMPTY_STRING) {
preparedParams = [parsedParams[0], UBO_SET_CONSTANT_EMPTY_STRING];
} else if (parsedName === ADG_SET_CONSTANT_NAME
// https://github.com/uBlockOrigin/uBlock-issues/issues/2411
&& parsedParams[1] === ADG_SET_CONSTANT_EMPTY_ARRAY) {
preparedParams = [parsedParams[0], UBO_SET_CONSTANT_EMPTY_ARRAY];
} else if (parsedName === ADG_SET_CONSTANT_NAME
&& parsedParams[1] === ADG_SET_CONSTANT_EMPTY_OBJECT) {
preparedParams = [parsedParams[0], UBO_SET_CONSTANT_EMPTY_OBJECT];
} else if (parsedName === ADG_PREVENT_FETCH_NAME
// https://github.com/AdguardTeam/Scriptlets/issues/109
&& (parsedParams[0] === ADG_PREVENT_FETCH_WILDCARD
Expand Down
10 changes: 10 additions & 0 deletions tests/lib-tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,16 @@ test('Test SCRIPTLET converting - ADG -> UBO', (assert) => {
inputAdg = 'example.com#%#//scriptlet(\'close-window\')';
expectedUbo = 'example.com##+js(window-close-if)';
assert.strictEqual(convertAdgScriptletToUbo(inputAdg), expectedUbo);

// emptyArr as set-constant parameter
inputAdg = "example.org#%#//scriptlet('set-constant', 'adUnits', 'emptyArr')";
expectedUbo = 'example.org##+js(set-constant, adUnits, [])';
assert.strictEqual(convertAdgScriptletToUbo(inputAdg), expectedUbo);

// emptyObj as set-constant parameter
inputAdg = "example.org#%#//scriptlet('set-constant', 'adUnits', 'emptyObj')";
expectedUbo = 'example.org##+js(set-constant, adUnits, {})';
assert.strictEqual(convertAdgScriptletToUbo(inputAdg), expectedUbo);
});

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

0 comments on commit 906c315

Please sign in to comment.