Skip to content

Commit

Permalink
creative comment injection spot reverted:
Browse files Browse the repository at this point in the history
- reverted injection point of creative comment to pre-PR 6860
- added code to reinject comment in case it was removed upon rendering

(cherry picked from commit 7c21d26)
  • Loading branch information
denys-berzoy-confiant committed Jan 13, 2022
1 parent 0d7faeb commit d58fd1f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
22 changes: 18 additions & 4 deletions src/prebid.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,10 +403,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 @@ -450,10 +463,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 @@ -472,7 +486,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 @@ -485,7 +499,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

0 comments on commit d58fd1f

Please sign in to comment.