@20baht/stickers 0.1.2 → 0.1.3

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/README.md CHANGED
@@ -136,6 +136,22 @@ needed). With `interactive` the diagram is a group of buttons: each node takes f
136
136
  fires `baht-node` with `{ id, node }` (bubbles, composed), for opening the machine it stands for. The docs site has a builder
137
137
  that writes this JSON: https://bahtui-docs.pages.dev/guides/sticker-graph/
138
138
 
139
+ ## Under a Content-Security-Policy
140
+
141
+ The canvas needs nothing. Each element adds one `<style>` to `<head>` the first time it connects
142
+ (`#baht-sticker-style`, `#baht-sticker-graph-style`), which a CSP whose `style-src` has no
143
+ `'unsafe-inline'` blocks. Give it the page's nonce:
144
+
145
+ ```html
146
+ <script nonce="{RANDOM}">window.litNonce = '{RANDOM}'</script> <!-- every element on the page -->
147
+ <baht-sticker name="piggy-bank" nonce="{RANDOM}"></baht-sticker> <!-- or the first one -->
148
+ ```
149
+
150
+ The sheet takes the `nonce` of the element that adds it, or of the nearest ancestor that has one (the
151
+ stickers inside a `<baht-sticker-graph nonce>`), else `window.litNonce` (Lit's own name for the
152
+ page's nonce). The sheet is added once, so the nonce must be there before the first sticker connects.
153
+ In Angular, `nonce="…"` or `[attr.nonce]="nonce"` on the element.
154
+
139
155
  ## Entry points
140
156
 
141
157
  | Import | Contents |
package/dist/element.d.ts CHANGED
@@ -22,6 +22,9 @@ import type { Icon, Palette } from './sticker.js';
22
22
  * @attr label - Accessible name; the sticker's name by default. Empty (`label=""`) makes the sticker decorative:
23
23
  * assistive technology skips it, for one beside words that already say the same.
24
24
  * @attr line - Outline weight as a multiple of the sticker's own: `0.6` thinner, `1.5` bolder; 1 by default.
25
+ * @attr nonce - The standard attribute, read when the first sticker adds the one `<style>` every sticker shares:
26
+ * that sheet carries it, for a CSP `style-src` without `'unsafe-inline'`. Unset: an ancestor's (a graph's), else
27
+ * `window.litNonce`.
25
28
  */
26
29
  export declare class BahtSticker extends LitElement {
27
30
  static properties: {
package/dist/element.js CHANGED
@@ -1,14 +1,17 @@
1
1
  // <baht-sticker>: plays a sticker on canvas, coloured from the page's --baht-* tokens unless given a palette.
2
2
  import { LitElement } from 'lit';
3
3
  import { getSticker, paletteFromTokens, play } from './sticker.js';
4
+ import { styleNonce } from './style-nonce.js';
4
5
  const TAG = 'baht-sticker';
5
6
  const STYLE_ID = 'baht-sticker-style';
6
7
  /** One sheet for every sticker: block by default, the canvas fills its width at 1:1. Size the element with css. */
7
- function injectStyle() {
8
+ function injectStyle(nonce) {
8
9
  if (document.getElementById(STYLE_ID))
9
10
  return;
10
11
  const style = document.createElement('style');
11
12
  style.id = STYLE_ID;
13
+ if (nonce)
14
+ style.nonce = nonce;
12
15
  style.textContent = `:where(${TAG}){display:block}:where(${TAG})>canvas{display:block;width:100%;aspect-ratio:1}`;
13
16
  document.head.append(style);
14
17
  }
@@ -65,6 +68,9 @@ function unwatchTheme(stale) {
65
68
  * @attr label - Accessible name; the sticker's name by default. Empty (`label=""`) makes the sticker decorative:
66
69
  * assistive technology skips it, for one beside words that already say the same.
67
70
  * @attr line - Outline weight as a multiple of the sticker's own: `0.6` thinner, `1.5` bolder; 1 by default.
71
+ * @attr nonce - The standard attribute, read when the first sticker adds the one `<style>` every sticker shares:
72
+ * that sheet carries it, for a CSP `style-src` without `'unsafe-inline'`. Unset: an ancestor's (a graph's), else
73
+ * `window.litNonce`.
68
74
  */
69
75
  export class BahtSticker extends LitElement {
70
76
  static { this.properties = {
@@ -122,7 +128,7 @@ export class BahtSticker extends LitElement {
122
128
  }
123
129
  connectedCallback() {
124
130
  super.connectedCallback();
125
- injectStyle();
131
+ injectStyle(styleNonce(this));
126
132
  this.canvas ??= document.createElement('canvas');
127
133
  if (!this.canvas.isConnected)
128
134
  this.append(this.canvas);
package/dist/graph.d.ts CHANGED
@@ -88,6 +88,8 @@ export declare function layoutGraph(graph: StickerGraph): GraphLayout;
88
88
  * @attr hide-grid - Leaves out the floor grid.
89
89
  * @attr label - Accessible name of the diagram.
90
90
  * @attr interactive - Nodes are focusable buttons that fire `baht-node`.
91
+ * @attr nonce - The standard attribute: the `<style>` the graph adds (and its stickers' one) carries it, for a CSP
92
+ * `style-src` without `'unsafe-inline'`. Unset: `window.litNonce`.
91
93
  */
92
94
  export declare class BahtStickerGraph extends LitElement {
93
95
  static properties: {
package/dist/graph.js CHANGED
@@ -3,6 +3,7 @@
3
3
  import { html, LitElement, nothing, svg } from 'lit';
4
4
  import './element.js';
5
5
  import { onFace, VIEWS } from './sticker.js';
6
+ import { styleNonce } from './style-nonce.js';
6
7
  /** Floor units per grid cell, and the sticker's width in the same units. */
7
8
  export const CELL = 150;
8
9
  export const STICKER = 150;
@@ -90,11 +91,13 @@ const TAG = 'baht-sticker-graph';
90
91
  const STYLE_ID = 'baht-sticker-graph-style';
91
92
  const GAP_MS = 260;
92
93
  const REST_MS = 1400;
93
- function injectStyle() {
94
+ function injectStyle(nonce) {
94
95
  if (document.getElementById(STYLE_ID))
95
96
  return;
96
97
  const style = document.createElement('style');
97
98
  style.id = STYLE_ID;
99
+ if (nonce)
100
+ style.nonce = nonce;
98
101
  style.textContent = `
99
102
  :where(${TAG}){display:block;position:relative;font-family:var(--baht-font-mono,ui-monospace,monospace)}
100
103
  :where(${TAG}) .bsg-stage{position:relative;width:100%;overflow:hidden}
@@ -157,6 +160,8 @@ function arrow(from, to) {
157
160
  * @attr hide-grid - Leaves out the floor grid.
158
161
  * @attr label - Accessible name of the diagram.
159
162
  * @attr interactive - Nodes are focusable buttons that fire `baht-node`.
163
+ * @attr nonce - The standard attribute: the `<style>` the graph adds (and its stickers' one) carries it, for a CSP
164
+ * `style-src` without `'unsafe-inline'`. Unset: `window.litNonce`.
160
165
  */
161
166
  export class BahtStickerGraph extends LitElement {
162
167
  static { this.properties = {
@@ -214,7 +219,7 @@ export class BahtStickerGraph extends LitElement {
214
219
  }
215
220
  connectedCallback() {
216
221
  super.connectedCallback();
217
- injectStyle();
222
+ injectStyle(styleNonce(this));
218
223
  if (typeof matchMedia !== 'undefined') {
219
224
  this.motion = matchMedia('(prefers-reduced-motion: reduce)');
220
225
  this.motion.addEventListener('change', this.wake);
@@ -0,0 +1,6 @@
1
+ /**
2
+ * The nonce a `<style>` an element of this package adds carries, for a page whose CSP `style-src`
3
+ * has no `'unsafe-inline'`: the `nonce` of the element or of the nearest ancestor that has one (a
4
+ * sticker inside a `<baht-sticker-graph nonce>`), else Lit's page-wide `window.litNonce`.
5
+ */
6
+ export declare function styleNonce(element: HTMLElement): string;
@@ -0,0 +1,17 @@
1
+ /**
2
+ * The nonce a `<style>` an element of this package adds carries, for a page whose CSP `style-src`
3
+ * has no `'unsafe-inline'`: the `nonce` of the element or of the nearest ancestor that has one (a
4
+ * sticker inside a `<baht-sticker-graph nonce>`), else Lit's page-wide `window.litNonce`.
5
+ */
6
+ export function styleNonce(element) {
7
+ for (let at = element; at; at = at.parentElement) {
8
+ // a browser hides the attribute once connected (the property keeps it); happy-dom has no property
9
+ if (at.nonce)
10
+ return at.nonce;
11
+ const attribute = at.getAttribute('nonce');
12
+ if (attribute)
13
+ return attribute;
14
+ }
15
+ const page = globalThis.litNonce;
16
+ return typeof page === 'string' ? page : '';
17
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@20baht/stickers",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "type": "module",
5
5
  "description": "Animated isometric slab stickers for bahtui: a <baht-sticker> Lit element that follows the --baht-* tokens and banknote themes, a growing set of illustrations, and the canvas runtime that plays them on After Effects keyframe presets.",
6
6
  "license": "MIT",