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