@shgysk8zer0/polyfills 0.3.10 → 0.3.12

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/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.10",
3
+ "version": "0.3.12",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "A collection of JavaScript polyfills",
@@ -78,6 +78,7 @@
78
78
  },
79
79
  "dependencies": {
80
80
  "@aegisjsproject/sanitizer": "^0.1.0",
81
- "@aegisjsproject/trusted-types": "^1.0.1"
81
+ "@aegisjsproject/trusted-types": "^1.0.1",
82
+ "string-dedent": "^3.0.1"
82
83
  }
83
84
  }
package/promise.js CHANGED
@@ -67,4 +67,25 @@ if ('Promise' in globalThis) {
67
67
  return def;
68
68
  };
69
69
  }
70
+
71
+ if (! (Promise.fromEntries instanceof Function)) {
72
+ Promise.fromEntries = async function fromEntries(entries) {
73
+ const keys = [];
74
+ const values = [];
75
+
76
+ for (const [key, value] of entries) {
77
+ keys.push(key);
78
+ values.push(value);
79
+ }
80
+
81
+ return await Promise.all(values)
82
+ .then(values => Object.fromEntries(values.map((value, i) => [keys[i], value])));
83
+ };
84
+ }
85
+
86
+ if (!(Promise.ownProperties instanceof Function)) {
87
+ Promise.ownProperties = async function ownProperties(obj) {
88
+ return await Promise.fromEntries(Object.entries(obj));
89
+ };
90
+ }
70
91
  }
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
+ });
package/string.js ADDED
@@ -0,0 +1,5 @@
1
+ import dedent from './node_modules/string-dedent/dist/dedent.mjs';
2
+
3
+ if (! (String.dedent instanceof Function)) {
4
+ String.dedent = dedent;
5
+ }
package/symbols.js CHANGED
@@ -1,4 +1,12 @@
1
+ import { polyfillMethod } from './utils.js';
2
+
1
3
  if ('Symbol' in globalThis) {
4
+ const known = new Set(
5
+ Reflect.ownKeys(Symbol)
6
+ .filter(item => typeof Symbol[item] === 'symbol')
7
+ .map(key => Symbol[key])
8
+ );
9
+
2
10
  if (typeof Symbol.toStringTag === 'undefined') {
3
11
  Symbol.toStringTag = Symbol('Symbol.toStringTag');
4
12
  }
@@ -6,4 +14,12 @@ if ('Symbol' in globalThis) {
6
14
  if (typeof Symbol.iterator === 'undefined') {
7
15
  Symbol.iterator = Symbol('Symbol.iterator');
8
16
  }
17
+
18
+ polyfillMethod(Symbol, 'isRegistered', function(symbol) {
19
+ return typeof symbol === 'symbol' && typeof Symbol.keyFor(symbol) === 'string';
20
+ });
21
+
22
+ polyfillMethod(Symbol, 'isWellKnown', function(symbol) {
23
+ return known.has(symbol);
24
+ });
9
25
  }
package/url.js CHANGED
@@ -1,9 +1,16 @@
1
1
  import { polyfillMethod } from './utils.js';
2
2
 
3
- polyfillMethod(URL, 'canParse', (str, base) => {
3
+ polyfillMethod(URL, 'parse', (url, base) => {
4
4
  try {
5
- new URL(str, base);
6
- return true;
5
+ return new URL(url, base);
6
+ } catch {
7
+ return null;
8
+ }
9
+ });
10
+
11
+ polyfillMethod(URL, 'canParse', (url, base) => {
12
+ try {
13
+ return new URL(url, base) instanceof URL;
7
14
  } catch {
8
15
  return false;
9
16
  }