Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core: fix bug where renderAd sometimes generates invisible iframes #10737

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions src/prebid.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import {getGlobal} from './prebidGlobal.js';
import {
callBurl,
createInvisibleIframe,
createIframe,
deepAccess,
deepClone,
deepSetValue,
Expand Down Expand Up @@ -558,13 +558,7 @@ pbjsInstance.renderAd = hook('async', function (doc, id, options) {
callBurl(bid);
emitAdRenderSucceeded({ doc, bid, id });
} else if (adUrl) {
const iframe = createInvisibleIframe();
iframe.height = height;
iframe.width = width;
iframe.style.display = 'inline';
iframe.style.overflow = 'hidden';
iframe.src = adUrl;

const iframe = createIframe({width, height, src: adUrl}, {display: 'inline', overflow: 'hidden'});
insertElement(iframe, doc, 'body');
setRenderSize(doc, width, height);
reinjectNodeIfRemoved(creativeComment, doc, 'html');
Expand Down
41 changes: 23 additions & 18 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,27 +252,32 @@ export function debugTurnedOn() {
return !!config.getConfig('debug');
}

export function createInvisibleIframe() {
var f = document.createElement('iframe');
f.id = getUniqueIdentifierStr();
f.height = 0;
f.width = 0;
f.border = '0px';
f.hspace = '0';
f.vspace = '0';
f.marginWidth = '0';
f.marginHeight = '0';
f.style.border = '0';
f.scrolling = 'no';
f.frameBorder = '0';
f.src = 'about:blank';
f.style.display = 'none';
f.style.height = '0px';
f.style.width = '0px';
f.allowtransparency = 'true';
export function createIframe(attrs, style) {
attrs = Object.assign({
id: getUniqueIdentifierStr(),
hspace: '0',
vspace: '0',
marginWidth: '0',
marginHeight: '0',
scrolling: 'no',
frameBorder: '0',
src: 'about:blank',
allowtransparency: 'true'
}, attrs);
style = Object.assign({
border: '0',
}, style);
['width', 'height'].filter(k => !style.hasOwnProperty(k) && attrs.hasOwnProperty(k)).forEach(k => style[k] = attrs[k] + 'px');
const f = document.createElement('iframe');
Object.assign(f, attrs);
Object.assign(f.style, style);
return f;
}

export function createInvisibleIframe() {
return createIframe({width: 0, height: 0}, {width: '0px', height: '0px', display: 'none'});
}

/*
* Check if a given parameter name exists in query string
* and if it does return the value
Expand Down
10 changes: 8 additions & 2 deletions test/spec/unit/pbjs_api_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1286,10 +1286,16 @@ describe('Unit: Prebid Module', function () {

it('should place the url inside an iframe on the doc', function () {
pushBidResponseToAuction({
adUrl: 'http://server.example.com/ad/ad.js'
adUrl: 'http://server.example.com/ad/ad.js',
width: 300,
height: 200,
});
$$PREBID_GLOBAL$$.renderAd(doc, bidId);
assert.ok(elStub.insertBefore.called, 'url was written to iframe in doc');
sinon.assert.calledWith(elStub.insertBefore, sinon.match(iframe =>
iframe.width === '300' && iframe.height === '200' &&
iframe.style.width === '300px' && iframe.style.height === '200px' &&
~iframe.src.indexOf('http://server.example.com/ad/ad.js')
));
});

it('should log an error when no ad or url', function () {
Expand Down
18 changes: 18 additions & 0 deletions test/spec/utils_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,24 @@ describe('Utils', function () {
});
});

describe('createIframe', () => {
it('sets attributes on frame', () => {
const f = utils.createIframe({src: 'https://some-url/'});
expect(f.src).to.eql('https://some-url/');
});
it('sets style', () => {
const f = utils.createIframe(null, {display: 'inline'});
expect(f.style.display).to.eql('inline');
});
it('sets w/h on both', () => {
const f = utils.createIframe({width: 1, height: 2});
expect(f.width).to.eql('1');
expect(f.height).to.eql('2');
expect(f.style.width).to.eql('1px');
expect(f.style.height).to.eql('2px');
});
});

describe('createInvisibleIframe', function () {
var output = utils.createInvisibleIframe();

Expand Down