@astl/core 0.0.0 → 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.
Files changed (139) hide show
  1. package/CHANGELOG.md +54 -0
  2. package/LICENSE +202 -0
  3. package/README.md +38 -0
  4. package/dist/answers.d.ts +29 -0
  5. package/dist/answers.d.ts.map +1 -0
  6. package/dist/answers.js +40 -0
  7. package/dist/answers.js.map +1 -0
  8. package/dist/ast.d.ts +25 -0
  9. package/dist/ast.d.ts.map +1 -0
  10. package/dist/ast.js +27 -0
  11. package/dist/ast.js.map +1 -0
  12. package/dist/authoring.d.ts +451 -0
  13. package/dist/authoring.d.ts.map +1 -0
  14. package/dist/authoring.js +2 -0
  15. package/dist/authoring.js.map +1 -0
  16. package/dist/checker.d.ts +78 -0
  17. package/dist/checker.d.ts.map +1 -0
  18. package/dist/checker.js +10 -0
  19. package/dist/checker.js.map +1 -0
  20. package/dist/compiled.d.ts +94 -0
  21. package/dist/compiled.d.ts.map +1 -0
  22. package/dist/compiled.js +16 -0
  23. package/dist/compiled.js.map +1 -0
  24. package/dist/content.d.ts +72 -0
  25. package/dist/content.d.ts.map +1 -0
  26. package/dist/content.js +128 -0
  27. package/dist/content.js.map +1 -0
  28. package/dist/declarations.d.ts +52 -0
  29. package/dist/declarations.d.ts.map +1 -0
  30. package/dist/declarations.js +358 -0
  31. package/dist/declarations.js.map +1 -0
  32. package/dist/definition.d.ts +106 -0
  33. package/dist/definition.d.ts.map +1 -0
  34. package/dist/definition.js +2 -0
  35. package/dist/definition.js.map +1 -0
  36. package/dist/engine.d.ts +95 -0
  37. package/dist/engine.d.ts.map +1 -0
  38. package/dist/engine.js +2 -0
  39. package/dist/engine.js.map +1 -0
  40. package/dist/format.d.ts +29 -0
  41. package/dist/format.d.ts.map +1 -0
  42. package/dist/format.js +55 -0
  43. package/dist/format.js.map +1 -0
  44. package/dist/help.d.ts +38 -0
  45. package/dist/help.d.ts.map +1 -0
  46. package/dist/help.js +22 -0
  47. package/dist/help.js.map +1 -0
  48. package/dist/i18n/check.d.ts +11 -0
  49. package/dist/i18n/check.d.ts.map +1 -0
  50. package/dist/i18n/check.js +64 -0
  51. package/dist/i18n/check.js.map +1 -0
  52. package/dist/i18n/format.d.ts +40 -0
  53. package/dist/i18n/format.d.ts.map +1 -0
  54. package/dist/i18n/format.js +280 -0
  55. package/dist/i18n/format.js.map +1 -0
  56. package/dist/i18n/i18n.d.ts +14 -0
  57. package/dist/i18n/i18n.d.ts.map +1 -0
  58. package/dist/i18n/i18n.js +82 -0
  59. package/dist/i18n/i18n.js.map +1 -0
  60. package/dist/i18n/types.d.ts +31 -0
  61. package/dist/i18n/types.d.ts.map +1 -0
  62. package/dist/i18n/types.js +7 -0
  63. package/dist/i18n/types.js.map +1 -0
  64. package/dist/index.d.ts +45 -0
  65. package/dist/index.d.ts.map +1 -0
  66. package/dist/index.js +21 -0
  67. package/dist/index.js.map +1 -0
  68. package/dist/issues.d.ts +98 -0
  69. package/dist/issues.d.ts.map +1 -0
  70. package/dist/issues.js +74 -0
  71. package/dist/issues.js.map +1 -0
  72. package/dist/json.d.ts +7 -0
  73. package/dist/json.d.ts.map +1 -0
  74. package/dist/json.js +2 -0
  75. package/dist/json.js.map +1 -0
  76. package/dist/manifest.d.ts +135 -0
  77. package/dist/manifest.d.ts.map +1 -0
  78. package/dist/manifest.js +63 -0
  79. package/dist/manifest.js.map +1 -0
  80. package/dist/player.d.ts +129 -0
  81. package/dist/player.d.ts.map +1 -0
  82. package/dist/player.js +18 -0
  83. package/dist/player.js.map +1 -0
  84. package/dist/policy.d.ts +53 -0
  85. package/dist/policy.d.ts.map +1 -0
  86. package/dist/policy.js +55 -0
  87. package/dist/policy.js.map +1 -0
  88. package/dist/protocol.d.ts +54 -0
  89. package/dist/protocol.d.ts.map +1 -0
  90. package/dist/protocol.js +51 -0
  91. package/dist/protocol.js.map +1 -0
  92. package/dist/result.d.ts +44 -0
  93. package/dist/result.d.ts.map +1 -0
  94. package/dist/result.js +5 -0
  95. package/dist/result.js.map +1 -0
  96. package/dist/scoring.d.ts +27 -0
  97. package/dist/scoring.d.ts.map +1 -0
  98. package/dist/scoring.js +6 -0
  99. package/dist/scoring.js.map +1 -0
  100. package/dist/slots.d.ts +47 -0
  101. package/dist/slots.d.ts.map +1 -0
  102. package/dist/slots.js +9 -0
  103. package/dist/slots.js.map +1 -0
  104. package/dist/snapshot.d.ts +23 -0
  105. package/dist/snapshot.d.ts.map +1 -0
  106. package/dist/snapshot.js +22 -0
  107. package/dist/snapshot.js.map +1 -0
  108. package/dist/style.d.ts +51 -0
  109. package/dist/style.d.ts.map +1 -0
  110. package/dist/style.js +36 -0
  111. package/dist/style.js.map +1 -0
  112. package/package.json +40 -4
  113. package/src/answers.ts +52 -0
  114. package/src/ast.ts +49 -0
  115. package/src/authoring.ts +475 -0
  116. package/src/checker.ts +91 -0
  117. package/src/compiled.ts +115 -0
  118. package/src/content.ts +156 -0
  119. package/src/declarations.ts +427 -0
  120. package/src/definition.ts +104 -0
  121. package/src/engine.ts +98 -0
  122. package/src/format.ts +67 -0
  123. package/src/help.ts +41 -0
  124. package/src/i18n/check.ts +67 -0
  125. package/src/i18n/format.ts +318 -0
  126. package/src/i18n/i18n.ts +92 -0
  127. package/src/i18n/types.ts +35 -0
  128. package/src/index.ts +201 -0
  129. package/src/issues.ts +107 -0
  130. package/src/json.ts +6 -0
  131. package/src/manifest.ts +193 -0
  132. package/src/player.ts +145 -0
  133. package/src/policy.ts +117 -0
  134. package/src/protocol.ts +65 -0
  135. package/src/result.ts +53 -0
  136. package/src/scoring.ts +31 -0
  137. package/src/slots.ts +57 -0
  138. package/src/snapshot.ts +40 -0
  139. package/src/style.ts +89 -0
package/dist/slots.js ADDED
@@ -0,0 +1,9 @@
1
+ export const DEFAULT_PROFILE = {
2
+ form: 'NFC',
3
+ space: 'collapse',
4
+ case: 'insensitive',
5
+ marks: 'strict',
6
+ punctuation: 'ignore-edge',
7
+ quotes: 'fold',
8
+ };
9
+ //# sourceMappingURL=slots.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"slots.js","sourceRoot":"","sources":["../src/slots.ts"],"names":[],"mappings":"AAiDA,MAAM,CAAC,MAAM,eAAe,GAAyB;IACnD,IAAI,EAAE,KAAK;IACX,KAAK,EAAE,UAAU;IACjB,IAAI,EAAE,aAAa;IACnB,KAAK,EAAE,QAAQ;IACf,WAAW,EAAE,aAAa;IAC1B,MAAM,EAAE,MAAM;CACf,CAAC"}
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Snapshot: everything needed to restore an attempt in the player after a reload. Hosts store it
3
+ * (localStorage, server draft); the player emits it and accepts it back.
4
+ */
5
+ import { type Answers } from './answers.js';
6
+ import type { JsonValue } from './json.js';
7
+ export declare const SNAPSHOT_FORMAT = 1;
8
+ export interface Snapshot {
9
+ readonly format: typeof SNAPSHOT_FORMAT;
10
+ /** View the snapshot was taken on. A player ignores snapshots of another view. */
11
+ readonly viewHash: string | null;
12
+ readonly answers: Answers;
13
+ /** 0-based page index. */
14
+ readonly page: number;
15
+ readonly checks: number;
16
+ readonly submitted: boolean;
17
+ /** Non-answer UI state per element id (e.g. a flashcard index). */
18
+ readonly ui: Readonly<Record<string, JsonValue>>;
19
+ /** ISO 8601. */
20
+ readonly savedAt: string;
21
+ }
22
+ export declare function isSnapshot(value: unknown): value is Snapshot;
23
+ //# sourceMappingURL=snapshot.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"snapshot.d.ts","sourceRoot":"","sources":["../src/snapshot.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,KAAK,OAAO,EAAa,MAAM,cAAc,CAAC;AACvD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAE3C,eAAO,MAAM,eAAe,IAAI,CAAC;AAEjC,MAAM,WAAW,QAAQ;IACvB,QAAQ,CAAC,MAAM,EAAE,OAAO,eAAe,CAAC;IACxC,kFAAkF;IAClF,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACjC,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,0BAA0B;IAC1B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAC;IAC5B,mEAAmE;IACnE,QAAQ,CAAC,EAAE,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC;IACjD,gBAAgB;IAChB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CAC1B;AAED,wBAAgB,UAAU,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,QAAQ,CAe5D"}
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Snapshot: everything needed to restore an attempt in the player after a reload. Hosts store it
3
+ * (localStorage, server draft); the player emits it and accepts it back.
4
+ */
5
+ import { isAnswers } from './answers.js';
6
+ export const SNAPSHOT_FORMAT = 1;
7
+ export function isSnapshot(value) {
8
+ if (typeof value !== 'object' || value === null)
9
+ return false;
10
+ const v = value;
11
+ return (v.format === SNAPSHOT_FORMAT &&
12
+ (v.viewHash === null || typeof v.viewHash === 'string') &&
13
+ isAnswers(v.answers) &&
14
+ Number.isInteger(v.page) &&
15
+ (v.page ?? -1) >= 0 &&
16
+ Number.isInteger(v.checks) &&
17
+ typeof v.submitted === 'boolean' &&
18
+ typeof v.ui === 'object' &&
19
+ v.ui !== null &&
20
+ typeof v.savedAt === 'string');
21
+ }
22
+ //# sourceMappingURL=snapshot.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"snapshot.js","sourceRoot":"","sources":["../src/snapshot.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAgB,SAAS,EAAE,MAAM,cAAc,CAAC;AAGvD,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,CAAC;AAiBjC,MAAM,UAAU,UAAU,CAAC,KAAc;IACvC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI;QAAE,OAAO,KAAK,CAAC;IAC9D,MAAM,CAAC,GAAG,KAA0B,CAAC;IACrC,OAAO,CACL,CAAC,CAAC,MAAM,KAAK,eAAe;QAC5B,CAAC,CAAC,CAAC,QAAQ,KAAK,IAAI,IAAI,OAAO,CAAC,CAAC,QAAQ,KAAK,QAAQ,CAAC;QACvD,SAAS,CAAC,CAAC,CAAC,OAAO,CAAC;QACpB,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC;QACxB,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC;QACnB,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC;QAC1B,OAAO,CAAC,CAAC,SAAS,KAAK,SAAS;QAChC,OAAO,CAAC,CAAC,EAAE,KAAK,QAAQ;QACxB,CAAC,CAAC,EAAE,KAAK,IAAI;QACb,OAAO,CAAC,CAAC,OAAO,KAAK,QAAQ,CAC9B,CAAC;AACJ,CAAC"}
@@ -0,0 +1,51 @@
1
+ /**
2
+ * StyleTemplate: a named look for a whole exercise or for single components, registered by the host (trusted code,
3
+ * like the host's own CSS; exercises only refer to templates by name). Authors pick one with
4
+ * `style-template="<name>"` on `<astl-doc>` or on a component. The closest one wins: the component's, then the
5
+ * document's, then the player's default. A template restyles through design tokens and `::part()` declarations only:
6
+ * it never changes behaviour or grading, and it does not apply in the `hc` theme or under forced colours, so
7
+ * high-contrast users always get the high-contrast look.
8
+ */
9
+ import type { MessageCatalog } from './i18n/types.js';
10
+ /** The attribute that picks a template, on `<astl-doc>` and on every component. */
11
+ export declare const STYLE_TEMPLATE_ATTRIBUTE = "style-template";
12
+ /** Template names: lowercase kebab-case, 1 to 40 characters, starting with a letter. */
13
+ export declare const STYLE_TEMPLATE_NAME: RegExp;
14
+ /** `document`: the whole exercise (the player's canvas and chrome). `component`: one element and its subtree. */
15
+ export type StyleScope = 'document' | 'component';
16
+ /** Token values by token name without the `--astl-` prefix, e.g. `{ accent: '#7a3ff2', 'radius-m': '1rem' }`. */
17
+ export type TokenOverrides = Readonly<Record<string, string>>;
18
+ /** CSS declarations by property name, e.g. `{ 'border-radius': 'var(--astl-radius-l)' }`. */
19
+ export type Declarations = Readonly<Record<string, string>>;
20
+ export interface StyleTemplate {
21
+ /** What authors write in `style-template`. Matches STYLE_TEMPLATE_NAME. */
22
+ readonly name: string;
23
+ /** Bumped when the look changes visibly. Exercises keep referring to the name. */
24
+ readonly version: number;
25
+ /** Where the template may be used. */
26
+ readonly scopes: readonly StyleScope[];
27
+ /** Component tags it supports at the `component` scope; absent = every registered component. */
28
+ readonly components?: readonly string[];
29
+ /** i18n namespace of its texts: `label` (picker label) and `description` (one line), e.g. `astl.style.notebook`. */
30
+ readonly messages: string;
31
+ /** Catalogs of that namespace. */
32
+ readonly catalogs?: readonly MessageCatalog[];
33
+ /** Token overrides for the light and dark themes (never `hc`). */
34
+ readonly tokens?: TokenOverrides;
35
+ /** Overrides on top of `tokens` for the light theme only. */
36
+ readonly light?: TokenOverrides;
37
+ /** Overrides on top of `tokens` for the dark theme only. */
38
+ readonly dark?: TokenOverrides;
39
+ /**
40
+ * `::part()` declarations by component tag, then part name (a name from the manifest's `cssParts`), e.g.
41
+ * `{ 'astl-choice': { option: { 'border-radius': 'var(--astl-radius-l)' } } }`. Use tokens in values so the
42
+ * declarations follow the theme.
43
+ */
44
+ readonly parts?: Readonly<Record<string, Readonly<Record<string, Declarations>>>>;
45
+ }
46
+ /**
47
+ * Whether `value` has the shape of a StyleTemplate: a valid name, a positive version, one or both scopes, string tags,
48
+ * a namespace, and records of strings for tokens and parts. Registries use it for hosts without type checking.
49
+ */
50
+ export declare function isStyleTemplate(value: unknown): value is StyleTemplate;
51
+ //# sourceMappingURL=style.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"style.d.ts","sourceRoot":"","sources":["../src/style.ts"],"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AAE,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC;CACnF;AAWD;;;GAGG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,aAAa,CAuBtE"}
package/dist/style.js ADDED
@@ -0,0 +1,36 @@
1
+ /** The attribute that picks a template, on `<astl-doc>` and on every component. */
2
+ export const STYLE_TEMPLATE_ATTRIBUTE = 'style-template';
3
+ /** Template names: lowercase kebab-case, 1 to 40 characters, starting with a letter. */
4
+ export const STYLE_TEMPLATE_NAME = /^(?=.{1,40}$)[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/;
5
+ const SCOPES = ['document', 'component'];
6
+ const isRecord = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
7
+ /** Absent, or a record of strings. */
8
+ const isStrings = (value) => value === undefined || (isRecord(value) && Object.values(value).every((v) => typeof v === 'string'));
9
+ /**
10
+ * Whether `value` has the shape of a StyleTemplate: a valid name, a positive version, one or both scopes, string tags,
11
+ * a namespace, and records of strings for tokens and parts. Registries use it for hosts without type checking.
12
+ */
13
+ export function isStyleTemplate(value) {
14
+ if (!isRecord(value))
15
+ return false;
16
+ const { name, version, scopes, components, messages, catalogs, tokens, light, dark, parts } = value;
17
+ const parted = (byTag) => isRecord(byTag) && Object.values(byTag).every(isStrings);
18
+ return (typeof name === 'string' &&
19
+ STYLE_TEMPLATE_NAME.test(name) &&
20
+ typeof version === 'number' &&
21
+ Number.isInteger(version) &&
22
+ version >= 1 &&
23
+ Array.isArray(scopes) &&
24
+ scopes.length > 0 &&
25
+ new Set(scopes).size === scopes.length &&
26
+ scopes.every((scope) => SCOPES.includes(scope)) &&
27
+ (components === undefined ||
28
+ (Array.isArray(components) && components.every((t) => typeof t === 'string'))) &&
29
+ typeof messages === 'string' &&
30
+ (catalogs === undefined || Array.isArray(catalogs)) &&
31
+ isStrings(tokens) &&
32
+ isStrings(light) &&
33
+ isStrings(dark) &&
34
+ (parts === undefined || (isRecord(parts) && Object.values(parts).every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,CAAC,IAAI,CAAC,IAAI,CAAC;QAC9B,OAAO,OAAO,KAAK,QAAQ;QAC3B,MAAM,CAAC,SAAS,CAAC,OAAO,CAAC;QACzB,OAAO,IAAI,CAAC;QACZ,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC;QACrB,MAAM,CAAC,MAAM,GAAG,CAAC;QACjB,IAAI,GAAG,CAAC,MAAM,CAAC,CAAC,IAAI,KAAK,MAAM,CAAC,MAAM;QACtC,MAAM,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;QAC/C,CAAC,UAAU,KAAK,SAAS;YACvB,CAAC,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,IAAI,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,QAAQ,CAAC,CAAC,CAAC;QAChF,OAAO,QAAQ,KAAK,QAAQ;QAC5B,CAAC,QAAQ,KAAK,SAAS,IAAI,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;QACnD,SAAS,CAAC,MAAM,CAAC;QACjB,SAAS,CAAC,KAAK,CAAC;QAChB,SAAS,CAAC,IAAI,CAAC;QACf,CAAC,KAAK,KAAK,SAAS,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CACjF,CAAC;AACJ,CAAC"}
package/package.json CHANGED
@@ -1,7 +1,43 @@
1
1
  {
2
2
  "name": "@astl/core",
3
- "version": "0.0.0",
4
- "description": "Placeholder for the trusted publishing setup. Use 0.1.0 or later.",
3
+ "version": "0.1.0",
4
+ "description": "Contracts and types of Astrolabe: content model, manifests, slots, scoring, checker protocol, i18n runtime. No runtime dependencies.",
5
5
  "license": "Apache-2.0",
6
- "repository": { "type": "git", "url": "git+https://github.com/starship1337/astrolabe.git" }
7
- }
6
+ "author": "The Astrolabe Authors",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/starship1337/astrolabe.git",
10
+ "directory": "packages/core"
11
+ },
12
+ "homepage": "https://astrolabe.starship.education",
13
+ "bugs": "https://github.com/starship1337/astrolabe/issues",
14
+ "type": "module",
15
+ "sideEffects": false,
16
+ "types": "./dist/index.d.ts",
17
+ "exports": {
18
+ ".": {
19
+ "@astl/source": "./src/index.ts",
20
+ "types": "./dist/index.d.ts",
21
+ "default": "./dist/index.js"
22
+ },
23
+ "./declarations": {
24
+ "@astl/source": "./src/declarations.ts",
25
+ "types": "./dist/declarations.d.ts",
26
+ "default": "./dist/declarations.js"
27
+ },
28
+ "./package.json": "./package.json"
29
+ },
30
+ "files": [
31
+ "dist",
32
+ "src",
33
+ "CHANGELOG.md",
34
+ "!src/**/*.test.ts",
35
+ "!src/**/testing"
36
+ ],
37
+ "engines": {
38
+ "node": ">=22.12"
39
+ },
40
+ "publishConfig": {
41
+ "access": "public"
42
+ }
43
+ }
package/src/answers.ts ADDED
@@ -0,0 +1,52 @@
1
+ /**
2
+ * Answers on the wire. Strings only, so any backend in any language can store and grade them.
3
+ * - A missing key or `''` means unanswered.
4
+ * - Text and number slots: what the student typed.
5
+ * - Choice slots: the picked value, an opaque option id or a fixed public value (`true` / `false` of a statement).
6
+ * - Flag slots (selection): `'true'` when selected.
7
+ * - Open slots: what the student wrote, kept for a teacher.
8
+ */
9
+ export type SlotId = string;
10
+
11
+ export type Answers = Readonly<Record<SlotId, string>>;
12
+
13
+ /**
14
+ * Longest accepted answer per slot, in Unicode code points, for every slot kind but `open`. Longer values count as
15
+ * unanswered (engine, servers, protocol 1).
16
+ */
17
+ export const MAX_ANSWER_LENGTH = 500;
18
+
19
+ /** The built-in slot kind of open answers: stored with the attempt for a teacher, never graded. */
20
+ export const OPEN_SLOT_KIND = 'open';
21
+
22
+ /** Longest open answer, in Unicode code points (so Polish and English letters count one each). */
23
+ export const MAX_OPEN_ANSWER_LENGTH = 2000;
24
+
25
+ /** Most slots one request may carry. */
26
+ export const MAX_ANSWER_COUNT = 2000;
27
+
28
+ export const FLAG_SELECTED = 'true';
29
+
30
+ /** The longest answer a slot of `kind` accepts, in code points: 2000 for open slots, 500 for the others. */
31
+ export function maxAnswerLength(kind: string | undefined): number {
32
+ return kind === OPEN_SLOT_KIND ? MAX_OPEN_ANSWER_LENGTH : MAX_ANSWER_LENGTH;
33
+ }
34
+
35
+ /** Whether `value` is within the length limit of a slot of `kind`, counted in Unicode code points. */
36
+ export function fitsAnswer(value: string, kind?: string): boolean {
37
+ const max = maxAnswerLength(kind);
38
+ // Code points never outnumber UTF-16 units, and each takes at most two: count only in between.
39
+ if (value.length <= max) return true;
40
+ if (value.length > 2 * max) return false;
41
+ let count = 0;
42
+ for (const _ of value) if (++count > max) return false;
43
+ return true;
44
+ }
45
+
46
+ /** Type guard for untrusted input (request bodies, storage). */
47
+ export function isAnswers(value: unknown): value is Answers {
48
+ if (typeof value !== 'object' || value === null || Array.isArray(value)) return false;
49
+ const entries = Object.entries(value);
50
+ if (entries.length > MAX_ANSWER_COUNT) return false;
51
+ return entries.every(([k, v]) => k.length > 0 && k.length <= 200 && typeof v === 'string');
52
+ }
package/src/ast.ts ADDED
@@ -0,0 +1,49 @@
1
+ /**
2
+ * The document tree the engine and component definitions work on. Produced by the engine's HTML parser
3
+ * (sources) and serialised back to HTML (views). Plain data: no DOM, no parent pointers.
4
+ */
5
+
6
+ export interface ElementNode {
7
+ readonly type: 'element';
8
+ /** Lowercase tag name, e.g. `p`, `astl-gap`. */
9
+ readonly tag: string;
10
+ /** Attributes in source order. Values are already entity-decoded. */
11
+ readonly attrs: Readonly<Record<string, string>>;
12
+ readonly children: readonly Node[];
13
+ }
14
+
15
+ export interface TextNode {
16
+ readonly type: 'text';
17
+ readonly value: string;
18
+ }
19
+
20
+ export type Node = ElementNode | TextNode;
21
+
22
+ export function element(
23
+ tag: string,
24
+ attrs: Readonly<Record<string, string>> = {},
25
+ children: readonly Node[] = [],
26
+ ): ElementNode {
27
+ return { type: 'element', tag, attrs, children };
28
+ }
29
+
30
+ export function text(value: string): TextNode {
31
+ return { type: 'text', value };
32
+ }
33
+
34
+ export function isElement(node: Node | undefined, tag?: string): node is ElementNode {
35
+ return node !== undefined && node.type === 'element' && (tag === undefined || node.tag === tag);
36
+ }
37
+
38
+ /** Concatenated text of a subtree, like DOM `textContent`. */
39
+ export function textContent(node: Node): string {
40
+ if (node.type === 'text') return node.value;
41
+ let out = '';
42
+ for (const child of node.children) out += textContent(child);
43
+ return out;
44
+ }
45
+
46
+ /** Element children with the given tag, in order. */
47
+ export function childElements(node: ElementNode, tag?: string): ElementNode[] {
48
+ return node.children.filter((c): c is ElementNode => isElement(c, tag));
49
+ }