File size: 11,592 Bytes
2a1c46d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// ---------------------------------------------------------------------------
// @nodable/entities — TypeScript declarations
// ---------------------------------------------------------------------------

/** A function-based entity replacement value (used for numeric refs). */
export type EntityValFn = (match: string, captured: string, ...rest: unknown[]) => string;

// ---------------------------------------------------------------------------
// Entity registration hook
// ---------------------------------------------------------------------------

/**
 * Actions returned by `onExternalEntity` / `onInputEntity` hooks.
 * Use the `ENTITY_ACTION` constant object instead of raw strings to avoid typos.
 *
 * - `'allow'` — register and expand the entity normally
 * - `'block'` — silently skip the entity (not registered, treated as unknown)
 * - `'throw'` — abort registration with an error
 */
export type EntityHookAction = 'allow' | 'block' | 'throw';

/**
 * Immutable constant bag for entity registration hook return values.
 *
 * @example
 * import { ENTITY_ACTION } from '@nodable/entities';
 * const dec = new EntityDecoder({
 *   onInputEntity: (_name, _value) => ENTITY_ACTION.BLOCK,
 * });
 */
export const ENTITY_ACTION: Readonly<{
  /** Register and expand the entity normally. */
  ALLOW: 'allow';
  /** Silently skip this entity — it will not be registered. */
  BLOCK: 'block';
  /** Throw an error, aborting entity registration. */
  THROW: 'throw';
}>;

/**
 * Callback signature for `onExternalEntity` and `onInputEntity` hooks.
 *
 * Called once per entity **at registration time** (not at decode time).
 * Return `ENTITY_ACTION.ALLOW` (or `'allow'`) to accept, `ENTITY_ACTION.BLOCK`
 * to silently skip, or `ENTITY_ACTION.THROW` to raise an error.
 *
 * @param name  — the entity name without `&` / `;`, e.g. `"brand"`
 * @param value — the resolved string value after any `{regex,val}` unwrapping
 */
export type EntityRegistrationHook = (name: string, value: string) => EntityHookAction;

// ---------------------------------------------------------------------------
// Encoder options
// ---------------------------------------------------------------------------

export interface EntityEncoderOptions {
  /**
   * Whether to encode XML unsafe characters: `&`, `<`, `>`, `"`, `'`.
   * @default true
   */
  encodeXmlSafe?: boolean;

  /**
   * Whether to encode non‑ASCII characters (e.g. `é` → `&eacute;`) using the
   * built‑in named entity trie.
   * @default true
   */
  encodeAllNamed?: boolean;

  /**
   * Maximum number of replacements performed **cumulatively** across all
   * `encode()` calls. `0` means unlimited.
   *
   * Use `reset()` to reset the internal counter.
   * @default 0
   */
  maxReplacements?: number;
}

// ---------------------------------------------------------------------------
// EntityEncoder class
// ---------------------------------------------------------------------------

/**
 * High‑performance encoder that replaces characters with XML/HTML entities.
 *
 * - Escapes XML unsafe characters (`&`, `<`, `>`, `"`, `'`) when `encodeXmlSafe` is true.
 * - Replaces non‑ASCII characters (e.g. `é`, `©`) with named entities using
 *   a compact trie‑based lookup when `encodeAllNamed` is true.
 * - Supports a cumulative replacement limit (`maxReplacements`) that persists
 *   across multiple `encode()` calls until `reset()` is called.
 *
 * @example
 * const encoder = new EntityEncoder({ encodeXmlSafe: true, encodeAllNamed: true });
 * encoder.encode('<foo>');   // "&lt;foo&gt;"
 * encoder.encode('© 2025');  // "&copy; 2025"
 *
 * // With limit
 * const limited = new EntityEncoder({ maxReplacements: 2 });
 * limited.encode('<>&');     // "&lt;&gt;&"  (third replacement omitted)
 * limited.reset();           // reset counter
 */
export class EntityEncoder {
  constructor(options?: EntityEncoderOptions);

  /**
   * Encode a string by replacing XML‑unsafe characters and (optionally)
   * non‑ASCII characters with named entities.
   *
   * If `maxReplacements` is set and the cumulative limit has been reached,
   * the input string is returned unchanged.
   *
   * @returns Encoded string (may be identical to input if no replacements needed
   *          or the limit has been exhausted).
   */
  encode(str: string): string;

  /**
   * Reset the internal replacement counter.
   * Does **not** change `encodeXmlSafe`, `encodeAllNamed`, or `maxReplacements`.
   */
  reset(): void;
}

// ---------------------------------------------------------------------------
// Constructor options for EntityDecoder (existing)
// ---------------------------------------------------------------------------

/**
 * Controls which entity categories count toward the expansion limits.
 *
 * - `'external'` — only untrusted / injected entities (default)
 * - `'base'`     — only built‑in XML entities + user‑supplied `namedEntities`
 * - `'all'`      — all entities regardless of tier
 * - `string[]`   — explicit combination, e.g. `['external', 'base']`
 */
export type ApplyLimitsTo = 'external' | 'base' | 'all' | Array<'external' | 'base'>;

export interface EntityDecoderLimitOptions {
  /**
   * Maximum number of entity references expanded **per document**.
   * `0` means unlimited.
   * @default 0
   */
  maxTotalExpansions?: number;

  /**
   * Maximum number of characters **added** by entity expansion per document.
   * `0` means unlimited.
   * @default 0
   */
  maxExpandedLength?: number;

  /**
   * Which entity tiers count toward the expansion limits.
   *
   * - `'external'` (default) – only input/runtime + persistent external entities
   * - `'base'`               – only built‑in XML + `namedEntities`
   * - `'all'`                – every entity regardless of tier
   * - `string[]`             – explicit combination, e.g. `['external', 'base']`
   *
   * @default 'external'
   */
  applyLimitsTo?: ApplyLimitsTo;
}

export interface EntityDecoderNCROptions {
  /**
   * XML version used for NCR classification.
   * @default 1.0
   */
  xmlVersion?: 1.0 | 1.1;

  /**
   * Base action for all numeric references.
   * @default 'allow'
   */
  onNCR?: 'allow' | 'leave' | 'remove' | 'throw';

  /**
   * Action for null NCR (U+0000).
   * @default 'remove'
   */
  nullNCR?: 'remove' | 'throw';
}

export interface EntityDecoderOptions {
  /**
   * Extra named entities merged into the **base map** (trusted, counts as `'base'` tier).
   * These are combined with the built‑in XML entities (`lt`, `gt`, `quot`, `apos`).
   * Values containing `&` are silently skipped to prevent recursive expansion.
   *
   * @default null
   */
  namedEntities?: Record<string, string | { regex: RegExp; val: string | EntityValFn }> | null;


  /**
   * Hook called once on the fully decoded string (after all replacements).
   *
   * - Receives `(resolved, original)` and **must return a string**.
   * - To reject expansion, return `original`.
   * - To sanitize, return a cleaned version of `resolved`.
   *
   * @example
   * postCheck: (resolved, original) =>
   *   /<[a-z]/i.test(resolved) ? original : resolved
   */
  postCheck?: ((resolved: string, original: string) => string) | null;

  /**
   * Whether numeric character references (`&#NNN;`, `&#xHH;`) are allowed.
   * @default true
   */
  numericAllowed?: boolean;

  /**
   * Array of entity names or numeric references to leave unexpanded.
   * @default []
   */
  leave?: string[];

  /**
   * Array of entity names or numeric references to remove.
   * @default []
   */
  remove?: string[];

  /**
   * Security limits for entity expansion.
   */
  limit?: EntityDecoderLimitOptions;

  /**
   * Numeric Character Reference (NCR) policy.
   */
  ncr?: EntityDecoderNCROptions;

  /**
   * Hook called once **at registration time** for each entity passed to
   * `setExternalEntities()` or `addExternalEntity()`.
   *
   * - `'allow'` (or `ENTITY_ACTION.ALLOW`) — register the entity normally (default)
   * - `'block'` (or `ENTITY_ACTION.BLOCK`) — silently skip; the entity is not stored
   * - `'throw'` (or `ENTITY_ACTION.THROW`) — abort registration with an `Error`
   *
   * The hook receives the entity name (without `&`/`;`) and the resolved string
   * value. It is **not** called during `decode()` — only when entities are added.
   *
   * @example
   * const dec = new EntityDecoder({
   *   onExternalEntity: (name, value) =>
   *     DANGEROUS_NAMES.has(name) ? ENTITY_ACTION.BLOCK : ENTITY_ACTION.ALLOW,
   * });
   */
  onExternalEntity?: EntityRegistrationHook | null;

  /**
   * Hook called once **at registration time** for each entity passed to
   * `addInputEntities()`.
   *
   * Follows the same `'allow' | 'block' | 'throw'` contract as `onExternalEntity`.
   *
   * @example
   * const dec = new EntityDecoder({
   *   // Block all input / DOCTYPE entities unconditionally
   *   onInputEntity: () => ENTITY_ACTION.BLOCK,
   * });
   */
  onInputEntity?: EntityRegistrationHook | null;
}

// ---------------------------------------------------------------------------
// EntityDecoder class (default export)
// ---------------------------------------------------------------------------

/**
 * Single‑pass, zero‑regex entity decoder for XML/HTML content.
 *
 * ## Entity lookup priority (highest → lowest)
 * 1. **input / runtime** – injected via `addInputEntities()` (DOCTYPE per document)
 * 2. **persistent external** – set via `setExternalEntities()` / `addExternalEntity()`
 * 3. **base map** – built‑in XML entities + user‑supplied `namedEntities`
 *
 * Numeric references (`&#NNN;`, `&#xHH;`) are resolved directly and count as the `'base'` tier.
 *
 * @example
 * const decoder = new EntityDecoder({
 *   namedEntities: COMMON_HTML,
 *   maxTotalExpansions: 100
 * });
 * decoder.setExternalEntities({ brand: 'Acme' });
 *
 * decoder.addInputEntities({ version: '1.0' });
 * decoder.decode('&brand; v&version; &lt;'); // 'Acme v1.0 <'
 *
 * decoder.reset(); // clears input entities + counters, keeps external entities
 */
export default class EntityDecoder {
  constructor(options?: EntityDecoderOptions);

  setExternalEntities(
    map: Record<string, string | { regex: RegExp; val: string | EntityValFn }>
  ): void;

  addExternalEntity(key: string, value: string): void;

  addInputEntities(
    map: Record<
      string,
      | string
      | { regx: RegExp; val: string | EntityValFn }
      | { regex: RegExp; val: string | EntityValFn }
    >
  ): void;

  reset(): this;

  decode(str: string): string;
}

// ---------------------------------------------------------------------------
// Named entity group exports (for use with `namedEntities` option)
// ---------------------------------------------------------------------------

export const COMMON_HTML: Record<string, string>;
export const ALL_ENTITIES: Record<string, string>;
export const XML: Record<string, string>;
export const BASIC_LATIN: Record<string, string>;
export const LATIN_ACCENTS: Record<string, string>;
export const LATIN_EXTENDED: Record<string, string>;
export const GREEK: Record<string, string>;
export const CYRILLIC: Record<string, string>;
export const MATH: Record<string, string>;
export const MATH_ADVANCED: Record<string, string>;
export const ARROWS: Record<string, string>;
export const SHAPES: Record<string, string>;
export const PUNCTUATION: Record<string, string>;
export const CURRENCY: Record<string, string>;
export const FRACTIONS: Record<string, string>;
export const MISC_SYMBOLS: Record<string, string>;