xslt-processor 5.0.0 → 5.0.2

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/README.md CHANGED
@@ -119,7 +119,47 @@ const xslt = new Xslt(options);
119
119
  - `parameters` (`array`, default `[]`): external parameters that you want to use.
120
120
  - `name`: the parameter name;
121
121
  - `namespaceUri` (optional): the namespace;
122
- - `value`: the value.
122
+ - `value`: the value. The type is preserved automatically:
123
+ - **string** values become `StringValue`;
124
+ - **number** values become `NumberValue` (usable in XPath arithmetic);
125
+ - **boolean** values become `BooleanValue` (usable in `xsl:if`/`xsl:when` tests);
126
+ - Note: in XPath/XSLT, any non-empty string is truthy, so the string "false" still behaves as true in tests;
127
+ - **`NodeSetValue`** instances are kept as-is (useful for passing additional documents);
128
+ - DOM nodes (objects with `nodeType`) are wrapped in a `NodeSetValue`;
129
+ - arrays of nodes are wrapped in a `NodeSetValue`.
130
+
131
+ **`parameters` examples:**
132
+
133
+ ```js
134
+ import { Xslt, XmlParser } from 'xslt-processor'
135
+
136
+ // String parameter (default behavior)
137
+ const xslt = new Xslt({ parameters: [
138
+ { name: 'title', value: 'Hello' }
139
+ ] });
140
+
141
+ // Number parameter — works in XPath arithmetic ($count + 1 = 43)
142
+ const xslt = new Xslt({ parameters: [
143
+ { name: 'count', value: 42 }
144
+ ] });
145
+
146
+ // Boolean parameter — works in xsl:if / xsl:when tests
147
+ const xslt = new Xslt({ parameters: [
148
+ { name: 'debug', value: true }
149
+ ] });
150
+
151
+ // Node-set parameter — pass an additional document for cross-document lookups
152
+ import { NodeSetValue } from 'xslt-processor/xpath/values'
153
+
154
+ const xmlParser = new XmlParser();
155
+ const lookupDoc = xmlParser.xmlParse('<lookup><entry key="a">Alpha</entry></lookup>');
156
+
157
+ const xslt = new Xslt({ parameters: [
158
+ { name: 'lookup', value: new NodeSetValue([lookupDoc]) }
159
+ ] });
160
+ // In XSLT: <xsl:value-of select="$lookup/lookup/entry[@key='a']"/>
161
+ ```
162
+
123
163
  - `fetchFunction` (`(uri: string) => Promise<string>`, optional): a custom function for loading external resources referenced by `<xsl:import>` and `<xsl:include>`. Receives the URI and must return the fetched content as a string. Defaults to the global `fetch` API. This is useful for:
124
164
  - Denying external loading entirely;
125
165
  - Loading from the local filesystem or other non-HTTP sources;
package/index.d.mts CHANGED
@@ -308,13 +308,14 @@ declare class ExprContext {
308
308
  *
309
309
  * Notice that position starts at 0 at the outside interface;
310
310
  * inside XPath expressions this shows up as position()=1.
311
- * @param nodeList TODO
312
- * @param opt_position TODO
313
- * @param opt_parent TODO
314
- * @param opt_caseInsensitive TODO
315
- * @param opt_ignoreAttributesWithoutValue TODO
316
- * @param opt_returnOnFirstMatch TODO
317
- * @param opt_ignoreNonElementNodesForNTA TODO
311
+ * @param nodeList The list of nodes that contains the current node. This is needed to implement the position() and last() functions, and to evaluate predicates.
312
+ * @param xsltVersion The XSLT version in use, which may affect certain function behaviors (e.g. 1.0 vs 2.0).
313
+ * @param opt_position The position of the current node in the nodeList. Defaults to 0.
314
+ * @param opt_parent The parent expression context, used for variable scoping. Defaults to null.
315
+ * @param opt_caseInsensitive Whether node name tests should be case insensitive. Defaults to false.
316
+ * @param opt_ignoreAttributesWithoutValue Whether to ignore attributes that have no value (e.g. <input disabled>) when evaluating XPath expressions. Defaults to false.
317
+ * @param opt_returnOnFirstMatch Whether XPath evaluation should return as soon as the first match is found. Defaults to false.
318
+ * @param opt_ignoreNonElementNodesForNTA Whether to ignore non-element nodes when evaluating the "node()" any node test. Defaults to false.
318
319
  */
319
320
  constructor(nodeList: XNode[], xsltVersion?: '1.0' | '2.0' | '3.0', opt_position?: number, opt_decimalFormatSettings?: XsltDecimalFormatSettings, opt_variables?: {
320
321
  [name: string]: any;
@@ -326,9 +327,9 @@ declare class ExprContext {
326
327
  * parent. If passed as argument to clone(), the new context has a
327
328
  * different node, position, or node set. What is not passed is
328
329
  * inherited from the cloned context.
329
- * @param opt_nodeList TODO
330
- * @param opt_position TODO
331
- * @returns TODO
330
+ * @param opt_nodeList The node list for the new context. If not provided, the new context inherits the node list of the current context.
331
+ * @param opt_position The position for the new context. If not provided, the new context inherits the position of the current context.
332
+ * @returns A new ExprContext instance with the specified node list and position, and the current context as its parent.
332
333
  */
333
334
  clone(opt_nodeList?: XNode[], opt_position?: number): ExprContext;
334
335
  setVariable(name?: string, value?: NodeValue | string): void;
@@ -343,13 +344,13 @@ declare class ExprContext {
343
344
  setNode(position: number): void;
344
345
  contextSize(): number;
345
346
  isCaseInsensitive(): any;
346
- setCaseInsensitive(caseInsensitive: any): any;
347
- isIgnoreAttributesWithoutValue(): any;
348
- setIgnoreAttributesWithoutValue(ignore: any): any;
349
- isReturnOnFirstMatch(): any;
350
- setReturnOnFirstMatch(returnOnFirstMatch: any): any;
351
- isIgnoreNonElementNodesForNTA(): any;
352
- setIgnoreNonElementNodesForNTA(ignoreNonElementNodesForNTA: any): any;
347
+ setCaseInsensitive(caseInsensitive: boolean): boolean;
348
+ isIgnoreAttributesWithoutValue(): boolean;
349
+ setIgnoreAttributesWithoutValue(ignore: boolean): boolean;
350
+ isReturnOnFirstMatch(): boolean;
351
+ setReturnOnFirstMatch(returnOnFirstMatch: boolean): boolean;
352
+ isIgnoreNonElementNodesForNTA(): boolean;
353
+ setIgnoreNonElementNodesForNTA(ignoreNonElementNodesForNTA: boolean): boolean;
353
354
  }
354
355
 
355
356
  /**
package/index.d.ts CHANGED
@@ -308,13 +308,14 @@ declare class ExprContext {
308
308
  *
309
309
  * Notice that position starts at 0 at the outside interface;
310
310
  * inside XPath expressions this shows up as position()=1.
311
- * @param nodeList TODO
312
- * @param opt_position TODO
313
- * @param opt_parent TODO
314
- * @param opt_caseInsensitive TODO
315
- * @param opt_ignoreAttributesWithoutValue TODO
316
- * @param opt_returnOnFirstMatch TODO
317
- * @param opt_ignoreNonElementNodesForNTA TODO
311
+ * @param nodeList The list of nodes that contains the current node. This is needed to implement the position() and last() functions, and to evaluate predicates.
312
+ * @param xsltVersion The XSLT version in use, which may affect certain function behaviors (e.g. 1.0 vs 2.0).
313
+ * @param opt_position The position of the current node in the nodeList. Defaults to 0.
314
+ * @param opt_parent The parent expression context, used for variable scoping. Defaults to null.
315
+ * @param opt_caseInsensitive Whether node name tests should be case insensitive. Defaults to false.
316
+ * @param opt_ignoreAttributesWithoutValue Whether to ignore attributes that have no value (e.g. <input disabled>) when evaluating XPath expressions. Defaults to false.
317
+ * @param opt_returnOnFirstMatch Whether XPath evaluation should return as soon as the first match is found. Defaults to false.
318
+ * @param opt_ignoreNonElementNodesForNTA Whether to ignore non-element nodes when evaluating the "node()" any node test. Defaults to false.
318
319
  */
319
320
  constructor(nodeList: XNode[], xsltVersion?: '1.0' | '2.0' | '3.0', opt_position?: number, opt_decimalFormatSettings?: XsltDecimalFormatSettings, opt_variables?: {
320
321
  [name: string]: any;
@@ -326,9 +327,9 @@ declare class ExprContext {
326
327
  * parent. If passed as argument to clone(), the new context has a
327
328
  * different node, position, or node set. What is not passed is
328
329
  * inherited from the cloned context.
329
- * @param opt_nodeList TODO
330
- * @param opt_position TODO
331
- * @returns TODO
330
+ * @param opt_nodeList The node list for the new context. If not provided, the new context inherits the node list of the current context.
331
+ * @param opt_position The position for the new context. If not provided, the new context inherits the position of the current context.
332
+ * @returns A new ExprContext instance with the specified node list and position, and the current context as its parent.
332
333
  */
333
334
  clone(opt_nodeList?: XNode[], opt_position?: number): ExprContext;
334
335
  setVariable(name?: string, value?: NodeValue | string): void;
@@ -343,13 +344,13 @@ declare class ExprContext {
343
344
  setNode(position: number): void;
344
345
  contextSize(): number;
345
346
  isCaseInsensitive(): any;
346
- setCaseInsensitive(caseInsensitive: any): any;
347
- isIgnoreAttributesWithoutValue(): any;
348
- setIgnoreAttributesWithoutValue(ignore: any): any;
349
- isReturnOnFirstMatch(): any;
350
- setReturnOnFirstMatch(returnOnFirstMatch: any): any;
351
- isIgnoreNonElementNodesForNTA(): any;
352
- setIgnoreNonElementNodesForNTA(ignoreNonElementNodesForNTA: any): any;
347
+ setCaseInsensitive(caseInsensitive: boolean): boolean;
348
+ isIgnoreAttributesWithoutValue(): boolean;
349
+ setIgnoreAttributesWithoutValue(ignore: boolean): boolean;
350
+ isReturnOnFirstMatch(): boolean;
351
+ setReturnOnFirstMatch(returnOnFirstMatch: boolean): boolean;
352
+ isIgnoreNonElementNodesForNTA(): boolean;
353
+ setIgnoreNonElementNodesForNTA(ignoreNonElementNodesForNTA: boolean): boolean;
353
354
  }
354
355
 
355
356
  /**
package/index.js CHANGED
@@ -6450,8 +6450,15 @@ var init_function_call_expression = __esm({
6450
6450
  return !!value;
6451
6451
  }
6452
6452
  toNumber(args, context) {
6453
+ const toNumberFromString = (text) => {
6454
+ const trimmed = text.trim();
6455
+ if (trimmed.length === 0) {
6456
+ return NaN;
6457
+ }
6458
+ return Number(trimmed);
6459
+ };
6453
6460
  if (args.length === 0) {
6454
- return Number(this.stringValue([], context));
6461
+ return toNumberFromString(this.stringValue([], context));
6455
6462
  }
6456
6463
  const value = args[0];
6457
6464
  if (typeof value === "object" && value !== null && "numberValue" in value && typeof value.numberValue === "function") {
@@ -6461,7 +6468,10 @@ var init_function_call_expression = __esm({
6461
6468
  if (value.length === 0) return NaN;
6462
6469
  const firstNode = value[0];
6463
6470
  const stringValue = this.getNodeStringValue(firstNode);
6464
- return Number(stringValue);
6471
+ return toNumberFromString(stringValue);
6472
+ }
6473
+ if (typeof value === "string") {
6474
+ return toNumberFromString(value);
6465
6475
  }
6466
6476
  return Number(value);
6467
6477
  }
@@ -11633,7 +11643,11 @@ var StringValue = class {
11633
11643
  return this.value.length > 0;
11634
11644
  }
11635
11645
  numberValue() {
11636
- return this.value - 0;
11646
+ const text = String(this.value).trim();
11647
+ if (text.length === 0) {
11648
+ return NaN;
11649
+ }
11650
+ return Number(text);
11637
11651
  }
11638
11652
  nodeSetValue() {
11639
11653
  throw this;
@@ -11867,28 +11881,37 @@ var NodeConverter = class {
11867
11881
  */
11868
11882
  convertVariables(exprContext) {
11869
11883
  const variables = {};
11870
- for (const [name, value] of Object.entries(exprContext.variables || {})) {
11871
- if (value && typeof value === "object" && "stringValue" in value) {
11872
- const nodeValue = value;
11873
- if (nodeValue.type === "node-set") {
11874
- variables[name] = value.nodeSetValue().map((n) => this.adaptXNode(n));
11875
- } else if (nodeValue.type === "string") {
11876
- variables[name] = value.stringValue();
11877
- } else if (nodeValue.type === "number") {
11878
- variables[name] = value.numberValue();
11879
- } else if (nodeValue.type === "boolean") {
11880
- variables[name] = value.booleanValue();
11881
- } else if (nodeValue.type === "map") {
11882
- variables[name] = nodeValue.value;
11883
- } else if (nodeValue.type === "array") {
11884
- variables[name] = nodeValue.value;
11885
- } else if (nodeValue.type === "function") {
11886
- variables[name] = nodeValue.value;
11884
+ const contexts = [];
11885
+ let ctx = exprContext;
11886
+ while (ctx) {
11887
+ contexts.push(ctx);
11888
+ ctx = ctx.parent;
11889
+ }
11890
+ for (let i = contexts.length - 1; i >= 0; i--) {
11891
+ const current = contexts[i];
11892
+ for (const [name, value] of Object.entries(current.variables || {})) {
11893
+ if (value && typeof value === "object" && "stringValue" in value) {
11894
+ const nodeValue = value;
11895
+ if (nodeValue.type === "node-set") {
11896
+ variables[name] = value.nodeSetValue().map((n) => this.adaptXNode(n));
11897
+ } else if (nodeValue.type === "string") {
11898
+ variables[name] = value.stringValue();
11899
+ } else if (nodeValue.type === "number") {
11900
+ variables[name] = value.numberValue();
11901
+ } else if (nodeValue.type === "boolean") {
11902
+ variables[name] = value.booleanValue();
11903
+ } else if (nodeValue.type === "map") {
11904
+ variables[name] = nodeValue.value;
11905
+ } else if (nodeValue.type === "array") {
11906
+ variables[name] = nodeValue.value;
11907
+ } else if (nodeValue.type === "function") {
11908
+ variables[name] = nodeValue.value;
11909
+ } else {
11910
+ variables[name] = value.stringValue();
11911
+ }
11887
11912
  } else {
11888
- variables[name] = value.stringValue();
11913
+ variables[name] = value;
11889
11914
  }
11890
- } else {
11891
- variables[name] = value;
11892
11915
  }
11893
11916
  }
11894
11917
  return variables;
@@ -12423,13 +12446,14 @@ var ExprContext = class _ExprContext {
12423
12446
  *
12424
12447
  * Notice that position starts at 0 at the outside interface;
12425
12448
  * inside XPath expressions this shows up as position()=1.
12426
- * @param nodeList TODO
12427
- * @param opt_position TODO
12428
- * @param opt_parent TODO
12429
- * @param opt_caseInsensitive TODO
12430
- * @param opt_ignoreAttributesWithoutValue TODO
12431
- * @param opt_returnOnFirstMatch TODO
12432
- * @param opt_ignoreNonElementNodesForNTA TODO
12449
+ * @param nodeList The list of nodes that contains the current node. This is needed to implement the position() and last() functions, and to evaluate predicates.
12450
+ * @param xsltVersion The XSLT version in use, which may affect certain function behaviors (e.g. 1.0 vs 2.0).
12451
+ * @param opt_position The position of the current node in the nodeList. Defaults to 0.
12452
+ * @param opt_parent The parent expression context, used for variable scoping. Defaults to null.
12453
+ * @param opt_caseInsensitive Whether node name tests should be case insensitive. Defaults to false.
12454
+ * @param opt_ignoreAttributesWithoutValue Whether to ignore attributes that have no value (e.g. <input disabled>) when evaluating XPath expressions. Defaults to false.
12455
+ * @param opt_returnOnFirstMatch Whether XPath evaluation should return as soon as the first match is found. Defaults to false.
12456
+ * @param opt_ignoreNonElementNodesForNTA Whether to ignore non-element nodes when evaluating the "node()" any node test. Defaults to false.
12433
12457
  */
12434
12458
  constructor(nodeList, xsltVersion = "1.0", opt_position, opt_decimalFormatSettings, opt_variables, opt_knownNamespaces, opt_parent, opt_caseInsensitive, opt_ignoreAttributesWithoutValue, opt_returnOnFirstMatch, opt_ignoreNonElementNodesForNTA, opt_warningsCallback) {
12435
12459
  this.nodeList = nodeList;
@@ -12471,9 +12495,9 @@ var ExprContext = class _ExprContext {
12471
12495
  * parent. If passed as argument to clone(), the new context has a
12472
12496
  * different node, position, or node set. What is not passed is
12473
12497
  * inherited from the cloned context.
12474
- * @param opt_nodeList TODO
12475
- * @param opt_position TODO
12476
- * @returns TODO
12498
+ * @param opt_nodeList The node list for the new context. If not provided, the new context inherits the node list of the current context.
12499
+ * @param opt_position The position for the new context. If not provided, the new context inherits the position of the current context.
12500
+ * @returns A new ExprContext instance with the specified node list and position, and the current context as its parent.
12477
12501
  */
12478
12502
  clone(opt_nodeList, opt_position) {
12479
12503
  return new _ExprContext(
@@ -12481,7 +12505,7 @@ var ExprContext = class _ExprContext {
12481
12505
  this.xsltVersion,
12482
12506
  typeof opt_position !== "undefined" ? opt_position : this.position,
12483
12507
  this.decimalFormatSettings,
12484
- this.variables,
12508
+ Object.create(this.variables || {}),
12485
12509
  this.knownNamespaces,
12486
12510
  this,
12487
12511
  this.caseInsensitive,