@astl/engine 0.5.1 → 0.6.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/CHANGELOG.md +13 -0
- package/dist/analyse.d.ts.map +1 -1
- package/dist/analyse.js +250 -11
- package/dist/analyse.js.map +1 -1
- package/dist/compile.d.ts.map +1 -1
- package/dist/compile.js +4 -1
- package/dist/compile.js.map +1 -1
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +16 -2
- package/dist/engine.js.map +1 -1
- package/dist/messages/en.json +2 -0
- package/dist/messages/pl.json +2 -0
- package/package.json +2 -2
- package/src/analyse.ts +276 -10
- package/src/compile.ts +4 -0
- package/src/engine.ts +23 -1
- package/src/messages/en.json +2 -0
- package/src/messages/pl.json +2 -0
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,8 @@ import {
|
|
|
57
64
|
MAX_COLUMNS,
|
|
58
65
|
MAX_NESTING_DEPTH,
|
|
59
66
|
MAX_SOURCE_LENGTH,
|
|
67
|
+
MAX_TASK_LABELS,
|
|
68
|
+
MAX_TASK_LEVEL,
|
|
60
69
|
MIN_COLUMNS,
|
|
61
70
|
type Node,
|
|
62
71
|
type NormalisationProfile,
|
|
@@ -71,8 +80,15 @@ import {
|
|
|
71
80
|
STYLE_TEMPLATE_ATTRIBUTE,
|
|
72
81
|
type StyleScope,
|
|
73
82
|
type StyleTemplate,
|
|
83
|
+
SUBTASK_LEVELS,
|
|
84
|
+
SUBTASK_TAG,
|
|
74
85
|
SUPPORTED_FORMATS,
|
|
75
86
|
TAG_ATTRIBUTES,
|
|
87
|
+
TASK_LABEL_ICONS,
|
|
88
|
+
TASK_LABEL_TAG,
|
|
89
|
+
TASK_NUMBERING_ATTRIBUTE,
|
|
90
|
+
TASK_NUMBERINGS,
|
|
91
|
+
TASK_TAG,
|
|
76
92
|
URL_ATTRIBUTES,
|
|
77
93
|
type UrlPolicy,
|
|
78
94
|
} from '@astl/core';
|
|
@@ -118,6 +134,13 @@ const RULE_TARGETS: ReadonlySet<string> = new Set([
|
|
|
118
134
|
FIGURE_TAG,
|
|
119
135
|
CAPTION_TAG,
|
|
120
136
|
BADGE_TAG,
|
|
137
|
+
BOX_TAG,
|
|
138
|
+
BOX_TITLE_TAG,
|
|
139
|
+
BOX_LABEL_TAG,
|
|
140
|
+
TASK_TAG,
|
|
141
|
+
INSTRUCTION_TAG,
|
|
142
|
+
TASK_LABEL_TAG,
|
|
143
|
+
SUBTASK_TAG,
|
|
121
144
|
]);
|
|
122
145
|
/** The contrast WCAG asks of normal text (AA); large text may have less, and then the warning is only stricter. */
|
|
123
146
|
const MIN_CONTRAST = 4.5;
|
|
@@ -233,6 +256,12 @@ interface Scope {
|
|
|
233
256
|
readonly mode: 'flow' | 'inline' | 'text';
|
|
234
257
|
/** The component whose child content this is. */
|
|
235
258
|
readonly host: Host | null;
|
|
259
|
+
/** In a frame: where the frame stands (`column`: in a layout's column, however deep; `top`: elsewhere); null outside. */
|
|
260
|
+
readonly box: 'top' | 'column' | null;
|
|
261
|
+
/** In a layout's column, however deep: no layout in it (two layout levels at most). */
|
|
262
|
+
readonly column: boolean;
|
|
263
|
+
/** In a task: no task in it. */
|
|
264
|
+
readonly task: boolean;
|
|
236
265
|
/** Inside help: its tag and the id of the interaction it belongs to (null for a page's). */
|
|
237
266
|
readonly help: { readonly tag: string; readonly element: string | null } | null;
|
|
238
267
|
readonly page: string | null;
|
|
@@ -284,7 +313,13 @@ function isBlank(nodes: readonly Node[]): boolean {
|
|
|
284
313
|
n.type === 'text'
|
|
285
314
|
? n.value.trim() === ''
|
|
286
315
|
: n.tag === 'br' ||
|
|
287
|
-
((n.tag === 'p' ||
|
|
316
|
+
((n.tag === 'p' ||
|
|
317
|
+
n.tag === COLUMNS_TAG ||
|
|
318
|
+
n.tag === COLUMN_TAG ||
|
|
319
|
+
n.tag === BOX_TAG ||
|
|
320
|
+
n.tag === TASK_TAG ||
|
|
321
|
+
n.tag === SUBTASK_TAG) &&
|
|
322
|
+
isBlank(n.children)),
|
|
288
323
|
);
|
|
289
324
|
}
|
|
290
325
|
|
|
@@ -303,7 +338,14 @@ function isSupportedFormat(value: string): boolean {
|
|
|
303
338
|
|
|
304
339
|
function placementAllowed(display: 'inline' | 'block', scope: Scope): boolean {
|
|
305
340
|
if (display === 'inline') return !scope.inLink && TEXT_PARENTS.has(scope.parent);
|
|
306
|
-
return
|
|
341
|
+
return (
|
|
342
|
+
scope.parent === PAGE_TAG ||
|
|
343
|
+
scope.parent === DOC_TAG ||
|
|
344
|
+
scope.parent === COLUMN_TAG ||
|
|
345
|
+
scope.parent === BOX_TAG ||
|
|
346
|
+
scope.parent === TASK_TAG ||
|
|
347
|
+
scope.parent === SUBTASK_TAG
|
|
348
|
+
);
|
|
307
349
|
}
|
|
308
350
|
|
|
309
351
|
function outOfRange(value: number, spec: AttributeSpec): boolean {
|
|
@@ -373,6 +415,11 @@ class Analyser {
|
|
|
373
415
|
/** The help placed directly on each page, by page index. */
|
|
374
416
|
private readonly pageHelp = new Map<number, HelpParts>();
|
|
375
417
|
private explicitPages = false;
|
|
418
|
+
/** How the tasks are numbered (`task-numbering`), and the number of the last one so far. */
|
|
419
|
+
private taskNumbering: (typeof TASK_NUMBERINGS)[number] = 'page';
|
|
420
|
+
private taskNumber = 0;
|
|
421
|
+
/** The tasks with a Check of their own, with their ids (their Check's). */
|
|
422
|
+
private readonly checkedTasks = new Map<ElementNode, string>();
|
|
376
423
|
/** While help content is read (`visitHelp`): it takes no style (see `inline`). */
|
|
377
424
|
private inHelp = false;
|
|
378
425
|
/** The components that declare each child tag, as listed in CHILD_OUTSIDE. */
|
|
@@ -446,6 +493,11 @@ class Analyser {
|
|
|
446
493
|
} else if (name === 'kind') {
|
|
447
494
|
if ((DOC_KINDS as readonly string[]).includes(value)) attrs.kind = value;
|
|
448
495
|
else this.invalid(DOC_TAG, name, value, 'enum', DOC_KINDS, onRoot);
|
|
496
|
+
} else if (name === TASK_NUMBERING_ATTRIBUTE) {
|
|
497
|
+
// The engine numbers the tasks into the view: the player needs no more of it.
|
|
498
|
+
if ((TASK_NUMBERINGS as readonly string[]).includes(value))
|
|
499
|
+
this.taskNumbering = value as (typeof TASK_NUMBERINGS)[number];
|
|
500
|
+
else this.invalid(DOC_TAG, name, value, 'enum', TASK_NUMBERINGS, onRoot);
|
|
449
501
|
} else {
|
|
450
502
|
attrs[name] = value;
|
|
451
503
|
if (name === STYLE_TEMPLATE_ATTRIBUTE) this.styleTemplate(value, DOC_TAG, 'document', onRoot);
|
|
@@ -469,6 +521,9 @@ class Analyser {
|
|
|
469
521
|
inLink: false,
|
|
470
522
|
mode: 'flow',
|
|
471
523
|
host: null,
|
|
524
|
+
box: null,
|
|
525
|
+
column: false,
|
|
526
|
+
task: false,
|
|
472
527
|
help: null,
|
|
473
528
|
page: null,
|
|
474
529
|
pageIndex: 0,
|
|
@@ -552,7 +607,7 @@ class Analyser {
|
|
|
552
607
|
const met = new Map(this.checkNodes.map((c) => [c.node, c]));
|
|
553
608
|
const named = new Map<ElementNode, string[] | null>();
|
|
554
609
|
for (const { node, page, where } of this.checkNodes) {
|
|
555
|
-
const id = own(node.attrs, 'id') ?? '';
|
|
610
|
+
const id = this.checkedTasks.get(node) ?? own(node.attrs, 'id') ?? '';
|
|
556
611
|
if (own(node.attrs, CHECK_FOR_ATTRIBUTE) === undefined) {
|
|
557
612
|
named.set(node, null);
|
|
558
613
|
continue;
|
|
@@ -589,6 +644,7 @@ class Analyser {
|
|
|
589
644
|
};
|
|
590
645
|
},
|
|
591
646
|
checkFor: (node) => (node.type === 'element' ? (named.get(node) ?? null) : null),
|
|
647
|
+
taskCheck: (node) => node.type === 'element' && this.checkedTasks.has(node),
|
|
592
648
|
};
|
|
593
649
|
const pages = this.explicitPages ? [...pageNodes.keys()].map((page) => page.children) : [loose];
|
|
594
650
|
pages.forEach((content, index) => {
|
|
@@ -598,7 +654,7 @@ class Analyser {
|
|
|
598
654
|
nodes.forEach((node, k) => {
|
|
599
655
|
const check = met.get(node);
|
|
600
656
|
if (check === undefined) return;
|
|
601
|
-
const id = own(node.attrs, 'id') ?? '';
|
|
657
|
+
const id = this.checkedTasks.get(node) ?? own(node.attrs, 'id') ?? '';
|
|
602
658
|
for (const target of plan.twice[k] ?? []) {
|
|
603
659
|
this.add(EngineIssue.CHECK_TARGET_TWICE, { check: id, id: target }, check.where, 'warning');
|
|
604
660
|
}
|
|
@@ -852,6 +908,8 @@ class Analyser {
|
|
|
852
908
|
return [];
|
|
853
909
|
}
|
|
854
910
|
if (tag === BADGE_TAG) return this.visitBadge(el, scope, place, where);
|
|
911
|
+
if (tag === BOX_TAG) return this.visitBox(el, scope, place, where);
|
|
912
|
+
if (tag === TASK_TAG) return this.visitTask(el, scope, place, where);
|
|
855
913
|
if (BLOCK_TAGS.has(tag) || INLINE_TAGS.has(tag)) {
|
|
856
914
|
if (scope.mode === 'text' || (scope.mode === 'inline' && BLOCK_TAGS.has(tag))) {
|
|
857
915
|
this.add(EngineIssue.ELEMENT_REMOVED, { tag }, where, 'warning');
|
|
@@ -883,6 +941,18 @@ class Analyser {
|
|
|
883
941
|
return [];
|
|
884
942
|
}
|
|
885
943
|
if (tag === COLUMNS_TAG) return [this.visitColumns(el, scope, place, where)];
|
|
944
|
+
// A frame's title or tag, a task's instruction, tag or sub-task out of its place (after the content, twice,
|
|
945
|
+
// outside its frame or task): its content stays.
|
|
946
|
+
if (
|
|
947
|
+
tag === BOX_TITLE_TAG ||
|
|
948
|
+
tag === BOX_LABEL_TAG ||
|
|
949
|
+
tag === INSTRUCTION_TAG ||
|
|
950
|
+
tag === TASK_LABEL_TAG ||
|
|
951
|
+
tag === SUBTASK_TAG
|
|
952
|
+
) {
|
|
953
|
+
this.add(EngineIssue.PLACEMENT_INVALID, { tag }, where);
|
|
954
|
+
return this.visitNodes(el, scope, place.path);
|
|
955
|
+
}
|
|
886
956
|
// A column without its layout (`astl-match` has columns of its own, which it reads itself). The content stays,
|
|
887
957
|
// to be validated.
|
|
888
958
|
if (tag === COLUMN_TAG && !(host && spec)) {
|
|
@@ -910,6 +980,9 @@ class Analyser {
|
|
|
910
980
|
scope.help === null &&
|
|
911
981
|
(scope.parent === PAGE_TAG ||
|
|
912
982
|
scope.parent === COLUMN_TAG ||
|
|
983
|
+
scope.parent === BOX_TAG ||
|
|
984
|
+
scope.parent === TASK_TAG ||
|
|
985
|
+
scope.parent === SUBTASK_TAG ||
|
|
913
986
|
(scope.parent === DOC_TAG && !this.explicitPages));
|
|
914
987
|
if (!onPage) {
|
|
915
988
|
this.add(EngineIssue.PLACEMENT_INVALID, { tag: CHECK_TAG }, where);
|
|
@@ -943,7 +1016,14 @@ class Analyser {
|
|
|
943
1016
|
private visitBadge(el: ElementNode, scope: Scope, place: Place, where: Where): Node[] {
|
|
944
1017
|
const id = own(el.attrs, 'id');
|
|
945
1018
|
const at: Where = { ...where, element: id ?? null };
|
|
946
|
-
const inText =
|
|
1019
|
+
const inText =
|
|
1020
|
+
(TEXT_PARENTS.has(scope.parent) ||
|
|
1021
|
+
scope.parent === BOX_TITLE_TAG ||
|
|
1022
|
+
scope.parent === BOX_LABEL_TAG ||
|
|
1023
|
+
scope.parent === INSTRUCTION_TAG ||
|
|
1024
|
+
scope.parent === TASK_LABEL_TAG) &&
|
|
1025
|
+
!scope.inLink &&
|
|
1026
|
+
scope.mode !== 'text';
|
|
947
1027
|
if (!inText || (scope.host !== null && scope.mode !== 'flow')) {
|
|
948
1028
|
this.add(EngineIssue.PLACEMENT_INVALID, { tag: BADGE_TAG }, at);
|
|
949
1029
|
return this.visitNodes(el, scope, place.path);
|
|
@@ -998,6 +1078,180 @@ class Analyser {
|
|
|
998
1078
|
return element(el.tag, attrs, this.visitNodes(el, inner, place.path));
|
|
999
1079
|
}
|
|
1000
1080
|
|
|
1081
|
+
/**
|
|
1082
|
+
* A frame: directly on a page, in the document without pages or in a column, outside components and help; in another
|
|
1083
|
+
* frame it is `BOX_NESTED`, elsewhere `PLACEMENT_INVALID`, and its content is validated where it stands. Its title and
|
|
1084
|
+
* its tag lead it, inline text without components (a badge, a picture); then content as a page holds it.
|
|
1085
|
+
*/
|
|
1086
|
+
private visitBox(el: ElementNode, scope: Scope, place: Place, where: Where): Node[] {
|
|
1087
|
+
if (scope.box !== null) {
|
|
1088
|
+
this.add(EngineIssue.BOX_NESTED, { tag: BOX_TAG }, where);
|
|
1089
|
+
return this.visitNodes(el, scope, place.path);
|
|
1090
|
+
}
|
|
1091
|
+
const placed =
|
|
1092
|
+
scope.host === null &&
|
|
1093
|
+
scope.help === null &&
|
|
1094
|
+
scope.mode === 'flow' &&
|
|
1095
|
+
(scope.parent === PAGE_TAG ||
|
|
1096
|
+
scope.parent === COLUMN_TAG ||
|
|
1097
|
+
scope.parent === TASK_TAG ||
|
|
1098
|
+
scope.parent === SUBTASK_TAG ||
|
|
1099
|
+
(scope.parent === DOC_TAG && !this.explicitPages));
|
|
1100
|
+
if (!placed) {
|
|
1101
|
+
this.add(EngineIssue.PLACEMENT_INVALID, { tag: BOX_TAG }, where);
|
|
1102
|
+
return this.visitNodes(el, scope, place.path);
|
|
1103
|
+
}
|
|
1104
|
+
const attrs = this.richAttributes(el, where, scope);
|
|
1105
|
+
const allowed: Readonly<Record<string, readonly string[]>> = {
|
|
1106
|
+
'title-place': BOX_TITLE_PLACES,
|
|
1107
|
+
variant: BOX_VARIANTS,
|
|
1108
|
+
icon: BOX_ICONS,
|
|
1109
|
+
};
|
|
1110
|
+
for (const [name, values] of [...Object.entries(allowed), ['palette', null] as const]) {
|
|
1111
|
+
const value = own(attrs, name);
|
|
1112
|
+
if (value === undefined) continue;
|
|
1113
|
+
const palette = parseIntegerValue(value);
|
|
1114
|
+
const valid =
|
|
1115
|
+
values === null
|
|
1116
|
+
? palette !== null && palette >= 1 && palette <= MAX_BADGE_PALETTE
|
|
1117
|
+
: values.includes(value);
|
|
1118
|
+
if (valid) continue;
|
|
1119
|
+
this.invalid(BOX_TAG, name, value, values === null ? 'integer' : 'enum', values ?? [], where);
|
|
1120
|
+
delete attrs[name];
|
|
1121
|
+
}
|
|
1122
|
+
const inner: Scope = {
|
|
1123
|
+
...scope,
|
|
1124
|
+
...this.below(attrs, scope),
|
|
1125
|
+
parent: BOX_TAG,
|
|
1126
|
+
box: scope.column ? 'column' : 'top',
|
|
1127
|
+
};
|
|
1128
|
+
// The title, then the tag, may only lead: before the first content (text or any other element).
|
|
1129
|
+
const content = el.children.findIndex((n) =>
|
|
1130
|
+
n.type === 'text' ? !isHtmlWhitespace(n.value) : n.tag !== BOX_TITLE_TAG && n.tag !== BOX_LABEL_TAG,
|
|
1131
|
+
);
|
|
1132
|
+
let led = '';
|
|
1133
|
+
const children = this.visitNodes(el, inner, place.path, (child, childPlace) => {
|
|
1134
|
+
const head = child.tag === BOX_TITLE_TAG || child.tag === BOX_LABEL_TAG;
|
|
1135
|
+
const leads = content === -1 || el.children.indexOf(child) < content;
|
|
1136
|
+
const order = child.tag === BOX_TITLE_TAG ? led === '' : led !== BOX_LABEL_TAG;
|
|
1137
|
+
if (!head || !leads || !order) return this.visitElement(child, inner, childPlace);
|
|
1138
|
+
led = child.tag;
|
|
1139
|
+
const at = this.where(inner, childPlace, child.tag);
|
|
1140
|
+
const headAttrs = this.richAttributes(child, at, inner);
|
|
1141
|
+
const text: Scope = { ...inner, ...this.below(headAttrs, inner), parent: child.tag, mode: 'inline' };
|
|
1142
|
+
return [element(child.tag, headAttrs, this.visitNodes(child, text, childPlace.path))];
|
|
1143
|
+
});
|
|
1144
|
+
return [element(BOX_TAG, attrs, children)];
|
|
1145
|
+
}
|
|
1146
|
+
|
|
1147
|
+
/**
|
|
1148
|
+
* A task: directly on a page, in the document without pages, in a column or in a frame, outside components and help;
|
|
1149
|
+
* in another task it is `TASK_NESTED`, elsewhere `PLACEMENT_INVALID`, and its content is validated where it stands.
|
|
1150
|
+
* Its id is required: its Check is addressed by it. Its instruction, then its tags (up to MAX_TASK_LABELS), may only
|
|
1151
|
+
* lead it; out of place, each is `PLACEMENT_INVALID` and its text stays. Then a page's content, its sub-tasks among
|
|
1152
|
+
* it. The view has its number: its own `number`, after which the next ones count on, else the next of its page or of
|
|
1153
|
+
* the document (`task-numbering`), none with `none`. A task with `check` has a Check of its own for all its graded
|
|
1154
|
+
* questions, which the view holds as a Check element at its end.
|
|
1155
|
+
*/
|
|
1156
|
+
private visitTask(el: ElementNode, scope: Scope, place: Place, where: Where): Node[] {
|
|
1157
|
+
const id = own(el.attrs, 'id');
|
|
1158
|
+
const at: Where = { ...where, element: id ?? null };
|
|
1159
|
+
if (scope.task) {
|
|
1160
|
+
// One error for the task: its head's content and its sub-tasks stay, as the outer task's.
|
|
1161
|
+
this.add(EngineIssue.TASK_NESTED, { tag: TASK_TAG }, at);
|
|
1162
|
+
return this.visitNodes(el, scope, place.path, (child, childPlace) =>
|
|
1163
|
+
child.tag === SUBTASK_TAG
|
|
1164
|
+
? [this.visitSubtask(child, scope, childPlace)]
|
|
1165
|
+
: child.tag === INSTRUCTION_TAG || child.tag === TASK_LABEL_TAG
|
|
1166
|
+
? this.visitNodes(child, scope, childPlace.path)
|
|
1167
|
+
: null,
|
|
1168
|
+
);
|
|
1169
|
+
}
|
|
1170
|
+
const placed =
|
|
1171
|
+
scope.host === null &&
|
|
1172
|
+
scope.help === null &&
|
|
1173
|
+
scope.mode === 'flow' &&
|
|
1174
|
+
(scope.parent === PAGE_TAG ||
|
|
1175
|
+
scope.parent === COLUMN_TAG ||
|
|
1176
|
+
scope.parent === BOX_TAG ||
|
|
1177
|
+
(scope.parent === DOC_TAG && !this.explicitPages));
|
|
1178
|
+
if (!placed) {
|
|
1179
|
+
this.add(EngineIssue.PLACEMENT_INVALID, { tag: TASK_TAG }, at);
|
|
1180
|
+
return this.visitNodes(el, scope, place.path);
|
|
1181
|
+
}
|
|
1182
|
+
if (id === undefined) this.add(EngineIssue.ID_MISSING, { tag: TASK_TAG }, at);
|
|
1183
|
+
else this.sourceId(id, TASK_TAG, at);
|
|
1184
|
+
const attrs = this.richAttributes(
|
|
1185
|
+
element(TASK_TAG, Object.fromEntries(Object.entries(el.attrs).filter(([name]) => name !== 'id')), []),
|
|
1186
|
+
at,
|
|
1187
|
+
scope,
|
|
1188
|
+
);
|
|
1189
|
+
const bounded = (name: string, max: number): number | null => {
|
|
1190
|
+
const value = own(attrs, name);
|
|
1191
|
+
if (value === undefined) return null;
|
|
1192
|
+
const n = parseIntegerValue(value);
|
|
1193
|
+
if (n !== null && n >= 1 && n <= max) return n;
|
|
1194
|
+
this.invalid(TASK_TAG, name, value, 'integer', [], at);
|
|
1195
|
+
delete attrs[name];
|
|
1196
|
+
return null;
|
|
1197
|
+
};
|
|
1198
|
+
bounded('level', MAX_TASK_LEVEL);
|
|
1199
|
+
bounded('palette', MAX_BADGE_PALETTE);
|
|
1200
|
+
const written = bounded('number', Number.MAX_SAFE_INTEGER);
|
|
1201
|
+
// A boolean: present or not, whatever its value. The view holds the Check itself, at the end of the task.
|
|
1202
|
+
const check = Object.hasOwn(attrs, 'check');
|
|
1203
|
+
delete attrs.check;
|
|
1204
|
+
if (written !== null) this.taskNumber = written;
|
|
1205
|
+
else if (this.taskNumbering !== 'none') this.taskNumber += 1;
|
|
1206
|
+
if (written !== null || this.taskNumbering !== 'none') attrs.number = String(this.taskNumber);
|
|
1207
|
+
const inner: Scope = { ...scope, ...this.below(attrs, scope), parent: TASK_TAG, task: true };
|
|
1208
|
+
// The instruction, then the tags, may only lead: before the first content (text or any other element).
|
|
1209
|
+
const content = el.children.findIndex((n) =>
|
|
1210
|
+
n.type === 'text' ? !isHtmlWhitespace(n.value) : n.tag !== INSTRUCTION_TAG && n.tag !== TASK_LABEL_TAG,
|
|
1211
|
+
);
|
|
1212
|
+
let led = false;
|
|
1213
|
+
let labels = 0;
|
|
1214
|
+
const children = this.visitNodes(el, inner, place.path, (child, childPlace) => {
|
|
1215
|
+
if (child.tag === SUBTASK_TAG) return [this.visitSubtask(child, inner, childPlace)];
|
|
1216
|
+
const head = child.tag === INSTRUCTION_TAG || child.tag === TASK_LABEL_TAG;
|
|
1217
|
+
const leads = content === -1 || el.children.indexOf(child) < content;
|
|
1218
|
+
const order = child.tag === INSTRUCTION_TAG ? !led : labels < MAX_TASK_LABELS;
|
|
1219
|
+
if (!head || !leads || !order) return this.visitElement(child, inner, childPlace);
|
|
1220
|
+
led = true;
|
|
1221
|
+
const where = this.where(inner, childPlace, child.tag);
|
|
1222
|
+
const headAttrs = this.richAttributes(child, where, inner);
|
|
1223
|
+
if (child.tag === TASK_LABEL_TAG) {
|
|
1224
|
+
labels++;
|
|
1225
|
+
const icon = own(headAttrs, 'icon');
|
|
1226
|
+
if (icon !== undefined && !(TASK_LABEL_ICONS as readonly string[]).includes(icon)) {
|
|
1227
|
+
this.invalid(TASK_LABEL_TAG, 'icon', icon, 'enum', TASK_LABEL_ICONS, where);
|
|
1228
|
+
delete headAttrs.icon;
|
|
1229
|
+
}
|
|
1230
|
+
}
|
|
1231
|
+
const text: Scope = { ...inner, ...this.below(headAttrs, inner), parent: child.tag, mode: 'inline' };
|
|
1232
|
+
return [element(child.tag, headAttrs, this.visitNodes(child, text, childPlace.path))];
|
|
1233
|
+
});
|
|
1234
|
+
const node = element(TASK_TAG, attrs, children);
|
|
1235
|
+
if (check) {
|
|
1236
|
+
this.checkNodes.push({ node, page: scope.pageIndex, where: at });
|
|
1237
|
+
this.checkedTasks.set(node, id ?? '');
|
|
1238
|
+
}
|
|
1239
|
+
return [node];
|
|
1240
|
+
}
|
|
1241
|
+
|
|
1242
|
+
/** A sub-task: a lettered part of a task, a page's content in it; its `level`, A to D, shows beside its letter. */
|
|
1243
|
+
private visitSubtask(el: ElementNode, scope: Scope, place: Place): ElementNode {
|
|
1244
|
+
const where = this.where(scope, place, SUBTASK_TAG);
|
|
1245
|
+
const attrs = this.richAttributes(el, where, scope);
|
|
1246
|
+
const level = own(attrs, 'level');
|
|
1247
|
+
if (level !== undefined && !(SUBTASK_LEVELS as readonly string[]).includes(level)) {
|
|
1248
|
+
this.invalid(SUBTASK_TAG, 'level', level, 'enum', SUBTASK_LEVELS, where);
|
|
1249
|
+
delete attrs.level;
|
|
1250
|
+
}
|
|
1251
|
+
const inner: Scope = { ...scope, ...this.below(attrs, scope), parent: SUBTASK_TAG };
|
|
1252
|
+
return element(SUBTASK_TAG, attrs, this.visitNodes(el, inner, place.path));
|
|
1253
|
+
}
|
|
1254
|
+
|
|
1001
1255
|
/**
|
|
1002
1256
|
* A layout: 2 to 4 columns, directly on a page or in the document, outside components. Whatever its shape, its
|
|
1003
1257
|
* content is still validated, each piece as what it is.
|
|
@@ -1014,11 +1268,19 @@ class Analyser {
|
|
|
1014
1268
|
}
|
|
1015
1269
|
columns++;
|
|
1016
1270
|
const column = this.richAttributes(child, this.where(inner, childPlace, COLUMN_TAG), inner);
|
|
1017
|
-
const content: Scope = { ...inner, ...this.below(column, inner), parent: COLUMN_TAG };
|
|
1271
|
+
const content: Scope = { ...inner, ...this.below(column, inner), parent: COLUMN_TAG, column: true };
|
|
1018
1272
|
return [element(COLUMN_TAG, column, this.visitNodes(child, content, childPlace.path))];
|
|
1019
1273
|
});
|
|
1020
1274
|
if (children.some((n) => n.type === 'text' && !isHtmlWhitespace(n.value))) stray = true;
|
|
1021
|
-
|
|
1275
|
+
// In a frame or a task too, never in a column, however deep: two layout levels at most.
|
|
1276
|
+
const placed =
|
|
1277
|
+
scope.host === null &&
|
|
1278
|
+
!scope.column &&
|
|
1279
|
+
(scope.parent === PAGE_TAG ||
|
|
1280
|
+
scope.parent === DOC_TAG ||
|
|
1281
|
+
scope.parent === BOX_TAG ||
|
|
1282
|
+
scope.parent === TASK_TAG ||
|
|
1283
|
+
scope.parent === SUBTASK_TAG);
|
|
1022
1284
|
if (!placed || stray || columns < MIN_COLUMNS || columns > MAX_COLUMNS) {
|
|
1023
1285
|
this.add(EngineIssue.COLUMNS_INVALID, { tag: COLUMNS_TAG }, where);
|
|
1024
1286
|
}
|
|
@@ -1067,14 +1329,17 @@ class Analyser {
|
|
|
1067
1329
|
}
|
|
1068
1330
|
|
|
1069
1331
|
/**
|
|
1070
|
-
* A figure: directly in a page, a column or the document (a block), or in a rich block, with exactly one picture
|
|
1071
|
-
* one caption. A caption holds rich inline text, which may hold inline components like any text.
|
|
1332
|
+
* A figure: directly in a page, a column, a frame or the document (a block), or in a rich block, with exactly one picture
|
|
1333
|
+
* and at most one caption. A caption holds rich inline text, which may hold inline components like any text.
|
|
1072
1334
|
*/
|
|
1073
1335
|
private visitFigure(el: ElementNode, scope: Scope, place: Place, where: Where): ElementNode {
|
|
1074
1336
|
if (
|
|
1075
1337
|
scope.parent !== PAGE_TAG &&
|
|
1076
1338
|
scope.parent !== DOC_TAG &&
|
|
1077
1339
|
scope.parent !== COLUMN_TAG &&
|
|
1340
|
+
scope.parent !== BOX_TAG &&
|
|
1341
|
+
scope.parent !== TASK_TAG &&
|
|
1342
|
+
scope.parent !== SUBTASK_TAG &&
|
|
1078
1343
|
scope.parent !== scope.host?.manifest.tag &&
|
|
1079
1344
|
!HELP_TAGS.has(scope.parent)
|
|
1080
1345
|
) {
|
|
@@ -1132,6 +1397,7 @@ class Analyser {
|
|
|
1132
1397
|
pageIndex: index,
|
|
1133
1398
|
pageName: namePage(index + 1, own(el.attrs, 'label')),
|
|
1134
1399
|
};
|
|
1400
|
+
if (this.taskNumbering === 'page') this.taskNumber = 0;
|
|
1135
1401
|
const children = this.visitNodes(el, scope, place.path);
|
|
1136
1402
|
if (isBlank(children)) this.add(EngineIssue.PAGE_EMPTY, scope.pageName, where, 'warning');
|
|
1137
1403
|
return element(PAGE_TAG, attrs, children);
|
|
@@ -1338,7 +1604,7 @@ class Analyser {
|
|
|
1338
1604
|
this.add(EngineIssue.COMPONENT_NESTED, { tag: child.tag, parent: tag }, where);
|
|
1339
1605
|
} else if (child.tag === COLUMNS_TAG || child.tag === COLUMN_TAG) {
|
|
1340
1606
|
this.add(EngineIssue.COLUMNS_INVALID, { tag: child.tag }, where);
|
|
1341
|
-
} else if (child.tag === STYLE_TAG) {
|
|
1607
|
+
} else if (child.tag === STYLE_TAG || child.tag === BOX_TAG || child.tag === TASK_TAG) {
|
|
1342
1608
|
this.add(EngineIssue.PLACEMENT_INVALID, { tag: child.tag }, where);
|
|
1343
1609
|
} else if (child.tag.includes('-') && child.tag !== DOC_TAG && child.tag !== PAGE_TAG) {
|
|
1344
1610
|
// A child another type declares is known, just not accepted here.
|
package/src/compile.ts
CHANGED
|
@@ -31,6 +31,7 @@ import {
|
|
|
31
31
|
type ScoringGroup,
|
|
32
32
|
type SlotKind,
|
|
33
33
|
STYLE_TEMPLATE_ATTRIBUTE,
|
|
34
|
+
TASK_TAG,
|
|
34
35
|
type UrlPolicy,
|
|
35
36
|
} from '@astl/core';
|
|
36
37
|
import type { ComponentRecord, DocRecord, HelpRecord } from './analyse.js';
|
|
@@ -158,6 +159,9 @@ export async function compileDocument(doc: DocRecord, setup: CompileSetup): Prom
|
|
|
158
159
|
const viewNode = (node: Node): Node => {
|
|
159
160
|
if (node.type === 'text') return node;
|
|
160
161
|
const rec = doc.components.get(node);
|
|
162
|
+
// A task's own Check is a Check element at its end, as one written there would be.
|
|
163
|
+
if (rec === undefined && node.tag === TASK_TAG)
|
|
164
|
+
return element(node.tag, node.attrs, [...viewNodes(node.children), ...viewCheck(node)]);
|
|
161
165
|
if (rec === undefined) return element(node.tag, node.attrs, viewNodes(node.children));
|
|
162
166
|
const ctx = contextOf(rec);
|
|
163
167
|
const view = rec.definition.toView(rec.element, ctx);
|
package/src/engine.ts
CHANGED
|
@@ -4,6 +4,10 @@
|
|
|
4
4
|
*/
|
|
5
5
|
import {
|
|
6
6
|
allAttributes,
|
|
7
|
+
BADGE_TAG,
|
|
8
|
+
BOX_LABEL_TAG,
|
|
9
|
+
BOX_TAG,
|
|
10
|
+
BOX_TITLE_TAG,
|
|
7
11
|
CLASS_ATTRIBUTE,
|
|
8
12
|
COLUMN_TAG,
|
|
9
13
|
COLUMNS_TAG,
|
|
@@ -21,6 +25,7 @@ import {
|
|
|
21
25
|
type EngineOptions,
|
|
22
26
|
HELP_ATTRIBUTES,
|
|
23
27
|
HELP_TAGS,
|
|
28
|
+
INSTRUCTION_TAG,
|
|
24
29
|
isSafeUrl,
|
|
25
30
|
isStyleTemplate,
|
|
26
31
|
type NormalisationProfile,
|
|
@@ -30,7 +35,10 @@ import {
|
|
|
30
35
|
STYLE_TAG,
|
|
31
36
|
STYLE_TEMPLATE_NAME,
|
|
32
37
|
type StyleTemplate,
|
|
38
|
+
SUBTASK_TAG,
|
|
33
39
|
strictUrlPolicy,
|
|
40
|
+
TASK_LABEL_TAG,
|
|
41
|
+
TASK_TAG,
|
|
34
42
|
} from '@astl/core';
|
|
35
43
|
import { parseStyleColor } from '@astl/core/declarations';
|
|
36
44
|
import { analyse } from './analyse.js';
|
|
@@ -44,7 +52,21 @@ const MIN_SECRET_BYTES = 16;
|
|
|
44
52
|
const NAME_PATTERN = /^[a-z][a-z0-9-]*$/;
|
|
45
53
|
const KEBAB = /^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/;
|
|
46
54
|
/** Elements of the format itself, which no component can be. A child can share a tag: inside its host it wins. */
|
|
47
|
-
const FORMAT_TAGS: ReadonlySet<string> = new Set([
|
|
55
|
+
const FORMAT_TAGS: ReadonlySet<string> = new Set([
|
|
56
|
+
DOC_TAG,
|
|
57
|
+
PAGE_TAG,
|
|
58
|
+
STYLE_TAG,
|
|
59
|
+
COLUMNS_TAG,
|
|
60
|
+
COLUMN_TAG,
|
|
61
|
+
BADGE_TAG,
|
|
62
|
+
BOX_TAG,
|
|
63
|
+
BOX_TITLE_TAG,
|
|
64
|
+
BOX_LABEL_TAG,
|
|
65
|
+
TASK_TAG,
|
|
66
|
+
INSTRUCTION_TAG,
|
|
67
|
+
TASK_LABEL_TAG,
|
|
68
|
+
SUBTASK_TAG,
|
|
69
|
+
]);
|
|
48
70
|
|
|
49
71
|
function checkManifest(manifest: ComponentManifest, slotKinds: ReadonlyMap<string, SlotKind>): void {
|
|
50
72
|
const tag = manifest.tag;
|
package/src/messages/en.json
CHANGED
|
@@ -40,6 +40,8 @@
|
|
|
40
40
|
"STYLE_LOW_CONTRAST": "The text of element {tag} is hard to read on its background: contrast {ratio}:1, at least {min}:1 is recommended.",
|
|
41
41
|
"COLUMNS_INVALID": "Element {tag} is not a valid layout: <astl-columns> goes directly on a page and holds 2 to 4 <astl-column> elements, with nothing else between them, and layouts do not nest.",
|
|
42
42
|
"BADGE_TOO_LONG": "This badge holds more than {max} characters: keep a badge to a number or a word or two, and write longer text beside it.",
|
|
43
|
+
"BOX_NESTED": "A frame cannot stand inside another frame: put it after the frame, or use a heading inside it.",
|
|
44
|
+
"TASK_NESTED": "A task cannot stand inside another task: put it after the task, or make it a sub-task (astl-subtask).",
|
|
43
45
|
"HELP_UNGRADED": "Element <{tag}> works only inside a graded interaction or on a page with one. Move it there: for now it is left out.",
|
|
44
46
|
"HELP_EMPTY": "Element <{tag}> is empty, so it is left out. Write its text or remove it.",
|
|
45
47
|
"PAGE_HELP_UNCHECKED": "Page {page}{titled, select, yes { (“{title}”)} other {}} has help for students but no Check of its own: turn on “Check this page”. Until then the player adds that Check by itself.",
|
package/src/messages/pl.json
CHANGED
|
@@ -40,6 +40,8 @@
|
|
|
40
40
|
"STYLE_LOW_CONTRAST": "Tekst elementu {tag} jest trudny do odczytania na swoim tle: kontrast {ratio}:1, zalecane co najmniej {min}:1.",
|
|
41
41
|
"COLUMNS_INVALID": "Element {tag} nie tworzy poprawnego układu: <astl-columns> stoi bezpośrednio na stronie i zawiera od 2 do 4 elementów <astl-column>, bez niczego między nimi, a układów nie można zagnieżdżać.",
|
|
42
42
|
"BADGE_TOO_LONG": "Ten znaczek ma więcej niż {max} znaków: w znaczku zostaw liczbę albo słowo lub dwa, a dłuższy tekst napisz obok.",
|
|
43
|
+
"BOX_NESTED": "Ramki nie można wstawić do innej ramki: postaw ją za ramką albo użyj w niej nagłówka.",
|
|
44
|
+
"TASK_NESTED": "Zadania nie można wstawić do innego zadania: postaw je za zadaniem albo zrób z niego podpunkt (astl-subtask).",
|
|
43
45
|
"HELP_UNGRADED": "Element <{tag}> działa tylko w ocenianym zadaniu albo na stronie z takim zadaniem. Przenieś go tam: na razie jest pominięty.",
|
|
44
46
|
"HELP_EMPTY": "Element <{tag}> jest pusty, więc jest pominięty. Wpisz jego treść albo go usuń.",
|
|
45
47
|
"PAGE_HELP_UNCHECKED": "Strona {page}{titled, select, yes { („{title}”)} other {}} ma pomoc dla uczniów, ale nie ma własnego sprawdzania: włącz „Sprawdź tę stronę”. Do tego czasu odtwarzacz sam dodaje to sprawdzanie.",
|