@astl/engine 0.0.0 → 0.1.1
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 +43 -0
- package/LICENSE +202 -0
- package/README.md +41 -0
- package/dist/analyse.d.ts +68 -0
- package/dist/analyse.d.ts.map +1 -0
- package/dist/analyse.js +1179 -0
- package/dist/analyse.js.map +1 -0
- package/dist/checker.d.ts +19 -0
- package/dist/checker.d.ts.map +1 -0
- package/dist/checker.js +103 -0
- package/dist/checker.js.map +1 -0
- package/dist/compile.d.ts +16 -0
- package/dist/compile.d.ts.map +1 -0
- package/dist/compile.js +329 -0
- package/dist/compile.js.map +1 -0
- package/dist/conformance.d.ts +35 -0
- package/dist/conformance.d.ts.map +1 -0
- package/dist/conformance.js +101 -0
- package/dist/conformance.js.map +1 -0
- package/dist/engine.d.ts +13 -0
- package/dist/engine.d.ts.map +1 -0
- package/dist/engine.js +231 -0
- package/dist/engine.js.map +1 -0
- package/dist/grade.d.ts +9 -0
- package/dist/grade.d.ts.map +1 -0
- package/dist/grade.js +192 -0
- package/dist/grade.js.map +1 -0
- package/dist/guard.d.ts +45 -0
- package/dist/guard.d.ts.map +1 -0
- package/dist/guard.js +327 -0
- package/dist/guard.js.map +1 -0
- package/dist/help.d.ts +32 -0
- package/dist/help.d.ts.map +1 -0
- package/dist/help.js +120 -0
- package/dist/help.js.map +1 -0
- package/dist/html.d.ts +31 -0
- package/dist/html.d.ts.map +1 -0
- package/dist/html.js +246 -0
- package/dist/html.js.map +1 -0
- package/dist/ids.d.ts +35 -0
- package/dist/ids.d.ts.map +1 -0
- package/dist/ids.js +126 -0
- package/dist/ids.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -0
- package/dist/kit/attributes.d.ts +6 -0
- package/dist/kit/attributes.d.ts.map +1 -0
- package/dist/kit/attributes.js +20 -0
- package/dist/kit/attributes.js.map +1 -0
- package/dist/kit/index.d.ts +43 -0
- package/dist/kit/index.d.ts.map +1 -0
- package/dist/kit/index.js +118 -0
- package/dist/kit/index.js.map +1 -0
- package/dist/limits.d.ts +17 -0
- package/dist/limits.d.ts.map +1 -0
- package/dist/limits.js +173 -0
- package/dist/limits.js.map +1 -0
- package/dist/messages/en.json +48 -0
- package/dist/messages/pl.json +48 -0
- package/dist/messages.d.ts +4 -0
- package/dist/messages.d.ts.map +1 -0
- package/dist/messages.js +9 -0
- package/dist/messages.js.map +1 -0
- package/dist/round.d.ts +3 -0
- package/dist/round.d.ts.map +1 -0
- package/dist/round.js +5 -0
- package/dist/round.js.map +1 -0
- package/dist/slot-kinds.d.ts +58 -0
- package/dist/slot-kinds.d.ts.map +1 -0
- package/dist/slot-kinds.js +151 -0
- package/dist/slot-kinds.js.map +1 -0
- package/dist/text.d.ts +23 -0
- package/dist/text.d.ts.map +1 -0
- package/dist/text.js +236 -0
- package/dist/text.js.map +1 -0
- package/package.json +51 -4
- package/src/analyse.ts +1396 -0
- package/src/checker.ts +132 -0
- package/src/compile.ts +394 -0
- package/src/conformance.ts +120 -0
- package/src/engine.ts +274 -0
- package/src/grade.ts +229 -0
- package/src/guard.ts +356 -0
- package/src/help.ts +141 -0
- package/src/html.ts +274 -0
- package/src/ids.ts +144 -0
- package/src/index.ts +12 -0
- package/src/kit/attributes.ts +21 -0
- package/src/kit/index.ts +140 -0
- package/src/limits.ts +153 -0
- package/src/messages/en.json +48 -0
- package/src/messages/pl.json +48 -0
- package/src/messages.ts +9 -0
- package/src/round.ts +4 -0
- package/src/slot-kinds.ts +212 -0
- package/src/text.ts +234 -0
package/src/analyse.ts
ADDED
|
@@ -0,0 +1,1396 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One pass over a source that sanitises it and validates it against the registered manifests. `validate` and
|
|
3
|
+
* `compile` share it: compile only runs on an error-free analysis and works on the sanitised tree it returns.
|
|
4
|
+
*/
|
|
5
|
+
import {
|
|
6
|
+
type AttributeSpec,
|
|
7
|
+
allAttributes,
|
|
8
|
+
BLOCK_TAGS,
|
|
9
|
+
CAPTION_TAG,
|
|
10
|
+
type ChildSpec,
|
|
11
|
+
CLASS_ATTRIBUTE,
|
|
12
|
+
COLUMN_TAG,
|
|
13
|
+
COLUMNS_TAG,
|
|
14
|
+
CONTENT_ATTRIBUTES,
|
|
15
|
+
type ComponentDefinition,
|
|
16
|
+
type ComponentManifest,
|
|
17
|
+
DEFAULT_DOC_KIND,
|
|
18
|
+
type Direction,
|
|
19
|
+
DOC_ATTRIBUTES,
|
|
20
|
+
DOC_KINDS,
|
|
21
|
+
DOC_TAG,
|
|
22
|
+
type DocKind,
|
|
23
|
+
directionOf,
|
|
24
|
+
type ElementNode,
|
|
25
|
+
ENGINE_NAMESPACE,
|
|
26
|
+
EngineIssue,
|
|
27
|
+
EXPLANATION_TAG,
|
|
28
|
+
element,
|
|
29
|
+
FEEDBACK_TAG,
|
|
30
|
+
FEEDBACK_VARIANTS,
|
|
31
|
+
type FeedbackVariant,
|
|
32
|
+
FIGURE_TAG,
|
|
33
|
+
type GroundColors,
|
|
34
|
+
HELP_TAGS,
|
|
35
|
+
HINT_TAG,
|
|
36
|
+
INLINE_TAGS,
|
|
37
|
+
type Issue,
|
|
38
|
+
type IssueParams,
|
|
39
|
+
isElement,
|
|
40
|
+
isSourceId,
|
|
41
|
+
LANGUAGE_TAG_PATTERN,
|
|
42
|
+
MAX_ATTRIBUTES_PER_ELEMENT,
|
|
43
|
+
MAX_COLUMNS,
|
|
44
|
+
MAX_NESTING_DEPTH,
|
|
45
|
+
MAX_SOURCE_LENGTH,
|
|
46
|
+
MIN_COLUMNS,
|
|
47
|
+
type Node,
|
|
48
|
+
type NormalisationProfile,
|
|
49
|
+
PAGE_ATTRIBUTES,
|
|
50
|
+
PAGE_TAG,
|
|
51
|
+
REMOVED_WITH_CONTENT,
|
|
52
|
+
type Severity,
|
|
53
|
+
STYLE_TAG,
|
|
54
|
+
STYLE_TEMPLATE_ATTRIBUTE,
|
|
55
|
+
type StyleScope,
|
|
56
|
+
type StyleTemplate,
|
|
57
|
+
SUPPORTED_FORMATS,
|
|
58
|
+
TAG_ATTRIBUTES,
|
|
59
|
+
URL_ATTRIBUTES,
|
|
60
|
+
type UrlPolicy,
|
|
61
|
+
} from '@astl/core';
|
|
62
|
+
import {
|
|
63
|
+
cleanStyle,
|
|
64
|
+
isStyleName,
|
|
65
|
+
MAX_INLINED_STYLES,
|
|
66
|
+
MAX_STYLE_LENGTH,
|
|
67
|
+
MAX_STYLE_RULES,
|
|
68
|
+
mergeStyles,
|
|
69
|
+
overStyleColor,
|
|
70
|
+
parseStyleColor,
|
|
71
|
+
type StyleColor,
|
|
72
|
+
type StyleDeclaration,
|
|
73
|
+
styleContrast,
|
|
74
|
+
styleText,
|
|
75
|
+
} from '@astl/core/declarations';
|
|
76
|
+
import { appendText, isHtmlWhitespace, parseSource } from './html.js';
|
|
77
|
+
import { isBooleanValue, parseDecimalValue, parseIntegerValue } from './kit/attributes.js';
|
|
78
|
+
import { mostAttributes } from './limits.js';
|
|
79
|
+
import { round6 } from './round.js';
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Where inline components may sit: text-holding blocks, captions and inline formatting, never inside a link (and a
|
|
83
|
+
* line break or a picture holds nothing).
|
|
84
|
+
*/
|
|
85
|
+
const TEXT_PARENTS: ReadonlySet<string> = new Set([
|
|
86
|
+
...[...BLOCK_TAGS].filter((t) => t !== 'ul' && t !== 'ol'),
|
|
87
|
+
...[...INLINE_TAGS].filter((t) => t !== 'a' && t !== 'br' && t !== 'img'),
|
|
88
|
+
CAPTION_TAG,
|
|
89
|
+
]);
|
|
90
|
+
|
|
91
|
+
const DIRECTIONS: ReadonlySet<string> = new Set(['ltr', 'rtl', 'auto']);
|
|
92
|
+
|
|
93
|
+
/** The attributes of `<astl-style>`. */
|
|
94
|
+
const RULE_ATTRIBUTES: readonly string[] = ['name', 'label', 'for', 'style'];
|
|
95
|
+
/** The tags of the format a `for` rule can name, besides inline and block tags and what components declare. */
|
|
96
|
+
const RULE_TARGETS: ReadonlySet<string> = new Set([
|
|
97
|
+
DOC_TAG,
|
|
98
|
+
PAGE_TAG,
|
|
99
|
+
COLUMNS_TAG,
|
|
100
|
+
COLUMN_TAG,
|
|
101
|
+
FIGURE_TAG,
|
|
102
|
+
CAPTION_TAG,
|
|
103
|
+
]);
|
|
104
|
+
/** The contrast WCAG asks of normal text (AA); large text may have less, and then the warning is only stricter. */
|
|
105
|
+
const MIN_CONTRAST = 4.5;
|
|
106
|
+
|
|
107
|
+
export interface ComponentRecord {
|
|
108
|
+
readonly definition: ComponentDefinition;
|
|
109
|
+
/** Sanitised element, as given to the definition. */
|
|
110
|
+
readonly element: ElementNode;
|
|
111
|
+
readonly sourceId: string;
|
|
112
|
+
/** Index into DocRecord.pages. */
|
|
113
|
+
readonly page: number;
|
|
114
|
+
readonly lang: string;
|
|
115
|
+
readonly dir: Direction;
|
|
116
|
+
/** Local ids (values of `id`-typed child attributes), in document order. */
|
|
117
|
+
readonly localIds: readonly string[];
|
|
118
|
+
/** Its help (`astl-hint`, `astl-explanation`, `astl-feedback` children), taken out of the element; null for none. */
|
|
119
|
+
readonly help: HelpRecord | null;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** The help of an interaction or a page as the source has it: the sanitised content of each part. */
|
|
123
|
+
export interface HelpRecord {
|
|
124
|
+
/** In source order; `cost` a fraction 0..1 of the points. */
|
|
125
|
+
readonly hints: readonly { readonly content: readonly Node[]; readonly cost: number }[];
|
|
126
|
+
readonly explanation: readonly Node[] | null;
|
|
127
|
+
readonly feedback: Readonly<Partial<Record<FeedbackVariant, readonly Node[]>>>;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export interface PageRecord {
|
|
131
|
+
/** null for the implicit page of a document without pages. */
|
|
132
|
+
readonly sourceId: string | null;
|
|
133
|
+
readonly label: string | null;
|
|
134
|
+
/** The help placed directly on the page, taken out of it; null for none. */
|
|
135
|
+
readonly help: HelpRecord | null;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export interface DocRecord {
|
|
139
|
+
/** Sanitised `astl-doc`; with explicit pages, the page elements are its direct children. */
|
|
140
|
+
readonly root: ElementNode;
|
|
141
|
+
readonly lang: string;
|
|
142
|
+
readonly dir: Direction;
|
|
143
|
+
readonly kind: DocKind;
|
|
144
|
+
readonly explicitPages: boolean;
|
|
145
|
+
readonly pages: readonly PageRecord[];
|
|
146
|
+
/** The `style` of the implicit page of a document without pages, which only the view has: its rule's, or null. */
|
|
147
|
+
readonly pageStyle: string | null;
|
|
148
|
+
/** Sanitised page elements by identity, to their index in `pages`. */
|
|
149
|
+
readonly pageNodes: ReadonlyMap<ElementNode, number>;
|
|
150
|
+
/** Components by sanitised node identity, in document order. */
|
|
151
|
+
readonly components: ReadonlyMap<ElementNode, ComponentRecord>;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export interface Analysis {
|
|
155
|
+
readonly ok: boolean;
|
|
156
|
+
readonly issues: readonly Issue[];
|
|
157
|
+
/** null when the analysis stopped before reading the document (size, root, format). */
|
|
158
|
+
readonly doc: DocRecord | null;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export interface AnalyseOptions {
|
|
162
|
+
readonly definitions: ReadonlyMap<string, ComponentDefinition>;
|
|
163
|
+
readonly urlPolicy: UrlPolicy;
|
|
164
|
+
readonly profile: NormalisationProfile;
|
|
165
|
+
/** Registered style templates by name, for `style-template` values. Default: none. */
|
|
166
|
+
readonly styleTemplates?: ReadonlyMap<string, StyleTemplate>;
|
|
167
|
+
/** The colours of the page, for STYLE_LOW_CONTRAST. Default: only colours declared together are compared. */
|
|
168
|
+
readonly groundColors?: GroundColors;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export function analyse(source: string, options: AnalyseOptions): Analysis {
|
|
172
|
+
return new Analyser(options).run(source);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
interface Host {
|
|
176
|
+
readonly manifest: ComponentManifest;
|
|
177
|
+
readonly id: string | null;
|
|
178
|
+
/** The declared children read so far (in a rich block, its inline children), where they are, their local ids. */
|
|
179
|
+
readonly children: ElementNode[];
|
|
180
|
+
readonly places: Map<ElementNode, Where>;
|
|
181
|
+
readonly localIds: Set<string>;
|
|
182
|
+
readonly help: HelpParts;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** Help read so far for one owner; `where` says where each part was, for issues found once the owner is read. */
|
|
186
|
+
interface HelpParts {
|
|
187
|
+
readonly hints: { readonly content: readonly Node[]; readonly cost: number }[];
|
|
188
|
+
explanation: readonly Node[] | null;
|
|
189
|
+
readonly feedback: Partial<Record<FeedbackVariant, readonly Node[]>>;
|
|
190
|
+
readonly where: { readonly tag: string; readonly at: Where }[];
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const noHelp = (): HelpParts => ({ hints: [], explanation: null, feedback: {}, where: [] });
|
|
194
|
+
|
|
195
|
+
const helpRecord = (parts: HelpParts): HelpRecord | null =>
|
|
196
|
+
parts.where.length === 0
|
|
197
|
+
? null
|
|
198
|
+
: { hints: parts.hints, explanation: parts.explanation, feedback: parts.feedback };
|
|
199
|
+
|
|
200
|
+
interface Scope {
|
|
201
|
+
/** Tag of the nearest kept ancestor (unwrapped elements do not count). */
|
|
202
|
+
readonly parent: string;
|
|
203
|
+
readonly inLink: boolean;
|
|
204
|
+
/** `flow`: rich text; `inline` / `text`: content of a component child with that content model. */
|
|
205
|
+
readonly mode: 'flow' | 'inline' | 'text';
|
|
206
|
+
/** The component whose child content this is. */
|
|
207
|
+
readonly host: Host | null;
|
|
208
|
+
/** Inside help: its tag and the id of the interaction it belongs to (null for a page's). */
|
|
209
|
+
readonly help: { readonly tag: string; readonly element: string | null } | null;
|
|
210
|
+
readonly page: string | null;
|
|
211
|
+
readonly pageIndex: number;
|
|
212
|
+
/** How messages name the page this content is on (`namePage`). */
|
|
213
|
+
readonly pageName: IssueParams;
|
|
214
|
+
readonly lang: string;
|
|
215
|
+
readonly dir: Direction;
|
|
216
|
+
/** The text colour here, null where it cannot be known (a palette token, or no page colours to start from). */
|
|
217
|
+
readonly ink: StyleColor | null;
|
|
218
|
+
/** The opaque colour text is seen on here; null as above. */
|
|
219
|
+
readonly ground: StyleColor | null;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
interface Place {
|
|
223
|
+
readonly path: string;
|
|
224
|
+
/** Pre-order position in the source, used to sort issues into document order. */
|
|
225
|
+
readonly order: number;
|
|
226
|
+
/** 1-based index among same-tag siblings. */
|
|
227
|
+
readonly index: number;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
interface Where {
|
|
231
|
+
readonly order: number;
|
|
232
|
+
readonly page: string | null;
|
|
233
|
+
readonly element: string | null;
|
|
234
|
+
readonly tag: string | null;
|
|
235
|
+
readonly path: string;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
type Visitor = (el: ElementNode, place: Place) => Node[] | null;
|
|
239
|
+
|
|
240
|
+
function own(attrs: Readonly<Record<string, string>>, name: string): string | undefined {
|
|
241
|
+
return Object.hasOwn(attrs, name) ? attrs[name] : undefined;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function segment(el: ElementNode, index: number): string {
|
|
245
|
+
const id = own(el.attrs, 'id');
|
|
246
|
+
return id ? `${el.tag}#${id}` : `${el.tag}[${index}]`;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function hasContent(nodes: readonly Node[]): boolean {
|
|
250
|
+
return nodes.some((n) => n.type === 'element' || n.value.trim() !== '');
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** Nothing a student would see: whitespace, line breaks and paragraphs of those (an editor's empty line). */
|
|
254
|
+
function isBlank(nodes: readonly Node[]): boolean {
|
|
255
|
+
return nodes.every((n) =>
|
|
256
|
+
n.type === 'text'
|
|
257
|
+
? n.value.trim() === ''
|
|
258
|
+
: n.tag === 'br' ||
|
|
259
|
+
((n.tag === 'p' || n.tag === COLUMNS_TAG || n.tag === COLUMN_TAG) && isBlank(n.children)),
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* How a message names a page: by its number (from 1; 0 for the content of a document without pages) and its title when
|
|
265
|
+
* it has one (`titled`: `yes` or `no`), never by its id, which authors do not see.
|
|
266
|
+
*/
|
|
267
|
+
function namePage(number: number, label: string | undefined): IssueParams {
|
|
268
|
+
const title = label?.trim() ?? '';
|
|
269
|
+
return { page: number, title, titled: title === '' ? 'no' : 'yes' };
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function isSupportedFormat(value: string): boolean {
|
|
273
|
+
return /^\d+$/.test(value) && SUPPORTED_FORMATS.includes(Number(value));
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function placementAllowed(display: 'inline' | 'block', scope: Scope): boolean {
|
|
277
|
+
if (display === 'inline') return !scope.inLink && TEXT_PARENTS.has(scope.parent);
|
|
278
|
+
return scope.parent === PAGE_TAG || scope.parent === DOC_TAG || scope.parent === COLUMN_TAG;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
function outOfRange(value: number, spec: AttributeSpec): boolean {
|
|
282
|
+
return (spec.min !== undefined && value < spec.min) || (spec.max !== undefined && value > spec.max);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/** Content language and direction below an element with these (already validated) attributes. */
|
|
286
|
+
function languageOf(
|
|
287
|
+
attrs: Readonly<Record<string, string>>,
|
|
288
|
+
scope: Pick<Scope, 'lang' | 'dir'>,
|
|
289
|
+
): Pick<Scope, 'lang' | 'dir'> {
|
|
290
|
+
const lang = own(attrs, 'lang');
|
|
291
|
+
const dir = own(attrs, 'dir');
|
|
292
|
+
return {
|
|
293
|
+
lang: lang ?? scope.lang,
|
|
294
|
+
dir: dir === 'ltr' || dir === 'rtl' ? dir : lang !== undefined ? directionOf(lang) : scope.dir,
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
type Colours = Pick<Scope, 'ink' | 'ground'>;
|
|
299
|
+
|
|
300
|
+
const NO_COLOURS: Colours = { ink: null, ground: null };
|
|
301
|
+
|
|
302
|
+
/** The text colour and the ground of an element with these (already clean) attributes, inside `around`. */
|
|
303
|
+
function coloursOf(attrs: Readonly<Record<string, string>>, around: Colours): Colours {
|
|
304
|
+
const style = own(attrs, 'style');
|
|
305
|
+
if (style === undefined) return around;
|
|
306
|
+
let { ink, ground } = around;
|
|
307
|
+
for (const [name, value] of cleanStyle(style).declarations) {
|
|
308
|
+
if (name === 'color') {
|
|
309
|
+
// On `color`, `currentcolor` is the inherited colour.
|
|
310
|
+
if (value !== 'currentcolor') ink = parseStyleColor(value);
|
|
311
|
+
} else if (name === 'background-color' && value !== 'transparent') {
|
|
312
|
+
const color = parseStyleColor(value);
|
|
313
|
+
if (color === null) ground = null;
|
|
314
|
+
else if (color[3] === 1) ground = color;
|
|
315
|
+
else if (ground !== null) ground = overStyleColor(color, ground);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
return { ink, ground };
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
class Analyser {
|
|
322
|
+
private readonly options: AnalyseOptions;
|
|
323
|
+
private readonly found: { readonly order: number; readonly issue: Issue }[] = [];
|
|
324
|
+
private readonly ids = new Set<string>();
|
|
325
|
+
private readonly components = new Map<ElementNode, ComponentRecord>();
|
|
326
|
+
/** Components by source id (the first of duplicates), for `ValidateContext.find`. */
|
|
327
|
+
private readonly bySource = new Map<string, ComponentRecord>();
|
|
328
|
+
/** Definition checks, run once the whole document is read so `find` sees components that come later. */
|
|
329
|
+
private readonly checks: (() => void)[] = [];
|
|
330
|
+
private order = 0;
|
|
331
|
+
private errors = 0;
|
|
332
|
+
/**
|
|
333
|
+
* Components of any registered type, graded or not: a document of ungraded ones alone (nothing to answer, or only
|
|
334
|
+
* answers for a teacher) is a practice-only exercise, not an empty one.
|
|
335
|
+
*/
|
|
336
|
+
private componentCount = 0;
|
|
337
|
+
/** Pages (by index) with a graded component: a page's own help needs one. */
|
|
338
|
+
private readonly gradedPages = new Set<number>();
|
|
339
|
+
/** The help placed directly on each page, by page index. */
|
|
340
|
+
private readonly pageHelp = new Map<number, HelpParts>();
|
|
341
|
+
private explicitPages = false;
|
|
342
|
+
/** While help content is read (`visitHelp`): it takes no style (see `inline`). */
|
|
343
|
+
private inHelp = false;
|
|
344
|
+
/** The components that declare each child tag, as listed in CHILD_OUTSIDE. */
|
|
345
|
+
private readonly owners = new Map<string, string>();
|
|
346
|
+
/** The rules of the document: named styles by name, the others by tag. */
|
|
347
|
+
private readonly namedStyles = new Map<string, readonly StyleDeclaration[]>();
|
|
348
|
+
private readonly tagStyles = new Map<string, readonly StyleDeclaration[]>();
|
|
349
|
+
/** Characters of `style` written so far: rules are short, the elements they reach may be many. */
|
|
350
|
+
private inlined = 0;
|
|
351
|
+
/** The colour pairs already warned about: one rule on every paragraph is one thing to fix. */
|
|
352
|
+
private readonly lowContrast = new Set<string>();
|
|
353
|
+
/** The colours of the page, or none; everything below is compared with them only if there are any. */
|
|
354
|
+
private readonly page: Colours;
|
|
355
|
+
private readonly tracksColours: boolean;
|
|
356
|
+
|
|
357
|
+
constructor(options: AnalyseOptions) {
|
|
358
|
+
this.options = options;
|
|
359
|
+
const surface = options.groundColors && parseStyleColor(options.groundColors.surface);
|
|
360
|
+
const ink = options.groundColors && parseStyleColor(options.groundColors.ink);
|
|
361
|
+
this.tracksColours = surface != null && ink != null;
|
|
362
|
+
this.page = this.tracksColours ? { ink: ink ?? null, ground: surface ?? null } : NO_COLOURS;
|
|
363
|
+
for (const { manifest } of options.definitions.values()) {
|
|
364
|
+
for (const { tag } of manifest.children) {
|
|
365
|
+
const known = this.owners.get(tag);
|
|
366
|
+
this.owners.set(tag, known === undefined ? manifest.tag : `${known}, ${manifest.tag}`);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
run(source: string): Analysis {
|
|
372
|
+
const outside: Where = { order: 0, page: null, element: null, tag: null, path: '' };
|
|
373
|
+
if (source.length > MAX_SOURCE_LENGTH) {
|
|
374
|
+
this.add(EngineIssue.SOURCE_TOO_LARGE, { max: MAX_SOURCE_LENGTH }, outside);
|
|
375
|
+
return this.result(null);
|
|
376
|
+
}
|
|
377
|
+
if (mostAttributes(source, MAX_ATTRIBUTES_PER_ELEMENT) > MAX_ATTRIBUTES_PER_ELEMENT) {
|
|
378
|
+
this.add(EngineIssue.SOURCE_TOO_COMPLEX, { max: MAX_ATTRIBUTES_PER_ELEMENT }, outside);
|
|
379
|
+
return this.result(null);
|
|
380
|
+
}
|
|
381
|
+
const parsed = parseSource(source);
|
|
382
|
+
if (parsed === null) {
|
|
383
|
+
this.add(EngineIssue.SOURCE_TOO_DEEP, { max: MAX_NESTING_DEPTH }, outside);
|
|
384
|
+
return this.result(null);
|
|
385
|
+
}
|
|
386
|
+
const roots = parsed.filter((n) => n.type === 'element' || !isHtmlWhitespace(n.value));
|
|
387
|
+
const raw = roots[0];
|
|
388
|
+
if (roots.length !== 1 || !isElement(raw, DOC_TAG)) {
|
|
389
|
+
this.add(EngineIssue.DOC_ROOT_INVALID, {}, outside);
|
|
390
|
+
return this.result(null);
|
|
391
|
+
}
|
|
392
|
+
const doc: Where = { order: this.order++, page: null, element: null, tag: null, path: DOC_TAG };
|
|
393
|
+
const format = own(raw.attrs, 'format');
|
|
394
|
+
if (format !== undefined && !isSupportedFormat(format)) {
|
|
395
|
+
this.add(EngineIssue.DOC_FORMAT_UNSUPPORTED, { format }, doc);
|
|
396
|
+
return this.result(null);
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
const content = this.readRules(raw);
|
|
400
|
+
const attrs: Record<string, string> = {};
|
|
401
|
+
const onRoot: Where = { ...doc, tag: DOC_TAG };
|
|
402
|
+
for (const [name, value] of Object.entries(raw.attrs)) {
|
|
403
|
+
if (name === 'style' || name === CLASS_ATTRIBUTE) continue;
|
|
404
|
+
if (!DOC_ATTRIBUTES.includes(name)) {
|
|
405
|
+
this.add(EngineIssue.ATTRIBUTE_REMOVED, { tag: DOC_TAG, attribute: name }, onRoot, 'warning');
|
|
406
|
+
} else if (name === 'lang') {
|
|
407
|
+
if (LANGUAGE_TAG_PATTERN.test(value)) attrs.lang = value;
|
|
408
|
+
else this.add(EngineIssue.DOC_LANG_INVALID, { lang: value }, doc);
|
|
409
|
+
} else if (name === 'dir') {
|
|
410
|
+
if (value === 'ltr' || value === 'rtl') attrs.dir = value;
|
|
411
|
+
else this.invalid(DOC_TAG, name, value, 'enum', ['ltr', 'rtl'], onRoot);
|
|
412
|
+
} else if (name === 'kind') {
|
|
413
|
+
if ((DOC_KINDS as readonly string[]).includes(value)) attrs.kind = value;
|
|
414
|
+
else this.invalid(DOC_TAG, name, value, 'enum', DOC_KINDS, onRoot);
|
|
415
|
+
} else {
|
|
416
|
+
attrs[name] = value;
|
|
417
|
+
if (name === STYLE_TEMPLATE_ATTRIBUTE) this.styleTemplate(value, DOC_TAG, 'document', onRoot);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
this.inline(attrs, raw.attrs, DOC_TAG, onRoot, this.page);
|
|
421
|
+
if (own(raw.attrs, 'lang') === undefined) this.add(EngineIssue.DOC_LANG_MISSING, {}, doc);
|
|
422
|
+
const lang = attrs.lang ?? 'und';
|
|
423
|
+
const dir: Direction = attrs.dir === 'ltr' || attrs.dir === 'rtl' ? attrs.dir : directionOf(lang);
|
|
424
|
+
const kind = (attrs.kind as DocKind | undefined) ?? DEFAULT_DOC_KIND;
|
|
425
|
+
|
|
426
|
+
const explicit = content.some((c) => isElement(c, PAGE_TAG));
|
|
427
|
+
const loose = content.some((c) =>
|
|
428
|
+
c.type === 'element' ? c.tag !== PAGE_TAG : !isHtmlWhitespace(c.value),
|
|
429
|
+
);
|
|
430
|
+
if (explicit && loose) this.add(EngineIssue.DOC_MIXED_PAGES, {}, doc);
|
|
431
|
+
this.explicitPages = explicit;
|
|
432
|
+
|
|
433
|
+
const base: Scope = {
|
|
434
|
+
parent: DOC_TAG,
|
|
435
|
+
inLink: false,
|
|
436
|
+
mode: 'flow',
|
|
437
|
+
host: null,
|
|
438
|
+
help: null,
|
|
439
|
+
page: null,
|
|
440
|
+
pageIndex: 0,
|
|
441
|
+
pageName: namePage(0, undefined),
|
|
442
|
+
lang,
|
|
443
|
+
dir,
|
|
444
|
+
...this.coloursBelow(attrs, this.page),
|
|
445
|
+
};
|
|
446
|
+
const body: ElementNode = { ...raw, children: content };
|
|
447
|
+
const pages: PageRecord[] = [];
|
|
448
|
+
const pageNodes = new Map<ElementNode, number>();
|
|
449
|
+
let children: Node[];
|
|
450
|
+
// The implicit page exists only in the view: what styles it is its rule, and what is on it sees that.
|
|
451
|
+
const implicit: Record<string, string> = {};
|
|
452
|
+
let scope = base;
|
|
453
|
+
if (!explicit) {
|
|
454
|
+
this.inline(implicit, {}, PAGE_TAG, { ...onRoot, tag: PAGE_TAG }, base);
|
|
455
|
+
scope = { ...base, ...this.coloursBelow(implicit, base) };
|
|
456
|
+
}
|
|
457
|
+
if (explicit) {
|
|
458
|
+
children = this.visitNodes(body, base, DOC_TAG, (el, place) => {
|
|
459
|
+
if (el.tag !== PAGE_TAG) return null;
|
|
460
|
+
const index = pages.length;
|
|
461
|
+
const page = this.visitPage(el, index, place, base);
|
|
462
|
+
const help = this.pageHelpOf(index);
|
|
463
|
+
pages.push({ sourceId: own(el.attrs, 'id') ?? null, label: own(el.attrs, 'label') ?? null, help });
|
|
464
|
+
pageNodes.set(page, index);
|
|
465
|
+
return [page];
|
|
466
|
+
});
|
|
467
|
+
} else {
|
|
468
|
+
children = this.visitNodes(body, scope, DOC_TAG);
|
|
469
|
+
pages.push({ sourceId: null, label: null, help: this.pageHelpOf(0) });
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
if (!explicit && !hasContent(children)) this.add(EngineIssue.DOC_EMPTY, {}, doc);
|
|
473
|
+
else if (this.componentCount === 0) this.add(EngineIssue.NO_COMPONENTS, {}, doc, 'warning');
|
|
474
|
+
for (const check of this.checks) check();
|
|
475
|
+
|
|
476
|
+
return this.result({
|
|
477
|
+
root: element(DOC_TAG, attrs, children),
|
|
478
|
+
lang,
|
|
479
|
+
dir,
|
|
480
|
+
kind,
|
|
481
|
+
explicitPages: explicit,
|
|
482
|
+
pages,
|
|
483
|
+
pageStyle: own(implicit, 'style') ?? null,
|
|
484
|
+
pageNodes,
|
|
485
|
+
components: this.components,
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
private result(doc: DocRecord | null): Analysis {
|
|
490
|
+
// Array sort is stable, so issues of one node keep the order they were found in.
|
|
491
|
+
const issues = [...this.found].sort((a, b) => a.order - b.order).map((f) => f.issue);
|
|
492
|
+
return { ok: this.errors === 0, issues, doc };
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
private add(
|
|
496
|
+
code: string,
|
|
497
|
+
params: IssueParams,
|
|
498
|
+
where: Where,
|
|
499
|
+
severity: Severity = 'error',
|
|
500
|
+
namespace: string = ENGINE_NAMESPACE,
|
|
501
|
+
): void {
|
|
502
|
+
if (severity === 'error') this.errors++;
|
|
503
|
+
const at = { page: where.page, element: where.element, tag: where.tag, path: where.path };
|
|
504
|
+
this.found.push({ order: where.order, issue: { code, severity, namespace, params, at } });
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
private where(scope: Scope, place: Place, tag: string): Where {
|
|
508
|
+
const element = scope.host?.id ?? scope.help?.element ?? null;
|
|
509
|
+
return { order: place.order, page: scope.page, element, tag, path: place.path };
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
/** Visits the children of `parent`; `special` may take over an element (returning null falls back). */
|
|
513
|
+
private visitNodes(parent: ElementNode, scope: Scope, parentPath: string, special?: Visitor): Node[] {
|
|
514
|
+
const out: Node[] = [];
|
|
515
|
+
const counts = new Map<string, number>();
|
|
516
|
+
for (const child of parent.children) {
|
|
517
|
+
const order = this.order++;
|
|
518
|
+
if (child.type === 'text') {
|
|
519
|
+
appendText(out, child.value);
|
|
520
|
+
continue;
|
|
521
|
+
}
|
|
522
|
+
const index = (counts.get(child.tag) ?? 0) + 1;
|
|
523
|
+
counts.set(child.tag, index);
|
|
524
|
+
const place: Place = { path: `${parentPath} > ${segment(child, index)}`, order, index };
|
|
525
|
+
for (const node of special?.(child, place) ?? this.visitElement(child, scope, place)) {
|
|
526
|
+
if (node.type === 'text') appendText(out, node.value);
|
|
527
|
+
else out.push(node);
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
return out;
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
/**
|
|
534
|
+
* The rules: the `<astl-style>`s that come first in the document. Returns what follows them; one anywhere else is
|
|
535
|
+
* misplaced, and reported where it is met.
|
|
536
|
+
*/
|
|
537
|
+
private readRules(raw: ElementNode): readonly Node[] {
|
|
538
|
+
let rules = 0;
|
|
539
|
+
let after = 0;
|
|
540
|
+
for (const [at, child] of raw.children.entries()) {
|
|
541
|
+
if (child.type === 'text') {
|
|
542
|
+
if (isHtmlWhitespace(child.value)) continue;
|
|
543
|
+
break;
|
|
544
|
+
}
|
|
545
|
+
if (child.tag !== STYLE_TAG) break;
|
|
546
|
+
rules++;
|
|
547
|
+
after = at + 1;
|
|
548
|
+
const path = `${DOC_TAG} > ${STYLE_TAG}[${rules}]`;
|
|
549
|
+
const where: Where = { order: this.order++, page: null, element: null, tag: STYLE_TAG, path };
|
|
550
|
+
if (rules <= MAX_STYLE_RULES) this.readRule(child, where);
|
|
551
|
+
else if (rules === MAX_STYLE_RULES + 1)
|
|
552
|
+
this.add(EngineIssue.STYLE_RULES_TOO_MANY, { max: MAX_STYLE_RULES }, where);
|
|
553
|
+
}
|
|
554
|
+
return raw.children.slice(after);
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
/** One rule: `name` and `label`, or `for`. Holding anything is the trap of `<astl-style ... />`, which HTML leaves open. */
|
|
558
|
+
private readRule(el: ElementNode, where: Where): void {
|
|
559
|
+
for (const attribute of Object.keys(el.attrs)) {
|
|
560
|
+
if (!RULE_ATTRIBUTES.includes(attribute)) {
|
|
561
|
+
this.add(EngineIssue.ATTRIBUTE_REMOVED, { tag: STYLE_TAG, attribute }, where, 'warning');
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
if (hasContent(el.children)) {
|
|
565
|
+
const inside = el.children.find((n) => n.type === 'element');
|
|
566
|
+
const child = inside === undefined ? 'text' : inside.tag;
|
|
567
|
+
this.add(EngineIssue.CHILD_UNEXPECTED, { tag: STYLE_TAG, child }, where);
|
|
568
|
+
}
|
|
569
|
+
const declarations = this.cleaned(own(el.attrs, 'style') ?? '', STYLE_TAG, where);
|
|
570
|
+
const name = own(el.attrs, 'name');
|
|
571
|
+
const target = own(el.attrs, 'for');
|
|
572
|
+
if (name !== undefined && target === undefined) {
|
|
573
|
+
if (isStyleName(name) && !this.namedStyles.has(name)) this.namedStyles.set(name, declarations);
|
|
574
|
+
else this.add(EngineIssue.STYLE_NAME_INVALID, { name }, where);
|
|
575
|
+
if ((own(el.attrs, 'label') ?? '').trim() === '') {
|
|
576
|
+
this.add(EngineIssue.ATTRIBUTE_REQUIRED, { tag: STYLE_TAG, attribute: 'label' }, where);
|
|
577
|
+
}
|
|
578
|
+
} else if (target !== undefined && name === undefined) {
|
|
579
|
+
if (this.isRuleTarget(target) && !this.tagStyles.has(target)) this.tagStyles.set(target, declarations);
|
|
580
|
+
else this.add(EngineIssue.STYLE_NAME_INVALID, { name: target }, where);
|
|
581
|
+
} else {
|
|
582
|
+
this.add(EngineIssue.STYLE_NAME_INVALID, { name: name ?? '' }, where);
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
private isRuleTarget(tag: string): boolean {
|
|
587
|
+
return (
|
|
588
|
+
BLOCK_TAGS.has(tag) ||
|
|
589
|
+
INLINE_TAGS.has(tag) ||
|
|
590
|
+
RULE_TARGETS.has(tag) ||
|
|
591
|
+
this.options.definitions.has(tag) ||
|
|
592
|
+
this.owners.has(tag)
|
|
593
|
+
);
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
/** A `style` as written through the allow-list; what it refuses is reported. */
|
|
597
|
+
private cleaned(value: string, tag: string, where: Where): readonly StyleDeclaration[] {
|
|
598
|
+
const { declarations, removed } = cleanStyle(value);
|
|
599
|
+
if (value.length > MAX_STYLE_LENGTH) {
|
|
600
|
+
this.add(EngineIssue.STYLE_TOO_LONG, { tag, max: MAX_STYLE_LENGTH }, where, 'warning');
|
|
601
|
+
} else {
|
|
602
|
+
for (const property of removed)
|
|
603
|
+
this.add(EngineIssue.STYLE_REMOVED, { tag, property }, where, 'warning');
|
|
604
|
+
}
|
|
605
|
+
return declarations;
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
/**
|
|
609
|
+
* Writes an element's `style` into `attrs`, as everything after the analyser sees it: its tag's rule, then its
|
|
610
|
+
* classes in order, then its own `style`, per property. `class` ends here: no view holds it.
|
|
611
|
+
*/
|
|
612
|
+
private inline(
|
|
613
|
+
attrs: Record<string, string>,
|
|
614
|
+
raw: Readonly<Record<string, string>>,
|
|
615
|
+
tag: string,
|
|
616
|
+
where: Where,
|
|
617
|
+
around: Colours,
|
|
618
|
+
): void {
|
|
619
|
+
if (this.inHelp) {
|
|
620
|
+
// Help shows in the player's own boxes, a feedback text on its verdict's colour: an author's colours, or a
|
|
621
|
+
// document rule meant for the page, could make it unreadable there, and no contrast check sees that ground.
|
|
622
|
+
for (const name of ['style', CLASS_ATTRIBUTE]) {
|
|
623
|
+
if (own(raw, name) !== undefined) {
|
|
624
|
+
this.add(EngineIssue.ATTRIBUTE_REMOVED, { tag, attribute: name }, where, 'warning');
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
return;
|
|
628
|
+
}
|
|
629
|
+
const written = own(raw, 'style');
|
|
630
|
+
const classes = own(raw, CLASS_ATTRIBUTE);
|
|
631
|
+
const rule = this.tagStyles.get(tag);
|
|
632
|
+
if (written === undefined && classes === undefined && rule === undefined) return;
|
|
633
|
+
const lists: (readonly StyleDeclaration[])[] = rule === undefined ? [] : [rule];
|
|
634
|
+
for (const name of classes?.split(/\s+/) ?? []) {
|
|
635
|
+
if (name === '') continue;
|
|
636
|
+
const named = this.namedStyles.get(name);
|
|
637
|
+
if (named === undefined) this.add(EngineIssue.STYLE_UNKNOWN, { tag, class: name }, where, 'warning');
|
|
638
|
+
else lists.push(named);
|
|
639
|
+
}
|
|
640
|
+
if (written !== undefined) lists.push(this.cleaned(written, tag, where));
|
|
641
|
+
const declarations = mergeStyles(...lists);
|
|
642
|
+
if (declarations.length === 0) return;
|
|
643
|
+
const style = styleText(declarations);
|
|
644
|
+
const before = this.inlined;
|
|
645
|
+
this.inlined += style.length;
|
|
646
|
+
if (this.inlined > MAX_INLINED_STYLES) {
|
|
647
|
+
if (before <= MAX_INLINED_STYLES) {
|
|
648
|
+
this.add(EngineIssue.STYLE_TOO_LARGE, { max: MAX_INLINED_STYLES }, where);
|
|
649
|
+
}
|
|
650
|
+
return;
|
|
651
|
+
}
|
|
652
|
+
attrs.style = style;
|
|
653
|
+
this.checkContrast(declarations, attrs, tag, where, around);
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
/** STYLE_LOW_CONTRAST: on an element that declares a colour, as what it declares is seen, once per pair of colours. */
|
|
657
|
+
private checkContrast(
|
|
658
|
+
declarations: readonly StyleDeclaration[],
|
|
659
|
+
attrs: Readonly<Record<string, string>>,
|
|
660
|
+
tag: string,
|
|
661
|
+
where: Where,
|
|
662
|
+
around: Colours,
|
|
663
|
+
): void {
|
|
664
|
+
if (tag === 'img') return;
|
|
665
|
+
if (!declarations.some(([name]) => name === 'color' || name === 'background-color')) return;
|
|
666
|
+
const { ink, ground } = coloursOf(attrs, around);
|
|
667
|
+
if (ink === null || ground === null) return;
|
|
668
|
+
const seen = overStyleColor(ink, ground);
|
|
669
|
+
const ratio = styleContrast(seen, ground);
|
|
670
|
+
if (ratio >= MIN_CONTRAST) return;
|
|
671
|
+
const pair = `${seen.map(Math.round).join()}/${ground.map(Math.round).join()}`;
|
|
672
|
+
if (this.lowContrast.has(pair)) return;
|
|
673
|
+
this.lowContrast.add(pair);
|
|
674
|
+
const params = { tag, ratio: Math.round(ratio * 100) / 100, min: MIN_CONTRAST };
|
|
675
|
+
this.add(EngineIssue.STYLE_LOW_CONTRAST, params, where, 'warning');
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
/** The colours content inside an element is compared with; without the page's, each element stands alone. */
|
|
679
|
+
private coloursBelow(attrs: Readonly<Record<string, string>>, around: Colours): Colours {
|
|
680
|
+
return this.tracksColours ? coloursOf(attrs, around) : NO_COLOURS;
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
/** What content inside an element inherits from it. */
|
|
684
|
+
private below(
|
|
685
|
+
attrs: Readonly<Record<string, string>>,
|
|
686
|
+
scope: Scope,
|
|
687
|
+
): Pick<Scope, 'lang' | 'dir' | 'ink' | 'ground'> {
|
|
688
|
+
return { ...languageOf(attrs, scope), ...this.coloursBelow(attrs, scope) };
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
private visitElement(el: ElementNode, scope: Scope, place: Place): Node[] {
|
|
692
|
+
const tag = el.tag;
|
|
693
|
+
const where = this.where(scope, place, tag);
|
|
694
|
+
if (tag === DOC_TAG) {
|
|
695
|
+
this.add(EngineIssue.PLACEMENT_INVALID, { tag }, where);
|
|
696
|
+
return [];
|
|
697
|
+
}
|
|
698
|
+
if (tag === PAGE_TAG) {
|
|
699
|
+
// Keep the content so it is still validated, in the enclosing page.
|
|
700
|
+
this.add(EngineIssue.PAGE_NESTED, scope.pageName, where);
|
|
701
|
+
return this.visitNodes(el, scope, place.path);
|
|
702
|
+
}
|
|
703
|
+
if (HELP_TAGS.has(tag)) {
|
|
704
|
+
const host = scope.host;
|
|
705
|
+
const own = host !== null && host.manifest.content === 'flow' && scope.parent === host.manifest.tag;
|
|
706
|
+
const onPage =
|
|
707
|
+
host === null &&
|
|
708
|
+
scope.help === null &&
|
|
709
|
+
(scope.parent === PAGE_TAG || (scope.parent === DOC_TAG && !this.explicitPages));
|
|
710
|
+
if (own) this.visitHelp(el, scope, place, where, host.help, host);
|
|
711
|
+
else if (onPage) this.visitHelp(el, scope, place, where, this.pageHelpParts(scope.pageIndex), null);
|
|
712
|
+
// Misplaced help goes with its content: it may hold the answer, and must not reach the view.
|
|
713
|
+
else this.add(EngineIssue.PLACEMENT_INVALID, { tag }, where);
|
|
714
|
+
return [];
|
|
715
|
+
}
|
|
716
|
+
const definition = this.options.definitions.get(tag);
|
|
717
|
+
if (definition !== undefined) {
|
|
718
|
+
const parent = scope.host?.manifest.tag ?? scope.help?.tag;
|
|
719
|
+
if (parent !== undefined) {
|
|
720
|
+
this.add(EngineIssue.COMPONENT_NESTED, { tag, parent }, where);
|
|
721
|
+
return [];
|
|
722
|
+
}
|
|
723
|
+
return [this.visitComponent(el, definition, scope, place)];
|
|
724
|
+
}
|
|
725
|
+
if (REMOVED_WITH_CONTENT.has(tag)) {
|
|
726
|
+
this.add(EngineIssue.ELEMENT_REMOVED, { tag }, where, 'warning');
|
|
727
|
+
return [];
|
|
728
|
+
}
|
|
729
|
+
if (BLOCK_TAGS.has(tag) || INLINE_TAGS.has(tag)) {
|
|
730
|
+
if (scope.mode === 'text' || (scope.mode === 'inline' && BLOCK_TAGS.has(tag))) {
|
|
731
|
+
this.add(EngineIssue.ELEMENT_REMOVED, { tag }, where, 'warning');
|
|
732
|
+
return this.visitNodes(el, scope, place.path);
|
|
733
|
+
}
|
|
734
|
+
return [this.visitRichText(el, scope, place, where)];
|
|
735
|
+
}
|
|
736
|
+
// A figure is a block: in a prompt or an option it is unwrapped like the other blocks, below.
|
|
737
|
+
if (tag === FIGURE_TAG && scope.mode === 'flow') return [this.visitFigure(el, scope, place, where)];
|
|
738
|
+
if (tag.includes('-')) {
|
|
739
|
+
const host = scope.host;
|
|
740
|
+
const spec = host?.manifest.children.find((c) => c.tag === tag);
|
|
741
|
+
// A child that leads a rich block stands before its content (visitLeadChild), never inside it.
|
|
742
|
+
if (host && spec?.place === 'lead' && host.manifest.content === 'flow') {
|
|
743
|
+
this.add(EngineIssue.PLACEMENT_INVALID, { tag }, where);
|
|
744
|
+
return [];
|
|
745
|
+
}
|
|
746
|
+
// The inline children of a rich block sit in its text, where an inline component may (not inside another).
|
|
747
|
+
if (host && spec && host.manifest.content === 'flow' && scope.mode === 'flow') {
|
|
748
|
+
if (scope.inLink || !TEXT_PARENTS.has(scope.parent))
|
|
749
|
+
this.add(EngineIssue.PLACEMENT_INVALID, { tag }, where);
|
|
750
|
+
const node = this.visitDeclaredChild(el, spec, scope, place, where, host.localIds);
|
|
751
|
+
host.places.set(node, where);
|
|
752
|
+
host.children.push(node);
|
|
753
|
+
return [node];
|
|
754
|
+
}
|
|
755
|
+
if (tag === STYLE_TAG) {
|
|
756
|
+
this.add(EngineIssue.PLACEMENT_INVALID, { tag }, where);
|
|
757
|
+
return [];
|
|
758
|
+
}
|
|
759
|
+
if (tag === COLUMNS_TAG) return [this.visitColumns(el, scope, place, where)];
|
|
760
|
+
// A column without its layout (`astl-match` has columns of its own, which it reads itself). The content stays,
|
|
761
|
+
// to be validated.
|
|
762
|
+
if (tag === COLUMN_TAG && !(host && spec)) {
|
|
763
|
+
this.add(EngineIssue.COLUMNS_INVALID, { tag }, where);
|
|
764
|
+
return this.visitNodes(el, scope, place.path);
|
|
765
|
+
}
|
|
766
|
+
const owners = this.owners.get(tag);
|
|
767
|
+
if (host && spec) this.add(EngineIssue.CHILD_UNEXPECTED, { tag: host.manifest.tag, child: tag }, where);
|
|
768
|
+
else if (owners !== undefined)
|
|
769
|
+
this.add(EngineIssue.CHILD_OUTSIDE, { child: tag, parents: owners }, where);
|
|
770
|
+
else this.add(EngineIssue.ELEMENT_UNKNOWN, { tag }, where);
|
|
771
|
+
return [];
|
|
772
|
+
}
|
|
773
|
+
this.add(EngineIssue.ELEMENT_REMOVED, { tag }, where, 'warning');
|
|
774
|
+
return this.visitNodes(el, scope, place.path);
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
private visitRichText(el: ElementNode, scope: Scope, place: Place, where: Where): ElementNode {
|
|
778
|
+
const attrs = this.richAttributes(el, where, scope);
|
|
779
|
+
const inner: Scope = {
|
|
780
|
+
...scope,
|
|
781
|
+
...this.below(attrs, scope),
|
|
782
|
+
parent: el.tag,
|
|
783
|
+
inLink: scope.inLink || el.tag === 'a',
|
|
784
|
+
};
|
|
785
|
+
return element(el.tag, attrs, this.visitNodes(el, inner, place.path));
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
/**
|
|
789
|
+
* A layout: 2 to 4 columns, directly on a page or in the document, outside components. Whatever its shape, its
|
|
790
|
+
* content is still validated, each piece as what it is.
|
|
791
|
+
*/
|
|
792
|
+
private visitColumns(el: ElementNode, scope: Scope, place: Place, where: Where): ElementNode {
|
|
793
|
+
const attrs = this.richAttributes(el, where, scope);
|
|
794
|
+
const inner: Scope = { ...scope, ...this.below(attrs, scope), parent: COLUMNS_TAG };
|
|
795
|
+
let columns = 0;
|
|
796
|
+
let stray = false;
|
|
797
|
+
const children = this.visitNodes(el, inner, place.path, (child, childPlace) => {
|
|
798
|
+
if (child.tag !== COLUMN_TAG) {
|
|
799
|
+
stray = true;
|
|
800
|
+
return this.visitElement(child, scope, childPlace);
|
|
801
|
+
}
|
|
802
|
+
columns++;
|
|
803
|
+
const column = this.richAttributes(child, this.where(inner, childPlace, COLUMN_TAG), inner);
|
|
804
|
+
const content: Scope = { ...inner, ...this.below(column, inner), parent: COLUMN_TAG };
|
|
805
|
+
return [element(COLUMN_TAG, column, this.visitNodes(child, content, childPlace.path))];
|
|
806
|
+
});
|
|
807
|
+
if (children.some((n) => n.type === 'text' && !isHtmlWhitespace(n.value))) stray = true;
|
|
808
|
+
const placed = scope.host === null && (scope.parent === PAGE_TAG || scope.parent === DOC_TAG);
|
|
809
|
+
if (!placed || stray || columns < MIN_COLUMNS || columns > MAX_COLUMNS) {
|
|
810
|
+
this.add(EngineIssue.COLUMNS_INVALID, { tag: COLUMNS_TAG }, where);
|
|
811
|
+
}
|
|
812
|
+
return element(
|
|
813
|
+
COLUMNS_TAG,
|
|
814
|
+
attrs,
|
|
815
|
+
children.filter((n) => n.type === 'element'),
|
|
816
|
+
);
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
/**
|
|
820
|
+
* The attributes a rich-text element keeps: `lang`, `dir` and its `style` after the rules, plus its own
|
|
821
|
+
* (TAG_ATTRIBUTES). A picture also needs a `src` the URL policy accepts (else an error: a picture without one cannot
|
|
822
|
+
* be shown) and an `alt`.
|
|
823
|
+
*/
|
|
824
|
+
private richAttributes(el: ElementNode, where: Where, around: Colours): Record<string, string> {
|
|
825
|
+
const attrs: Record<string, string> = {};
|
|
826
|
+
const extra = Object.hasOwn(TAG_ATTRIBUTES, el.tag) ? (TAG_ATTRIBUTES[el.tag] ?? []) : [];
|
|
827
|
+
const picture = el.tag === 'img';
|
|
828
|
+
for (const [name, value] of Object.entries(el.attrs)) {
|
|
829
|
+
if (name === 'style' || name === CLASS_ATTRIBUTE) continue;
|
|
830
|
+
if (CONTENT_ATTRIBUTES.has(name)) {
|
|
831
|
+
if (this.globalAttribute(name, value, el.tag, where)) attrs[name] = value;
|
|
832
|
+
} else if (!extra.includes(name)) {
|
|
833
|
+
this.add(EngineIssue.ATTRIBUTE_REMOVED, { tag: el.tag, attribute: name }, where, 'warning');
|
|
834
|
+
} else if (name === URL_ATTRIBUTES[el.tag]) {
|
|
835
|
+
// A blank picture address is a missing one (reported below).
|
|
836
|
+
if (picture && value.trim() === '') continue;
|
|
837
|
+
if (this.allowsUrl(value, el.tag, name, where, picture)) attrs[name] = value;
|
|
838
|
+
} else if (name === 'start' && parseIntegerValue(value) === null) {
|
|
839
|
+
this.invalid(el.tag, name, value, 'integer', [], where);
|
|
840
|
+
} else if ((name === 'width' || name === 'height') && !((parseIntegerValue(value) ?? 0) > 0)) {
|
|
841
|
+
this.invalid(el.tag, name, value, 'integer', [], where);
|
|
842
|
+
} else {
|
|
843
|
+
attrs[name] = value;
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
this.inline(attrs, el.attrs, el.tag, where, around);
|
|
847
|
+
if (picture) {
|
|
848
|
+
if ((own(el.attrs, 'src') ?? '').trim() === '') {
|
|
849
|
+
this.add(EngineIssue.ATTRIBUTE_REQUIRED, { tag: el.tag, attribute: 'src' }, where);
|
|
850
|
+
}
|
|
851
|
+
if (own(el.attrs, 'alt') === undefined) this.add(EngineIssue.IMG_ALT_MISSING, {}, where);
|
|
852
|
+
}
|
|
853
|
+
return attrs;
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
/**
|
|
857
|
+
* 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
|
|
858
|
+
* one caption. A caption holds rich inline text, which may hold inline components like any text.
|
|
859
|
+
*/
|
|
860
|
+
private visitFigure(el: ElementNode, scope: Scope, place: Place, where: Where): ElementNode {
|
|
861
|
+
if (
|
|
862
|
+
scope.parent !== PAGE_TAG &&
|
|
863
|
+
scope.parent !== DOC_TAG &&
|
|
864
|
+
scope.parent !== COLUMN_TAG &&
|
|
865
|
+
scope.parent !== scope.host?.manifest.tag &&
|
|
866
|
+
!HELP_TAGS.has(scope.parent)
|
|
867
|
+
) {
|
|
868
|
+
this.add(EngineIssue.PLACEMENT_INVALID, { tag: el.tag }, where);
|
|
869
|
+
}
|
|
870
|
+
const attrs = this.richAttributes(el, where, scope);
|
|
871
|
+
const inner: Scope = { ...scope, ...this.below(attrs, scope), parent: FIGURE_TAG };
|
|
872
|
+
let pictures = 0;
|
|
873
|
+
let captions = 0;
|
|
874
|
+
let other = false;
|
|
875
|
+
const children = this.visitNodes(el, inner, place.path, (child, childPlace) => {
|
|
876
|
+
if (child.tag === 'img') pictures++;
|
|
877
|
+
else if (child.tag === CAPTION_TAG) {
|
|
878
|
+
captions++;
|
|
879
|
+
const caption = this.richAttributes(child, this.where(inner, childPlace, child.tag), inner);
|
|
880
|
+
const text: Scope = { ...inner, ...this.below(caption, inner), parent: CAPTION_TAG, mode: 'inline' };
|
|
881
|
+
return [element(CAPTION_TAG, caption, this.visitNodes(child, text, childPlace.path))];
|
|
882
|
+
} else other = true;
|
|
883
|
+
return null;
|
|
884
|
+
});
|
|
885
|
+
if (children.some((n) => n.type === 'text' && !isHtmlWhitespace(n.value))) other = true;
|
|
886
|
+
if (pictures !== 1 || captions > 1 || other) this.add(EngineIssue.FIGURE_INVALID, {}, where);
|
|
887
|
+
return element(FIGURE_TAG, attrs, children);
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
private visitPage(el: ElementNode, index: number, place: Place, base: Scope): ElementNode {
|
|
891
|
+
const id = own(el.attrs, 'id');
|
|
892
|
+
const where: Where = {
|
|
893
|
+
order: place.order,
|
|
894
|
+
page: id ?? null,
|
|
895
|
+
element: null,
|
|
896
|
+
tag: PAGE_TAG,
|
|
897
|
+
path: place.path,
|
|
898
|
+
};
|
|
899
|
+
const attrs: Record<string, string> = {};
|
|
900
|
+
for (const [name, value] of Object.entries(el.attrs)) {
|
|
901
|
+
if (name === 'style' || name === CLASS_ATTRIBUTE) continue;
|
|
902
|
+
if (!PAGE_ATTRIBUTES.includes(name)) {
|
|
903
|
+
this.add(EngineIssue.ATTRIBUTE_REMOVED, { tag: PAGE_TAG, attribute: name }, where, 'warning');
|
|
904
|
+
} else if (CONTENT_ATTRIBUTES.has(name)) {
|
|
905
|
+
if (this.globalAttribute(name, value, PAGE_TAG, where)) attrs[name] = value;
|
|
906
|
+
} else {
|
|
907
|
+
attrs[name] = value;
|
|
908
|
+
if (name === 'id') this.sourceId(value, PAGE_TAG, where);
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
this.inline(attrs, el.attrs, PAGE_TAG, where, base);
|
|
912
|
+
if (id === undefined) this.add(EngineIssue.ID_MISSING, { tag: PAGE_TAG }, where);
|
|
913
|
+
const scope: Scope = {
|
|
914
|
+
...base,
|
|
915
|
+
...this.below(attrs, base),
|
|
916
|
+
parent: PAGE_TAG,
|
|
917
|
+
page: id ?? null,
|
|
918
|
+
pageIndex: index,
|
|
919
|
+
pageName: namePage(index + 1, own(el.attrs, 'label')),
|
|
920
|
+
};
|
|
921
|
+
const children = this.visitNodes(el, scope, place.path);
|
|
922
|
+
if (isBlank(children)) this.add(EngineIssue.PAGE_EMPTY, scope.pageName, where, 'warning');
|
|
923
|
+
return element(PAGE_TAG, attrs, children);
|
|
924
|
+
}
|
|
925
|
+
|
|
926
|
+
private visitComponent(
|
|
927
|
+
raw: ElementNode,
|
|
928
|
+
definition: ComponentDefinition,
|
|
929
|
+
scope: Scope,
|
|
930
|
+
place: Place,
|
|
931
|
+
): ElementNode {
|
|
932
|
+
const manifest = definition.manifest;
|
|
933
|
+
const tag = manifest.tag;
|
|
934
|
+
const errorsBefore = this.errors;
|
|
935
|
+
const id = own(raw.attrs, 'id');
|
|
936
|
+
const where: Where = { order: place.order, page: scope.page, element: id ?? null, tag, path: place.path };
|
|
937
|
+
this.componentCount++;
|
|
938
|
+
if (manifest.graded) this.gradedPages.add(scope.pageIndex);
|
|
939
|
+
if (!placementAllowed(manifest.display, scope)) this.add(EngineIssue.PLACEMENT_INVALID, { tag }, where);
|
|
940
|
+
|
|
941
|
+
const el = this.upgrade(raw, definition, where);
|
|
942
|
+
const specs = allAttributes(manifest);
|
|
943
|
+
const attrs: Record<string, string> = {};
|
|
944
|
+
for (const [name, value] of Object.entries(el.attrs)) {
|
|
945
|
+
if (name === 'style' || name === CLASS_ATTRIBUTE) continue;
|
|
946
|
+
if (CONTENT_ATTRIBUTES.has(name)) {
|
|
947
|
+
if (this.globalAttribute(name, value, tag, where)) attrs[name] = value;
|
|
948
|
+
continue;
|
|
949
|
+
}
|
|
950
|
+
const spec = specs.find((s) => s.name === name);
|
|
951
|
+
if (spec === undefined || spec.visibility === 'derived') {
|
|
952
|
+
this.add(EngineIssue.ATTRIBUTE_REMOVED, { tag, attribute: name }, where, 'warning');
|
|
953
|
+
continue;
|
|
954
|
+
}
|
|
955
|
+
if (spec.type === 'url') {
|
|
956
|
+
if (this.allowsUrl(value, tag, name, where)) attrs[name] = value;
|
|
957
|
+
continue;
|
|
958
|
+
}
|
|
959
|
+
attrs[name] = value;
|
|
960
|
+
if (name === 'id') {
|
|
961
|
+
this.sourceId(value, tag, where);
|
|
962
|
+
} else if (name === 'scoring' && manifest.scoring !== null) {
|
|
963
|
+
const modes = manifest.scoring.modes;
|
|
964
|
+
if (!Object.hasOwn(modes, value)) {
|
|
965
|
+
const list = Object.keys(modes).join(', ');
|
|
966
|
+
this.add(EngineIssue.SCORING_MODE_INVALID, { tag, mode: value, modes: list }, where);
|
|
967
|
+
}
|
|
968
|
+
} else if (name === 'points' && manifest.scoring !== null) {
|
|
969
|
+
const points = parseDecimalValue(value);
|
|
970
|
+
if (points === null || outOfRange(points, spec))
|
|
971
|
+
this.add(EngineIssue.POINTS_INVALID, { value }, where);
|
|
972
|
+
} else if (name === STYLE_TEMPLATE_ATTRIBUTE) {
|
|
973
|
+
this.styleTemplate(value, tag, 'component', where);
|
|
974
|
+
} else {
|
|
975
|
+
this.checkValue(spec, value, tag, where);
|
|
976
|
+
}
|
|
977
|
+
}
|
|
978
|
+
this.inline(attrs, el.attrs, tag, where, scope);
|
|
979
|
+
this.checkRequired(specs, attrs, tag, where);
|
|
980
|
+
|
|
981
|
+
const language = languageOf(attrs, scope);
|
|
982
|
+
const childPlaces = new Map<ElementNode, Where>();
|
|
983
|
+
// A Set, so a source with thousands of children cannot make the duplicate check quadratic.
|
|
984
|
+
const localIds = new Set<string>();
|
|
985
|
+
const host: Host = {
|
|
986
|
+
manifest,
|
|
987
|
+
id: id ?? null,
|
|
988
|
+
children: [],
|
|
989
|
+
places: childPlaces,
|
|
990
|
+
localIds,
|
|
991
|
+
help: noHelp(),
|
|
992
|
+
};
|
|
993
|
+
const inner: Scope = { ...scope, ...this.below(attrs, scope), parent: tag, host };
|
|
994
|
+
// A rich block's content is rich text (its inline children are collected on the way); others hold children only.
|
|
995
|
+
let children: Node[];
|
|
996
|
+
if (manifest.content === 'flow') {
|
|
997
|
+
children = this.visitNodes(el, { ...inner, mode: 'flow' }, place.path, (child, childPlace) =>
|
|
998
|
+
this.visitLeadChild(el, child, manifest, inner, childPlace),
|
|
999
|
+
);
|
|
1000
|
+
} else {
|
|
1001
|
+
const own = this.visitComponentChildren(el, manifest, inner, place, where, childPlaces, localIds);
|
|
1002
|
+
host.children.push(...own);
|
|
1003
|
+
children = own;
|
|
1004
|
+
}
|
|
1005
|
+
for (const spec of manifest.children) {
|
|
1006
|
+
const count = host.children.filter((c) => c.tag === spec.tag).length;
|
|
1007
|
+
if (count < spec.min || (spec.max !== null && count > spec.max)) {
|
|
1008
|
+
const params = { tag, child: spec.tag, min: spec.min, max: spec.max ?? -1, count };
|
|
1009
|
+
this.add(EngineIssue.CHILD_COUNT, params, where);
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
|
|
1013
|
+
const node = element(tag, attrs, children);
|
|
1014
|
+
if (this.errors === errorsBefore) {
|
|
1015
|
+
const page = scope.pageIndex;
|
|
1016
|
+
this.checks.push(() => this.runDefinition(definition, node, language.lang, where, childPlaces, page));
|
|
1017
|
+
}
|
|
1018
|
+
if (id !== undefined) {
|
|
1019
|
+
const record: ComponentRecord = {
|
|
1020
|
+
definition,
|
|
1021
|
+
element: node,
|
|
1022
|
+
sourceId: id,
|
|
1023
|
+
page: scope.pageIndex,
|
|
1024
|
+
...language,
|
|
1025
|
+
localIds: [...localIds],
|
|
1026
|
+
help: helpRecord(host.help),
|
|
1027
|
+
};
|
|
1028
|
+
this.components.set(node, record);
|
|
1029
|
+
if (!this.bySource.has(id)) this.bySource.set(id, record);
|
|
1030
|
+
}
|
|
1031
|
+
return node;
|
|
1032
|
+
}
|
|
1033
|
+
|
|
1034
|
+
/** Applies `v` handling: newer than the manifest is an error, older runs the definition's migrations. */
|
|
1035
|
+
private upgrade(raw: ElementNode, definition: ComponentDefinition, where: Where): ElementNode {
|
|
1036
|
+
const manifest = definition.manifest;
|
|
1037
|
+
const written = own(raw.attrs, 'v');
|
|
1038
|
+
const version = written === undefined ? null : parseIntegerValue(written);
|
|
1039
|
+
// Invalid values are reported by the attribute checks.
|
|
1040
|
+
if (version === null || version < 1 || version === manifest.version) return raw;
|
|
1041
|
+
if (version > manifest.version) {
|
|
1042
|
+
this.add(EngineIssue.VERSION_UNSUPPORTED, { tag: manifest.tag, version }, where);
|
|
1043
|
+
return raw;
|
|
1044
|
+
}
|
|
1045
|
+
let el = raw;
|
|
1046
|
+
for (let from = version; from < manifest.version; from++) {
|
|
1047
|
+
const step = definition.migrations?.[from];
|
|
1048
|
+
if (step === undefined) {
|
|
1049
|
+
this.add(EngineIssue.VERSION_UNSUPPORTED, { tag: manifest.tag, version }, where);
|
|
1050
|
+
return raw;
|
|
1051
|
+
}
|
|
1052
|
+
el = step(el);
|
|
1053
|
+
}
|
|
1054
|
+
return { ...el, attrs: { ...el.attrs, v: String(manifest.version) } };
|
|
1055
|
+
}
|
|
1056
|
+
|
|
1057
|
+
/**
|
|
1058
|
+
* A rich block's own children that lead it (`place: 'lead'`, a prompt): visited as declared children when they come
|
|
1059
|
+
* before any content (white space aside); one after content is misplaced. Other children fall back (null).
|
|
1060
|
+
*/
|
|
1061
|
+
private visitLeadChild(
|
|
1062
|
+
block: ElementNode,
|
|
1063
|
+
child: ElementNode,
|
|
1064
|
+
manifest: ComponentManifest,
|
|
1065
|
+
scope: Scope,
|
|
1066
|
+
place: Place,
|
|
1067
|
+
): Node[] | null {
|
|
1068
|
+
const spec = manifest.children.find((c) => c.tag === child.tag);
|
|
1069
|
+
const host = scope.host;
|
|
1070
|
+
if (spec?.place !== 'lead' || host === null) return null;
|
|
1071
|
+
const where = this.where(scope, place, child.tag);
|
|
1072
|
+
const at = block.children.indexOf(child);
|
|
1073
|
+
const leads = block.children
|
|
1074
|
+
.slice(0, at)
|
|
1075
|
+
.every((n) =>
|
|
1076
|
+
n.type === 'text'
|
|
1077
|
+
? isHtmlWhitespace(n.value)
|
|
1078
|
+
: manifest.children.some((c) => c.tag === n.tag && c.place === 'lead'),
|
|
1079
|
+
);
|
|
1080
|
+
if (!leads) {
|
|
1081
|
+
this.add(EngineIssue.PLACEMENT_INVALID, { tag: child.tag }, where);
|
|
1082
|
+
return [];
|
|
1083
|
+
}
|
|
1084
|
+
const node = this.visitDeclaredChild(child, spec, scope, place, where, host.localIds);
|
|
1085
|
+
host.places.set(node, where);
|
|
1086
|
+
host.children.push(node);
|
|
1087
|
+
return [node];
|
|
1088
|
+
}
|
|
1089
|
+
|
|
1090
|
+
private visitComponentChildren(
|
|
1091
|
+
el: ElementNode,
|
|
1092
|
+
manifest: ComponentManifest,
|
|
1093
|
+
scope: Scope,
|
|
1094
|
+
place: Place,
|
|
1095
|
+
host: Where,
|
|
1096
|
+
childPlaces: Map<ElementNode, Where>,
|
|
1097
|
+
localIds: Set<string>,
|
|
1098
|
+
): ElementNode[] {
|
|
1099
|
+
const tag = manifest.tag;
|
|
1100
|
+
const out: ElementNode[] = [];
|
|
1101
|
+
const counts = new Map<string, number>();
|
|
1102
|
+
for (const child of el.children) {
|
|
1103
|
+
const order = this.order++;
|
|
1104
|
+
if (child.type === 'text') {
|
|
1105
|
+
if (!isHtmlWhitespace(child.value)) {
|
|
1106
|
+
this.add(EngineIssue.CHILD_UNEXPECTED, { tag, child: 'text' }, { ...host, order });
|
|
1107
|
+
}
|
|
1108
|
+
continue;
|
|
1109
|
+
}
|
|
1110
|
+
const index = (counts.get(child.tag) ?? 0) + 1;
|
|
1111
|
+
counts.set(child.tag, index);
|
|
1112
|
+
const childPlace: Place = { path: `${place.path} > ${segment(child, index)}`, order, index };
|
|
1113
|
+
const where = this.where(scope, childPlace, child.tag);
|
|
1114
|
+
const spec = manifest.children.find((c) => c.tag === child.tag);
|
|
1115
|
+
if (spec !== undefined) {
|
|
1116
|
+
const node = this.visitDeclaredChild(child, spec, scope, childPlace, where, localIds);
|
|
1117
|
+
childPlaces.set(node, where);
|
|
1118
|
+
out.push(node);
|
|
1119
|
+
} else if (HELP_TAGS.has(child.tag) && scope.host !== null) {
|
|
1120
|
+
this.visitHelp(child, scope, childPlace, where, scope.host.help, scope.host);
|
|
1121
|
+
} else if (REMOVED_WITH_CONTENT.has(child.tag)) {
|
|
1122
|
+
this.add(EngineIssue.ELEMENT_REMOVED, { tag: child.tag }, where, 'warning');
|
|
1123
|
+
} else if (this.options.definitions.has(child.tag)) {
|
|
1124
|
+
this.add(EngineIssue.COMPONENT_NESTED, { tag: child.tag, parent: tag }, where);
|
|
1125
|
+
} else if (child.tag === COLUMNS_TAG || child.tag === COLUMN_TAG) {
|
|
1126
|
+
this.add(EngineIssue.COLUMNS_INVALID, { tag: child.tag }, where);
|
|
1127
|
+
} else if (child.tag === STYLE_TAG) {
|
|
1128
|
+
this.add(EngineIssue.PLACEMENT_INVALID, { tag: child.tag }, where);
|
|
1129
|
+
} else if (child.tag.includes('-') && child.tag !== DOC_TAG && child.tag !== PAGE_TAG) {
|
|
1130
|
+
// A child another type declares is known, just not accepted here.
|
|
1131
|
+
if (this.owners.has(child.tag))
|
|
1132
|
+
this.add(EngineIssue.CHILD_UNEXPECTED, { tag, child: child.tag }, where);
|
|
1133
|
+
else this.add(EngineIssue.ELEMENT_UNKNOWN, { tag: child.tag }, where);
|
|
1134
|
+
} else {
|
|
1135
|
+
this.add(EngineIssue.CHILD_UNEXPECTED, { tag, child: child.tag }, where);
|
|
1136
|
+
}
|
|
1137
|
+
}
|
|
1138
|
+
return out;
|
|
1139
|
+
}
|
|
1140
|
+
|
|
1141
|
+
private visitDeclaredChild(
|
|
1142
|
+
child: ElementNode,
|
|
1143
|
+
spec: ChildSpec,
|
|
1144
|
+
scope: Scope,
|
|
1145
|
+
place: Place,
|
|
1146
|
+
where: Where,
|
|
1147
|
+
localIds: Set<string>,
|
|
1148
|
+
): ElementNode {
|
|
1149
|
+
const attrs: Record<string, string> = {};
|
|
1150
|
+
for (const [name, value] of Object.entries(child.attrs)) {
|
|
1151
|
+
if (name === 'style' || name === CLASS_ATTRIBUTE) continue;
|
|
1152
|
+
if (CONTENT_ATTRIBUTES.has(name)) {
|
|
1153
|
+
if (this.globalAttribute(name, value, child.tag, where)) attrs[name] = value;
|
|
1154
|
+
continue;
|
|
1155
|
+
}
|
|
1156
|
+
const attr = spec.attributes.find((a) => a.name === name);
|
|
1157
|
+
if (attr === undefined || attr.visibility === 'derived') {
|
|
1158
|
+
this.add(EngineIssue.ATTRIBUTE_REMOVED, { tag: child.tag, attribute: name }, where, 'warning');
|
|
1159
|
+
continue;
|
|
1160
|
+
}
|
|
1161
|
+
if (attr.type === 'url') {
|
|
1162
|
+
if (this.allowsUrl(value, child.tag, name, where)) attrs[name] = value;
|
|
1163
|
+
continue;
|
|
1164
|
+
}
|
|
1165
|
+
attrs[name] = value;
|
|
1166
|
+
if (attr.type !== 'id') this.checkValue(attr, value, child.tag, where);
|
|
1167
|
+
else if (!isSourceId(value)) this.add(EngineIssue.ID_INVALID, { id: value, tag: child.tag }, where);
|
|
1168
|
+
else if (localIds.has(value)) this.add(EngineIssue.ID_DUPLICATE, { id: value }, where);
|
|
1169
|
+
else localIds.add(value);
|
|
1170
|
+
}
|
|
1171
|
+
this.inline(attrs, child.attrs, child.tag, where, scope);
|
|
1172
|
+
this.checkRequired(spec.attributes, attrs, child.tag, where);
|
|
1173
|
+
const inner: Scope = { ...scope, ...this.below(attrs, scope), parent: child.tag, mode: spec.content };
|
|
1174
|
+
return element(child.tag, attrs, this.visitNodes(child, inner, place.path));
|
|
1175
|
+
}
|
|
1176
|
+
|
|
1177
|
+
private pageHelpParts(index: number): HelpParts {
|
|
1178
|
+
const known = this.pageHelp.get(index);
|
|
1179
|
+
if (known !== undefined) return known;
|
|
1180
|
+
const parts = noHelp();
|
|
1181
|
+
this.pageHelp.set(index, parts);
|
|
1182
|
+
return parts;
|
|
1183
|
+
}
|
|
1184
|
+
|
|
1185
|
+
/** A page's help once the page is read: with no graded component on the page, nothing could ever show it. */
|
|
1186
|
+
private pageHelpOf(index: number): HelpRecord | null {
|
|
1187
|
+
const parts = this.pageHelp.get(index);
|
|
1188
|
+
if (parts === undefined) return null;
|
|
1189
|
+
if (this.gradedPages.has(index)) return helpRecord(parts);
|
|
1190
|
+
for (const { tag, at } of parts.where) this.add(EngineIssue.HELP_UNGRADED, { tag }, at, 'warning');
|
|
1191
|
+
return null;
|
|
1192
|
+
}
|
|
1193
|
+
|
|
1194
|
+
/**
|
|
1195
|
+
* One part of the help of `host` (a component) or of a page, into `parts`, never into the tree (compile puts it into
|
|
1196
|
+
* the key): a hint's `cost`, a feedback text's `when`, no other attribute, and content as a page holds it (inline
|
|
1197
|
+
* for an inline component), without components.
|
|
1198
|
+
*/
|
|
1199
|
+
private visitHelp(
|
|
1200
|
+
el: ElementNode,
|
|
1201
|
+
scope: Scope,
|
|
1202
|
+
place: Place,
|
|
1203
|
+
where: Where,
|
|
1204
|
+
parts: HelpParts,
|
|
1205
|
+
host: Host | null,
|
|
1206
|
+
): void {
|
|
1207
|
+
const tag = el.tag;
|
|
1208
|
+
let cost = 0;
|
|
1209
|
+
let when: FeedbackVariant | null = null;
|
|
1210
|
+
let valid = true;
|
|
1211
|
+
for (const [name, value] of Object.entries(el.attrs)) {
|
|
1212
|
+
if (tag === HINT_TAG && name === 'cost') {
|
|
1213
|
+
const n = parseDecimalValue(value);
|
|
1214
|
+
if (n !== null && n >= 0 && n <= 1) cost = round6(n);
|
|
1215
|
+
else {
|
|
1216
|
+
this.add(EngineIssue.HINT_COST_INVALID, { value }, where);
|
|
1217
|
+
valid = false;
|
|
1218
|
+
}
|
|
1219
|
+
} else if (tag === FEEDBACK_TAG && name === 'when') {
|
|
1220
|
+
const variant = FEEDBACK_VARIANTS.find((v) => v === value);
|
|
1221
|
+
if (variant !== undefined) when = variant;
|
|
1222
|
+
else {
|
|
1223
|
+
this.add(EngineIssue.FEEDBACK_WHEN_INVALID, { value }, where);
|
|
1224
|
+
valid = false;
|
|
1225
|
+
}
|
|
1226
|
+
} else {
|
|
1227
|
+
this.add(EngineIssue.ATTRIBUTE_REMOVED, { tag, attribute: name }, where, 'warning');
|
|
1228
|
+
}
|
|
1229
|
+
}
|
|
1230
|
+
if (tag === FEEDBACK_TAG && own(el.attrs, 'when') === undefined) {
|
|
1231
|
+
this.add(EngineIssue.ATTRIBUTE_REQUIRED, { tag, attribute: 'when' }, where);
|
|
1232
|
+
valid = false;
|
|
1233
|
+
}
|
|
1234
|
+
const inner: Scope = {
|
|
1235
|
+
...scope,
|
|
1236
|
+
parent: tag,
|
|
1237
|
+
inLink: false,
|
|
1238
|
+
mode: host?.manifest.display === 'inline' ? 'inline' : 'flow',
|
|
1239
|
+
host: null,
|
|
1240
|
+
help: { tag, element: host?.id ?? null },
|
|
1241
|
+
};
|
|
1242
|
+
this.inHelp = true;
|
|
1243
|
+
let content: Node[];
|
|
1244
|
+
try {
|
|
1245
|
+
content = this.visitNodes(el, inner, place.path);
|
|
1246
|
+
} finally {
|
|
1247
|
+
this.inHelp = false;
|
|
1248
|
+
}
|
|
1249
|
+
if (host !== null && !host.manifest.graded) {
|
|
1250
|
+
this.add(EngineIssue.HELP_UNGRADED, { tag }, where, 'warning');
|
|
1251
|
+
return;
|
|
1252
|
+
}
|
|
1253
|
+
if (isBlank(content)) {
|
|
1254
|
+
this.add(EngineIssue.HELP_EMPTY, { tag }, where, 'warning');
|
|
1255
|
+
return;
|
|
1256
|
+
}
|
|
1257
|
+
if (!valid) return;
|
|
1258
|
+
if (tag === EXPLANATION_TAG) {
|
|
1259
|
+
if (parts.explanation !== null) {
|
|
1260
|
+
this.add(EngineIssue.EXPLANATION_DUPLICATE, {}, where);
|
|
1261
|
+
return;
|
|
1262
|
+
}
|
|
1263
|
+
parts.explanation = content;
|
|
1264
|
+
} else if (tag === FEEDBACK_TAG && when !== null) {
|
|
1265
|
+
if (Object.hasOwn(parts.feedback, when)) {
|
|
1266
|
+
this.add(EngineIssue.FEEDBACK_DUPLICATE, { when }, where);
|
|
1267
|
+
return;
|
|
1268
|
+
}
|
|
1269
|
+
parts.feedback[when] = content;
|
|
1270
|
+
} else parts.hints.push({ content, cost });
|
|
1271
|
+
parts.where.push({ tag, at: where });
|
|
1272
|
+
}
|
|
1273
|
+
|
|
1274
|
+
private runDefinition(
|
|
1275
|
+
definition: ComponentDefinition,
|
|
1276
|
+
node: ElementNode,
|
|
1277
|
+
lang: string,
|
|
1278
|
+
where: Where,
|
|
1279
|
+
childPlaces: ReadonlyMap<ElementNode, Where>,
|
|
1280
|
+
page: number,
|
|
1281
|
+
): void {
|
|
1282
|
+
const manifest = definition.manifest;
|
|
1283
|
+
definition.validate(node, {
|
|
1284
|
+
lang,
|
|
1285
|
+
profile: this.options.profile,
|
|
1286
|
+
find: (id) => {
|
|
1287
|
+
const record = this.bySource.get(id);
|
|
1288
|
+
return record?.page === page ? record.element : undefined;
|
|
1289
|
+
},
|
|
1290
|
+
report: (code, params = {}, options = {}) => {
|
|
1291
|
+
if (!manifest.issues.includes(code)) {
|
|
1292
|
+
throw new Error(
|
|
1293
|
+
`Component definition ${manifest.tag}: issue ${code} is not listed in its manifest`,
|
|
1294
|
+
);
|
|
1295
|
+
}
|
|
1296
|
+
const child = options.child;
|
|
1297
|
+
const at = child === undefined ? where : (childPlaces.get(child) ?? { ...where, tag: child.tag });
|
|
1298
|
+
this.add(code, params, at, options.severity ?? 'error', manifest.messages);
|
|
1299
|
+
},
|
|
1300
|
+
});
|
|
1301
|
+
}
|
|
1302
|
+
|
|
1303
|
+
/** A page or component id: one namespace for the whole document. */
|
|
1304
|
+
private sourceId(value: string, tag: string, where: Where): void {
|
|
1305
|
+
if (!isSourceId(value)) this.add(EngineIssue.ID_INVALID, { id: value, tag }, where);
|
|
1306
|
+
else if (this.ids.has(value)) this.add(EngineIssue.ID_DUPLICATE, { id: value }, where);
|
|
1307
|
+
else this.ids.add(value);
|
|
1308
|
+
}
|
|
1309
|
+
|
|
1310
|
+
/**
|
|
1311
|
+
* A URL attribute through the URL policy: a blocked URL is reported and the attribute dropped. A `required` one (a
|
|
1312
|
+
* picture's address: without it nothing can be shown) is an error; a link without its address is still text.
|
|
1313
|
+
*/
|
|
1314
|
+
private allowsUrl(value: string, tag: string, attribute: string, where: Where, required = false): boolean {
|
|
1315
|
+
if (this.options.urlPolicy(value, { tag, attribute })) return true;
|
|
1316
|
+
const params = { url: value, tag };
|
|
1317
|
+
if (required) this.add(EngineIssue.URL_BLOCKED, params, where);
|
|
1318
|
+
else this.add(EngineIssue.URL_BLOCKED, params, where, 'warning');
|
|
1319
|
+
return false;
|
|
1320
|
+
}
|
|
1321
|
+
|
|
1322
|
+
/** `lang` and `dir`, allowed on every element (`style` is `inline`'s). Returns whether the value is kept. */
|
|
1323
|
+
private globalAttribute(name: string, value: string, tag: string, where: Where): boolean {
|
|
1324
|
+
const valid = name === 'lang' ? LANGUAGE_TAG_PATTERN.test(value) : DIRECTIONS.has(value);
|
|
1325
|
+
if (!valid && name === 'lang') this.invalid(tag, name, value, 'string', [], where);
|
|
1326
|
+
else if (!valid) this.invalid(tag, name, value, 'enum', [...DIRECTIONS], where);
|
|
1327
|
+
return valid;
|
|
1328
|
+
}
|
|
1329
|
+
|
|
1330
|
+
/** ATTRIBUTE_INVALID; for enums the message lists the allowed values. */
|
|
1331
|
+
private invalid(
|
|
1332
|
+
tag: string,
|
|
1333
|
+
attribute: string,
|
|
1334
|
+
value: string,
|
|
1335
|
+
type: AttributeSpec['type'],
|
|
1336
|
+
values: readonly string[],
|
|
1337
|
+
where: Where,
|
|
1338
|
+
): void {
|
|
1339
|
+
const params = { tag, attribute, value, type, values: values.join(', ') };
|
|
1340
|
+
this.add(EngineIssue.ATTRIBUTE_INVALID, params, where);
|
|
1341
|
+
}
|
|
1342
|
+
|
|
1343
|
+
/**
|
|
1344
|
+
* A `style-template` value: a registered template allowed at this scope and, on a component, for its tag.
|
|
1345
|
+
* Otherwise a warning; the attribute stays (the players that show the view may know more templates).
|
|
1346
|
+
*/
|
|
1347
|
+
private styleTemplate(name: string, tag: string, scope: StyleScope, where: Where): void {
|
|
1348
|
+
const template = this.options.styleTemplates?.get(name);
|
|
1349
|
+
const allowed =
|
|
1350
|
+
template?.scopes.includes(scope) === true &&
|
|
1351
|
+
(scope === 'document' || template.components === undefined || template.components.includes(tag));
|
|
1352
|
+
if (!allowed) this.add(EngineIssue.STYLE_TEMPLATE_UNKNOWN, { name, tag }, where, 'warning');
|
|
1353
|
+
}
|
|
1354
|
+
|
|
1355
|
+
private checkValue(spec: AttributeSpec, value: string, tag: string, where: Where): void {
|
|
1356
|
+
let valid: boolean;
|
|
1357
|
+
switch (spec.type) {
|
|
1358
|
+
case 'string':
|
|
1359
|
+
valid = true;
|
|
1360
|
+
break;
|
|
1361
|
+
case 'boolean':
|
|
1362
|
+
valid = isBooleanValue(spec.name, value);
|
|
1363
|
+
break;
|
|
1364
|
+
case 'integer':
|
|
1365
|
+
case 'number': {
|
|
1366
|
+
const n = spec.type === 'integer' ? parseIntegerValue(value) : parseDecimalValue(value);
|
|
1367
|
+
valid = n !== null && !outOfRange(n, spec);
|
|
1368
|
+
break;
|
|
1369
|
+
}
|
|
1370
|
+
case 'enum':
|
|
1371
|
+
valid = (spec.values ?? []).includes(value);
|
|
1372
|
+
break;
|
|
1373
|
+
case 'id':
|
|
1374
|
+
if (!isSourceId(value)) this.add(EngineIssue.ID_INVALID, { id: value, tag }, where);
|
|
1375
|
+
return;
|
|
1376
|
+
case 'url':
|
|
1377
|
+
// Checked by the URL policy before any value check (a blocked URL is removed, not kept as invalid).
|
|
1378
|
+
return;
|
|
1379
|
+
}
|
|
1380
|
+
if (!valid)
|
|
1381
|
+
this.invalid(tag, spec.name, value, spec.type, spec.type === 'enum' ? (spec.values ?? []) : [], where);
|
|
1382
|
+
}
|
|
1383
|
+
|
|
1384
|
+
private checkRequired(
|
|
1385
|
+
specs: readonly AttributeSpec[],
|
|
1386
|
+
attrs: Readonly<Record<string, string>>,
|
|
1387
|
+
tag: string,
|
|
1388
|
+
where: Where,
|
|
1389
|
+
): void {
|
|
1390
|
+
for (const spec of specs) {
|
|
1391
|
+
if (!spec.required || spec.visibility === 'derived' || Object.hasOwn(attrs, spec.name)) continue;
|
|
1392
|
+
if (spec.name === 'id') this.add(EngineIssue.ID_MISSING, { tag }, where);
|
|
1393
|
+
else this.add(EngineIssue.ATTRIBUTE_REQUIRED, { tag, attribute: spec.name }, where);
|
|
1394
|
+
}
|
|
1395
|
+
}
|
|
1396
|
+
}
|