@pie-element/shared-utils 0.1.2-next.20261002215153 → 0.1.3-next.20261007064237

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/dist/index.d.ts CHANGED
@@ -4,3 +4,4 @@
4
4
  * Framework-agnostic utility functions for PIE elements.
5
5
  */
6
6
  export * from './utils.js';
7
+ export * from './sanitize-model-html.js';
package/dist/index.js CHANGED
@@ -1,48 +1,49 @@
1
+ import e from "dompurify";
1
2
  //#region src/utils.ts
2
- function e(e) {
3
+ function t(e) {
3
4
  let t = typeof process < "u" && process.env?.DEBUG?.includes(e);
4
5
  return (...n) => {
5
6
  t && console.log(`[${e}]`, ...n);
6
7
  };
7
8
  }
8
- function t(e) {
9
+ function n(e) {
9
10
  return !e || typeof e == "object" && Object.keys(e).length === 0;
10
11
  }
11
- function n(e, t) {
12
+ function r(e, t) {
12
13
  return e.mode === "evaluate" && t;
13
14
  }
14
- function r(e, t) {
15
+ function i(e, t) {
15
16
  return e.mode === "evaluate" && t && e.role === "instructor";
16
17
  }
17
- function i(e, t, n) {
18
+ function a(e, t, n) {
18
19
  return Math.min(Math.max(e, t), n);
19
20
  }
20
- function a() {
21
+ function o() {
21
22
  return crypto.randomUUID();
22
23
  }
23
- function o(e, t) {
24
+ function s(e, t) {
24
25
  let n;
25
26
  return (...r) => {
26
27
  clearTimeout(n), n = setTimeout(() => e(...r), t);
27
28
  };
28
29
  }
29
- function s(e, t) {
30
- let n = [...e], r = t ? c(t) : Math.random;
30
+ function c(e, t) {
31
+ let n = [...e], r = t ? l(t) : Math.random;
31
32
  for (let e = n.length - 1; e > 0; e--) {
32
33
  let t = Math.floor(r() * (e + 1));
33
34
  [n[e], n[t]] = [n[t], n[e]];
34
35
  }
35
36
  return n;
36
37
  }
37
- function c(e) {
38
+ function l(e) {
38
39
  let t = e;
39
40
  return () => (t = (t * 9301 + 49297) % 233280, t / 233280);
40
41
  }
41
- function l(e, t, n = {}) {
42
+ function u(e, t, n = {}) {
42
43
  let { skipUndefined: r = !0 } = n;
43
44
  for (let [n, i] of Object.entries(t)) r && i === void 0 || (e[n] = i);
44
45
  }
45
- function u(e, t) {
46
+ function d(e, t) {
46
47
  if (e === t) return !0;
47
48
  if (!e || !t || typeof e != "object" || typeof t != "object") return e === t;
48
49
  let n = e, r = t;
@@ -57,4 +58,67 @@ function u(e, t) {
57
58
  return JSON.stringify(e) === JSON.stringify(t);
58
59
  }
59
60
  //#endregion
60
- export { l as assignProps, i as clamp, o as debounce, e as debug, t as isEmpty, u as sessionsEqual, n as showFeedback, r as showRationale, s as shuffle, a as uuid };
61
+ //#region src/sanitize-model-html.ts
62
+ var f = [
63
+ "script",
64
+ "embed",
65
+ "base",
66
+ "form",
67
+ "meta",
68
+ "link",
69
+ "style",
70
+ "foreignobject"
71
+ ], p = [
72
+ "onerror",
73
+ "onload",
74
+ "onclick",
75
+ "onmouseover",
76
+ "onmouseout",
77
+ "onmouseenter",
78
+ "onmouseleave",
79
+ "onfocus",
80
+ "onblur",
81
+ "onkeydown",
82
+ "onkeyup",
83
+ "onkeypress",
84
+ "onsubmit",
85
+ "onchange",
86
+ "onbeforeunload",
87
+ "formaction",
88
+ "xlink:href"
89
+ ], m = /* @__PURE__ */ new Set([
90
+ "iframe",
91
+ "object",
92
+ "semantics",
93
+ "annotation",
94
+ "none"
95
+ ]), h = /^[a-z_][\w.-]*:[a-z][\w.-]*$/, g = {
96
+ ADD_TAGS: (e) => m.has(e) || h.test(e),
97
+ ADD_ATTR: [
98
+ "alignment",
99
+ "target",
100
+ "allow",
101
+ "allowfullscreen",
102
+ "frameborder",
103
+ "scrolling",
104
+ "data"
105
+ ],
106
+ FORBID_TAGS: f,
107
+ FORBID_ATTR: p,
108
+ ALLOW_UNKNOWN_PROTOCOLS: !1,
109
+ RETURN_TRUSTED_TYPE: !1
110
+ }, _ = 1e6, v = /* @__PURE__ */ new Map(), y = 0;
111
+ function b(t) {
112
+ if (t == null || t === "" || !e.isSupported) return "";
113
+ let n = typeof t == "string" ? t : String(t), r = v.get(n);
114
+ if (r !== void 0) return r;
115
+ let i = String(e.sanitize(n, g)), a = n.length + i.length;
116
+ if (a > _) return i;
117
+ for (let [e, t] of v) {
118
+ if (y + a <= _) break;
119
+ v.delete(e), y -= e.length + t.length;
120
+ }
121
+ return v.set(n, i), y += a, i;
122
+ }
123
+ //#endregion
124
+ export { p as MODEL_HTML_FORBIDDEN_ATTRS, f as MODEL_HTML_FORBIDDEN_TAGS, u as assignProps, a as clamp, s as debounce, t as debug, n as isEmpty, b as sanitizeModelHtml, d as sessionsEqual, r as showFeedback, i as showRationale, c as shuffle, o as uuid };
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Sanitizer for the rich-text fields of an element model: prompts, choice labels, feedback,
3
+ * rationale, teacher instructions and the markup cloze elements build blanks from.
4
+ *
5
+ * Players pass models to elements verbatim and sanitize only the item's own markup, so every
6
+ * element sink that writes model HTML into the DOM runs it through `sanitizeModelHtml` first.
7
+ *
8
+ * The forbid-lists are pie-players' `sanitize-forbidden-lists.ts` without `<iframe>` and
9
+ * `<object>`: production prompts embed videos and simulations in `<iframe>` and item-bank SVG
10
+ * images in `<object>`. DOMPurify's URI check confines both to http(s) and relative URLs, and
11
+ * `srcdoc`, `classid` and `codebase` are not allowed attributes.
12
+ * https://github.com/pie-framework/pie-players/blob/develop/packages/players-shared/src/security/sanitize-forbidden-lists.ts
13
+ */
14
+ export declare const MODEL_HTML_FORBIDDEN_TAGS: string[];
15
+ export declare const MODEL_HTML_FORBIDDEN_ATTRS: string[];
16
+ /**
17
+ * Returns `html` with scripts, event handlers, `javascript:` URLs and the forbidden tags and
18
+ * attributes removed. Returns an empty string where there is no DOM to sanitize against.
19
+ *
20
+ * Takes any value because model fields are not always strings: a number renders as its text,
21
+ * as it did when passed to `innerHTML` directly.
22
+ */
23
+ export declare function sanitizeModelHtml(value: unknown): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pie-element/shared-utils",
3
- "version": "0.1.2-next.20261002215153",
3
+ "version": "0.1.3-next.20261007064237",
4
4
  "description": "Shared utilities for PIE elements",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -21,7 +21,8 @@
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.1-next.20261002215153",
25
+ "dompurify": "^3.4.16"
25
26
  },
26
27
  "publishConfig": {
27
28
  "access": "public"