Skip to content

Commit 0b39298

Browse files
benjaminclotmike-chowla
authored andcommitted
Apply matchMedia to the top frame (#3612)
* Apply matchMedia to the top frame In case Prebid.js is called from within an iFrame, matchMedia should be applied to window.top, not the containing iFrame. This PR falls back to the legacy behavior in case of unfriendly iFrames. * Spec update
1 parent 16e5e16 commit 0b39298

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/sizeMapping.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { config } from './config';
2-
import {logWarn, isPlainObject, deepAccess, deepClone} from './utils';
2+
import {logWarn, isPlainObject, deepAccess, deepClone, getWindowTop} from './utils';
33
import includes from 'core-js/library/fn/array/includes';
44

55
let sizeConfig = [];
@@ -123,7 +123,17 @@ function evaluateSizeConfig(configs) {
123123
typeof config === 'object' &&
124124
typeof config.mediaQuery === 'string'
125125
) {
126-
if (matchMedia(config.mediaQuery).matches) {
126+
let ruleMatch = false;
127+
128+
try {
129+
ruleMatch = getWindowTop().matchMedia(config.mediaQuery).matches;
130+
} catch (e) {
131+
logWarn('Unfriendly iFrame blocks sizeConfig from being correctly evaluated');
132+
133+
ruleMatch = matchMedia(config.mediaQuery).matches;
134+
}
135+
136+
if (ruleMatch) {
127137
if (Array.isArray(config.sizesSupported)) {
128138
results.shouldFilter = true;
129139
}

test/spec/sizeMapping_spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ describe('sizeMapping', function () {
6060

6161
matchMediaOverride = {matches: false};
6262

63+
sandbox.stub(utils.getWindowTop(), 'matchMedia').callsFake((...args) => {
64+
if (typeof matchMediaOverride === 'function') {
65+
return matchMediaOverride.apply(utils.getWindowTop(), args);
66+
}
67+
return matchMediaOverride;
68+
});
69+
6370
sandbox.stub(window, 'matchMedia').callsFake((...args) => {
6471
if (typeof matchMediaOverride === 'function') {
6572
return matchMediaOverride.apply(window, args);

0 commit comments

Comments
 (0)