From 1160b8080405eb224c860143ee985d14fc214e52 Mon Sep 17 00:00:00 2001 From: Dmitry Lyzo Date: Sun, 1 Dec 2024 20:34:02 +0300 Subject: [PATCH 1/3] Add polyfills for 'Element.append' and 'Element.prepend' --- src/lib/legacy/elementAppendPrepend.js | 51 ++++++++++++++++++++++++++ src/lib/legacy/index.ts | 1 + 2 files changed, 52 insertions(+) create mode 100644 src/lib/legacy/elementAppendPrepend.js diff --git a/src/lib/legacy/elementAppendPrepend.js b/src/lib/legacy/elementAppendPrepend.js new file mode 100644 index 000000000..b02631755 --- /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 9a836578e..d90a9e684 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'; From 3fd36ee626848e30b8f8d868d4a36ae00efcafe5 Mon Sep 17 00:00:00 2001 From: Dmitry Lyzo Date: Sun, 1 Dec 2024 20:34:36 +0300 Subject: [PATCH 2/3] Add element-closest-polyfill --- package-lock.json | 11 +++++++++++ package.json | 1 + src/lib/legacy/index.ts | 1 + 3 files changed, 13 insertions(+) diff --git a/package-lock.json b/package-lock.json index 023d5e743..364f05eae 100644 --- a/package-lock.json +++ b/package-lock.json @@ -32,6 +32,7 @@ "core-js": "3.41.0", "date-fns": "2.30.0", "dompurify": "2.5.8", + "element-closest-polyfill": "1.0.7", "epubjs": "0.3.93", "escape-html": "1.0.3", "fast-text-encoding": "1.0.6", @@ -9696,6 +9697,11 @@ "dev": true, "license": "ISC" }, + "node_modules/element-closest-polyfill": { + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/element-closest-polyfill/-/element-closest-polyfill-1.0.7.tgz", + "integrity": "sha512-SX7RrUUEybUllkGjVf5XJR5I0Fl2L0iawK/caX/yJu94xW/WoRRD8Fky65gKpvqhU9PLBKOliIy4Ly2rDRzdUQ==" + }, "node_modules/emoji-regex": { "version": "8.0.0", "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz", @@ -32265,6 +32271,11 @@ "integrity": "sha512-eHhqaja8tE/FNpIiBrvBjFV/SSKpyWHLvxuR9dPTdo+3V9ppdLmFB7ZZQ98qNovcngPLYIz0oOBF9P0FfZef5Q==", "dev": true }, + "element-closest-polyfill": { + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/element-closest-polyfill/-/element-closest-polyfill-1.0.7.tgz", + "integrity": "sha512-SX7RrUUEybUllkGjVf5XJR5I0Fl2L0iawK/caX/yJu94xW/WoRRD8Fky65gKpvqhU9PLBKOliIy4Ly2rDRzdUQ==" + }, "emoji-regex": { "version": "8.0.0", "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz", diff --git a/package.json b/package.json index 5d86090f2..7149a9707 100644 --- a/package.json +++ b/package.json @@ -98,6 +98,7 @@ "core-js": "3.41.0", "date-fns": "2.30.0", "dompurify": "2.5.8", + "element-closest-polyfill": "1.0.7", "epubjs": "0.3.93", "escape-html": "1.0.3", "fast-text-encoding": "1.0.6", diff --git a/src/lib/legacy/index.ts b/src/lib/legacy/index.ts index d90a9e684..d8dcf9539 100644 --- a/src/lib/legacy/index.ts +++ b/src/lib/legacy/index.ts @@ -1,6 +1,7 @@ import 'core-js/stable'; import 'regenerator-runtime/runtime'; import 'jquery'; +import 'element-closest-polyfill'; import 'fast-text-encoding'; import 'intersection-observer'; import 'classlist.js'; From 6863ab7e89fdf5a885f570a4d2cbdc211c731203 Mon Sep 17 00:00:00 2001 From: Bill Thornton Date: Wed, 9 Apr 2025 11:35:44 -0400 Subject: [PATCH 3/3] Fix lint and add license header --- src/lib/legacy/elementAppendPrepend.js | 40 ++++++++++++++++++++------ 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/src/lib/legacy/elementAppendPrepend.js b/src/lib/legacy/elementAppendPrepend.js index b02631755..53039f451 100644 --- a/src/lib/legacy/elementAppendPrepend.js +++ b/src/lib/legacy/elementAppendPrepend.js @@ -1,9 +1,33 @@ +/** + * MIT License + * + * Copyright (c) 2016-2025, jszhou + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + // 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')) { + if (Object.prototype.hasOwnProperty.call(item, 'append')) { return; } Object.defineProperty(item, 'append', { @@ -11,11 +35,11 @@ enumerable: true, writable: true, value: function append() { - var argArr = Array.prototype.slice.call(arguments), - docFrag = document.createDocumentFragment(); + const argArr = Array.prototype.slice.call(arguments); + const docFrag = document.createDocumentFragment(); argArr.forEach(function (argItem) { - var isNode = argItem instanceof Node; + const isNode = argItem instanceof Node; docFrag.appendChild(isNode ? argItem : document.createTextNode(String(argItem))); }); @@ -28,7 +52,7 @@ // Source: https://github.com/jserz/js_piece/blob/master/DOM/ParentNode/prepend()/prepend().md (function (arr) { arr.forEach(function (item) { - if (item.hasOwnProperty('prepend')) { + if (Object.prototype.hasOwnProperty.call(item, 'prepend')) { return; } Object.defineProperty(item, 'prepend', { @@ -36,11 +60,11 @@ enumerable: true, writable: true, value: function prepend() { - var argArr = Array.prototype.slice.call(arguments), - docFrag = document.createDocumentFragment(); + const argArr = Array.prototype.slice.call(arguments); + const docFrag = document.createDocumentFragment(); argArr.forEach(function (argItem) { - var isNode = argItem instanceof Node; + const isNode = argItem instanceof Node; docFrag.appendChild(isNode ? argItem : document.createTextNode(String(argItem))); });