xslt-processor 5.0.3 → 5.0.5

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
@@ -296,7 +296,7 @@ console.log(result2); // "<users><user>John</user></users>" (XML)
296
296
  You can simply add a tag like this:
297
297
 
298
298
  ```html
299
- <script type="application/javascript" src="https://unpkg.com/xslt-processor@latest/umd/xslt-processor.global.js"></script>
299
+ <script type="application/javascript" src="https://unpkg.com/xslt-processor@latest/dist/umd/xslt-processor.global.js"></script>
300
300
  ```
301
301
 
302
302
  All the exports will live under `globalThis.XsltProcessor` and `window.XsltProcessor`. [See a usage example here](https://github.com/DesignLiquido/xslt-processor/blob/main/interactive-tests/xslt.html).
package/dist/README.md CHANGED
@@ -296,7 +296,7 @@ console.log(result2); // "<users><user>John</user></users>" (XML)
296
296
  You can simply add a tag like this:
297
297
 
298
298
  ```html
299
- <script type="application/javascript" src="https://unpkg.com/xslt-processor@latest/umd/xslt-processor.global.js"></script>
299
+ <script type="application/javascript" src="https://unpkg.com/xslt-processor@latest/dist/umd/xslt-processor.global.js"></script>
300
300
  ```
301
301
 
302
302
  All the exports will live under `globalThis.XsltProcessor` and `window.XsltProcessor`. [See a usage example here](https://github.com/DesignLiquido/xslt-processor/blob/main/interactive-tests/xslt.html).
package/dist/index.d.mts CHANGED
@@ -1263,6 +1263,12 @@ declare class Xslt {
1263
1263
  * @param template The template.
1264
1264
  */
1265
1265
  protected xsltKey(context: ExprContext, template: XNode): void;
1266
+ /**
1267
+ * Returns all descendant nodes of the given node, depth-first.
1268
+ * @param node The root node to traverse from.
1269
+ * @returns All descendant nodes.
1270
+ */
1271
+ private collectAllDescendants;
1266
1272
  /**
1267
1273
  * Implements `xsl:message`.
1268
1274
  * Outputs a message to the console. If terminate="yes", throws an error to stop processing.
package/dist/index.d.ts CHANGED
@@ -1263,6 +1263,12 @@ declare class Xslt {
1263
1263
  * @param template The template.
1264
1264
  */
1265
1265
  protected xsltKey(context: ExprContext, template: XNode): void;
1266
+ /**
1267
+ * Returns all descendant nodes of the given node, depth-first.
1268
+ * @param node The root node to traverse from.
1269
+ * @returns All descendant nodes.
1270
+ */
1271
+ private collectAllDescendants;
1266
1272
  /**
1267
1273
  * Implements `xsl:message`.
1268
1274
  * Outputs a message to the console. If terminate="yes", throws an error to stop processing.
package/dist/index.js CHANGED
@@ -6328,9 +6328,9 @@ var init_function_call_expression = __esm({
6328
6328
  case "concat":
6329
6329
  return evaluatedArgs.map((arg) => this.convertToString(arg)).join("");
6330
6330
  case "starts-with":
6331
- return String(evaluatedArgs[0]).startsWith(String(evaluatedArgs[1]));
6331
+ return this.convertToString(evaluatedArgs[0]).startsWith(this.convertToString(evaluatedArgs[1]));
6332
6332
  case "contains":
6333
- return String(evaluatedArgs[0]).includes(String(evaluatedArgs[1]));
6333
+ return this.convertToString(evaluatedArgs[0]).includes(this.convertToString(evaluatedArgs[1]));
6334
6334
  case "substring-before":
6335
6335
  return this.substringBefore(evaluatedArgs);
6336
6336
  case "substring-after":
@@ -6476,13 +6476,13 @@ var init_function_call_expression = __esm({
6476
6476
  return Number(value);
6477
6477
  }
6478
6478
  stringValue(args, context) {
6479
- var _a, _b, _c, _d;
6479
+ var _a, _b;
6480
6480
  if (args.length === 0) {
6481
6481
  return (_b = (_a = context.node) == null ? void 0 : _a.textContent) != null ? _b : "";
6482
6482
  }
6483
6483
  const value = args[0];
6484
6484
  if (Array.isArray(value) && value.length > 0) {
6485
- return (_d = (_c = value[0]) == null ? void 0 : _c.textContent) != null ? _d : String(value[0]);
6485
+ return this.getNodeStringValue(value[0]);
6486
6486
  }
6487
6487
  return String(value);
6488
6488
  }
@@ -6515,6 +6515,9 @@ var init_function_call_expression = __esm({
6515
6515
  if (!node) {
6516
6516
  return "";
6517
6517
  }
6518
+ if (typeof node === "string") return node;
6519
+ if (typeof node === "number") return String(node);
6520
+ if (typeof node === "boolean") return node ? "true" : "false";
6518
6521
  if (typeof node.textContent === "string") {
6519
6522
  return node.textContent;
6520
6523
  }
@@ -6552,26 +6555,26 @@ var init_function_call_expression = __esm({
6552
6555
  if (args.length === 0) {
6553
6556
  return this.stringValue([], context).length;
6554
6557
  }
6555
- return String(args[0]).length;
6558
+ return this.convertToString(args[0]).length;
6556
6559
  }
6557
6560
  normalizeSpace(args, context) {
6558
- const str = args.length === 0 ? this.stringValue([], context) : String(args[0]);
6561
+ const str = args.length === 0 ? this.stringValue([], context) : this.convertToString(args[0]);
6559
6562
  return str.trim().replace(/\s+/g, " ");
6560
6563
  }
6561
6564
  substringBefore(args) {
6562
- const str = String(args[0]);
6563
- const search = String(args[1]);
6565
+ const str = this.convertToString(args[0]);
6566
+ const search = this.convertToString(args[1]);
6564
6567
  const index = str.indexOf(search);
6565
6568
  return index === -1 ? "" : str.substring(0, index);
6566
6569
  }
6567
6570
  substringAfter(args) {
6568
- const str = String(args[0]);
6569
- const search = String(args[1]);
6571
+ const str = this.convertToString(args[0]);
6572
+ const search = this.convertToString(args[1]);
6570
6573
  const index = str.indexOf(search);
6571
6574
  return index === -1 ? "" : str.substring(index + search.length);
6572
6575
  }
6573
6576
  substring(args) {
6574
- const str = String(args[0]);
6577
+ const str = this.convertToString(args[0]);
6575
6578
  const start = Math.round(Number(args[1])) - 1;
6576
6579
  if (args.length === 2) {
6577
6580
  return str.substring(Math.max(0, start));
@@ -6585,9 +6588,9 @@ var init_function_call_expression = __esm({
6585
6588
  return str.substring(adjustedStart, adjustedStart + adjustedLength);
6586
6589
  }
6587
6590
  translate(args) {
6588
- const str = String(args[0]);
6589
- const from = String(args[1]);
6590
- const to = String(args[2]);
6591
+ const str = this.convertToString(args[0]);
6592
+ const from = this.convertToString(args[1]);
6593
+ const to = this.convertToString(args[2]);
6591
6594
  let result = "";
6592
6595
  for (const char of str) {
6593
6596
  const index = from.indexOf(char);
@@ -6625,7 +6628,7 @@ var init_function_call_expression = __esm({
6625
6628
  if (!Array.isArray(nodeSet)) return 0;
6626
6629
  return nodeSet.reduce((acc, node) => {
6627
6630
  var _a;
6628
- const value = Number((_a = node == null ? void 0 : node.textContent) != null ? _a : node);
6631
+ const value = Number((_a = node == null ? void 0 : node.textContent) != null ? _a : this.getNodeStringValue(node));
6629
6632
  return acc + (isNaN(value) ? 0 : value);
6630
6633
  }, 0);
6631
6634
  }
@@ -14540,7 +14543,7 @@ var Xslt = class {
14540
14543
  return;
14541
14544
  }
14542
14545
  const childNodes = node.childNodes.filter(
14543
- (n) => n.nodeType !== DOM_ATTRIBUTE_NODE
14546
+ (n) => n.nodeType !== DOM_ATTRIBUTE_NODE && n.nodeName !== "#dtd-section"
14544
14547
  );
14545
14548
  for (const childNode of childNodes) {
14546
14549
  if (childNode.nodeType === DOM_TEXT_NODE) {
@@ -16226,23 +16229,36 @@ var Xslt = class {
16226
16229
  errorMessage = errorMessage.slice(0, -2);
16227
16230
  throw new Error(errorMessage);
16228
16231
  }
16229
- let keyContext;
16230
- if (context.nodeList[context.position].nodeName === "#document") {
16231
- keyContext = context.clone(context.nodeList[context.position].childNodes);
16232
- } else {
16233
- keyContext = context;
16234
- }
16235
- const nodes = this.xsltMatch(match, keyContext);
16236
16232
  if (!(name in context.keys)) {
16237
16233
  context.keys[name] = {};
16238
16234
  }
16239
- for (const node of nodes) {
16240
- const nodeContext = context.clone([node]);
16241
- const attribute = this.xPath.xPathEval(use, nodeContext);
16242
- const attributeValue = attribute.stringValue();
16243
- context.keys[name][attributeValue] = new NodeSetValue([node]);
16235
+ const allNodes = this.collectAllDescendants(context.root);
16236
+ for (const node of allNodes) {
16237
+ if (nodeMatchesPattern(node, match, context, this.matchResolver, this.xPath)) {
16238
+ const nodeContext = context.clone([node]);
16239
+ const attribute = this.xPath.xPathEval(use, nodeContext);
16240
+ const attributeValue = attribute.stringValue();
16241
+ if (!(attributeValue in context.keys[name])) {
16242
+ context.keys[name][attributeValue] = new NodeSetValue([node]);
16243
+ } else {
16244
+ context.keys[name][attributeValue].nodeSetValue().push(node);
16245
+ }
16246
+ }
16244
16247
  }
16245
16248
  }
16249
+ /**
16250
+ * Returns all descendant nodes of the given node, depth-first.
16251
+ * @param node The root node to traverse from.
16252
+ * @returns All descendant nodes.
16253
+ */
16254
+ collectAllDescendants(node) {
16255
+ const result = [];
16256
+ for (const child of node.childNodes || []) {
16257
+ result.push(child);
16258
+ result.push(...this.collectAllDescendants(child));
16259
+ }
16260
+ return result;
16261
+ }
16246
16262
  /**
16247
16263
  * Implements `xsl:message`.
16248
16264
  * Outputs a message to the console. If terminate="yes", throws an error to stop processing.
@@ -16964,7 +16980,7 @@ var Xslt = class {
16964
16980
  */
16965
16981
  xsltTransformOrStylesheet(context, template, output) {
16966
16982
  return __async(this, null, function* () {
16967
- var _a, _b, _c;
16983
+ var _a;
16968
16984
  const mainStylesheetMetadata = {
16969
16985
  importDepth: 0,
16970
16986
  href: "(main stylesheet)",
@@ -17055,80 +17071,8 @@ var Xslt = class {
17055
17071
  this.currentTemplateStack.pop();
17056
17072
  } else {
17057
17073
  const rootNode = context.nodeList[context.position];
17058
- if (rootNode && rootNode.childNodes && rootNode.childNodes.length > 0) {
17059
- const childNodes = rootNode.childNodes.filter((n) => n.nodeName !== "#dtd-section");
17060
- if (childNodes.length > 0) {
17061
- const childContext = context.clone(childNodes);
17062
- for (let j = 0; j < childContext.contextSize(); ++j) {
17063
- const currentNode = childContext.nodeList[j];
17064
- if (currentNode.nodeType === DOM_TEXT_NODE) {
17065
- const textNodeContext = context.clone([currentNode], 0);
17066
- this.commonLogicTextNode(textNodeContext, currentNode, output);
17067
- } else {
17068
- const clonedContext = childContext.clone([currentNode], 0);
17069
- const selection = selectBestTemplate(
17070
- expandedTemplates,
17071
- clonedContext,
17072
- this.matchResolver,
17073
- this.xPath,
17074
- this.warningsCallback
17075
- );
17076
- if (selection.selectedTemplate) {
17077
- const templateContext = clonedContext.clone([currentNode], 0);
17078
- templateContext.inApplyTemplates = true;
17079
- const metadata = this.templateSourceMap.get(selection.selectedTemplate);
17080
- const matchPattern = xmlGetAttribute(selection.selectedTemplate, "match");
17081
- const modeAttr = xmlGetAttribute(selection.selectedTemplate, "mode");
17082
- this.currentTemplateStack.push({
17083
- template: selection.selectedTemplate,
17084
- stylesheetDepth: (_b = metadata == null ? void 0 : metadata.importDepth) != null ? _b : 0,
17085
- mode: modeAttr || null,
17086
- match: matchPattern
17087
- });
17088
- yield this.xsltChildNodes(templateContext, selection.selectedTemplate, output);
17089
- this.currentTemplateStack.pop();
17090
- } else {
17091
- if (currentNode.childNodes && currentNode.childNodes.length > 0) {
17092
- const grandchildNodes = currentNode.childNodes.filter((n) => n.nodeName !== "#dtd-section");
17093
- if (grandchildNodes.length > 0) {
17094
- const grandchildContext = context.clone(grandchildNodes);
17095
- for (let k = 0; k < grandchildContext.contextSize(); ++k) {
17096
- const grandchildNode = grandchildContext.nodeList[k];
17097
- if (grandchildNode.nodeType === DOM_TEXT_NODE) {
17098
- const textNodeContext = context.clone([grandchildNode], 0);
17099
- this.commonLogicTextNode(textNodeContext, grandchildNode, output);
17100
- } else {
17101
- const grandchildClonedContext = grandchildContext.clone([grandchildNode], 0);
17102
- const grandchildSelection = selectBestTemplate(
17103
- expandedTemplates,
17104
- grandchildClonedContext,
17105
- this.matchResolver,
17106
- this.xPath,
17107
- this.warningsCallback
17108
- );
17109
- if (grandchildSelection.selectedTemplate) {
17110
- const grandchildTemplateContext = grandchildClonedContext.clone([grandchildNode], 0);
17111
- grandchildTemplateContext.inApplyTemplates = true;
17112
- const metadata = this.templateSourceMap.get(grandchildSelection.selectedTemplate);
17113
- const matchPattern = xmlGetAttribute(grandchildSelection.selectedTemplate, "match");
17114
- const modeAttr = xmlGetAttribute(grandchildSelection.selectedTemplate, "mode");
17115
- this.currentTemplateStack.push({
17116
- template: grandchildSelection.selectedTemplate,
17117
- stylesheetDepth: (_c = metadata == null ? void 0 : metadata.importDepth) != null ? _c : 0,
17118
- mode: modeAttr || null,
17119
- match: matchPattern
17120
- });
17121
- yield this.xsltChildNodes(grandchildTemplateContext, grandchildSelection.selectedTemplate, output);
17122
- this.currentTemplateStack.pop();
17123
- }
17124
- }
17125
- }
17126
- }
17127
- }
17128
- }
17129
- }
17130
- }
17131
- }
17074
+ if (rootNode) {
17075
+ yield this.applyBuiltInTemplate(rootNode, expandedTemplates, null, context, output);
17132
17076
  }
17133
17077
  }
17134
17078
  }