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

Insert iframe as the last child of Html tag #3415

Merged
merged 9 commits into from
Jan 8, 2019
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
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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