@shgysk8zer0/polyfills 0.0.7 → 0.1.0

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/CHANGELOG.md CHANGED
@@ -6,6 +6,31 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [v0.1.0] - 2023-06-19
10
+
11
+ ### Added
12
+ - Add shim for `Document.parseHTML`
13
+ - Add shim for constructable stylesheets (#21)
14
+ - Add `@shgysk8zer0/js-utils`
15
+
16
+ ### Changed
17
+ - `el.setHTML()` now conforms to the updated draft Sanitizer API spec
18
+
19
+ ### Removed
20
+ - Remove `eslint` and `rollup` and plugins
21
+
22
+ ### Deprecated
23
+ - Sanitizer is now considered deprecated, pending changes to the spec
24
+
25
+ ## [v0.0.8] - 2023-06-02
26
+
27
+ ### Added
28
+ - `URL.canParse()` [#15]
29
+ - `utils.js` module with helper functions (`polyfillMethod()`, currently)
30
+
31
+ ### Fixed
32
+ - Update GitHub Release Action with correct permissions
33
+
9
34
  ## [v0.0.7] - 2023-05-15
10
35
 
11
36
  ### Fixed
@@ -0,0 +1,83 @@
1
+ export function createSheet(rules = '', { disabled = false, media } = {}) {
2
+ const style = Document.parseHTML(
3
+ `<style>${rules}</style>`,
4
+ { sanitizer: new Sanitizer({ allowElements: ['style'] }) }
5
+ );
6
+
7
+
8
+ const sheet = style.styleSheets.item(0);
9
+ if (typeof media === 'string') {
10
+ sheet.media.appendMedium(media);
11
+ }
12
+ sheet.disabled = disabled;
13
+ return sheet;
14
+ }
15
+
16
+ export function clearCSSRules(sheet) {
17
+ while (sheet.cssRules.length > 0) {
18
+ sheet.deleteRule(0);
19
+ }
20
+ }
21
+
22
+ try {
23
+ new CSSStyleSheet();
24
+ } catch {
25
+ const NativeStyleSheet = globalThis.CSSStyleSheet;
26
+
27
+ globalThis.CSSStyleSheet = function CSSStyleSheet({ disabled = false, media } = {}) {
28
+ // Need at least one rule to create the `.sheet`
29
+ const sheet = createSheet('#dfgbkjdfg{color:red;}', { disabled, media });
30
+ sheet.deleteRule(0);
31
+ return sheet;
32
+ };
33
+
34
+ globalThis.CSSStyleSheet.prototype = NativeStyleSheet.prototype;
35
+ NativeStyleSheet.prototype.constructor = globalThis.CSSStyleSheet;
36
+ }
37
+
38
+ if (! (CSSStyleSheet.prototype.replace instanceof Function)) {
39
+ CSSStyleSheet.prototype.replace = function replace(rules) {
40
+ const link = document.createElement('link');
41
+ const controller = new AbortController();
42
+ const { resolve, reject, promise } = Promise.withResolvers();
43
+
44
+ link.addEventListener('load', ({ target }) => {
45
+ clearCSSRules(this);
46
+ [...target.sheet.cssRules].forEach((rule, i) => this.insertRule(rule.cssText, i));
47
+ resolve(this);
48
+ controller.abort();
49
+ URL.revokeObjectURL(target.href);
50
+ target.remove();
51
+ }, { once: true, signal: controller.signal });
52
+
53
+ link.addEventListener('error', ({ target }) => {
54
+ const error = new DOMException('Error loading stylesheet.');
55
+ reject(error);
56
+ controller.abort(error);
57
+ URL.revokeObjectURL(target.href);
58
+ target.remove();
59
+ }, { once: true, signal: controller.signal });
60
+
61
+ link.rel = 'stylesheet';
62
+ link.crossOrigin = 'anonymous';
63
+ link.referrerPolicy = 'no-referrer';
64
+ link.media = 'no-op';
65
+ link.href = URL.createObjectURL(new File([rules], 'tmp.css', { type: 'text/css' }));
66
+ document.head.append(link);
67
+
68
+ return promise;
69
+ };
70
+ }
71
+
72
+ if (! (CSSStyleSheet.prototype.replaceSync instanceof Function)) {
73
+ CSSStyleSheet.prototype.replaceSync = function replaceSync(rules) {
74
+ const sheet = Document.parseHTML(
75
+ `<style>${rules}</style>`,
76
+ { sanitizer: new Sanitizer({ allowElements: ['style'] }) }
77
+ ).styleSheets.item(0);
78
+
79
+ clearCSSRules(this);
80
+
81
+ [...sheet.cssRules].forEach((rule, i) => this.insertRule(rule.cssText, i));
82
+ };
83
+ }
package/Document.js ADDED
@@ -0,0 +1,6 @@
1
+ import { polyfillMethod, polyfillGetterSetter } from './utils.js';
2
+ import { safeParseHTML } from './assets/sanitizerUtils.js';
3
+ import { adoptedStyleSheets } from './assets/adoptedStylesheets.js';
4
+
5
+ polyfillMethod(Document, 'parseHTML', (...args) => safeParseHTML(...args));
6
+ polyfillGetterSetter(Document.prototype, 'adoptedStyleSheets', adoptedStyleSheets);
package/ShadowRoot.js ADDED
@@ -0,0 +1,6 @@
1
+ import { polyfillGetterSetter } from './utils.js';
2
+ import { adoptedStyleSheets } from './assets/adoptedStylesheets.js';
3
+
4
+ if ('ShadowRoot' in globalThis) {
5
+ polyfillGetterSetter(ShadowRoot.prototype, 'adoptedStyleSheets', adoptedStyleSheets);
6
+ }
package/all.js CHANGED
@@ -1,12 +1,12 @@
1
1
  /**
2
2
  * @copyright 2023 Chris Zuber <admin@kernvalley.us>
3
3
  */
4
-
4
+ import './globalThis.js';
5
5
  import './deprefixer.js';
6
- import './symbols.js';
7
6
  import './array.js';
8
- import './globalThis.js';
7
+ import './symbols.js';
9
8
  import './function.js';
9
+ import './url.js';
10
10
  import './number.js';
11
11
  import './iterator.js';
12
12
  import './math.js';
@@ -22,7 +22,10 @@ import './crypto.js';
22
22
  import './cookieStore.js';
23
23
  import './animation.js';
24
24
  import './trustedTypes.js';
25
- import './sanitizer.js';
25
+ import './deprecated/sanitizer.js';
26
+ import './Document.js';
27
+ import './ShadowRoot.js';
28
+ import './CSSStyleSheet.js';
26
29
  import './element.js';
27
30
  import './set.js';
28
31
  import './map.js';