@es3d/es3d 1.0.0 → 1.0.2
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/LICENSE +201 -201
- package/README.md +95 -86
- package/docs/SPEC.md +426 -417
- package/package.json +1 -1
- package/scripts/es3d-cli.mjs +390 -377
- package/src/cache/geometry-store.js +392 -392
- package/src/cache/mesh-bundle.js +123 -123
- package/src/cache/sha256.js +73 -73
- package/src/format/ast.js +238 -238
- package/src/format/colors.js +93 -93
- package/src/format/compact-parser.js +171 -171
- package/src/format/compact-tokenizer.js +165 -165
- package/src/format/errors.js +247 -247
- package/src/format/json.js +55 -55
- package/src/format/limits.js +14 -14
- package/src/format/parse.js +132 -92
- package/src/format/schema.js +617 -617
- package/src/format/serializer.js +333 -333
- package/src/format/validator.js +330 -330
- package/src/format/value-error.js +12 -12
- package/src/format/value-format.js +314 -314
- package/src/format/values.js +459 -459
- package/src/format/xml-parser.js +136 -136
- package/src/format/xml-tokenizer.js +237 -237
- package/src/geometry/contour.js +86 -86
- package/src/geometry/extrude.js +175 -175
- package/src/geometry/gear.js +85 -85
- package/src/geometry/geometry-cache.js +159 -159
- package/src/geometry/lod-error.js +178 -178
- package/src/geometry/mesh.js +458 -458
- package/src/geometry/primitives.js +299 -299
- package/src/geometry/registry.js +645 -645
- package/src/geometry/revolve.js +136 -136
- package/src/geometry/sweep.js +441 -441
- package/src/index.js +47 -47
- package/src/io/compression.js +67 -67
- package/src/io/convert.js +236 -236
- package/src/io/download.js +13 -13
- package/src/io/external-scene.js +127 -127
- package/src/io/file-loader.js +28 -28
- package/src/io/gltf-pack.js +1461 -0
- package/src/io/gltf.js +630 -630
- package/src/io/mesh-import.js +104 -104
- package/src/io/obj.js +352 -352
- package/src/io/step-brep.js +439 -439
- package/src/io/step-exact.js +1149 -1149
- package/src/io/step.js +1418 -1418
- package/src/io/stl.js +193 -193
- package/src/io/svg-import.js +787 -787
- package/src/material/materials.js +154 -154
- package/src/math/mat4.js +475 -475
- package/src/math/random.js +30 -30
- package/src/math/trig.js +65 -65
- package/src/math/vec3.js +81 -81
- package/src/optimize/optimizer.js +604 -579
- package/src/path/path-flatten.js +322 -322
- package/src/path/path-parser.js +332 -332
- package/src/path/shapes.js +129 -129
- package/src/path/triangulate.js +517 -517
- package/src/pipeline.js +74 -74
- package/src/render/camera.js +168 -168
- package/src/render/frame-builder.js +362 -362
- package/src/render/geometry-pool.js +149 -149
- package/src/render/lighting.js +149 -149
- package/src/render/orbit-controls.js +237 -237
- package/src/render/renderer.js +258 -249
- package/src/render/scroll-timeline.js +104 -104
- package/src/render/shaders/glsl.js +285 -285
- package/src/render/shaders/wgsl.js +252 -252
- package/src/render/stage.js +365 -352
- package/src/render/texture-cache.js +40 -40
- package/src/render/textures.js +223 -223
- package/src/render/webgl2/webgl2-backend.js +586 -532
- package/src/render/webgpu/webgpu-backend.js +482 -480
- package/src/scene/flatten.js +754 -754
- package/src/scene/picking.js +161 -161
- package/src/scene/scene-graph.js +320 -320
- package/src/transform/transform.js +211 -211
- package/src/wasm/kernels-binary.js +3 -3
- package/src/wasm/kernels.js +385 -385
package/src/format/validator.js
CHANGED
|
@@ -1,330 +1,330 @@
|
|
|
1
|
-
// Schema validation and reference resolution.
|
|
2
|
-
//
|
|
3
|
-
// Converts raw attribute strings into typed values (element.values), checks
|
|
4
|
-
// element nesting, numeric ranges and cross-attribute rules, verifies every id
|
|
5
|
-
// reference and detects circular <use> chains. All problems are collected (not
|
|
6
|
-
// just the first) and reported with exact source positions and suggestions.
|
|
7
|
-
|
|
8
|
-
import { DiagnosticList, suggest } from './errors.js';
|
|
9
|
-
import { indexTree } from './ast.js';
|
|
10
|
-
import { ELEMENTS, ROOT_ELEMENTS, SHAPES, CONTAINERS, aliasesFor, elementSpec, ALL_ELEMENT_NAMES } from './schema.js';
|
|
11
|
-
import { parseTyped } from './values.js';
|
|
12
|
-
import { ValueError } from './value-error.js';
|
|
13
|
-
|
|
14
|
-
const USABLE = new Set([...SHAPES, ...CONTAINERS, 'use', 'light']);
|
|
15
|
-
|
|
16
|
-
function elementPath(el) {
|
|
17
|
-
const parts = [];
|
|
18
|
-
for (let e = el; e; e = e.parent) {
|
|
19
|
-
let label = e.name;
|
|
20
|
-
if (e.parent) {
|
|
21
|
-
let index = -1, same = 0;
|
|
22
|
-
for (const c of e.parent.children) {
|
|
23
|
-
if (c.type !== 'element' || c.name !== e.name) continue;
|
|
24
|
-
if (c === e) index = same;
|
|
25
|
-
same++;
|
|
26
|
-
}
|
|
27
|
-
if (same > 1) label += `[${index}]`;
|
|
28
|
-
}
|
|
29
|
-
const id = e.attr && e.attr('id');
|
|
30
|
-
if (id) label += `#${id}`;
|
|
31
|
-
parts.unshift(label);
|
|
32
|
-
}
|
|
33
|
-
return parts.join(' > ');
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function describeNumber(spec) {
|
|
37
|
-
const parts = [];
|
|
38
|
-
if (spec.exclusiveMin !== undefined) parts.push(`greater than ${spec.exclusiveMin}`);
|
|
39
|
-
else if (spec.min !== undefined) parts.push(`at least ${spec.min}`);
|
|
40
|
-
if (spec.max !== undefined) parts.push(`at most ${spec.max}`);
|
|
41
|
-
return parts.join(' and ');
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function checkNumeric(spec, value) {
|
|
45
|
-
if (typeof value !== 'number') return null;
|
|
46
|
-
if (spec.exclusiveMin !== undefined && !(value > spec.exclusiveMin)) return describeNumber(spec);
|
|
47
|
-
if (spec.min !== undefined && value < spec.min) return describeNumber(spec);
|
|
48
|
-
if (spec.max !== undefined && value > spec.max) return describeNumber(spec);
|
|
49
|
-
return null;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Validate a parsed document in place.
|
|
54
|
-
* Sets el.values / el.attrIndex, doc.ids and doc.diagnostics.
|
|
55
|
-
* @returns {import('./ast.js').Document}
|
|
56
|
-
*/
|
|
57
|
-
const REQUIRED = new WeakMap();
|
|
58
|
-
|
|
59
|
-
/** Names of an element's required attributes, in schema order (computed once per element type). */
|
|
60
|
-
function requiredAttrs(spec) {
|
|
61
|
-
let list = REQUIRED.get(spec);
|
|
62
|
-
if (!list) {
|
|
63
|
-
list = Object.entries(spec.attrs).filter(([, a]) => a.required).map(([name]) => name);
|
|
64
|
-
REQUIRED.set(spec, list);
|
|
65
|
-
}
|
|
66
|
-
return list;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
export function validate(doc) {
|
|
70
|
-
const diags = new DiagnosticList(doc.lineIndex);
|
|
71
|
-
indexTree(doc);
|
|
72
|
-
doc.ids = new Map();
|
|
73
|
-
const refs = []; // {el, attr, id, kind, list?}
|
|
74
|
-
|
|
75
|
-
const locEl = (el) => (el.nameStart >= 0 ? { offset: el.nameStart, length: el.nameEnd - el.nameStart } : { offset: -1, length: 0, path: elementPath(el) });
|
|
76
|
-
const locAttrName = (el, a) => (a && a.nameStart >= 0 ? { offset: a.nameStart, length: a.nameEnd - a.nameStart } : locEl(el));
|
|
77
|
-
const locAttrValue = (el, a, e) => {
|
|
78
|
-
if (!a || a.valueStart < 0) return locEl(el);
|
|
79
|
-
if (e && !a.decoded && e.offset !== undefined) {
|
|
80
|
-
const off = Math.min(a.valueStart + e.offset, a.valueEnd);
|
|
81
|
-
const len = Math.max(1, Math.min(e.length || 1, a.valueEnd - off));
|
|
82
|
-
return { offset: off, length: len };
|
|
83
|
-
}
|
|
84
|
-
return { offset: a.valueStart, length: Math.max(1, a.valueEnd - a.valueStart) };
|
|
85
|
-
};
|
|
86
|
-
const report = (severity, code, message, loc, extra = {}, el = null) => {
|
|
87
|
-
diags.add({ severity, code, message, offset: loc.offset, length: loc.length, path: loc.path || (loc.offset < 0 && el ? elementPath(el) : null), uid: el ? el.uid : 0, ...extra });
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
const root = doc.root;
|
|
91
|
-
if (!ROOT_ELEMENTS.includes(root.name)) {
|
|
92
|
-
report('error', 'E_ROOT', `The root element must be <es3d> but found <${root.name}>.`, locEl(root), { suggestion: 'es3d' }, root);
|
|
93
|
-
doc.diagnostics = diags.sorted();
|
|
94
|
-
return doc;
|
|
95
|
-
}
|
|
96
|
-
if (root.name === 'v3d') {
|
|
97
|
-
report('warning', 'W_LEGACY_ROOT', 'The root element <v3d> is a legacy alias; write <es3d>.', locEl(root), {}, root);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
let sceneCount = 0;
|
|
101
|
-
let envCount = 0;
|
|
102
|
-
let controlsCount = 0;
|
|
103
|
-
|
|
104
|
-
const visit = (el, parentSpec, inDefs) => {
|
|
105
|
-
const spec = elementSpec(el.name);
|
|
106
|
-
if (!spec) {
|
|
107
|
-
if (el.name.startsWith('x-')) {
|
|
108
|
-
report('warning', 'W_EXTENSION', `Extension element <${el.name}> is not supported by this reader; it and its children are not rendered.`, locEl(el), {}, el);
|
|
109
|
-
} else {
|
|
110
|
-
report('error', 'E_UNKNOWN_ELEMENT', `Unknown element: ${el.name}`, locEl(el), { suggestion: suggest(el.name, ALL_ELEMENT_NAMES.concat(['es3d'])) }, el);
|
|
111
|
-
}
|
|
112
|
-
el.values = {};
|
|
113
|
-
el.attrIndex = {};
|
|
114
|
-
el.unknown = true;
|
|
115
|
-
return;
|
|
116
|
-
}
|
|
117
|
-
el.unknown = false;
|
|
118
|
-
if (el.name === 'scene' && ++sceneCount > 1) report('error', 'E_MULTIPLE_SCENES', 'Only one <scene> element is allowed.', locEl(el), {}, el);
|
|
119
|
-
if (el.name === 'environment' && ++envCount > 1) {
|
|
120
|
-
report('warning', 'W_MULTIPLE_ENVIRONMENTS', 'More than one <environment>: only the first one is used.', locEl(el), {}, el);
|
|
121
|
-
}
|
|
122
|
-
if (el.name === 'controls' && ++controlsCount > 1) {
|
|
123
|
-
report('warning', 'W_MULTIPLE_CONTROLS', 'More than one <controls>: only the first one is used.', locEl(el), {}, el);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
// attributes
|
|
127
|
-
const aliases = aliasesFor(el.name);
|
|
128
|
-
const values = {};
|
|
129
|
-
const attrIndex = {};
|
|
130
|
-
for (const a of el.attrs) {
|
|
131
|
-
const canonical = aliases.get(a.name);
|
|
132
|
-
if (!canonical) {
|
|
133
|
-
if (a.name === 'xmlns' || a.name.startsWith('xmlns:')) continue;
|
|
134
|
-
const candidates = [...aliases.keys()];
|
|
135
|
-
const sug = suggest(a.name, candidates);
|
|
136
|
-
let hint = null;
|
|
137
|
-
if (!sug) {
|
|
138
|
-
const own = Object.keys(spec.attrs).filter((k) => !['id', 'name', 'transform', 'position', 'rotation', 'scale', 'visible', 'material', 'color'].includes(k));
|
|
139
|
-
if (own.length) hint = `<${el.name}> accepts: ${own.join(', ')} (plus id, name, transform, position, rotation, scale, material, color).`;
|
|
140
|
-
}
|
|
141
|
-
report('error', 'E_UNKNOWN_ATTRIBUTE', `Unknown attribute: ${a.name}`, locAttrName(el, a), { suggestion: sug, hint }, el);
|
|
142
|
-
continue;
|
|
143
|
-
}
|
|
144
|
-
if (attrIndex[canonical]) {
|
|
145
|
-
report('error', 'E_DUPLICATE_ATTRIBUTE', `Attribute '${canonical}' is given twice (as '${attrIndex[canonical].name}' and '${a.name}').`, locAttrName(el, a), {}, el);
|
|
146
|
-
continue;
|
|
147
|
-
}
|
|
148
|
-
attrIndex[canonical] = a;
|
|
149
|
-
const aspec = spec.attrs[canonical];
|
|
150
|
-
let value;
|
|
151
|
-
try {
|
|
152
|
-
value = a.typed !== undefined ? a.typed : parseTyped(aspec, a.value);
|
|
153
|
-
} catch (e) {
|
|
154
|
-
if (!(e instanceof ValueError)) throw e;
|
|
155
|
-
report('error', e.code || 'E_INVALID_VALUE', `Invalid value for '${a.name}': ${e.message}`, locAttrValue(el, a, e), { suggestion: e.suggestion, hint: e.hint }, el);
|
|
156
|
-
continue;
|
|
157
|
-
}
|
|
158
|
-
const bad = checkNumeric(aspec, value);
|
|
159
|
-
if (bad) {
|
|
160
|
-
report('error', 'E_RANGE', `'${a.name}' must be ${bad}; found ${value}.`, locAttrValue(el, a), {}, el);
|
|
161
|
-
continue;
|
|
162
|
-
}
|
|
163
|
-
values[canonical] = value;
|
|
164
|
-
if (aspec.type === 'id' && canonical === 'id') {
|
|
165
|
-
const prev = doc.ids.get(value);
|
|
166
|
-
if (prev) {
|
|
167
|
-
let where = '';
|
|
168
|
-
if (prev.nameStart >= 0 && doc.lineIndex) {
|
|
169
|
-
const p = doc.lineIndex.position(prev.nameStart);
|
|
170
|
-
where = ` (first defined at line ${p.line})`;
|
|
171
|
-
}
|
|
172
|
-
report('error', 'E_DUPLICATE_ID', `Duplicate id '${value}'${where}.`, locAttrValue(el, a), {}, el);
|
|
173
|
-
} else {
|
|
174
|
-
doc.ids.set(value, el);
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
if (aspec.type === 'ref') refs.push({ el, attr: a, canonical, id: value, kind: aspec.ref });
|
|
178
|
-
if (aspec.type === 'idlist' && aspec.ref) for (const id of value) refs.push({ el, attr: a, canonical, id, kind: aspec.ref });
|
|
179
|
-
}
|
|
180
|
-
el.values = values;
|
|
181
|
-
el.attrIndex = attrIndex;
|
|
182
|
-
|
|
183
|
-
for (const name of requiredAttrs(spec)) {
|
|
184
|
-
if (values[name] === undefined && !attrIndex[name]) {
|
|
185
|
-
report('error', 'E_MISSING_ATTRIBUTE', `<${el.name}> requires the attribute '${name}'.`, locEl(el), { hint: spec.attrs[name].desc }, el);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
if (spec.check) {
|
|
189
|
-
// attributes that are present but failed to parse count as given, so
|
|
190
|
-
// cross-attribute rules do not cascade into "missing attribute" errors
|
|
191
|
-
const given = (k) => values[k] !== undefined || !!attrIndex[k];
|
|
192
|
-
spec.check(values, (attrName, message, code = 'E_INVALID_VALUE') => {
|
|
193
|
-
const a = attrName ? attrIndex[attrName] : null;
|
|
194
|
-
report('error', code, message, a ? locAttrValue(el, a) : locEl(el), {}, el);
|
|
195
|
-
}, el, given);
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
// text content
|
|
199
|
-
if (el.text !== null && el.text.trim() !== '' && spec.children !== 'text') {
|
|
200
|
-
const loc = el.textStart >= 0 ? { offset: el.textStart, length: Math.max(1, Math.min(20, el.textEnd - el.textStart)) } : locEl(el);
|
|
201
|
-
report('error', 'E_TEXT_NOT_ALLOWED', `Text content is not allowed inside <${el.name}>.`, loc, {}, el);
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
if (inDefs && el.parent && el.parent.name === 'defs' && values.id === undefined && !attrIndex.id) {
|
|
205
|
-
report('warning', 'W_DEF_WITHOUT_ID', `<${el.name}> inside <defs> has no id, so it can never be used.`, locEl(el), {}, el);
|
|
206
|
-
}
|
|
207
|
-
if (el.name === 'material' && values.id === undefined && !attrIndex.id) {
|
|
208
|
-
report('warning', 'W_MATERIAL_WITHOUT_ID', '<material> without an id can never be referenced.', locEl(el), {}, el);
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
// children
|
|
212
|
-
const allowed = spec.children;
|
|
213
|
-
for (const c of el.children) {
|
|
214
|
-
if (c.type !== 'element') continue;
|
|
215
|
-
if (allowed === 'text' || !Array.isArray(allowed) || !allowed.includes(c.name)) {
|
|
216
|
-
const childSpec = elementSpec(c.name);
|
|
217
|
-
if (childSpec || !c.name.startsWith('x-')) {
|
|
218
|
-
if (childSpec) {
|
|
219
|
-
let hint = null;
|
|
220
|
-
if (c.name === 'animate') hint = '<animate> can only be placed inside an element with a transform (shapes, group, use, repeat, scatter, lod, light, camera).';
|
|
221
|
-
else if (c.name === 'scene' || c.name === 'defs') hint = `<${c.name}> is only allowed directly inside <es3d>.`;
|
|
222
|
-
else if (c.name === 'controls') hint = '<controls> is only allowed directly inside <es3d> or <scene>.';
|
|
223
|
-
else if (spec.category === 'shape') hint = 'Shapes cannot contain other shapes; wrap them in a <group>.';
|
|
224
|
-
report('error', 'E_MISPLACED_ELEMENT', `<${c.name}> is not allowed inside <${el.name}>.`, locEl(c), { hint }, c);
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
visit(c, spec, inDefs || el.name === 'defs');
|
|
229
|
-
}
|
|
230
|
-
};
|
|
231
|
-
visit(root, null, false);
|
|
232
|
-
|
|
233
|
-
// references ---------------------------------------------------------------
|
|
234
|
-
const kindLabel = { material: 'material', path: 'path', node: 'element' };
|
|
235
|
-
for (const r of refs) {
|
|
236
|
-
const target = doc.ids.get(r.id);
|
|
237
|
-
// computed only when reporting: element paths are expensive for large binary documents
|
|
238
|
-
const where = () => (r.attr.valueStart >= 0 ? { offset: r.attr.valueStart, length: Math.max(1, r.attr.valueEnd - r.attr.valueStart) } : locEl(r.el));
|
|
239
|
-
if (!target) {
|
|
240
|
-
const loc = where();
|
|
241
|
-
const candidates = [...doc.ids.entries()]
|
|
242
|
-
.filter(([, t]) => (r.kind === 'material' ? t.name === 'material' : r.kind === 'path' ? t.name === 'path' : USABLE.has(t.name)))
|
|
243
|
-
.map(([id]) => id);
|
|
244
|
-
diags.add({
|
|
245
|
-
phase: 'reference',
|
|
246
|
-
code: 'E_UNKNOWN_REFERENCE',
|
|
247
|
-
message: `Unknown ${kindLabel[r.kind]} '${r.id}' referenced by ${r.attr.name}.`,
|
|
248
|
-
offset: loc.offset,
|
|
249
|
-
length: loc.length,
|
|
250
|
-
path: loc.path || null,
|
|
251
|
-
suggestion: suggest(r.id, candidates),
|
|
252
|
-
uid: r.el.uid,
|
|
253
|
-
});
|
|
254
|
-
continue;
|
|
255
|
-
}
|
|
256
|
-
let ok = true;
|
|
257
|
-
if (r.kind === 'material') ok = target.name === 'material';
|
|
258
|
-
else if (r.kind === 'path') ok = target.name === 'path';
|
|
259
|
-
else if (r.kind === 'node') ok = USABLE.has(target.name);
|
|
260
|
-
if (!ok) {
|
|
261
|
-
const loc = where();
|
|
262
|
-
diags.add({
|
|
263
|
-
phase: 'reference',
|
|
264
|
-
code: 'E_REFERENCE_KIND',
|
|
265
|
-
message: `${r.attr.name}="${r.id}" refers to a <${target.name}>, but a ${r.kind === 'node' ? 'shape, group, use, repeat, scatter, lod or light' : `<${r.kind}>`} is required.`,
|
|
266
|
-
offset: loc.offset,
|
|
267
|
-
length: loc.length,
|
|
268
|
-
path: loc.path || null,
|
|
269
|
-
uid: r.el.uid,
|
|
270
|
-
});
|
|
271
|
-
continue;
|
|
272
|
-
}
|
|
273
|
-
if (r.canonical === 'ref') r.el.target = target;
|
|
274
|
-
if (r.kind === 'path') r.el.pathTarget = target;
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
// circular <use> chains ------------------------------------------------------
|
|
278
|
-
const usesInside = new Map();
|
|
279
|
-
const collectUses = (el) => {
|
|
280
|
-
if (usesInside.has(el)) return usesInside.get(el);
|
|
281
|
-
const list = [];
|
|
282
|
-
const stack = [el];
|
|
283
|
-
while (stack.length) {
|
|
284
|
-
const e = stack.pop();
|
|
285
|
-
if (e.name === 'use' && e.target) list.push(e);
|
|
286
|
-
for (const c of e.children) if (c.type === 'element') stack.push(c);
|
|
287
|
-
}
|
|
288
|
-
usesInside.set(el, list);
|
|
289
|
-
return list;
|
|
290
|
-
};
|
|
291
|
-
const state = new Map(); // target -> 1 visiting, 2 done
|
|
292
|
-
const reported = new Set();
|
|
293
|
-
const dfs = (target, chain) => {
|
|
294
|
-
state.set(target, 1);
|
|
295
|
-
for (const u of collectUses(target)) {
|
|
296
|
-
const t = u.target;
|
|
297
|
-
const st = state.get(t);
|
|
298
|
-
if (st === 1) {
|
|
299
|
-
if (!reported.has(u)) {
|
|
300
|
-
reported.add(u);
|
|
301
|
-
const ids = [...chain.map((x) => x.values.id || x.name), t.values.id || t.name];
|
|
302
|
-
const a = u.attrIndex.ref;
|
|
303
|
-
diags.add({
|
|
304
|
-
phase: 'reference',
|
|
305
|
-
code: 'E_CIRCULAR_REFERENCE',
|
|
306
|
-
message: `Circular reference: ${ids.join(' → ')}.`,
|
|
307
|
-
offset: a && a.valueStart >= 0 ? a.valueStart : u.nameStart,
|
|
308
|
-
length: a && a.valueStart >= 0 ? a.valueEnd - a.valueStart : u.nameEnd - u.nameStart,
|
|
309
|
-
path: u.nameStart < 0 ? elementPath(u) : null,
|
|
310
|
-
hint: 'An element cannot (directly or indirectly) contain a <use> of itself.',
|
|
311
|
-
uid: u.uid,
|
|
312
|
-
});
|
|
313
|
-
}
|
|
314
|
-
u.circular = true;
|
|
315
|
-
} else if (!st) {
|
|
316
|
-
dfs(t, [...chain, t]);
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
state.set(target, 2);
|
|
320
|
-
};
|
|
321
|
-
for (const target of new Set([...doc.ids.values()])) {
|
|
322
|
-
if (!state.has(target)) dfs(target, [target]);
|
|
323
|
-
}
|
|
324
|
-
// uses outside any referenced subtree (e.g. in the scene) that point into a cycle are covered above.
|
|
325
|
-
|
|
326
|
-
doc.diagnostics = diags.sorted();
|
|
327
|
-
return doc;
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
export { elementPath, ELEMENTS };
|
|
1
|
+
// Schema validation and reference resolution.
|
|
2
|
+
//
|
|
3
|
+
// Converts raw attribute strings into typed values (element.values), checks
|
|
4
|
+
// element nesting, numeric ranges and cross-attribute rules, verifies every id
|
|
5
|
+
// reference and detects circular <use> chains. All problems are collected (not
|
|
6
|
+
// just the first) and reported with exact source positions and suggestions.
|
|
7
|
+
|
|
8
|
+
import { DiagnosticList, suggest } from './errors.js';
|
|
9
|
+
import { indexTree } from './ast.js';
|
|
10
|
+
import { ELEMENTS, ROOT_ELEMENTS, SHAPES, CONTAINERS, aliasesFor, elementSpec, ALL_ELEMENT_NAMES } from './schema.js';
|
|
11
|
+
import { parseTyped } from './values.js';
|
|
12
|
+
import { ValueError } from './value-error.js';
|
|
13
|
+
|
|
14
|
+
const USABLE = new Set([...SHAPES, ...CONTAINERS, 'use', 'light']);
|
|
15
|
+
|
|
16
|
+
function elementPath(el) {
|
|
17
|
+
const parts = [];
|
|
18
|
+
for (let e = el; e; e = e.parent) {
|
|
19
|
+
let label = e.name;
|
|
20
|
+
if (e.parent) {
|
|
21
|
+
let index = -1, same = 0;
|
|
22
|
+
for (const c of e.parent.children) {
|
|
23
|
+
if (c.type !== 'element' || c.name !== e.name) continue;
|
|
24
|
+
if (c === e) index = same;
|
|
25
|
+
same++;
|
|
26
|
+
}
|
|
27
|
+
if (same > 1) label += `[${index}]`;
|
|
28
|
+
}
|
|
29
|
+
const id = e.attr && e.attr('id');
|
|
30
|
+
if (id) label += `#${id}`;
|
|
31
|
+
parts.unshift(label);
|
|
32
|
+
}
|
|
33
|
+
return parts.join(' > ');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function describeNumber(spec) {
|
|
37
|
+
const parts = [];
|
|
38
|
+
if (spec.exclusiveMin !== undefined) parts.push(`greater than ${spec.exclusiveMin}`);
|
|
39
|
+
else if (spec.min !== undefined) parts.push(`at least ${spec.min}`);
|
|
40
|
+
if (spec.max !== undefined) parts.push(`at most ${spec.max}`);
|
|
41
|
+
return parts.join(' and ');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function checkNumeric(spec, value) {
|
|
45
|
+
if (typeof value !== 'number') return null;
|
|
46
|
+
if (spec.exclusiveMin !== undefined && !(value > spec.exclusiveMin)) return describeNumber(spec);
|
|
47
|
+
if (spec.min !== undefined && value < spec.min) return describeNumber(spec);
|
|
48
|
+
if (spec.max !== undefined && value > spec.max) return describeNumber(spec);
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Validate a parsed document in place.
|
|
54
|
+
* Sets el.values / el.attrIndex, doc.ids and doc.diagnostics.
|
|
55
|
+
* @returns {import('./ast.js').Document}
|
|
56
|
+
*/
|
|
57
|
+
const REQUIRED = new WeakMap();
|
|
58
|
+
|
|
59
|
+
/** Names of an element's required attributes, in schema order (computed once per element type). */
|
|
60
|
+
function requiredAttrs(spec) {
|
|
61
|
+
let list = REQUIRED.get(spec);
|
|
62
|
+
if (!list) {
|
|
63
|
+
list = Object.entries(spec.attrs).filter(([, a]) => a.required).map(([name]) => name);
|
|
64
|
+
REQUIRED.set(spec, list);
|
|
65
|
+
}
|
|
66
|
+
return list;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function validate(doc) {
|
|
70
|
+
const diags = new DiagnosticList(doc.lineIndex);
|
|
71
|
+
indexTree(doc);
|
|
72
|
+
doc.ids = new Map();
|
|
73
|
+
const refs = []; // {el, attr, id, kind, list?}
|
|
74
|
+
|
|
75
|
+
const locEl = (el) => (el.nameStart >= 0 ? { offset: el.nameStart, length: el.nameEnd - el.nameStart } : { offset: -1, length: 0, path: elementPath(el) });
|
|
76
|
+
const locAttrName = (el, a) => (a && a.nameStart >= 0 ? { offset: a.nameStart, length: a.nameEnd - a.nameStart } : locEl(el));
|
|
77
|
+
const locAttrValue = (el, a, e) => {
|
|
78
|
+
if (!a || a.valueStart < 0) return locEl(el);
|
|
79
|
+
if (e && !a.decoded && e.offset !== undefined) {
|
|
80
|
+
const off = Math.min(a.valueStart + e.offset, a.valueEnd);
|
|
81
|
+
const len = Math.max(1, Math.min(e.length || 1, a.valueEnd - off));
|
|
82
|
+
return { offset: off, length: len };
|
|
83
|
+
}
|
|
84
|
+
return { offset: a.valueStart, length: Math.max(1, a.valueEnd - a.valueStart) };
|
|
85
|
+
};
|
|
86
|
+
const report = (severity, code, message, loc, extra = {}, el = null) => {
|
|
87
|
+
diags.add({ severity, code, message, offset: loc.offset, length: loc.length, path: loc.path || (loc.offset < 0 && el ? elementPath(el) : null), uid: el ? el.uid : 0, ...extra });
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const root = doc.root;
|
|
91
|
+
if (!ROOT_ELEMENTS.includes(root.name)) {
|
|
92
|
+
report('error', 'E_ROOT', `The root element must be <es3d> but found <${root.name}>.`, locEl(root), { suggestion: 'es3d' }, root);
|
|
93
|
+
doc.diagnostics = diags.sorted();
|
|
94
|
+
return doc;
|
|
95
|
+
}
|
|
96
|
+
if (root.name === 'v3d') {
|
|
97
|
+
report('warning', 'W_LEGACY_ROOT', 'The root element <v3d> is a legacy alias; write <es3d>.', locEl(root), {}, root);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
let sceneCount = 0;
|
|
101
|
+
let envCount = 0;
|
|
102
|
+
let controlsCount = 0;
|
|
103
|
+
|
|
104
|
+
const visit = (el, parentSpec, inDefs) => {
|
|
105
|
+
const spec = elementSpec(el.name);
|
|
106
|
+
if (!spec) {
|
|
107
|
+
if (el.name.startsWith('x-')) {
|
|
108
|
+
report('warning', 'W_EXTENSION', `Extension element <${el.name}> is not supported by this reader; it and its children are not rendered.`, locEl(el), {}, el);
|
|
109
|
+
} else {
|
|
110
|
+
report('error', 'E_UNKNOWN_ELEMENT', `Unknown element: ${el.name}`, locEl(el), { suggestion: suggest(el.name, ALL_ELEMENT_NAMES.concat(['es3d'])) }, el);
|
|
111
|
+
}
|
|
112
|
+
el.values = {};
|
|
113
|
+
el.attrIndex = {};
|
|
114
|
+
el.unknown = true;
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
el.unknown = false;
|
|
118
|
+
if (el.name === 'scene' && ++sceneCount > 1) report('error', 'E_MULTIPLE_SCENES', 'Only one <scene> element is allowed.', locEl(el), {}, el);
|
|
119
|
+
if (el.name === 'environment' && ++envCount > 1) {
|
|
120
|
+
report('warning', 'W_MULTIPLE_ENVIRONMENTS', 'More than one <environment>: only the first one is used.', locEl(el), {}, el);
|
|
121
|
+
}
|
|
122
|
+
if (el.name === 'controls' && ++controlsCount > 1) {
|
|
123
|
+
report('warning', 'W_MULTIPLE_CONTROLS', 'More than one <controls>: only the first one is used.', locEl(el), {}, el);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// attributes
|
|
127
|
+
const aliases = aliasesFor(el.name);
|
|
128
|
+
const values = {};
|
|
129
|
+
const attrIndex = {};
|
|
130
|
+
for (const a of el.attrs) {
|
|
131
|
+
const canonical = aliases.get(a.name);
|
|
132
|
+
if (!canonical) {
|
|
133
|
+
if (a.name === 'xmlns' || a.name.startsWith('xmlns:')) continue;
|
|
134
|
+
const candidates = [...aliases.keys()];
|
|
135
|
+
const sug = suggest(a.name, candidates);
|
|
136
|
+
let hint = null;
|
|
137
|
+
if (!sug) {
|
|
138
|
+
const own = Object.keys(spec.attrs).filter((k) => !['id', 'name', 'transform', 'position', 'rotation', 'scale', 'visible', 'material', 'color'].includes(k));
|
|
139
|
+
if (own.length) hint = `<${el.name}> accepts: ${own.join(', ')} (plus id, name, transform, position, rotation, scale, material, color).`;
|
|
140
|
+
}
|
|
141
|
+
report('error', 'E_UNKNOWN_ATTRIBUTE', `Unknown attribute: ${a.name}`, locAttrName(el, a), { suggestion: sug, hint }, el);
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
if (attrIndex[canonical]) {
|
|
145
|
+
report('error', 'E_DUPLICATE_ATTRIBUTE', `Attribute '${canonical}' is given twice (as '${attrIndex[canonical].name}' and '${a.name}').`, locAttrName(el, a), {}, el);
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
attrIndex[canonical] = a;
|
|
149
|
+
const aspec = spec.attrs[canonical];
|
|
150
|
+
let value;
|
|
151
|
+
try {
|
|
152
|
+
value = a.typed !== undefined ? a.typed : parseTyped(aspec, a.value);
|
|
153
|
+
} catch (e) {
|
|
154
|
+
if (!(e instanceof ValueError)) throw e;
|
|
155
|
+
report('error', e.code || 'E_INVALID_VALUE', `Invalid value for '${a.name}': ${e.message}`, locAttrValue(el, a, e), { suggestion: e.suggestion, hint: e.hint }, el);
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
const bad = checkNumeric(aspec, value);
|
|
159
|
+
if (bad) {
|
|
160
|
+
report('error', 'E_RANGE', `'${a.name}' must be ${bad}; found ${value}.`, locAttrValue(el, a), {}, el);
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
values[canonical] = value;
|
|
164
|
+
if (aspec.type === 'id' && canonical === 'id') {
|
|
165
|
+
const prev = doc.ids.get(value);
|
|
166
|
+
if (prev) {
|
|
167
|
+
let where = '';
|
|
168
|
+
if (prev.nameStart >= 0 && doc.lineIndex) {
|
|
169
|
+
const p = doc.lineIndex.position(prev.nameStart);
|
|
170
|
+
where = ` (first defined at line ${p.line})`;
|
|
171
|
+
}
|
|
172
|
+
report('error', 'E_DUPLICATE_ID', `Duplicate id '${value}'${where}.`, locAttrValue(el, a), {}, el);
|
|
173
|
+
} else {
|
|
174
|
+
doc.ids.set(value, el);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
if (aspec.type === 'ref') refs.push({ el, attr: a, canonical, id: value, kind: aspec.ref });
|
|
178
|
+
if (aspec.type === 'idlist' && aspec.ref) for (const id of value) refs.push({ el, attr: a, canonical, id, kind: aspec.ref });
|
|
179
|
+
}
|
|
180
|
+
el.values = values;
|
|
181
|
+
el.attrIndex = attrIndex;
|
|
182
|
+
|
|
183
|
+
for (const name of requiredAttrs(spec)) {
|
|
184
|
+
if (values[name] === undefined && !attrIndex[name]) {
|
|
185
|
+
report('error', 'E_MISSING_ATTRIBUTE', `<${el.name}> requires the attribute '${name}'.`, locEl(el), { hint: spec.attrs[name].desc }, el);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
if (spec.check) {
|
|
189
|
+
// attributes that are present but failed to parse count as given, so
|
|
190
|
+
// cross-attribute rules do not cascade into "missing attribute" errors
|
|
191
|
+
const given = (k) => values[k] !== undefined || !!attrIndex[k];
|
|
192
|
+
spec.check(values, (attrName, message, code = 'E_INVALID_VALUE') => {
|
|
193
|
+
const a = attrName ? attrIndex[attrName] : null;
|
|
194
|
+
report('error', code, message, a ? locAttrValue(el, a) : locEl(el), {}, el);
|
|
195
|
+
}, el, given);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// text content
|
|
199
|
+
if (el.text !== null && el.text.trim() !== '' && spec.children !== 'text') {
|
|
200
|
+
const loc = el.textStart >= 0 ? { offset: el.textStart, length: Math.max(1, Math.min(20, el.textEnd - el.textStart)) } : locEl(el);
|
|
201
|
+
report('error', 'E_TEXT_NOT_ALLOWED', `Text content is not allowed inside <${el.name}>.`, loc, {}, el);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
if (inDefs && el.parent && el.parent.name === 'defs' && values.id === undefined && !attrIndex.id) {
|
|
205
|
+
report('warning', 'W_DEF_WITHOUT_ID', `<${el.name}> inside <defs> has no id, so it can never be used.`, locEl(el), {}, el);
|
|
206
|
+
}
|
|
207
|
+
if (el.name === 'material' && values.id === undefined && !attrIndex.id) {
|
|
208
|
+
report('warning', 'W_MATERIAL_WITHOUT_ID', '<material> without an id can never be referenced.', locEl(el), {}, el);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// children
|
|
212
|
+
const allowed = spec.children;
|
|
213
|
+
for (const c of el.children) {
|
|
214
|
+
if (c.type !== 'element') continue;
|
|
215
|
+
if (allowed === 'text' || !Array.isArray(allowed) || !allowed.includes(c.name)) {
|
|
216
|
+
const childSpec = elementSpec(c.name);
|
|
217
|
+
if (childSpec || !c.name.startsWith('x-')) {
|
|
218
|
+
if (childSpec) {
|
|
219
|
+
let hint = null;
|
|
220
|
+
if (c.name === 'animate') hint = '<animate> can only be placed inside an element with a transform (shapes, group, use, repeat, scatter, lod, light, camera).';
|
|
221
|
+
else if (c.name === 'scene' || c.name === 'defs') hint = `<${c.name}> is only allowed directly inside <es3d>.`;
|
|
222
|
+
else if (c.name === 'controls') hint = '<controls> is only allowed directly inside <es3d> or <scene>.';
|
|
223
|
+
else if (spec.category === 'shape') hint = 'Shapes cannot contain other shapes; wrap them in a <group>.';
|
|
224
|
+
report('error', 'E_MISPLACED_ELEMENT', `<${c.name}> is not allowed inside <${el.name}>.`, locEl(c), { hint }, c);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
visit(c, spec, inDefs || el.name === 'defs');
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
visit(root, null, false);
|
|
232
|
+
|
|
233
|
+
// references ---------------------------------------------------------------
|
|
234
|
+
const kindLabel = { material: 'material', path: 'path', node: 'element' };
|
|
235
|
+
for (const r of refs) {
|
|
236
|
+
const target = doc.ids.get(r.id);
|
|
237
|
+
// computed only when reporting: element paths are expensive for large binary documents
|
|
238
|
+
const where = () => (r.attr.valueStart >= 0 ? { offset: r.attr.valueStart, length: Math.max(1, r.attr.valueEnd - r.attr.valueStart) } : locEl(r.el));
|
|
239
|
+
if (!target) {
|
|
240
|
+
const loc = where();
|
|
241
|
+
const candidates = [...doc.ids.entries()]
|
|
242
|
+
.filter(([, t]) => (r.kind === 'material' ? t.name === 'material' : r.kind === 'path' ? t.name === 'path' : USABLE.has(t.name)))
|
|
243
|
+
.map(([id]) => id);
|
|
244
|
+
diags.add({
|
|
245
|
+
phase: 'reference',
|
|
246
|
+
code: 'E_UNKNOWN_REFERENCE',
|
|
247
|
+
message: `Unknown ${kindLabel[r.kind]} '${r.id}' referenced by ${r.attr.name}.`,
|
|
248
|
+
offset: loc.offset,
|
|
249
|
+
length: loc.length,
|
|
250
|
+
path: loc.path || null,
|
|
251
|
+
suggestion: suggest(r.id, candidates),
|
|
252
|
+
uid: r.el.uid,
|
|
253
|
+
});
|
|
254
|
+
continue;
|
|
255
|
+
}
|
|
256
|
+
let ok = true;
|
|
257
|
+
if (r.kind === 'material') ok = target.name === 'material';
|
|
258
|
+
else if (r.kind === 'path') ok = target.name === 'path';
|
|
259
|
+
else if (r.kind === 'node') ok = USABLE.has(target.name);
|
|
260
|
+
if (!ok) {
|
|
261
|
+
const loc = where();
|
|
262
|
+
diags.add({
|
|
263
|
+
phase: 'reference',
|
|
264
|
+
code: 'E_REFERENCE_KIND',
|
|
265
|
+
message: `${r.attr.name}="${r.id}" refers to a <${target.name}>, but a ${r.kind === 'node' ? 'shape, group, use, repeat, scatter, lod or light' : `<${r.kind}>`} is required.`,
|
|
266
|
+
offset: loc.offset,
|
|
267
|
+
length: loc.length,
|
|
268
|
+
path: loc.path || null,
|
|
269
|
+
uid: r.el.uid,
|
|
270
|
+
});
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
if (r.canonical === 'ref') r.el.target = target;
|
|
274
|
+
if (r.kind === 'path') r.el.pathTarget = target;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// circular <use> chains ------------------------------------------------------
|
|
278
|
+
const usesInside = new Map();
|
|
279
|
+
const collectUses = (el) => {
|
|
280
|
+
if (usesInside.has(el)) return usesInside.get(el);
|
|
281
|
+
const list = [];
|
|
282
|
+
const stack = [el];
|
|
283
|
+
while (stack.length) {
|
|
284
|
+
const e = stack.pop();
|
|
285
|
+
if (e.name === 'use' && e.target) list.push(e);
|
|
286
|
+
for (const c of e.children) if (c.type === 'element') stack.push(c);
|
|
287
|
+
}
|
|
288
|
+
usesInside.set(el, list);
|
|
289
|
+
return list;
|
|
290
|
+
};
|
|
291
|
+
const state = new Map(); // target -> 1 visiting, 2 done
|
|
292
|
+
const reported = new Set();
|
|
293
|
+
const dfs = (target, chain) => {
|
|
294
|
+
state.set(target, 1);
|
|
295
|
+
for (const u of collectUses(target)) {
|
|
296
|
+
const t = u.target;
|
|
297
|
+
const st = state.get(t);
|
|
298
|
+
if (st === 1) {
|
|
299
|
+
if (!reported.has(u)) {
|
|
300
|
+
reported.add(u);
|
|
301
|
+
const ids = [...chain.map((x) => x.values.id || x.name), t.values.id || t.name];
|
|
302
|
+
const a = u.attrIndex.ref;
|
|
303
|
+
diags.add({
|
|
304
|
+
phase: 'reference',
|
|
305
|
+
code: 'E_CIRCULAR_REFERENCE',
|
|
306
|
+
message: `Circular reference: ${ids.join(' → ')}.`,
|
|
307
|
+
offset: a && a.valueStart >= 0 ? a.valueStart : u.nameStart,
|
|
308
|
+
length: a && a.valueStart >= 0 ? a.valueEnd - a.valueStart : u.nameEnd - u.nameStart,
|
|
309
|
+
path: u.nameStart < 0 ? elementPath(u) : null,
|
|
310
|
+
hint: 'An element cannot (directly or indirectly) contain a <use> of itself.',
|
|
311
|
+
uid: u.uid,
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
u.circular = true;
|
|
315
|
+
} else if (!st) {
|
|
316
|
+
dfs(t, [...chain, t]);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
state.set(target, 2);
|
|
320
|
+
};
|
|
321
|
+
for (const target of new Set([...doc.ids.values()])) {
|
|
322
|
+
if (!state.has(target)) dfs(target, [target]);
|
|
323
|
+
}
|
|
324
|
+
// uses outside any referenced subtree (e.g. in the scene) that point into a cycle are covered above.
|
|
325
|
+
|
|
326
|
+
doc.diagnostics = diags.sorted();
|
|
327
|
+
return doc;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
export { elementPath, ELEMENTS };
|