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