@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/dist/analyse.js CHANGED
@@ -2,20 +2,23 @@
2
2
  * One pass over a source that sanitises it and validates it against the registered manifests. `validate` and
3
3
  * `compile` share it: compile only runs on an error-free analysis and works on the sanitised tree it returns.
4
4
  */
5
- import { allAttributes, BADGE_ATTRIBUTES, BADGE_FILLS, BADGE_SHAPES, BADGE_TAG, BLOCK_TAGS, CAPTION_TAG, CHECK_ATTRIBUTES, CHECK_FOR_ATTRIBUTE, CHECK_TAG, CLASS_ATTRIBUTE, COLUMN_TAG, COLUMNS_TAG, CONTENT_ATTRIBUTES, checkFor, checkNodes, checkParts, DEFAULT_DOC_KIND, DOC_ATTRIBUTES, DOC_KINDS, DOC_TAG, directionOf, ENGINE_NAMESPACE, EngineIssue, EXPLANATION_TAG, element, FEEDBACK_TAG, FEEDBACK_VARIANTS, FIGURE_TAG, HELP_TAGS, HINT_TAG, INLINE_TAGS, isCheckable, isElement, isSourceId, LANGUAGE_TAG_PATTERN, MAX_ATTRIBUTES_PER_ELEMENT, MAX_BADGE_LENGTH, MAX_BADGE_PALETTE, MAX_COLUMNS, MAX_NESTING_DEPTH, MAX_SOURCE_LENGTH, MIN_COLUMNS, PAGE_ATTRIBUTES, PAGE_CHECK_ATTRIBUTE, PAGE_TAG, planChecks, REMOVED_WITH_CONTENT, RULE_TAG, STYLE_TAG, STYLE_TEMPLATE_ATTRIBUTE, SUPPORTED_FORMATS, TAG_ATTRIBUTES, URL_ATTRIBUTES, } from '@astl/core';
5
+ import { allAttributes, BADGE_ATTRIBUTES, BADGE_FILLS, BADGE_SHAPES, BADGE_TAG, BLOCK_TAGS, BOX_ICONS, BOX_LABEL_TAG, BOX_TAG, BOX_TITLE_PLACES, BOX_TITLE_TAG, BOX_VARIANTS, CAPTION_TAG, CHECK_ATTRIBUTES, CHECK_FOR_ATTRIBUTE, CHECK_TAG, CLASS_ATTRIBUTE, COLUMN_TAG, COLUMNS_TAG, CONTENT_ATTRIBUTES, checkFor, checkNodes, checkParts, DEFAULT_DOC_KIND, DOC_ATTRIBUTES, DOC_KINDS, DOC_TAG, directionOf, ENGINE_NAMESPACE, EngineIssue, EXPLANATION_TAG, element, FEEDBACK_TAG, FEEDBACK_VARIANTS, FIGURE_TAG, HELP_TAGS, HINT_TAG, INLINE_TAGS, INSTRUCTION_TAG, isCheckable, isElement, isSourceId, LANGUAGE_TAG_PATTERN, MAX_ATTRIBUTES_PER_ELEMENT, MAX_BADGE_LENGTH, MAX_BADGE_PALETTE, MAX_COLUMNS, MAX_NESTING_DEPTH, MAX_SOURCE_LENGTH, MAX_TABLE_COLUMNS, MAX_TABLE_ROWS, MAX_TASK_LABELS, MAX_TASK_LEVEL, MIN_COLUMNS, PAGE_ATTRIBUTES, PAGE_CHECK_ATTRIBUTE, PAGE_TAG, planChecks, REMOVED_WITH_CONTENT, RULE_TAG, STYLE_TAG, STYLE_TEMPLATE_ATTRIBUTE, SUBTASK_LEVELS, SUBTASK_TAG, SUPPORTED_FORMATS, TABLE_BODY_TAG, TABLE_CAPTION_TAG, TABLE_CELL_TAG, TABLE_HEAD_TAG, TABLE_HEADER_CELL_TAG, TABLE_ROW_TAG, TABLE_SCOPES, TABLE_TAG, TABLE_TAGS, TAG_ATTRIBUTES, TASK_LABEL_ICONS, TASK_LABEL_TAG, TASK_NUMBERING_ATTRIBUTE, TASK_NUMBERINGS, TASK_TAG, URL_ATTRIBUTES, } from '@astl/core';
6
6
  import { cleanStyle, isStyleName, MAX_INLINED_STYLES, MAX_STYLE_LENGTH, MAX_STYLE_RULES, mergeStyles, overStyleColor, parseStyleColor, styleContrast, styleText, } from '@astl/core/declarations';
7
7
  import { appendText, isHtmlWhitespace, parseSource } from './html.js';
8
8
  import { isBooleanValue, parseDecimalValue, parseIntegerValue } from './kit/attributes.js';
9
9
  import { mostAttributes } from './limits.js';
10
10
  import { round6 } from './round.js';
11
11
  /**
12
- * Where inline components may sit: text-holding blocks, captions and inline formatting, never inside a link (and a
13
- * rule, a line break or a picture holds nothing).
12
+ * Where inline components may sit: text-holding blocks, captions, table cells and inline formatting, never inside a
13
+ * link (and a rule, a line break or a picture holds nothing).
14
14
  */
15
15
  const TEXT_PARENTS = new Set([
16
16
  ...[...BLOCK_TAGS].filter((t) => t !== 'ul' && t !== 'ol' && t !== RULE_TAG),
17
17
  ...[...INLINE_TAGS].filter((t) => t !== 'a' && t !== 'br' && t !== 'img'),
18
18
  CAPTION_TAG,
19
+ TABLE_CAPTION_TAG,
20
+ TABLE_HEADER_CELL_TAG,
21
+ TABLE_CELL_TAG,
19
22
  ]);
20
23
  const DIRECTIONS = new Set(['ltr', 'rtl', 'auto']);
21
24
  /** The attributes of `<astl-style>`. */
@@ -29,6 +32,14 @@ const RULE_TARGETS = new Set([
29
32
  FIGURE_TAG,
30
33
  CAPTION_TAG,
31
34
  BADGE_TAG,
35
+ BOX_TAG,
36
+ BOX_TITLE_TAG,
37
+ BOX_LABEL_TAG,
38
+ TASK_TAG,
39
+ INSTRUCTION_TAG,
40
+ TASK_LABEL_TAG,
41
+ SUBTASK_TAG,
42
+ ...TABLE_TAGS,
32
43
  ]);
33
44
  /** The contrast WCAG asks of normal text (AA); large text may have less, and then the warning is only stricter. */
34
45
  const MIN_CONTRAST = 4.5;
@@ -39,6 +50,61 @@ const noHelp = () => ({ hints: [], explanation: null, feedback: {}, where: [] })
39
50
  const helpRecord = (parts) => parts.where.length === 0
40
51
  ? null
41
52
  : { hints: parts.hints, explanation: parts.explanation, feedback: parts.feedback };
53
+ /** A cell's `colspan` or `rowspan`: 1 when absent, 0 when it is no whole number from 1 to `max`. */
54
+ function tableSpan(value, max) {
55
+ if (value === undefined)
56
+ return 1;
57
+ const n = parseIntegerValue(value);
58
+ return n !== null && n >= 1 && n <= max ? n : 0;
59
+ }
60
+ /**
61
+ * 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
62
+ * above holds, and as many as its `colspan`; its `rowspan` holds them in the rows below, within its part (a head, a
63
+ * body, the rows standing directly in the table).
64
+ */
65
+ class TableGrid {
66
+ rows = 0;
67
+ /** The table's columns: the most any row covers. */
68
+ columns = 0;
69
+ /** What the first row covered: every other row must cover as much. */
70
+ #width = null;
71
+ /** Per column, how many more rows a span from above holds it. */
72
+ #held = [];
73
+ /** A new part: spans never cross into it. */
74
+ start() {
75
+ this.#held = [];
76
+ }
77
+ /** The end of a part: whether a span reached past its last row. */
78
+ end() {
79
+ const past = this.#held.some((n) => n > 0);
80
+ this.#held = [];
81
+ return past;
82
+ }
83
+ /** One row of cells (`[colspan, rowspan]`, 0 for an invalid span, counted as 1): whether it fits the table. */
84
+ row(cells) {
85
+ this.rows++;
86
+ const above = this.#held.map((n) => n > 0);
87
+ const held = this.#held.map((n) => Math.max(0, n - 1));
88
+ let fits = true;
89
+ let at = 0;
90
+ for (const [colspan, rowspan] of cells) {
91
+ while (above[at])
92
+ at++;
93
+ for (let column = at; column < at + Math.max(1, colspan); column++) {
94
+ // A cell running into a column a span from above holds.
95
+ if (above[column])
96
+ fits = false;
97
+ held[column] = Math.max(held[column] ?? 0, Math.max(1, rowspan) - 1);
98
+ }
99
+ at += Math.max(1, colspan);
100
+ }
101
+ this.#held = Array.from({ length: Math.max(held.length, at) }, (_, i) => held[i] ?? 0);
102
+ const width = Math.max(at, above.lastIndexOf(true) + 1);
103
+ this.columns = Math.max(this.columns, width);
104
+ this.#width ??= width;
105
+ return fits && width === this.#width;
106
+ }
107
+ }
42
108
  function own(attrs, name) {
43
109
  return Object.hasOwn(attrs, name) ? attrs[name] : undefined;
44
110
  }
@@ -54,7 +120,13 @@ function isBlank(nodes) {
54
120
  return nodes.every((n) => n.type === 'text'
55
121
  ? n.value.trim() === ''
56
122
  : n.tag === 'br' ||
57
- ((n.tag === 'p' || n.tag === COLUMNS_TAG || n.tag === COLUMN_TAG) && isBlank(n.children)));
123
+ ((n.tag === 'p' ||
124
+ n.tag === COLUMNS_TAG ||
125
+ n.tag === COLUMN_TAG ||
126
+ n.tag === BOX_TAG ||
127
+ n.tag === TASK_TAG ||
128
+ n.tag === SUBTASK_TAG) &&
129
+ isBlank(n.children)));
58
130
  }
59
131
  /**
60
132
  * How a message names a page: by its number (from 1; 0 for the content of a document without pages) and its title when
@@ -70,7 +142,12 @@ function isSupportedFormat(value) {
70
142
  function placementAllowed(display, scope) {
71
143
  if (display === 'inline')
72
144
  return !scope.inLink && TEXT_PARENTS.has(scope.parent);
73
- return scope.parent === PAGE_TAG || scope.parent === DOC_TAG || scope.parent === COLUMN_TAG;
145
+ return (scope.parent === PAGE_TAG ||
146
+ scope.parent === DOC_TAG ||
147
+ scope.parent === COLUMN_TAG ||
148
+ scope.parent === BOX_TAG ||
149
+ scope.parent === TASK_TAG ||
150
+ scope.parent === SUBTASK_TAG);
74
151
  }
75
152
  function outOfRange(value, spec) {
76
153
  return (spec.min !== undefined && value < spec.min) || (spec.max !== undefined && value > spec.max);
@@ -132,6 +209,11 @@ class Analyser {
132
209
  /** The help placed directly on each page, by page index. */
133
210
  pageHelp = new Map();
134
211
  explicitPages = false;
212
+ /** How the tasks are numbered (`task-numbering`), and the number of the last one so far. */
213
+ taskNumbering = 'page';
214
+ taskNumber = 0;
215
+ /** The tasks with a Check of their own, with their ids (their Check's). */
216
+ checkedTasks = new Map();
135
217
  /** While help content is read (`visitHelp`): it takes no style (see `inline`). */
136
218
  inHelp = false;
137
219
  /** The components that declare each child tag, as listed in CHILD_OUTSIDE. */
@@ -213,6 +295,13 @@ class Analyser {
213
295
  else
214
296
  this.invalid(DOC_TAG, name, value, 'enum', DOC_KINDS, onRoot);
215
297
  }
298
+ else if (name === TASK_NUMBERING_ATTRIBUTE) {
299
+ // The engine numbers the tasks into the view: the player needs no more of it.
300
+ if (TASK_NUMBERINGS.includes(value))
301
+ this.taskNumbering = value;
302
+ else
303
+ this.invalid(DOC_TAG, name, value, 'enum', TASK_NUMBERINGS, onRoot);
304
+ }
216
305
  else {
217
306
  attrs[name] = value;
218
307
  if (name === STYLE_TEMPLATE_ATTRIBUTE)
@@ -235,6 +324,9 @@ class Analyser {
235
324
  inLink: false,
236
325
  mode: 'flow',
237
326
  host: null,
327
+ box: null,
328
+ column: false,
329
+ task: false,
238
330
  help: null,
239
331
  page: null,
240
332
  pageIndex: 0,
@@ -313,7 +405,7 @@ class Analyser {
313
405
  const met = new Map(this.checkNodes.map((c) => [c.node, c]));
314
406
  const named = new Map();
315
407
  for (const { node, page, where } of this.checkNodes) {
316
- const id = own(node.attrs, 'id') ?? '';
408
+ const id = this.checkedTasks.get(node) ?? own(node.attrs, 'id') ?? '';
317
409
  if (own(node.attrs, CHECK_FOR_ATTRIBUTE) === undefined) {
318
410
  named.set(node, null);
319
411
  continue;
@@ -353,11 +445,13 @@ class Analyser {
353
445
  const record = this.components.get(node);
354
446
  return {
355
447
  id: record?.sourceId ?? null,
356
- graded: isCheckable(definition.manifest),
448
+ // An example is answered already: nothing for a Check.
449
+ graded: isCheckable(definition.manifest) && definition.example?.(node) !== true,
357
450
  block: definition.manifest.display === 'block',
358
451
  };
359
452
  },
360
453
  checkFor: (node) => (node.type === 'element' ? (named.get(node) ?? null) : null),
454
+ taskCheck: (node) => node.type === 'element' && this.checkedTasks.has(node),
361
455
  };
362
456
  const pages = this.explicitPages ? [...pageNodes.keys()].map((page) => page.children) : [loose];
363
457
  pages.forEach((content, index) => {
@@ -368,7 +462,7 @@ class Analyser {
368
462
  const check = met.get(node);
369
463
  if (check === undefined)
370
464
  return;
371
- const id = own(node.attrs, 'id') ?? '';
465
+ const id = this.checkedTasks.get(node) ?? own(node.attrs, 'id') ?? '';
372
466
  for (const target of plan.twice[k] ?? []) {
373
467
  this.add(EngineIssue.CHECK_TARGET_TWICE, { check: id, id: target }, check.where, 'warning');
374
468
  }
@@ -616,6 +710,10 @@ class Analyser {
616
710
  }
617
711
  if (tag === BADGE_TAG)
618
712
  return this.visitBadge(el, scope, place, where);
713
+ if (tag === BOX_TAG)
714
+ return this.visitBox(el, scope, place, where);
715
+ if (tag === TASK_TAG)
716
+ return this.visitTask(el, scope, place, where);
619
717
  if (BLOCK_TAGS.has(tag) || INLINE_TAGS.has(tag)) {
620
718
  if (scope.mode === 'text' || (scope.mode === 'inline' && BLOCK_TAGS.has(tag))) {
621
719
  this.add(EngineIssue.ELEMENT_REMOVED, { tag }, where, 'warning');
@@ -626,6 +724,16 @@ class Analyser {
626
724
  // A figure is a block: in a prompt or an option it is unwrapped like the other blocks, below.
627
725
  if (tag === FIGURE_TAG && scope.mode === 'flow')
628
726
  return [this.visitFigure(el, scope, place, where)];
727
+ if (tag === TABLE_TAG) {
728
+ return scope.mode === 'flow'
729
+ ? [this.visitTable(el, scope, place, where)]
730
+ : this.unwrapTable(el, scope, place, where);
731
+ }
732
+ // A part of a table out of its table (a cell outside a row): its content stays, to be validated.
733
+ if (TABLE_TAGS.has(tag)) {
734
+ this.add(EngineIssue.TABLE_INVALID, {}, where);
735
+ return this.visitNodes(el, scope, place.path);
736
+ }
629
737
  if (tag.includes('-')) {
630
738
  const host = scope.host;
631
739
  const spec = host?.manifest.children.find((c) => c.tag === tag);
@@ -649,6 +757,16 @@ class Analyser {
649
757
  }
650
758
  if (tag === COLUMNS_TAG)
651
759
  return [this.visitColumns(el, scope, place, where)];
760
+ // A frame's title or tag, a task's instruction, tag or sub-task out of its place (after the content, twice,
761
+ // outside its frame or task): its content stays.
762
+ if (tag === BOX_TITLE_TAG ||
763
+ tag === BOX_LABEL_TAG ||
764
+ tag === INSTRUCTION_TAG ||
765
+ tag === TASK_LABEL_TAG ||
766
+ tag === SUBTASK_TAG) {
767
+ this.add(EngineIssue.PLACEMENT_INVALID, { tag }, where);
768
+ return this.visitNodes(el, scope, place.path);
769
+ }
652
770
  // A column without its layout (`astl-match` has columns of its own, which it reads itself). The content stays,
653
771
  // to be validated.
654
772
  if (tag === COLUMN_TAG && !(host && spec)) {
@@ -676,6 +794,9 @@ class Analyser {
676
794
  scope.help === null &&
677
795
  (scope.parent === PAGE_TAG ||
678
796
  scope.parent === COLUMN_TAG ||
797
+ scope.parent === BOX_TAG ||
798
+ scope.parent === TASK_TAG ||
799
+ scope.parent === SUBTASK_TAG ||
679
800
  (scope.parent === DOC_TAG && !this.explicitPages));
680
801
  if (!onPage) {
681
802
  this.add(EngineIssue.PLACEMENT_INVALID, { tag: CHECK_TAG }, where);
@@ -713,7 +834,13 @@ class Analyser {
713
834
  visitBadge(el, scope, place, where) {
714
835
  const id = own(el.attrs, 'id');
715
836
  const at = { ...where, element: id ?? null };
716
- const inText = TEXT_PARENTS.has(scope.parent) && !scope.inLink && scope.mode !== 'text';
837
+ const inText = (TEXT_PARENTS.has(scope.parent) ||
838
+ scope.parent === BOX_TITLE_TAG ||
839
+ scope.parent === BOX_LABEL_TAG ||
840
+ scope.parent === INSTRUCTION_TAG ||
841
+ scope.parent === TASK_LABEL_TAG) &&
842
+ !scope.inLink &&
843
+ scope.mode !== 'text';
717
844
  if (!inText || (scope.host !== null && scope.mode !== 'flow')) {
718
845
  this.add(EngineIssue.PLACEMENT_INVALID, { tag: BADGE_TAG }, at);
719
846
  return this.visitNodes(el, scope, place.path);
@@ -766,6 +893,176 @@ class Analyser {
766
893
  };
767
894
  return element(el.tag, attrs, this.visitNodes(el, inner, place.path));
768
895
  }
896
+ /**
897
+ * A frame: directly on a page, in the document without pages or in a column, outside components and help; in another
898
+ * frame it is `BOX_NESTED`, elsewhere `PLACEMENT_INVALID`, and its content is validated where it stands. Its title and
899
+ * its tag lead it, inline text without components (a badge, a picture); then content as a page holds it.
900
+ */
901
+ visitBox(el, scope, place, where) {
902
+ if (scope.box !== null) {
903
+ this.add(EngineIssue.BOX_NESTED, { tag: BOX_TAG }, where);
904
+ return this.visitNodes(el, scope, place.path);
905
+ }
906
+ const placed = scope.host === null &&
907
+ scope.help === null &&
908
+ scope.mode === 'flow' &&
909
+ (scope.parent === PAGE_TAG ||
910
+ scope.parent === COLUMN_TAG ||
911
+ scope.parent === TASK_TAG ||
912
+ scope.parent === SUBTASK_TAG ||
913
+ (scope.parent === DOC_TAG && !this.explicitPages));
914
+ if (!placed) {
915
+ this.add(EngineIssue.PLACEMENT_INVALID, { tag: BOX_TAG }, where);
916
+ return this.visitNodes(el, scope, place.path);
917
+ }
918
+ const attrs = this.richAttributes(el, where, scope);
919
+ const allowed = {
920
+ 'title-place': BOX_TITLE_PLACES,
921
+ variant: BOX_VARIANTS,
922
+ icon: BOX_ICONS,
923
+ };
924
+ for (const [name, values] of [...Object.entries(allowed), ['palette', null]]) {
925
+ const value = own(attrs, name);
926
+ if (value === undefined)
927
+ continue;
928
+ const palette = parseIntegerValue(value);
929
+ const valid = values === null
930
+ ? palette !== null && palette >= 1 && palette <= MAX_BADGE_PALETTE
931
+ : values.includes(value);
932
+ if (valid)
933
+ continue;
934
+ this.invalid(BOX_TAG, name, value, values === null ? 'integer' : 'enum', values ?? [], where);
935
+ delete attrs[name];
936
+ }
937
+ const inner = {
938
+ ...scope,
939
+ ...this.below(attrs, scope),
940
+ parent: BOX_TAG,
941
+ box: scope.column ? 'column' : 'top',
942
+ };
943
+ // The title, then the tag, may only lead: before the first content (text or any other element).
944
+ const content = el.children.findIndex((n) => n.type === 'text' ? !isHtmlWhitespace(n.value) : n.tag !== BOX_TITLE_TAG && n.tag !== BOX_LABEL_TAG);
945
+ let led = '';
946
+ const children = this.visitNodes(el, inner, place.path, (child, childPlace) => {
947
+ const head = child.tag === BOX_TITLE_TAG || child.tag === BOX_LABEL_TAG;
948
+ const leads = content === -1 || el.children.indexOf(child) < content;
949
+ const order = child.tag === BOX_TITLE_TAG ? led === '' : led !== BOX_LABEL_TAG;
950
+ if (!head || !leads || !order)
951
+ return this.visitElement(child, inner, childPlace);
952
+ led = child.tag;
953
+ const at = this.where(inner, childPlace, child.tag);
954
+ const headAttrs = this.richAttributes(child, at, inner);
955
+ const text = { ...inner, ...this.below(headAttrs, inner), parent: child.tag, mode: 'inline' };
956
+ return [element(child.tag, headAttrs, this.visitNodes(child, text, childPlace.path))];
957
+ });
958
+ return [element(BOX_TAG, attrs, children)];
959
+ }
960
+ /**
961
+ * A task: directly on a page, in the document without pages, in a column or in a frame, outside components and help;
962
+ * in another task it is `TASK_NESTED`, elsewhere `PLACEMENT_INVALID`, and its content is validated where it stands.
963
+ * Its id is required: its Check is addressed by it. Its instruction, then its tags (up to MAX_TASK_LABELS), may only
964
+ * lead it; out of place, each is `PLACEMENT_INVALID` and its text stays. Then a page's content, its sub-tasks among
965
+ * it. The view has its number: its own `number`, after which the next ones count on, else the next of its page or of
966
+ * the document (`task-numbering`), none with `none`. A task with `check` has a Check of its own for all its graded
967
+ * questions, which the view holds as a Check element at its end.
968
+ */
969
+ visitTask(el, scope, place, where) {
970
+ const id = own(el.attrs, 'id');
971
+ const at = { ...where, element: id ?? null };
972
+ if (scope.task) {
973
+ // One error for the task: its head's content and its sub-tasks stay, as the outer task's.
974
+ this.add(EngineIssue.TASK_NESTED, { tag: TASK_TAG }, at);
975
+ return this.visitNodes(el, scope, place.path, (child, childPlace) => child.tag === SUBTASK_TAG
976
+ ? [this.visitSubtask(child, scope, childPlace)]
977
+ : child.tag === INSTRUCTION_TAG || child.tag === TASK_LABEL_TAG
978
+ ? this.visitNodes(child, scope, childPlace.path)
979
+ : null);
980
+ }
981
+ const placed = scope.host === null &&
982
+ scope.help === null &&
983
+ scope.mode === 'flow' &&
984
+ (scope.parent === PAGE_TAG ||
985
+ scope.parent === COLUMN_TAG ||
986
+ scope.parent === BOX_TAG ||
987
+ (scope.parent === DOC_TAG && !this.explicitPages));
988
+ if (!placed) {
989
+ this.add(EngineIssue.PLACEMENT_INVALID, { tag: TASK_TAG }, at);
990
+ return this.visitNodes(el, scope, place.path);
991
+ }
992
+ if (id === undefined)
993
+ this.add(EngineIssue.ID_MISSING, { tag: TASK_TAG }, at);
994
+ else
995
+ this.sourceId(id, TASK_TAG, at);
996
+ const attrs = this.richAttributes(element(TASK_TAG, Object.fromEntries(Object.entries(el.attrs).filter(([name]) => name !== 'id')), []), at, scope);
997
+ const bounded = (name, max) => {
998
+ const value = own(attrs, name);
999
+ if (value === undefined)
1000
+ return null;
1001
+ const n = parseIntegerValue(value);
1002
+ if (n !== null && n >= 1 && n <= max)
1003
+ return n;
1004
+ this.invalid(TASK_TAG, name, value, 'integer', [], at);
1005
+ delete attrs[name];
1006
+ return null;
1007
+ };
1008
+ bounded('level', MAX_TASK_LEVEL);
1009
+ bounded('palette', MAX_BADGE_PALETTE);
1010
+ const written = bounded('number', Number.MAX_SAFE_INTEGER);
1011
+ // A boolean: present or not, whatever its value. The view holds the Check itself, at the end of the task.
1012
+ const check = Object.hasOwn(attrs, 'check');
1013
+ delete attrs.check;
1014
+ if (written !== null)
1015
+ this.taskNumber = written;
1016
+ else if (this.taskNumbering !== 'none')
1017
+ this.taskNumber += 1;
1018
+ if (written !== null || this.taskNumbering !== 'none')
1019
+ attrs.number = String(this.taskNumber);
1020
+ const inner = { ...scope, ...this.below(attrs, scope), parent: TASK_TAG, task: true };
1021
+ // The instruction, then the tags, may only lead: before the first content (text or any other element).
1022
+ const content = el.children.findIndex((n) => n.type === 'text' ? !isHtmlWhitespace(n.value) : n.tag !== INSTRUCTION_TAG && n.tag !== TASK_LABEL_TAG);
1023
+ let led = false;
1024
+ let labels = 0;
1025
+ const children = this.visitNodes(el, inner, place.path, (child, childPlace) => {
1026
+ if (child.tag === SUBTASK_TAG)
1027
+ return [this.visitSubtask(child, inner, childPlace)];
1028
+ const head = child.tag === INSTRUCTION_TAG || child.tag === TASK_LABEL_TAG;
1029
+ const leads = content === -1 || el.children.indexOf(child) < content;
1030
+ const order = child.tag === INSTRUCTION_TAG ? !led : labels < MAX_TASK_LABELS;
1031
+ if (!head || !leads || !order)
1032
+ return this.visitElement(child, inner, childPlace);
1033
+ led = true;
1034
+ const where = this.where(inner, childPlace, child.tag);
1035
+ const headAttrs = this.richAttributes(child, where, inner);
1036
+ if (child.tag === TASK_LABEL_TAG) {
1037
+ labels++;
1038
+ const icon = own(headAttrs, 'icon');
1039
+ if (icon !== undefined && !TASK_LABEL_ICONS.includes(icon)) {
1040
+ this.invalid(TASK_LABEL_TAG, 'icon', icon, 'enum', TASK_LABEL_ICONS, where);
1041
+ delete headAttrs.icon;
1042
+ }
1043
+ }
1044
+ const text = { ...inner, ...this.below(headAttrs, inner), parent: child.tag, mode: 'inline' };
1045
+ return [element(child.tag, headAttrs, this.visitNodes(child, text, childPlace.path))];
1046
+ });
1047
+ const node = element(TASK_TAG, attrs, children);
1048
+ if (check) {
1049
+ this.checkNodes.push({ node, page: scope.pageIndex, where: at });
1050
+ this.checkedTasks.set(node, id ?? '');
1051
+ }
1052
+ return [node];
1053
+ }
1054
+ /** A sub-task: a lettered part of a task, a page's content in it; its `level`, A to D, shows beside its letter. */
1055
+ visitSubtask(el, scope, place) {
1056
+ const where = this.where(scope, place, SUBTASK_TAG);
1057
+ const attrs = this.richAttributes(el, where, scope);
1058
+ const level = own(attrs, 'level');
1059
+ if (level !== undefined && !SUBTASK_LEVELS.includes(level)) {
1060
+ this.invalid(SUBTASK_TAG, 'level', level, 'enum', SUBTASK_LEVELS, where);
1061
+ delete attrs.level;
1062
+ }
1063
+ const inner = { ...scope, ...this.below(attrs, scope), parent: SUBTASK_TAG };
1064
+ return element(SUBTASK_TAG, attrs, this.visitNodes(el, inner, place.path));
1065
+ }
769
1066
  /**
770
1067
  * A layout: 2 to 4 columns, directly on a page or in the document, outside components. Whatever its shape, its
771
1068
  * content is still validated, each piece as what it is.
@@ -782,12 +1079,19 @@ class Analyser {
782
1079
  }
783
1080
  columns++;
784
1081
  const column = this.richAttributes(child, this.where(inner, childPlace, COLUMN_TAG), inner);
785
- const content = { ...inner, ...this.below(column, inner), parent: COLUMN_TAG };
1082
+ const content = { ...inner, ...this.below(column, inner), parent: COLUMN_TAG, column: true };
786
1083
  return [element(COLUMN_TAG, column, this.visitNodes(child, content, childPlace.path))];
787
1084
  });
788
1085
  if (children.some((n) => n.type === 'text' && !isHtmlWhitespace(n.value)))
789
1086
  stray = true;
790
- const placed = scope.host === null && (scope.parent === PAGE_TAG || scope.parent === DOC_TAG);
1087
+ // In a frame or a task too, never in a column, however deep: two layout levels at most.
1088
+ const placed = scope.host === null &&
1089
+ !scope.column &&
1090
+ (scope.parent === PAGE_TAG ||
1091
+ scope.parent === DOC_TAG ||
1092
+ scope.parent === BOX_TAG ||
1093
+ scope.parent === TASK_TAG ||
1094
+ scope.parent === SUBTASK_TAG);
791
1095
  if (!placed || stray || columns < MIN_COLUMNS || columns > MAX_COLUMNS) {
792
1096
  this.add(EngineIssue.COLUMNS_INVALID, { tag: COLUMNS_TAG }, where);
793
1097
  }
@@ -825,6 +1129,13 @@ class Analyser {
825
1129
  else if ((name === 'width' || name === 'height') && !((parseIntegerValue(value) ?? 0) > 0)) {
826
1130
  this.invalid(el.tag, name, value, 'integer', [], where);
827
1131
  }
1132
+ else if ((name === 'colspan' || name === 'rowspan') &&
1133
+ tableSpan(value, name === 'colspan' ? MAX_TABLE_COLUMNS : MAX_TABLE_ROWS) === 0) {
1134
+ this.invalid(el.tag, name, value, 'integer', [], where);
1135
+ }
1136
+ else if (name === 'scope' && !TABLE_SCOPES.includes(value)) {
1137
+ this.invalid(el.tag, name, value, 'enum', TABLE_SCOPES, where);
1138
+ }
828
1139
  else {
829
1140
  attrs[name] = value;
830
1141
  }
@@ -840,17 +1151,12 @@ class Analyser {
840
1151
  return attrs;
841
1152
  }
842
1153
  /**
843
- * 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
844
- * one caption. A caption holds rich inline text, which may hold inline components like any text.
1154
+ * A figure: directly in a page, a column, a frame or the document (a block), or in a rich block, with exactly one picture
1155
+ * and at most one caption. A caption holds rich inline text, which may hold inline components like any text.
845
1156
  */
846
1157
  visitFigure(el, scope, place, where) {
847
- if (scope.parent !== PAGE_TAG &&
848
- scope.parent !== DOC_TAG &&
849
- scope.parent !== COLUMN_TAG &&
850
- scope.parent !== scope.host?.manifest.tag &&
851
- !HELP_TAGS.has(scope.parent)) {
1158
+ if (!this.blockPlace(scope))
852
1159
  this.add(EngineIssue.PLACEMENT_INVALID, { tag: el.tag }, where);
853
- }
854
1160
  const attrs = this.richAttributes(el, where, scope);
855
1161
  const inner = { ...scope, ...this.below(attrs, scope), parent: FIGURE_TAG };
856
1162
  let pictures = 0;
@@ -875,6 +1181,135 @@ class Analyser {
875
1181
  this.add(EngineIssue.FIGURE_INVALID, {}, where);
876
1182
  return element(FIGURE_TAG, attrs, children);
877
1183
  }
1184
+ /** 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. */
1185
+ blockPlace(scope) {
1186
+ return (scope.parent === PAGE_TAG ||
1187
+ scope.parent === DOC_TAG ||
1188
+ scope.parent === COLUMN_TAG ||
1189
+ scope.parent === BOX_TAG ||
1190
+ scope.parent === TASK_TAG ||
1191
+ scope.parent === SUBTASK_TAG ||
1192
+ scope.parent === scope.host?.manifest.tag ||
1193
+ HELP_TAGS.has(scope.parent));
1194
+ }
1195
+ /**
1196
+ * A table, where a figure may stand. Its caption (first, once) holds rich inline text; then a head (once, before the
1197
+ * bodies) and bodies of rows, or rows directly in it (one body until the next part); a row holds header and data
1198
+ * cells, whose content is rich inline text with inline components in it. A part out of its place, text between
1199
+ * rows or cells, rows that cover different columns, a span past the rows of its part or no rows at all are
1200
+ * TABLE_INVALID, reported at the part; more than MAX_TABLE_COLUMNS columns or MAX_TABLE_ROWS rows TABLE_TOO_LARGE.
1201
+ */
1202
+ visitTable(el, scope, place, where) {
1203
+ if (!this.blockPlace(scope))
1204
+ this.add(EngineIssue.PLACEMENT_INVALID, { tag: el.tag }, where);
1205
+ const attrs = this.richAttributes(el, where, scope);
1206
+ const inner = { ...scope, ...this.below(attrs, scope), parent: TABLE_TAG };
1207
+ const grid = new TableGrid();
1208
+ // What came last: the order is caption, head, bodies (and rows of their own).
1209
+ const seen = { last: 'start' };
1210
+ const children = this.visitNodes(el, inner, place.path, (child, childPlace) => {
1211
+ const at = this.where(inner, childPlace, child.tag);
1212
+ if (child.tag === TABLE_CAPTION_TAG && seen.last === 'start') {
1213
+ seen.last = 'caption';
1214
+ const caption = this.richAttributes(child, at, inner);
1215
+ const text = {
1216
+ ...inner,
1217
+ ...this.below(caption, inner),
1218
+ parent: TABLE_CAPTION_TAG,
1219
+ mode: 'inline',
1220
+ };
1221
+ return [element(TABLE_CAPTION_TAG, caption, this.visitNodes(child, text, childPlace.path))];
1222
+ }
1223
+ if (child.tag === TABLE_HEAD_TAG && (seen.last === 'start' || seen.last === 'caption')) {
1224
+ seen.last = 'head';
1225
+ return [this.visitTableSection(child, inner, childPlace, at, grid)];
1226
+ }
1227
+ if (child.tag === TABLE_BODY_TAG) {
1228
+ if (seen.last === 'rows')
1229
+ this.endTableSection(grid, where);
1230
+ seen.last = 'body';
1231
+ return [this.visitTableSection(child, inner, childPlace, at, grid)];
1232
+ }
1233
+ if (child.tag === TABLE_ROW_TAG) {
1234
+ if (seen.last !== 'rows')
1235
+ grid.start();
1236
+ seen.last = 'rows';
1237
+ return [this.visitTableRow(child, inner, childPlace, at, grid)];
1238
+ }
1239
+ this.add(EngineIssue.TABLE_INVALID, {}, at);
1240
+ return [];
1241
+ });
1242
+ if (seen.last === 'rows')
1243
+ this.endTableSection(grid, where);
1244
+ if (children.some((n) => n.type === 'text' && !isHtmlWhitespace(n.value)) || grid.rows === 0) {
1245
+ this.add(EngineIssue.TABLE_INVALID, {}, where);
1246
+ }
1247
+ if (grid.columns > MAX_TABLE_COLUMNS || grid.rows > MAX_TABLE_ROWS) {
1248
+ const params = {
1249
+ columns: grid.columns,
1250
+ rows: grid.rows,
1251
+ maxColumns: MAX_TABLE_COLUMNS,
1252
+ maxRows: MAX_TABLE_ROWS,
1253
+ };
1254
+ this.add(EngineIssue.TABLE_TOO_LARGE, params, where);
1255
+ }
1256
+ return element(TABLE_TAG, attrs, children);
1257
+ }
1258
+ /** A head or a body: rows only, its spans ending with it. */
1259
+ visitTableSection(el, scope, place, where, grid) {
1260
+ const attrs = this.richAttributes(el, where, scope);
1261
+ const inner = { ...scope, ...this.below(attrs, scope), parent: el.tag };
1262
+ grid.start();
1263
+ const children = this.visitNodes(el, inner, place.path, (child, childPlace) => {
1264
+ const at = this.where(inner, childPlace, child.tag);
1265
+ if (child.tag === TABLE_ROW_TAG)
1266
+ return [this.visitTableRow(child, inner, childPlace, at, grid)];
1267
+ this.add(EngineIssue.TABLE_INVALID, {}, at);
1268
+ return [];
1269
+ });
1270
+ if (children.some((n) => n.type === 'text' && !isHtmlWhitespace(n.value))) {
1271
+ this.add(EngineIssue.TABLE_INVALID, {}, where);
1272
+ }
1273
+ this.endTableSection(grid, where);
1274
+ return element(el.tag, attrs, children);
1275
+ }
1276
+ /** A span that reaches past the last row of its part. */
1277
+ endTableSection(grid, where) {
1278
+ if (grid.end())
1279
+ this.add(EngineIssue.TABLE_INVALID, {}, where);
1280
+ }
1281
+ /** A row: cells only, covering the columns of the rows before it. */
1282
+ visitTableRow(el, scope, place, where, grid) {
1283
+ const attrs = this.richAttributes(el, where, scope);
1284
+ const inner = { ...scope, ...this.below(attrs, scope), parent: TABLE_ROW_TAG };
1285
+ const spans = [];
1286
+ const children = this.visitNodes(el, inner, place.path, (child, childPlace) => {
1287
+ const at = this.where(inner, childPlace, child.tag);
1288
+ if (child.tag !== TABLE_HEADER_CELL_TAG && child.tag !== TABLE_CELL_TAG) {
1289
+ this.add(EngineIssue.TABLE_INVALID, {}, at);
1290
+ return [];
1291
+ }
1292
+ const cell = this.richAttributes(child, at, inner);
1293
+ spans.push([tableSpan(cell.colspan, MAX_TABLE_COLUMNS), tableSpan(cell.rowspan, MAX_TABLE_ROWS)]);
1294
+ const text = { ...inner, ...this.below(cell, inner), parent: child.tag, mode: 'inline' };
1295
+ return [element(child.tag, cell, this.visitNodes(child, text, childPlace.path))];
1296
+ });
1297
+ if (children.some((n) => n.type === 'text' && !isHtmlWhitespace(n.value)) || !grid.row(spans)) {
1298
+ this.add(EngineIssue.TABLE_INVALID, {}, where);
1299
+ }
1300
+ return element(TABLE_ROW_TAG, attrs, children);
1301
+ }
1302
+ /**
1303
+ * A table in a prompt, an option or a card, where blocks are unwrapped: its parts go, the text of its caption and
1304
+ * cells stays, a space between them.
1305
+ */
1306
+ unwrapTable(el, scope, place, where) {
1307
+ this.add(EngineIssue.ELEMENT_REMOVED, { tag: el.tag }, where, 'warning');
1308
+ const unwrap = (child, childPlace) => TABLE_TAGS.has(child.tag)
1309
+ ? [{ type: 'text', value: ' ' }, ...this.visitNodes(child, scope, childPlace.path, unwrap)]
1310
+ : null;
1311
+ return this.visitNodes(el, scope, place.path, unwrap);
1312
+ }
878
1313
  visitPage(el, index, place, base) {
879
1314
  const id = own(el.attrs, 'id');
880
1315
  const where = {
@@ -913,6 +1348,8 @@ class Analyser {
913
1348
  pageIndex: index,
914
1349
  pageName: namePage(index + 1, own(el.attrs, 'label')),
915
1350
  };
1351
+ if (this.taskNumbering === 'page')
1352
+ this.taskNumber = 0;
916
1353
  const children = this.visitNodes(el, scope, place.path);
917
1354
  if (isBlank(children))
918
1355
  this.add(EngineIssue.PAGE_EMPTY, scope.pageName, where, 'warning');
@@ -925,7 +1362,7 @@ class Analyser {
925
1362
  const id = own(raw.attrs, 'id');
926
1363
  const where = { order: place.order, page: scope.page, element: id ?? null, tag, path: place.path };
927
1364
  this.componentCount++;
928
- if (manifest.graded)
1365
+ if (manifest.graded && definition.example?.(raw) !== true)
929
1366
  this.gradedPages.add(scope.pageIndex);
930
1367
  if (!placementAllowed(manifest.display, scope))
931
1368
  this.add(EngineIssue.PLACEMENT_INVALID, { tag }, where);
@@ -1108,7 +1545,7 @@ class Analyser {
1108
1545
  else if (child.tag === COLUMNS_TAG || child.tag === COLUMN_TAG) {
1109
1546
  this.add(EngineIssue.COLUMNS_INVALID, { tag: child.tag }, where);
1110
1547
  }
1111
- else if (child.tag === STYLE_TAG) {
1548
+ else if (child.tag === STYLE_TAG || child.tag === BOX_TAG || child.tag === TASK_TAG) {
1112
1549
  this.add(EngineIssue.PLACEMENT_INVALID, { tag: child.tag }, where);
1113
1550
  }
1114
1551
  else if (child.tag.includes('-') && child.tag !== DOC_TAG && child.tag !== PAGE_TAG) {