@astl/engine 0.5.1 → 0.7.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.
package/src/analyse.ts CHANGED
@@ -10,6 +10,12 @@ import {
10
10
  BADGE_SHAPES,
11
11
  BADGE_TAG,
12
12
  BLOCK_TAGS,
13
+ BOX_ICONS,
14
+ BOX_LABEL_TAG,
15
+ BOX_TAG,
16
+ BOX_TITLE_PLACES,
17
+ BOX_TITLE_TAG,
18
+ BOX_VARIANTS,
13
19
  CAPTION_TAG,
14
20
  CHECK_ATTRIBUTES,
15
21
  CHECK_FOR_ATTRIBUTE,
@@ -45,6 +51,7 @@ import {
45
51
  HELP_TAGS,
46
52
  HINT_TAG,
47
53
  INLINE_TAGS,
54
+ INSTRUCTION_TAG,
48
55
  type Issue,
49
56
  type IssueParams,
50
57
  isCheckable,
@@ -57,6 +64,10 @@ import {
57
64
  MAX_COLUMNS,
58
65
  MAX_NESTING_DEPTH,
59
66
  MAX_SOURCE_LENGTH,
67
+ MAX_TABLE_COLUMNS,
68
+ MAX_TABLE_ROWS,
69
+ MAX_TASK_LABELS,
70
+ MAX_TASK_LEVEL,
60
71
  MIN_COLUMNS,
61
72
  type Node,
62
73
  type NormalisationProfile,
@@ -71,8 +82,24 @@ import {
71
82
  STYLE_TEMPLATE_ATTRIBUTE,
72
83
  type StyleScope,
73
84
  type StyleTemplate,
85
+ SUBTASK_LEVELS,
86
+ SUBTASK_TAG,
74
87
  SUPPORTED_FORMATS,
88
+ TABLE_BODY_TAG,
89
+ TABLE_CAPTION_TAG,
90
+ TABLE_CELL_TAG,
91
+ TABLE_HEAD_TAG,
92
+ TABLE_HEADER_CELL_TAG,
93
+ TABLE_ROW_TAG,
94
+ TABLE_SCOPES,
95
+ TABLE_TAG,
96
+ TABLE_TAGS,
75
97
  TAG_ATTRIBUTES,
98
+ TASK_LABEL_ICONS,
99
+ TASK_LABEL_TAG,
100
+ TASK_NUMBERING_ATTRIBUTE,
101
+ TASK_NUMBERINGS,
102
+ TASK_TAG,
76
103
  URL_ATTRIBUTES,
77
104
  type UrlPolicy,
78
105
  } from '@astl/core';
@@ -96,13 +123,16 @@ import { mostAttributes } from './limits.js';
96
123
  import { round6 } from './round.js';
97
124
 
98
125
  /**
99
- * Where inline components may sit: text-holding blocks, captions and inline formatting, never inside a link (and a
100
- * rule, a line break or a picture holds nothing).
126
+ * Where inline components may sit: text-holding blocks, captions, table cells and inline formatting, never inside a
127
+ * link (and a rule, a line break or a picture holds nothing).
101
128
  */
102
129
  const TEXT_PARENTS: ReadonlySet<string> = new Set([
103
130
  ...[...BLOCK_TAGS].filter((t) => t !== 'ul' && t !== 'ol' && t !== RULE_TAG),
104
131
  ...[...INLINE_TAGS].filter((t) => t !== 'a' && t !== 'br' && t !== 'img'),
105
132
  CAPTION_TAG,
133
+ TABLE_CAPTION_TAG,
134
+ TABLE_HEADER_CELL_TAG,
135
+ TABLE_CELL_TAG,
106
136
  ]);
107
137
 
108
138
  const DIRECTIONS: ReadonlySet<string> = new Set(['ltr', 'rtl', 'auto']);
@@ -118,6 +148,14 @@ const RULE_TARGETS: ReadonlySet<string> = new Set([
118
148
  FIGURE_TAG,
119
149
  CAPTION_TAG,
120
150
  BADGE_TAG,
151
+ BOX_TAG,
152
+ BOX_TITLE_TAG,
153
+ BOX_LABEL_TAG,
154
+ TASK_TAG,
155
+ INSTRUCTION_TAG,
156
+ TASK_LABEL_TAG,
157
+ SUBTASK_TAG,
158
+ ...TABLE_TAGS,
121
159
  ]);
122
160
  /** The contrast WCAG asks of normal text (AA); large text may have less, and then the warning is only stricter. */
123
161
  const MIN_CONTRAST = 4.5;
@@ -233,6 +271,12 @@ interface Scope {
233
271
  readonly mode: 'flow' | 'inline' | 'text';
234
272
  /** The component whose child content this is. */
235
273
  readonly host: Host | null;
274
+ /** In a frame: where the frame stands (`column`: in a layout's column, however deep; `top`: elsewhere); null outside. */
275
+ readonly box: 'top' | 'column' | null;
276
+ /** In a layout's column, however deep: no layout in it (two layout levels at most). */
277
+ readonly column: boolean;
278
+ /** In a task: no task in it. */
279
+ readonly task: boolean;
236
280
  /** Inside help: its tag and the id of the interaction it belongs to (null for a page's). */
237
281
  readonly help: { readonly tag: string; readonly element: string | null } | null;
238
282
  readonly page: string | null;
@@ -265,6 +309,63 @@ interface Where {
265
309
 
266
310
  type Visitor = (el: ElementNode, place: Place) => Node[] | null;
267
311
 
312
+ /** A cell's `colspan` or `rowspan`: 1 when absent, 0 when it is no whole number from 1 to `max`. */
313
+ function tableSpan(value: string | undefined, max: number): number {
314
+ if (value === undefined) return 1;
315
+ const n = parseIntegerValue(value);
316
+ return n !== null && n >= 1 && n <= max ? n : 0;
317
+ }
318
+
319
+ /**
320
+ * The columns a table's rows cover, row by row as HTML lays them out: a cell takes the first column no span from a row
321
+ * above holds, and as many as its `colspan`; its `rowspan` holds them in the rows below, within its part (a head, a
322
+ * body, the rows standing directly in the table).
323
+ */
324
+ class TableGrid {
325
+ rows = 0;
326
+ /** The table's columns: the most any row covers. */
327
+ columns = 0;
328
+ /** What the first row covered: every other row must cover as much. */
329
+ #width: number | null = null;
330
+ /** Per column, how many more rows a span from above holds it. */
331
+ #held: number[] = [];
332
+
333
+ /** A new part: spans never cross into it. */
334
+ start(): void {
335
+ this.#held = [];
336
+ }
337
+
338
+ /** The end of a part: whether a span reached past its last row. */
339
+ end(): boolean {
340
+ const past = this.#held.some((n) => n > 0);
341
+ this.#held = [];
342
+ return past;
343
+ }
344
+
345
+ /** One row of cells (`[colspan, rowspan]`, 0 for an invalid span, counted as 1): whether it fits the table. */
346
+ row(cells: readonly (readonly [number, number])[]): boolean {
347
+ this.rows++;
348
+ const above = this.#held.map((n) => n > 0);
349
+ const held = this.#held.map((n) => Math.max(0, n - 1));
350
+ let fits = true;
351
+ let at = 0;
352
+ for (const [colspan, rowspan] of cells) {
353
+ while (above[at]) at++;
354
+ for (let column = at; column < at + Math.max(1, colspan); column++) {
355
+ // A cell running into a column a span from above holds.
356
+ if (above[column]) fits = false;
357
+ held[column] = Math.max(held[column] ?? 0, Math.max(1, rowspan) - 1);
358
+ }
359
+ at += Math.max(1, colspan);
360
+ }
361
+ this.#held = Array.from({ length: Math.max(held.length, at) }, (_, i) => held[i] ?? 0);
362
+ const width = Math.max(at, above.lastIndexOf(true) + 1);
363
+ this.columns = Math.max(this.columns, width);
364
+ this.#width ??= width;
365
+ return fits && width === this.#width;
366
+ }
367
+ }
368
+
268
369
  function own(attrs: Readonly<Record<string, string>>, name: string): string | undefined {
269
370
  return Object.hasOwn(attrs, name) ? attrs[name] : undefined;
270
371
  }
@@ -284,7 +385,13 @@ function isBlank(nodes: readonly Node[]): boolean {
284
385
  n.type === 'text'
285
386
  ? n.value.trim() === ''
286
387
  : n.tag === 'br' ||
287
- ((n.tag === 'p' || n.tag === COLUMNS_TAG || n.tag === COLUMN_TAG) && isBlank(n.children)),
388
+ ((n.tag === 'p' ||
389
+ n.tag === COLUMNS_TAG ||
390
+ n.tag === COLUMN_TAG ||
391
+ n.tag === BOX_TAG ||
392
+ n.tag === TASK_TAG ||
393
+ n.tag === SUBTASK_TAG) &&
394
+ isBlank(n.children)),
288
395
  );
289
396
  }
290
397
 
@@ -303,7 +410,14 @@ function isSupportedFormat(value: string): boolean {
303
410
 
304
411
  function placementAllowed(display: 'inline' | 'block', scope: Scope): boolean {
305
412
  if (display === 'inline') return !scope.inLink && TEXT_PARENTS.has(scope.parent);
306
- return scope.parent === PAGE_TAG || scope.parent === DOC_TAG || scope.parent === COLUMN_TAG;
413
+ return (
414
+ scope.parent === PAGE_TAG ||
415
+ scope.parent === DOC_TAG ||
416
+ scope.parent === COLUMN_TAG ||
417
+ scope.parent === BOX_TAG ||
418
+ scope.parent === TASK_TAG ||
419
+ scope.parent === SUBTASK_TAG
420
+ );
307
421
  }
308
422
 
309
423
  function outOfRange(value: number, spec: AttributeSpec): boolean {
@@ -373,6 +487,11 @@ class Analyser {
373
487
  /** The help placed directly on each page, by page index. */
374
488
  private readonly pageHelp = new Map<number, HelpParts>();
375
489
  private explicitPages = false;
490
+ /** How the tasks are numbered (`task-numbering`), and the number of the last one so far. */
491
+ private taskNumbering: (typeof TASK_NUMBERINGS)[number] = 'page';
492
+ private taskNumber = 0;
493
+ /** The tasks with a Check of their own, with their ids (their Check's). */
494
+ private readonly checkedTasks = new Map<ElementNode, string>();
376
495
  /** While help content is read (`visitHelp`): it takes no style (see `inline`). */
377
496
  private inHelp = false;
378
497
  /** The components that declare each child tag, as listed in CHILD_OUTSIDE. */
@@ -446,6 +565,11 @@ class Analyser {
446
565
  } else if (name === 'kind') {
447
566
  if ((DOC_KINDS as readonly string[]).includes(value)) attrs.kind = value;
448
567
  else this.invalid(DOC_TAG, name, value, 'enum', DOC_KINDS, onRoot);
568
+ } else if (name === TASK_NUMBERING_ATTRIBUTE) {
569
+ // The engine numbers the tasks into the view: the player needs no more of it.
570
+ if ((TASK_NUMBERINGS as readonly string[]).includes(value))
571
+ this.taskNumbering = value as (typeof TASK_NUMBERINGS)[number];
572
+ else this.invalid(DOC_TAG, name, value, 'enum', TASK_NUMBERINGS, onRoot);
449
573
  } else {
450
574
  attrs[name] = value;
451
575
  if (name === STYLE_TEMPLATE_ATTRIBUTE) this.styleTemplate(value, DOC_TAG, 'document', onRoot);
@@ -469,6 +593,9 @@ class Analyser {
469
593
  inLink: false,
470
594
  mode: 'flow',
471
595
  host: null,
596
+ box: null,
597
+ column: false,
598
+ task: false,
472
599
  help: null,
473
600
  page: null,
474
601
  pageIndex: 0,
@@ -552,7 +679,7 @@ class Analyser {
552
679
  const met = new Map(this.checkNodes.map((c) => [c.node, c]));
553
680
  const named = new Map<ElementNode, string[] | null>();
554
681
  for (const { node, page, where } of this.checkNodes) {
555
- const id = own(node.attrs, 'id') ?? '';
682
+ const id = this.checkedTasks.get(node) ?? own(node.attrs, 'id') ?? '';
556
683
  if (own(node.attrs, CHECK_FOR_ATTRIBUTE) === undefined) {
557
684
  named.set(node, null);
558
685
  continue;
@@ -584,11 +711,13 @@ class Analyser {
584
711
  const record = this.components.get(node);
585
712
  return {
586
713
  id: record?.sourceId ?? null,
587
- graded: isCheckable(definition.manifest),
714
+ // An example is answered already: nothing for a Check.
715
+ graded: isCheckable(definition.manifest) && definition.example?.(node) !== true,
588
716
  block: definition.manifest.display === 'block',
589
717
  };
590
718
  },
591
719
  checkFor: (node) => (node.type === 'element' ? (named.get(node) ?? null) : null),
720
+ taskCheck: (node) => node.type === 'element' && this.checkedTasks.has(node),
592
721
  };
593
722
  const pages = this.explicitPages ? [...pageNodes.keys()].map((page) => page.children) : [loose];
594
723
  pages.forEach((content, index) => {
@@ -598,7 +727,7 @@ class Analyser {
598
727
  nodes.forEach((node, k) => {
599
728
  const check = met.get(node);
600
729
  if (check === undefined) return;
601
- const id = own(node.attrs, 'id') ?? '';
730
+ const id = this.checkedTasks.get(node) ?? own(node.attrs, 'id') ?? '';
602
731
  for (const target of plan.twice[k] ?? []) {
603
732
  this.add(EngineIssue.CHECK_TARGET_TWICE, { check: id, id: target }, check.where, 'warning');
604
733
  }
@@ -852,6 +981,8 @@ class Analyser {
852
981
  return [];
853
982
  }
854
983
  if (tag === BADGE_TAG) return this.visitBadge(el, scope, place, where);
984
+ if (tag === BOX_TAG) return this.visitBox(el, scope, place, where);
985
+ if (tag === TASK_TAG) return this.visitTask(el, scope, place, where);
855
986
  if (BLOCK_TAGS.has(tag) || INLINE_TAGS.has(tag)) {
856
987
  if (scope.mode === 'text' || (scope.mode === 'inline' && BLOCK_TAGS.has(tag))) {
857
988
  this.add(EngineIssue.ELEMENT_REMOVED, { tag }, where, 'warning');
@@ -861,6 +992,16 @@ class Analyser {
861
992
  }
862
993
  // A figure is a block: in a prompt or an option it is unwrapped like the other blocks, below.
863
994
  if (tag === FIGURE_TAG && scope.mode === 'flow') return [this.visitFigure(el, scope, place, where)];
995
+ if (tag === TABLE_TAG) {
996
+ return scope.mode === 'flow'
997
+ ? [this.visitTable(el, scope, place, where)]
998
+ : this.unwrapTable(el, scope, place, where);
999
+ }
1000
+ // A part of a table out of its table (a cell outside a row): its content stays, to be validated.
1001
+ if (TABLE_TAGS.has(tag)) {
1002
+ this.add(EngineIssue.TABLE_INVALID, {}, where);
1003
+ return this.visitNodes(el, scope, place.path);
1004
+ }
864
1005
  if (tag.includes('-')) {
865
1006
  const host = scope.host;
866
1007
  const spec = host?.manifest.children.find((c) => c.tag === tag);
@@ -883,6 +1024,18 @@ class Analyser {
883
1024
  return [];
884
1025
  }
885
1026
  if (tag === COLUMNS_TAG) return [this.visitColumns(el, scope, place, where)];
1027
+ // A frame's title or tag, a task's instruction, tag or sub-task out of its place (after the content, twice,
1028
+ // outside its frame or task): its content stays.
1029
+ if (
1030
+ tag === BOX_TITLE_TAG ||
1031
+ tag === BOX_LABEL_TAG ||
1032
+ tag === INSTRUCTION_TAG ||
1033
+ tag === TASK_LABEL_TAG ||
1034
+ tag === SUBTASK_TAG
1035
+ ) {
1036
+ this.add(EngineIssue.PLACEMENT_INVALID, { tag }, where);
1037
+ return this.visitNodes(el, scope, place.path);
1038
+ }
886
1039
  // A column without its layout (`astl-match` has columns of its own, which it reads itself). The content stays,
887
1040
  // to be validated.
888
1041
  if (tag === COLUMN_TAG && !(host && spec)) {
@@ -910,6 +1063,9 @@ class Analyser {
910
1063
  scope.help === null &&
911
1064
  (scope.parent === PAGE_TAG ||
912
1065
  scope.parent === COLUMN_TAG ||
1066
+ scope.parent === BOX_TAG ||
1067
+ scope.parent === TASK_TAG ||
1068
+ scope.parent === SUBTASK_TAG ||
913
1069
  (scope.parent === DOC_TAG && !this.explicitPages));
914
1070
  if (!onPage) {
915
1071
  this.add(EngineIssue.PLACEMENT_INVALID, { tag: CHECK_TAG }, where);
@@ -943,7 +1099,14 @@ class Analyser {
943
1099
  private visitBadge(el: ElementNode, scope: Scope, place: Place, where: Where): Node[] {
944
1100
  const id = own(el.attrs, 'id');
945
1101
  const at: Where = { ...where, element: id ?? null };
946
- const inText = TEXT_PARENTS.has(scope.parent) && !scope.inLink && scope.mode !== 'text';
1102
+ const inText =
1103
+ (TEXT_PARENTS.has(scope.parent) ||
1104
+ scope.parent === BOX_TITLE_TAG ||
1105
+ scope.parent === BOX_LABEL_TAG ||
1106
+ scope.parent === INSTRUCTION_TAG ||
1107
+ scope.parent === TASK_LABEL_TAG) &&
1108
+ !scope.inLink &&
1109
+ scope.mode !== 'text';
947
1110
  if (!inText || (scope.host !== null && scope.mode !== 'flow')) {
948
1111
  this.add(EngineIssue.PLACEMENT_INVALID, { tag: BADGE_TAG }, at);
949
1112
  return this.visitNodes(el, scope, place.path);
@@ -998,6 +1161,180 @@ class Analyser {
998
1161
  return element(el.tag, attrs, this.visitNodes(el, inner, place.path));
999
1162
  }
1000
1163
 
1164
+ /**
1165
+ * A frame: directly on a page, in the document without pages or in a column, outside components and help; in another
1166
+ * frame it is `BOX_NESTED`, elsewhere `PLACEMENT_INVALID`, and its content is validated where it stands. Its title and
1167
+ * its tag lead it, inline text without components (a badge, a picture); then content as a page holds it.
1168
+ */
1169
+ private visitBox(el: ElementNode, scope: Scope, place: Place, where: Where): Node[] {
1170
+ if (scope.box !== null) {
1171
+ this.add(EngineIssue.BOX_NESTED, { tag: BOX_TAG }, where);
1172
+ return this.visitNodes(el, scope, place.path);
1173
+ }
1174
+ const placed =
1175
+ scope.host === null &&
1176
+ scope.help === null &&
1177
+ scope.mode === 'flow' &&
1178
+ (scope.parent === PAGE_TAG ||
1179
+ scope.parent === COLUMN_TAG ||
1180
+ scope.parent === TASK_TAG ||
1181
+ scope.parent === SUBTASK_TAG ||
1182
+ (scope.parent === DOC_TAG && !this.explicitPages));
1183
+ if (!placed) {
1184
+ this.add(EngineIssue.PLACEMENT_INVALID, { tag: BOX_TAG }, where);
1185
+ return this.visitNodes(el, scope, place.path);
1186
+ }
1187
+ const attrs = this.richAttributes(el, where, scope);
1188
+ const allowed: Readonly<Record<string, readonly string[]>> = {
1189
+ 'title-place': BOX_TITLE_PLACES,
1190
+ variant: BOX_VARIANTS,
1191
+ icon: BOX_ICONS,
1192
+ };
1193
+ for (const [name, values] of [...Object.entries(allowed), ['palette', null] as const]) {
1194
+ const value = own(attrs, name);
1195
+ if (value === undefined) continue;
1196
+ const palette = parseIntegerValue(value);
1197
+ const valid =
1198
+ values === null
1199
+ ? palette !== null && palette >= 1 && palette <= MAX_BADGE_PALETTE
1200
+ : values.includes(value);
1201
+ if (valid) continue;
1202
+ this.invalid(BOX_TAG, name, value, values === null ? 'integer' : 'enum', values ?? [], where);
1203
+ delete attrs[name];
1204
+ }
1205
+ const inner: Scope = {
1206
+ ...scope,
1207
+ ...this.below(attrs, scope),
1208
+ parent: BOX_TAG,
1209
+ box: scope.column ? 'column' : 'top',
1210
+ };
1211
+ // The title, then the tag, may only lead: before the first content (text or any other element).
1212
+ const content = el.children.findIndex((n) =>
1213
+ n.type === 'text' ? !isHtmlWhitespace(n.value) : n.tag !== BOX_TITLE_TAG && n.tag !== BOX_LABEL_TAG,
1214
+ );
1215
+ let led = '';
1216
+ const children = this.visitNodes(el, inner, place.path, (child, childPlace) => {
1217
+ const head = child.tag === BOX_TITLE_TAG || child.tag === BOX_LABEL_TAG;
1218
+ const leads = content === -1 || el.children.indexOf(child) < content;
1219
+ const order = child.tag === BOX_TITLE_TAG ? led === '' : led !== BOX_LABEL_TAG;
1220
+ if (!head || !leads || !order) return this.visitElement(child, inner, childPlace);
1221
+ led = child.tag;
1222
+ const at = this.where(inner, childPlace, child.tag);
1223
+ const headAttrs = this.richAttributes(child, at, inner);
1224
+ const text: Scope = { ...inner, ...this.below(headAttrs, inner), parent: child.tag, mode: 'inline' };
1225
+ return [element(child.tag, headAttrs, this.visitNodes(child, text, childPlace.path))];
1226
+ });
1227
+ return [element(BOX_TAG, attrs, children)];
1228
+ }
1229
+
1230
+ /**
1231
+ * A task: directly on a page, in the document without pages, in a column or in a frame, outside components and help;
1232
+ * in another task it is `TASK_NESTED`, elsewhere `PLACEMENT_INVALID`, and its content is validated where it stands.
1233
+ * Its id is required: its Check is addressed by it. Its instruction, then its tags (up to MAX_TASK_LABELS), may only
1234
+ * lead it; out of place, each is `PLACEMENT_INVALID` and its text stays. Then a page's content, its sub-tasks among
1235
+ * it. The view has its number: its own `number`, after which the next ones count on, else the next of its page or of
1236
+ * the document (`task-numbering`), none with `none`. A task with `check` has a Check of its own for all its graded
1237
+ * questions, which the view holds as a Check element at its end.
1238
+ */
1239
+ private visitTask(el: ElementNode, scope: Scope, place: Place, where: Where): Node[] {
1240
+ const id = own(el.attrs, 'id');
1241
+ const at: Where = { ...where, element: id ?? null };
1242
+ if (scope.task) {
1243
+ // One error for the task: its head's content and its sub-tasks stay, as the outer task's.
1244
+ this.add(EngineIssue.TASK_NESTED, { tag: TASK_TAG }, at);
1245
+ return this.visitNodes(el, scope, place.path, (child, childPlace) =>
1246
+ child.tag === SUBTASK_TAG
1247
+ ? [this.visitSubtask(child, scope, childPlace)]
1248
+ : child.tag === INSTRUCTION_TAG || child.tag === TASK_LABEL_TAG
1249
+ ? this.visitNodes(child, scope, childPlace.path)
1250
+ : null,
1251
+ );
1252
+ }
1253
+ const placed =
1254
+ scope.host === null &&
1255
+ scope.help === null &&
1256
+ scope.mode === 'flow' &&
1257
+ (scope.parent === PAGE_TAG ||
1258
+ scope.parent === COLUMN_TAG ||
1259
+ scope.parent === BOX_TAG ||
1260
+ (scope.parent === DOC_TAG && !this.explicitPages));
1261
+ if (!placed) {
1262
+ this.add(EngineIssue.PLACEMENT_INVALID, { tag: TASK_TAG }, at);
1263
+ return this.visitNodes(el, scope, place.path);
1264
+ }
1265
+ if (id === undefined) this.add(EngineIssue.ID_MISSING, { tag: TASK_TAG }, at);
1266
+ else this.sourceId(id, TASK_TAG, at);
1267
+ const attrs = this.richAttributes(
1268
+ element(TASK_TAG, Object.fromEntries(Object.entries(el.attrs).filter(([name]) => name !== 'id')), []),
1269
+ at,
1270
+ scope,
1271
+ );
1272
+ const bounded = (name: string, max: number): number | null => {
1273
+ const value = own(attrs, name);
1274
+ if (value === undefined) return null;
1275
+ const n = parseIntegerValue(value);
1276
+ if (n !== null && n >= 1 && n <= max) return n;
1277
+ this.invalid(TASK_TAG, name, value, 'integer', [], at);
1278
+ delete attrs[name];
1279
+ return null;
1280
+ };
1281
+ bounded('level', MAX_TASK_LEVEL);
1282
+ bounded('palette', MAX_BADGE_PALETTE);
1283
+ const written = bounded('number', Number.MAX_SAFE_INTEGER);
1284
+ // A boolean: present or not, whatever its value. The view holds the Check itself, at the end of the task.
1285
+ const check = Object.hasOwn(attrs, 'check');
1286
+ delete attrs.check;
1287
+ if (written !== null) this.taskNumber = written;
1288
+ else if (this.taskNumbering !== 'none') this.taskNumber += 1;
1289
+ if (written !== null || this.taskNumbering !== 'none') attrs.number = String(this.taskNumber);
1290
+ const inner: Scope = { ...scope, ...this.below(attrs, scope), parent: TASK_TAG, task: true };
1291
+ // The instruction, then the tags, may only lead: before the first content (text or any other element).
1292
+ const content = el.children.findIndex((n) =>
1293
+ n.type === 'text' ? !isHtmlWhitespace(n.value) : n.tag !== INSTRUCTION_TAG && n.tag !== TASK_LABEL_TAG,
1294
+ );
1295
+ let led = false;
1296
+ let labels = 0;
1297
+ const children = this.visitNodes(el, inner, place.path, (child, childPlace) => {
1298
+ if (child.tag === SUBTASK_TAG) return [this.visitSubtask(child, inner, childPlace)];
1299
+ const head = child.tag === INSTRUCTION_TAG || child.tag === TASK_LABEL_TAG;
1300
+ const leads = content === -1 || el.children.indexOf(child) < content;
1301
+ const order = child.tag === INSTRUCTION_TAG ? !led : labels < MAX_TASK_LABELS;
1302
+ if (!head || !leads || !order) return this.visitElement(child, inner, childPlace);
1303
+ led = true;
1304
+ const where = this.where(inner, childPlace, child.tag);
1305
+ const headAttrs = this.richAttributes(child, where, inner);
1306
+ if (child.tag === TASK_LABEL_TAG) {
1307
+ labels++;
1308
+ const icon = own(headAttrs, 'icon');
1309
+ if (icon !== undefined && !(TASK_LABEL_ICONS as readonly string[]).includes(icon)) {
1310
+ this.invalid(TASK_LABEL_TAG, 'icon', icon, 'enum', TASK_LABEL_ICONS, where);
1311
+ delete headAttrs.icon;
1312
+ }
1313
+ }
1314
+ const text: Scope = { ...inner, ...this.below(headAttrs, inner), parent: child.tag, mode: 'inline' };
1315
+ return [element(child.tag, headAttrs, this.visitNodes(child, text, childPlace.path))];
1316
+ });
1317
+ const node = element(TASK_TAG, attrs, children);
1318
+ if (check) {
1319
+ this.checkNodes.push({ node, page: scope.pageIndex, where: at });
1320
+ this.checkedTasks.set(node, id ?? '');
1321
+ }
1322
+ return [node];
1323
+ }
1324
+
1325
+ /** A sub-task: a lettered part of a task, a page's content in it; its `level`, A to D, shows beside its letter. */
1326
+ private visitSubtask(el: ElementNode, scope: Scope, place: Place): ElementNode {
1327
+ const where = this.where(scope, place, SUBTASK_TAG);
1328
+ const attrs = this.richAttributes(el, where, scope);
1329
+ const level = own(attrs, 'level');
1330
+ if (level !== undefined && !(SUBTASK_LEVELS as readonly string[]).includes(level)) {
1331
+ this.invalid(SUBTASK_TAG, 'level', level, 'enum', SUBTASK_LEVELS, where);
1332
+ delete attrs.level;
1333
+ }
1334
+ const inner: Scope = { ...scope, ...this.below(attrs, scope), parent: SUBTASK_TAG };
1335
+ return element(SUBTASK_TAG, attrs, this.visitNodes(el, inner, place.path));
1336
+ }
1337
+
1001
1338
  /**
1002
1339
  * A layout: 2 to 4 columns, directly on a page or in the document, outside components. Whatever its shape, its
1003
1340
  * content is still validated, each piece as what it is.
@@ -1014,11 +1351,19 @@ class Analyser {
1014
1351
  }
1015
1352
  columns++;
1016
1353
  const column = this.richAttributes(child, this.where(inner, childPlace, COLUMN_TAG), inner);
1017
- const content: Scope = { ...inner, ...this.below(column, inner), parent: COLUMN_TAG };
1354
+ const content: Scope = { ...inner, ...this.below(column, inner), parent: COLUMN_TAG, column: true };
1018
1355
  return [element(COLUMN_TAG, column, this.visitNodes(child, content, childPlace.path))];
1019
1356
  });
1020
1357
  if (children.some((n) => n.type === 'text' && !isHtmlWhitespace(n.value))) stray = true;
1021
- const placed = scope.host === null && (scope.parent === PAGE_TAG || scope.parent === DOC_TAG);
1358
+ // In a frame or a task too, never in a column, however deep: two layout levels at most.
1359
+ const placed =
1360
+ scope.host === null &&
1361
+ !scope.column &&
1362
+ (scope.parent === PAGE_TAG ||
1363
+ scope.parent === DOC_TAG ||
1364
+ scope.parent === BOX_TAG ||
1365
+ scope.parent === TASK_TAG ||
1366
+ scope.parent === SUBTASK_TAG);
1022
1367
  if (!placed || stray || columns < MIN_COLUMNS || columns > MAX_COLUMNS) {
1023
1368
  this.add(EngineIssue.COLUMNS_INVALID, { tag: COLUMNS_TAG }, where);
1024
1369
  }
@@ -1052,6 +1397,13 @@ class Analyser {
1052
1397
  this.invalid(el.tag, name, value, 'integer', [], where);
1053
1398
  } else if ((name === 'width' || name === 'height') && !((parseIntegerValue(value) ?? 0) > 0)) {
1054
1399
  this.invalid(el.tag, name, value, 'integer', [], where);
1400
+ } else if (
1401
+ (name === 'colspan' || name === 'rowspan') &&
1402
+ tableSpan(value, name === 'colspan' ? MAX_TABLE_COLUMNS : MAX_TABLE_ROWS) === 0
1403
+ ) {
1404
+ this.invalid(el.tag, name, value, 'integer', [], where);
1405
+ } else if (name === 'scope' && !(TABLE_SCOPES as readonly string[]).includes(value)) {
1406
+ this.invalid(el.tag, name, value, 'enum', TABLE_SCOPES, where);
1055
1407
  } else {
1056
1408
  attrs[name] = value;
1057
1409
  }
@@ -1067,19 +1419,11 @@ class Analyser {
1067
1419
  }
1068
1420
 
1069
1421
  /**
1070
- * A figure: directly in a page, a column or the document (a block), or in a rich block, with exactly one picture and at most
1071
- * one caption. A caption holds rich inline text, which may hold inline components like any text.
1422
+ * A figure: directly in a page, a column, a frame or the document (a block), or in a rich block, with exactly one picture
1423
+ * and at most one caption. A caption holds rich inline text, which may hold inline components like any text.
1072
1424
  */
1073
1425
  private visitFigure(el: ElementNode, scope: Scope, place: Place, where: Where): ElementNode {
1074
- if (
1075
- scope.parent !== PAGE_TAG &&
1076
- scope.parent !== DOC_TAG &&
1077
- scope.parent !== COLUMN_TAG &&
1078
- scope.parent !== scope.host?.manifest.tag &&
1079
- !HELP_TAGS.has(scope.parent)
1080
- ) {
1081
- this.add(EngineIssue.PLACEMENT_INVALID, { tag: el.tag }, where);
1082
- }
1426
+ if (!this.blockPlace(scope)) this.add(EngineIssue.PLACEMENT_INVALID, { tag: el.tag }, where);
1083
1427
  const attrs = this.richAttributes(el, where, scope);
1084
1428
  const inner: Scope = { ...scope, ...this.below(attrs, scope), parent: FIGURE_TAG };
1085
1429
  let pictures = 0;
@@ -1100,6 +1444,150 @@ class Analyser {
1100
1444
  return element(FIGURE_TAG, attrs, children);
1101
1445
  }
1102
1446
 
1447
+ /** Where a figure or a table may stand: directly in a page, the document, a column, a frame, a task, a rich block or help. */
1448
+ private blockPlace(scope: Scope): boolean {
1449
+ return (
1450
+ scope.parent === PAGE_TAG ||
1451
+ scope.parent === DOC_TAG ||
1452
+ scope.parent === COLUMN_TAG ||
1453
+ scope.parent === BOX_TAG ||
1454
+ scope.parent === TASK_TAG ||
1455
+ scope.parent === SUBTASK_TAG ||
1456
+ scope.parent === scope.host?.manifest.tag ||
1457
+ HELP_TAGS.has(scope.parent)
1458
+ );
1459
+ }
1460
+
1461
+ /**
1462
+ * A table, where a figure may stand. Its caption (first, once) holds rich inline text; then a head (once, before the
1463
+ * bodies) and bodies of rows, or rows directly in it (one body until the next part); a row holds header and data
1464
+ * cells, whose content is rich inline text with inline components in it. A part out of its place, text between
1465
+ * rows or cells, rows that cover different columns, a span past the rows of its part or no rows at all are
1466
+ * TABLE_INVALID, reported at the part; more than MAX_TABLE_COLUMNS columns or MAX_TABLE_ROWS rows TABLE_TOO_LARGE.
1467
+ */
1468
+ private visitTable(el: ElementNode, scope: Scope, place: Place, where: Where): ElementNode {
1469
+ if (!this.blockPlace(scope)) this.add(EngineIssue.PLACEMENT_INVALID, { tag: el.tag }, where);
1470
+ const attrs = this.richAttributes(el, where, scope);
1471
+ const inner: Scope = { ...scope, ...this.below(attrs, scope), parent: TABLE_TAG };
1472
+ const grid = new TableGrid();
1473
+ // What came last: the order is caption, head, bodies (and rows of their own).
1474
+ const seen: { last: 'start' | 'caption' | 'head' | 'body' | 'rows' } = { last: 'start' };
1475
+ const children = this.visitNodes(el, inner, place.path, (child, childPlace) => {
1476
+ const at = this.where(inner, childPlace, child.tag);
1477
+ if (child.tag === TABLE_CAPTION_TAG && seen.last === 'start') {
1478
+ seen.last = 'caption';
1479
+ const caption = this.richAttributes(child, at, inner);
1480
+ const text: Scope = {
1481
+ ...inner,
1482
+ ...this.below(caption, inner),
1483
+ parent: TABLE_CAPTION_TAG,
1484
+ mode: 'inline',
1485
+ };
1486
+ return [element(TABLE_CAPTION_TAG, caption, this.visitNodes(child, text, childPlace.path))];
1487
+ }
1488
+ if (child.tag === TABLE_HEAD_TAG && (seen.last === 'start' || seen.last === 'caption')) {
1489
+ seen.last = 'head';
1490
+ return [this.visitTableSection(child, inner, childPlace, at, grid)];
1491
+ }
1492
+ if (child.tag === TABLE_BODY_TAG) {
1493
+ if (seen.last === 'rows') this.endTableSection(grid, where);
1494
+ seen.last = 'body';
1495
+ return [this.visitTableSection(child, inner, childPlace, at, grid)];
1496
+ }
1497
+ if (child.tag === TABLE_ROW_TAG) {
1498
+ if (seen.last !== 'rows') grid.start();
1499
+ seen.last = 'rows';
1500
+ return [this.visitTableRow(child, inner, childPlace, at, grid)];
1501
+ }
1502
+ this.add(EngineIssue.TABLE_INVALID, {}, at);
1503
+ return [];
1504
+ });
1505
+ if (seen.last === 'rows') this.endTableSection(grid, where);
1506
+ if (children.some((n) => n.type === 'text' && !isHtmlWhitespace(n.value)) || grid.rows === 0) {
1507
+ this.add(EngineIssue.TABLE_INVALID, {}, where);
1508
+ }
1509
+ if (grid.columns > MAX_TABLE_COLUMNS || grid.rows > MAX_TABLE_ROWS) {
1510
+ const params = {
1511
+ columns: grid.columns,
1512
+ rows: grid.rows,
1513
+ maxColumns: MAX_TABLE_COLUMNS,
1514
+ maxRows: MAX_TABLE_ROWS,
1515
+ };
1516
+ this.add(EngineIssue.TABLE_TOO_LARGE, params, where);
1517
+ }
1518
+ return element(TABLE_TAG, attrs, children);
1519
+ }
1520
+
1521
+ /** A head or a body: rows only, its spans ending with it. */
1522
+ private visitTableSection(
1523
+ el: ElementNode,
1524
+ scope: Scope,
1525
+ place: Place,
1526
+ where: Where,
1527
+ grid: TableGrid,
1528
+ ): ElementNode {
1529
+ const attrs = this.richAttributes(el, where, scope);
1530
+ const inner: Scope = { ...scope, ...this.below(attrs, scope), parent: el.tag };
1531
+ grid.start();
1532
+ const children = this.visitNodes(el, inner, place.path, (child, childPlace) => {
1533
+ const at = this.where(inner, childPlace, child.tag);
1534
+ if (child.tag === TABLE_ROW_TAG) return [this.visitTableRow(child, inner, childPlace, at, grid)];
1535
+ this.add(EngineIssue.TABLE_INVALID, {}, at);
1536
+ return [];
1537
+ });
1538
+ if (children.some((n) => n.type === 'text' && !isHtmlWhitespace(n.value))) {
1539
+ this.add(EngineIssue.TABLE_INVALID, {}, where);
1540
+ }
1541
+ this.endTableSection(grid, where);
1542
+ return element(el.tag, attrs, children);
1543
+ }
1544
+
1545
+ /** A span that reaches past the last row of its part. */
1546
+ private endTableSection(grid: TableGrid, where: Where): void {
1547
+ if (grid.end()) this.add(EngineIssue.TABLE_INVALID, {}, where);
1548
+ }
1549
+
1550
+ /** A row: cells only, covering the columns of the rows before it. */
1551
+ private visitTableRow(
1552
+ el: ElementNode,
1553
+ scope: Scope,
1554
+ place: Place,
1555
+ where: Where,
1556
+ grid: TableGrid,
1557
+ ): ElementNode {
1558
+ const attrs = this.richAttributes(el, where, scope);
1559
+ const inner: Scope = { ...scope, ...this.below(attrs, scope), parent: TABLE_ROW_TAG };
1560
+ const spans: (readonly [columns: number, rows: number])[] = [];
1561
+ const children = this.visitNodes(el, inner, place.path, (child, childPlace) => {
1562
+ const at = this.where(inner, childPlace, child.tag);
1563
+ if (child.tag !== TABLE_HEADER_CELL_TAG && child.tag !== TABLE_CELL_TAG) {
1564
+ this.add(EngineIssue.TABLE_INVALID, {}, at);
1565
+ return [];
1566
+ }
1567
+ const cell = this.richAttributes(child, at, inner);
1568
+ spans.push([tableSpan(cell.colspan, MAX_TABLE_COLUMNS), tableSpan(cell.rowspan, MAX_TABLE_ROWS)]);
1569
+ const text: Scope = { ...inner, ...this.below(cell, inner), parent: child.tag, mode: 'inline' };
1570
+ return [element(child.tag, cell, this.visitNodes(child, text, childPlace.path))];
1571
+ });
1572
+ if (children.some((n) => n.type === 'text' && !isHtmlWhitespace(n.value)) || !grid.row(spans)) {
1573
+ this.add(EngineIssue.TABLE_INVALID, {}, where);
1574
+ }
1575
+ return element(TABLE_ROW_TAG, attrs, children);
1576
+ }
1577
+
1578
+ /**
1579
+ * A table in a prompt, an option or a card, where blocks are unwrapped: its parts go, the text of its caption and
1580
+ * cells stays, a space between them.
1581
+ */
1582
+ private unwrapTable(el: ElementNode, scope: Scope, place: Place, where: Where): Node[] {
1583
+ this.add(EngineIssue.ELEMENT_REMOVED, { tag: el.tag }, where, 'warning');
1584
+ const unwrap: Visitor = (child, childPlace) =>
1585
+ TABLE_TAGS.has(child.tag)
1586
+ ? [{ type: 'text', value: ' ' }, ...this.visitNodes(child, scope, childPlace.path, unwrap)]
1587
+ : null;
1588
+ return this.visitNodes(el, scope, place.path, unwrap);
1589
+ }
1590
+
1103
1591
  private visitPage(el: ElementNode, index: number, place: Place, base: Scope): ElementNode {
1104
1592
  const id = own(el.attrs, 'id');
1105
1593
  const where: Where = {
@@ -1132,6 +1620,7 @@ class Analyser {
1132
1620
  pageIndex: index,
1133
1621
  pageName: namePage(index + 1, own(el.attrs, 'label')),
1134
1622
  };
1623
+ if (this.taskNumbering === 'page') this.taskNumber = 0;
1135
1624
  const children = this.visitNodes(el, scope, place.path);
1136
1625
  if (isBlank(children)) this.add(EngineIssue.PAGE_EMPTY, scope.pageName, where, 'warning');
1137
1626
  return element(PAGE_TAG, attrs, children);
@@ -1149,7 +1638,7 @@ class Analyser {
1149
1638
  const id = own(raw.attrs, 'id');
1150
1639
  const where: Where = { order: place.order, page: scope.page, element: id ?? null, tag, path: place.path };
1151
1640
  this.componentCount++;
1152
- if (manifest.graded) this.gradedPages.add(scope.pageIndex);
1641
+ if (manifest.graded && definition.example?.(raw) !== true) this.gradedPages.add(scope.pageIndex);
1153
1642
  if (!placementAllowed(manifest.display, scope)) this.add(EngineIssue.PLACEMENT_INVALID, { tag }, where);
1154
1643
 
1155
1644
  const el = this.upgrade(raw, definition, where);
@@ -1338,7 +1827,7 @@ class Analyser {
1338
1827
  this.add(EngineIssue.COMPONENT_NESTED, { tag: child.tag, parent: tag }, where);
1339
1828
  } else if (child.tag === COLUMNS_TAG || child.tag === COLUMN_TAG) {
1340
1829
  this.add(EngineIssue.COLUMNS_INVALID, { tag: child.tag }, where);
1341
- } else if (child.tag === STYLE_TAG) {
1830
+ } else if (child.tag === STYLE_TAG || child.tag === BOX_TAG || child.tag === TASK_TAG) {
1342
1831
  this.add(EngineIssue.PLACEMENT_INVALID, { tag: child.tag }, where);
1343
1832
  } else if (child.tag.includes('-') && child.tag !== DOC_TAG && child.tag !== PAGE_TAG) {
1344
1833
  // A child another type declares is known, just not accepted here.