@tradik/xslt-processor 1.0.3 → 1.3.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.
Files changed (131) hide show
  1. package/LICENSE.md +1 -1
  2. package/README.md +110 -520
  3. package/bin/lib/decode.js +15 -0
  4. package/bin/lib/dom.js +177 -0
  5. package/bin/lib/loaders.js +127 -0
  6. package/bin/lib/options.js +131 -0
  7. package/bin/lib/output.js +114 -0
  8. package/bin/lib/paths.js +186 -0
  9. package/bin/lib/transform.js +206 -0
  10. package/bin/xslt.js +73 -168
  11. package/dist/xslt-processor.browser.js +9564 -1585
  12. package/dist/xslt-processor.browser.js.map +4 -4
  13. package/dist/xslt-processor.browser.min.js +13 -2
  14. package/dist/xslt-processor.browser.min.js.map +4 -4
  15. package/dist/xslt-processor.cjs +9572 -1586
  16. package/dist/xslt-processor.cjs.map +4 -4
  17. package/dist/xslt-processor.d.cts +658 -0
  18. package/dist/xslt-processor.d.ts +459 -12
  19. package/dist/xslt-processor.js +9546 -1582
  20. package/dist/xslt-processor.js.map +4 -4
  21. package/package.json +71 -20
  22. package/src/XSLTProcessor.js +494 -48
  23. package/src/async/abort.js +63 -0
  24. package/src/async/documentUris.js +128 -0
  25. package/src/async/loaders.js +134 -0
  26. package/src/async/preload.js +159 -0
  27. package/src/async/processor.js +206 -0
  28. package/src/async/stream.js +125 -0
  29. package/src/bridge/engine.js +221 -0
  30. package/src/bridge/loader.js +78 -0
  31. package/src/bridge/results.js +75 -0
  32. package/src/bridge/version.js +63 -0
  33. package/src/index.js +26 -8
  34. package/src/io/decode.js +140 -0
  35. package/src/io/readSource.js +167 -0
  36. package/src/xpath/axes.js +562 -0
  37. package/src/xpath/documentOrder.js +270 -0
  38. package/src/xpath/evaluator.js +518 -357
  39. package/src/xpath/index.js +8 -2
  40. package/src/xpath/namespaceNodes.js +172 -0
  41. package/src/xpath/nodeSetFunctions.js +169 -0
  42. package/src/xpath/parser.js +30 -5
  43. package/src/xpath/strings.js +183 -0
  44. package/src/xpath/tokenizer.js +37 -23
  45. package/src/xslt/attributeSets.js +95 -0
  46. package/src/xslt/avt.js +103 -0
  47. package/src/xslt/computedNames.js +91 -0
  48. package/src/xslt/copying.js +212 -0
  49. package/src/xslt/declarationNames.js +80 -0
  50. package/src/xslt/domParsing.js +95 -0
  51. package/src/xslt/elements.js +57 -0
  52. package/src/xslt/engine/bindings.js +195 -0
  53. package/src/xslt/engine/context.js +105 -0
  54. package/src/xslt/engine/controlFlow.js +145 -0
  55. package/src/xslt/engine/copyInstructions.js +133 -0
  56. package/src/xslt/engine/declarations.js +233 -0
  57. package/src/xslt/engine/functionSupport.js +103 -0
  58. package/src/xslt/engine/methods.js +33 -0
  59. package/src/xslt/engine/nodeConstruction.js +187 -0
  60. package/src/xslt/engine/numbering.js +104 -0
  61. package/src/xslt/engine/outputDeclaration.js +77 -0
  62. package/src/xslt/engine/sequenceConstructor.js +228 -0
  63. package/src/xslt/engine/stylesheetLoading.js +208 -0
  64. package/src/xslt/engine/templateInvocation.js +253 -0
  65. package/src/xslt/engine/templateRules.js +243 -0
  66. package/src/xslt/engine/textInstructions.js +171 -0
  67. package/src/xslt/engine/topLevel.js +130 -0
  68. package/src/xslt/engine/transformation.js +263 -0
  69. package/src/xslt/engine/workStack.js +245 -0
  70. package/src/xslt/engine.js +184 -1736
  71. package/src/xslt/exslt/arguments.js +99 -0
  72. package/src/xslt/exslt/calendar.js +120 -0
  73. package/src/xslt/exslt/common.js +44 -0
  74. package/src/xslt/exslt/dateCalc.js +261 -0
  75. package/src/xslt/exslt/dateFormat.js +150 -0
  76. package/src/xslt/exslt/dateParse.js +265 -0
  77. package/src/xslt/exslt/dates.js +259 -0
  78. package/src/xslt/exslt/duration.js +207 -0
  79. package/src/xslt/exslt/dynamic.js +59 -0
  80. package/src/xslt/exslt/index.js +59 -0
  81. package/src/xslt/exslt/math.js +177 -0
  82. package/src/xslt/exslt/sets.js +96 -0
  83. package/src/xslt/exslt/stringOps.js +163 -0
  84. package/src/xslt/exslt/strings.js +147 -0
  85. package/src/xslt/exslt/uri.js +92 -0
  86. package/src/xslt/formatNumber.js +233 -0
  87. package/src/xslt/forwardsCompatible.js +75 -0
  88. package/src/xslt/functions.js +270 -0
  89. package/src/xslt/index.js +38 -1
  90. package/src/xslt/keys.js +164 -0
  91. package/src/xslt/literalResult.js +223 -0
  92. package/src/xslt/matchScope.js +116 -0
  93. package/src/xslt/number.js +271 -0
  94. package/src/xslt/numberFormat.js +253 -0
  95. package/src/xslt/outputNames.js +58 -0
  96. package/src/xslt/patternCompiler.js +175 -0
  97. package/src/xslt/patterns.js +324 -0
  98. package/src/xslt/qname.js +90 -0
  99. package/src/xslt/resultDocument.js +98 -0
  100. package/src/xslt/resultNamespaces.js +219 -0
  101. package/src/xslt/resultTree.js +211 -0
  102. package/src/xslt/serializer/baseWriter.js +390 -0
  103. package/src/xslt/serializer/chunks.js +120 -0
  104. package/src/xslt/serializer/constants.js +92 -0
  105. package/src/xslt/serializer/encoding.js +327 -0
  106. package/src/xslt/serializer/escape.js +135 -0
  107. package/src/xslt/serializer/frames.js +168 -0
  108. package/src/xslt/serializer/htmlDoctype.js +102 -0
  109. package/src/xslt/serializer/htmlEntities.js +77 -0
  110. package/src/xslt/serializer/htmlSerializer.js +239 -0
  111. package/src/xslt/serializer/indent.js +51 -0
  112. package/src/xslt/serializer/namespaces.js +68 -0
  113. package/src/xslt/serializer/rawText.js +41 -0
  114. package/src/xslt/serializer/settings.js +179 -0
  115. package/src/xslt/serializer/textSerializer.js +77 -0
  116. package/src/xslt/serializer/xhtmlDocument.js +103 -0
  117. package/src/xslt/serializer/xmlSerializer.js +227 -0
  118. package/src/xslt/serializer.js +90 -0
  119. package/src/xslt/sort.js +151 -0
  120. package/src/xslt/spaceNameTests.js +115 -0
  121. package/src/xslt/stylesheetChecks.js +206 -0
  122. package/src/xslt/stylesheetNamespaces.js +266 -0
  123. package/src/xslt/templatePriority.js +45 -0
  124. package/src/xslt/uri.js +68 -0
  125. package/src/xslt/variables.js +152 -0
  126. package/src/xslt/whitespace.js +200 -0
  127. package/LICENSE +0 -29
  128. package/src/XSLTProcessor.test.js +0 -930
  129. package/src/xpath/evaluator.test.js +0 -1852
  130. package/src/xpath/tokenizer.test.js +0 -224
  131. package/src/xslt/engine.test.js +0 -3130
@@ -1,18 +1,189 @@
1
1
  /**
2
- * @cv-xslt/xslt-processor - TypeScript Declarations
2
+ * @tradik/xslt-processor - TypeScript Declarations
3
+ *
4
+ * Source of dist/xslt-processor.d.ts and dist/xslt-processor.d.cts, copied
5
+ * by scripts/build.js. Keep it in step with the exports of src/index.js.
3
6
  */
4
7
 
8
+ /**
9
+ * Loader used to resolve xsl:import and xsl:include references.
10
+ *
11
+ * The loader is synchronous: it must return the external stylesheet as a
12
+ * Document or as an XML string (which is parsed automatically).
13
+ *
14
+ * @param href - The resolved URI of the referenced stylesheet
15
+ * @param baseUri - The URI of the importing/including stylesheet, if known
16
+ */
17
+ export type StylesheetLoader = (
18
+ href: string,
19
+ baseUri?: string,
20
+ ) => Document | string;
21
+
22
+ /**
23
+ * Loader used by the XSLT document() function.
24
+ *
25
+ * The loader is synchronous: it must return the document as a Document, as an
26
+ * XML string (which is parsed automatically) or null when it is unavailable
27
+ * (document() then evaluates to an empty node-set).
28
+ *
29
+ * @param uri - The resolved URI of the requested document
30
+ * @param baseUri - The base URI the reference was resolved against, if known
31
+ */
32
+ export type DocumentLoader = (
33
+ uri: string,
34
+ baseUri?: string,
35
+ ) => Document | string | null;
36
+
37
+ /**
38
+ * Anything with a DOMParser-like parseFromString method (a browser, jsdom or
39
+ * xmldom DOMParser instance).
40
+ */
41
+ export interface DomParserLike {
42
+ parseFromString(text: string, type: string): Document;
43
+ }
44
+
45
+ /**
46
+ * What an asynchronous loader may resolve to: a Document, markup, bytes
47
+ * (decoded from the byte order mark or XML declaration, else UTF-8), a fetch
48
+ * Response, or null when the resource is unavailable.
49
+ */
50
+ export type AsyncLoaderResult =
51
+ Document | string | Uint8Array | ArrayBuffer | Response | null;
52
+
53
+ /**
54
+ * Asynchronous loader of stylesheet modules and document() documents.
55
+ * @param uri - The resolved URI
56
+ * @param baseUri - The URI it was resolved against, if known
57
+ * @param init - Holds the AbortSignal of the call
58
+ */
59
+ export type AsyncLoader = (
60
+ uri: string,
61
+ baseUri: string | undefined,
62
+ init: { signal?: AbortSignal },
63
+ ) => AsyncLoaderResult | Promise<AsyncLoaderResult>;
64
+
65
+ /**
66
+ * Input of the asynchronous API: a node, markup, bytes, or a stream of
67
+ * markup/bytes. Streams are read to their end before parsing (XSLT 1.0 needs
68
+ * the whole source tree).
69
+ */
70
+ export type AsyncSource =
71
+ | Node
72
+ | string
73
+ | Uint8Array
74
+ | ArrayBuffer
75
+ | ReadableStream<string | Uint8Array | ArrayBuffer>
76
+ | AsyncIterable<string | Uint8Array | ArrayBuffer>;
77
+
78
+ /** Options of transformToStream. */
79
+ export interface StreamOptions {
80
+ /** Cancels: the stream is errored with the abort reason. */
81
+ signal?: AbortSignal;
82
+ /** Chunk size in UTF-16 code units (default 16384); Infinity for one chunk. */
83
+ chunkSize?: number;
84
+ }
85
+
86
+ /** Options of importStylesheetAsync. */
87
+ export interface ImportAsyncOptions {
88
+ /** Loader of xsl:import/xsl:include modules; the global fetch by default. */
89
+ loader?: AsyncLoader;
90
+ /** Loader of literal document() documents; `loader` by default. */
91
+ documentLoader?: AsyncLoader;
92
+ /** Cancels loading. */
93
+ signal?: AbortSignal;
94
+ }
95
+
96
+ /** Options of transformAsync. */
97
+ export interface TransformAsyncOptions {
98
+ /** Cancels loading and reading. */
99
+ signal?: AbortSignal;
100
+ /** A stylesheet to import first with importStylesheetAsync. */
101
+ stylesheet?: AsyncSource;
102
+ /** The URI of that stylesheet. */
103
+ stylesheetUri?: string;
104
+ /** Loader of its xsl:import/xsl:include modules (fetch by default). */
105
+ fetchStylesheet?: AsyncLoader;
106
+ /** Loader of the literal document() documents of the stylesheet. */
107
+ fetchDocument?: AsyncLoader;
108
+ }
109
+
110
+ /** The `xsltVersion` option of XSLTProcessor. */
111
+ export type XsltVersionMode = "1.0" | "auto";
112
+
5
113
  /**
6
114
  * XSLTProcessor - Applies XSLT stylesheet transformations to XML documents.
7
115
  */
8
116
  export class XSLTProcessor {
9
- constructor();
117
+ constructor(options?: {
118
+ /**
119
+ * @deprecated Let unprefixed name tests (`item`, `@a`) also match nodes
120
+ * in a namespace, as before 1.2.0. XPath 1.0 and Chrome match only nodes
121
+ * in no namespace.
122
+ */
123
+ legacyNameTests?: boolean;
124
+ /** Allow EXSLT `dyn:evaluate()`; it evaluates XPath built from data. */
125
+ enableDynamicEvaluate?: boolean;
126
+ /** Clock for EXSLT current-time functions (reproducible output). */
127
+ clock?: () => Date;
128
+ /**
129
+ * Deepest nesting of template instantiations; deeper recursion throws
130
+ * "Template recursion too deep" (default 3000, as in libxslt).
131
+ */
132
+ maxTemplateDepth?: number;
133
+ /**
134
+ * "1.0" (default): every stylesheet runs with the XSLT 1.0 engine, a
135
+ * version="2.0" one in forwards-compatible mode (as in Chrome).
136
+ * "auto": a stylesheet whose version is 2.0 or more runs with the
137
+ * optional peer dependency @tradik/xslt3, loaded with import() by the
138
+ * asynchronous API or by {@link XSLTProcessor.preload}.
139
+ * @throws RangeError for any other value
140
+ */
141
+ xsltVersion?: XsltVersionMode;
142
+ });
143
+
144
+ /**
145
+ * Load @tradik/xslt3 so that the synchronous API of processors created
146
+ * with `xsltVersion: "auto"` can run XSLT 2.0/3.0 stylesheets (non-W3C).
147
+ * Rejects with "Cannot load @tradik/xslt3: install @tradik/xslt3 to run
148
+ * XSLT 2.0/3.0 stylesheets" when the package is missing.
149
+ * @param version - "3.0" (default) or "2.0"
150
+ */
151
+ static preload(version?: "2.0" | "3.0"): Promise<void>;
152
+
153
+ /**
154
+ * The underlying XSLT engine (advanced usage).
155
+ * Null until a stylesheet has been imported. With `xsltVersion: "auto"`
156
+ * and an XSLT 2.0/3.0 stylesheet it is the @tradik/xslt3 bridge engine,
157
+ * which has `outputSettings` but not the other XsltEngine members.
158
+ */
159
+ readonly engine: XsltEngine | null;
160
+
161
+ /**
162
+ * Sets the loader used to resolve xsl:import and xsl:include references.
163
+ * Call it before importStylesheet() so the loader is available while the
164
+ * stylesheet is compiled; calling it afterwards updates the live engine.
165
+ * @param loader - The loader function, or null to remove it
166
+ * @returns This processor, to allow chaining
167
+ */
168
+ setStylesheetLoader(loader: StylesheetLoader | null): this;
169
+
170
+ /**
171
+ * Sets the loader used by the XSLT document() function. It may be set
172
+ * before or after importStylesheet(); a live engine is kept in sync.
173
+ * @param loader - The loader function, or null to remove it
174
+ * @returns This processor, to allow chaining
175
+ */
176
+ setDocumentLoader(loader: DocumentLoader | null): this;
10
177
 
11
178
  /**
12
179
  * Imports the XSLT stylesheet.
180
+ * Throws when the stylesheet is malformed or invalid (for example when a
181
+ * pattern is invalid); the processor then keeps its previous stylesheet.
13
182
  * @param style - The XSLT stylesheet to import (Document or Element)
183
+ * @param stylesheetUri - Optional base URI used to resolve relative
184
+ * xsl:import/xsl:include hrefs
14
185
  */
15
- importStylesheet(style: Node): void;
186
+ importStylesheet(style: Node, stylesheetUri?: string): void;
16
187
 
17
188
  /**
18
189
  * Transforms the node source and returns a document fragment.
@@ -29,13 +200,62 @@ export class XSLTProcessor {
29
200
  */
30
201
  transformToDocument(source: Node): XMLDocument | null;
31
202
 
203
+ /**
204
+ * Transforms the node source and serializes the result to a string,
205
+ * honoring the stylesheet xsl:output settings (non-W3C convenience method).
206
+ * @param source - The XML document to transform
207
+ * @returns The serialized result, or null on a transformation error
208
+ */
209
+ transformToString(source: Node): string | null;
210
+
211
+ /**
212
+ * Imports a stylesheet after loading its xsl:import/xsl:include tree and
213
+ * its literal document() documents asynchronously (non-W3C). Rejects on a
214
+ * load failure or an import cycle; the previous stylesheet is then kept.
215
+ * @param style - The stylesheet, as a node or as markup/stream to parse
216
+ * @param stylesheetUri - Base URI of relative hrefs and document() URIs
217
+ * @param options - Loaders and AbortSignal
218
+ */
219
+ importStylesheetAsync(
220
+ style: AsyncSource,
221
+ stylesheetUri?: string,
222
+ options?: ImportAsyncOptions,
223
+ ): Promise<void>;
224
+
225
+ /**
226
+ * Transforms asynchronously and resolves with the serialized result
227
+ * (non-W3C). Unlike transformToString, failures reject.
228
+ * @param source - Node, markup, bytes or stream of the source document
229
+ * @param options - AbortSignal, optional stylesheet and loaders
230
+ */
231
+ transformAsync(
232
+ source: AsyncSource,
233
+ options?: TransformAsyncOptions,
234
+ ): Promise<string>;
235
+
236
+ /**
237
+ * Transforms and streams the serialized result in chunks (non-W3C). The
238
+ * result tree is built in memory on the first read; the output string is
239
+ * produced chunk by chunk on demand. Failures error the stream.
240
+ * @param source - Node, markup, bytes or stream of the source document
241
+ * @param options - AbortSignal and chunk size
242
+ */
243
+ transformToStream(
244
+ source: AsyncSource,
245
+ options?: StreamOptions,
246
+ ): ReadableStream<string>;
247
+
32
248
  /**
33
249
  * Sets a parameter in the XSLT stylesheet.
34
250
  * @param namespaceURI - The namespace URI (use null for no namespace)
35
251
  * @param localName - The local name of the parameter
36
252
  * @param value - The value to set
37
253
  */
38
- setParameter(namespaceURI: string | null, localName: string, value: unknown): void;
254
+ setParameter(
255
+ namespaceURI: string | null,
256
+ localName: string,
257
+ value: unknown,
258
+ ): void;
39
259
 
40
260
  /**
41
261
  * Gets the value of a parameter from the XSLT stylesheet.
@@ -59,12 +279,14 @@ export class XSLTProcessor {
59
279
 
60
280
  /**
61
281
  * Removes all parameters and stylesheets from the XSLTProcessor.
282
+ * The loaders are configuration, not stylesheet state, and are preserved.
62
283
  */
63
284
  reset(): void;
64
285
  }
65
286
 
66
287
  /**
67
- * Check if native XSLTProcessor is available and functional.
288
+ * Check if native XSLTProcessor is available and functional. After
289
+ * installGlobal() replaced it, the original native implementation is probed.
68
290
  */
69
291
  export function isNativeXSLTSupported(): boolean;
70
292
 
@@ -91,6 +313,16 @@ export const XPathResultType: {
91
313
  FIRST_ORDERED_NODE_TYPE: 9;
92
314
  };
93
315
 
316
+ /**
317
+ * Default limits of the standalone XPath evaluator, guarding against
318
+ * untrusted expressions.
319
+ */
320
+ export const XPathLimits: {
321
+ MAX_RECURSION_DEPTH: number;
322
+ MAX_RESULT_SIZE: number;
323
+ MAX_STRING_LENGTH: number;
324
+ };
325
+
94
326
  /**
95
327
  * XPath evaluation context.
96
328
  */
@@ -100,7 +332,8 @@ export class XPathContext {
100
332
  position?: number,
101
333
  size?: number,
102
334
  variables?: Record<string, unknown>,
103
- namespaces?: Record<string, string>
335
+ namespaces?: Record<string, string>,
336
+ hostContext?: unknown,
104
337
  );
105
338
 
106
339
  node: Node;
@@ -108,17 +341,48 @@ export class XPathContext {
108
341
  size: number;
109
342
  variables: Record<string, unknown>;
110
343
  namespaces: Record<string, string>;
344
+ /** Opaque context of the host language (XSLT), for host defined functions. */
345
+ hostContext: unknown;
111
346
 
112
347
  clone(overrides?: Partial<XPathContext>): XPathContext;
113
348
  }
114
349
 
350
+ /**
351
+ * An extension function: called with the argument expressions (unevaluated
352
+ * ASTs) and the evaluation context, with the evaluator as `this`.
353
+ */
354
+ export type XPathFunction = (
355
+ this: XPathEvaluator,
356
+ args: unknown[],
357
+ context: XPathContext,
358
+ ) => unknown;
359
+
115
360
  /**
116
361
  * XPath evaluator.
117
362
  */
118
363
  export class XPathEvaluator {
119
- constructor();
364
+ constructor(options?: {
365
+ /** Deepest expression nesting (default XPathLimits.MAX_RECURSION_DEPTH). */
366
+ maxRecursionDepth?: number;
367
+ /** Largest node-set one step may produce (default XPathLimits.MAX_RESULT_SIZE). */
368
+ maxResultSize?: number;
369
+ /** Longest string a function may produce (default XPathLimits.MAX_STRING_LENGTH). */
370
+ maxStringLength?: number;
371
+ /**
372
+ * @deprecated Let unprefixed name tests (`item`, `@a`) also match nodes
373
+ * in a namespace, as before 1.2.0. XPath 1.0 and Chrome match only nodes
374
+ * in no namespace.
375
+ */
376
+ legacyNameTests?: boolean;
377
+ });
120
378
 
121
379
  evaluate(ast: unknown, context: XPathContext): unknown;
380
+ /**
381
+ * Register extension functions by name, `{namespace-uri}local-name` for
382
+ * namespaced ones.
383
+ * @returns This evaluator, to allow chaining
384
+ */
385
+ registerFunctions(functions: Record<string, XPathFunction>): this;
122
386
  toBoolean(value: unknown): boolean;
123
387
  toNumber(value: unknown): number;
124
388
  toString(value: unknown): string;
@@ -131,7 +395,10 @@ export class XPathEvaluator {
131
395
  export function evaluateXPath(
132
396
  expression: string,
133
397
  contextNode: Node,
134
- options?: { variables?: Record<string, unknown>; namespaces?: Record<string, string> }
398
+ options?: {
399
+ variables?: Record<string, unknown>;
400
+ namespaces?: Record<string, string>;
401
+ },
135
402
  ): unknown;
136
403
 
137
404
  /**
@@ -140,7 +407,10 @@ export function evaluateXPath(
140
407
  export function selectXPath(
141
408
  expression: string,
142
409
  contextNode: Node,
143
- options?: { variables?: Record<string, unknown>; namespaces?: Record<string, string> }
410
+ options?: {
411
+ variables?: Record<string, unknown>;
412
+ namespaces?: Record<string, string>;
413
+ },
144
414
  ): Node[];
145
415
 
146
416
  /**
@@ -149,7 +419,10 @@ export function selectXPath(
149
419
  export function selectFirstXPath(
150
420
  expression: string,
151
421
  contextNode: Node,
152
- options?: { variables?: Record<string, unknown>; namespaces?: Record<string, string> }
422
+ options?: {
423
+ variables?: Record<string, unknown>;
424
+ namespaces?: Record<string, string>;
425
+ },
153
426
  ): Node | null;
154
427
 
155
428
  /**
@@ -167,6 +440,8 @@ export class XsltContext {
167
440
  position?: number;
168
441
  variables?: Record<string, unknown>;
169
442
  parameters?: Record<string, unknown>;
443
+ /** Global variables and parameters of the transformation. */
444
+ globals?: unknown;
170
445
  outputDocument?: Document;
171
446
  stylesheet?: Document;
172
447
  namespaces?: Record<string, string>;
@@ -175,6 +450,10 @@ export class XsltContext {
175
450
  decimalFormats?: Record<string, unknown>;
176
451
  outputMethod?: string;
177
452
  xpathEvaluator?: XPathEvaluator;
453
+ /** The template rule being instantiated (for xsl:apply-imports). */
454
+ currentTemplate?: unknown;
455
+ /** The current mode, null for the default mode. */
456
+ currentMode?: string | null;
178
457
  });
179
458
 
180
459
  clone(overrides?: Partial<XsltContext>): XsltContext;
@@ -182,17 +461,185 @@ export class XsltContext {
182
461
  setVariable(name: string, value: unknown): void;
183
462
  }
184
463
 
464
+ /**
465
+ * Default node-set limit of an XSLT transformation (5,000,000).
466
+ */
467
+ export const XSLT_MAX_RESULT_SIZE: number;
468
+
469
+ /**
470
+ * Default XPath expression depth limit of an XSLT transformation (1000).
471
+ */
472
+ export const XSLT_MAX_EXPRESSION_DEPTH: number;
473
+
474
+ /**
475
+ * Default limit of nested template instantiations in a transformation
476
+ * (3000, libxslt's `xsltMaxDepth`).
477
+ */
478
+ export const XSLT_MAX_TEMPLATE_DEPTH: number;
479
+
185
480
  /**
186
481
  * XSLT processing engine.
187
482
  */
188
483
  export class XsltEngine {
189
- constructor();
484
+ constructor(options?: {
485
+ stylesheetLoader?: StylesheetLoader | null;
486
+ documentLoader?: DocumentLoader | null;
487
+ baseUri?: string;
488
+ /**
489
+ * Parser for the XML strings returned by the loaders; defaults to the
490
+ * global DOMParser, then to the DOMParser of the stylesheet's window.
491
+ */
492
+ domParser?: DomParserLike | null;
493
+ /** Largest node-set one XPath step may produce (default XSLT_MAX_RESULT_SIZE). */
494
+ maxResultSize?: number;
495
+ /** Deepest XPath expression nesting (default XSLT_MAX_EXPRESSION_DEPTH). */
496
+ maxRecursionDepth?: number;
497
+ /**
498
+ * Deepest nesting of template instantiations; deeper recursion throws
499
+ * "Template recursion too deep" (default XSLT_MAX_TEMPLATE_DEPTH).
500
+ */
501
+ maxTemplateDepth?: number;
502
+ /**
503
+ * @deprecated Let unprefixed name tests (`item`, `@a`) also match nodes
504
+ * in a namespace, as before 1.2.0. XPath 1.0 and Chrome match only nodes
505
+ * in no namespace.
506
+ */
507
+ legacyNameTests?: boolean;
508
+ /** Allow EXSLT `dyn:evaluate()`; it evaluates XPath built from data. */
509
+ enableDynamicEvaluate?: boolean;
510
+ /** Clock for EXSLT current-time functions (reproducible output). */
511
+ clock?: () => Date;
512
+ });
513
+
514
+ /** Whether EXSLT `dyn:evaluate()` is allowed. */
515
+ enableDynamicEvaluate: boolean;
516
+ /** Clock for EXSLT current-time functions, or null for the system clock. */
517
+ clock: (() => Date) | null;
190
518
 
191
- importStylesheet(stylesheetNode: Node): void;
519
+ setStylesheetLoader(loader: StylesheetLoader | null): this;
520
+ setDocumentLoader(loader: DocumentLoader | null): this;
521
+ importStylesheet(stylesheetNode: Node, stylesheetUri?: string): void;
192
522
  transform(sourceNode: Node, ownerDocument: Document): DocumentFragment;
523
+ /**
524
+ * Like transform(), but html output into an HTML document is parsed as
525
+ * HTML (real HTMLElements), as Chrome's transformToFragment does.
526
+ */
527
+ transformToFragment(
528
+ sourceNode: Node,
529
+ ownerDocument: Document,
530
+ ): DocumentFragment;
531
+ /**
532
+ * Register the implementation of an extension element (XSLT 1.0 section
533
+ * 14.1), used instead of its xsl:fallback children.
534
+ * @returns This engine, to allow chaining
535
+ */
536
+ registerExtensionElement(
537
+ namespaceUri: string,
538
+ localName: string,
539
+ handler: (
540
+ node: Element,
541
+ context: XsltContext,
542
+ output: Node,
543
+ engine: XsltEngine,
544
+ ) => void,
545
+ ): this;
193
546
  transformToDocument(sourceNode: Node): Document;
547
+ transformToString(sourceNode: Node): string;
548
+
549
+ outputSettings: OutputSettings;
194
550
  }
195
551
 
552
+ /**
553
+ * xsl:output settings driving the result serialization
554
+ * (XSLT 1.0 section 16). Accepts the raw stylesheet values, so the yes/no
555
+ * attributes are strings and cdata-section-elements may be a name list.
556
+ * A null method means "not declared": html or xml is picked from the result.
557
+ */
558
+ export interface OutputSettings {
559
+ method?: "xml" | "html" | "xhtml" | "text" | "auto" | (string & {}) | null;
560
+ version?: string;
561
+ encoding?: string;
562
+ standalone?: "yes" | "no" | string | null;
563
+ indent?: "yes" | "no" | boolean;
564
+ omitXmlDeclaration?: "yes" | "no" | boolean;
565
+ doctypePublic?: string | null;
566
+ doctypeSystem?: string | null;
567
+ mediaType?: string | null;
568
+ /**
569
+ * QNames (whitespace separated or as an array), or expanded names as the
570
+ * engine resolves them from xsl:output.
571
+ */
572
+ cdataSectionElements?:
573
+ string | Array<string | { namespaceUri: string | null; localName: string }>;
574
+ }
575
+
576
+ /**
577
+ * Serialize a transformation result honoring the xsl:output settings.
578
+ * @param node - Result document, fragment or element
579
+ * @param outputSettings - xsl:output settings
580
+ * @returns The serialized result, or an empty string for a null node
581
+ */
582
+ export function serializeResult(
583
+ node: Node | null,
584
+ outputSettings?: OutputSettings,
585
+ ): string;
586
+
587
+ /** Default chunk size of the streaming serializer (16384 code units). */
588
+ export const DEFAULT_CHUNK_SIZE: number;
589
+
590
+ /**
591
+ * Serialize a transformation result in chunks of at most `chunkSize` UTF-16
592
+ * code units (one more when a surrogate pair straddles the boundary); joined,
593
+ * they equal serializeResult().
594
+ * @throws RangeError When chunkSize is not a positive integer or Infinity
595
+ */
596
+ export function serializeChunks(
597
+ node: Node | null,
598
+ outputSettings?: OutputSettings,
599
+ options?: { chunkSize?: number },
600
+ ): Iterator<string> & Iterable<string>;
601
+
602
+ /**
603
+ * Transform with an engine and serialize the result in chunks: the result
604
+ * tree is built by this call, serialization runs as the chunks are read.
605
+ */
606
+ export function transformToChunks(
607
+ engine: XsltEngine,
608
+ sourceNode: Node,
609
+ options?: { chunkSize?: number },
610
+ ): Iterator<string> & Iterable<string>;
611
+
612
+ /**
613
+ * Transform with an engine and stream the serialized result.
614
+ */
615
+ export function transformToStream(
616
+ engine: XsltEngine,
617
+ sourceNode: Node,
618
+ options?: StreamOptions,
619
+ ): ReadableStream<string>;
620
+
621
+ /**
622
+ * Mark a text node as produced with disable-output-escaping="yes".
623
+ */
624
+ export function markRawText<T extends Node | null>(node: T): T;
625
+
626
+ /**
627
+ * Check whether a node must be serialized without output escaping.
628
+ */
629
+ export function isRawText(node: Node | null): boolean;
630
+
631
+ /**
632
+ * Normalize raw xsl:output settings for the serializers.
633
+ */
634
+ export function resolveOutputSettings(
635
+ outputSettings: OutputSettings | null,
636
+ node: Node | null,
637
+ ): Required<OutputSettings> & {
638
+ indent: boolean;
639
+ omitXmlDeclaration: boolean;
640
+ cdataSectionElements: Set<string>;
641
+ };
642
+
196
643
  /**
197
644
  * Version information.
198
645
  */