Add polyfills for 'Element.append' and 'Element.prepend'
This commit is contained in:
committed by
Bill Thornton
parent
c1780ea8b0
commit
1160b80804
51
src/lib/legacy/elementAppendPrepend.js
Normal file
51
src/lib/legacy/elementAppendPrepend.js
Normal file
@@ -0,0 +1,51 @@
|
||||
// From https://gist.github.com/jickoo/7b4122829240cc415c098aab89d6f49d
|
||||
|
||||
// Source: https://github.com/jserz/js_piece/blob/master/DOM/ParentNode/append()/append().md
|
||||
(function (arr) {
|
||||
arr.forEach(function (item) {
|
||||
if (item.hasOwnProperty('append')) {
|
||||
return;
|
||||
}
|
||||
Object.defineProperty(item, 'append', {
|
||||
configurable: true,
|
||||
enumerable: true,
|
||||
writable: true,
|
||||
value: function append() {
|
||||
var argArr = Array.prototype.slice.call(arguments),
|
||||
docFrag = document.createDocumentFragment();
|
||||
|
||||
argArr.forEach(function (argItem) {
|
||||
var isNode = argItem instanceof Node;
|
||||
docFrag.appendChild(isNode ? argItem : document.createTextNode(String(argItem)));
|
||||
});
|
||||
|
||||
this.appendChild(docFrag);
|
||||
}
|
||||
});
|
||||
});
|
||||
})([Element.prototype, Document.prototype, DocumentFragment.prototype]);
|
||||
|
||||
// Source: https://github.com/jserz/js_piece/blob/master/DOM/ParentNode/prepend()/prepend().md
|
||||
(function (arr) {
|
||||
arr.forEach(function (item) {
|
||||
if (item.hasOwnProperty('prepend')) {
|
||||
return;
|
||||
}
|
||||
Object.defineProperty(item, 'prepend', {
|
||||
configurable: true,
|
||||
enumerable: true,
|
||||
writable: true,
|
||||
value: function prepend() {
|
||||
var argArr = Array.prototype.slice.call(arguments),
|
||||
docFrag = document.createDocumentFragment();
|
||||
|
||||
argArr.forEach(function (argItem) {
|
||||
var isNode = argItem instanceof Node;
|
||||
docFrag.appendChild(isNode ? argItem : document.createTextNode(String(argItem)));
|
||||
});
|
||||
|
||||
this.insertBefore(docFrag, this.firstChild);
|
||||
}
|
||||
});
|
||||
});
|
||||
})([Element.prototype, Document.prototype, DocumentFragment.prototype]);
|
||||
@@ -9,6 +9,7 @@ import 'abortcontroller-polyfill'; // requires fetch
|
||||
import 'resize-observer-polyfill';
|
||||
|
||||
import './domParserTextHtml';
|
||||
import './elementAppendPrepend';
|
||||
import './focusPreventScroll';
|
||||
import './htmlMediaElement';
|
||||
import './keyboardEvent';
|
||||
|
||||
Reference in New Issue
Block a user