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

Prebid Core: creative comment injection spot reverted #7874

Merged
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
22 changes: 18 additions & 4 deletions src/prebid.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,10 +402,23 @@ function emitAdRenderSucceeded({ doc, bid, id }) {
events.emit(AD_RENDER_SUCCEEDED, data);
}

/**
* This function will check for presence of given node in given parent. If not present - will inject it.
* @param {Node} node node, whose existance is in question
* @param {Document} doc document element do look in
* @param {string} tagName tag name to look in
*/
function reinjectNodeIfRemoved(node, doc, tagName) {
const injectionNode = doc.querySelector(tagName);
if (!node.parentNode || node.parentNode !== injectionNode) {
insertElement(node, doc, tagName);
}
}

/**
* This function will render the ad (based on params) in the given iframe document passed through.
* Note that doc SHOULD NOT be the parent document page as we can't doc.write() asynchronously
* @param {HTMLDocument} doc document
* @param {Document} doc document
* @param {string} id bid id to locate the ad
* @alias module:pbjs.renderAd
*/
Expand Down Expand Up @@ -449,10 +462,11 @@ $$PREBID_GLOBAL$$.renderAd = hook('async', function (doc, id, options) {
const {height, width, ad, mediaType, adUrl, renderer} = bid;

const creativeComment = document.createComment(`Creative ${bid.creativeId} served by ${bid.bidder} Prebid.js Header Bidding`);
insertElement(creativeComment, doc, 'html');

if (isRendererRequired(renderer)) {
executeRenderer(renderer, bid);
insertElement(creativeComment, doc, 'html');
reinjectNodeIfRemoved(creativeComment, doc, 'html');
emitAdRenderSucceeded({ doc, bid, id });
} else if ((doc === document && !inIframe()) || mediaType === 'video') {
const message = `Error trying to write ad. Ad render call ad id ${id} was prevented from writing to the main document.`;
Expand All @@ -471,7 +485,7 @@ $$PREBID_GLOBAL$$.renderAd = hook('async', function (doc, id, options) {
doc.write(ad);
doc.close();
setRenderSize(doc, width, height);
insertElement(creativeComment, doc, 'html');
reinjectNodeIfRemoved(creativeComment, doc, 'html');
callBurl(bid);
emitAdRenderSucceeded({ doc, bid, id });
} else if (adUrl) {
Expand All @@ -484,7 +498,7 @@ $$PREBID_GLOBAL$$.renderAd = hook('async', function (doc, id, options) {

insertElement(iframe, doc, 'body');
setRenderSize(doc, width, height);
insertElement(creativeComment, doc, 'html');
reinjectNodeIfRemoved(creativeComment, doc, 'html');
callBurl(bid);
emitAdRenderSucceeded({ doc, bid, id });
} else {
Expand Down
4 changes: 3 additions & 1 deletion test/spec/unit/pbjs_api_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1135,13 +1135,15 @@ describe('Unit: Prebid Module', function () {
height: 0
}
},
getElementsByTagName: sinon.stub()
getElementsByTagName: sinon.stub(),
querySelector: sinon.stub()
};

elStub = {
insertBefore: sinon.stub()
};
doc.getElementsByTagName.returns([elStub]);
doc.querySelector.returns(elStub);

spyLogError = sinon.spy(utils, 'logError');
spyLogMessage = sinon.spy(utils, 'logMessage');
Expand Down