diff --git a/src/lib/legacy/elementAppendPrepend.js b/src/lib/legacy/elementAppendPrepend.js new file mode 100644 index 0000000000..b02631755a --- /dev/null +++ b/src/lib/legacy/elementAppendPrepend.js @@ -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]); diff --git a/src/lib/legacy/index.ts b/src/lib/legacy/index.ts index 9a836578e5..d90a9e6846 100644 --- a/src/lib/legacy/index.ts +++ b/src/lib/legacy/index.ts @@ -9,6 +9,7 @@ import 'abortcontroller-polyfill'; // requires fetch import 'resize-observer-polyfill'; import './domParserTextHtml'; +import './elementAppendPrepend'; import './focusPreventScroll'; import './htmlMediaElement'; import './keyboardEvent';