Skip to content

Commit

Permalink
Fix issue with updateTargetingFromMap() method in googletagservices-g…
Browse files Browse the repository at this point in the history
…pt redirect resource

Fix issue with sandbox attribute
  • Loading branch information
AdamWr committed May 17, 2023
1 parent 573a4e2 commit 10e7098
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- issue with `updateTargetingFromMap()` method
in `googletagservices-gpt` redirect [#293](https://github.com/AdguardTeam/Scriptlets/issues/293)
- website reloading if `$now$`/`$currentDate$` value is used
in `trusted-set-cookie-reload` scriptlet [#291](https://github.com/AdguardTeam/Scriptlets/issues/291)
- `getResponseHeader()` and `getAllResponseHeaders()` methods mock
Expand Down
11 changes: 6 additions & 5 deletions src/redirects/googletagservices-gpt.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export function GoogleTagServicesGpt(source) {
// https://github.com/AdguardTeam/Scriptlets/issues/259
f.setAttribute('data-load-complete', true);
f.setAttribute('data-google-container-id', true);
f.setAttribute('sandbox', true);
f.setAttribute('sandbox', '');
node.appendChild(f);
}
};
Expand Down Expand Up @@ -143,7 +143,7 @@ export function GoogleTagServicesGpt(source) {
return [v];
}
try {
return [Array.prototype.flat.call(v)[0]];
return Array.prototype.flat.call(v);
} catch {
// do nothing
}
Expand All @@ -152,9 +152,10 @@ export function GoogleTagServicesGpt(source) {

const updateTargeting = (targeting, map) => {
if (typeof map === 'object') {
const entries = Object.entries(map || {});
for (const [k, v] of entries) {
targeting.set(k, getTargetingValue(v));
for (const key in map) {
if (Object.hasOwn(map, key)) {
targeting.set(key, getTargetingValue(map[key]));
}
}
}
};
Expand Down
34 changes: 33 additions & 1 deletion tests/redirects/googletagservices-gpt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,39 @@ test('Test recreateIframeForSlot', (assert) => {
// https://github.com/AdguardTeam/Scriptlets/issues/259
assert.ok(iframe.getAttribute('data-load-complete'), 'attr was mocked');
assert.ok(iframe.getAttribute('data-google-container-id'), 'attr was mocked');
assert.ok(iframe.getAttribute('sandbox'), 'attr was mocked');
assert.strictEqual(iframe.getAttribute('sandbox'), '', 'attr was mocked');

assert.strictEqual(window.hit, 'FIRED', 'hit function was executed');
});

test('Test updateTargetingFromMap', (assert) => {
runRedirect(name);

assert.ok(window.googletag, 'window.googletag have been created');
assert.strictEqual(typeof window.googletag.defineSlot(), 'object', 'Slot has been mocked');

const slot = window.googletag.defineSlot('/1234567/sports', [160, 600], 'div');

// https://github.com/AdguardTeam/Scriptlets/issues/293
slot.updateTargetingFromMap({
color: 'red',
interests: ['sports', 'music', 'movies'],
});

assert.strictEqual(
slot.getTargeting('color')[0],
'red',
'.getTargeting() has been mocked - color[0] = red.',
);
assert.strictEqual(
slot.getTargeting('interests')[0],
'sports',
'.getTargeting() has been mocked - interests[0] = sports.',
);
assert.strictEqual(
slot.getTargeting('interests')[1],
'music',
'.getTargeting() has been mocked - interests[1] = music.',
);
assert.strictEqual(window.hit, 'FIRED', 'hit function was executed');
});

0 comments on commit 10e7098

Please sign in to comment.