Skip to content

Commit

Permalink
Prebid Core : add setScriptAttributes to adloader in Utils (prebid#8624)
Browse files Browse the repository at this point in the history
* Allow SpotX module to load script without appendChild

* Fix SpotX spec to no longer use appendChild

* Restore spotx files

* Formatting fixes

* Unit test for setScriptAttributes

* Unit test and formatting fix

* Formatting fix

* Comment that attributes are added once only
  • Loading branch information
spotxslagle authored and JacobKlein26 committed Feb 8, 2023
1 parent e199e91 commit 2c769e9
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/adloader.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {includes} from './polyfill.js';
import { logError, logWarn, insertElement } from './utils.js';
import { logError, logWarn, insertElement, setScriptAttributes } from './utils.js';

const _requestCache = new WeakMap();
// The below list contains modules or vendors whom Prebid allows to load external JS.
Expand All @@ -9,6 +9,7 @@ const _approvedLoadExternalJSList = [
'criteo',
'outstream',
'adagio',
'spotx',
'browsi',
'brandmetrics',
'justtag',
Expand All @@ -27,8 +28,9 @@ const _approvedLoadExternalJSList = [
* @param {string} moduleCode bidderCode or module code of the module requesting this resource
* @param {function} [callback] callback function to be called after the script is loaded
* @param {Document} [doc] the context document, in which the script will be loaded, defaults to loaded document
* @param {object} an object of attributes to be added to the script with setAttribute by [key] and [value]; Only the attributes passed in the first request of a url will be added.
*/
export function loadExternalScript(url, moduleCode, callback, doc) {
export function loadExternalScript(url, moduleCode, callback, doc, attributes) {
if (!moduleCode || !url) {
logError('cannot load external script without url and moduleCode');
return;
Expand Down Expand Up @@ -77,9 +79,9 @@ export function loadExternalScript(url, moduleCode, callback, doc) {
} catch (e) {
logError('Error executing callback', 'adloader.js:loadExternalScript', e);
}
}, doc);
}, doc, attributes);

function requestResource(tagSrc, callback, doc) {
function requestResource(tagSrc, callback, doc, attributes) {
if (!doc) {
doc = document;
}
Expand Down Expand Up @@ -107,6 +109,10 @@ export function loadExternalScript(url, moduleCode, callback, doc) {

jptScript.src = tagSrc;

if (attributes) {
setScriptAttributes(jptScript, attributes);
}

// add the new script tag to the page
insertElement(jptScript, doc);

Expand Down
13 changes: 13 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1368,3 +1368,16 @@ export function safeJSONParse(data) {
return JSON.parse(data);
} catch (e) {}
}

/**
* Sets dataset attributes on a script
* @param {Script} script
* @param {object} attributes
*/
export function setScriptAttributes(script, attributes) {
for (let key in attributes) {
if (attributes.hasOwnProperty(key)) {
script.setAttribute(key, attributes[key]);
}
}
}
23 changes: 23 additions & 0 deletions test/spec/adloader_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,27 @@ describe('adLoader', function () {
expect(utilsinsertElementStub.callCount).to.equal(2);
});
});

it('attaches passed attributes to a script', function () {
const doc = {
createElement: function () {
return {
setAttribute: function (key, value) {
this[key] = value;
}
}
},
getElementsByTagName: function() {
return {
firstChild: {
insertBefore: function() {}
}
}
}
},
attrs = {'z': 'A', 'y': 2};
let script = adLoader.loadExternalScript('someUrl', 'criteo', undefined, doc, attrs);
expect(script.z).to.equal('A');
expect(script.y).to.equal(2);
});
});
16 changes: 16 additions & 0 deletions test/spec/utils_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1243,4 +1243,20 @@ describe('Utils', function () {
});
});
});

describe('setScriptAttributes', () => {
it('correctly adds attributes from an object', () => {
const script = document.createElement('script'),
attrs = {
'data-first_prop': '1',
'data-second_prop': 'b',
'id': 'newId'
};
script.id = 'oldId';
utils.setScriptAttributes(script, attrs);
expect(script.dataset['first_prop']).to.equal('1');
expect(script.dataset.second_prop).to.equal('b');
expect(script.id).to.equal('newId');
});
});
});

0 comments on commit 2c769e9

Please sign in to comment.