@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.
- package/LICENSE.md +1 -1
- package/README.md +110 -520
- package/bin/lib/decode.js +15 -0
- package/bin/lib/dom.js +177 -0
- package/bin/lib/loaders.js +127 -0
- package/bin/lib/options.js +131 -0
- package/bin/lib/output.js +114 -0
- package/bin/lib/paths.js +186 -0
- package/bin/lib/transform.js +206 -0
- package/bin/xslt.js +73 -168
- package/dist/xslt-processor.browser.js +9564 -1585
- package/dist/xslt-processor.browser.js.map +4 -4
- package/dist/xslt-processor.browser.min.js +13 -2
- package/dist/xslt-processor.browser.min.js.map +4 -4
- package/dist/xslt-processor.cjs +9572 -1586
- package/dist/xslt-processor.cjs.map +4 -4
- package/dist/xslt-processor.d.cts +658 -0
- package/dist/xslt-processor.d.ts +459 -12
- package/dist/xslt-processor.js +9546 -1582
- package/dist/xslt-processor.js.map +4 -4
- package/package.json +71 -20
- package/src/XSLTProcessor.js +494 -48
- package/src/async/abort.js +63 -0
- package/src/async/documentUris.js +128 -0
- package/src/async/loaders.js +134 -0
- package/src/async/preload.js +159 -0
- package/src/async/processor.js +206 -0
- package/src/async/stream.js +125 -0
- package/src/bridge/engine.js +221 -0
- package/src/bridge/loader.js +78 -0
- package/src/bridge/results.js +75 -0
- package/src/bridge/version.js +63 -0
- package/src/index.js +26 -8
- package/src/io/decode.js +140 -0
- package/src/io/readSource.js +167 -0
- package/src/xpath/axes.js +562 -0
- package/src/xpath/documentOrder.js +270 -0
- package/src/xpath/evaluator.js +518 -357
- package/src/xpath/index.js +8 -2
- package/src/xpath/namespaceNodes.js +172 -0
- package/src/xpath/nodeSetFunctions.js +169 -0
- package/src/xpath/parser.js +30 -5
- package/src/xpath/strings.js +183 -0
- package/src/xpath/tokenizer.js +37 -23
- package/src/xslt/attributeSets.js +95 -0
- package/src/xslt/avt.js +103 -0
- package/src/xslt/computedNames.js +91 -0
- package/src/xslt/copying.js +212 -0
- package/src/xslt/declarationNames.js +80 -0
- package/src/xslt/domParsing.js +95 -0
- package/src/xslt/elements.js +57 -0
- package/src/xslt/engine/bindings.js +195 -0
- package/src/xslt/engine/context.js +105 -0
- package/src/xslt/engine/controlFlow.js +145 -0
- package/src/xslt/engine/copyInstructions.js +133 -0
- package/src/xslt/engine/declarations.js +233 -0
- package/src/xslt/engine/functionSupport.js +103 -0
- package/src/xslt/engine/methods.js +33 -0
- package/src/xslt/engine/nodeConstruction.js +187 -0
- package/src/xslt/engine/numbering.js +104 -0
- package/src/xslt/engine/outputDeclaration.js +77 -0
- package/src/xslt/engine/sequenceConstructor.js +228 -0
- package/src/xslt/engine/stylesheetLoading.js +208 -0
- package/src/xslt/engine/templateInvocation.js +253 -0
- package/src/xslt/engine/templateRules.js +243 -0
- package/src/xslt/engine/textInstructions.js +171 -0
- package/src/xslt/engine/topLevel.js +130 -0
- package/src/xslt/engine/transformation.js +263 -0
- package/src/xslt/engine/workStack.js +245 -0
- package/src/xslt/engine.js +184 -1736
- package/src/xslt/exslt/arguments.js +99 -0
- package/src/xslt/exslt/calendar.js +120 -0
- package/src/xslt/exslt/common.js +44 -0
- package/src/xslt/exslt/dateCalc.js +261 -0
- package/src/xslt/exslt/dateFormat.js +150 -0
- package/src/xslt/exslt/dateParse.js +265 -0
- package/src/xslt/exslt/dates.js +259 -0
- package/src/xslt/exslt/duration.js +207 -0
- package/src/xslt/exslt/dynamic.js +59 -0
- package/src/xslt/exslt/index.js +59 -0
- package/src/xslt/exslt/math.js +177 -0
- package/src/xslt/exslt/sets.js +96 -0
- package/src/xslt/exslt/stringOps.js +163 -0
- package/src/xslt/exslt/strings.js +147 -0
- package/src/xslt/exslt/uri.js +92 -0
- package/src/xslt/formatNumber.js +233 -0
- package/src/xslt/forwardsCompatible.js +75 -0
- package/src/xslt/functions.js +270 -0
- package/src/xslt/index.js +38 -1
- package/src/xslt/keys.js +164 -0
- package/src/xslt/literalResult.js +223 -0
- package/src/xslt/matchScope.js +116 -0
- package/src/xslt/number.js +271 -0
- package/src/xslt/numberFormat.js +253 -0
- package/src/xslt/outputNames.js +58 -0
- package/src/xslt/patternCompiler.js +175 -0
- package/src/xslt/patterns.js +324 -0
- package/src/xslt/qname.js +90 -0
- package/src/xslt/resultDocument.js +98 -0
- package/src/xslt/resultNamespaces.js +219 -0
- package/src/xslt/resultTree.js +211 -0
- package/src/xslt/serializer/baseWriter.js +390 -0
- package/src/xslt/serializer/chunks.js +120 -0
- package/src/xslt/serializer/constants.js +92 -0
- package/src/xslt/serializer/encoding.js +327 -0
- package/src/xslt/serializer/escape.js +135 -0
- package/src/xslt/serializer/frames.js +168 -0
- package/src/xslt/serializer/htmlDoctype.js +102 -0
- package/src/xslt/serializer/htmlEntities.js +77 -0
- package/src/xslt/serializer/htmlSerializer.js +239 -0
- package/src/xslt/serializer/indent.js +51 -0
- package/src/xslt/serializer/namespaces.js +68 -0
- package/src/xslt/serializer/rawText.js +41 -0
- package/src/xslt/serializer/settings.js +179 -0
- package/src/xslt/serializer/textSerializer.js +77 -0
- package/src/xslt/serializer/xhtmlDocument.js +103 -0
- package/src/xslt/serializer/xmlSerializer.js +227 -0
- package/src/xslt/serializer.js +90 -0
- package/src/xslt/sort.js +151 -0
- package/src/xslt/spaceNameTests.js +115 -0
- package/src/xslt/stylesheetChecks.js +206 -0
- package/src/xslt/stylesheetNamespaces.js +266 -0
- package/src/xslt/templatePriority.js +45 -0
- package/src/xslt/uri.js +68 -0
- package/src/xslt/variables.js +152 -0
- package/src/xslt/whitespace.js +200 -0
- package/LICENSE +0 -29
- package/src/XSLTProcessor.test.js +0 -930
- package/src/xpath/evaluator.test.js +0 -1852
- package/src/xpath/tokenizer.test.js +0 -224
- 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.
|