@astl/engine 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 (98) hide show
  1. package/CHANGELOG.md +35 -0
  2. package/LICENSE +202 -0
  3. package/README.md +41 -0
  4. package/dist/analyse.d.ts +68 -0
  5. package/dist/analyse.d.ts.map +1 -0
  6. package/dist/analyse.js +1179 -0
  7. package/dist/analyse.js.map +1 -0
  8. package/dist/checker.d.ts +19 -0
  9. package/dist/checker.d.ts.map +1 -0
  10. package/dist/checker.js +103 -0
  11. package/dist/checker.js.map +1 -0
  12. package/dist/compile.d.ts +16 -0
  13. package/dist/compile.d.ts.map +1 -0
  14. package/dist/compile.js +329 -0
  15. package/dist/compile.js.map +1 -0
  16. package/dist/conformance.d.ts +35 -0
  17. package/dist/conformance.d.ts.map +1 -0
  18. package/dist/conformance.js +101 -0
  19. package/dist/conformance.js.map +1 -0
  20. package/dist/engine.d.ts +13 -0
  21. package/dist/engine.d.ts.map +1 -0
  22. package/dist/engine.js +231 -0
  23. package/dist/engine.js.map +1 -0
  24. package/dist/grade.d.ts +9 -0
  25. package/dist/grade.d.ts.map +1 -0
  26. package/dist/grade.js +192 -0
  27. package/dist/grade.js.map +1 -0
  28. package/dist/guard.d.ts +45 -0
  29. package/dist/guard.d.ts.map +1 -0
  30. package/dist/guard.js +327 -0
  31. package/dist/guard.js.map +1 -0
  32. package/dist/help.d.ts +32 -0
  33. package/dist/help.d.ts.map +1 -0
  34. package/dist/help.js +120 -0
  35. package/dist/help.js.map +1 -0
  36. package/dist/html.d.ts +31 -0
  37. package/dist/html.d.ts.map +1 -0
  38. package/dist/html.js +246 -0
  39. package/dist/html.js.map +1 -0
  40. package/dist/ids.d.ts +35 -0
  41. package/dist/ids.d.ts.map +1 -0
  42. package/dist/ids.js +126 -0
  43. package/dist/ids.js.map +1 -0
  44. package/dist/index.d.ts +13 -0
  45. package/dist/index.d.ts.map +1 -0
  46. package/dist/index.js +10 -0
  47. package/dist/index.js.map +1 -0
  48. package/dist/kit/attributes.d.ts +6 -0
  49. package/dist/kit/attributes.d.ts.map +1 -0
  50. package/dist/kit/attributes.js +20 -0
  51. package/dist/kit/attributes.js.map +1 -0
  52. package/dist/kit/index.d.ts +43 -0
  53. package/dist/kit/index.d.ts.map +1 -0
  54. package/dist/kit/index.js +118 -0
  55. package/dist/kit/index.js.map +1 -0
  56. package/dist/limits.d.ts +17 -0
  57. package/dist/limits.d.ts.map +1 -0
  58. package/dist/limits.js +173 -0
  59. package/dist/limits.js.map +1 -0
  60. package/dist/messages/en.json +48 -0
  61. package/dist/messages/pl.json +48 -0
  62. package/dist/messages.d.ts +4 -0
  63. package/dist/messages.d.ts.map +1 -0
  64. package/dist/messages.js +9 -0
  65. package/dist/messages.js.map +1 -0
  66. package/dist/round.d.ts +3 -0
  67. package/dist/round.d.ts.map +1 -0
  68. package/dist/round.js +5 -0
  69. package/dist/round.js.map +1 -0
  70. package/dist/slot-kinds.d.ts +58 -0
  71. package/dist/slot-kinds.d.ts.map +1 -0
  72. package/dist/slot-kinds.js +151 -0
  73. package/dist/slot-kinds.js.map +1 -0
  74. package/dist/text.d.ts +23 -0
  75. package/dist/text.d.ts.map +1 -0
  76. package/dist/text.js +236 -0
  77. package/dist/text.js.map +1 -0
  78. package/package.json +39 -4
  79. package/src/analyse.ts +1396 -0
  80. package/src/checker.ts +132 -0
  81. package/src/compile.ts +394 -0
  82. package/src/conformance.ts +120 -0
  83. package/src/engine.ts +274 -0
  84. package/src/grade.ts +229 -0
  85. package/src/guard.ts +356 -0
  86. package/src/help.ts +141 -0
  87. package/src/html.ts +274 -0
  88. package/src/ids.ts +144 -0
  89. package/src/index.ts +12 -0
  90. package/src/kit/attributes.ts +21 -0
  91. package/src/kit/index.ts +140 -0
  92. package/src/limits.ts +153 -0
  93. package/src/messages/en.json +48 -0
  94. package/src/messages/pl.json +48 -0
  95. package/src/messages.ts +9 -0
  96. package/src/round.ts +4 -0
  97. package/src/slot-kinds.ts +212 -0
  98. package/src/text.ts +234 -0
package/dist/help.js ADDED
@@ -0,0 +1,120 @@
1
+ /**
2
+ * Help over an answer key: the feedback texts and explanations a result brings, and the next hint of an interaction
3
+ * or a page. Pure functions, shared by `localChecker` and the grading endpoint, so both answer the same.
4
+ */
5
+ import { helpPolicy, } from '@astl/core';
6
+ /**
7
+ * Whether a check or the hand-in brings feedback texts and explanations under a policy: after a check when the policy
8
+ * says `check`; at the hand-in when it says `check` or `submit`, and only with the answers (`reveal: 'answers'`), as a
9
+ * feedback text names the right answer as surely as a solution does.
10
+ */
11
+ export function helpShown(policy, moment) {
12
+ const help = helpPolicy(policy);
13
+ const shown = (when) => moment === 'check' ? when === 'check' : when !== 'never' && policy.reveal === 'answers';
14
+ return { feedback: shown(help.feedback), explanations: shown(help.explanations) };
15
+ }
16
+ /** The view attributes (`HELP_ATTRIBUTES`) for what an element or page offers: only those with something to say. */
17
+ export function helpAttributes(help) {
18
+ const out = {};
19
+ if (help.hints.length > 0)
20
+ out.hints = help.hints.map(String).join(' ');
21
+ if (help.explanation)
22
+ out.explanation = '';
23
+ if (help.feedback.length > 0)
24
+ out.feedback = help.feedback.join(' ');
25
+ return out;
26
+ }
27
+ const feedbackVariant = (verdict) => verdict === 'empty' ? 'omitted' : verdict;
28
+ const own = (key, id) => key.help !== undefined && Object.hasOwn(key.help, id) ? key.help[id] : undefined;
29
+ /** The view element of every group (the component of its slots), by group id. */
30
+ function elementsOf(key) {
31
+ const out = new Map();
32
+ for (const group of key.groups) {
33
+ const first = group.slots[0];
34
+ const element = first !== undefined && Object.hasOwn(key.slots, first) ? key.slots[first]?.group : undefined;
35
+ if (element !== undefined)
36
+ out.set(group.id, element);
37
+ }
38
+ return out;
39
+ }
40
+ /** Whether `id` is a graded view element of the key: what a check of one interaction may name. */
41
+ export function isGradedElement(key, id) {
42
+ return [...elementsOf(key).values()].includes(id);
43
+ }
44
+ /**
45
+ * One verdict for several groups (an element graded in parts, a page): nothing answered is `empty`, everything right
46
+ * `correct`, something right `partial`, else `incorrect`.
47
+ */
48
+ function overall(verdicts) {
49
+ if (verdicts.every((v) => v === 'empty'))
50
+ return 'empty';
51
+ if (verdicts.every((v) => v === 'correct'))
52
+ return 'correct';
53
+ return verdicts.some((v) => v === 'correct' || v === 'partial') ? 'partial' : 'incorrect';
54
+ }
55
+ /** The text for a verdict; a partial one without its own text gets the `incorrect` one. */
56
+ function feedbackFor(help, verdict) {
57
+ const variant = feedbackVariant(verdict);
58
+ const html = help.feedback[variant] ?? (variant === 'partial' ? help.feedback.incorrect : undefined);
59
+ return typeof html === 'string' ? { variant, html } : null;
60
+ }
61
+ /**
62
+ * The help a result brings, for what it graded: each graded element's feedback for its verdict and its explanation,
63
+ * and, unless one element was checked (`element`), each graded page's own, its feedback picked by the page's overall
64
+ * verdict. `shown` says which of the two the policy gives now (`helpShown`). Empty parts are left out.
65
+ */
66
+ export function resultHelp(key, result, shown, element) {
67
+ if (key.help === undefined || (!shown.feedback && !shown.explanations))
68
+ return {};
69
+ const elements = elementsOf(key);
70
+ const pageOf = new Map(key.groups.map((g) => [g.id, g.page]));
71
+ const byElement = new Map();
72
+ const byPage = new Map();
73
+ for (const group of result.groups) {
74
+ const add = (map, id) => {
75
+ if (id !== undefined)
76
+ map.set(id, [...(map.get(id) ?? []), group.verdict]);
77
+ };
78
+ add(byElement, elements.get(group.id));
79
+ add(byPage, pageOf.get(group.id));
80
+ }
81
+ const feedback = {};
82
+ const explanations = {};
83
+ const give = (id, verdicts) => {
84
+ const help = own(key, id);
85
+ if (help === undefined)
86
+ return;
87
+ const text = shown.feedback ? feedbackFor(help, overall(verdicts)) : null;
88
+ if (text !== null)
89
+ feedback[id] = text;
90
+ if (shown.explanations && typeof help.explanation === 'string')
91
+ explanations[id] = help.explanation;
92
+ };
93
+ for (const [id, verdicts] of byElement)
94
+ give(id, verdicts);
95
+ if (element === undefined) {
96
+ for (const page of Object.keys(result.pages)) {
97
+ const verdicts = byPage.get(page);
98
+ if (verdicts !== undefined)
99
+ give(page, verdicts);
100
+ }
101
+ }
102
+ return {
103
+ ...(Object.keys(feedback).length === 0 ? {} : { feedback }),
104
+ ...(Object.keys(explanations).length === 0 ? {} : { explanations }),
105
+ };
106
+ }
107
+ /** How many hints an element or a page has. */
108
+ export function hintCount(key, id) {
109
+ const hints = own(key, id)?.hints;
110
+ return Array.isArray(hints) ? hints.length : 0;
111
+ }
112
+ /** The hint after the first `used` ones of an element or a page; null when there is none. */
113
+ export function nextHint(key, id, used) {
114
+ const hints = own(key, id)?.hints ?? [];
115
+ const hint = Number.isInteger(used) && used >= 0 ? hints[used] : undefined;
116
+ if (hint === undefined)
117
+ return null;
118
+ return { html: hint.html, index: used, left: hints.length - used - 1, cost: hint.cost };
119
+ }
120
+ //# sourceMappingURL=help.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"help.js","sourceRoot":"","sources":["../src/help.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAUL,UAAU,GAGX,MAAM,YAAY,CAAC;AAEpB;;;;GAIG;AACH,MAAM,UAAU,SAAS,CACvB,MAAqB,EACrB,MAA0B;IAE1B,MAAM,IAAI,GAAG,UAAU,CAAC,MAAM,CAAC,CAAC;IAChC,MAAM,KAAK,GAAG,CAAC,IAAgB,EAAW,EAAE,CAC1C,MAAM,KAAK,OAAO,CAAC,CAAC,CAAC,IAAI,KAAK,OAAO,CAAC,CAAC,CAAC,IAAI,KAAK,OAAO,IAAI,MAAM,CAAC,MAAM,KAAK,SAAS,CAAC;IAC1F,OAAO,EAAE,QAAQ,EAAE,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE,YAAY,EAAE,KAAK,CAAC,IAAI,CAAC,YAAY,CAAC,EAAE,CAAC;AACpF,CAAC;AAED,oHAAoH;AACpH,MAAM,UAAU,cAAc,CAAC,IAAiB;IAC9C,MAAM,GAAG,GAA2B,EAAE,CAAC;IACvC,IAAI,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG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,GAAG,CAAC,GAAG,EAAE,EAAE,CAAC,EAAE,KAAK,CAAC;IAClC,OAAO,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;AACjD,CAAC;AAED,6FAA6F;AAC7F,MAAM,UAAU,QAAQ,CAAC,GAAc,EAAE,EAAU,EAAE,IAAY;IAC/D,MAAM,KAAK,GAAG,GAAG,CAAC,GAAG,EAAE,EAAE,CAAC,EAAE,KAAK,IAAI,EAAE,CAAC;IACxC,MAAM,IAAI,GAAG,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IAC3E,IAAI,IAAI,KAAK,SAAS;QAAE,OAAO,IAAI,CAAC;IACpC,OAAO,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,CAAC,MAAM,GAAG,IAAI,GAAG,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,CAAC;AAC1F,CAAC"}
package/dist/html.d.ts ADDED
@@ -0,0 +1,31 @@
1
+ /**
2
+ * HTML <-> core AST. Parsing follows the HTML standard (parse5, fragment mode), so the engine sees the same tree a
3
+ * browser would build. Serialisation is deterministic: the same tree always gives the same bytes.
4
+ */
5
+ import { type Node } from '@astl/core';
6
+ /**
7
+ * Parses HTML (a whole source or a single element snippet) into core nodes. Comments and doctypes are dropped,
8
+ * tag and attribute names are lowercased, `<template>` content is dropped and adjacent text nodes are merged.
9
+ *
10
+ * The result is stable: serialising it and parsing again gives the same tree. The HTML parser alone does not promise
11
+ * that (`<h3><table><h6>` puts the h6 inside the h3, and re-parsing `<h3><h6>` closes the h3), so the serialisation
12
+ * is re-parsed until it stops changing; well-formed input settles after one check.
13
+ *
14
+ * Throws a RangeError for input nested deeper than MAX_NESTING_DEPTH elements or with an element carrying more than
15
+ * MAX_ATTRIBUTES_PER_ELEMENT attributes (`validate` reports SOURCE_TOO_DEEP and SOURCE_TOO_COMPLEX instead).
16
+ */
17
+ export declare function parseHtml(html: string): Node[];
18
+ /** Like parseHtml, but returns null for input nested deeper than MAX_NESTING_DEPTH elements. */
19
+ export declare function parseSource(html: string): Node[] | null;
20
+ /** Appends text, merging it into a preceding text node. */
21
+ export declare function appendText(out: Node[], value: string): void;
22
+ /**
23
+ * Serialises nodes to HTML. Text escapes `&`, `<`, `>` and U+00A0 (`&nbsp;`); attribute values escape `&`, `"` and
24
+ * U+00A0; empty attributes are written bare (`multiple`); void elements have no end tag. Text is kept as is.
25
+ * Parsing the output gives the same tree back (a fixpoint), including the parser's special cases: raw text elements,
26
+ * the newline after `<pre>`, `<plaintext>` (which never ends) and SVG / MathML content.
27
+ */
28
+ export declare function serializeHtml(nodes: readonly Node[]): string;
29
+ /** HTML inter-element whitespace (ASCII only, as in the HTML standard). */
30
+ export declare function isHtmlWhitespace(value: string): boolean;
31
+ //# sourceMappingURL=html.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"html.d.ts","sourceRoot":"","sources":["../src/html.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAKL,KAAK,IAAI,EAEV,MAAM,YAAY,CAAC;AAapB;;;;;;;;;;GAUG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,EAAE,CAO9C;AAED,gGAAgG;AAChG,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,EAAE,GAAG,IAAI,CAavD;AA0FD,2DAA2D;AAC3D,wBAAgB,UAAU,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,CAI3D;AAuCD;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,SAAS,IAAI,EAAE,GAAG,MAAM,CAG5D;AAqED,2EAA2E;AAC3E,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAEvD"}
package/dist/html.js ADDED
@@ -0,0 +1,246 @@
1
+ /**
2
+ * HTML <-> core AST. Parsing follows the HTML standard (parse5, fragment mode), so the engine sees the same tree a
3
+ * browser would build. Serialisation is deterministic: the same tree always gives the same bytes.
4
+ */
5
+ import { element, MAX_ATTRIBUTES_PER_ELEMENT, MAX_NESTING_DEPTH, text, } from '@astl/core';
6
+ import { defaultTreeAdapter, parseFragment, } from 'parse5';
7
+ import { mostAttributes } from './limits.js';
8
+ /**
9
+ * Parses HTML (a whole source or a single element snippet) into core nodes. Comments and doctypes are dropped,
10
+ * tag and attribute names are lowercased, `<template>` content is dropped and adjacent text nodes are merged.
11
+ *
12
+ * The result is stable: serialising it and parsing again gives the same tree. The HTML parser alone does not promise
13
+ * that (`<h3><table><h6>` puts the h6 inside the h3, and re-parsing `<h3><h6>` closes the h3), so the serialisation
14
+ * is re-parsed until it stops changing; well-formed input settles after one check.
15
+ *
16
+ * Throws a RangeError for input nested deeper than MAX_NESTING_DEPTH elements or with an element carrying more than
17
+ * MAX_ATTRIBUTES_PER_ELEMENT attributes (`validate` reports SOURCE_TOO_DEEP and SOURCE_TOO_COMPLEX instead).
18
+ */
19
+ export function parseHtml(html) {
20
+ if (mostAttributes(html, MAX_ATTRIBUTES_PER_ELEMENT) > MAX_ATTRIBUTES_PER_ELEMENT) {
21
+ throw new RangeError(`HTML has an element with more than ${MAX_ATTRIBUTES_PER_ELEMENT} attributes`);
22
+ }
23
+ const nodes = parseSource(html);
24
+ if (nodes === null)
25
+ throw new RangeError(`HTML is nested deeper than ${MAX_NESTING_DEPTH} elements`);
26
+ return nodes;
27
+ }
28
+ /** Like parseHtml, but returns null for input nested deeper than MAX_NESTING_DEPTH elements. */
29
+ export function parseSource(html) {
30
+ let nodes = parseOnce(html);
31
+ if (nodes === null)
32
+ return null;
33
+ let serialised = serializeHtml(nodes);
34
+ for (let round = 0; round < MAX_ROUNDS; round++) {
35
+ const again = parseOnce(serialised);
36
+ if (again === null)
37
+ return null;
38
+ const next = serializeHtml(again);
39
+ if (next === serialised)
40
+ return nodes;
41
+ nodes = again;
42
+ serialised = next;
43
+ }
44
+ return nodes;
45
+ }
46
+ /** Each round undoes one level of misnesting; the cap only bounds pathological input. */
47
+ const MAX_ROUNDS = 8;
48
+ class TooDeep extends Error {
49
+ }
50
+ /** Element ancestors of a node while parsing, plus itself, counted up to the guard's bound only. */
51
+ function depthOf(node) {
52
+ let depth = 0;
53
+ for (let n = node; n !== null && 'tagName' in n; n = n.parentNode) {
54
+ if (++depth > PARSER_BOUND)
55
+ break;
56
+ }
57
+ return depth;
58
+ }
59
+ /**
60
+ * The fragment parser builds its tree inside two wrapper elements (a stand-in document and a temporary `<html>`
61
+ * root). The guard only has to bound the parser's work, so it allows them; the exact limit is checked on the finished
62
+ * tree.
63
+ */
64
+ const PARSER_BOUND = MAX_NESTING_DEPTH + 2;
65
+ /**
66
+ * The default tree adapter, refusing to nest elements beyond the bound while the tree is built. The parser scans its
67
+ * stack of open elements for many tags, so deep input (tens of thousands of nested divs) would take quadratic time
68
+ * before any check after parsing could run.
69
+ */
70
+ const guardedAdapter = {
71
+ ...defaultTreeAdapter,
72
+ appendChild(parent, node) {
73
+ if ('tagName' in node && depthOf(parent) >= PARSER_BOUND)
74
+ throw new TooDeep();
75
+ defaultTreeAdapter.appendChild(parent, node);
76
+ },
77
+ insertBefore(parent, node, reference) {
78
+ if ('tagName' in node && depthOf(parent) >= PARSER_BOUND)
79
+ throw new TooDeep();
80
+ defaultTreeAdapter.insertBefore(parent, node, reference);
81
+ },
82
+ };
83
+ /** Deepest element nesting of a parsed fragment, without recursion (the adapter does not see moved subtrees). */
84
+ function maxDepth(root) {
85
+ let max = 0;
86
+ const stack = root.childNodes.map((n) => [n, 1]);
87
+ for (let entry = stack.pop(); entry !== undefined; entry = stack.pop()) {
88
+ const [node, depth] = entry;
89
+ if (!('tagName' in node))
90
+ continue;
91
+ if (depth > max)
92
+ max = depth;
93
+ for (const child of node.childNodes)
94
+ stack.push([child, depth + 1]);
95
+ }
96
+ return max;
97
+ }
98
+ function parseOnce(html) {
99
+ let fragment;
100
+ try {
101
+ fragment = parseFragment(html, { treeAdapter: guardedAdapter });
102
+ }
103
+ catch (e) {
104
+ if (e instanceof TooDeep)
105
+ return null;
106
+ throw e;
107
+ }
108
+ return maxDepth(fragment) > MAX_NESTING_DEPTH ? null : convert(fragment.childNodes);
109
+ }
110
+ function convert(nodes) {
111
+ const out = [];
112
+ for (const node of nodes) {
113
+ if (node.nodeName === '#text') {
114
+ appendText(out, node.value);
115
+ }
116
+ else if ('tagName' in node) {
117
+ const attrs = {};
118
+ for (const a of node.attrs) {
119
+ const name = (a.prefix ? `${a.prefix}:${a.name}` : a.name).toLowerCase();
120
+ // defineProperty keeps names such as `__proto__` as plain own attributes.
121
+ if (!Object.hasOwn(attrs, name))
122
+ Object.defineProperty(attrs, name, {
123
+ value: a.value,
124
+ enumerable: true,
125
+ writable: true,
126
+ configurable: true,
127
+ });
128
+ }
129
+ const children = node.nodeName === 'template' ? [] : convert(node.childNodes);
130
+ out.push(element(node.tagName.toLowerCase(), attrs, children));
131
+ }
132
+ // Comments and doctypes are dropped.
133
+ }
134
+ return out;
135
+ }
136
+ /** Appends text, merging it into a preceding text node. */
137
+ export function appendText(out, value) {
138
+ const last = out[out.length - 1];
139
+ if (last?.type === 'text')
140
+ out[out.length - 1] = text(last.value + value);
141
+ else if (value !== '')
142
+ out.push(text(value));
143
+ }
144
+ const VOID_ELEMENTS = new Set([
145
+ 'area',
146
+ 'base',
147
+ 'br',
148
+ 'col',
149
+ 'embed',
150
+ 'hr',
151
+ 'img',
152
+ 'input',
153
+ 'link',
154
+ 'meta',
155
+ 'source',
156
+ 'track',
157
+ 'wbr',
158
+ ]);
159
+ /** Elements whose text the parser reads raw (no entity decoding), so it must be written raw too. */
160
+ const RAW_TEXT_ELEMENTS = new Set([
161
+ 'script',
162
+ 'style',
163
+ 'xmp',
164
+ 'iframe',
165
+ 'noembed',
166
+ 'noframes',
167
+ 'plaintext',
168
+ 'noscript',
169
+ ]);
170
+ /** The parser drops a newline right after these start tags, so one is written back when the text starts with one. */
171
+ const LEADING_NEWLINE = new Set(['pre', 'textarea', 'listing']);
172
+ /** SVG and MathML elements whose content the parser reads as HTML again. */
173
+ const SVG_HTML_POINTS = new Set(['foreignobject', 'desc', 'title']);
174
+ const MATHML_HTML_POINTS = new Set(['mi', 'mo', 'mn', 'ms', 'mtext']);
175
+ /**
176
+ * Serialises nodes to HTML. Text escapes `&`, `<`, `>` and U+00A0 (`&nbsp;`); attribute values escape `&`, `"` and
177
+ * U+00A0; empty attributes are written bare (`multiple`); void elements have no end tag. Text is kept as is.
178
+ * Parsing the output gives the same tree back (a fixpoint), including the parser's special cases: raw text elements,
179
+ * the newline after `<pre>`, `<plaintext>` (which never ends) and SVG / MathML content.
180
+ */
181
+ export function serializeHtml(nodes) {
182
+ const state = { ended: false };
183
+ return serializeList(nodes, 'html', false, state);
184
+ }
185
+ function serializeList(nodes, ns, raw, state) {
186
+ let out = '';
187
+ for (const node of nodes) {
188
+ if (state.ended)
189
+ break;
190
+ out +=
191
+ node.type === 'text' ? (raw ? node.value : escapeText(node.value)) : serializeElement(node, ns, state);
192
+ }
193
+ return out;
194
+ }
195
+ function serializeElement(node, parentNs, state) {
196
+ const tag = node.tag;
197
+ const ns = parentNs === 'html' && (tag === 'svg' || tag === 'math') ? tag : parentNs;
198
+ let out = `<${tag}`;
199
+ for (const [name, value] of Object.entries(node.attrs)) {
200
+ out += value === '' ? ` ${name}` : ` ${name}="${escapeAttribute(value)}"`;
201
+ }
202
+ out += '>';
203
+ if (ns === 'html' && VOID_ELEMENTS.has(tag))
204
+ return out;
205
+ const first = node.children[0];
206
+ if (ns === 'html' && LEADING_NEWLINE.has(tag) && first?.type === 'text' && first.value.startsWith('\n'))
207
+ out += '\n';
208
+ out += serializeList(node.children, childNamespace(node, ns), ns === 'html' && RAW_TEXT_ELEMENTS.has(tag), state);
209
+ // Nothing can follow <plaintext>: the parser reads the rest of the input as its text.
210
+ if (ns === 'html' && tag === 'plaintext')
211
+ state.ended = true;
212
+ return state.ended ? out : `${out}</${tag}>`;
213
+ }
214
+ function childNamespace(node, ns) {
215
+ if (ns === 'svg' && SVG_HTML_POINTS.has(node.tag))
216
+ return 'html';
217
+ if (ns === 'math' && MATHML_HTML_POINTS.has(node.tag))
218
+ return 'html';
219
+ if (ns === 'math' && node.tag === 'annotation-xml') {
220
+ const encoding = (node.attrs.encoding ?? '').toLowerCase();
221
+ if (encoding === 'text/html' || encoding === 'application/xhtml+xml')
222
+ return 'html';
223
+ }
224
+ return ns;
225
+ }
226
+ const NBSP = String.fromCodePoint(0xa0);
227
+ const TEXT_SPECIALS = new RegExp(`[&<>${NBSP}]`, 'g');
228
+ const ATTRIBUTE_SPECIALS = new RegExp(`[&"${NBSP}]`, 'g');
229
+ const ENTITIES = {
230
+ '&': '&amp;',
231
+ '<': '&lt;',
232
+ '>': '&gt;',
233
+ '"': '&quot;',
234
+ [NBSP]: '&nbsp;',
235
+ };
236
+ function escapeText(value) {
237
+ return value.replace(TEXT_SPECIALS, (ch) => ENTITIES[ch] ?? ch);
238
+ }
239
+ function escapeAttribute(value) {
240
+ return value.replace(ATTRIBUTE_SPECIALS, (ch) => ENTITIES[ch] ?? ch);
241
+ }
242
+ /** HTML inter-element whitespace (ASCII only, as in the HTML standard). */
243
+ export function isHtmlWhitespace(value) {
244
+ return /^[\t\n\f\r ]*$/.test(value);
245
+ }
246
+ //# sourceMappingURL=html.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"html.js","sourceRoot":"","sources":["../src/html.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAEL,OAAO,EACP,0BAA0B,EAC1B,iBAAiB,EAEjB,IAAI,GACL,MAAM,YAAY,CAAC;AACpB,OAAO,EAGL,kBAAkB,EAClB,aAAa,GAE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AS,UAAU,CAAC,KAAa;IAC/B,OAAO,KAAK,CAAC,OAAO,CAAC,aAAa,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,IAAI,EAAE,CAAC,CAAC;AAClE,CAAC;AAED,SAAS,eAAe,CAAC,KAAa;IACpC,OAAO,KAAK,CAAC,OAAO,CAAC,kBAAkB,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,IAAI,EAAE,CAAC,CAAC;AACvE,CAAC;AAED,2EAA2E;AAC3E,MAAM,UAAU,gBAAgB,CAAC,KAAa;IAC5C,OAAO,gBAAgB,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AACtC,CAAC"}
package/dist/ids.d.ts ADDED
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Opaque ids and keyed permutations. Every view id is derived from HMAC-SHA-256(secret, salt U+001F path), with
3
+ * paths `page:<id>` (implicit page: `page:#0`), `el:<id>` and `el:<id>/<localId>`. Ports in other languages must
4
+ * implement exactly this to produce byte-identical views.
5
+ */
6
+ /** Bytes of a secret given as a string (UTF-8) or as bytes (copied). */
7
+ export declare function secretBytes(secret: string | Uint8Array): Uint8Array<ArrayBuffer>;
8
+ /** HMAC-SHA-256 with a lazily imported key, reused for every call. */
9
+ export type Hmac = (message: string) => Promise<Uint8Array>;
10
+ export declare function createHmac(secret: Uint8Array<ArrayBuffer>): Hmac;
11
+ /** The HMAC message for an id path: salt, U+001F, path. */
12
+ export declare function idMessage(salt: string, path: string): string;
13
+ export declare function pagePath(sourceId: string | null): string;
14
+ export declare function elementPath(sourceId: string, localId?: string): string;
15
+ /** RFC 4648 base32, lowercase, without padding. */
16
+ export declare function base32(bytes: Uint8Array): string;
17
+ /** `k` + base32 of digest bytes 0..9: 16 characters, 80 bits. */
18
+ export declare function opaqueId(digest: Uint8Array): string;
19
+ /** Permutation seed: digest bytes 16..31 as four big-endian 32-bit words. */
20
+ export declare function seedWords(digest: Uint8Array): readonly number[];
21
+ /** 32-bit FNV-1a over UTF-8. */
22
+ export declare function fnv1a(value: string): number;
23
+ /**
24
+ * Keyed permutation of `n` indices: a pure function of (seed, salt, n). The generator is sfc32 seeded with the four
25
+ * seed words, the first XOR FNV-1a(salt), and warmed up for 12 rounds; then Fisher-Yates from the end with
26
+ * j = floor(r * (i + 1) / 2^32).
27
+ *
28
+ * With `avoidOriginal` and n >= 2, an identity result is drawn again from the same generator, so the result is
29
+ * uniform over the orders that differ from the source. For n = 2 that leaves one order: the swap. (Rotating the
30
+ * identity instead would make one order twice as likely as the others.)
31
+ */
32
+ export declare function permutation(seed: readonly number[], salt: string, n: number, avoidOriginal: boolean): number[];
33
+ /** Lowercase hex SHA-256 of the UTF-8 text. */
34
+ export declare function sha256Hex(text: string): Promise<string>;
35
+ //# sourceMappingURL=ids.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ids.d.ts","sourceRoot":"","sources":["../src/ids.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,wEAAwE;AACxE,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,UAAU,GAAG,UAAU,CAAC,WAAW,CAAC,CAEhF;AAED,sEAAsE;AACtE,MAAM,MAAM,IAAI,GAAG,CAAC,OAAO,EAAE,MAAM,KAAK,OAAO,CAAC,UAAU,CAAC,CAAC;AAE5D,wBAAgB,UAAU,CAAC,MAAM,EAAE,UAAU,CAAC,WAAW,CAAC,GAAG,IAAI,CAMhE;AAKD,2DAA2D;AAC3D,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAE5D;AAED,wBAAgB,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,GAAG,MAAM,CAExD;AAED,wBAAgB,WAAW,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,CAEtE;AAID,mDAAmD;AACnD,wBAAgB,MAAM,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAchD;AAED,iEAAiE;AACjE,wBAAgB,QAAQ,CAAC,MAAM,EAAE,UAAU,GAAG,MAAM,CAEnD;AAED,6EAA6E;AAC7E,wBAAgB,SAAS,CAAC,MAAM,EAAE,UAAU,GAAG,SAAS,MAAM,EAAE,CAG/D;AAED,gCAAgC;AAChC,wBAAgB,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAI3C;AAmBD;;;;;;;;GAQG;AACH,wBAAgB,WAAW,CACzB,IAAI,EAAE,SAAS,MAAM,EAAE,EACvB,IAAI,EAAE,MAAM,EACZ,CAAC,EAAE,MAAM,EACT,aAAa,EAAE,OAAO,GACrB,MAAM,EAAE,CAqBV;AAQD,+CAA+C;AAC/C,wBAAsB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAK7D"}
package/dist/ids.js ADDED
@@ -0,0 +1,126 @@
1
+ /**
2
+ * Opaque ids and keyed permutations. Every view id is derived from HMAC-SHA-256(secret, salt U+001F path), with
3
+ * paths `page:<id>` (implicit page: `page:#0`), `el:<id>` and `el:<id>/<localId>`. Ports in other languages must
4
+ * implement exactly this to produce byte-identical views.
5
+ */
6
+ const encoder = new TextEncoder();
7
+ /** Bytes of a secret given as a string (UTF-8) or as bytes (copied). */
8
+ export function secretBytes(secret) {
9
+ return typeof secret === 'string' ? encoder.encode(secret) : new Uint8Array(secret);
10
+ }
11
+ export function createHmac(secret) {
12
+ let key;
13
+ return async (message) => {
14
+ key ??= crypto.subtle.importKey('raw', secret, { name: 'HMAC', hash: 'SHA-256' }, false, ['sign']);
15
+ return new Uint8Array(await crypto.subtle.sign('HMAC', await key, encoder.encode(message)));
16
+ };
17
+ }
18
+ /** U+001F (unit separator), written as a code point so no formatter turns it into a raw control character. */
19
+ const SEPARATOR = String.fromCodePoint(0x1f);
20
+ /** The HMAC message for an id path: salt, U+001F, path. */
21
+ export function idMessage(salt, path) {
22
+ return `${salt}${SEPARATOR}${path}`;
23
+ }
24
+ export function pagePath(sourceId) {
25
+ return `page:${sourceId ?? '#0'}`;
26
+ }
27
+ export function elementPath(sourceId, localId) {
28
+ return localId === undefined ? `el:${sourceId}` : `el:${sourceId}/${localId}`;
29
+ }
30
+ const BASE32 = 'abcdefghijklmnopqrstuvwxyz234567';
31
+ /** RFC 4648 base32, lowercase, without padding. */
32
+ export function base32(bytes) {
33
+ let out = '';
34
+ let buffer = 0;
35
+ let bits = 0;
36
+ for (const byte of bytes) {
37
+ buffer = ((buffer << 8) | byte) & 0xffff;
38
+ bits += 8;
39
+ while (bits >= 5) {
40
+ bits -= 5;
41
+ out += BASE32[(buffer >>> bits) & 31];
42
+ }
43
+ }
44
+ if (bits > 0)
45
+ out += BASE32[(buffer << (5 - bits)) & 31];
46
+ return out;
47
+ }
48
+ /** `k` + base32 of digest bytes 0..9: 16 characters, 80 bits. */
49
+ export function opaqueId(digest) {
50
+ return `k${base32(digest.subarray(0, 10))}`;
51
+ }
52
+ /** Permutation seed: digest bytes 16..31 as four big-endian 32-bit words. */
53
+ export function seedWords(digest) {
54
+ const view = new DataView(digest.buffer, digest.byteOffset + 16, 16);
55
+ return [view.getUint32(0), view.getUint32(4), view.getUint32(8), view.getUint32(12)];
56
+ }
57
+ /** 32-bit FNV-1a over UTF-8. */
58
+ export function fnv1a(value) {
59
+ let hash = 0x811c9dc5;
60
+ for (const byte of encoder.encode(value))
61
+ hash = Math.imul(hash ^ byte, 0x01000193);
62
+ return hash >>> 0;
63
+ }
64
+ /** sfc32 (Chris Doty-Humphrey's Small Fast Counting generator): 32-bit outputs, integer arithmetic only. */
65
+ function sfc32(seed) {
66
+ let a = (seed[0] ?? 0) | 0;
67
+ let b = (seed[1] ?? 0) | 0;
68
+ let c = (seed[2] ?? 0) | 0;
69
+ let d = (seed[3] ?? 0) | 0;
70
+ return () => {
71
+ const t = (((a + b) | 0) + d) | 0;
72
+ d = (d + 1) | 0;
73
+ a = b ^ (b >>> 9);
74
+ b = (c + (c << 3)) | 0;
75
+ c = (c << 21) | (c >>> 11);
76
+ c = (c + t) | 0;
77
+ return t >>> 0;
78
+ };
79
+ }
80
+ /**
81
+ * Keyed permutation of `n` indices: a pure function of (seed, salt, n). The generator is sfc32 seeded with the four
82
+ * seed words, the first XOR FNV-1a(salt), and warmed up for 12 rounds; then Fisher-Yates from the end with
83
+ * j = floor(r * (i + 1) / 2^32).
84
+ *
85
+ * With `avoidOriginal` and n >= 2, an identity result is drawn again from the same generator, so the result is
86
+ * uniform over the orders that differ from the source. For n = 2 that leaves one order: the swap. (Rotating the
87
+ * identity instead would make one order twice as likely as the others.)
88
+ */
89
+ export function permutation(seed, salt, n, avoidOriginal) {
90
+ const next = sfc32([(seed[0] ?? 0) ^ fnv1a(salt), seed[1] ?? 0, seed[2] ?? 0, seed[3] ?? 0]);
91
+ for (let i = 0; i < 12; i++)
92
+ next();
93
+ const draw = () => {
94
+ const order = Array.from({ length: n }, (_, i) => i);
95
+ for (let i = n - 1; i > 0; i--) {
96
+ const j = Math.floor((next() * (i + 1)) / 0x100000000);
97
+ const tmp = order[i];
98
+ order[i] = order[j];
99
+ order[j] = tmp;
100
+ }
101
+ return order;
102
+ };
103
+ if (avoidOriginal && n === 2)
104
+ return [1, 0];
105
+ let order = draw();
106
+ // An identity draw has probability 1/n! <= 1/6 here, so a second draw almost always succeeds; the cap only makes
107
+ // termination certain.
108
+ for (let tries = 0; avoidOriginal && n > 2 && isIdentity(order) && tries < MAX_DRAWS; tries++)
109
+ order = draw();
110
+ if (avoidOriginal && n > 2 && isIdentity(order))
111
+ order.push(order.shift());
112
+ return order;
113
+ }
114
+ const MAX_DRAWS = 64;
115
+ function isIdentity(order) {
116
+ return order.every((v, i) => v === i);
117
+ }
118
+ /** Lowercase hex SHA-256 of the UTF-8 text. */
119
+ export async function sha256Hex(text) {
120
+ const digest = new Uint8Array(await crypto.subtle.digest('SHA-256', encoder.encode(text)));
121
+ let out = '';
122
+ for (const byte of digest)
123
+ out += byte.toString(16).padStart(2, '0');
124
+ return out;
125
+ }
126
+ //# sourceMappingURL=ids.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ids.js","sourceRoot":"","sources":["../src/ids.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC;AAElC,wEAAwE;AACxE,MAAM,UAAU,WAAW,CAAC,MAA2B;IACrD,OAAO,OAAO,MAAM,KAAK,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,UAAU,CAAC,MAAM,CAAC,CAAC;AACtF,CAAC;AAKD,MAAM,UAAU,UAAU,CAAC,MAA+B;IACxD,IAAI,GAAmC,CAAC;IACxC,OAAO,KAAK,EAAE,OAAO,EAAE,EAAE;QACvB,GAAG,KAAK,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,SAAS,EAAE,EAAE,KAAK,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC;QACnG,OAAO,IAAI,UAAU,CAAC,MAAM,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,MAAM,GAAG,EAAE,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;IAC9F,CAAC,CAAC;AACJ,CAAC;AAED,8GAA8G;AAC9G,MAAM,SAAS,GAAG,MAAM,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC;AAE7C,2DAA2D;AAC3D,MAAM,UAAU,SAAS,CAAC,IAAY,EAAE,IAAY;IAClD,OAAO,GAAG,IAAI,GAAG,SAAS,GAAG,IAAI,EAAE,CAAC;AACtC,CAAC;AAED,MAAM,UAAU,QAAQ,CAAC,QAAuB;IAC9C,OAAO,QAAQ,QAAQ,IAAI,IAAI,EAAE,CAAC;AACpC,CAAC;AAED,MAAM,UAAU,WA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CAAC"}
@@ -0,0 +1,13 @@
1
+ export type { LocalChecker } from './checker.js';
2
+ export { localChecker } from './checker.js';
3
+ export type { Leak, RefResolver } from './conformance.js';
4
+ export { leakProbe, refResolver } from './conformance.js';
5
+ export { createEngine } from './engine.js';
6
+ export { helpShown, hintCount, isGradedElement, nextHint, resultHelp } from './help.js';
7
+ export { parseHtml, serializeHtml } from './html.js';
8
+ export * as kit from './kit/index.js';
9
+ export { engineMessages } from './messages.js';
10
+ export type { ChoiceKey, FlagKey, LetterKey, NumberKey, ProfileData, TextKey } from './slot-kinds.js';
11
+ export { choiceSlot, flagSlot, letterSlot, numberSlot, textSlot } from './slot-kinds.js';
12
+ export { normalise, parseNumber } from './text.js';
13
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AACjD,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAC5C,YAAY,EAAE,IAAI,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC1D,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,eAAe,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACxF,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AACrD,OAAO,KAAK,GAAG,MAAM,gBAAgB,CAAC;AACtC,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAC/C,YAAY,EAAE,SAAS,EAAE,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AACtG,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,UAAU,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AACzF,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,10 @@
1
+ export { localChecker } from './checker.js';
2
+ export { leakProbe, refResolver } from './conformance.js';
3
+ export { createEngine } from './engine.js';
4
+ export { helpShown, hintCount, isGradedElement, nextHint, resultHelp } from './help.js';
5
+ export { parseHtml, serializeHtml } from './html.js';
6
+ export * as kit from './kit/index.js';
7
+ export { engineMessages } from './messages.js';
8
+ export { choiceSlot, flagSlot, letterSlot, numberSlot, textSlot } from './slot-kinds.js';
9
+ export { normalise, parseNumber } from './text.js';
10
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAE5C,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,eAAe,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACxF,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AACrD,OAAO,KAAK,GAAG,MAAM,gBAAgB,CAAC;AACtC,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAE/C,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,UAAU,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AACzF,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC"}