|
1 | 1 | (function () { |
| 2 | + // polyfill for Object.hasOwn |
| 3 | + |
| 4 | + (function () { |
| 5 | + var oldHasOwn = Function.prototype.call.bind( |
| 6 | + Object.prototype.hasOwnProperty, |
| 7 | + ); |
| 8 | + if (oldHasOwn(Object, "hasOwn")) return; |
| 9 | + Object.defineProperty(Object, "hasOwn", { |
| 10 | + configurable: true, |
| 11 | + enumerable: false, |
| 12 | + writable: true, |
| 13 | + value: function hasOwn(obj, prop) { |
| 14 | + return oldHasOwn(obj, prop); |
| 15 | + }, |
| 16 | + }); |
| 17 | + Object.hasOwn.prototype = null; |
| 18 | + })(); |
| 19 | + |
2 | 20 | // polyfill for prepend |
3 | 21 |
|
4 | 22 | (function (arr) { |
5 | 23 | arr.forEach(function (item) { |
6 | | - if (item.hasOwnProperty("prepend")) { |
7 | | - return; |
8 | | - } |
| 24 | + if (Object.hasOwn(item, "prepend")) return; |
9 | 25 | Object.defineProperty(item, "prepend", { |
10 | 26 | configurable: true, |
11 | | - enumerable: true, |
| 27 | + enumerable: false, |
12 | 28 | writable: true, |
13 | 29 | value: function prepend() { |
14 | | - var argArr = Array.prototype.slice.call(arguments), |
15 | | - docFrag = document.createDocumentFragment(); |
| 30 | + var docFrag = createDocumentFragment(); |
16 | 31 |
|
17 | | - argArr.forEach(function (argItem) { |
18 | | - var node = |
19 | | - argItem instanceof Node |
20 | | - ? argItem |
21 | | - : document.createTextNode(String(argItem)); |
22 | | - docFrag.appendChild(node); |
23 | | - }); |
| 32 | + var argLength = arguments.length; |
| 33 | + for (var i = 0; i < argLength; i++) { |
| 34 | + var argItem = arguments[i]; |
| 35 | + docFrag.appendChild( |
| 36 | + argItem instanceof Node ? argItem : createTextNode(argItem + ""), |
| 37 | + ); |
| 38 | + } |
24 | 39 |
|
25 | 40 | this.insertBefore(docFrag, this.firstChild); |
26 | 41 | }, |
27 | 42 | }); |
| 43 | + item.prepend.prototype = null; |
28 | 44 | }); |
29 | 45 | })([Element.prototype, Document.prototype, DocumentFragment.prototype]); |
30 | 46 |
|
31 | 47 | // polyfill for closest |
32 | 48 |
|
33 | 49 | (function (arr) { |
34 | 50 | arr.forEach(function (item) { |
35 | | - if (item.hasOwnProperty("closest")) { |
36 | | - return; |
37 | | - } |
| 51 | + if (Object.hasOwn(item, "closest")) return; |
38 | 52 | Object.defineProperty(item, "closest", { |
39 | 53 | configurable: true, |
40 | | - enumerable: true, |
| 54 | + enumerable: false, |
41 | 55 | writable: true, |
42 | 56 | value: function closest(s) { |
43 | | - var matches = (this.document || this.ownerDocument).querySelectorAll(s), |
| 57 | + var matches = (this.document || this.ownerDocument).querySelectorAll( |
| 58 | + s, |
| 59 | + ), |
44 | 60 | i, |
45 | 61 | el = this; |
46 | 62 | do { |
|
50 | 66 | return el; |
51 | 67 | }, |
52 | 68 | }); |
| 69 | + item.closest.prototype = null; |
53 | 70 | }); |
54 | 71 | })([Element.prototype]); |
55 | 72 |
|
56 | 73 | // polyfill for replaceWith |
57 | 74 |
|
58 | 75 | (function (arr) { |
59 | 76 | arr.forEach(function (item) { |
60 | | - if (item.hasOwnProperty("replaceWith")) { |
61 | | - return; |
62 | | - } |
| 77 | + if (Object.hasOwn(item, "replaceWith")) return; |
63 | 78 | Object.defineProperty(item, "replaceWith", { |
64 | 79 | configurable: true, |
65 | | - enumerable: true, |
| 80 | + enumerable: false, |
66 | 81 | writable: true, |
67 | 82 | value: function replaceWith() { |
68 | | - var parent = this.parentNode, |
69 | | - i = arguments.length, |
70 | | - currentNode; |
| 83 | + var parent = this.parentNode; |
71 | 84 | if (!parent) return; |
72 | | - if (!i) |
73 | | - // if there are no arguments |
74 | | - parent.removeChild(this); |
75 | | - while (i--) { |
76 | | - // i-- decrements i and returns the value of i before the decrement |
77 | | - currentNode = arguments[i]; |
| 85 | + var docFrag = createDocumentFragment(); |
| 86 | + var argLength = arguments.length; |
| 87 | + for (var i = 0; i < argLength; i++) { |
| 88 | + var currentNode = arguments[i]; |
78 | 89 | if (typeof currentNode !== "object") { |
79 | | - currentNode = this.ownerDocument.createTextNode(currentNode); |
| 90 | + currentNode = createTextNode(currentNode, this.ownerDocument); |
80 | 91 | } else if (currentNode.parentNode) { |
81 | 92 | currentNode.parentNode.removeChild(currentNode); |
82 | 93 | } |
83 | | - // the value of "i" below is after the decrement |
84 | | - if (!i) |
85 | | - // if currentNode is the first argument (currentNode === arguments[0]) |
86 | | - parent.replaceChild(currentNode, this); |
87 | | - // if currentNode isn't the first |
88 | | - else parent.insertBefore(this.previousSibling, currentNode); |
| 94 | + docFrag.appendChild(currentNode); |
| 95 | + } |
| 96 | + if (argLength >= 1) { |
| 97 | + parent.replaceChild(docFrag, this); |
| 98 | + } else { |
| 99 | + parent.removeChild(this); |
89 | 100 | } |
90 | 101 | }, |
91 | 102 | }); |
| 103 | + item.replaceWith.prototype = null; |
92 | 104 | }); |
93 | 105 | })([Element.prototype, CharacterData.prototype, DocumentType.prototype]); |
94 | 106 |
|
95 | 107 | // polyfill for toggleAttribute |
96 | 108 |
|
97 | 109 | (function (arr) { |
98 | 110 | arr.forEach(function (item) { |
99 | | - if (item.hasOwnProperty("toggleAttribute")) { |
100 | | - return; |
101 | | - } |
| 111 | + if (Object.hasOwn(item, "toggleAttribute")) return; |
102 | 112 | Object.defineProperty(item, "toggleAttribute", { |
103 | 113 | configurable: true, |
104 | | - enumerable: true, |
| 114 | + enumerable: false, |
105 | 115 | writable: true, |
106 | 116 | value: function toggleAttribute() { |
107 | 117 | var attr = arguments[0]; |
108 | 118 | if (this.hasAttribute(attr)) { |
109 | 119 | this.removeAttribute(attr); |
| 120 | + return false; |
110 | 121 | } else { |
111 | | - this.setAttribute(attr, arguments[1] || ""); |
| 122 | + this.setAttribute(attr, arguments.length >= 2 ? arguments[1] : ""); |
| 123 | + return true; |
112 | 124 | } |
113 | 125 | }, |
114 | 126 | }); |
| 127 | + item.toggleAttribute.prototype = null; |
115 | 128 | }); |
116 | 129 | })([Element.prototype]); |
117 | 130 |
|
|
141 | 154 | }; |
142 | 155 | } |
143 | 156 | })(); |
| 157 | + |
| 158 | + // polyfill for Promise.withResolvers |
| 159 | + |
| 160 | + if (!Object.hasOwn(Promise, "withResolvers")) { |
| 161 | + Object.defineProperty(Promise, "withResolvers", { |
| 162 | + configurable: true, |
| 163 | + enumerable: false, |
| 164 | + writable: true, |
| 165 | + value: function withResolvers() { |
| 166 | + var resolve, reject; |
| 167 | + var promise = new this(function (_resolve, _reject) { |
| 168 | + resolve = _resolve; |
| 169 | + reject = _reject; |
| 170 | + }); |
| 171 | + if (typeof resolve !== "function" || typeof reject !== "function") { |
| 172 | + throw new TypeError( |
| 173 | + "Promise resolve or reject function is not callable", |
| 174 | + ); |
| 175 | + } |
| 176 | + return { |
| 177 | + promise: promise, |
| 178 | + resolve: resolve, |
| 179 | + reject: reject, |
| 180 | + }; |
| 181 | + }, |
| 182 | + }); |
| 183 | + Promise.withResolvers.prototype = null; |
| 184 | + } |
| 185 | + |
| 186 | + // utils |
| 187 | + |
| 188 | + function createTextNode(text, doc) { |
| 189 | + if (doc === undefined) doc = document; |
| 190 | + try { |
| 191 | + if (doc !== document) throw undefined; |
| 192 | + return new Text(text); |
| 193 | + } catch (_) { |
| 194 | + return doc.createTextNode(text); |
| 195 | + } |
| 196 | + } |
| 197 | + function createDocumentFragment() { |
| 198 | + try { |
| 199 | + return new DocumentFragment(); |
| 200 | + } catch (_) { |
| 201 | + return document.createDocumentFragment(); |
| 202 | + } |
| 203 | + } |
144 | 204 | })(); |
0 commit comments