Skip to content

Commit

Permalink
Core: allow renderAd on main document for bids with a renderer (#12391)
Browse files Browse the repository at this point in the history
  • Loading branch information
dgirardi authored Oct 31, 2024
1 parent 16642a5 commit 190f540
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
19 changes: 8 additions & 11 deletions src/adRendering.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,12 @@ export const getRenderingData = hook('sync', function (bidResponse, options) {
};
})

export const doRender = hook('sync', function({renderFn, resizeFn, bidResponse, options}) {
if (FEATURES.VIDEO && bidResponse.mediaType === VIDEO) {
export const doRender = hook('sync', function({renderFn, resizeFn, bidResponse, options, doc, isMainDocument = doc === document && !inIframe()}) {
const videoBid = (FEATURES.VIDEO && bidResponse.mediaType === VIDEO)
if (isMainDocument || videoBid) {
emitAdRenderFail({
reason: AD_RENDER_FAILED_REASON.PREVENT_WRITING_ON_MAIN_DOCUMENT,
message: 'Cannot render video ad',
message: videoBid ? 'Cannot render video ad without a renderer' : `renderAd was prevented from writing to the main document.`,
bid: bidResponse,
id: bidResponse.adId
});
Expand Down Expand Up @@ -277,14 +278,10 @@ export function renderAdDirect(doc, adId, options) {
if (!adId || !doc) {
fail(AD_RENDER_FAILED_REASON.MISSING_DOC_OR_ADID, `missing ${adId ? 'doc' : 'adId'}`);
} else {
if ((doc === document && !inIframe())) {
fail(AD_RENDER_FAILED_REASON.PREVENT_WRITING_ON_MAIN_DOCUMENT, `renderAd was prevented from writing to the main document.`);
} else {
getBidToRender(adId).then(bidResponse => {
bid = bidResponse;
handleRender({renderFn, resizeFn, adId, options: {clickUrl: options?.clickThrough}, bidResponse, doc});
});
}
getBidToRender(adId).then(bidResponse => {
bid = bidResponse;
handleRender({renderFn, resizeFn, adId, options: {clickUrl: options?.clickThrough}, bidResponse, doc});
});
}
} catch (e) {
fail(EXCEPTION, e.message);
Expand Down
11 changes: 11 additions & 0 deletions test/spec/unit/adRendering_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ describe('adRendering', () => {
sinon.assert.called(bidResponse.renderer.render);
});

it('allows rendering on the main document', () => {
doRender({renderFn, bidResponse, isMainDocument: true});
sinon.assert.notCalled(renderFn);
sinon.assert.called(bidResponse.renderer.render);
})

it('emits AD_RENDER_SUCCEDED', () => {
doRender({renderFn, bidResponse});
sinon.assert.calledWith(events.emit, EVENTS.AD_RENDER_SUCCEEDED, sinon.match({
Expand All @@ -144,6 +150,11 @@ describe('adRendering', () => {
});
}

it('should emit AD_RENDER_FAILED when renderer-less bid is being rendered on the main document', () => {
doRender({renderFn, bidResponse, isMainDocument: true});
expectAdRenderFailedEvent(AD_RENDER_FAILED_REASON.PREVENT_WRITING_ON_MAIN_DOCUMENT);
});

it('invokes renderFn with rendering data', () => {
const data = {ad: 'creative'};
getRenderingDataStub.returns(data);
Expand Down

0 comments on commit 190f540

Please sign in to comment.