@ac-kit/format-xpath 0.0.0-stage → 0.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/CHANGELOG.md +91 -0
- package/LICENSE +28 -0
- package/README.md +223 -2
- package/dist/_axes.d.ts +18 -0
- package/dist/_axes.d.ts.map +1 -0
- package/dist/_axes.js +124 -0
- package/dist/_axes.js.map +1 -0
- package/dist/_code-points.d.ts +13 -0
- package/dist/_code-points.d.ts.map +1 -0
- package/dist/_code-points.js +40 -0
- package/dist/_code-points.js.map +1 -0
- package/dist/_compare.d.ts +14 -0
- package/dist/_compare.d.ts.map +1 -0
- package/dist/_compare.js +160 -0
- package/dist/_compare.js.map +1 -0
- package/dist/_compile.d.ts +87 -0
- package/dist/_compile.d.ts.map +1 -0
- package/dist/_compile.js +262 -0
- package/dist/_compile.js.map +1 -0
- package/dist/_convert.d.ts +16 -0
- package/dist/_convert.d.ts.map +1 -0
- package/dist/_convert.js +46 -0
- package/dist/_convert.js.map +1 -0
- package/dist/_evaluate.d.ts +15 -0
- package/dist/_evaluate.d.ts.map +1 -0
- package/dist/_evaluate.js +160 -0
- package/dist/_evaluate.js.map +1 -0
- package/dist/_foreign-functions.d.ts +7 -0
- package/dist/_foreign-functions.d.ts.map +1 -0
- package/dist/_foreign-functions.js +54 -0
- package/dist/_foreign-functions.js.map +1 -0
- package/dist/_functions.d.ts +24 -0
- package/dist/_functions.d.ts.map +1 -0
- package/dist/_functions.js +312 -0
- package/dist/_functions.js.map +1 -0
- package/dist/_lexer.d.ts +23 -0
- package/dist/_lexer.d.ts.map +1 -0
- package/dist/_lexer.js +211 -0
- package/dist/_lexer.js.map +1 -0
- package/dist/_node-test.d.ts +15 -0
- package/dist/_node-test.d.ts.map +1 -0
- package/dist/_node-test.js +123 -0
- package/dist/_node-test.js.map +1 -0
- package/dist/_number.d.ts +15 -0
- package/dist/_number.d.ts.map +1 -0
- package/dist/_number.js +31 -0
- package/dist/_number.js.map +1 -0
- package/dist/_parse-bounded.d.ts +17 -0
- package/dist/_parse-bounded.d.ts.map +1 -0
- package/dist/_parse-bounded.js +107 -0
- package/dist/_parse-bounded.js.map +1 -0
- package/dist/_parser.d.ts +15 -0
- package/dist/_parser.d.ts.map +1 -0
- package/dist/_parser.js +391 -0
- package/dist/_parser.js.map +1 -0
- package/dist/_run.d.ts +169 -0
- package/dist/_run.d.ts.map +1 -0
- package/dist/_run.js +646 -0
- package/dist/_run.js.map +1 -0
- package/dist/_walk-expression.d.ts +8 -0
- package/dist/_walk-expression.d.ts.map +1 -0
- package/dist/_walk-expression.js +54 -0
- package/dist/_walk-expression.js.map +1 -0
- package/dist/ast.d.ts +110 -0
- package/dist/ast.d.ts.map +1 -0
- package/dist/ast.js +2 -0
- package/dist/ast.js.map +1 -0
- package/dist/compile-xpath.d.ts +97 -0
- package/dist/compile-xpath.d.ts.map +1 -0
- package/dist/compile-xpath.js +105 -0
- package/dist/compile-xpath.js.map +1 -0
- package/dist/errors.d.ts +52 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +68 -0
- package/dist/errors.js.map +1 -0
- package/dist/evaluate-xpath.d.ts +11 -0
- package/dist/evaluate-xpath.d.ts.map +1 -0
- package/dist/evaluate-xpath.js +11 -0
- package/dist/evaluate-xpath.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/dist/limits.d.ts +77 -0
- package/dist/limits.d.ts.map +1 -0
- package/dist/limits.js +74 -0
- package/dist/limits.js.map +1 -0
- package/dist/parse-xpath.d.ts +27 -0
- package/dist/parse-xpath.d.ts.map +1 -0
- package/dist/parse-xpath.js +41 -0
- package/dist/parse-xpath.js.map +1 -0
- package/dist/values.d.ts +68 -0
- package/dist/values.d.ts.map +1 -0
- package/dist/values.js +28 -0
- package/dist/values.js.map +1 -0
- package/package.json +68 -3
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { XPathExpression } from "./ast.js";
|
|
2
|
+
/**
|
|
3
|
+
* Calls `visit` on every expression of the tree in pre-order with its depth
|
|
4
|
+
* (the root is `1`), without recursion, so a tree built by hand of any depth is
|
|
5
|
+
* walked safely. `visit` returning `false` skips the node's children.
|
|
6
|
+
*/
|
|
7
|
+
export declare function forEachExpression(root: XPathExpression, visit: (expression: XPathExpression, depth: number) => boolean | void): void;
|
|
8
|
+
//# sourceMappingURL=_walk-expression.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_walk-expression.d.ts","sourceRoot":"","sources":["../src/_walk-expression.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAEhD;;;;GAIG;AACH,wBAAgB,iBAAiB,CAChC,IAAI,EAAE,eAAe,EACrB,KAAK,EAAE,CAAC,UAAU,EAAE,eAAe,EAAE,KAAK,EAAE,MAAM,KAAK,OAAO,GAAG,IAAI,GACnE,IAAI,CAmDN"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Calls `visit` on every expression of the tree in pre-order with its depth
|
|
3
|
+
* (the root is `1`), without recursion, so a tree built by hand of any depth is
|
|
4
|
+
* walked safely. `visit` returning `false` skips the node's children.
|
|
5
|
+
*/
|
|
6
|
+
export function forEachExpression(root, visit) {
|
|
7
|
+
const pending = [root];
|
|
8
|
+
const depths = [1];
|
|
9
|
+
while (pending.length > 0) {
|
|
10
|
+
const expression = pending.pop();
|
|
11
|
+
const depth = depths.pop();
|
|
12
|
+
if (visit(expression, depth) === false) {
|
|
13
|
+
continue;
|
|
14
|
+
}
|
|
15
|
+
const push = (child) => {
|
|
16
|
+
pending.push(child);
|
|
17
|
+
depths.push(depth + 1);
|
|
18
|
+
};
|
|
19
|
+
switch (expression.kind) {
|
|
20
|
+
case "binary":
|
|
21
|
+
push(expression.right);
|
|
22
|
+
push(expression.left);
|
|
23
|
+
break;
|
|
24
|
+
case "negate":
|
|
25
|
+
push(expression.operand);
|
|
26
|
+
break;
|
|
27
|
+
case "path":
|
|
28
|
+
for (let step = expression.steps.length - 1; step >= 0; step--) {
|
|
29
|
+
const { predicates } = expression.steps[step];
|
|
30
|
+
for (let index = predicates.length - 1; index >= 0; index--) {
|
|
31
|
+
push(predicates[index]);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
if (typeof expression.start !== "string") {
|
|
35
|
+
push(expression.start);
|
|
36
|
+
}
|
|
37
|
+
break;
|
|
38
|
+
case "filter":
|
|
39
|
+
for (let index = expression.predicates.length - 1; index >= 0; index--) {
|
|
40
|
+
push(expression.predicates[index]);
|
|
41
|
+
}
|
|
42
|
+
push(expression.primary);
|
|
43
|
+
break;
|
|
44
|
+
case "call":
|
|
45
|
+
for (let index = expression.arguments.length - 1; index >= 0; index--) {
|
|
46
|
+
push(expression.arguments[index]);
|
|
47
|
+
}
|
|
48
|
+
break;
|
|
49
|
+
default:
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=_walk-expression.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_walk-expression.js","sourceRoot":"","sources":["../src/_walk-expression.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,MAAM,UAAU,iBAAiB,CAChC,IAAqB,EACrB,KAAqE;IAErE,MAAM,OAAO,GAAsB,CAAC,IAAI,CAAC,CAAC;IAC1C,MAAM,MAAM,GAAa,CAAC,CAAC,CAAC,CAAC;IAC7B,OAAO,OAAO,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC3B,MAAM,UAAU,GAAG,OAAO,CAAC,GAAG,EAAG,CAAC;QAClC,MAAM,KAAK,GAAG,MAAM,CAAC,GAAG,EAAG,CAAC;QAC5B,IAAI,KAAK,CAAC,UAAU,EAAE,KAAK,CAAC,KAAK,KAAK,EAAE,CAAC;YACxC,SAAS;QACV,CAA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,UAAU,CAAC,OAAO,CAAC,CAAC;gBACzB,MAAM;YACP,KAAK,MAAM;gBACV,KAAK,IAAI,KAAK,GAAG,UAAU,CAAC,SAAS,CAAC,MAAM,GAAG,CAAC,EAAE,KAAK,IAAI,CAAC,EAAE,KAAK,EAAE,EAAE,CAAC;oBACvE,IAAI,CAAC,UAAU,CAAC,SAAS,CAAC,KAAK,CAAE,CAAC,CAAC;gBACpC,CAAC;gBACD,MAAM;YACP;gBACC,MAAM;QACR,CAAC;IACF,CAAC;AACF,CAAC"}
|
package/dist/ast.d.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import type { Span } from "@ac-kit/core";
|
|
2
|
+
/** The thirteen XPath 1.0 axes. */
|
|
3
|
+
export type XPathAxis = "ancestor" | "ancestor-or-self" | "attribute" | "child" | "descendant" | "descendant-or-self" | "following" | "following-sibling" | "namespace" | "parent" | "preceding" | "preceding-sibling" | "self";
|
|
4
|
+
/**
|
|
5
|
+
* What a step keeps of the nodes its axis yields.
|
|
6
|
+
*
|
|
7
|
+
* Name tests (`name`, `anyName`, `anyLocalName`) select the axis's principal
|
|
8
|
+
* node type: attributes on the `attribute` axis, namespaces on the `namespace`
|
|
9
|
+
* axis, elements everywhere else. A prefix is kept as written and resolved
|
|
10
|
+
* against the namespace bindings when the expression is compiled.
|
|
11
|
+
*/
|
|
12
|
+
export type XPathNodeTest =
|
|
13
|
+
/** `local` or `prefix:local`. */
|
|
14
|
+
{
|
|
15
|
+
readonly kind: "name";
|
|
16
|
+
readonly prefix: string | null;
|
|
17
|
+
readonly local: string;
|
|
18
|
+
}
|
|
19
|
+
/** `*`. */
|
|
20
|
+
| {
|
|
21
|
+
readonly kind: "anyName";
|
|
22
|
+
}
|
|
23
|
+
/** `prefix:*`. */
|
|
24
|
+
| {
|
|
25
|
+
readonly kind: "anyLocalName";
|
|
26
|
+
readonly prefix: string;
|
|
27
|
+
}
|
|
28
|
+
/** `node()`, `text()` or `comment()`. */
|
|
29
|
+
| {
|
|
30
|
+
readonly kind: "nodeType";
|
|
31
|
+
readonly type: "node" | "text" | "comment";
|
|
32
|
+
}
|
|
33
|
+
/** `processing-instruction()`, or `processing-instruction('target')`. */
|
|
34
|
+
| {
|
|
35
|
+
readonly kind: "processingInstruction";
|
|
36
|
+
readonly target: string | null;
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* One step of a location path, with every abbreviation expanded: `@x` is
|
|
40
|
+
* `attribute::x`, `.` is `self::node()`, `..` is `parent::node()`, a bare name
|
|
41
|
+
* is `child::name`, and `//` is a `descendant-or-self::node()` step of its
|
|
42
|
+
* own.
|
|
43
|
+
*/
|
|
44
|
+
export type XPathStep = {
|
|
45
|
+
readonly axis: XPathAxis;
|
|
46
|
+
readonly test: XPathNodeTest;
|
|
47
|
+
readonly predicates: readonly XPathExpression[];
|
|
48
|
+
/** Where the step is written, when the tree came from the parser. */
|
|
49
|
+
readonly span?: Span;
|
|
50
|
+
};
|
|
51
|
+
/** Every binary operator, `|` (union) included. */
|
|
52
|
+
export type XPathBinaryOperator = "or" | "and" | "=" | "!=" | "<" | "<=" | ">" | ">=" | "+" | "-" | "*" | "div" | "mod" | "|";
|
|
53
|
+
/**
|
|
54
|
+
* An XPath 1.0 expression.
|
|
55
|
+
*
|
|
56
|
+
* `span` is set on every node {@link parseXPath} builds and is optional for a
|
|
57
|
+
* tree built by hand; errors found when compiling report it.
|
|
58
|
+
*/
|
|
59
|
+
export type XPathExpression = {
|
|
60
|
+
readonly span?: Span;
|
|
61
|
+
} & ({
|
|
62
|
+
readonly kind: "binary";
|
|
63
|
+
readonly operator: XPathBinaryOperator;
|
|
64
|
+
readonly left: XPathExpression;
|
|
65
|
+
readonly right: XPathExpression;
|
|
66
|
+
}
|
|
67
|
+
/** Unary minus. */
|
|
68
|
+
| {
|
|
69
|
+
readonly kind: "negate";
|
|
70
|
+
readonly operand: XPathExpression;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* A location path. `start` is `"root"` for an absolute path (`/…`),
|
|
74
|
+
* `"context"` for a relative one, or the filter expression written before the
|
|
75
|
+
* first `/` (`$nodes/a`, `id('x')//b`). An absolute path may have no steps:
|
|
76
|
+
* `/` alone selects the root.
|
|
77
|
+
*/
|
|
78
|
+
| {
|
|
79
|
+
readonly kind: "path";
|
|
80
|
+
readonly start: "root" | "context" | XPathExpression;
|
|
81
|
+
readonly steps: readonly XPathStep[];
|
|
82
|
+
}
|
|
83
|
+
/** A primary expression followed by predicates: `(a | b)[1]`, `$set[@x]`. */
|
|
84
|
+
| {
|
|
85
|
+
readonly kind: "filter";
|
|
86
|
+
readonly primary: XPathExpression;
|
|
87
|
+
readonly predicates: readonly XPathExpression[];
|
|
88
|
+
}
|
|
89
|
+
/** `$local` or `$prefix:local`. */
|
|
90
|
+
| {
|
|
91
|
+
readonly kind: "variable";
|
|
92
|
+
readonly prefix: string | null;
|
|
93
|
+
readonly local: string;
|
|
94
|
+
}
|
|
95
|
+
/** A string literal, without its quotes. */
|
|
96
|
+
| {
|
|
97
|
+
readonly kind: "literal";
|
|
98
|
+
readonly value: string;
|
|
99
|
+
} | {
|
|
100
|
+
readonly kind: "number";
|
|
101
|
+
readonly value: number;
|
|
102
|
+
}
|
|
103
|
+
/** A function call; `prefix` is `null` for the core function library. */
|
|
104
|
+
| {
|
|
105
|
+
readonly kind: "call";
|
|
106
|
+
readonly prefix: string | null;
|
|
107
|
+
readonly local: string;
|
|
108
|
+
readonly arguments: readonly XPathExpression[];
|
|
109
|
+
});
|
|
110
|
+
//# sourceMappingURL=ast.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ast.d.ts","sourceRoot":"","sources":["../src/ast.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,cAAc,CAAC;AAEzC,mCAAmC;AACnC,MAAM,MAAM,SAAS,GAClB,UAAU,GACV,kBAAkB,GAClB,WAAW,GACX,OAAO,GACP,YAAY,GACZ,oB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AC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IAC/B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,SAAS,EAAE,SAAS,eAAe,EAAE,CAAC;CAC9C,CACH,CAAC"}
|
package/dist/ast.js
ADDED
package/dist/ast.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ast.js","sourceRoot":"","sources":["../src/ast.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { type WorkBudget } from "@ac-kit/core";
|
|
2
|
+
import { type MarkupDocumentMode, type MarkupTreeShape } from "@ac-kit/model-markup";
|
|
3
|
+
import type { XPathExpression } from "./ast.js";
|
|
4
|
+
import { type XPathLimits } from "./limits.js";
|
|
5
|
+
import type { XPathNode, XPathValue, XPathVariableValue } from "./values.js";
|
|
6
|
+
/** How an expression is compiled. */
|
|
7
|
+
export type XPathOptions = {
|
|
8
|
+
/**
|
|
9
|
+
* Prefix → namespace URI, for the prefixes of names, wildcards, variables and
|
|
10
|
+
* functions in the expression. `xml` is always bound to the XML namespace. An
|
|
11
|
+
* unprefixed name never takes a namespace from here: it is in no namespace,
|
|
12
|
+
* except an element name in an HTML document, which is in the HTML
|
|
13
|
+
* namespace.
|
|
14
|
+
*/
|
|
15
|
+
readonly namespaces?: ReadonlyMap<string, string>;
|
|
16
|
+
readonly limits?: XPathLimits;
|
|
17
|
+
};
|
|
18
|
+
/** What one evaluation reads besides the tree. */
|
|
19
|
+
export type XPathEvaluationContext<TNode, TAttribute> = {
|
|
20
|
+
/**
|
|
21
|
+
* The variables, keyed by local name for `$name`, or `{uri}local` for
|
|
22
|
+
* `$prefix:local` with the prefix resolved through
|
|
23
|
+
* {@link XPathOptions.namespaces}.
|
|
24
|
+
*/
|
|
25
|
+
readonly variables?: ReadonlyMap<string, XPathVariableValue<TNode, TAttribute>>;
|
|
26
|
+
/**
|
|
27
|
+
* Overrides `shape.documentModeOf(contextNode)`: `"html"` and `"html-quirks"`
|
|
28
|
+
* apply the HTML rules to element and attribute names.
|
|
29
|
+
*/
|
|
30
|
+
readonly documentMode?: MarkupDocumentMode;
|
|
31
|
+
/**
|
|
32
|
+
* Charged with every unit of work the evaluation does, so one allowance caps
|
|
33
|
+
* many evaluations — every expression run over one page, say. Its exhaustion
|
|
34
|
+
* throws whatever `spend` throws (`FixedWorkBudget` throws
|
|
35
|
+
* `WorkBudgetExceededError`). Without it, each evaluation gets a budget of
|
|
36
|
+
* `limits.maxSteps` units and reports exhaustion as
|
|
37
|
+
* {@link XPathLimitExceededError} of kind `"steps"`.
|
|
38
|
+
*/
|
|
39
|
+
readonly budget?: WorkBudget;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* An expression compiled once, then evaluated over any shape and context node,
|
|
43
|
+
* with context position and size 1.
|
|
44
|
+
*
|
|
45
|
+
* Every evaluating method is synchronous and bounded by the work budget; the
|
|
46
|
+
* shape's members are called unbound. A tree without `compareDocumentOrder` is
|
|
47
|
+
* numbered once per evaluation the first time two nodes must be ordered, and
|
|
48
|
+
* one without `elementByIdOf` is scanned once per evaluation by the first
|
|
49
|
+
* `id()`: index a page read by many evaluations with `indexMarkupTree`
|
|
50
|
+
* (`@ac-kit/model-markup`). Nothing is cached across evaluations.
|
|
51
|
+
*
|
|
52
|
+
* Every evaluating method throws {@link XPathTypeError} for a non-node-set where
|
|
53
|
+
* one is required (when a variable made it undecidable at compile time),
|
|
54
|
+
* {@link XPathUnboundVariableError}, {@link XPathLimitExceededError} of kind
|
|
55
|
+
* `"steps"` (own budget) or `"stringLength"`, and whatever a caller's budget
|
|
56
|
+
* throws.
|
|
57
|
+
*/
|
|
58
|
+
export type CompiledXPath = {
|
|
59
|
+
/** The syntax tree, as parsed or as given. */
|
|
60
|
+
readonly expression: XPathExpression;
|
|
61
|
+
evaluate<TNode, TAttribute>(shape: MarkupTreeShape<TNode, TAttribute>, contextNode: XPathNode<TNode, TAttribute>, context?: XPathEvaluationContext<TNode, TAttribute>): XPathValue<TNode, TAttribute>;
|
|
62
|
+
/**
|
|
63
|
+
* The node-set the expression selects, in document order.
|
|
64
|
+
*
|
|
65
|
+
* @throws {XPathTypeError} When the result is not a node-set.
|
|
66
|
+
*/
|
|
67
|
+
selectNodes<TNode, TAttribute>(shape: MarkupTreeShape<TNode, TAttribute>, contextNode: XPathNode<TNode, TAttribute>, context?: XPathEvaluationContext<TNode, TAttribute>): XPathNode<TNode, TAttribute>[];
|
|
68
|
+
/**
|
|
69
|
+
* The first selected node in document order, `null` for an empty node-set.
|
|
70
|
+
*
|
|
71
|
+
* @throws {XPathTypeError} When the result is not a node-set.
|
|
72
|
+
*/
|
|
73
|
+
selectFirst<TNode, TAttribute>(shape: MarkupTreeShape<TNode, TAttribute>, contextNode: XPathNode<TNode, TAttribute>, context?: XPathEvaluationContext<TNode, TAttribute>): XPathNode<TNode, TAttribute> | null;
|
|
74
|
+
/** The result converted as `string()` does. */
|
|
75
|
+
evaluateString<TNode, TAttribute>(shape: MarkupTreeShape<TNode, TAttribute>, contextNode: XPathNode<TNode, TAttribute>, context?: XPathEvaluationContext<TNode, TAttribute>): string;
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* Compiles an XPath 1.0 expression, given as source text or as a syntax tree
|
|
79
|
+
* (checked against the limits as if parsed).
|
|
80
|
+
*
|
|
81
|
+
* The whole of XPath 1.0: the 13 axes, every node test, predicates, unions,
|
|
82
|
+
* filter expressions, the operators with their node-set semantics, the 27 core
|
|
83
|
+
* functions (string functions counting code points, never splitting a surrogate
|
|
84
|
+
* pair), variables and namespace prefixes. Every name is resolved and every
|
|
85
|
+
* arity and node-set requirement decidable without the variables is checked
|
|
86
|
+
* now. O(length).
|
|
87
|
+
*
|
|
88
|
+
* @throws {XPathSyntaxError} When `expression` is text that does not parse.
|
|
89
|
+
* @throws {XPathStaticError} For a function outside the core library (named
|
|
90
|
+
* when it is an XPath 2.0, 3.0 or XSLT one), a wrong argument count or an
|
|
91
|
+
* unbound prefix.
|
|
92
|
+
* @throws {XPathTypeError} For a non-node-set where one is required.
|
|
93
|
+
* @throws {XPathLimitExceededError} Of kind `"length"` or `"nestingDepth"`.
|
|
94
|
+
* @throws {RangeError} For an invalid limit or namespace binding.
|
|
95
|
+
*/
|
|
96
|
+
export declare function compileXPath(expression: string | XPathExpression, options?: XPathOptions): CompiledXPath;
|
|
97
|
+
//# sourceMappingURL=compile-xpath.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compile-xpath.d.ts","sourceRoot":"","sources":["../src/compile-xpath.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,UAAU,EAAkB,MAAM,cAAc,CAAC;AAC/D,OAAO,EACN,KAAK,kBAAkB,EACvB,KAAK,eAAe,EAEpB,MAAM,sBAAsB,CAAC;AAQ9B,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAEhD,OAAO,EACN,KAAK,WAAW,EAGhB,MAAM,aAAa,CAAC;AACrB,OAAO,KAAK,EAAE,SAAS,EAAE,UAAU,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAE7E,qCAAqC;AACrC,MAAM,MAAM,YAAY,GAAG;IAC1B;;;;;;OAMG;IACH,QAAQ,CAAC,UAAU,CAAC,EAAE,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAClD,QAAQ,CAAC,MAAM,CAAC,EAAE,WAAW,CAAC;CAC9B,CAAC;AAEF,kDAAkD;AAClD,MAAM,MAAM,sBAAsB,CAAC,KAAK,EAAE,UAAU,IAAI;IACvD;;;;OAIG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,WAAW,CAC/B,MAAM,EACN,kBAAkB,CAAC,KAAK,EAAE,UAAU,CAAC,CACrC,CAAC;IAEF;;;OAGG;IACH,QAAQ,CAAC,YAAY,CAAC,EAAE,kBAAkB,CAAC;IAE3C;;;;;;;OAOG;IACH,QAAQ,CAAC,MAAM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uBF;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,YAAY,CAC3B,UAAU,EAAE,MAAM,GAAG,eAAe,EACpC,OAAO,GAAE,YAAiB,GACxB,aAAa,CA0Gf"}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { withWorkBudget } from "@ac-kit/core";
|
|
2
|
+
import { XML_NAMESPACE, } from "@ac-kit/model-markup";
|
|
3
|
+
import { compileExpression } from "./_compile.js";
|
|
4
|
+
import { toStringValue } from "./_convert.js";
|
|
5
|
+
import { evaluateCompiled } from "./_evaluate.js";
|
|
6
|
+
import { checkTreeSize, parseBounded } from "./_parse-bounded.js";
|
|
7
|
+
import { EvaluationRun } from "./_run.js";
|
|
8
|
+
import { XPathTypeError } from "./errors.js";
|
|
9
|
+
import { XPathLimitExceededError, xpathLimits, } from "./limits.js";
|
|
10
|
+
function checkNamespaces(namespaces) {
|
|
11
|
+
const resolved = new Map([["xml", XML_NAMESPACE]]);
|
|
12
|
+
if (namespaces === undefined) {
|
|
13
|
+
return resolved;
|
|
14
|
+
}
|
|
15
|
+
for (const [prefix, uri] of namespaces) {
|
|
16
|
+
if (uri === "") {
|
|
17
|
+
throw new RangeError(`The prefix '${prefix}' is bound to an empty namespace`);
|
|
18
|
+
}
|
|
19
|
+
if (prefix === "xml" && uri !== XML_NAMESPACE) {
|
|
20
|
+
throw new RangeError("The prefix 'xml' cannot be rebound");
|
|
21
|
+
}
|
|
22
|
+
resolved.set(prefix, uri);
|
|
23
|
+
}
|
|
24
|
+
return resolved;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Compiles an XPath 1.0 expression, given as source text or as a syntax tree
|
|
28
|
+
* (checked against the limits as if parsed).
|
|
29
|
+
*
|
|
30
|
+
* The whole of XPath 1.0: the 13 axes, every node test, predicates, unions,
|
|
31
|
+
* filter expressions, the operators with their node-set semantics, the 27 core
|
|
32
|
+
* functions (string functions counting code points, never splitting a surrogate
|
|
33
|
+
* pair), variables and namespace prefixes. Every name is resolved and every
|
|
34
|
+
* arity and node-set requirement decidable without the variables is checked
|
|
35
|
+
* now. O(length).
|
|
36
|
+
*
|
|
37
|
+
* @throws {XPathSyntaxError} When `expression` is text that does not parse.
|
|
38
|
+
* @throws {XPathStaticError} For a function outside the core library (named
|
|
39
|
+
* when it is an XPath 2.0, 3.0 or XSLT one), a wrong argument count or an
|
|
40
|
+
* unbound prefix.
|
|
41
|
+
* @throws {XPathTypeError} For a non-node-set where one is required.
|
|
42
|
+
* @throws {XPathLimitExceededError} Of kind `"length"` or `"nestingDepth"`.
|
|
43
|
+
* @throws {RangeError} For an invalid limit or namespace binding.
|
|
44
|
+
*/
|
|
45
|
+
export function compileXPath(expression, options = {}) {
|
|
46
|
+
const limits = xpathLimits(options.limits);
|
|
47
|
+
const { maxLength, maxNestingDepth, maxSteps, maxStringLength } = limits;
|
|
48
|
+
let tree;
|
|
49
|
+
if (typeof expression === "string") {
|
|
50
|
+
tree = parseBounded(expression, maxLength, maxNestingDepth);
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
checkTreeSize(expression, maxLength, maxNestingDepth);
|
|
54
|
+
tree = expression;
|
|
55
|
+
}
|
|
56
|
+
const compiled = compileExpression(tree, checkNamespaces(options.namespaces));
|
|
57
|
+
const run = (shape, contextNode, context, finish) => {
|
|
58
|
+
return withWorkBudget(context?.budget ?? maxSteps, (budget) => {
|
|
59
|
+
const evaluation = new EvaluationRun(shape, budget, maxStringLength, contextNode, context?.documentMode, context?.variables);
|
|
60
|
+
const node = evaluation.adopt(contextNode);
|
|
61
|
+
return finish(evaluation, evaluateCompiled(evaluation, compiled, node));
|
|
62
|
+
}, (cause) => new XPathLimitExceededError("steps", maxSteps, { cause }));
|
|
63
|
+
};
|
|
64
|
+
const nodesOf = (value) => {
|
|
65
|
+
if (!Array.isArray(value)) {
|
|
66
|
+
throw new XPathTypeError(`The expression evaluates to a ${typeof value}, not a node-set`, tree.span ?? null);
|
|
67
|
+
}
|
|
68
|
+
return value;
|
|
69
|
+
};
|
|
70
|
+
function evaluate(shape, contextNode, context) {
|
|
71
|
+
return run(shape, contextNode, context, (_evaluation, value) => {
|
|
72
|
+
if (Array.isArray(value)) {
|
|
73
|
+
return { type: "nodeSet", nodes: nodesOf(value) };
|
|
74
|
+
}
|
|
75
|
+
switch (typeof value) {
|
|
76
|
+
case "string":
|
|
77
|
+
return { type: "string", value };
|
|
78
|
+
case "number":
|
|
79
|
+
return { type: "number", value };
|
|
80
|
+
default:
|
|
81
|
+
return { type: "boolean", value };
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
function selectNodes(shape, contextNode, context) {
|
|
86
|
+
return run(shape, contextNode, context, (_evaluation, value) => nodesOf(value));
|
|
87
|
+
}
|
|
88
|
+
function selectFirst(shape, contextNode, context) {
|
|
89
|
+
return run(shape, contextNode, context, (_evaluation, value) => {
|
|
90
|
+
const nodes = nodesOf(value);
|
|
91
|
+
return nodes.length === 0 ? null : nodes[0];
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
function evaluateString(shape, contextNode, context) {
|
|
95
|
+
return run(shape, contextNode, context, (evaluation, value) => toStringValue(evaluation, value));
|
|
96
|
+
}
|
|
97
|
+
return {
|
|
98
|
+
expression: tree,
|
|
99
|
+
evaluate,
|
|
100
|
+
selectNodes,
|
|
101
|
+
selectFirst,
|
|
102
|
+
evaluateString,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
//# sourceMappingURL=compile-xpath.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compile-xpath.js","sourceRoot":"","sources":["../src/compile-xpath.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmB,cAAc,EAAE,MAAM,cAAc,CAAC;AAC/D,OAAO,EAGN,aAAa,GACb,MAAM,sBAAsB,CAAC;AAG9B,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAClD,OAAO,EAAE,aAAa,EAAc,MAAM,eAAe,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AAyC,EACzC,OAAmD;QAEnD,OAAO,GAAG,CAAC,KAAK,EAAE,WAAW,EAAE,OAAO,EAAE,CAAC,UAAU,EAAE,KAAK,EAAE,EAAE,CAC7D,aAAa,CAAC,UAAU,EAAE,KAAK,CAAC,CAChC,CAAC;IACH,CAAC;IAED,OAAO;QACN,UAAU,EAAE,IAAI;QAChB,QAAQ;QACR,WAAW;QACX,WAAW;QACX,cAAc;KACd,CAAC;AACH,CAAC"}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { Span } from "@ac-kit/core";
|
|
2
|
+
/** Thrown when an expression is not XPath 1.0 syntax. */
|
|
3
|
+
export declare class XPathSyntaxError extends SyntaxError {
|
|
4
|
+
readonly source: string;
|
|
5
|
+
readonly offset: number;
|
|
6
|
+
/**
|
|
7
|
+
* @param source The whole expression.
|
|
8
|
+
* @param offset Where parsing gave up, in UTF-16 code units.
|
|
9
|
+
*/
|
|
10
|
+
constructor(message: string, source: string, offset: number);
|
|
11
|
+
}
|
|
12
|
+
/** What an {@link XPathStaticError} reports. */
|
|
13
|
+
export type XPathStaticErrorCode = "unknownFunction" | "arity" | "unboundPrefix";
|
|
14
|
+
/**
|
|
15
|
+
* An expression that parses but cannot be compiled: a function outside the
|
|
16
|
+
* XPath 1.0 core library (the message names the XPath 2.0, 3.0 or XSLT function
|
|
17
|
+
* when one is called, such as `matches()` or `lower-case()`), a call with the
|
|
18
|
+
* wrong number of arguments, or a prefix the namespace bindings do not
|
|
19
|
+
* declare.
|
|
20
|
+
*/
|
|
21
|
+
export declare class XPathStaticError extends Error {
|
|
22
|
+
readonly code: XPathStaticErrorCode;
|
|
23
|
+
readonly subject: string;
|
|
24
|
+
readonly span: Span | null;
|
|
25
|
+
/**
|
|
26
|
+
* @param subject The function name as written (`prefix:local` when prefixed),
|
|
27
|
+
* or the unbound prefix.
|
|
28
|
+
* @param span Where it is written; `null` for a syntax tree built without
|
|
29
|
+
* spans.
|
|
30
|
+
*/
|
|
31
|
+
constructor(message: string, code: XPathStaticErrorCode, subject: string, span: Span | null);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* A value that is not a node-set where XPath 1.0 requires one: a path step
|
|
35
|
+
* after it, a predicate on it, a `|` operand, or the argument of `count()`,
|
|
36
|
+
* `sum()`, `name()`, `local-name()` or `namespace-uri()`. Reported when
|
|
37
|
+
* compiling when the type is known without the variables, otherwise when
|
|
38
|
+
* evaluating; also thrown for a variable bound to something that is not an
|
|
39
|
+
* XPath value.
|
|
40
|
+
*/
|
|
41
|
+
export declare class XPathTypeError extends TypeError {
|
|
42
|
+
readonly span: Span | null;
|
|
43
|
+
/** @param span Where the offending expression is written, when known. */
|
|
44
|
+
constructor(message: string, span: Span | null);
|
|
45
|
+
}
|
|
46
|
+
/** A variable the expression references but the evaluation does not bind. */
|
|
47
|
+
export declare class XPathUnboundVariableError extends Error {
|
|
48
|
+
readonly variable: string;
|
|
49
|
+
/** @param variable Its key, as in `variables`: `local` or `{uri}local`. */
|
|
50
|
+
constructor(variable: string);
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,cAAc,CAAC;AAEzC,yDAAyD;AACzD,qBAAa,gBAAiB,SAAQ,WAAW;IAO/C,QAAQ,CAAC,MAAM,EAAE,MAAM;IACvB,QAAQ,CAAC,MAAM,EAAE,MAAM;IAPxB;;;OAGG;IACH,YACC,OAAO,EAAE,MAAM,EACN,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EAIvB;CACD;AAED,gDAAgD;AAChD,MAAM,MAAM,oBAAoB,GAC7B,iBAAiB,GACjB,OAAO,GACP,eAAe,CAAC;AAEnB;;;;;;GAMG;AACH,qBAAa,gBAAiB,SAAQ,KAAK;IASzC,QAAQ,CAAC,IAAI,EAAE,oBAAoB;IACnC,QAAQ,CAAC,OAAO,EAAE,MAAM;IACxB,QAAQ,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI;IAV3B;;;;;OAKG;IACH,YACC,OAAO,EAAE,MAAM,EACN,IAAI,EAAE,oBAAoB,EAC1B,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,IAAI,GAAG,IAAI,EAI1B;CACD;AAED;;;;;;;GAOG;AACH,qBAAa,cAAe,SAAQ,SAAS;IAI3C,QAAQ,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI;IAH3B,yEAAyE;IACzE,YACC,OAAO,EAAE,MAAM,EACN,IAAI,EAAE,IAAI,GAAG,IAAI,EAI1B;CACD;AAED,6EAA6E;AAC7E,qBAAa,yBAA0B,SAAQ,KAAK;IAEvC,QAAQ,CAAC,QAAQ,EAAE,MAAM;IADrC,2EAA2E;IAC3E,YAAqB,QAAQ,EAAE,MAAM,EAGpC;CACD"}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/** Thrown when an expression is not XPath 1.0 syntax. */
|
|
2
|
+
export class XPathSyntaxError extends SyntaxError {
|
|
3
|
+
source;
|
|
4
|
+
offset;
|
|
5
|
+
/**
|
|
6
|
+
* @param source The whole expression.
|
|
7
|
+
* @param offset Where parsing gave up, in UTF-16 code units.
|
|
8
|
+
*/
|
|
9
|
+
constructor(message, source, offset) {
|
|
10
|
+
super(`${message} at offset ${offset}`);
|
|
11
|
+
this.source = source;
|
|
12
|
+
this.offset = offset;
|
|
13
|
+
this.name = "XPathSyntaxError";
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* An expression that parses but cannot be compiled: a function outside the
|
|
18
|
+
* XPath 1.0 core library (the message names the XPath 2.0, 3.0 or XSLT function
|
|
19
|
+
* when one is called, such as `matches()` or `lower-case()`), a call with the
|
|
20
|
+
* wrong number of arguments, or a prefix the namespace bindings do not
|
|
21
|
+
* declare.
|
|
22
|
+
*/
|
|
23
|
+
export class XPathStaticError extends Error {
|
|
24
|
+
code;
|
|
25
|
+
subject;
|
|
26
|
+
span;
|
|
27
|
+
/**
|
|
28
|
+
* @param subject The function name as written (`prefix:local` when prefixed),
|
|
29
|
+
* or the unbound prefix.
|
|
30
|
+
* @param span Where it is written; `null` for a syntax tree built without
|
|
31
|
+
* spans.
|
|
32
|
+
*/
|
|
33
|
+
constructor(message, code, subject, span) {
|
|
34
|
+
super(message);
|
|
35
|
+
this.code = code;
|
|
36
|
+
this.subject = subject;
|
|
37
|
+
this.span = span;
|
|
38
|
+
this.name = "XPathStaticError";
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* A value that is not a node-set where XPath 1.0 requires one: a path step
|
|
43
|
+
* after it, a predicate on it, a `|` operand, or the argument of `count()`,
|
|
44
|
+
* `sum()`, `name()`, `local-name()` or `namespace-uri()`. Reported when
|
|
45
|
+
* compiling when the type is known without the variables, otherwise when
|
|
46
|
+
* evaluating; also thrown for a variable bound to something that is not an
|
|
47
|
+
* XPath value.
|
|
48
|
+
*/
|
|
49
|
+
export class XPathTypeError extends TypeError {
|
|
50
|
+
span;
|
|
51
|
+
/** @param span Where the offending expression is written, when known. */
|
|
52
|
+
constructor(message, span) {
|
|
53
|
+
super(message);
|
|
54
|
+
this.span = span;
|
|
55
|
+
this.name = "XPathTypeError";
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/** A variable the expression references but the evaluation does not bind. */
|
|
59
|
+
export class XPathUnboundVariableError extends Error {
|
|
60
|
+
variable;
|
|
61
|
+
/** @param variable Its key, as in `variables`: `local` or `{uri}local`. */
|
|
62
|
+
constructor(variable) {
|
|
63
|
+
super(`XPath variable $${variable} is not bound`);
|
|
64
|
+
this.variable = variable;
|
|
65
|
+
this.name = "XPathUnboundVariableError";
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAEA,yDAAyD;AACzD,MAAM,OAAO,gBAAiB,SAAQ,WAAW;IAOtC,MAAM;IACN,MAAM;IAPhB;;;OAGG;IACH,YACC,OAAe,EACN,MAAc,EACd,MAAc;QAEvB,KAAK,CAAC,GAAG,OAAO,cAAc,MAAM,EAAE,CAAC,CAAC;sBAH/B,MAAM;sBACN,MAAM;QAGf,IAAI,CAAC,IAAI,GAAG,kBAAkB,CAAC;IAChC,CAAC;CACD;AAQD;;;;;;GAMG;AACH,MAAM,OAAO,gBAAiB,SAAQ,KAAK;IAShC,IAAI;IACJ,OAAO;IACP,IAAI;IAVd;;;;;OAKG;IACH,YACC,OAAe,EACN,IAA0B,EAC1B,OAAe,EACf,IAAiB;QAE1B,KAAK,CAAC,OAAO,CAAC,CAAC;oBAJN,IAAI;uBACJ,OAAO;oBACP,IAAI;QAGb,IAAI,CAAC,IAAI,GAAG,kBAAkB,CAAC;IAChC,CAAC;CACD;AAED;;;;;;;GAOG;AACH,MAAM,OAAO,cAAe,SAAQ,SAAS;IAIlC,IAAI;IAHd,yEAAyE;IACzE,YACC,OAAe,EACN,IAAiB;QAE1B,KAAK,CAAC,OAAO,CAAC,CAAC;oBAFN,IAAI;QAGb,IAAI,CAAC,IAAI,GAAG,gBAAgB,CAAC;IAC9B,CAAC;CACD;AAED,6EAA6E;AAC7E,MAAM,OAAO,yBAA0B,SAAQ,KAAK;IAE9B,QAAQ;IAD7B,2EAA2E;IAC3E,YAAqB,QAAgB;QACpC,KAAK,CAAC,mBAAmB,QAAQ,eAAe,CAAC,CAAC;wBAD9B,QAAQ;QAE5B,IAAI,CAAC,IAAI,GAAG,2BAA2B,CAAC;IACzC,CAAC;CACD"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { MarkupTreeShape } from "@ac-kit/model-markup";
|
|
2
|
+
import { type XPathEvaluationContext, type XPathOptions } from "./compile-xpath.js";
|
|
3
|
+
import type { XPathNode, XPathValue } from "./values.js";
|
|
4
|
+
/**
|
|
5
|
+
* Evaluates an XPath 1.0 expression once: {@link compileXPath} then `evaluate`.
|
|
6
|
+
* Compile once and reuse the result when an expression runs more than once.
|
|
7
|
+
*
|
|
8
|
+
* @throws Everything {@link compileXPath} and `CompiledXPath.evaluate` throw.
|
|
9
|
+
*/
|
|
10
|
+
export declare function evaluateXPath<TNode, TAttribute>(shape: MarkupTreeShape<TNode, TAttribute>, contextNode: XPathNode<TNode, TAttribute>, expression: string, options?: XPathOptions & XPathEvaluationContext<TNode, TAttribute>): XPathValue<TNode, TAttribute>;
|
|
11
|
+
//# sourceMappingURL=evaluate-xpath.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"evaluate-xpath.d.ts","sourceRoot":"","sources":["../src/evaluate-xpath.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAE5D,OAAO,EAEN,KAAK,sBAAsB,EAC3B,KAAK,YAAY,EACjB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,KAAK,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzD;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,UAAU,EAC9C,KAAK,EAAE,eAAe,CAAC,KAAK,EAAE,UAAU,CAAC,EACzC,WAAW,EAAE,SAAS,CAAC,KAAK,EAAE,UAAU,CAAC,EACzC,UAAU,EAAE,MAAM,EAClB,OAAO,GAAE,YAAY,GAAG,sBAAsB,CAAC,KAAK,EAAE,UAAU,CAAM,GACpE,UAAU,CAAC,KAAK,EAAE,UAAU,CAAC,CAM/B"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { compileXPath, } from "./compile-xpath.js";
|
|
2
|
+
/**
|
|
3
|
+
* Evaluates an XPath 1.0 expression once: {@link compileXPath} then `evaluate`.
|
|
4
|
+
* Compile once and reuse the result when an expression runs more than once.
|
|
5
|
+
*
|
|
6
|
+
* @throws Everything {@link compileXPath} and `CompiledXPath.evaluate` throw.
|
|
7
|
+
*/
|
|
8
|
+
export function evaluateXPath(shape, contextNode, expression, options = {}) {
|
|
9
|
+
return compileXPath(expression, options).evaluate(shape, contextNode, options);
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=evaluate-xpath.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"evaluate-xpath.js","sourceRoot":"","sources":["../src/evaluate-xpath.ts"],"names":[],"mappings":"AAEA,OAAO,EACN,YAAY,GAGZ,MAAM,oBAAoB,CAAC;AAG5B;;;;;GAKG;AACH,MAAM,UAAU,aAAa,CAC5B,KAAyC,EACzC,WAAyC,EACzC,UAAkB,EAClB,OAAO,GAA6D,EAAE;IAEtE,OAAO,YAAY,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC,QAAQ,CAChD,KAAK,EACL,WAAW,EACX,OAAO,CACP,CAAC;AACH,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from "./ast.js";
|
|
2
|
+
export * from "./compile-xpath.js";
|
|
3
|
+
export * from "./errors.js";
|
|
4
|
+
export * from "./evaluate-xpath.js";
|
|
5
|
+
export * from "./limits.js";
|
|
6
|
+
export * from "./parse-xpath.js";
|
|
7
|
+
export * from "./values.js";
|
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,oBAAoB,CAAC;AACnC,cAAc,aAAa,CAAC;AAC5B,cAAc,qBAAqB,CAAC;AACpC,cAAc,aAAa,CAAC;AAC5B,cAAc,kBAAkB,CAAC;AACjC,cAAc,aAAa,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from "./ast.js";
|
|
2
|
+
export * from "./compile-xpath.js";
|
|
3
|
+
export * from "./errors.js";
|
|
4
|
+
export * from "./evaluate-xpath.js";
|
|
5
|
+
export * from "./limits.js";
|
|
6
|
+
export * from "./parse-xpath.js";
|
|
7
|
+
export * from "./values.js";
|
|
8
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,oBAAoB,CAAC;AACnC,cAAc,aAAa,CAAC;AAC5B,cAAc,qBAAqB,CAAC;AACpC,cAAc,aAAa,CAAC;AAC5B,cAAc,kBAAkB,CAAC;AACjC,cAAc,aAAa,CAAC"}
|