@tradik/xslt-processor 1.0.3 → 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.
- package/README.md +290 -45
- package/bin/lib/options.js +114 -0
- package/bin/lib/paths.js +186 -0
- package/bin/lib/transform.js +115 -0
- package/bin/xslt.js +68 -162
- package/dist/xslt-processor.browser.js +2074 -159
- package/dist/xslt-processor.browser.js.map +4 -4
- package/dist/xslt-processor.browser.min.js +6 -2
- package/dist/xslt-processor.browser.min.js.map +4 -4
- package/dist/xslt-processor.cjs +2078 -158
- package/dist/xslt-processor.cjs.map +4 -4
- package/dist/xslt-processor.d.cts +299 -0
- package/dist/xslt-processor.d.ts +92 -4
- package/dist/xslt-processor.js +2073 -157
- package/dist/xslt-processor.js.map +4 -4
- package/package.json +26 -15
- package/src/XSLTProcessor.js +177 -8
- package/src/index.js +11 -5
- package/src/xpath/evaluator.js +48 -7
- package/src/xslt/elements.js +57 -0
- package/src/xslt/engine.js +471 -179
- package/src/xslt/formatNumber.js +220 -0
- package/src/xslt/functions.js +191 -0
- package/src/xslt/index.js +31 -0
- package/src/xslt/keys.js +141 -0
- package/src/xslt/literalResult.js +167 -0
- package/src/xslt/number.js +178 -0
- package/src/xslt/numberFormat.js +155 -0
- package/src/xslt/resultTree.js +74 -0
- package/src/xslt/serializer/baseWriter.js +283 -0
- package/src/xslt/serializer/constants.js +78 -0
- package/src/xslt/serializer/escape.js +98 -0
- package/src/xslt/serializer/htmlSerializer.js +141 -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 +103 -0
- package/src/xslt/serializer/textSerializer.js +29 -0
- package/src/xslt/serializer/xmlSerializer.js +127 -0
- package/src/xslt/serializer.js +57 -0
- package/src/xslt/templatePriority.js +45 -0
- package/src/xslt/uri.js +68 -0
- package/src/xslt/whitespace.js +184 -0
- 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,184 @@
|
|
|
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
|
+
/** Node type of a document type declaration, which cannot be imported. */
|
|
13
|
+
const DOCUMENT_TYPE_NODE = 10;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Compute the XSLT default priority of an element name test.
|
|
17
|
+
*
|
|
18
|
+
* @param {string} nameTest - A name test such as `*`, `ns:*` or `item`
|
|
19
|
+
* @returns {number} The default priority
|
|
20
|
+
*/
|
|
21
|
+
function nameTestPriority(nameTest) {
|
|
22
|
+
if (nameTest === "*") return -0.5;
|
|
23
|
+
if (nameTest.endsWith(":*")) return -0.25;
|
|
24
|
+
return 0;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Check whether an element matches a name test.
|
|
29
|
+
*
|
|
30
|
+
* @param {Element} element - The element to test
|
|
31
|
+
* @param {string} nameTest - A name test such as `*`, `ns:*` or `item`
|
|
32
|
+
* @returns {boolean} True when the element matches
|
|
33
|
+
*/
|
|
34
|
+
function matchesNameTest(element, nameTest) {
|
|
35
|
+
if (nameTest === "*") return true;
|
|
36
|
+
|
|
37
|
+
if (nameTest.endsWith(":*")) {
|
|
38
|
+
const prefix = nameTest.slice(0, -2);
|
|
39
|
+
return element.nodeName.startsWith(`${prefix}:`);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return element.nodeName === nameTest || element.localName === nameTest;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Decides which whitespace-only text nodes of the source tree are removed.
|
|
47
|
+
*/
|
|
48
|
+
export class WhitespaceFilter {
|
|
49
|
+
/**
|
|
50
|
+
* @param {string[]} [stripSpace] - Name tests from `xsl:strip-space`
|
|
51
|
+
* @param {string[]} [preserveSpace] - Name tests from `xsl:preserve-space`
|
|
52
|
+
*/
|
|
53
|
+
constructor(stripSpace = [], preserveSpace = []) {
|
|
54
|
+
this.stripSpace = stripSpace;
|
|
55
|
+
this.preserveSpace = preserveSpace;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Whether the filter can remove anything at all.
|
|
60
|
+
*
|
|
61
|
+
* @returns {boolean} True when at least one `xsl:strip-space` was declared
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* new WhitespaceFilter(['*']).isActive(); // true
|
|
65
|
+
*/
|
|
66
|
+
isActive() {
|
|
67
|
+
return this.stripSpace.length > 0;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Whether whitespace-only children of an element are stripped.
|
|
72
|
+
*
|
|
73
|
+
* The most specific name test wins; `xsl:preserve-space` wins ties.
|
|
74
|
+
*
|
|
75
|
+
* @param {Element} element - The parent element
|
|
76
|
+
* @returns {boolean} True when whitespace-only text children are removed
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* new WhitespaceFilter(['*'], ['pre']).isStripped(preElement); // false
|
|
80
|
+
*/
|
|
81
|
+
isStripped(element) {
|
|
82
|
+
let stripPriority = -Infinity;
|
|
83
|
+
let preservePriority = -Infinity;
|
|
84
|
+
|
|
85
|
+
for (const nameTest of this.stripSpace) {
|
|
86
|
+
if (matchesNameTest(element, nameTest)) {
|
|
87
|
+
stripPriority = Math.max(stripPriority, nameTestPriority(nameTest));
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
for (const nameTest of this.preserveSpace) {
|
|
92
|
+
if (matchesNameTest(element, nameTest)) {
|
|
93
|
+
preservePriority = Math.max(
|
|
94
|
+
preservePriority,
|
|
95
|
+
nameTestPriority(nameTest),
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return stripPriority > -Infinity && stripPriority > preservePriority;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Check whether `xml:space="preserve"` is in scope for a node.
|
|
106
|
+
*
|
|
107
|
+
* @param {Node} node - The node to inspect
|
|
108
|
+
* @returns {boolean} True when whitespace must be preserved
|
|
109
|
+
*/
|
|
110
|
+
function hasXmlSpacePreserve(node) {
|
|
111
|
+
let current = node;
|
|
112
|
+
|
|
113
|
+
while (current?.nodeType === 1) {
|
|
114
|
+
const value = current.getAttribute("xml:space");
|
|
115
|
+
if (value === "preserve") return true;
|
|
116
|
+
if (value === "default") return false;
|
|
117
|
+
current = current.parentNode;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Remove whitespace-only text nodes from a subtree.
|
|
125
|
+
*
|
|
126
|
+
* @param {Node} root - The root of the subtree to prune
|
|
127
|
+
* @param {WhitespaceFilter} filter - The configured filter
|
|
128
|
+
* @returns {void}
|
|
129
|
+
*/
|
|
130
|
+
function pruneWhitespace(root, filter) {
|
|
131
|
+
const doomed = [];
|
|
132
|
+
const stack = [root];
|
|
133
|
+
|
|
134
|
+
while (stack.length > 0) {
|
|
135
|
+
const current = stack.pop();
|
|
136
|
+
|
|
137
|
+
if (
|
|
138
|
+
(current.nodeType === 3 || current.nodeType === 4) &&
|
|
139
|
+
current.nodeValue !== null &&
|
|
140
|
+
current.nodeValue.trim() === "" &&
|
|
141
|
+
current.parentNode?.nodeType === 1 &&
|
|
142
|
+
filter.isStripped(current.parentNode) &&
|
|
143
|
+
!hasXmlSpacePreserve(current.parentNode)
|
|
144
|
+
) {
|
|
145
|
+
doomed.push(current);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const children = current.childNodes;
|
|
149
|
+
if (children) {
|
|
150
|
+
for (let i = children.length - 1; i >= 0; i--) stack.push(children[i]);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
for (const node of doomed) node.remove();
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Build a copy of the source tree with stripped whitespace removed.
|
|
159
|
+
*
|
|
160
|
+
* @param {Node} sourceNode - The source document or element
|
|
161
|
+
* @param {WhitespaceFilter} filter - The configured filter
|
|
162
|
+
* @param {Document} targetDoc - An empty document owning the copy
|
|
163
|
+
* @returns {Node} The stripped copy, a document when the source was a document
|
|
164
|
+
*
|
|
165
|
+
* @example
|
|
166
|
+
* const stripped = stripWhitespaceNodes(xmlDoc, filter, emptyDoc);
|
|
167
|
+
*/
|
|
168
|
+
export function stripWhitespaceNodes(sourceNode, filter, targetDoc) {
|
|
169
|
+
let root;
|
|
170
|
+
|
|
171
|
+
if (sourceNode.nodeType === 9) {
|
|
172
|
+
root = targetDoc;
|
|
173
|
+
for (const child of Array.from(sourceNode.childNodes)) {
|
|
174
|
+
if (child.nodeType === DOCUMENT_TYPE_NODE) continue;
|
|
175
|
+
targetDoc.appendChild(targetDoc.importNode(child, true));
|
|
176
|
+
}
|
|
177
|
+
} else {
|
|
178
|
+
root = targetDoc.importNode(sourceNode, true);
|
|
179
|
+
targetDoc.appendChild(root);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
pruneWhitespace(root, filter);
|
|
183
|
+
return root;
|
|
184
|
+
}
|