vectorbench 0.0.0-stage → 3.0.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/.env.example +9 -0
- package/LICENSE +201 -0
- package/NOTICE +49 -0
- package/README.md +163 -2
- package/bench.config.mjs +255 -0
- package/bin/vectorbench.mjs +68 -0
- package/harness/Dockerfile +104 -0
- package/harness/adapters/chat.mjs +192 -0
- package/harness/adapters/index.mjs +77 -0
- package/harness/adapters/interactions.mjs +212 -0
- package/harness/adapters/messages.mjs +190 -0
- package/harness/adapters/responses.mjs +179 -0
- package/harness/box/README.md +46 -0
- package/harness/box/check.mjs +18 -0
- package/harness/box/tools.py +638 -0
- package/harness/box/vb3d.py +556 -0
- package/harness/claude-code.mjs +581 -0
- package/harness/codex-account.mjs +122 -0
- package/harness/codex.mjs +933 -0
- package/harness/loop.mjs +114 -0
- package/harness/prices.mjs +30 -0
- package/harness/request.mjs +155 -0
- package/harness/sandbox.mjs +77 -0
- package/package.json +76 -4
- package/scripts/doctor.mjs +111 -0
- package/scripts/harness.mjs +538 -0
- package/scripts/partner.mjs +129 -0
- package/scripts/zip.mjs +77 -0
- package/src/model-apis.js +516 -0
- package/worker/svg.js +350 -0
package/worker/svg.js
ADDED
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
// Checks a drawing before it is stored. Drawings are only ever shown through
|
|
2
|
+
// <img>, where the browser runs no script and loads nothing external, and the
|
|
3
|
+
// file itself is served with a locked-down Content-Security-Policy. This is
|
|
4
|
+
// the third layer: refuse anything that has no business in a static drawing,
|
|
5
|
+
// because the file may also be opened on its own, downloaded, or pasted into
|
|
6
|
+
// a web page.
|
|
7
|
+
//
|
|
8
|
+
// It is not an XML parser. It reads the text the way the most forgiving
|
|
9
|
+
// reader would (an HTML parser, with character references and CSS escapes
|
|
10
|
+
// decoded) and checks every part of it, comments and CDATA included, so
|
|
11
|
+
// nothing depends on guessing which parts a browser will treat as markup.
|
|
12
|
+
//
|
|
13
|
+
// Pure functions: also used by scripts/arena.mjs under Node.
|
|
14
|
+
|
|
15
|
+
export const MAX_SVG_BYTES = 200 * 1024;
|
|
16
|
+
|
|
17
|
+
export class SvgError extends Error {}
|
|
18
|
+
|
|
19
|
+
const refused = (what) => new SvgError(`The SVG contains ${what}, which isn’t allowed. Remove it and try again.`);
|
|
20
|
+
const notSvg = () => new SvgError('That isn’t an SVG. It needs to start with <svg and end with </svg>.');
|
|
21
|
+
|
|
22
|
+
// Everything a drawing can be built from. Any other element is refused, so
|
|
23
|
+
// there is no list of dangerous ones to keep up to date.
|
|
24
|
+
const ELEMENTS = new Map(
|
|
25
|
+
[
|
|
26
|
+
'svg', 'g', 'defs', 'symbol', 'use', 'switch', 'view', 'title', 'desc', 'metadata', 'style',
|
|
27
|
+
'path', 'rect', 'circle', 'ellipse', 'line', 'polyline', 'polygon', 'image',
|
|
28
|
+
'text', 'tspan', 'textPath',
|
|
29
|
+
'linearGradient', 'radialGradient', 'stop', 'pattern', 'clipPath', 'mask', 'marker',
|
|
30
|
+
'filter', 'feBlend', 'feColorMatrix', 'feComponentTransfer', 'feComposite', 'feConvolveMatrix',
|
|
31
|
+
'feDiffuseLighting', 'feDisplacementMap', 'feDistantLight', 'feDropShadow', 'feFlood',
|
|
32
|
+
'feFuncA', 'feFuncB', 'feFuncG', 'feFuncR', 'feGaussianBlur', 'feImage', 'feMerge', 'feMergeNode',
|
|
33
|
+
'feMorphology', 'feOffset', 'fePointLight', 'feSpecularLighting', 'feSpotLight', 'feTile', 'feTurbulence',
|
|
34
|
+
'animate', 'animateTransform', 'animateMotion', 'mpath', 'set',
|
|
35
|
+
].map((name) => [name.toLowerCase(), name]),
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
// The usual suspects get named, whatever prefix or case they arrive in.
|
|
39
|
+
const KNOWN_BAD = [
|
|
40
|
+
[/^script$/, 'a <script> element'],
|
|
41
|
+
[/^foreignobject$/, 'a <foreignObject> element'],
|
|
42
|
+
[/^(iframe|embed|object|audio|video|canvas|handler|listener)$/, 'an embedded-content element'],
|
|
43
|
+
[/^a$/, 'a link'],
|
|
44
|
+
];
|
|
45
|
+
|
|
46
|
+
// Looked for anywhere in the text, as written and again once decoded.
|
|
47
|
+
const FORBIDDEN = [
|
|
48
|
+
[/<\s*script\b/i, 'a <script> element'],
|
|
49
|
+
[/<\s*foreignObject\b/i, 'a <foreignObject> element'],
|
|
50
|
+
[/<\s*(iframe|embed|object|audio|video|canvas|handler|listener)\b/i, 'an embedded-content element'],
|
|
51
|
+
[/<!ENTITY/i, 'an entity declaration'],
|
|
52
|
+
[/<!(?!--|\[CDATA\[)/, 'a DOCTYPE or other declaration inside the SVG'],
|
|
53
|
+
[/<\?/, 'a processing instruction'],
|
|
54
|
+
// A handler can follow a space, a slash or a closing quote: <rect/onclick=…>.
|
|
55
|
+
[/(?:^|[^\w.:-])on[a-z]+\s*=/i, 'an event handler attribute'],
|
|
56
|
+
[/\bxml:base\b/i, 'an xml:base attribute'],
|
|
57
|
+
[/@import/i, 'a CSS @import'],
|
|
58
|
+
// The two CSS functions besides url() that take an address as a plain string.
|
|
59
|
+
[/image-set\s*\(|\bsrc\(/i, 'a CSS image-set() or src()'],
|
|
60
|
+
[/expression\(/i, 'a CSS expression'],
|
|
61
|
+
[/-moz-binding|(?:^|[^\w-])behavior\s*:\s*(?:url\(|["']?#)/i, 'a CSS behavior binding'],
|
|
62
|
+
];
|
|
63
|
+
const SCRIPT_LINK = /(?:java|vb|live)script:/i;
|
|
64
|
+
|
|
65
|
+
// What an entity in a DOCTYPE can smuggle in is cut away with the DOCTYPE,
|
|
66
|
+
// but say so rather than store a drawing with holes in it.
|
|
67
|
+
const DECLARATION = /<!(?:ENTITY|ATTLIST)/i;
|
|
68
|
+
const UNDEFINED_ENTITY = /&(?!(?:amp|lt|gt|quot|apos);)[A-Za-z][A-Za-z0-9]*;/;
|
|
69
|
+
|
|
70
|
+
// XML allows tab and newline and no other control character. Half a
|
|
71
|
+
// surrogate pair has no place either.
|
|
72
|
+
const CONTROL = /[\0-\x08\x0B\x0C\x0E-\x1F\x7F\uFFFE\uFFFF]|\p{Cs}/u;
|
|
73
|
+
|
|
74
|
+
// A link may point inside the file or hold an inline bitmap. Nothing else:
|
|
75
|
+
// this is a list of what is allowed, so there is no scheme to forget. No
|
|
76
|
+
// quotes, = or brackets in a fragment either: a decoy like x='href="#' would
|
|
77
|
+
// otherwise read as one long harmless link that swallows the real one after it.
|
|
78
|
+
const SAME_FILE = /^#[^"'=<>()\\]*$/;
|
|
79
|
+
const INLINE_BITMAP = /^data:image\/(?:png|jpeg|gif|webp);base64,[a-z0-9+/]*=*$/i;
|
|
80
|
+
const allowedTarget = (target) => SAME_FILE.test(target) || INLINE_BITMAP.test(target);
|
|
81
|
+
|
|
82
|
+
const VALUE = String.raw`\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]*))`;
|
|
83
|
+
// href under any prefix (xlink:href, x:href), and src.
|
|
84
|
+
const REFERENCE = new RegExp(String.raw`\b(?:href|src)${VALUE}`, 'gi');
|
|
85
|
+
// Which attribute an <animate> or <set> rewrites while the drawing plays.
|
|
86
|
+
const ANIMATED = new RegExp(String.raw`\battributeName${VALUE}`, 'gi');
|
|
87
|
+
// The last branch catches a url( that isn't followed by anything well formed.
|
|
88
|
+
const CSS_URL = /url\(\s*(?:(["'])([^"']*)\1|([^"'()\s]*))\s*\)|url\(/gi;
|
|
89
|
+
|
|
90
|
+
// The few HTML names that can spell out a link or a style rule. XML itself
|
|
91
|
+
// only knows the first five.
|
|
92
|
+
const NAMED_REFERENCES = {
|
|
93
|
+
amp: '&', lt: '<', gt: '>', quot: '"', apos: "'",
|
|
94
|
+
colon: ':', tab: '\t', newline: '\n', lpar: '(', rpar: ')', sol: '/', bsol: '\\',
|
|
95
|
+
commat: '@', equals: '=', num: '#', period: '.', semi: ';', hyphen: '-', dash: '-',
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const character = (code) => (code > 0 && code <= 0x10ffff ? String.fromCodePoint(code) : '\uFFFD');
|
|
99
|
+
|
|
100
|
+
/** j j : and friends, read as a browser reads them. */
|
|
101
|
+
function decodeReferences(text) {
|
|
102
|
+
return text.replace(/&(?:#x([0-9a-f]+)|#(\d+)|([a-z]+));?/gi, (whole, hex, decimal, name) => {
|
|
103
|
+
if (name) return NAMED_REFERENCES[name.toLowerCase()] ?? whole;
|
|
104
|
+
return character(hex ? parseInt(hex, 16) : parseInt(decimal, 10));
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** CSS escapes: u\72l( is url( and @\69mport is @import. */
|
|
109
|
+
function decodeCss(text) {
|
|
110
|
+
return text.replace(/\\(?:([0-9a-f]{1,6})[ \t\n\f]?|\n|([^]))/gi, (whole, hex, literal) =>
|
|
111
|
+
hex ? character(parseInt(hex, 16)) : (literal ?? ''));
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Browsers drop tabs, newlines and stray control characters from a link
|
|
115
|
+
// before reading its scheme, so "java	script:" still runs.
|
|
116
|
+
const squash = (text) => text.replace(/[\s\0-\x20\x7F]+/g, '');
|
|
117
|
+
|
|
118
|
+
/** The values given to an attribute, decoded and squashed. */
|
|
119
|
+
function* valuesOf(attribute, svg) {
|
|
120
|
+
for (const match of svg.matchAll(attribute)) yield squash(decodeReferences(match[1] ?? match[2] ?? match[3]));
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* The text with every comment and CDATA section spaced out, so a </svg>
|
|
125
|
+
* written inside one can't be taken for the end of the drawing. Same length
|
|
126
|
+
* as the input, so positions carry over.
|
|
127
|
+
*/
|
|
128
|
+
function withoutAsides(text) {
|
|
129
|
+
const opening = /<!--|<!\[CDATA\[/g;
|
|
130
|
+
let out = '';
|
|
131
|
+
let from = 0;
|
|
132
|
+
for (let open; (open = opening.exec(text)); ) {
|
|
133
|
+
const closing = open[0] === '<!--' ? '-->' : ']]>';
|
|
134
|
+
const close = text.indexOf(closing, opening.lastIndex);
|
|
135
|
+
if (close === -1) throw new SvgError('The SVG has a comment or CDATA section that is never closed.');
|
|
136
|
+
const end = close + closing.length;
|
|
137
|
+
out += text.slice(from, open.index) + ' '.repeat(end - open.index);
|
|
138
|
+
from = opening.lastIndex = end;
|
|
139
|
+
}
|
|
140
|
+
return out + text.slice(from);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** Where the tag that opens at `from` ends, minding quoted values. -1 if it never does. */
|
|
144
|
+
function tagEnd(text, from) {
|
|
145
|
+
let quote = '';
|
|
146
|
+
for (let at = from; at < text.length; at++) {
|
|
147
|
+
const char = text[at];
|
|
148
|
+
if (quote) {
|
|
149
|
+
if (char === quote) quote = '';
|
|
150
|
+
} else if (char === '"' || char === "'") {
|
|
151
|
+
quote = char;
|
|
152
|
+
} else if (char === '>') {
|
|
153
|
+
return at;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return -1;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Finds the drawing in what was pasted: from the first <svg to the </svg>
|
|
161
|
+
* that closes it, counting nested ones. Models wrap their answer in prose and
|
|
162
|
+
* code fences, and whatever sits outside the drawing is left behind.
|
|
163
|
+
*/
|
|
164
|
+
function findDrawing(input) {
|
|
165
|
+
const plain = withoutAsides(input);
|
|
166
|
+
const start = plain.search(/<svg[\s>/]/i);
|
|
167
|
+
if (start === -1) throw notSvg();
|
|
168
|
+
|
|
169
|
+
const edge = /<svg(?=[\s>/])|<\/svg\s*>/gi;
|
|
170
|
+
edge.lastIndex = start;
|
|
171
|
+
let depth = 0;
|
|
172
|
+
let rootEnd = -1;
|
|
173
|
+
let end = -1;
|
|
174
|
+
for (let match; end === -1 && (match = edge.exec(plain)); ) {
|
|
175
|
+
if (match[0][1] === '/') {
|
|
176
|
+
depth -= 1;
|
|
177
|
+
if (depth === 0) end = edge.lastIndex;
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
const close = tagEnd(plain, match.index);
|
|
181
|
+
if (close === -1) throw notSvg();
|
|
182
|
+
if (match.index === start) rootEnd = close;
|
|
183
|
+
edge.lastIndex = close + 1;
|
|
184
|
+
if (plain[close - 1] !== '/') depth += 1;
|
|
185
|
+
else if (depth === 0) throw new SvgError('That SVG is empty.');
|
|
186
|
+
}
|
|
187
|
+
if (end === -1) throw notSvg();
|
|
188
|
+
// A stray </svg> further on means the tags don't pair up the way they look.
|
|
189
|
+
if (/<\/svg[\s>]/i.test(plain.slice(end))) {
|
|
190
|
+
throw new SvgError('There is more than one </svg> at the end. Paste one complete SVG.');
|
|
191
|
+
}
|
|
192
|
+
return { start, end, rootEnd: rootEnd - start, plain: plain.slice(start, end) };
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Returns the drawing ready to store, or throws SvgError with a sentence that
|
|
197
|
+
* can be shown to the person who submitted it. `maxBytes` is raised only by
|
|
198
|
+
* the owner's import, for a drawing let in over the limit on purpose.
|
|
199
|
+
*/
|
|
200
|
+
// How many copies of its parts a drawing may ask the browser to draw. Each
|
|
201
|
+
// <use>, and each url(#…) in an attribute, a style or an animation value,
|
|
202
|
+
// counts what it points at, and what that points at in turn; a marker is
|
|
203
|
+
// drawn once per vertex of the shape that names it. A few kilobytes of
|
|
204
|
+
// nested references can otherwise ask for billions and freeze the viewer's
|
|
205
|
+
// tab, even seconds after it loads (a <set> that switches a marker on).
|
|
206
|
+
// The most in the collection on 1 December 2026 was about 62,000.
|
|
207
|
+
export const MAX_COPIES = 200_000;
|
|
208
|
+
// A tag never spans another '<' (XML allows none in a value), which keeps
|
|
209
|
+
// each attempt to the next tag and the whole scan linear.
|
|
210
|
+
const TAG = /<(\/?)([A-Za-z][\w:.-]*)((?:[^<>"']|"[^"<]*"|'[^'<]*')*?)(\/?)>/g;
|
|
211
|
+
const ATTRIBUTE = /([\w:.-]+)\s*=\s*(?:"([^"]*)"|'([^']*)')/g;
|
|
212
|
+
const LOCAL = /url\(\s*["']?\s*#([^"')\s]*)|^\s*#(\S*)\s*$/g;
|
|
213
|
+
|
|
214
|
+
/** The copies of its own parts a drawing asks for (MAX_COPIES); Infinity past the limit or too deep to follow. */
|
|
215
|
+
export function copies(plain) {
|
|
216
|
+
const holds = new Map(); // id → [target, times] for it and everything inside it
|
|
217
|
+
const open = [{ id: null, refs: [], vertices: 1 }];
|
|
218
|
+
const shut = (frame) => {
|
|
219
|
+
if (frame.id) holds.set(frame.id, frame.refs);
|
|
220
|
+
for (const ref of frame.refs) open.at(-1).refs.push(ref);
|
|
221
|
+
};
|
|
222
|
+
for (const [, end, name, attributes, empty] of plain.matchAll(TAG)) {
|
|
223
|
+
if (end) {
|
|
224
|
+
if (open.length > 1) shut(open.pop());
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
const frame = { id: null, refs: [], vertices: 1 };
|
|
228
|
+
const said = [];
|
|
229
|
+
for (const [, attribute, a, b] of attributes.matchAll(ATTRIBUTE)) {
|
|
230
|
+
const value = decodeCss(decodeReferences(a ?? b));
|
|
231
|
+
if (attribute === 'id') frame.id = value.trim();
|
|
232
|
+
else if (attribute === 'd' || attribute === 'points') {
|
|
233
|
+
frame.vertices = Math.max(1, Math.ceil((value.match(/\d*\.?\d+(?:e[-+]?\d+)?/gi) ?? []).length / 2));
|
|
234
|
+
} else if (!/(?:^|:)href$/.test(attribute) || name === 'use') said.push(value);
|
|
235
|
+
}
|
|
236
|
+
const marker = /marker/i.test(decodeCss(decodeReferences(attributes)));
|
|
237
|
+
for (const value of said) {
|
|
238
|
+
for (const [, url, bare] of value.matchAll(LOCAL)) {
|
|
239
|
+
const target = (url ?? bare ?? '').trim();
|
|
240
|
+
// An <animate> or <set> puts its value on the shape it sits in.
|
|
241
|
+
if (target) frame.refs.push([target, marker ? (/^(?:set|animate)$/.test(name) ? open.at(-1).vertices : frame.vertices) : 1]);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
if (empty) shut(frame);
|
|
245
|
+
else open.push(frame);
|
|
246
|
+
}
|
|
247
|
+
while (open.length > 1) shut(open.pop());
|
|
248
|
+
|
|
249
|
+
const known = new Map();
|
|
250
|
+
const visiting = new Set();
|
|
251
|
+
const cost = (id) => {
|
|
252
|
+
if (known.has(id)) return known.get(id);
|
|
253
|
+
if (visiting.has(id) || !holds.has(id)) return 0; // a loop or a missing target draws nothing
|
|
254
|
+
visiting.add(id);
|
|
255
|
+
let n = 1;
|
|
256
|
+
for (const [target, times] of holds.get(id)) if ((n += times * cost(target)) > MAX_COPIES) break;
|
|
257
|
+
visiting.delete(id);
|
|
258
|
+
known.set(id, n);
|
|
259
|
+
return n;
|
|
260
|
+
};
|
|
261
|
+
let total = 0;
|
|
262
|
+
try {
|
|
263
|
+
for (const [target, times] of open[0].refs) if ((total += times * cost(target)) > MAX_COPIES) break;
|
|
264
|
+
} catch (error) {
|
|
265
|
+
if (error instanceof RangeError) return Infinity; // a chain of references too deep to follow
|
|
266
|
+
throw error;
|
|
267
|
+
}
|
|
268
|
+
return total;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
export function cleanSvg(input, { maxBytes = MAX_SVG_BYTES } = {}) {
|
|
272
|
+
if (typeof input !== 'string') throw new SvgError('Paste the SVG code.');
|
|
273
|
+
input = input.replace(/\r\n?/g, '\n');
|
|
274
|
+
|
|
275
|
+
const { start, end, rootEnd, plain } = findDrawing(input);
|
|
276
|
+
let svg = input.slice(start, end);
|
|
277
|
+
|
|
278
|
+
const bytes = new TextEncoder().encode(svg).length;
|
|
279
|
+
if (bytes > maxBytes) {
|
|
280
|
+
throw new SvgError(`That SVG is ${Math.ceil(bytes / 1024)} KB. The limit is ${Math.floor(maxBytes / 1024)} KB.`);
|
|
281
|
+
}
|
|
282
|
+
if (CONTROL.test(svg)) {
|
|
283
|
+
throw new SvgError('The SVG contains control characters, which SVG doesn’t allow. Remove them and try again.');
|
|
284
|
+
}
|
|
285
|
+
if (DECLARATION.test(input.slice(0, start))) throw refused('a DOCTYPE that declares entities or attributes');
|
|
286
|
+
|
|
287
|
+
// Three readings of the same text: as written, with character references
|
|
288
|
+
// decoded, and with CSS escapes decoded too.
|
|
289
|
+
const decoded = decodeReferences(svg);
|
|
290
|
+
const css = decodeCss(decoded);
|
|
291
|
+
for (const [pattern, what] of FORBIDDEN) {
|
|
292
|
+
if ([svg, decoded, css].some((reading) => pattern.test(reading))) throw refused(what);
|
|
293
|
+
}
|
|
294
|
+
if (SCRIPT_LINK.test(squash(decoded)) || SCRIPT_LINK.test(squash(css))) throw refused('a javascript: link');
|
|
295
|
+
|
|
296
|
+
for (const [, name] of svg.matchAll(/<\/?([a-z_:\u0080-\uFFFF][^\s/>]*)/gi)) {
|
|
297
|
+
const plainName = name.toLowerCase();
|
|
298
|
+
const local = plainName.slice(plainName.lastIndexOf(':') + 1);
|
|
299
|
+
const bad = KNOWN_BAD.find(([pattern]) => pattern.test(local));
|
|
300
|
+
if (bad) throw refused(bad[1]);
|
|
301
|
+
const proper = ELEMENTS.get(plainName);
|
|
302
|
+
if (proper === name) continue;
|
|
303
|
+
if (proper) throw new SvgError(`The SVG has a <${name}> tag. SVG is case-sensitive, so it has to be written <${proper}>.`);
|
|
304
|
+
// Only echo a name back if it is nothing but a name.
|
|
305
|
+
throw refused(/^[\w:.-]{1,40}$/.test(name) ? `a <${name}> element` : 'an element that isn’t part of SVG');
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
for (const target of valuesOf(REFERENCE, svg)) {
|
|
309
|
+
if (target && !allowedTarget(target)) {
|
|
310
|
+
throw new SvgError('The SVG links to something outside the file. Everything it uses has to be inside the SVG.');
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
// An animation can rewrite a link or a handler after this check has passed.
|
|
314
|
+
for (const attribute of valuesOf(ANIMATED, svg)) {
|
|
315
|
+
if (!/^[\w:.-]*$/.test(attribute) || /href$|^on/i.test(attribute)) {
|
|
316
|
+
throw refused('an animation that rewrites a link or an event handler');
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
for (const reading of [decoded, css]) {
|
|
320
|
+
for (const match of reading.matchAll(CSS_URL)) {
|
|
321
|
+
if (!allowedTarget(squash(match[2] ?? match[3] ?? ''))) {
|
|
322
|
+
throw new SvgError('The SVG’s styles load something outside the file. Everything it uses has to be inside the SVG.');
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
if (copies(plain.slice(start, end)) > MAX_COPIES) {
|
|
328
|
+
throw refused('more nested copies (<use>, markers, masks or clip paths) than a browser can draw');
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// An entity XML doesn't define stops the whole drawing from rendering.
|
|
332
|
+
const entity = plain.match(UNDEFINED_ENTITY);
|
|
333
|
+
if (entity) {
|
|
334
|
+
throw new SvgError(`The SVG uses ${entity[0]}, which SVG doesn’t define. Write the character itself instead.`);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// The root tag needs a namespace to render as an image, and a viewBox to
|
|
338
|
+
// scale into the frame. Models often leave out one or both.
|
|
339
|
+
let root = svg.slice(0, rootEnd);
|
|
340
|
+
if (!/\sxmlns\s*=/.test(root)) root += ' xmlns="http://www.w3.org/2000/svg"';
|
|
341
|
+
if (/\bxlink:/.test(svg) && !/\sxmlns:xlink\s*=/.test(root)) root += ' xmlns:xlink="http://www.w3.org/1999/xlink"';
|
|
342
|
+
if (!/\sviewBox\s*=/i.test(root)) {
|
|
343
|
+
const width = root.match(/\swidth\s*=\s*["']\s*([\d.]+)(?:px)?\s*["']/i);
|
|
344
|
+
const height = root.match(/\sheight\s*=\s*["']\s*([\d.]+)(?:px)?\s*["']/i);
|
|
345
|
+
if (width && height) root += ` viewBox="0 0 ${Number(width[1])} ${Number(height[1])}"`;
|
|
346
|
+
}
|
|
347
|
+
svg = root + svg.slice(rootEnd);
|
|
348
|
+
|
|
349
|
+
return { svg, bytes: new TextEncoder().encode(svg).length };
|
|
350
|
+
}
|