Skip to content

Commit

Permalink
handle set-constant error if some chain prop is null while loading. A…
Browse files Browse the repository at this point in the history
…G-8269 #128

Merge in ADGUARD-FILTERS/scriptlets from fix/AG-8269 to master

Squashed commit of the following:

commit a96f804
Author: Slava Leleka <v.leleka@adguard.com>
Date:   Fri May 28 14:42:25 2021 +0300

    rebuild dist

commit e182461
Author: Slava Leleka <v.leleka@adguard.com>
Date:   Fri May 28 14:40:56 2021 +0300

    return base if it's null without let nextBase

commit 40aa593
Author: Slava Leleka <v.leleka@adguard.com>
Date:   Thu May 27 19:51:05 2021 +0300

    build dist

commit 5e42baa
Author: Slava Leleka <v.leleka@adguard.com>
Date:   Thu May 27 19:49:34 2021 +0300

    handle set-constant error if some chain prop is null while loading
  • Loading branch information
slavaleleka committed May 28, 2021
1 parent ef53cec commit 72e8e2b
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 11 deletions.
13 changes: 12 additions & 1 deletion dist/cjs/scriptlets.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,18 @@ function getPropertyInChain(base, chain) {
};
}

var prop = chain.slice(0, pos);
var prop = chain.slice(0, pos); // https://github.com/AdguardTeam/Scriptlets/issues/128

if (base === null) {
// if base is null, return 'null' as base.
// it's needed for triggering the reason logging while debugging
return {
base: base,
prop: prop,
chain: chain
};
}

var nextBase = base[prop];
chain = chain.slice(pos + 1);

Expand Down
14 changes: 7 additions & 7 deletions dist/scriptlets.corelibs.json

Large diffs are not rendered by default.

13 changes: 12 additions & 1 deletion dist/scriptlets.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,18 @@
};
}

var prop = chain.slice(0, pos);
var prop = chain.slice(0, pos); // https://github.com/AdguardTeam/Scriptlets/issues/128

if (base === null) {
// if base is null, return 'null' as base.
// it's needed for triggering the reason logging while debugging
return {
base: base,
prop: prop,
chain: chain
};
}

var nextBase = base[prop];
chain = chain.slice(pos + 1);

Expand Down
8 changes: 8 additions & 0 deletions src/helpers/get-property-in-chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ export function getPropertyInChain(base, chain) {
return { base, prop: chain };
}
const prop = chain.slice(0, pos);

// https://github.com/AdguardTeam/Scriptlets/issues/128
if (base === null) {
// if base is null, return 'null' as base.
// it's needed for triggering the reason logging while debugging
return { base, prop, chain };
}

const nextBase = base[prop];
chain = chain.slice(pos + 1);
if (nextBase !== undefined) {
Expand Down
4 changes: 3 additions & 1 deletion tests/scriptlets/prevent-window-open.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/* eslint-disable no-eval, no-underscore-dangle */
/* eslint-disable no-eval, no-underscore-dangle, no-console */
import { clearGlobalProps } from '../helpers';

const { test, module } = QUnit;
const name = 'prevent-window-open';

const nativeOpen = window.open;
const nativeSetTimeout = window.setTimeout;
const nativeConsole = console.log;

/**
* Runs sctiptlet with given props
Expand All @@ -31,6 +32,7 @@ const beforeEach = () => {

const afterEach = () => {
window.open = nativeOpen;
console.log = nativeConsole;
clearGlobalProps('hit', '__debug');
};

Expand Down
51 changes: 50 additions & 1 deletion tests/scriptlets/set-constant.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
/* eslint-disable no-eval, no-underscore-dangle */
/* eslint-disable no-eval, no-underscore-dangle, no-console */
import { clearGlobalProps } from '../helpers';

const { test, module } = QUnit;
const name = 'set-constant';

const nativeConsole = console.log;

const afterEach = () => {
console.log = nativeConsole;
clearGlobalProps('hit', '__debug', 'counter');
};

Expand Down Expand Up @@ -223,3 +226,49 @@ test('sets values correctly + no stack match', (assert) => {
assert.strictEqual(window.counter, undefined);
clearGlobalProps(property);
});

test('no value setting if chain is not relevant', (assert) => {
window.chain = { property: {} };
runScriptlet(['noprop.property.aaa', 'true']);
assert.deepEqual(window.chain.property, {}, 'predefined obj was not changed');
assert.strictEqual(window.noprop, undefined, '"noprop" was not set');
clearGlobalProps('chain');
});

test('no value setting if some property in chain is undefined while loading', (assert) => {
const testObj = { prop: undefined };
window.chain = testObj;
runScriptlet(['chain.prop.aaa', 'true']);
assert.deepEqual(window.chain, testObj, 'predefined obj was not changed');
clearGlobalProps('chain');
});

test('no value setting if first property in chain is null', (assert) => {
window.chain = null;
runScriptlet(['chain.property.aaa', 'true']);
assert.strictEqual(window.chain, null, 'predefined obj was not changed');
clearGlobalProps('chain');
});

// for now the scriptlet does not set the chained property if one of chain prop is null.
// that might happen, for example, while loading the page.
// after the needed property is loaded, the scriptlet does not check it and do not set the value
// https://github.com/AdguardTeam/Scriptlets/issues/128
test('set value after timeout if it was null earlier', (assert) => {
window.chain = null;
runScriptlet(['chain.property.aaa', 'true']);
assert.strictEqual(window.chain, null, 'predefined obj was not changed');

const done = assert.async();

setTimeout(() => {
window.chain = { property: {} };
}, 50);

setTimeout(() => {
assert.strictEqual(window.chain.property.aaa, undefined, 'chained prop was NOT set after delay');
done();
}, 100);

clearGlobalProps('chain');
});

0 comments on commit 72e8e2b

Please sign in to comment.