@tradik/xslt-processor 1.0.2 → 1.1.1

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 (47) hide show
  1. package/README.md +292 -47
  2. package/bin/lib/options.js +114 -0
  3. package/bin/lib/paths.js +186 -0
  4. package/bin/lib/transform.js +115 -0
  5. package/bin/xslt.js +68 -162
  6. package/dist/xslt-processor.browser.js +2073 -163
  7. package/dist/xslt-processor.browser.js.map +4 -4
  8. package/dist/xslt-processor.browser.min.js +6 -2
  9. package/dist/xslt-processor.browser.min.js.map +4 -4
  10. package/dist/xslt-processor.cjs +2077 -162
  11. package/dist/xslt-processor.cjs.map +4 -4
  12. package/dist/xslt-processor.d.cts +299 -0
  13. package/dist/xslt-processor.d.ts +92 -4
  14. package/dist/xslt-processor.js +2072 -161
  15. package/dist/xslt-processor.js.map +4 -4
  16. package/package.json +27 -16
  17. package/src/XSLTProcessor.js +177 -8
  18. package/src/index.js +11 -5
  19. package/src/xpath/evaluator.js +48 -7
  20. package/src/xslt/elements.js +57 -0
  21. package/src/xslt/engine.js +474 -185
  22. package/src/xslt/formatNumber.js +220 -0
  23. package/src/xslt/functions.js +191 -0
  24. package/src/xslt/index.js +31 -0
  25. package/src/xslt/keys.js +141 -0
  26. package/src/xslt/literalResult.js +167 -0
  27. package/src/xslt/number.js +178 -0
  28. package/src/xslt/numberFormat.js +155 -0
  29. package/src/xslt/resultTree.js +74 -0
  30. package/src/xslt/serializer/baseWriter.js +283 -0
  31. package/src/xslt/serializer/constants.js +78 -0
  32. package/src/xslt/serializer/escape.js +98 -0
  33. package/src/xslt/serializer/htmlSerializer.js +141 -0
  34. package/src/xslt/serializer/indent.js +51 -0
  35. package/src/xslt/serializer/namespaces.js +68 -0
  36. package/src/xslt/serializer/rawText.js +41 -0
  37. package/src/xslt/serializer/settings.js +103 -0
  38. package/src/xslt/serializer/textSerializer.js +29 -0
  39. package/src/xslt/serializer/xmlSerializer.js +127 -0
  40. package/src/xslt/serializer.js +57 -0
  41. package/src/xslt/templatePriority.js +45 -0
  42. package/src/xslt/uri.js +68 -0
  43. package/src/xslt/whitespace.js +184 -0
  44. package/src/XSLTProcessor.test.js +0 -930
  45. package/src/xpath/evaluator.test.js +0 -1852
  46. package/src/xpath/tokenizer.test.js +0 -224
  47. package/src/xslt/engine.test.js +0 -3130
@@ -0,0 +1,299 @@
1
+ /**
2
+ * @tradik/xslt-processor - TypeScript Declarations
3
+ */
4
+
5
+ /**
6
+ * Loader used to resolve xsl:import and xsl:include references.
7
+ *
8
+ * The loader is synchronous: it must return the external stylesheet as a
9
+ * Document or as an XML string (which is parsed automatically).
10
+ *
11
+ * @param href - The resolved URI of the referenced stylesheet
12
+ * @param baseUri - The URI of the importing/including stylesheet, if known
13
+ */
14
+ export type StylesheetLoader = (href: string, baseUri?: string) => Document | string;
15
+
16
+ /**
17
+ * XSLTProcessor - Applies XSLT stylesheet transformations to XML documents.
18
+ */
19
+ export class XSLTProcessor {
20
+ constructor();
21
+
22
+ /**
23
+ * The underlying XSLT engine (advanced usage).
24
+ * Null until a stylesheet has been imported.
25
+ */
26
+ readonly engine: XsltEngine | null;
27
+
28
+ /**
29
+ * Sets the loader used to resolve xsl:import and xsl:include references.
30
+ * Call it before importStylesheet() so the loader is available while the
31
+ * stylesheet is compiled; calling it afterwards updates the live engine.
32
+ * @param loader - The loader function, or null to remove it
33
+ * @returns This processor, to allow chaining
34
+ */
35
+ setStylesheetLoader(loader: StylesheetLoader | null): this;
36
+
37
+ /**
38
+ * Imports the XSLT stylesheet.
39
+ * @param style - The XSLT stylesheet to import (Document or Element)
40
+ * @param stylesheetUri - Optional base URI used to resolve relative
41
+ * xsl:import/xsl:include hrefs
42
+ */
43
+ importStylesheet(style: Node, stylesheetUri?: string): void;
44
+
45
+ /**
46
+ * Transforms the node source and returns a document fragment.
47
+ * @param source - The XML document to transform
48
+ * @param output - The document that will own the generated fragment
49
+ * @returns The transformed result as a DocumentFragment
50
+ */
51
+ transformToFragment(source: Node, output: Document): DocumentFragment | null;
52
+
53
+ /**
54
+ * Transforms the node source and returns a full XML document.
55
+ * @param source - The XML document to transform
56
+ * @returns The transformed result as an XMLDocument
57
+ */
58
+ transformToDocument(source: Node): XMLDocument | null;
59
+
60
+ /**
61
+ * Transforms the node source and serializes the result to a string,
62
+ * honoring the stylesheet xsl:output settings (non-W3C convenience method).
63
+ * @param source - The XML document to transform
64
+ * @returns The serialized result, or null on a transformation error
65
+ */
66
+ transformToString(source: Node): string | null;
67
+
68
+ /**
69
+ * Sets a parameter in the XSLT stylesheet.
70
+ * @param namespaceURI - The namespace URI (use null for no namespace)
71
+ * @param localName - The local name of the parameter
72
+ * @param value - The value to set
73
+ */
74
+ setParameter(namespaceURI: string | null, localName: string, value: unknown): void;
75
+
76
+ /**
77
+ * Gets the value of a parameter from the XSLT stylesheet.
78
+ * @param namespaceURI - The namespace URI
79
+ * @param localName - The local name of the parameter
80
+ * @returns The parameter value, or empty string if not set
81
+ */
82
+ getParameter(namespaceURI: string | null, localName: string): unknown;
83
+
84
+ /**
85
+ * Removes a parameter from the XSLT processor.
86
+ * @param namespaceURI - The namespace URI
87
+ * @param localName - The local name of the parameter
88
+ */
89
+ removeParameter(namespaceURI: string | null, localName: string): void;
90
+
91
+ /**
92
+ * Removes all set parameters from the XSLTProcessor.
93
+ */
94
+ clearParameters(): void;
95
+
96
+ /**
97
+ * Removes all parameters and stylesheets from the XSLTProcessor.
98
+ * The stylesheet loader is configuration, not stylesheet state, and is preserved.
99
+ */
100
+ reset(): void;
101
+ }
102
+
103
+ /**
104
+ * Check if native XSLTProcessor is available and functional.
105
+ */
106
+ export function isNativeXSLTSupported(): boolean;
107
+
108
+ /**
109
+ * Install as global XSLTProcessor replacement if native is not functional.
110
+ * @param force - Force installation even if native is available
111
+ * @returns True if installed as global
112
+ */
113
+ export function installGlobal(force?: boolean): boolean;
114
+
115
+ /**
116
+ * XPath evaluation result types.
117
+ */
118
+ export const XPathResultType: {
119
+ ANY_TYPE: 0;
120
+ NUMBER_TYPE: 1;
121
+ STRING_TYPE: 2;
122
+ BOOLEAN_TYPE: 3;
123
+ UNORDERED_NODE_ITERATOR_TYPE: 4;
124
+ ORDERED_NODE_ITERATOR_TYPE: 5;
125
+ UNORDERED_NODE_SNAPSHOT_TYPE: 6;
126
+ ORDERED_NODE_SNAPSHOT_TYPE: 7;
127
+ ANY_UNORDERED_NODE_TYPE: 8;
128
+ FIRST_ORDERED_NODE_TYPE: 9;
129
+ };
130
+
131
+ /**
132
+ * XPath evaluation context.
133
+ */
134
+ export class XPathContext {
135
+ constructor(
136
+ node: Node,
137
+ position?: number,
138
+ size?: number,
139
+ variables?: Record<string, unknown>,
140
+ namespaces?: Record<string, string>
141
+ );
142
+
143
+ node: Node;
144
+ position: number;
145
+ size: number;
146
+ variables: Record<string, unknown>;
147
+ namespaces: Record<string, string>;
148
+
149
+ clone(overrides?: Partial<XPathContext>): XPathContext;
150
+ }
151
+
152
+ /**
153
+ * XPath evaluator.
154
+ */
155
+ export class XPathEvaluator {
156
+ constructor();
157
+
158
+ evaluate(ast: unknown, context: XPathContext): unknown;
159
+ toBoolean(value: unknown): boolean;
160
+ toNumber(value: unknown): number;
161
+ toString(value: unknown): string;
162
+ getStringValue(node: Node): string;
163
+ }
164
+
165
+ /**
166
+ * Evaluate an XPath expression against a node.
167
+ */
168
+ export function evaluateXPath(
169
+ expression: string,
170
+ contextNode: Node,
171
+ options?: { variables?: Record<string, unknown>; namespaces?: Record<string, string> }
172
+ ): unknown;
173
+
174
+ /**
175
+ * Select nodes matching an XPath expression.
176
+ */
177
+ export function selectXPath(
178
+ expression: string,
179
+ contextNode: Node,
180
+ options?: { variables?: Record<string, unknown>; namespaces?: Record<string, string> }
181
+ ): Node[];
182
+
183
+ /**
184
+ * Select first node matching an XPath expression.
185
+ */
186
+ export function selectFirstXPath(
187
+ expression: string,
188
+ contextNode: Node,
189
+ options?: { variables?: Record<string, unknown>; namespaces?: Record<string, string> }
190
+ ): Node | null;
191
+
192
+ /**
193
+ * Parse an XPath expression into an AST.
194
+ */
195
+ export function parseXPath(expression: string): unknown;
196
+
197
+ /**
198
+ * XSLT processing context.
199
+ */
200
+ export class XsltContext {
201
+ constructor(options?: {
202
+ currentNode?: Node;
203
+ currentNodeList?: Node[];
204
+ position?: number;
205
+ variables?: Record<string, unknown>;
206
+ parameters?: Record<string, unknown>;
207
+ outputDocument?: Document;
208
+ stylesheet?: Document;
209
+ namespaces?: Record<string, string>;
210
+ templates?: unknown[];
211
+ keys?: Record<string, unknown>;
212
+ decimalFormats?: Record<string, unknown>;
213
+ outputMethod?: string;
214
+ xpathEvaluator?: XPathEvaluator;
215
+ });
216
+
217
+ clone(overrides?: Partial<XsltContext>): XsltContext;
218
+ getVariable(name: string): unknown;
219
+ setVariable(name: string, value: unknown): void;
220
+ }
221
+
222
+ /**
223
+ * XSLT processing engine.
224
+ */
225
+ export class XsltEngine {
226
+ constructor(options?: { stylesheetLoader?: StylesheetLoader | null; baseUri?: string });
227
+
228
+ setStylesheetLoader(loader: StylesheetLoader | null): void;
229
+ importStylesheet(stylesheetNode: Node, stylesheetUri?: string): void;
230
+ transform(sourceNode: Node, ownerDocument: Document): DocumentFragment;
231
+ transformToDocument(sourceNode: Node): Document;
232
+ transformToString(sourceNode: Node): string;
233
+
234
+ outputSettings: OutputSettings;
235
+ }
236
+
237
+ /**
238
+ * xsl:output settings driving the result serialization
239
+ * (XSLT 1.0 section 16). Accepts the raw stylesheet values, so the yes/no
240
+ * attributes are strings and cdata-section-elements may be a name list.
241
+ */
242
+ export interface OutputSettings {
243
+ method?: 'xml' | 'html' | 'xhtml' | 'text' | 'auto' | string;
244
+ version?: string;
245
+ encoding?: string;
246
+ standalone?: 'yes' | 'no' | string | null;
247
+ indent?: 'yes' | 'no' | boolean;
248
+ omitXmlDeclaration?: 'yes' | 'no' | boolean;
249
+ doctypePublic?: string | null;
250
+ doctypeSystem?: string | null;
251
+ mediaType?: string | null;
252
+ cdataSectionElements?: string[] | string;
253
+ }
254
+
255
+ /**
256
+ * Serialize a transformation result honoring the xsl:output settings.
257
+ * @param node - Result document, fragment or element
258
+ * @param outputSettings - xsl:output settings
259
+ * @returns The serialized result, or an empty string for a null node
260
+ */
261
+ export function serializeResult(
262
+ node: Node | null,
263
+ outputSettings?: OutputSettings
264
+ ): string;
265
+
266
+ /**
267
+ * Mark a text node as produced with disable-output-escaping="yes".
268
+ */
269
+ export function markRawText<T extends Node | null>(node: T): T;
270
+
271
+ /**
272
+ * Check whether a node must be serialized without output escaping.
273
+ */
274
+ export function isRawText(node: Node | null): boolean;
275
+
276
+ /**
277
+ * Normalize raw xsl:output settings for the serializers.
278
+ */
279
+ export function resolveOutputSettings(
280
+ outputSettings: OutputSettings | null,
281
+ node: Node | null
282
+ ): Required<OutputSettings> & { indent: boolean; omitXmlDeclaration: boolean; cdataSectionElements: Set<string> };
283
+
284
+ /**
285
+ * Version information.
286
+ */
287
+ export const VERSION: string;
288
+
289
+ /**
290
+ * Check if running in a browser environment.
291
+ */
292
+ export const isBrowser: boolean;
293
+
294
+ /**
295
+ * Check if running in Node.js.
296
+ */
297
+ export const isNode: boolean;
298
+
299
+ export default XSLTProcessor;
@@ -1,18 +1,46 @@
1
1
  /**
2
- * @cv-xslt/xslt-processor - TypeScript Declarations
2
+ * @tradik/xslt-processor - TypeScript Declarations
3
3
  */
4
4
 
5
+ /**
6
+ * Loader used to resolve xsl:import and xsl:include references.
7
+ *
8
+ * The loader is synchronous: it must return the external stylesheet as a
9
+ * Document or as an XML string (which is parsed automatically).
10
+ *
11
+ * @param href - The resolved URI of the referenced stylesheet
12
+ * @param baseUri - The URI of the importing/including stylesheet, if known
13
+ */
14
+ export type StylesheetLoader = (href: string, baseUri?: string) => Document | string;
15
+
5
16
  /**
6
17
  * XSLTProcessor - Applies XSLT stylesheet transformations to XML documents.
7
18
  */
8
19
  export class XSLTProcessor {
9
20
  constructor();
10
21
 
22
+ /**
23
+ * The underlying XSLT engine (advanced usage).
24
+ * Null until a stylesheet has been imported.
25
+ */
26
+ readonly engine: XsltEngine | null;
27
+
28
+ /**
29
+ * Sets the loader used to resolve xsl:import and xsl:include references.
30
+ * Call it before importStylesheet() so the loader is available while the
31
+ * stylesheet is compiled; calling it afterwards updates the live engine.
32
+ * @param loader - The loader function, or null to remove it
33
+ * @returns This processor, to allow chaining
34
+ */
35
+ setStylesheetLoader(loader: StylesheetLoader | null): this;
36
+
11
37
  /**
12
38
  * Imports the XSLT stylesheet.
13
39
  * @param style - The XSLT stylesheet to import (Document or Element)
40
+ * @param stylesheetUri - Optional base URI used to resolve relative
41
+ * xsl:import/xsl:include hrefs
14
42
  */
15
- importStylesheet(style: Node): void;
43
+ importStylesheet(style: Node, stylesheetUri?: string): void;
16
44
 
17
45
  /**
18
46
  * Transforms the node source and returns a document fragment.
@@ -29,6 +57,14 @@ export class XSLTProcessor {
29
57
  */
30
58
  transformToDocument(source: Node): XMLDocument | null;
31
59
 
60
+ /**
61
+ * Transforms the node source and serializes the result to a string,
62
+ * honoring the stylesheet xsl:output settings (non-W3C convenience method).
63
+ * @param source - The XML document to transform
64
+ * @returns The serialized result, or null on a transformation error
65
+ */
66
+ transformToString(source: Node): string | null;
67
+
32
68
  /**
33
69
  * Sets a parameter in the XSLT stylesheet.
34
70
  * @param namespaceURI - The namespace URI (use null for no namespace)
@@ -59,6 +95,7 @@ export class XSLTProcessor {
59
95
 
60
96
  /**
61
97
  * Removes all parameters and stylesheets from the XSLTProcessor.
98
+ * The stylesheet loader is configuration, not stylesheet state, and is preserved.
62
99
  */
63
100
  reset(): void;
64
101
  }
@@ -186,13 +223,64 @@ export class XsltContext {
186
223
  * XSLT processing engine.
187
224
  */
188
225
  export class XsltEngine {
189
- constructor();
226
+ constructor(options?: { stylesheetLoader?: StylesheetLoader | null; baseUri?: string });
190
227
 
191
- importStylesheet(stylesheetNode: Node): void;
228
+ setStylesheetLoader(loader: StylesheetLoader | null): void;
229
+ importStylesheet(stylesheetNode: Node, stylesheetUri?: string): void;
192
230
  transform(sourceNode: Node, ownerDocument: Document): DocumentFragment;
193
231
  transformToDocument(sourceNode: Node): Document;
232
+ transformToString(sourceNode: Node): string;
233
+
234
+ outputSettings: OutputSettings;
194
235
  }
195
236
 
237
+ /**
238
+ * xsl:output settings driving the result serialization
239
+ * (XSLT 1.0 section 16). Accepts the raw stylesheet values, so the yes/no
240
+ * attributes are strings and cdata-section-elements may be a name list.
241
+ */
242
+ export interface OutputSettings {
243
+ method?: 'xml' | 'html' | 'xhtml' | 'text' | 'auto' | string;
244
+ version?: string;
245
+ encoding?: string;
246
+ standalone?: 'yes' | 'no' | string | null;
247
+ indent?: 'yes' | 'no' | boolean;
248
+ omitXmlDeclaration?: 'yes' | 'no' | boolean;
249
+ doctypePublic?: string | null;
250
+ doctypeSystem?: string | null;
251
+ mediaType?: string | null;
252
+ cdataSectionElements?: string[] | string;
253
+ }
254
+
255
+ /**
256
+ * Serialize a transformation result honoring the xsl:output settings.
257
+ * @param node - Result document, fragment or element
258
+ * @param outputSettings - xsl:output settings
259
+ * @returns The serialized result, or an empty string for a null node
260
+ */
261
+ export function serializeResult(
262
+ node: Node | null,
263
+ outputSettings?: OutputSettings
264
+ ): string;
265
+
266
+ /**
267
+ * Mark a text node as produced with disable-output-escaping="yes".
268
+ */
269
+ export function markRawText<T extends Node | null>(node: T): T;
270
+
271
+ /**
272
+ * Check whether a node must be serialized without output escaping.
273
+ */
274
+ export function isRawText(node: Node | null): boolean;
275
+
276
+ /**
277
+ * Normalize raw xsl:output settings for the serializers.
278
+ */
279
+ export function resolveOutputSettings(
280
+ outputSettings: OutputSettings | null,
281
+ node: Node | null
282
+ ): Required<OutputSettings> & { indent: boolean; omitXmlDeclaration: boolean; cdataSectionElements: Set<string> };
283
+
196
284
  /**
197
285
  * Version information.
198
286
  */