@pie-element/shared-utils 0.1.3-next.20261007075859 → 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.
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Markers for the colors an author wrote into model HTML, so a color scheme can override them.
3
+ *
4
+ * An inline `color` or `background-color` outranks the stylesheet that applies a color scheme,
5
+ * so authored ink and fills stay as written and can fall below WCAG contrast on a dark scheme.
6
+ * `sanitizeModelHtml` marks every element that carries one, and pie-players' scheme CSS
7
+ * overrides marked elements under `[data-color-scheme]`: ink takes the scheme's text color,
8
+ * borders its border color, and a fill follows `AUTHORED_FILL_ATTR`. (PIE-1119)
9
+ */
10
+ /** On an element with an authored text color: inline `color` or `<font color>`. */
11
+ export declare const AUTHORED_INK_ATTR = "data-pie-authored-ink";
12
+ /** On an element with an authored fill: inline `background-color` or `bgcolor`. */
13
+ export declare const AUTHORED_FILL_ATTR = "data-pie-authored-fill";
14
+ /** On an element with an authored border color on any side. */
15
+ export declare const AUTHORED_BORDER_ATTR = "data-pie-authored-border";
16
+ /**
17
+ * `light` is a near-white fill, usually the background of a page the text was pasted from, which
18
+ * a scheme drops. `shade` is any other fill, which marks a header, a highlight or a figure's
19
+ * shading, and which a scheme inverts so it keeps standing out.
20
+ */
21
+ export type AuthoredFill = 'light' | 'shade';
22
+ /** Channels from 0 to 255, unrounded, and alpha from 0 to 1. */
23
+ export interface Rgba {
24
+ r: number;
25
+ g: number;
26
+ b: number;
27
+ a: number;
28
+ }
29
+ /** Whether `value` is a color the author chose, as opposed to a keyword, a token or no paint. */
30
+ export declare function isAuthoredColor(value: string | null | undefined): boolean;
31
+ /**
32
+ * Classifies a fill composited over white. Light means relative luminance of at least 0.85 and
33
+ * chroma of at most 0.08, so `#eee` is light and `#ddd`, `lightgrey` and every hue are shade.
34
+ * A color this parser cannot read, such as `color-mix()`, counts as shade.
35
+ */
36
+ export declare function classifyAuthoredFill(value: string): AuthoredFill;
37
+ /**
38
+ * Sets the authored-color markers on `el` from its inline style and presentational attributes,
39
+ * replacing any markers already there, and drops `!important` from each marked declaration,
40
+ * because an inline `!important` beats the scheme stylesheet's.
41
+ */
42
+ export declare function markAuthoredColors(el: Element): void;
43
+ /**
44
+ * Parses a CSS color into sRGB: hex, named colors and `rgb()`, `hsl()`, `hwb()`, `lab()`,
45
+ * `lch()`, `oklab()` and `oklch()` in legacy and modern syntax, clipped to the sRGB gamut.
46
+ * Returns null for anything else, including `var()`, `calc()`, `color()` and system colors.
47
+ */
48
+ export declare function parseCssColor(value: string): Rgba | null;
package/dist/index.d.ts CHANGED
@@ -5,3 +5,4 @@
5
5
  */
6
6
  export * from './utils.js';
7
7
  export * from './sanitize-model-html.js';
8
+ export { AUTHORED_BORDER_ATTR, AUTHORED_FILL_ATTR, AUTHORED_INK_ATTR, markAuthoredColors, type AuthoredFill, } from './authored-colors.js';
package/dist/index.js CHANGED
@@ -58,8 +58,242 @@ function d(e, t) {
58
58
  return JSON.stringify(e) === JSON.stringify(t);
59
59
  }
60
60
  //#endregion
61
+ //#region src/authored-colors.ts
62
+ var f = "data-pie-authored-ink", p = "data-pie-authored-fill", m = "data-pie-authored-border", h = [
63
+ "border-top-color",
64
+ "border-right-color",
65
+ "border-bottom-color",
66
+ "border-left-color"
67
+ ], g = /* @__PURE__ */ new Set([
68
+ "currentcolor",
69
+ "inherit",
70
+ "initial",
71
+ "unset",
72
+ "revert",
73
+ "revert-layer",
74
+ "transparent"
75
+ ]);
76
+ function _(e) {
77
+ let t = e?.trim().toLowerCase();
78
+ return !t || g.has(t) || t.includes("var(") ? !1 : b(t)?.a !== 0;
79
+ }
80
+ function ee(e) {
81
+ let t = b(e);
82
+ if (!t) return "shade";
83
+ let n = (e) => (e * t.a + 255 * (1 - t.a)) / 255, [r, i, a] = [
84
+ n(t.r),
85
+ n(t.g),
86
+ n(t.b)
87
+ ], o = .2126 * B(r) + .7152 * B(i) + .0722 * B(a), s = Math.max(r, i, a) - Math.min(r, i, a);
88
+ return o >= .85 && s <= .08 ? "light" : "shade";
89
+ }
90
+ function v(e) {
91
+ e.removeAttribute(f), e.removeAttribute(p), e.removeAttribute(m);
92
+ let t = e.hasAttribute("style") ? e.style : void 0, n = (e) => t?.getPropertyValue(e).trim() ?? "", r = [], i = n("color"), a = e.localName === "font" ? e.getAttribute("color") : null;
93
+ _(i || a) && (e.setAttribute(f, ""), i && r.push("color"));
94
+ let o = n("background-color"), s = o || y(e.getAttribute("bgcolor"));
95
+ _(s) && (e.setAttribute(p, ee(s)), o && r.push("background-color"));
96
+ let c = h.filter((e) => _(n(e)));
97
+ c.length && (e.setAttribute(m, ""), r.push(...c));
98
+ for (let e of r) t?.getPropertyPriority(e) === "important" && t.setProperty(e, t.getPropertyValue(e));
99
+ }
100
+ function y(e) {
101
+ let t = e?.trim() ?? "";
102
+ return /^(?:[\da-f]{3}|[\da-f]{6})$/i.test(t) ? `#${t}` : t;
103
+ }
104
+ function b(e) {
105
+ let t = e.trim().toLowerCase();
106
+ if (t === "transparent") return {
107
+ r: 0,
108
+ g: 0,
109
+ b: 0,
110
+ a: 0
111
+ };
112
+ let n = S().get(t);
113
+ if (n) return C(n);
114
+ if (t.startsWith("#")) return C(t.slice(1));
115
+ let r = /^([a-z]+)\(([^()]*)\)$/.exec(t);
116
+ if (!r) return null;
117
+ let i = r[2].trim().split(/\s*[\s,/]\s*/);
118
+ if (i.length !== 3 && i.length !== 4) return null;
119
+ let [a, o, s, c] = i, l = D(c), u;
120
+ switch (r[1]) {
121
+ case "rgb":
122
+ case "rgba":
123
+ u = [
124
+ a,
125
+ o,
126
+ s
127
+ ].map((e) => T(e, 255) / 255);
128
+ break;
129
+ case "hsl":
130
+ case "hsla":
131
+ u = k(E(a), T(o, 100) / 100, T(s, 100) / 100);
132
+ break;
133
+ case "hwb":
134
+ u = A(E(a), T(o, 100) / 100, T(s, 100) / 100);
135
+ break;
136
+ case "lab":
137
+ u = I(T(a, 100), T(o, 125), T(s, 125));
138
+ break;
139
+ case "lch":
140
+ u = I(T(a, 100), ...O(T(o, 150), E(s)));
141
+ break;
142
+ case "oklab":
143
+ u = L(T(a, 1), T(o, .4), T(s, .4));
144
+ break;
145
+ case "oklch":
146
+ u = L(T(a, 1), ...O(T(o, .4), E(s)));
147
+ break;
148
+ default: return null;
149
+ }
150
+ if (Number.isNaN(l) || u.some(Number.isNaN)) return null;
151
+ let [d, f, p] = u.map((e) => V(e, 0, 1) * 255);
152
+ return {
153
+ r: d,
154
+ g: f,
155
+ b: p,
156
+ a: l
157
+ };
158
+ }
159
+ var x;
160
+ function S() {
161
+ return x ??= new Map("aliceblue:f0f8ff antiquewhite:faebd7 aqua:00ffff aquamarine:7fffd4 azure:f0ffff beige:f5f5dc bisque:ffe4c4 black:000000 blanchedalmond:ffebcd blue:0000ff blueviolet:8a2be2 brown:a52a2a burlywood:deb887 cadetblue:5f9ea0 chartreuse:7fff00 chocolate:d2691e coral:ff7f50 cornflowerblue:6495ed cornsilk:fff8dc crimson:dc143c cyan:00ffff darkblue:00008b darkcyan:008b8b darkgoldenrod:b8860b darkgray:a9a9a9 darkgreen:006400 darkgrey:a9a9a9 darkkhaki:bdb76b darkmagenta:8b008b darkolivegreen:556b2f darkorange:ff8c00 darkorchid:9932cc darkred:8b0000 darksalmon:e9967a darkseagreen:8fbc8f darkslateblue:483d8b darkslategray:2f4f4f darkslategrey:2f4f4f darkturquoise:00ced1 darkviolet:9400d3 deeppink:ff1493 deepskyblue:00bfff dimgray:696969 dimgrey:696969 dodgerblue:1e90ff firebrick:b22222 floralwhite:fffaf0 forestgreen:228b22 fuchsia:ff00ff gainsboro:dcdcdc ghostwhite:f8f8ff gold:ffd700 goldenrod:daa520 gray:808080 green:008000 greenyellow:adff2f grey:808080 honeydew:f0fff0 hotpink:ff69b4 indianred:cd5c5c indigo:4b0082 ivory:fffff0 khaki:f0e68c lavender:e6e6fa lavenderblush:fff0f5 lawngreen:7cfc00 lemonchiffon:fffacd lightblue:add8e6 lightcoral:f08080 lightcyan:e0ffff lightgoldenrodyellow:fafad2 lightgray:d3d3d3 lightgreen:90ee90 lightgrey:d3d3d3 lightpink:ffb6c1 lightsalmon:ffa07a lightseagreen:20b2aa lightskyblue:87cefa lightslategray:778899 lightslategrey:778899 lightsteelblue:b0c4de lightyellow:ffffe0 lime:00ff00 limegreen:32cd32 linen:faf0e6 magenta:ff00ff maroon:800000 mediumaquamarine:66cdaa mediumblue:0000cd mediumorchid:ba55d3 mediumpurple:9370db mediumseagreen:3cb371 mediumslateblue:7b68ee mediumspringgreen:00fa9a mediumturquoise:48d1cc mediumvioletred:c71585 midnightblue:191970 mintcream:f5fffa mistyrose:ffe4e1 moccasin:ffe4b5 navajowhite:ffdead navy:000080 oldlace:fdf5e6 olive:808000 olivedrab:6b8e23 orange:ffa500 orangered:ff4500 orchid:da70d6 palegoldenrod:eee8aa palegreen:98fb98 paleturquoise:afeeee palevioletred:db7093 papayawhip:ffefd5 peachpuff:ffdab9 peru:cd853f pink:ffc0cb plum:dda0dd powderblue:b0e0e6 purple:800080 rebeccapurple:663399 red:ff0000 rosybrown:bc8f8f royalblue:4169e1 saddlebrown:8b4513 salmon:fa8072 sandybrown:f4a460 seagreen:2e8b57 seashell:fff5ee sienna:a0522d silver:c0c0c0 skyblue:87ceeb slateblue:6a5acd slategray:708090 slategrey:708090 snow:fffafa springgreen:00ff7f steelblue:4682b4 tan:d2b48c teal:008080 thistle:d8bfd8 tomato:ff6347 turquoise:40e0d0 violet:ee82ee wheat:f5deb3 white:ffffff whitesmoke:f5f5f5 yellow:ffff00 yellowgreen:9acd32".split(" ").map((e) => e.split(":"))), x;
162
+ }
163
+ function C(e) {
164
+ if (!/^(?:[\da-f]{3,4}|[\da-f]{6}|[\da-f]{8})$/.test(e)) return null;
165
+ let [t, n, r, i = 255] = (e.length <= 4 ? [...e].map((e) => e + e) : e.match(/../g) ?? []).map((e) => Number.parseInt(e, 16));
166
+ return {
167
+ r: t,
168
+ g: n,
169
+ b: r,
170
+ a: i / 255
171
+ };
172
+ }
173
+ var w = /^([+-]?(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?)(%|deg|rad|grad|turn)?$/;
174
+ function T(e, t) {
175
+ if (e === "none") return 0;
176
+ let n = w.exec(e);
177
+ return !n || n[2] && n[2] !== "%" ? NaN : n[2] ? Number(n[1]) / 100 * t : Number(n[1]);
178
+ }
179
+ function E(e) {
180
+ if (e === "none") return 0;
181
+ let t = w.exec(e);
182
+ if (!t || t[2] === "%") return NaN;
183
+ let n = Number(t[1]);
184
+ return ((t[2] === "rad" ? n * 180 / Math.PI : t[2] === "grad" ? n * .9 : t[2] === "turn" ? n * 360 : n) % 360 + 360) % 360;
185
+ }
186
+ function D(e) {
187
+ return e === void 0 ? 1 : V(T(e, 1), 0, 1);
188
+ }
189
+ function O(e, t) {
190
+ let n = Math.max(0, e), r = t * Math.PI / 180;
191
+ return [n * Math.cos(r), n * Math.sin(r)];
192
+ }
193
+ function k(e, t, n) {
194
+ let r = V(t, 0, 1), i = V(n, 0, 1), a = (t) => {
195
+ let n = (t + e / 30) % 12, a = r * Math.min(i, 1 - i);
196
+ return i - a * Math.max(-1, Math.min(n - 3, 9 - n, 1));
197
+ };
198
+ return [
199
+ a(0),
200
+ a(8),
201
+ a(4)
202
+ ];
203
+ }
204
+ function A(e, t, n) {
205
+ let r = V(t, 0, 1), i = V(n, 0, 1);
206
+ return r + i >= 1 ? [
207
+ ,
208
+ ,
209
+ ,
210
+ ].fill(r / (r + i)) : k(e, 1, .5).map((e) => e * (1 - r - i) + r);
211
+ }
212
+ var j = 24389 / 27, M = 216 / 24389, N = [
213
+ .3457 / .3585,
214
+ 1,
215
+ .2958 / .3585
216
+ ], P = [
217
+ [
218
+ .955473421488075,
219
+ -.02309845494876471,
220
+ .06325924320057072
221
+ ],
222
+ [
223
+ -.0283697093338637,
224
+ 1.0099953980813041,
225
+ .021041441191917323
226
+ ],
227
+ [
228
+ .012314014864481998,
229
+ -.020507649298898964,
230
+ 1.330365926242124
231
+ ]
232
+ ], F = [
233
+ [
234
+ 12831 / 3959,
235
+ -329 / 214,
236
+ -1974 / 3959
237
+ ],
238
+ [
239
+ -851781 / 878810,
240
+ 1648619 / 878810,
241
+ 36519 / 878810
242
+ ],
243
+ [
244
+ 705 / 12673,
245
+ -2585 / 12673,
246
+ 705 / 667
247
+ ]
248
+ ];
249
+ function I(e, t, n) {
250
+ let r = V(e, 0, 100), i = (r + 16) / 116, a = t / 500 + i, o = i - n / 200;
251
+ return R(F, R(P, [
252
+ a ** 3 > M ? a ** 3 : (116 * a - 16) / j,
253
+ r > j * M ? i ** 3 : r / j,
254
+ o ** 3 > M ? o ** 3 : (116 * o - 16) / j
255
+ ].map((e, t) => e * N[t]))).map(z);
256
+ }
257
+ function L(e, t, n) {
258
+ let r = V(e, 0, 1);
259
+ return R([
260
+ [
261
+ 4.0767416621,
262
+ -3.3077115913,
263
+ .2309699292
264
+ ],
265
+ [
266
+ -1.2684380046,
267
+ 2.6097574011,
268
+ -.3413193965
269
+ ],
270
+ [
271
+ -.0041960863,
272
+ -.7034186147,
273
+ 1.707614701
274
+ ]
275
+ ], [
276
+ r + .3963377774 * t + .2158037573 * n,
277
+ r - .1055613458 * t - .0638541728 * n,
278
+ r - .0894841775 * t - 1.291485548 * n
279
+ ].map((e) => e ** 3)).map(z);
280
+ }
281
+ function R(e, t) {
282
+ return e.map((e) => e[0] * t[0] + e[1] * t[1] + e[2] * t[2]);
283
+ }
284
+ function z(e) {
285
+ let t = V(e, 0, 1);
286
+ return t <= .0031308 ? 12.92 * t : 1.055 * t ** (1 / 2.4) - .055;
287
+ }
288
+ function B(e) {
289
+ return e <= .04045 ? e / 12.92 : ((e + .055) / 1.055) ** 2.4;
290
+ }
291
+ function V(e, t, n) {
292
+ return Math.min(n, Math.max(t, e));
293
+ }
294
+ //#endregion
61
295
  //#region src/sanitize-model-html.ts
62
- var f = [
296
+ var H = [
63
297
  "script",
64
298
  "embed",
65
299
  "base",
@@ -68,7 +302,7 @@ var f = [
68
302
  "link",
69
303
  "style",
70
304
  "foreignobject"
71
- ], p = [
305
+ ], U = [
72
306
  "onerror",
73
307
  "onload",
74
308
  "onclick",
@@ -86,7 +320,7 @@ var f = [
86
320
  "onbeforeunload",
87
321
  "formaction",
88
322
  "xlink:href"
89
- ], m = /* @__PURE__ */ new Set([
323
+ ], W = /* @__PURE__ */ new Set([
90
324
  "iframe",
91
325
  "object",
92
326
  "mstack",
@@ -99,7 +333,7 @@ var f = [
99
333
  "semantics",
100
334
  "annotation",
101
335
  "none"
102
- ]), h = /^[a-z_][\w.-]*:[a-z][\w.-]*$/, g = [
336
+ ]), G = /^[a-z_][\w.-]*:[a-z][\w.-]*$/, K = [
103
337
  "alignment",
104
338
  "target",
105
339
  "allow",
@@ -119,18 +353,18 @@ var f = [
119
353
  "rightoverhang",
120
354
  "mslinethickness",
121
355
  "linebreak"
122
- ], _ = /^\s*document\.getElementById\((['"])([^'"]+)\1\)\.play\(\)\s*;?\s*(?:return\s+false\s*;?\s*)?$/, v = "data-pie-play-audio", y = {
123
- ADD_TAGS: (e) => m.has(e) || h.test(e),
124
- ADD_ATTR: g,
125
- FORBID_TAGS: f,
126
- FORBID_ATTR: p,
356
+ ], q = /^\s*document\.getElementById\((['"])([^'"]+)\1\)\.play\(\)\s*;?\s*(?:return\s+false\s*;?\s*)?$/, J = "data-pie-play-audio", Y = {
357
+ ADD_TAGS: (e) => W.has(e) || G.test(e),
358
+ ADD_ATTR: K,
359
+ FORBID_TAGS: H,
360
+ FORBID_ATTR: U,
127
361
  ALLOW_UNKNOWN_PROTOCOLS: !1,
128
362
  RETURN_TRUSTED_TYPE: !1
129
- }, b = !1;
130
- function x(e) {
363
+ }, te = !1;
364
+ function ne(e) {
131
365
  for (let t of e.composedPath()) {
132
366
  if (!(t instanceof Element)) continue;
133
- let n = t.getAttribute(v);
367
+ let n = t.getAttribute(J);
134
368
  if (n === null) continue;
135
369
  e.preventDefault();
136
370
  let r = t.getRootNode().getElementById?.(n) ?? document.getElementById(n);
@@ -138,26 +372,26 @@ function x(e) {
138
372
  return;
139
373
  }
140
374
  }
141
- var S = null;
142
- function C() {
143
- return S || (S = e(window), S.addHook("beforeSanitizeAttributes", (e) => {
375
+ var X = null;
376
+ function re() {
377
+ return X || (X = e(window), X.addHook("beforeSanitizeAttributes", (e) => {
144
378
  if (e.nodeName !== "A") return;
145
- let t = _.exec(e.getAttribute("onclick") ?? "")?.[2];
146
- t !== void 0 && (e.removeAttribute("onclick"), e.setAttribute(v, t), b ||= (document.addEventListener("click", x), !0));
147
- }), S);
379
+ let t = q.exec(e.getAttribute("onclick") ?? "")?.[2];
380
+ t !== void 0 && (e.removeAttribute("onclick"), e.setAttribute(J, t), te ||= (document.addEventListener("click", ne), !0));
381
+ }), X.addHook("afterSanitizeAttributes", v), X);
148
382
  }
149
- var w = 1e6, T = /* @__PURE__ */ new Map(), E = 0;
150
- function D(t) {
383
+ var Z = 1e6, Q = /* @__PURE__ */ new Map(), $ = 0;
384
+ function ie(t) {
151
385
  if (t == null || t === "" || !e.isSupported) return "";
152
- let n = typeof t == "string" ? t : String(t), r = T.get(n);
386
+ let n = typeof t == "string" ? t : String(t), r = Q.get(n);
153
387
  if (r !== void 0) return r;
154
- let i = String(C().sanitize(n, y)), a = n.length + i.length;
155
- if (a > w) return i;
156
- for (let [e, t] of T) {
157
- if (E + a <= w) break;
158
- T.delete(e), E -= e.length + t.length;
388
+ let i = String(re().sanitize(n, Y)), a = n.length + i.length;
389
+ if (a > Z) return i;
390
+ for (let [e, t] of Q) {
391
+ if ($ + a <= Z) break;
392
+ Q.delete(e), $ -= e.length + t.length;
159
393
  }
160
- return T.set(n, i), E += a, i;
394
+ return Q.set(n, i), $ += a, i;
161
395
  }
162
396
  //#endregion
163
- export { p as MODEL_HTML_FORBIDDEN_ATTRS, f as MODEL_HTML_FORBIDDEN_TAGS, v as PLAY_AUDIO_ATTR, u as assignProps, a as clamp, s as debounce, t as debug, n as isEmpty, D as sanitizeModelHtml, d as sessionsEqual, r as showFeedback, i as showRationale, c as shuffle, o as uuid };
397
+ export { m as AUTHORED_BORDER_ATTR, p as AUTHORED_FILL_ATTR, f as AUTHORED_INK_ATTR, U as MODEL_HTML_FORBIDDEN_ATTRS, H as MODEL_HTML_FORBIDDEN_TAGS, J as PLAY_AUDIO_ATTR, u as assignProps, a as clamp, s as debounce, t as debug, n as isEmpty, v as markAuthoredColors, ie as sanitizeModelHtml, d as sessionsEqual, r as showFeedback, i as showRationale, c as shuffle, o as uuid };
@@ -17,6 +17,8 @@ export declare const PLAY_AUDIO_ATTR = "data-pie-play-audio";
17
17
  /**
18
18
  * Returns `html` with scripts, event handlers, `javascript:` URLs and the forbidden tags and
19
19
  * attributes removed. A link that only plays an `<audio>` keeps working; see `PLAY_AUDIO_ATTR`.
20
+ * Elements with an authored color carry the markers a color scheme overrides them by; see
21
+ * `markAuthoredColors`.
20
22
  * Returns an empty string where there is no DOM to sanitize against.
21
23
  *
22
24
  * Takes any value because model fields are not always strings: a number renders as its text,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pie-element/shared-utils",
3
- "version": "0.1.3-next.20261007075859",
3
+ "version": "0.1.3",
4
4
  "description": "Shared utilities for PIE elements",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -21,7 +21,7 @@
21
21
  "clean": "rm -rf dist"
22
22
  },
23
23
  "dependencies": {
24
- "@pie-element/shared-types": "0.2.1-next.20261002215153",
24
+ "@pie-element/shared-types": "0.2.0",
25
25
  "dompurify": "^3.4.16"
26
26
  },
27
27
  "publishConfig": {