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