Skip to content

Commit

Permalink
Insert iframe as the last child of Html tag (prebid#3415)
Browse files Browse the repository at this point in the history
* 3413-Insert Iframes for user sync in body

* Append Iframe as the last child

* Add unit test for insertElement

* utls insert element at the top by default

* add unit tests for utls insert element

* Insert iframe as html last child

* revert package.lock changes
  • Loading branch information
deekshithraop authored and Pedro López Jiménez committed Mar 18, 2019
1 parent 18d694e commit a1091e3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
29 changes: 18 additions & 11 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -549,21 +549,28 @@ var hasOwn = function (objectToCheck, propertyToCheckFor) {
}
};

exports.insertElement = function(elm, doc, target) {
/*
* Inserts an element(elm) as targets child, by default as first child
* @param {HTMLElement} elm
* @param {HTMLElement} [doc]
* @param {HTMLElement} [target]
* @param {Boolean} [asLastChildChild]
* @return {HTMLElement}
*/
exports.insertElement = function(elm, doc, target, asLastChildChild) {
doc = doc || document;
let elToAppend;
const head = doc.getElementsByTagName('head');
let parentEl;
if (target) {
elToAppend = doc.getElementsByTagName(target);
parentEl = doc.getElementsByTagName(target);
} else {
elToAppend = head;
parentEl = doc.getElementsByTagName('head');
}
try {
elToAppend = elToAppend.length ? elToAppend : doc.getElementsByTagName('body');
if (elToAppend.length) {
elToAppend = elToAppend[0];
const refChild = head && head[0] === elToAppend ? null : elToAppend.firstChild;
return elToAppend.insertBefore(elm, refChild);
parentEl = parentEl.length ? parentEl : doc.getElementsByTagName('body');
if (parentEl.length) {
parentEl = parentEl[0];
let insertBeforeEl = asLastChildChild ? null : parentEl.firstChild;
return parentEl.insertBefore(elm, insertBeforeEl);
}
} catch (e) {}
};
Expand Down Expand Up @@ -635,7 +642,7 @@ exports.insertUserSyncIframe = function(url, done) {
iframe.addEventListener('load', done);
iframe.addEventListener('error', done);
}
exports.insertElement(iframe);
exports.insertElement(iframe, document, 'html', true);
};

/**
Expand Down
18 changes: 15 additions & 3 deletions test/spec/utils_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -916,11 +916,23 @@ describe('Utils', function () {
});

describe('insertElement', function () {
it('returns a node at bottom of head if no target is given', function () {
it('returns a node at the top of the target by default', function () {
const toInsert = document.createElement('div');
const head = document.getElementsByTagName('head')[0];
const target = document.getElementsByTagName('body')[0];
const inserted = utils.insertElement(toInsert, document, 'body');
expect(inserted).to.equal(target.firstChild);
});
it('returns a node at bottom of target if 4th argument is true', function () {
const toInsert = document.createElement('div');
const target = document.getElementsByTagName('html')[0];
const inserted = utils.insertElement(toInsert, document, 'html', true);
expect(inserted).to.equal(target.lastChild);
});
it('returns a node at top of the head if no target is given', function () {
const toInsert = document.createElement('div');
const target = document.getElementsByTagName('head')[0];
const inserted = utils.insertElement(toInsert);
expect(inserted).to.equal(head.lastChild);
expect(inserted).to.equal(target.firstChild);
});
});
});
Expand Down

0 comments on commit a1091e3

Please sign in to comment.