@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
@@ -0,0 +1,200 @@
1
+ /**
2
+ * `xsl:strip-space` and `xsl:preserve-space` support.
3
+ *
4
+ * XSLT 1.0 removes whitespace-only text nodes from the source tree before the
5
+ * transformation starts. This module builds a stripped copy of the source so
6
+ * the caller's document is never mutated, and resolves the usual conflict rules
7
+ * between the two declarations.
8
+ */
9
+
10
+ "use strict";
11
+
12
+ import { isTextContinuation, isTextNode } from "../xpath/axes.js";
13
+ import { matchesNameTest, nameTestPriority } from "./spaceNameTests.js";
14
+
15
+ /** Node type of a document type declaration, which cannot be imported. */
16
+ const DOCUMENT_TYPE_NODE = 10;
17
+
18
+ /** Text made only of XML whitespace: #x20, #x9, #xD and #xA (not NBSP). */
19
+ const XML_WHITESPACE_ONLY = /^[ \t\r\n]*$/;
20
+
21
+ /**
22
+ * Whether a string consists of XML whitespace only (XML 1.0 production S).
23
+ * Unlike `trim()` and `\s`, a no-break space is not whitespace.
24
+ *
25
+ * @param {string} text - Any string
26
+ * @returns {boolean} True for "" and strings of #x20 #x9 #xD #xA
27
+ *
28
+ * @example
29
+ * isXmlWhitespace(" \n"); // true
30
+ * isXmlWhitespace("\u00a0"); // false
31
+ */
32
+ export function isXmlWhitespace(text) {
33
+ return XML_WHITESPACE_ONLY.test(text);
34
+ }
35
+
36
+ /**
37
+ * The DOM nodes of the text run starting at a node: the node and every
38
+ * directly following Text/CDATA sibling (one XPath text node).
39
+ *
40
+ * @param {Node} first - The first node of the run
41
+ * @returns {Node[]} The nodes of the run
42
+ */
43
+ export function textRun(first) {
44
+ const run = [first];
45
+ for (
46
+ let next = first.nextSibling;
47
+ isTextNode(next);
48
+ next = next.nextSibling
49
+ ) {
50
+ run.push(next);
51
+ }
52
+ return run;
53
+ }
54
+
55
+ /**
56
+ * Decides which whitespace-only text nodes of the source tree are removed.
57
+ */
58
+ export class WhitespaceFilter {
59
+ /**
60
+ * @param {Array<string|import('./spaceNameTests.js').ElementNameTest>} [stripSpace] -
61
+ * Name tests from `xsl:strip-space`: expanded (see spaceNameTests.js),
62
+ * or strings matched by their lexical name
63
+ * @param {Array<string|import('./spaceNameTests.js').ElementNameTest>} [preserveSpace] -
64
+ * Name tests from `xsl:preserve-space`
65
+ */
66
+ constructor(stripSpace = [], preserveSpace = []) {
67
+ this.stripSpace = stripSpace;
68
+ this.preserveSpace = preserveSpace;
69
+ }
70
+
71
+ /**
72
+ * Whether the filter can remove anything at all.
73
+ *
74
+ * @returns {boolean} True when at least one `xsl:strip-space` was declared
75
+ *
76
+ * @example
77
+ * new WhitespaceFilter(['*']).isActive(); // true
78
+ */
79
+ isActive() {
80
+ return this.stripSpace.length > 0;
81
+ }
82
+
83
+ /**
84
+ * Whether whitespace-only children of an element are stripped.
85
+ *
86
+ * The most specific name test wins; `xsl:preserve-space` wins ties.
87
+ *
88
+ * @param {Element} element - The parent element
89
+ * @returns {boolean} True when whitespace-only text children are removed
90
+ *
91
+ * @example
92
+ * new WhitespaceFilter(['*'], ['pre']).isStripped(preElement); // false
93
+ */
94
+ isStripped(element) {
95
+ let stripPriority = -Infinity;
96
+ let preservePriority = -Infinity;
97
+
98
+ for (const nameTest of this.stripSpace) {
99
+ if (matchesNameTest(element, nameTest)) {
100
+ stripPriority = Math.max(stripPriority, nameTestPriority(nameTest));
101
+ }
102
+ }
103
+
104
+ for (const nameTest of this.preserveSpace) {
105
+ if (matchesNameTest(element, nameTest)) {
106
+ preservePriority = Math.max(
107
+ preservePriority,
108
+ nameTestPriority(nameTest),
109
+ );
110
+ }
111
+ }
112
+
113
+ return stripPriority > -Infinity && stripPriority > preservePriority;
114
+ }
115
+ }
116
+
117
+ /**
118
+ * Check whether `xml:space="preserve"` is in scope for a node.
119
+ *
120
+ * @param {Node} node - The node to inspect
121
+ * @returns {boolean} True when whitespace must be preserved
122
+ */
123
+ function hasXmlSpacePreserve(node) {
124
+ let current = node;
125
+
126
+ while (current?.nodeType === 1) {
127
+ const value = current.getAttribute("xml:space");
128
+ if (value === "preserve") return true;
129
+ if (value === "default") return false;
130
+ current = current.parentNode;
131
+ }
132
+
133
+ return false;
134
+ }
135
+
136
+ /**
137
+ * Remove whitespace-only text nodes from a subtree. Adjacent Text/CDATA
138
+ * nodes form one text node and are only removed together.
139
+ *
140
+ * @param {Node} root - The root of the subtree to prune
141
+ * @param {WhitespaceFilter} filter - The configured filter
142
+ * @returns {void}
143
+ */
144
+ function pruneWhitespace(root, filter) {
145
+ const doomed = [];
146
+ const stack = [root];
147
+
148
+ while (stack.length > 0) {
149
+ const current = stack.pop();
150
+
151
+ if (
152
+ isTextNode(current) &&
153
+ !isTextContinuation(current) &&
154
+ current.parentNode?.nodeType === 1 &&
155
+ filter.isStripped(current.parentNode) &&
156
+ !hasXmlSpacePreserve(current.parentNode)
157
+ ) {
158
+ const run = textRun(current);
159
+ if (run.every((node) => isXmlWhitespace(node.nodeValue))) {
160
+ doomed.push(...run);
161
+ }
162
+ }
163
+
164
+ const children = current.childNodes;
165
+ if (children) {
166
+ for (let i = children.length - 1; i >= 0; i--) stack.push(children[i]);
167
+ }
168
+ }
169
+
170
+ for (const node of doomed) node.parentNode.removeChild(node);
171
+ }
172
+
173
+ /**
174
+ * Build a copy of the source tree with stripped whitespace removed.
175
+ *
176
+ * @param {Node} sourceNode - The source document or element
177
+ * @param {WhitespaceFilter} filter - The configured filter
178
+ * @param {Document} targetDoc - An empty document owning the copy
179
+ * @returns {Node} The stripped copy, a document when the source was a document
180
+ *
181
+ * @example
182
+ * const stripped = stripWhitespaceNodes(xmlDoc, filter, emptyDoc);
183
+ */
184
+ export function stripWhitespaceNodes(sourceNode, filter, targetDoc) {
185
+ let root;
186
+
187
+ if (sourceNode.nodeType === 9) {
188
+ root = targetDoc;
189
+ for (const child of Array.from(sourceNode.childNodes)) {
190
+ if (child.nodeType === DOCUMENT_TYPE_NODE) continue;
191
+ targetDoc.appendChild(targetDoc.importNode(child, true));
192
+ }
193
+ } else {
194
+ root = targetDoc.importNode(sourceNode, true);
195
+ targetDoc.appendChild(root);
196
+ }
197
+
198
+ pruneWhitespace(root, filter);
199
+ return root;
200
+ }
package/LICENSE DELETED
@@ -1,29 +0,0 @@
1
- BSD 3-Clause License
2
-
3
- Copyright (c) 2026, XSLT-Processor Contributors
4
- All rights reserved.
5
-
6
- Redistribution and use in source and binary forms, with or without
7
- modification, are permitted provided that the following conditions are met:
8
-
9
- 1. Redistributions of source code must retain the above copyright notice, this
10
- list of conditions and the following disclaimer.
11
-
12
- 2. Redistributions in binary form must reproduce the above copyright notice,
13
- this list of conditions and the following disclaimer in the documentation
14
- and/or other materials provided with the distribution.
15
-
16
- 3. Neither the name of the copyright holder nor the names of its
17
- contributors may be used to endorse or promote products derived from
18
- this software without specific prior written permission.
19
-
20
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
- DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24
- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
- SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
- CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
- OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.