@shgysk8zer0/polyfills 0.3.9 → 0.3.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/blob.js ADDED
@@ -0,0 +1,5 @@
1
+ import { polyfillMethod } from './utils.js';
2
+
3
+ polyfillMethod(Blob.prototype, 'bytes', async function () {
4
+ return new Uint8Array(await this.arrayBuffer());
5
+ });
package/element.js CHANGED
@@ -1,7 +1,10 @@
1
1
  import { aria } from './aom.js';
2
- import { polyfillGetterSetter } from './utils.js';
2
+ import { polyfillGetterSetter, polyfillMethod } from './utils.js';
3
+ import { setHTMLUnsafe } from './methods/dom.js';
3
4
  import './sanitizer.js';
4
5
 
6
+ polyfillMethod(Element.prototype, 'setHTMLUnsafe', setHTMLUnsafe);
7
+
5
8
  function handlePopover({ currentTarget }) {
6
9
  switch(currentTarget.popoverTargetAction) {
7
10
  case 'show':
package/math.js CHANGED
@@ -67,8 +67,29 @@ if (! (Math.constrain instanceof Function)) {
67
67
  /**
68
68
  * @see https://github.com/tc39/proposal-math-sum
69
69
  */
70
+ if (! (Math.sumPrecise instanceof Function)) {
71
+ Math.sumPrecise = function sumPrecise(nums) {
72
+ let sum = -0;
73
+
74
+ for (const num of nums) {
75
+ if (! Number.isFinite(num)) {
76
+ sum = -0;
77
+ break;
78
+ } else {
79
+ sum += num;
80
+ }
81
+ }
82
+
83
+ return sum;
84
+ };
85
+ }
86
+
87
+ /**
88
+ * @deprecated
89
+ */
70
90
  if(! (Math.sum instanceof Function)) {
71
91
  Math.sum = function(...nums) {
72
- return nums.map(num => parseFloat(num)).reduce((sum, num) => sum + num);
92
+ console.warn('Math.sum is deprecated. Please use Math.sumPrecise instead.');
93
+ return Math.sumPrecise(nums);
73
94
  };
74
95
  }
package/methods/dom.js ADDED
@@ -0,0 +1,39 @@
1
+ function attachShadow(template){
2
+ if (template instanceof HTMLTemplateElement && template.parentElement instanceof HTMLElement) {
3
+ const shadow = template.parentElement.attachShadow({
4
+ mode: template.getAttribute('shadowrootmode'),
5
+ clonable: template.hasAttribute('shadowrootclonable'),
6
+ delegatesFocus: template.hasAttribute('shadowrootdelegatesfocus'),
7
+ serializable: template.hasAttribute('shadowrootserializable'),
8
+ });
9
+
10
+ shadow.append(template.content);
11
+ template.remove();
12
+ }
13
+ }
14
+
15
+ export function parseHTMLUnsafe(input){
16
+ const parser = new DOMParser();
17
+ // Ensures `URL` is "about:blank"
18
+ const doc = document.implementation.createHTMLDocument();
19
+ // Rely on this method's TrustedTypes implementation, if available
20
+ const parsed = parser.parseFromString(input, 'text/html');
21
+ doc.head.append(...parsed.head.childNodes);
22
+ doc.body.append(...parsed.body.childNodes);
23
+ doc.querySelectorAll('template[shadowrootmode]').forEach(attachShadow);
24
+ return doc;
25
+ }
26
+
27
+ export function setHTMLUnsafe(input) {
28
+ const parser = new DOMParser();
29
+ // Rely on this method's TrustedTypes implementation, if available
30
+ const parsed = parser.parseFromString(input, 'text/html');
31
+ const frag = document.createDocumentFragment();
32
+ frag.append(...parsed.body.childNodes);
33
+ attachShadow(frag.querySelector('template[shadowroootmode]'));
34
+ this.replaceChildren(frag);
35
+ }
36
+
37
+ export function getHTML() {
38
+ return this.innerHTML;
39
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shgysk8zer0/polyfills",
3
- "version": "0.3.9",
3
+ "version": "0.3.11",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "A collection of JavaScript polyfills",
@@ -77,7 +77,7 @@
77
77
  "http-server": "^14.1.1"
78
78
  },
79
79
  "dependencies": {
80
- "@aegisjsproject/sanitizer": "^0.0.10",
80
+ "@aegisjsproject/sanitizer": "^0.1.0",
81
81
  "@aegisjsproject/trusted-types": "^1.0.1"
82
82
  }
83
83
  }
package/request.js ADDED
@@ -0,0 +1,5 @@
1
+ import { polyfillMethod } from './utils.js';
2
+
3
+ polyfillMethod(Request.prototype, 'bytes', async function () {
4
+ return new Uint8Array(await this.arrayBuffer());
5
+ });
package/response.js CHANGED
@@ -15,3 +15,7 @@ polyfillMethod(Response, 'redirect', (url, status = 302) => {
15
15
  headers: new Headers({ Location: url }),
16
16
  });
17
17
  });
18
+
19
+ polyfillMethod(Response.prototype, 'bytes', async function() {
20
+ return new Uint8Array(await this.arrayBuffer());
21
+ });