diff --git a/src/prebid.js b/src/prebid.js index d22dd462373..792a8118208 100644 --- a/src/prebid.js +++ b/src/prebid.js @@ -1,7 +1,7 @@ /** @module $$PREBID_GLOBAL$$ */ import { getGlobal } from './prebidGlobal'; -import {flatten, uniques, isGptPubadsDefined, adUnitsFilter} from './utils'; +import {flatten, uniques, isGptPubadsDefined, adUnitsFilter, isSrcdocSupported} from './utils'; import { videoAdUnit, hasNonVideoBidder } from './video'; import 'polyfill'; import {parse as parseURL, format as formatURL} from './url'; @@ -72,7 +72,7 @@ $$PREBID_GLOBAL$$.adUnits = $$PREBID_GLOBAL$$.adUnits || []; /** * Command queue that functions will execute once prebid.js is loaded - * @param {function} cmd Annoymous function to execute + * @param {function} cmd Anonymous function to execute * @alias module:$$PREBID_GLOBAL$$.que.push */ $$PREBID_GLOBAL$$.que.push = function (cmd) { @@ -277,8 +277,9 @@ $$PREBID_GLOBAL$$.allBidsAvailable = function () { }; /** - * 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() asynchrounsly - * @param {object} doc document + * 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 {string} id bid id to locate the ad * @alias module:$$PREBID_GLOBAL$$.renderAd */ @@ -299,15 +300,19 @@ $$PREBID_GLOBAL$$.renderAd = function (doc, id) { var width = adObject.width; var url = adObject.adUrl; var ad = adObject.ad; - - if (doc===document || adObject.mediaType === 'video') { - utils.logError('Error trying to write ad. Ad render call ad id ' + id + ' was prevented from writing to the main document.'); + + if (doc === document || adObject.mediaType === 'video') { + utils.logError(`Error trying to write ad. Ad render call ad id ${id} was prevented from writing to the main document.`); } else if (ad) { - doc.write(ad); - doc.close(); + if (isSrcdocSupported(doc)) { + doc.defaultView.frameElement.srcdoc = ad; + } else { + doc.write(ad); + doc.close(); + } setRenderSize(doc, width, height); } else if (url) { - doc.write(''); + doc.write(``); doc.close(); setRenderSize(doc, width, height); } else { @@ -483,7 +488,7 @@ $$PREBID_GLOBAL$$.offEvent = function (event, handler, id) { /** * Add a callback event * @param {String} eventStr event to attach callback to Options: "allRequestedBidsBack" | "adUnitBidsBack" - * @param {Function} func function to execute. Paramaters passed into the function: (bidResObj), [adUnitCode]); + * @param {Function} func function to execute. Parameters passed into the function: (bidResObj), [adUnitCode]); * @alias module:$$PREBID_GLOBAL$$.addCallback * @returns {String} id for callback */ @@ -586,7 +591,7 @@ $$PREBID_GLOBAL$$.loadScript = function (tagSrc, callback, useCache) { }; /** - * Will enable sendinga prebid.js to data provider specified + * Will enable sending a prebid.js to data provider specified * @param {object} config object {provider : 'string', options : {}} */ $$PREBID_GLOBAL$$.enableAnalytics = function (config) { @@ -679,7 +684,7 @@ $$PREBID_GLOBAL$$.buildMasterVideoTagFromAdserverTag = function (adserverTag, op /** * Set the order bidders are called in. If not set, the bidders are called in - * the order they are defined wihin the adUnit.bids array + * the order they are defined within the adUnit.bids array * @param {string} order - Order to call bidders in. Currently the only possible value * is 'random', which randomly shuffles the order */ diff --git a/src/url.js b/src/url.js index 3588d44fad5..9bd555d6f4e 100644 --- a/src/url.js +++ b/src/url.js @@ -31,7 +31,7 @@ export function parse(url) { protocol: (parsed.protocol || '').replace(/:$/, ''), hostname: parsed.hostname, port: +parsed.port, - pathname: parsed.pathname, + pathname: parsed.pathname.replace(/^(?!\/)/,'/'), search: parseQS(parsed.search || ''), hash: (parsed.hash || '').replace(/^#/, ''), host: parsed.host diff --git a/src/utils.js b/src/utils.js index 5cceb65d391..71ebd1cb936 100644 --- a/src/utils.js +++ b/src/utils.js @@ -592,3 +592,12 @@ export function shuffle(array) { export function adUnitsFilter(filter, bid) { return filter.includes(bid && bid.placementCode || bid && bid.adUnitCode); } + +/** + * Check if parent iframe of passed document supports content rendering via 'srcdoc' property + * @param {HTMLDocument} doc document to check support of 'srcdoc' + */ +export function isSrcdocSupported(doc) { + //Firefox is excluded due to https://bugzilla.mozilla.org/show_bug.cgi?id=1265961 + return !!doc.defaultView && 'srcdoc' in doc.defaultView.frameElement && !/firefox/i.test(navigator.userAgent); +} diff --git a/test/spec/url_spec.js b/test/spec/url_spec.js index cc4c1178a18..55b7de5b04f 100644 --- a/test/spec/url_spec.js +++ b/test/spec/url_spec.js @@ -24,7 +24,7 @@ describe('helpers.url', () => { }); it('extracts the pathname', () => { - expect(['/pathname/', 'pathname/']).to.include(parsed.pathname); + expect(parsed).to.have.property('pathname', '/pathname/'); }); it('extracts the search query', () => {