@webtides/element-js 1.1.0 → 1.1.1

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
@@ -16,6 +16,13 @@ to [Semantic Versioning](http://semver.org/).
16
16
  <!-- ### Removed -->
17
17
  <!-- ### Fixed -->
18
18
 
19
+ ## [1.1.1] - 2024-09-16
20
+
21
+ ### FIXED
22
+
23
+ - Fixed base url and css rules order in adopted style sheets.
24
+ - Fixed randomUUID not being available in unsecure hosts (like localhost) by adding a helper function
25
+
19
26
  ## [1.1.0] - 2024-06-24
20
27
 
21
28
  ### Added
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webtides/element-js",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "module": "index.js",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -2,6 +2,7 @@ import { parseAttribute, isNaN, dashToCamel, camelToDash, isObjectLike } from '.
2
2
  import { getAllElementChildren, getClosestParentCustomElementNode, isOfSameNodeType } from './util/DOMHelper.js';
3
3
  import { Store } from './util/Store.js';
4
4
  import { serializeState, deserializeState, hasSerializedState } from './util/SerializeStateHelper.js';
5
+ import { randomUUID } from './util/crypto.js';
5
6
  export { defineElement } from './util/defineElement.js';
6
7
  export { toString } from './util/toString.js';
7
8
 
@@ -97,7 +98,7 @@ class BaseElement extends HTMLElement {
97
98
  this._serializationKey = this.getAttribute('ejs:key');
98
99
  deserializeState(this);
99
100
  } else {
100
- this._serializationKey = globalThis.crypto.randomUUID();
101
+ this._serializationKey = randomUUID();
101
102
  if (globalThis.elementJsConfig?.serializeState) {
102
103
  this.setAttribute('ejs:key', this._serializationKey);
103
104
  }
@@ -18,7 +18,7 @@ export class NodePart extends Part {
18
18
  constructor(node, initialValue) {
19
19
  if (!isObjectLike(initialValue) && !initialValue.directiveClass) {
20
20
  throw new Error(
21
- 'NodePart: value is not a wrapped directive function. You must wrap you custom directive class with the defineDirective function.',
21
+ `NodePart: value "${initialValue}" is not a wrapped directive function. You must wrap you custom directive class with the defineDirective function.`,
22
22
  );
23
23
  }
24
24
  super();
@@ -49,12 +49,19 @@ class GlobalStylesStore extends Store {
49
49
  // TODO: this will always be null as we never set anything to the cache...
50
50
  let cssStyleSheet = this.globalStyleSheetsCache.get(styleSheet.ownerNode);
51
51
  if (!cssStyleSheet) {
52
- cssStyleSheet = new CSSStyleSheet();
53
52
  if (styleSheet.ownerNode.tagName === 'STYLE') {
53
+ cssStyleSheet = new CSSStyleSheet({ media: styleSheet.media, disabled: styleSheet.disabled });
54
54
  cssStyleSheet.replaceSync(styleSheet.ownerNode.textContent);
55
55
  } else if (styleSheet.ownerNode.tagName === 'LINK') {
56
+ cssStyleSheet = new CSSStyleSheet({
57
+ baseURL: styleSheet.href,
58
+ media: styleSheet.media,
59
+ disabled: styleSheet.disabled,
60
+ });
56
61
  try {
57
- Array.from(styleSheet?.cssRules ?? []).map((rule) => cssStyleSheet.insertRule(rule.cssText));
62
+ Array.from(styleSheet?.cssRules ?? []).map((rule, index) =>
63
+ cssStyleSheet.insertRule(rule.cssText, index),
64
+ );
58
65
  } catch (e) {
59
66
  console.error(
60
67
  'GlobalStylesStore: cannot read cssRules. Maybe add crossorigin="anonymous" to your style link?',
package/src/util/Store.js CHANGED
@@ -6,6 +6,7 @@ import {
6
6
  serializableObjectsCache,
7
7
  } from './SerializeStateHelper.js';
8
8
  import { BaseElement } from '../BaseElement.js';
9
+ import { randomUUID } from './crypto.js';
9
10
 
10
11
  /**
11
12
  * Options object for the Store
@@ -36,7 +37,7 @@ export class Store {
36
37
  if (options?.key) {
37
38
  this._serializationKey = options?.key;
38
39
  } else {
39
- this._serializationKey = globalThis.crypto.randomUUID();
40
+ this._serializationKey = randomUUID();
40
41
  }
41
42
 
42
43
  if (options?.serializedState) {
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Helper function to use the crypto.randomUUID() as it will fail in none secure hosts (e.g. localhost)
3
+ * @return {`${string}-${string}-${string}-${string}-${string}`|*}
4
+ */
5
+ export function randomUUID() {
6
+ if (!('randomUUID' in globalThis.crypto)) {
7
+ // https://stackoverflow.com/a/2117523/2800218
8
+ return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, (c) =>
9
+ (c ^ (crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4)))).toString(16),
10
+ );
11
+ }
12
+ return globalThis.crypto.randomUUID();
13
+ }