@openrewrite/rewrite 8.107.1 → 8.107.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/dist/javascript/add-import.d.ts +63 -3
- package/dist/javascript/add-import.d.ts.map +1 -1
- package/dist/javascript/add-import.js +238 -40
- package/dist/javascript/add-import.js.map +1 -1
- package/dist/javascript/autodetect.d.ts.map +1 -1
- package/dist/javascript/autodetect.js +43 -16
- package/dist/javascript/autodetect.js.map +1 -1
- package/dist/javascript/binding.d.ts.map +1 -1
- package/dist/javascript/binding.js +10 -2
- package/dist/javascript/binding.js.map +1 -1
- package/dist/rewrite-javascript-version.txt +1 -1
- package/dist/xml/index.d.ts +1 -0
- package/dist/xml/index.d.ts.map +1 -1
- package/dist/xml/index.js +1 -0
- package/dist/xml/index.js.map +1 -1
- package/dist/xml/xpath-compiler.d.ts +64 -0
- package/dist/xml/xpath-compiler.d.ts.map +1 -0
- package/dist/xml/xpath-compiler.js +496 -0
- package/dist/xml/xpath-compiler.js.map +1 -0
- package/dist/xml/xpath.d.ts +8 -0
- package/dist/xml/xpath.d.ts.map +1 -0
- package/dist/xml/xpath.js +736 -0
- package/dist/xml/xpath.js.map +1 -0
- package/package.json +1 -1
- package/src/javascript/add-import.ts +261 -34
- package/src/javascript/autodetect.ts +48 -18
- package/src/javascript/binding.ts +13 -4
- package/src/xml/index.ts +1 -0
- package/src/xml/xpath-compiler.ts +652 -0
- package/src/xml/xpath.ts +776 -0
|
@@ -0,0 +1,736 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.XPathMatcher = void 0;
|
|
4
|
+
/*
|
|
5
|
+
* Copyright 2026 the original author or authors.
|
|
6
|
+
* <p>
|
|
7
|
+
* Licensed under the Moderne Source Available License (the "License");
|
|
8
|
+
* you may not use this file except in compliance with the License.
|
|
9
|
+
* You may obtain a copy of the License at
|
|
10
|
+
* <p>
|
|
11
|
+
* https://docs.moderne.io/licensing/moderne-source-available-license
|
|
12
|
+
* <p>
|
|
13
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
14
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
+
* See the License for the specific language governing permissions and
|
|
17
|
+
* limitations under the License.
|
|
18
|
+
*/
|
|
19
|
+
const tree_1 = require("../tree");
|
|
20
|
+
const tree_2 = require("./tree");
|
|
21
|
+
const namespaces_1 = require("./namespaces");
|
|
22
|
+
const xpath_compiler_1 = require("./xpath-compiler");
|
|
23
|
+
class XPathMatcher {
|
|
24
|
+
constructor(expression) {
|
|
25
|
+
this.expression = expression;
|
|
26
|
+
}
|
|
27
|
+
matches(cursor) {
|
|
28
|
+
var _a;
|
|
29
|
+
const compiled = (_a = this.compiled) !== null && _a !== void 0 ? _a : (this.compiled = (0, xpath_compiler_1.compileXPath)(this.expression));
|
|
30
|
+
switch (compiled.type) {
|
|
31
|
+
case "path":
|
|
32
|
+
return compiled.steps.length > 0 && new PathMatch(compiled.steps, compiled.absolute).matches(cursor);
|
|
33
|
+
case "boolean":
|
|
34
|
+
return matchBooleanExpr(compiled.expr, cursor);
|
|
35
|
+
case "filter":
|
|
36
|
+
return matchFilterExpr(compiled.filter, cursor);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.XPathMatcher = XPathMatcher;
|
|
41
|
+
function isTagCursor(cursor) {
|
|
42
|
+
return cursor !== undefined && (0, tree_2.isXml)(cursor.value) && (0, tree_2.isTag)(cursor.value);
|
|
43
|
+
}
|
|
44
|
+
function parentTagCursor(cursor) {
|
|
45
|
+
let parent = cursor === null || cursor === void 0 ? void 0 : cursor.parent;
|
|
46
|
+
while (parent && !isTagCursor(parent)) {
|
|
47
|
+
if ((0, tree_2.isXml)(parent.value) && (0, tree_2.isDocument)(parent.value)) {
|
|
48
|
+
return undefined;
|
|
49
|
+
}
|
|
50
|
+
parent = parent.parent;
|
|
51
|
+
}
|
|
52
|
+
return parent;
|
|
53
|
+
}
|
|
54
|
+
function matchesElementName(step, name) {
|
|
55
|
+
return step.nodeType === "node" || step.nodeType === "unknown" && matchesName(step.name, name);
|
|
56
|
+
}
|
|
57
|
+
function matchesName(pattern, name) {
|
|
58
|
+
return pattern === undefined || pattern === "*" || name === pattern;
|
|
59
|
+
}
|
|
60
|
+
class PathMatch {
|
|
61
|
+
constructor(steps, absolute) {
|
|
62
|
+
this.steps = steps;
|
|
63
|
+
this.absolute = absolute;
|
|
64
|
+
}
|
|
65
|
+
matches(cursor) {
|
|
66
|
+
const steps = this.steps;
|
|
67
|
+
const value = (0, tree_2.isXml)(cursor.value) ? cursor.value : undefined;
|
|
68
|
+
const last = steps[steps.length - 1];
|
|
69
|
+
if (last.type === "attribute") {
|
|
70
|
+
if (!value || !(0, tree_2.isAttribute)(value) || !matchesName(last.name, value.key.name)) {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
if (!last.predicates.every(p => evaluateExpr(p, undefined, value, cursor, 1, 1))) {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
const parent = cursor.parent;
|
|
77
|
+
return isTagCursor(parent) && this.matchRemaining(parent, steps.length - 2);
|
|
78
|
+
}
|
|
79
|
+
if (last.type === "nodeType") {
|
|
80
|
+
return this.matchNodeTypeTest(last, cursor, steps.length - 2);
|
|
81
|
+
}
|
|
82
|
+
if (last.type === "dotdot" || (last.type === "axis" && last.axis === "parent")) {
|
|
83
|
+
return this.matchParentStepAsLast(last, cursor, steps.length - 2);
|
|
84
|
+
}
|
|
85
|
+
if (last.type === "dot" || (last.type === "axis" && last.axis === "self")) {
|
|
86
|
+
if (!value || !(0, tree_2.isTag)(value) || (last.type === "axis" && !matchesElementName(last, value.name))) {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
return this.matchRemaining(cursor, steps.length - 2);
|
|
90
|
+
}
|
|
91
|
+
if (!value || !(0, tree_2.isTag)(value) || !matchStepAgainstTag(last, value, cursor)) {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
return this.matchRemaining(parentTagCursor(cursor), steps.length - 2);
|
|
95
|
+
}
|
|
96
|
+
matchParentStepAsLast(step, cursor, previous) {
|
|
97
|
+
if (!isTagCursor(cursor)) {
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
const tag = cursor.value;
|
|
101
|
+
if (step.type === "axis" && !matchesElementName(step, tag.name)) {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
if (previous < 0) {
|
|
105
|
+
return true;
|
|
106
|
+
}
|
|
107
|
+
const previousStep = this.steps[previous];
|
|
108
|
+
if (previousStep.type === "nodeTest" && previousStep.name !== undefined &&
|
|
109
|
+
!findChildTag(tag, previousStep.name)) {
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
return this.matchRemaining(cursor, previous - 1);
|
|
113
|
+
}
|
|
114
|
+
matchParentStepInMiddle(cursor, previous) {
|
|
115
|
+
if (!isTagCursor(cursor)) {
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
if (previous < 0) {
|
|
119
|
+
if (this.absolute && !this.steps[0].isDescendant) {
|
|
120
|
+
return !isTagCursor(parentTagCursor(cursor));
|
|
121
|
+
}
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
const previousStep = this.steps[previous];
|
|
125
|
+
if (previousStep.type === "nodeTest" && previousStep.name !== undefined &&
|
|
126
|
+
!findChildTag(cursor.value, previousStep.name)) {
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
return this.matchRemaining(cursor, previous - 1);
|
|
130
|
+
}
|
|
131
|
+
matchNodeTypeTest(step, cursor, next) {
|
|
132
|
+
const value = cursor.value;
|
|
133
|
+
switch (step.nodeType) {
|
|
134
|
+
case "text":
|
|
135
|
+
if ((0, tree_2.isXml)(value) && value.kind === tree_2.Xml.Kind.CharData) {
|
|
136
|
+
return this.matchRemaining(parentTagCursor(cursor), next);
|
|
137
|
+
}
|
|
138
|
+
if ((0, tree_2.isXml)(value) && (0, tree_2.isTag)(value) && (0, tree_2.getValue)(value) !== undefined) {
|
|
139
|
+
return this.matchRemaining(cursor, next);
|
|
140
|
+
}
|
|
141
|
+
return false;
|
|
142
|
+
case "comment":
|
|
143
|
+
return (0, tree_2.isXml)(value) && value.kind === tree_2.Xml.Kind.Comment &&
|
|
144
|
+
this.matchRemaining(parentTagCursor(cursor), next);
|
|
145
|
+
case "node":
|
|
146
|
+
return this.matchRemaining(parentTagCursor(cursor), next);
|
|
147
|
+
case "processing-instruction":
|
|
148
|
+
return (0, tree_2.isXml)(value) && value.kind === tree_2.Xml.Kind.ProcessingInstruction &&
|
|
149
|
+
this.matchRemaining(parentTagCursor(cursor), next);
|
|
150
|
+
default:
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
matchRemaining(cursor, index) {
|
|
155
|
+
if (index < 0) {
|
|
156
|
+
return !this.absolute || !isTagCursor(cursor);
|
|
157
|
+
}
|
|
158
|
+
const step = this.steps[index];
|
|
159
|
+
switch (step.type) {
|
|
160
|
+
case "dot":
|
|
161
|
+
return this.matchRemaining(cursor, index - 1);
|
|
162
|
+
case "dotdot":
|
|
163
|
+
return this.matchParentStepInMiddle(cursor, index - 1);
|
|
164
|
+
case "axis":
|
|
165
|
+
return this.matchAxisStep(step, cursor, index);
|
|
166
|
+
case "nodeType":
|
|
167
|
+
return step.nodeType === "node" && this.matchRemaining(cursor, index - 1);
|
|
168
|
+
}
|
|
169
|
+
if (this.steps[index + 1].isDescendant) {
|
|
170
|
+
for (let pos = cursor; isTagCursor(pos); pos = parentTagCursor(pos)) {
|
|
171
|
+
if (matchStepAgainstTag(step, pos.value, pos) && this.matchRemaining(parentTagCursor(pos), index - 1)) {
|
|
172
|
+
return true;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
return isTagCursor(cursor) && matchStepAgainstTag(step, cursor.value, cursor) &&
|
|
178
|
+
this.matchRemaining(parentTagCursor(cursor), index - 1);
|
|
179
|
+
}
|
|
180
|
+
matchAxisStep(step, cursor, index) {
|
|
181
|
+
switch (step.axis) {
|
|
182
|
+
case "parent":
|
|
183
|
+
if (isTagCursor(cursor) && !matchesElementName(step, cursor.value.name)) {
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
return this.matchParentStepInMiddle(cursor, index - 1);
|
|
187
|
+
case "self":
|
|
188
|
+
if (isTagCursor(cursor) && !matchesElementName(step, cursor.value.name)) {
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
return this.matchRemaining(cursor, index - 1);
|
|
192
|
+
case "child":
|
|
193
|
+
return isTagCursor(cursor) && matchStepAgainstTag(step, cursor.value, cursor) &&
|
|
194
|
+
this.matchRemaining(parentTagCursor(cursor), index - 1);
|
|
195
|
+
default:
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
function matchStepAgainstTag(step, tag, cursor) {
|
|
201
|
+
switch (step.type) {
|
|
202
|
+
case "nodeTest":
|
|
203
|
+
return matchesName(step.name, tag.name) &&
|
|
204
|
+
(step.predicates.length === 0 || evaluatePredicates(step, tag, cursor));
|
|
205
|
+
case "dot":
|
|
206
|
+
return true;
|
|
207
|
+
case "nodeType":
|
|
208
|
+
return step.nodeType === "node";
|
|
209
|
+
default:
|
|
210
|
+
return false;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
function needsPosition(expr) {
|
|
214
|
+
switch (expr.type) {
|
|
215
|
+
case "numeric":
|
|
216
|
+
return true;
|
|
217
|
+
case "function":
|
|
218
|
+
return expr.functionType === "position" || expr.functionType === "last" || expr.args.some(needsPosition);
|
|
219
|
+
case "comparison":
|
|
220
|
+
case "and":
|
|
221
|
+
case "or":
|
|
222
|
+
return needsPosition(expr.left) || needsPosition(expr.right);
|
|
223
|
+
default:
|
|
224
|
+
return false;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
function evaluatePredicates(step, tag, cursor) {
|
|
228
|
+
var _a;
|
|
229
|
+
if (!step.predicates.some(needsPosition)) {
|
|
230
|
+
return step.predicates.every(p => evaluateExpr(p, tag, undefined, cursor, 1, 1));
|
|
231
|
+
}
|
|
232
|
+
const parent = cursor.parent;
|
|
233
|
+
const candidates = isTagCursor(parent) ? ((_a = parent.value.content) !== null && _a !== void 0 ? _a : [])
|
|
234
|
+
.filter((c) => (0, tree_2.isTag)(c) && matchesName(step.name, c.name))
|
|
235
|
+
.map((c) => new tree_1.Cursor(c, parent)) : [cursor];
|
|
236
|
+
return filterCandidates(candidates, step.predicates).some(c => c.value === tag);
|
|
237
|
+
}
|
|
238
|
+
function evaluateExpr(expr, tag, attr, cursor, position, size) {
|
|
239
|
+
switch (expr.type) {
|
|
240
|
+
case "numeric":
|
|
241
|
+
return position === expr.value;
|
|
242
|
+
case "and":
|
|
243
|
+
return evaluateExpr(expr.left, tag, attr, cursor, position, size) &&
|
|
244
|
+
evaluateExpr(expr.right, tag, attr, cursor, position, size);
|
|
245
|
+
case "or":
|
|
246
|
+
return evaluateExpr(expr.left, tag, attr, cursor, position, size) ||
|
|
247
|
+
evaluateExpr(expr.right, tag, attr, cursor, position, size);
|
|
248
|
+
case "comparison": {
|
|
249
|
+
const left = resolveValue(expr.left, tag, attr, cursor, position, size);
|
|
250
|
+
const right = resolveValue(expr.right, tag, attr, cursor, position, size);
|
|
251
|
+
return left !== undefined && right !== undefined && compareValues(left, right, expr.op);
|
|
252
|
+
}
|
|
253
|
+
case "function":
|
|
254
|
+
return evaluateFunction(expr, tag, attr, cursor, position, size);
|
|
255
|
+
case "child":
|
|
256
|
+
return tag !== undefined && findChildTag(tag, expr.name) !== undefined;
|
|
257
|
+
case "path":
|
|
258
|
+
return tag !== undefined && (expr.pathSteps ? selectSteps([cursor], expr.pathSteps).length > 0 : pathExists(tag, expr.steps));
|
|
259
|
+
case "attribute":
|
|
260
|
+
return tag !== undefined && tag.attributes.some(a => matchesName(expr.name, a.key.name));
|
|
261
|
+
default:
|
|
262
|
+
return false;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
function resolveValue(expr, tag, attr, cursor, position, size) {
|
|
266
|
+
var _a, _b;
|
|
267
|
+
switch (expr.type) {
|
|
268
|
+
case "string":
|
|
269
|
+
case "numeric":
|
|
270
|
+
return expr.value;
|
|
271
|
+
case "child": {
|
|
272
|
+
const child = tag && findChildTag(tag, expr.name);
|
|
273
|
+
return child && ((_a = (0, tree_2.getValue)(child)) !== null && _a !== void 0 ? _a : "");
|
|
274
|
+
}
|
|
275
|
+
case "attribute":
|
|
276
|
+
if (tag) {
|
|
277
|
+
return attributeValue(tag, expr.name);
|
|
278
|
+
}
|
|
279
|
+
if (attr) {
|
|
280
|
+
if (expr.name === "*") {
|
|
281
|
+
return attr.value.value;
|
|
282
|
+
}
|
|
283
|
+
const parent = cursor.parent;
|
|
284
|
+
if (isTagCursor(parent)) {
|
|
285
|
+
return attributeValue(parent.value, expr.name);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
return undefined;
|
|
289
|
+
case "self":
|
|
290
|
+
return tag ? stringValue(tag) : attr === null || attr === void 0 ? void 0 : attr.value.value;
|
|
291
|
+
case "path": {
|
|
292
|
+
if (expr.pathSteps) {
|
|
293
|
+
const first = selectSteps([cursor], expr.pathSteps)[0];
|
|
294
|
+
return first && stringValue(first.value);
|
|
295
|
+
}
|
|
296
|
+
return tag && resolvePathValue(expr.steps, tag);
|
|
297
|
+
}
|
|
298
|
+
case "absolutePath": {
|
|
299
|
+
const root = rootTag(cursor);
|
|
300
|
+
const first = root && [...findTagsByPath(root, expr.path).values()][0];
|
|
301
|
+
return first ? (_b = (0, tree_2.getValue)(first)) !== null && _b !== void 0 ? _b : "" : "";
|
|
302
|
+
}
|
|
303
|
+
case "function":
|
|
304
|
+
return resolveFunctionValue(expr, tag, attr, cursor, position, size);
|
|
305
|
+
default:
|
|
306
|
+
return undefined;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
function evaluateFunction(expr, tag, attr, cursor, position, size) {
|
|
310
|
+
switch (expr.functionType) {
|
|
311
|
+
case "position":
|
|
312
|
+
return position > 0;
|
|
313
|
+
case "last":
|
|
314
|
+
return position === size;
|
|
315
|
+
case "not":
|
|
316
|
+
return expr.args.length > 0 && !evaluateExpr(expr.args[0], tag, attr, cursor, position, size);
|
|
317
|
+
case "contains":
|
|
318
|
+
case "starts-with":
|
|
319
|
+
case "ends-with":
|
|
320
|
+
return resolveFunctionValue(expr, tag, attr, cursor, position, size) === true;
|
|
321
|
+
case "string-length":
|
|
322
|
+
case "count": {
|
|
323
|
+
const result = resolveFunctionValue(expr, tag, attr, cursor, position, size);
|
|
324
|
+
return typeof result === "number" && result > 0;
|
|
325
|
+
}
|
|
326
|
+
case "text": {
|
|
327
|
+
const value = tag && (0, tree_2.getValue)(tag);
|
|
328
|
+
return value !== undefined && javaTrim(value) !== "";
|
|
329
|
+
}
|
|
330
|
+
case "local-name":
|
|
331
|
+
case "namespace-uri":
|
|
332
|
+
return true;
|
|
333
|
+
default:
|
|
334
|
+
return false;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
function resolveFunctionValue(expr, tag, attr, cursor, position, size) {
|
|
338
|
+
var _a, _b, _c, _d;
|
|
339
|
+
const args = expr.args;
|
|
340
|
+
const stringArg = (i) => {
|
|
341
|
+
const value = resolveValue(args[i], tag, attr, cursor, position, size);
|
|
342
|
+
return value === undefined ? undefined : functionStringValue(value);
|
|
343
|
+
};
|
|
344
|
+
switch (expr.functionType) {
|
|
345
|
+
case "position":
|
|
346
|
+
return position;
|
|
347
|
+
case "last":
|
|
348
|
+
return size;
|
|
349
|
+
case "local-name": {
|
|
350
|
+
const target = args.length > 0 && args[0].type === "parent" ? (_a = parentTagCursor(cursor)) === null || _a === void 0 ? void 0 : _a.value : tag;
|
|
351
|
+
if (target) {
|
|
352
|
+
return (0, namespaces_1.localName)(target.name);
|
|
353
|
+
}
|
|
354
|
+
return attr && args.length === 0 ? (0, namespaces_1.localName)(attr.key.name) : undefined;
|
|
355
|
+
}
|
|
356
|
+
case "namespace-uri": {
|
|
357
|
+
let target = tag;
|
|
358
|
+
let targetCursor = cursor;
|
|
359
|
+
if (args.length > 0 && args[0].type === "parent") {
|
|
360
|
+
const parent = parentTagCursor(cursor);
|
|
361
|
+
if (!isTagCursor(parent)) {
|
|
362
|
+
return undefined;
|
|
363
|
+
}
|
|
364
|
+
target = parent.value;
|
|
365
|
+
targetCursor = parent;
|
|
366
|
+
}
|
|
367
|
+
if (target) {
|
|
368
|
+
return findNamespaceUri(namespacePrefixOf(target.name), targetCursor);
|
|
369
|
+
}
|
|
370
|
+
if (attr && args.length === 0) {
|
|
371
|
+
const name = attr.key.name;
|
|
372
|
+
return name.includes(":") ? findNamespaceUri(namespacePrefixOf(name), cursor) : "";
|
|
373
|
+
}
|
|
374
|
+
return undefined;
|
|
375
|
+
}
|
|
376
|
+
case "text":
|
|
377
|
+
return tag && ((_b = (0, tree_2.getValue)(tag)) !== null && _b !== void 0 ? _b : "");
|
|
378
|
+
case "string-length":
|
|
379
|
+
return args.length > 0 ? (_d = (_c = stringArg(0)) === null || _c === void 0 ? void 0 : _c.length) !== null && _d !== void 0 ? _d : 0 : 0;
|
|
380
|
+
case "substring-before":
|
|
381
|
+
case "substring-after": {
|
|
382
|
+
if (args.length < 2) {
|
|
383
|
+
return "";
|
|
384
|
+
}
|
|
385
|
+
const str = stringArg(0);
|
|
386
|
+
const delimiter = stringArg(1);
|
|
387
|
+
if (str === undefined || delimiter === undefined) {
|
|
388
|
+
return "";
|
|
389
|
+
}
|
|
390
|
+
const index = str.indexOf(delimiter);
|
|
391
|
+
if (index < 0) {
|
|
392
|
+
return "";
|
|
393
|
+
}
|
|
394
|
+
return expr.functionType === "substring-before" ? str.substring(0, index) : str.substring(index + delimiter.length);
|
|
395
|
+
}
|
|
396
|
+
case "count": {
|
|
397
|
+
const path = args[0];
|
|
398
|
+
if ((path === null || path === void 0 ? void 0 : path.type) === "absolutePath") {
|
|
399
|
+
const root = rootTag(cursor);
|
|
400
|
+
if (root) {
|
|
401
|
+
return findTagsByPath(root, path.path).size;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
return 0;
|
|
405
|
+
}
|
|
406
|
+
case "contains":
|
|
407
|
+
case "starts-with":
|
|
408
|
+
case "ends-with": {
|
|
409
|
+
if (args.length < 2) {
|
|
410
|
+
return false;
|
|
411
|
+
}
|
|
412
|
+
const str = stringArg(0);
|
|
413
|
+
const other = stringArg(1);
|
|
414
|
+
if (str === undefined || other === undefined) {
|
|
415
|
+
return false;
|
|
416
|
+
}
|
|
417
|
+
return expr.functionType === "contains" ? str.includes(other) :
|
|
418
|
+
expr.functionType === "starts-with" ? str.startsWith(other) : str.endsWith(other);
|
|
419
|
+
}
|
|
420
|
+
default:
|
|
421
|
+
return undefined;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
function namespacePrefixOf(name) {
|
|
425
|
+
const colon = name.indexOf(":");
|
|
426
|
+
return colon >= 0 ? name.substring(0, colon) : "";
|
|
427
|
+
}
|
|
428
|
+
function findNamespaceUri(prefix, cursor) {
|
|
429
|
+
const declaration = prefix === "" ? "xmlns" : `xmlns:${prefix}`;
|
|
430
|
+
for (let c = cursor; c; c = c.parent) {
|
|
431
|
+
if (isTagCursor(c)) {
|
|
432
|
+
const attribute = c.value.attributes.find((a) => a.key.name === declaration);
|
|
433
|
+
if (attribute) {
|
|
434
|
+
return attribute.value.value;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
return prefix === "" ? "" : undefined;
|
|
439
|
+
}
|
|
440
|
+
function attributeValue(tag, name) {
|
|
441
|
+
var _a;
|
|
442
|
+
return (_a = tag.attributes.find(a => matchesName(name, a.key.name))) === null || _a === void 0 ? void 0 : _a.value.value;
|
|
443
|
+
}
|
|
444
|
+
function findChildTag(parent, name) {
|
|
445
|
+
var _a;
|
|
446
|
+
return (_a = parent.content) === null || _a === void 0 ? void 0 : _a.find((c) => (0, tree_2.isTag)(c) && matchesName(name, c.name));
|
|
447
|
+
}
|
|
448
|
+
function pathExists(tag, steps) {
|
|
449
|
+
let current = tag;
|
|
450
|
+
for (const step of steps) {
|
|
451
|
+
current = step.type === "child" ? findChildTag(current, step.name) : undefined;
|
|
452
|
+
if (!current) {
|
|
453
|
+
return false;
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
return true;
|
|
457
|
+
}
|
|
458
|
+
function resolvePathValue(steps, tag) {
|
|
459
|
+
var _a;
|
|
460
|
+
if (steps.length === 0) {
|
|
461
|
+
return undefined;
|
|
462
|
+
}
|
|
463
|
+
let current = tag;
|
|
464
|
+
for (const step of steps) {
|
|
465
|
+
current = step.type === "child" ? findChildTag(current, step.name) : undefined;
|
|
466
|
+
if (!current) {
|
|
467
|
+
return undefined;
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
return (_a = (0, tree_2.getValue)(current)) !== null && _a !== void 0 ? _a : "";
|
|
471
|
+
}
|
|
472
|
+
function compareValues(left, right, op) {
|
|
473
|
+
if (typeof left === "number" || typeof right === "number" || op !== "=" && op !== "!=") {
|
|
474
|
+
return compareOrdered(numberValue(left), numberValue(right), op);
|
|
475
|
+
}
|
|
476
|
+
return op === "=" ? String(left) === String(right) : String(left) !== String(right);
|
|
477
|
+
}
|
|
478
|
+
function numberValue(value) {
|
|
479
|
+
if (typeof value === "number") {
|
|
480
|
+
return value;
|
|
481
|
+
}
|
|
482
|
+
const text = javaTrim(String(value));
|
|
483
|
+
return /^-?(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)$/.test(text) ? Number(text) : NaN;
|
|
484
|
+
}
|
|
485
|
+
function functionStringValue(value) {
|
|
486
|
+
const text = String(value);
|
|
487
|
+
if (typeof value !== "number" || !Number.isFinite(value) || !text.includes("e")) {
|
|
488
|
+
return text;
|
|
489
|
+
}
|
|
490
|
+
const [coefficient, exponent] = text.split("e");
|
|
491
|
+
const sign = coefficient.startsWith("-") ? "-" : "";
|
|
492
|
+
const unsigned = coefficient.substring(sign.length);
|
|
493
|
+
const digits = unsigned.replace(".", "");
|
|
494
|
+
const point = (unsigned.includes(".") ? unsigned.indexOf(".") : unsigned.length) + Number(exponent);
|
|
495
|
+
if (point <= 0) {
|
|
496
|
+
return `${sign}0.${"0".repeat(-point)}${digits}`;
|
|
497
|
+
}
|
|
498
|
+
if (point >= digits.length) {
|
|
499
|
+
return sign + digits + "0".repeat(point - digits.length);
|
|
500
|
+
}
|
|
501
|
+
return `${sign}${digits.substring(0, point)}.${digits.substring(point)}`;
|
|
502
|
+
}
|
|
503
|
+
function compareOrdered(left, right, op) {
|
|
504
|
+
switch (op) {
|
|
505
|
+
case "=":
|
|
506
|
+
return left === right;
|
|
507
|
+
case "!=":
|
|
508
|
+
return left !== right;
|
|
509
|
+
case "<":
|
|
510
|
+
return left < right;
|
|
511
|
+
case "<=":
|
|
512
|
+
return left <= right;
|
|
513
|
+
case ">":
|
|
514
|
+
return left > right;
|
|
515
|
+
case ">=":
|
|
516
|
+
return left >= right;
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
function javaTrim(s) {
|
|
520
|
+
let start = 0;
|
|
521
|
+
let end = s.length;
|
|
522
|
+
while (start < end && s.charCodeAt(start) <= 0x20) {
|
|
523
|
+
start++;
|
|
524
|
+
}
|
|
525
|
+
while (end > start && s.charCodeAt(end - 1) <= 0x20) {
|
|
526
|
+
end--;
|
|
527
|
+
}
|
|
528
|
+
return s.substring(start, end);
|
|
529
|
+
}
|
|
530
|
+
function matchBooleanExpr(expr, cursor) {
|
|
531
|
+
if (!isTagCursor(cursor)) {
|
|
532
|
+
return false;
|
|
533
|
+
}
|
|
534
|
+
if (hasRelativePath(expr) || !hasPureAbsolutePath(expr)) {
|
|
535
|
+
return evaluateExpr(expr, cursor.value, undefined, cursor, 1, 1);
|
|
536
|
+
}
|
|
537
|
+
let parent = cursor.parent;
|
|
538
|
+
while (parent && !isTagCursor(parent) && !((0, tree_2.isXml)(parent.value) && (0, tree_2.isDocument)(parent.value))) {
|
|
539
|
+
parent = parent.parent;
|
|
540
|
+
}
|
|
541
|
+
if (!parent || !(0, tree_2.isXml)(parent.value) || !(0, tree_2.isDocument)(parent.value)) {
|
|
542
|
+
return false;
|
|
543
|
+
}
|
|
544
|
+
return evaluateExpr(expr, cursor.value, undefined, cursor, 1, 1);
|
|
545
|
+
}
|
|
546
|
+
function hasRelativePath(expr) {
|
|
547
|
+
switch (expr.type) {
|
|
548
|
+
case "child":
|
|
549
|
+
case "path":
|
|
550
|
+
return true;
|
|
551
|
+
case "comparison":
|
|
552
|
+
case "and":
|
|
553
|
+
case "or":
|
|
554
|
+
return hasRelativePath(expr.left) || hasRelativePath(expr.right);
|
|
555
|
+
case "function":
|
|
556
|
+
return expr.args.some(hasRelativePath);
|
|
557
|
+
default:
|
|
558
|
+
return false;
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
function hasPureAbsolutePath(expr) {
|
|
562
|
+
switch (expr.type) {
|
|
563
|
+
case "absolutePath":
|
|
564
|
+
return expr.path.startsWith("/") && !expr.path.startsWith("//");
|
|
565
|
+
case "comparison":
|
|
566
|
+
case "and":
|
|
567
|
+
case "or": {
|
|
568
|
+
const leftPure = !hasAnyAbsolutePath(expr.left) || hasPureAbsolutePath(expr.left);
|
|
569
|
+
const rightPure = !hasAnyAbsolutePath(expr.right) || hasPureAbsolutePath(expr.right);
|
|
570
|
+
return (hasAnyAbsolutePath(expr.left) || hasAnyAbsolutePath(expr.right)) && leftPure && rightPure;
|
|
571
|
+
}
|
|
572
|
+
case "function": {
|
|
573
|
+
const absolute = expr.args.filter(hasAnyAbsolutePath);
|
|
574
|
+
return absolute.length > 0 && absolute.every(hasPureAbsolutePath);
|
|
575
|
+
}
|
|
576
|
+
default:
|
|
577
|
+
return false;
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
function hasAnyAbsolutePath(expr) {
|
|
581
|
+
switch (expr.type) {
|
|
582
|
+
case "absolutePath":
|
|
583
|
+
return true;
|
|
584
|
+
case "comparison":
|
|
585
|
+
case "and":
|
|
586
|
+
case "or":
|
|
587
|
+
return hasAnyAbsolutePath(expr.left) || hasAnyAbsolutePath(expr.right);
|
|
588
|
+
case "function":
|
|
589
|
+
return expr.args.some(hasAnyAbsolutePath);
|
|
590
|
+
default:
|
|
591
|
+
return false;
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
function matchFilterExpr(filter, cursor) {
|
|
595
|
+
const root = rootTag(cursor);
|
|
596
|
+
if (!root) {
|
|
597
|
+
return false;
|
|
598
|
+
}
|
|
599
|
+
let nodes = filterCandidates(findNodesByPath(root, filter.path), filter.predicates);
|
|
600
|
+
if (filter.trailingPath) {
|
|
601
|
+
if (filter.trailingIsDescendant) {
|
|
602
|
+
nodes = nodes.flatMap(c => collectDescendants(c));
|
|
603
|
+
}
|
|
604
|
+
nodes = selectSteps(nodes, filter.trailingPath);
|
|
605
|
+
}
|
|
606
|
+
return nodes.some(c => c.value === cursor.value);
|
|
607
|
+
}
|
|
608
|
+
function rootTag(cursor) {
|
|
609
|
+
let c = cursor;
|
|
610
|
+
while (c.parent && !((0, tree_2.isXml)(c.parent.value) && (0, tree_2.isDocument)(c.parent.value))) {
|
|
611
|
+
c = c.parent;
|
|
612
|
+
}
|
|
613
|
+
return isTagCursor(c) ? c.value : undefined;
|
|
614
|
+
}
|
|
615
|
+
function findTagsByPath(root, path) {
|
|
616
|
+
return new Map(findNodesByPath(root, path).filter(isTagCursor).map(c => [c.value.id, c.value]));
|
|
617
|
+
}
|
|
618
|
+
function findNodesByPath(root, path) {
|
|
619
|
+
if ((0, xpath_compiler_1.compileXPath)(path).type !== "path") {
|
|
620
|
+
throw new Error(`Unsupported XPath expression: parenthesized expression must be a location path: ${path}`);
|
|
621
|
+
}
|
|
622
|
+
const matcher = new XPathMatcher(path);
|
|
623
|
+
const matches = [];
|
|
624
|
+
for (const candidate of collectDescendants(new tree_1.Cursor(root))) {
|
|
625
|
+
if (matcher.matches(candidate)) {
|
|
626
|
+
matches.push(candidate);
|
|
627
|
+
}
|
|
628
|
+
if (isTagCursor(candidate)) {
|
|
629
|
+
for (const attr of candidate.value.attributes) {
|
|
630
|
+
const attrCursor = new tree_1.Cursor(attr, candidate);
|
|
631
|
+
if (matcher.matches(attrCursor)) {
|
|
632
|
+
matches.push(attrCursor);
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
return matches;
|
|
638
|
+
}
|
|
639
|
+
function collectDescendants(cursor, result = []) {
|
|
640
|
+
var _a;
|
|
641
|
+
result.push(cursor);
|
|
642
|
+
if (isTagCursor(cursor)) {
|
|
643
|
+
for (const child of (_a = cursor.value.content) !== null && _a !== void 0 ? _a : []) {
|
|
644
|
+
if ((0, tree_2.isTag)(child)) {
|
|
645
|
+
collectDescendants(new tree_1.Cursor(child, cursor), result);
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
return result;
|
|
650
|
+
}
|
|
651
|
+
function selectSteps(contexts, steps) {
|
|
652
|
+
var _a, _b;
|
|
653
|
+
for (const step of steps) {
|
|
654
|
+
const next = [];
|
|
655
|
+
for (const context of contexts) {
|
|
656
|
+
const origins = step.isDescendant ? collectDescendants(context) : [context];
|
|
657
|
+
for (const origin of origins) {
|
|
658
|
+
const candidates = [];
|
|
659
|
+
const value = origin.value;
|
|
660
|
+
if (step.type === "dot" || step.type === "axis" && step.axis === "self") {
|
|
661
|
+
if (step.type === "dot" || matchesNodeTest(step, value)) {
|
|
662
|
+
candidates.push(origin);
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
else if (step.type === "dotdot" || step.type === "axis" && step.axis === "parent") {
|
|
666
|
+
const parent = parentTagCursor(origin);
|
|
667
|
+
if (isTagCursor(parent) && (step.type === "dotdot" || matchesElementName(step, parent.value.name))) {
|
|
668
|
+
candidates.push(parent);
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
else if (isTagCursor(origin)) {
|
|
672
|
+
if (step.type === "attribute") {
|
|
673
|
+
for (const attr of origin.value.attributes) {
|
|
674
|
+
if (matchesName(step.name, attr.key.name)) {
|
|
675
|
+
candidates.push(new tree_1.Cursor(attr, origin));
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
else if (step.type === "nodeType") {
|
|
680
|
+
for (const content of (_a = origin.value.content) !== null && _a !== void 0 ? _a : []) {
|
|
681
|
+
if (matchesNodeTest(step, content)) {
|
|
682
|
+
candidates.push(new tree_1.Cursor(content, origin));
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
else {
|
|
687
|
+
for (const child of (_b = origin.value.content) !== null && _b !== void 0 ? _b : []) {
|
|
688
|
+
if ((0, tree_2.isTag)(child) && (step.type === "axis" ? matchesElementName(step, child.name) :
|
|
689
|
+
matchesName(step.name, child.name))) {
|
|
690
|
+
candidates.push(new tree_1.Cursor(child, origin));
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
next.push(...filterCandidates(candidates, step.predicates));
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
contexts = next;
|
|
699
|
+
}
|
|
700
|
+
return contexts;
|
|
701
|
+
}
|
|
702
|
+
function filterCandidates(candidates, predicates) {
|
|
703
|
+
for (const predicate of predicates) {
|
|
704
|
+
candidates = candidates.filter((candidate, i) => {
|
|
705
|
+
const value = candidate.value;
|
|
706
|
+
return evaluateExpr(predicate, isTagCursor(candidate) ? value : undefined, (0, tree_2.isXml)(value) && (0, tree_2.isAttribute)(value) ? value : undefined, candidate, i + 1, candidates.length);
|
|
707
|
+
});
|
|
708
|
+
}
|
|
709
|
+
return candidates;
|
|
710
|
+
}
|
|
711
|
+
function stringValue(node) {
|
|
712
|
+
var _a;
|
|
713
|
+
if ((0, tree_2.isAttribute)(node)) {
|
|
714
|
+
return node.value.value;
|
|
715
|
+
}
|
|
716
|
+
if ((0, tree_2.isCharData)(node)) {
|
|
717
|
+
return node.text;
|
|
718
|
+
}
|
|
719
|
+
return (0, tree_2.isTag)(node) ? ((_a = node.content) !== null && _a !== void 0 ? _a : []).filter(c => (0, tree_2.isTag)(c) || c.kind === tree_2.Xml.Kind.CharData)
|
|
720
|
+
.map(stringValue).join("") : "";
|
|
721
|
+
}
|
|
722
|
+
function matchesNodeTest(step, value) {
|
|
723
|
+
switch (step.nodeType) {
|
|
724
|
+
case "node":
|
|
725
|
+
return true;
|
|
726
|
+
case "text":
|
|
727
|
+
return value.kind === tree_2.Xml.Kind.CharData;
|
|
728
|
+
case "comment":
|
|
729
|
+
return value.kind === tree_2.Xml.Kind.Comment;
|
|
730
|
+
case "processing-instruction":
|
|
731
|
+
return value.kind === tree_2.Xml.Kind.ProcessingInstruction;
|
|
732
|
+
default:
|
|
733
|
+
return (0, tree_2.isTag)(value) && matchesName(step.name, value.name);
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
//# sourceMappingURL=xpath.js.map
|