@knowvah/dot-engine 2.6.10 → 2.6.11
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/dist/common/html-entities.d.ts +13 -5
- package/dist/common/html-entities.d.ts.map +1 -1
- package/dist/common/htmltable-lex-chardata.d.ts +9 -0
- package/dist/common/htmltable-lex-chardata.d.ts.map +1 -1
- package/dist/index.js +57 -21
- package/dist/index.js.map +2 -2
- package/dist/render.js +56 -20
- package/dist/render.js.map +2 -2
- package/package.json +1 -1
- package/src/common/html-entities.ts +77 -25
- package/src/common/htmltable-lex-chardata.ts +42 -8
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@knowvah/dot-engine",
|
|
3
|
-
"version": "2.6.
|
|
3
|
+
"version": "2.6.11",
|
|
4
4
|
"description": "Faithful TypeScript port of Graphviz (dot, neato, fdp, sfdp, circo, twopi, osage, patchwork) — no C: no native binary, no WASM. Oracle-verified against the C implementation, runs in the browser.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"graphviz",
|
|
@@ -260,40 +260,92 @@ export const HTML_ENTITIES: Readonly<Record<string, number>> = {
|
|
|
260
260
|
"zwnj": 8204,
|
|
261
261
|
};
|
|
262
262
|
|
|
263
|
-
/**
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
263
|
+
/**
|
|
264
|
+
* Longest name htmlEntity looks up: its scan for the `;` stops after this
|
|
265
|
+
* many characters.
|
|
266
|
+
* @see lib/common/entities.h:ENTITY_NAME_LENGTH_MAX
|
|
267
|
+
*/
|
|
268
|
+
const ENTITY_NAME_LENGTH_MAX = 8;
|
|
267
269
|
|
|
268
|
-
/**
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
270
|
+
/** htmlEntity's result: the value (0 for none) and where scanning resumes. */
|
|
271
|
+
interface EntityScan {
|
|
272
|
+
value: number;
|
|
273
|
+
next: number;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* The `#` branch of htmlEntity, `at` on the `#`. It reads up to six decimal
|
|
278
|
+
* or five hex digits (`x` or `X`); the `;` must come within the first eight
|
|
279
|
+
* characters, else nothing is consumed. A `;` right after the digits, even
|
|
280
|
+
* none, is consumed with value `n`, so `&#;` keeps its `&` and loses `#;`.
|
|
281
|
+
* @see lib/common/utils.c:htmlEntity
|
|
282
|
+
*/
|
|
283
|
+
function numericEntity(s: string, at: number): EntityScan {
|
|
284
|
+
const hex = s[at + 1] === 'x' || s[at + 1] === 'X';
|
|
285
|
+
const digit = hex ? /^[0-9A-Fa-f]$/ : /^[0-9]$/;
|
|
286
|
+
let n = 0;
|
|
287
|
+
let i = hex ? 2 : 1;
|
|
288
|
+
for (; i < 8; i++) {
|
|
289
|
+
const c = s[at + i] ?? '';
|
|
290
|
+
if (!digit.test(c)) break;
|
|
291
|
+
n = n * (hex ? 16 : 10) + parseInt(c, 16);
|
|
292
|
+
}
|
|
293
|
+
if (i < 8 && s[at + i] === ';') return { value: n, next: at + i + 1 };
|
|
294
|
+
return { value: 0, next: at };
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* The name branch of htmlEntity, `at` on the first name character: a `;`
|
|
299
|
+
* within ENTITY_NAME_LENGTH_MAX characters ends a name looked up in
|
|
300
|
+
* entities.h (case-sensitive; no `apos`).
|
|
301
|
+
* @see lib/common/utils.c:htmlEntity
|
|
302
|
+
*/
|
|
303
|
+
function namedEntity(s: string, at: number): EntityScan {
|
|
304
|
+
for (let i = 0; i < ENTITY_NAME_LENGTH_MAX; i++) {
|
|
305
|
+
const c = s[at + i];
|
|
306
|
+
if (c === undefined || c === '\0') break;
|
|
307
|
+
if (c === ';') {
|
|
308
|
+
const key = s.slice(at, at + i);
|
|
309
|
+
if (Object.hasOwn(HTML_ENTITIES, key)) return { value: HTML_ENTITIES[key]!, next: at + i + 1 };
|
|
310
|
+
break;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
return { value: 0, next: at };
|
|
273
314
|
}
|
|
274
315
|
|
|
275
316
|
/**
|
|
276
|
-
*
|
|
277
|
-
*
|
|
278
|
-
* plus the full HTML 4 table (Symbol etc.). @see lib/common/utils.c:htmlEntity
|
|
317
|
+
* Check for an HTML entity at `at`, the character after an `&`.
|
|
318
|
+
* @see lib/common/utils.c:htmlEntity
|
|
279
319
|
*/
|
|
280
|
-
function
|
|
281
|
-
|
|
282
|
-
return BASIC_ENTITIES[body] ?? HTML_ENTITIES[body] ?? null;
|
|
320
|
+
function htmlEntity(s: string, at: number): EntityScan {
|
|
321
|
+
return s[at] === '#' ? numericEntity(s, at) : namedEntity(s, at);
|
|
283
322
|
}
|
|
284
323
|
|
|
285
324
|
/**
|
|
286
|
-
*
|
|
287
|
-
*
|
|
288
|
-
* Latin-1 inputs are decoded at the
|
|
289
|
-
* survey's render-one), so this
|
|
290
|
-
*
|
|
325
|
+
* Substitute entities like `{` and `&` with their characters: C's
|
|
326
|
+
* make_label UTF-8 branch. An `&` whose entity has no value stays (scanning
|
|
327
|
+
* resumes where htmlEntity left it). Latin-1 inputs are decoded at the
|
|
328
|
+
* byte->string boundary by the caller (the survey's render-one), so this
|
|
329
|
+
* covers them too, as does latin1ToUTF8's identical entity handling.
|
|
330
|
+
*
|
|
331
|
+
* C appends a value's bytes itself: below 0x7F one byte, below 0x7FF two,
|
|
332
|
+
* else three, truncated to `char`. For every value whose bytes are valid
|
|
333
|
+
* UTF-8 that is the character itself. 0x7F, 0x7FF, surrogates and values
|
|
334
|
+
* above 0xFFFF come out as invalid UTF-8, which a JS string cannot hold; the
|
|
335
|
+
* port keeps the character there (documented divergence).
|
|
336
|
+
* @see lib/common/utils.c:htmlEntityUTF8
|
|
337
|
+
* @see lib/common/utils.c:latin1ToUTF8
|
|
291
338
|
* @see lib/common/labels.c:make_label (CHAR_LATIN1 vs default)
|
|
292
339
|
*/
|
|
293
340
|
export function htmlEntityUTF8(s: string): string {
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
341
|
+
let amp = s.indexOf('&');
|
|
342
|
+
if (amp < 0) return s;
|
|
343
|
+
let out = '';
|
|
344
|
+
let from = 0;
|
|
345
|
+
for (; amp >= 0; amp = s.indexOf('&', from)) {
|
|
346
|
+
const e = htmlEntity(s, amp + 1);
|
|
347
|
+
out += s.slice(from, amp) + (e.value ? String.fromCodePoint(e.value) : '&');
|
|
348
|
+
from = e.next;
|
|
349
|
+
}
|
|
350
|
+
return out + s.slice(from);
|
|
299
351
|
}
|
|
@@ -12,7 +12,8 @@
|
|
|
12
12
|
* @see lib/common/htmllex.c:characterData
|
|
13
13
|
*/
|
|
14
14
|
|
|
15
|
-
import {
|
|
15
|
+
import { HTML_ENTITIES } from './html-entities.js';
|
|
16
|
+
import { charRefNumber } from './htmltable-lex-attvalue.js';
|
|
16
17
|
import type { TagChunk } from './htmltable-lex-expat.js';
|
|
17
18
|
import type { Token } from './htmltable-types.js';
|
|
18
19
|
|
|
@@ -77,15 +78,48 @@ function cleanText(decoded: string) {
|
|
|
77
78
|
return decoded.replace(/[\x00-\x1f]/g, '');
|
|
78
79
|
}
|
|
79
80
|
|
|
81
|
+
/** expat's predefined entities: no DTD declares others. */
|
|
82
|
+
const XML_PREDEFINED: Readonly<Record<string, string>> = { lt: '<', gt: '>', amp: '&', quot: '"', apos: "'" };
|
|
83
|
+
|
|
84
|
+
/** Shortest and longest name scanEntity looks up. @see lib/common/utils.c:scanEntity */
|
|
85
|
+
const SCAN_ENTITY_MIN = 2;
|
|
86
|
+
const SCAN_ENTITY_MAX = 8;
|
|
87
|
+
|
|
88
|
+
const TEXT_REF = /&(#x[0-9A-Fa-f]+|#[0-9]+|[^&;]+);/g;
|
|
89
|
+
|
|
90
|
+
/** One reference of a text run, as scanEntity then expat leave it. */
|
|
91
|
+
function textRefValue(ref: string, body: string): string {
|
|
92
|
+
if (body[0] === '#') {
|
|
93
|
+
const v = charRefNumber(ref);
|
|
94
|
+
return v < 0 ? ref : String.fromCodePoint(v);
|
|
95
|
+
}
|
|
96
|
+
const n = body.length;
|
|
97
|
+
if (n >= SCAN_ENTITY_MIN && n <= SCAN_ENTITY_MAX && Object.hasOwn(HTML_ENTITIES, body)) {
|
|
98
|
+
return String.fromCodePoint(HTML_ENTITIES[body]!);
|
|
99
|
+
}
|
|
100
|
+
return Object.hasOwn(XML_PREDEFINED, body) ? XML_PREDEFINED[body]! : ref;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* The characters expat hands characterData for a text run: scanEntity first
|
|
105
|
+
* rewrites each entities.h name to `&#N;` (its `&` only when no `#` follows),
|
|
106
|
+
* then expat decodes character references, of any length, and its five
|
|
107
|
+
* predefined entities. Runs expat rejects never get here (textRefError).
|
|
108
|
+
* @see lib/common/htmllex.c:findNext
|
|
109
|
+
* @see lib/common/utils.c:scanEntity
|
|
110
|
+
*/
|
|
111
|
+
export function decodeTextRefs(raw: string): string {
|
|
112
|
+
return raw.indexOf('&') < 0 ? raw : raw.replace(TEXT_REF, textRefValue);
|
|
113
|
+
}
|
|
114
|
+
|
|
80
115
|
/** The text token of a text run, if anything is left after cleaning. */
|
|
81
116
|
export function textTokens(raw: string): Token[] {
|
|
82
|
-
//
|
|
83
|
-
//
|
|
84
|
-
//
|
|
85
|
-
//
|
|
86
|
-
//
|
|
87
|
-
|
|
88
|
-
return charDataTokens(htmlEntityUTF8(raw));
|
|
117
|
+
// C's HTML lexer resolves entities via scanEntity and expat during
|
|
118
|
+
// tokenization (htmllex.c), so both sizing and emit see the decoded text.
|
|
119
|
+
// Skipping this measured the raw "[el..." (9-10 chars) instead of
|
|
120
|
+
// "[el..." (6 chars), inflating edge-label vnodes and shifting layout
|
|
121
|
+
// (corpus 1949: +18.7px).
|
|
122
|
+
return charDataTokens(decodeTextRefs(raw));
|
|
89
123
|
}
|
|
90
124
|
|
|
91
125
|
/** characterData on what expat decoded: a T_string when any character survives. */
|