@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.
Files changed (96) hide show
  1. package/CHANGELOG.md +91 -0
  2. package/LICENSE +28 -0
  3. package/README.md +223 -2
  4. package/dist/_axes.d.ts +18 -0
  5. package/dist/_axes.d.ts.map +1 -0
  6. package/dist/_axes.js +124 -0
  7. package/dist/_axes.js.map +1 -0
  8. package/dist/_code-points.d.ts +13 -0
  9. package/dist/_code-points.d.ts.map +1 -0
  10. package/dist/_code-points.js +40 -0
  11. package/dist/_code-points.js.map +1 -0
  12. package/dist/_compare.d.ts +14 -0
  13. package/dist/_compare.d.ts.map +1 -0
  14. package/dist/_compare.js +160 -0
  15. package/dist/_compare.js.map +1 -0
  16. package/dist/_compile.d.ts +87 -0
  17. package/dist/_compile.d.ts.map +1 -0
  18. package/dist/_compile.js +262 -0
  19. package/dist/_compile.js.map +1 -0
  20. package/dist/_convert.d.ts +16 -0
  21. package/dist/_convert.d.ts.map +1 -0
  22. package/dist/_convert.js +46 -0
  23. package/dist/_convert.js.map +1 -0
  24. package/dist/_evaluate.d.ts +15 -0
  25. package/dist/_evaluate.d.ts.map +1 -0
  26. package/dist/_evaluate.js +160 -0
  27. package/dist/_evaluate.js.map +1 -0
  28. package/dist/_foreign-functions.d.ts +7 -0
  29. package/dist/_foreign-functions.d.ts.map +1 -0
  30. package/dist/_foreign-functions.js +54 -0
  31. package/dist/_foreign-functions.js.map +1 -0
  32. package/dist/_functions.d.ts +24 -0
  33. package/dist/_functions.d.ts.map +1 -0
  34. package/dist/_functions.js +312 -0
  35. package/dist/_functions.js.map +1 -0
  36. package/dist/_lexer.d.ts +23 -0
  37. package/dist/_lexer.d.ts.map +1 -0
  38. package/dist/_lexer.js +211 -0
  39. package/dist/_lexer.js.map +1 -0
  40. package/dist/_node-test.d.ts +15 -0
  41. package/dist/_node-test.d.ts.map +1 -0
  42. package/dist/_node-test.js +123 -0
  43. package/dist/_node-test.js.map +1 -0
  44. package/dist/_number.d.ts +15 -0
  45. package/dist/_number.d.ts.map +1 -0
  46. package/dist/_number.js +31 -0
  47. package/dist/_number.js.map +1 -0
  48. package/dist/_parse-bounded.d.ts +17 -0
  49. package/dist/_parse-bounded.d.ts.map +1 -0
  50. package/dist/_parse-bounded.js +107 -0
  51. package/dist/_parse-bounded.js.map +1 -0
  52. package/dist/_parser.d.ts +15 -0
  53. package/dist/_parser.d.ts.map +1 -0
  54. package/dist/_parser.js +391 -0
  55. package/dist/_parser.js.map +1 -0
  56. package/dist/_run.d.ts +169 -0
  57. package/dist/_run.d.ts.map +1 -0
  58. package/dist/_run.js +646 -0
  59. package/dist/_run.js.map +1 -0
  60. package/dist/_walk-expression.d.ts +8 -0
  61. package/dist/_walk-expression.d.ts.map +1 -0
  62. package/dist/_walk-expression.js +54 -0
  63. package/dist/_walk-expression.js.map +1 -0
  64. package/dist/ast.d.ts +110 -0
  65. package/dist/ast.d.ts.map +1 -0
  66. package/dist/ast.js +2 -0
  67. package/dist/ast.js.map +1 -0
  68. package/dist/compile-xpath.d.ts +97 -0
  69. package/dist/compile-xpath.d.ts.map +1 -0
  70. package/dist/compile-xpath.js +105 -0
  71. package/dist/compile-xpath.js.map +1 -0
  72. package/dist/errors.d.ts +52 -0
  73. package/dist/errors.d.ts.map +1 -0
  74. package/dist/errors.js +68 -0
  75. package/dist/errors.js.map +1 -0
  76. package/dist/evaluate-xpath.d.ts +11 -0
  77. package/dist/evaluate-xpath.d.ts.map +1 -0
  78. package/dist/evaluate-xpath.js +11 -0
  79. package/dist/evaluate-xpath.js.map +1 -0
  80. package/dist/index.d.ts +8 -0
  81. package/dist/index.d.ts.map +1 -0
  82. package/dist/index.js +8 -0
  83. package/dist/index.js.map +1 -0
  84. package/dist/limits.d.ts +77 -0
  85. package/dist/limits.d.ts.map +1 -0
  86. package/dist/limits.js +74 -0
  87. package/dist/limits.js.map +1 -0
  88. package/dist/parse-xpath.d.ts +27 -0
  89. package/dist/parse-xpath.d.ts.map +1 -0
  90. package/dist/parse-xpath.js +41 -0
  91. package/dist/parse-xpath.js.map +1 -0
  92. package/dist/values.d.ts +68 -0
  93. package/dist/values.d.ts.map +1 -0
  94. package/dist/values.js +28 -0
  95. package/dist/values.js.map +1 -0
  96. package/package.json +68 -3
@@ -0,0 +1,77 @@
1
+ /**
2
+ * Bounds on what an expression may cost to parse, compile and evaluate.
3
+ *
4
+ * An extractor's expressions come from third-party plugin indexes and its
5
+ * documents from the web, so neither the expression nor the tree is trusted.
6
+ * The parser, the compiler and the evaluator keep their nesting on explicit
7
+ * stacks, so no input reaches a stack overflow whatever the caller's stack
8
+ * already holds; `maxLength` and `maxNestingDepth` bound the expression, and
9
+ * `maxSteps` and `maxStringLength` the work and the memory of an evaluation.
10
+ */
11
+ export type XPathLimits = {
12
+ /**
13
+ * Longest expression source accepted, in UTF-16 code units, checked before
14
+ * anything is parsed. A syntax tree given to `compileXPath` is measured as
15
+ * printing it would roughly take — one unit per expression and per step, plus
16
+ * the characters of its names and literals — and rejected as soon as the
17
+ * count passes the bound.
18
+ */
19
+ readonly maxLength?: number;
20
+ /**
21
+ * Deepest syntax tree accepted, counting every expression node: `1 + 2` is
22
+ * two deep, `f(g(x))` three, and a predicate one deeper than the path or
23
+ * filter it belongs to. At most {@link XPATH_MAX_NESTING_DEPTH}.
24
+ */
25
+ readonly maxNestingDepth?: number;
26
+ /**
27
+ * Most work units one evaluation may spend when the caller passes no
28
+ * `WorkBudget`; a positive integer or `Infinity`. One unit is one expression
29
+ * evaluated, one axis step from one node, one node visited or ancestor
30
+ * climbed, one character scanned, compared or built, one value compared, or
31
+ * one function call; see the README for the full tariff. Exceeding it throws
32
+ * {@link XPathLimitExceededError} of kind `"steps"`.
33
+ */
34
+ readonly maxSteps?: number;
35
+ /**
36
+ * Longest string an evaluation may produce, in UTF-16 code units: every
37
+ * string-value it reads (of text, elements, attributes, namespaces, comments,
38
+ * instructions), every string a function returns, a number's numeral, a name.
39
+ * A string the caller passes in a variable is input and is not checked; what
40
+ * is computed from it is.
41
+ */
42
+ readonly maxStringLength?: number;
43
+ };
44
+ /**
45
+ * The hard ceiling on {@link XPathLimits.maxNestingDepth}, deep enough for any
46
+ * hand-written expression; nothing recurses on the native stack in proportion
47
+ * to it.
48
+ */
49
+ export declare const XPATH_MAX_NESTING_DEPTH = 1024;
50
+ /**
51
+ * Defaults, chosen so no hand-written expression over a real page reaches them:
52
+ * 16 Ki characters and 256 levels are two orders of magnitude above extractor
53
+ * expressions, 2²¹ work units take about 100 ms on a typical machine when spent
54
+ * on node visits (the dearest unit; spent on characters they take a tenth of
55
+ * that), and 2²⁴ characters is a string-value well above any page's text.
56
+ */
57
+ export declare const DEFAULT_XPATH_LIMITS: Required<XPathLimits>;
58
+ /** Which bound an {@link XPathLimitExceededError} reports. */
59
+ export type XPathLimitKind = "length" | "nestingDepth" | "steps" | "stringLength";
60
+ /**
61
+ * Thrown when an expression or its evaluation exceeds one of the
62
+ * {@link XPathLimits}.
63
+ */
64
+ export declare class XPathLimitExceededError extends Error {
65
+ readonly kind: XPathLimitKind;
66
+ readonly limit: number;
67
+ constructor(kind: XPathLimitKind, limit: number, options?: ErrorOptions);
68
+ }
69
+ /**
70
+ * Fills in the unset bounds from {@link DEFAULT_XPATH_LIMITS} and checks them.
71
+ *
72
+ * @throws {RangeError} When a bound is not a positive safe integer (or
73
+ * `Infinity` for `maxSteps`), or `maxNestingDepth` is above
74
+ * {@link XPATH_MAX_NESTING_DEPTH}.
75
+ */
76
+ export declare function xpathLimits(limits?: XPathLimits): Required<XPathLimits>;
77
+ //# sourceMappingURL=limits.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"limits.d.ts","sourceRoot":"","sources":["../src/limits.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,MAAM,MAAM,WAAW,GAAG;IACzB;;;;;;OAMG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAE5B;;;;OAIG;IACH,QAAQ,CAAC,eAAe,CAAC,EAAE,MAAM,CAAC;IAElC;;;;;;;OAOG;IACH,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAE3B;;;;;;OAMG;IACH,QAAQ,CAAC,eAAe,CAAC,EAAE,MAAM,CAAC;CAClC,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,uBAAuB,OAAO,CAAC;AAE5C;;;;;;GAMG;AACH,eAAO,MAAM,oBAAoB,EAAE,QAAQ,CAAC,WAAW,CAKtD,CAAC;AAEF,8DAA8D;AAC9D,MAAM,MAAM,cAAc,GACvB,QAAQ,GACR,cAAc,GACd,OAAO,GACP,cAAc,CAAC;AASlB;;;GAGG;AACH,qBAAa,uBAAwB,SAAQ,KAAK;IAEhD,QAAQ,CAAC,IAAI,EAAE,cAAc;IAC7B,QAAQ,CAAC,KAAK,EAAE,MAAM;IAFvB,YACU,IAAI,EAAE,cAAc,EACpB,KAAK,EAAE,MAAM,EACtB,OAAO,CAAC,EAAE,YAAY,EAItB;CACD;AAED;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,MAAM,CAAC,EAAE,WAAW,GAAG,QAAQ,CAAC,WAAW,CAAC,CAkCvE"}
package/dist/limits.js ADDED
@@ -0,0 +1,74 @@
1
+ /**
2
+ * The hard ceiling on {@link XPathLimits.maxNestingDepth}, deep enough for any
3
+ * hand-written expression; nothing recurses on the native stack in proportion
4
+ * to it.
5
+ */
6
+ export const XPATH_MAX_NESTING_DEPTH = 1024;
7
+ /**
8
+ * Defaults, chosen so no hand-written expression over a real page reaches them:
9
+ * 16 Ki characters and 256 levels are two orders of magnitude above extractor
10
+ * expressions, 2²¹ work units take about 100 ms on a typical machine when spent
11
+ * on node visits (the dearest unit; spent on characters they take a tenth of
12
+ * that), and 2²⁴ characters is a string-value well above any page's text.
13
+ */
14
+ export const DEFAULT_XPATH_LIMITS = {
15
+ maxLength: 16384,
16
+ maxNestingDepth: 256,
17
+ maxSteps: 2097152,
18
+ maxStringLength: 16777216,
19
+ };
20
+ const LIMIT_MESSAGES_ = {
21
+ length: "XPath expression is longer than {limit} characters",
22
+ nestingDepth: "XPath expression nests deeper than {limit}",
23
+ steps: "XPath evaluation exceeded {limit} work units",
24
+ stringLength: "XPath evaluation built a string longer than {limit}",
25
+ };
26
+ /**
27
+ * Thrown when an expression or its evaluation exceeds one of the
28
+ * {@link XPathLimits}.
29
+ */
30
+ export class XPathLimitExceededError extends Error {
31
+ kind;
32
+ limit;
33
+ constructor(kind, limit, options) {
34
+ super(LIMIT_MESSAGES_[kind].replace("{limit}", String(limit)), options);
35
+ this.kind = kind;
36
+ this.limit = limit;
37
+ this.name = "XPathLimitExceededError";
38
+ }
39
+ }
40
+ /**
41
+ * Fills in the unset bounds from {@link DEFAULT_XPATH_LIMITS} and checks them.
42
+ *
43
+ * @throws {RangeError} When a bound is not a positive safe integer (or
44
+ * `Infinity` for `maxSteps`), or `maxNestingDepth` is above
45
+ * {@link XPATH_MAX_NESTING_DEPTH}.
46
+ */
47
+ export function xpathLimits(limits) {
48
+ const resolved = {
49
+ maxLength: limits?.maxLength ?? DEFAULT_XPATH_LIMITS.maxLength,
50
+ maxNestingDepth: limits?.maxNestingDepth ?? DEFAULT_XPATH_LIMITS.maxNestingDepth,
51
+ maxSteps: limits?.maxSteps ?? DEFAULT_XPATH_LIMITS.maxSteps,
52
+ maxStringLength: limits?.maxStringLength ?? DEFAULT_XPATH_LIMITS.maxStringLength,
53
+ };
54
+ for (const name of [
55
+ "maxLength",
56
+ "maxNestingDepth",
57
+ "maxStringLength",
58
+ ]) {
59
+ const value = resolved[name];
60
+ if (!Number.isSafeInteger(value) || value < 1) {
61
+ throw new RangeError(`${name} must be a positive safe integer`);
62
+ }
63
+ }
64
+ if (resolved.maxNestingDepth > XPATH_MAX_NESTING_DEPTH) {
65
+ throw new RangeError(`maxNestingDepth must not exceed ${XPATH_MAX_NESTING_DEPTH}`);
66
+ }
67
+ const { maxSteps } = resolved;
68
+ if (maxSteps !== Infinity &&
69
+ !(Number.isSafeInteger(maxSteps) && maxSteps > 0)) {
70
+ throw new RangeError("maxSteps must be a positive safe integer or Infinity");
71
+ }
72
+ return resolved;
73
+ }
74
+ //# sourceMappingURL=limits.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"limits.js","sourceRoot":"","sources":["../src/limits.ts"],"names":[],"mappings":"AA+CA;;;;GAIG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,IAAI,CAAC;AAE5C;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAA0B;IAC1D,SAAS,EAAE,KAAM;IACjB,eAAe,EAAE,GAAG;IACpB,QAAQ,EAAE,OAAS;IACnB,eAAe,EAAE,QAAU;CAC3B,CAAC;AASF,MAAM,eAAe,GAA6C;IACjE,MAAM,EAAE,oDAAoD;IAC5D,YAAY,EAAE,4CAA4C;IAC1D,KAAK,EAAE,8CAA8C;IACrD,YAAY,EAAE,qDAAqD;CACnE,CAAC;AAEF;;;GAGG;AACH,MAAM,OAAO,uBAAwB,SAAQ,KAAK;IAEvC,IAAI;IACJ,KAAK;IAFf,YACU,IAAoB,EACpB,KAAa,EACtB,OAAsB;QAEtB,KAAK,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,SAAS,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC;oBAJ/D,IAAI;qBACJ,KAAK;QAId,IAAI,CAAC,IAAI,GAAG,yBAAyB,CAAC;IACvC,CAAC;CACD;AAED;;;;;;GAMG;AACH,MAAM,UAAU,WAAW,CAAC,MAAoB;IAC/C,MAAM,QAAQ,GAA0B;QACvC,SAAS,EAAE,MAAM,EAAE,SAAS,IAAI,oBAAoB,CAAC,SAAS;QAC9D,eAAe,EACd,MAAM,EAAE,eAAe,IAAI,oBAAoB,CAAC,eAAe;QAChE,QAAQ,EAAE,MAAM,EAAE,QAAQ,IAAI,oBAAoB,CAAC,QAAQ;QAC3D,eAAe,EACd,MAAM,EAAE,eAAe,IAAI,oBAAoB,CAAC,eAAe;KAChE,CAAC;IACF,KAAK,MAAM,IAAI,IAAI;QAClB,WAAW;QACX,iBAAiB;QACjB,iBAAiB;KACR,EAAE,CAAC;QACZ,MAAM,KAAK,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAC;QAC7B,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;YAC/C,MAAM,IAAI,UAAU,CAAC,GAAG,IAAI,kCAAkC,CAAC,CAAC;QACjE,CAAC;IACF,CAAC;IACD,IAAI,QAAQ,CAAC,eAAe,GAAG,uBAAuB,EAAE,CAAC;QACxD,MAAM,IAAI,UAAU,CACnB,mCAAmC,uBAAuB,EAAE,CAC5D,CAAC;IACH,CAAC;IACD,MAAM,EAAE,QAAQ,EAAE,GAAG,QAAQ,CAAC;IAC9B,IACC,QAAQ,KAAK,QAAQ;QACrB,CAAC,CAAC,MAAM,CAAC,aAAa,CAAC,QAAQ,CAAC,IAAI,QAAQ,GAAG,CAAC,CAAC,EAChD,CAAC;QACF,MAAM,IAAI,UAAU,CACnB,sDAAsD,CACtD,CAAC;IACH,CAAC;IACD,OAAO,QAAQ,CAAC;AACjB,CAAC"}
@@ -0,0 +1,27 @@
1
+ import type { XPathExpression } from "./ast.js";
2
+ import { type XPathLimits } from "./limits.js";
3
+ /**
4
+ * Parses an XPath 1.0 expression into its syntax tree, abbreviations expanded
5
+ * and every node carrying its span.
6
+ *
7
+ * Follows the lexical rules of XPath 1.0 §3.7: only space, tab, carriage return
8
+ * and line feed are whitespace, `*` and `and`, `or`, `div`, `mod` are operators
9
+ * after an operand, and a name before `(` is a function or node type. Names are
10
+ * not checked here — an unknown function or prefix is a compile error — so any
11
+ * well-formed expression parses. O(length); iterative, its nesting on an
12
+ * explicit stack and bounded by `maxNestingDepth`.
13
+ *
14
+ * @throws {XPathSyntaxError} When `source` is not an XPath 1.0 expression.
15
+ * @throws {XPathLimitExceededError} Of kind `"length"` or `"nestingDepth"`.
16
+ * @throws {RangeError} When `limits` holds an invalid bound.
17
+ */
18
+ export declare function parseXPath(source: string, limits?: XPathLimits): XPathExpression;
19
+ /**
20
+ * {@link parseXPath}, answering `null` instead of throwing an
21
+ * {@link XPathSyntaxError}. Limits still throw.
22
+ *
23
+ * @throws {XPathLimitExceededError}
24
+ * @throws {RangeError} When `limits` holds an invalid bound.
25
+ */
26
+ export declare function tryParseXPath(source: string, limits?: XPathLimits): XPathExpression | null;
27
+ //# sourceMappingURL=parse-xpath.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse-xpath.d.ts","sourceRoot":"","sources":["../src/parse-xpath.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAEhD,OAAO,EAAE,KAAK,WAAW,EAAe,MAAM,aAAa,CAAC;AAE5D;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,UAAU,CACzB,MAAM,EAAE,MAAM,EACd,MAAM,CAAC,EAAE,WAAW,GAClB,eAAe,CAGjB;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,CAC5B,MAAM,EAAE,MAAM,EACd,MAAM,CAAC,EAAE,WAAW,GAClB,eAAe,GAAG,IAAI,CASxB"}
@@ -0,0 +1,41 @@
1
+ import { parseBounded } from "./_parse-bounded.js";
2
+ import { XPathSyntaxError } from "./errors.js";
3
+ import { xpathLimits } from "./limits.js";
4
+ /**
5
+ * Parses an XPath 1.0 expression into its syntax tree, abbreviations expanded
6
+ * and every node carrying its span.
7
+ *
8
+ * Follows the lexical rules of XPath 1.0 §3.7: only space, tab, carriage return
9
+ * and line feed are whitespace, `*` and `and`, `or`, `div`, `mod` are operators
10
+ * after an operand, and a name before `(` is a function or node type. Names are
11
+ * not checked here — an unknown function or prefix is a compile error — so any
12
+ * well-formed expression parses. O(length); iterative, its nesting on an
13
+ * explicit stack and bounded by `maxNestingDepth`.
14
+ *
15
+ * @throws {XPathSyntaxError} When `source` is not an XPath 1.0 expression.
16
+ * @throws {XPathLimitExceededError} Of kind `"length"` or `"nestingDepth"`.
17
+ * @throws {RangeError} When `limits` holds an invalid bound.
18
+ */
19
+ export function parseXPath(source, limits) {
20
+ const { maxLength, maxNestingDepth } = xpathLimits(limits);
21
+ return parseBounded(source, maxLength, maxNestingDepth);
22
+ }
23
+ /**
24
+ * {@link parseXPath}, answering `null` instead of throwing an
25
+ * {@link XPathSyntaxError}. Limits still throw.
26
+ *
27
+ * @throws {XPathLimitExceededError}
28
+ * @throws {RangeError} When `limits` holds an invalid bound.
29
+ */
30
+ export function tryParseXPath(source, limits) {
31
+ try {
32
+ return parseXPath(source, limits);
33
+ }
34
+ catch (error) {
35
+ if (error instanceof XPathSyntaxError) {
36
+ return null;
37
+ }
38
+ throw error;
39
+ }
40
+ }
41
+ //# sourceMappingURL=parse-xpath.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse-xpath.js","sourceRoot":"","sources":["../src/parse-xpath.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAEnD,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,EAAoB,WAAW,EAAE,MAAM,aAAa,CAAC;AAE5D;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,UAAU,CACzB,MAAc,EACd,MAAoB;IAEpB,MAAM,EAAE,SAAS,EAAE,eAAe,EAAE,GAAG,WAAW,CAAC,MAAM,CAAC,CAAC;IAC3D,OAAO,YAAY,CAAC,MAAM,EAAE,SAAS,EAAE,eAAe,CAAC,CAAC;AACzD,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,aAAa,CAC5B,MAAc,EACd,MAAoB;IAEpB,IAAI,CAAC;QACJ,OAAO,UAAU,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACnC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QAChB,IAAI,KAAK,YAAY,gBAAgB,EAAE,CAAC;YACvC,OAAO,IAAI,CAAC;QACb,CAAC;QACD,MAAM,KAAK,CAAC;IACb,CAAC;AACF,CAAC"}
@@ -0,0 +1,68 @@
1
+ /**
2
+ * The key marking the attribute and namespace nodes this package creates, so
3
+ * they are told apart from a tree's own nodes whatever those look like. Read it
4
+ * through {@link isXPathAttributeNode} and {@link isXPathNamespaceNode}.
5
+ */
6
+ export declare const XPATH_NODE_KIND: unique symbol;
7
+ /**
8
+ * An attribute as an XPath node: a shape's attributes have no parent, so the
9
+ * node carries its owner element.
10
+ *
11
+ * Two attribute nodes are the same node when their `owner` and `attribute` are,
12
+ * whichever objects hold them: an evaluation deduplicates and orders them by
13
+ * that pair, and returns one object per pair.
14
+ */
15
+ export type XPathAttributeNode<TNode, TAttribute> = {
16
+ readonly [XPATH_NODE_KIND]: "attribute";
17
+ readonly owner: TNode;
18
+ readonly attribute: TAttribute;
19
+ };
20
+ /**
21
+ * A namespace binding in scope at an element, as an XPath node; `prefix` is
22
+ * `null` for the default namespace. Identified by `owner` and `prefix`.
23
+ */
24
+ export type XPathNamespaceNode<TNode> = {
25
+ readonly [XPATH_NODE_KIND]: "namespace";
26
+ readonly owner: TNode;
27
+ readonly prefix: string | null;
28
+ readonly uri: string;
29
+ };
30
+ /**
31
+ * Any XPath node: a tree node, an attribute or a namespace.
32
+ *
33
+ * A tree node is never a doctype, and a text node always stands for its whole
34
+ * run of adjacent text siblings — the first node of the run, as
35
+ * `markupTextRunStart` (`@ac-kit/model-markup`) returns it.
36
+ */
37
+ export type XPathNode<TNode, TAttribute> = TNode | XPathAttributeNode<TNode, TAttribute> | XPathNamespaceNode<TNode>;
38
+ /** The value of an expression: one of the four XPath 1.0 types. */
39
+ export type XPathValue<TNode, TAttribute> =
40
+ /** In document order, without duplicates. */
41
+ {
42
+ readonly type: "nodeSet";
43
+ readonly nodes: readonly XPathNode<TNode, TAttribute>[];
44
+ } | {
45
+ readonly type: "string";
46
+ readonly value: string;
47
+ } | {
48
+ readonly type: "number";
49
+ readonly value: number;
50
+ } | {
51
+ readonly type: "boolean";
52
+ readonly value: boolean;
53
+ };
54
+ /**
55
+ * The value a variable is bound to. An array is a node-set: given in any order
56
+ * and with duplicates, it is put in document order and deduplicated, and a text
57
+ * node in it stands for its whole run.
58
+ */
59
+ export type XPathVariableValue<TNode, TAttribute> = string | number | boolean | readonly XPathNode<TNode, TAttribute>[];
60
+ /** An attribute node for `attribute` of `owner`, e.g. to start an evaluation at. */
61
+ export declare function createXPathAttributeNode<TNode, TAttribute>(owner: TNode, attribute: TAttribute): XPathAttributeNode<TNode, TAttribute>;
62
+ /** A namespace node binding `prefix` (`null` for the default) at `owner`. */
63
+ export declare function createXPathNamespaceNode<TNode>(owner: TNode, prefix: string | null, uri: string): XPathNamespaceNode<TNode>;
64
+ /** Whether `node` is an attribute node rather than a tree or namespace node. */
65
+ export declare function isXPathAttributeNode<TNode, TAttribute>(node: XPathNode<TNode, TAttribute>): node is XPathAttributeNode<TNode, TAttribute>;
66
+ /** Whether `node` is a namespace node rather than a tree or attribute node. */
67
+ export declare function isXPathNamespaceNode<TNode, TAttribute>(node: XPathNode<TNode, TAttribute>): node is XPathNamespaceNode<TNode>;
68
+ //# sourceMappingURL=values.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"values.d.ts","sourceRoot":"","sources":["../src/values.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,eAAO,MAAM,eAAe,EAAE,OAAO,MAAgC,CAAC;AAEtE;;;;;;;GAOG;AACH,MAAM,MAAM,kBAAkB,CAAC,KAAK,EAAE,UAAU,IAAI;IACnD,QAAQ,CAAC,CAAC,eAAe,CAAC,EAAE,WAAW,CAAC;IACxC,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC;IACtB,QAAQ,CAAC,SAAS,EAAE,UAAU,CAAC;CAC/B,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,kBAAkB,CAAC,KAAK,IAAI;IACvC,QAAQ,CAAC,CAAC,eAAe,CAAC,EAAE,WAAW,CAAC;IACxC,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC;IACtB,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IAC/B,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,MAAM,SAAS,CAAC,KAAK,EAAE,UAAU,IACpC,KAAK,GACL,kBAAkB,CAAC,KAAK,EAAE,UAAU,CAAC,GACrC,kBAAkB,CAAC,KAAK,CAAC,CAAC;AAE7B,mEAAmE;AACnE,MAAM,MAAM,UAAU,CAAC,KAAK,EAAE,UAAU;AACvC,6CAA6C;AAC3C;IACA,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC;IACzB,QAAQ,CAAC,KAAK,EAAE,SAAS,SAAS,CAAC,KAAK,EAAE,UAAU,CAAC,EAAE,CAAC;CACvD,GACD;IAAE,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GACnD;IAAE,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GACnD;IAAE,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAA;CAAE,CAAC;AAEzD;;;;GAIG;AACH,MAAM,MAAM,kBAAkB,CAAC,KAAK,EAAE,UAAU,IAC7C,MAAM,GACN,MAAM,GACN,OAAO,GACP,SAAS,SAAS,CAAC,KAAK,EAAE,UAAU,CAAC,EAAE,CAAC;AAE3C,oFAAoF;AACpF,wBAAgB,wBAAwB,CAAC,KAAK,EAAE,UAAU,EACzD,KAAK,EAAE,KAAK,EACZ,SAAS,EAAE,UAAU,GACnB,kBAAkB,CAAC,KAAK,EAAE,UAAU,CAAC,CAEvC;AAED,6EAA6E;AAC7E,wBAAgB,wBAAwB,CAAC,KAAK,EAC7C,KAAK,EAAE,KAAK,EACZ,MAAM,EAAE,MAAM,GAAG,IAAI,EACrB,GAAG,EAAE,MAAM,GACT,kBAAkB,CAAC,KAAK,CAAC,CAE3B;AAED,gFAAgF;AAChF,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,UAAU,EACrD,IAAI,EAAE,SAAS,CAAC,KAAK,EAAE,UAAU,CAAC,GAChC,IAAI,IAAI,kBAAkB,CAAC,KAAK,EAAE,UAAU,CAAC,CAE/C;AAED,+EAA+E;AAC/E,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,UAAU,EACrD,IAAI,EAAE,SAAS,CAAC,KAAK,EAAE,UAAU,CAAC,GAChC,IAAI,IAAI,kBAAkB,CAAC,KAAK,CAAC,CAEnC"}
package/dist/values.js ADDED
@@ -0,0 +1,28 @@
1
+ /**
2
+ * The key marking the attribute and namespace nodes this package creates, so
3
+ * they are told apart from a tree's own nodes whatever those look like. Read it
4
+ * through {@link isXPathAttributeNode} and {@link isXPathNamespaceNode}.
5
+ */
6
+ export const XPATH_NODE_KIND = Symbol("XPathNodeKind");
7
+ /** An attribute node for `attribute` of `owner`, e.g. to start an evaluation at. */
8
+ export function createXPathAttributeNode(owner, attribute) {
9
+ return { [XPATH_NODE_KIND]: "attribute", owner, attribute };
10
+ }
11
+ /** A namespace node binding `prefix` (`null` for the default) at `owner`. */
12
+ export function createXPathNamespaceNode(owner, prefix, uri) {
13
+ return { [XPATH_NODE_KIND]: "namespace", owner, prefix, uri };
14
+ }
15
+ /** Whether `node` is an attribute node rather than a tree or namespace node. */
16
+ export function isXPathAttributeNode(node) {
17
+ return kindOfWrapper(node) === "attribute";
18
+ }
19
+ /** Whether `node` is a namespace node rather than a tree or attribute node. */
20
+ export function isXPathNamespaceNode(node) {
21
+ return kindOfWrapper(node) === "namespace";
22
+ }
23
+ function kindOfWrapper(node) {
24
+ return typeof node === "object" && node !== null && XPATH_NODE_KIND in node
25
+ ? node[XPATH_NODE_KIND]
26
+ : undefined;
27
+ }
28
+ //# sourceMappingURL=values.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"values.js","sourceRoot":"","sources":["../src/values.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,CAAC,MAAM,eAAe,GAAkB,MAAM,CAAC,eAAe,CAAC,CAAC;AA6DtE,oFAAoF;AACpF,MAAM,UAAU,wBAAwB,CACvC,KAAY,EACZ,SAAqB;IAErB,OAAO,EAAE,CAAC,eAAe,CAAC,EAAE,WAAW,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC;AAC7D,CAAC;AAED,6EAA6E;AAC7E,MAAM,UAAU,wBAAwB,CACvC,KAAY,EACZ,MAAqB,EACrB,GAAW;IAEX,OAAO,EAAE,CAAC,eAAe,CAAC,EAAE,WAAW,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC;AAC/D,CAAC;AAED,gFAAgF;AAChF,MAAM,UAAU,oBAAoB,CACnC,IAAkC;IAElC,OAAO,aAAa,CAAC,IAAI,CAAC,KAAK,WAAW,CAAC;AAC5C,CAAC;AAED,+EAA+E;AAC/E,MAAM,UAAU,oBAAoB,CACnC,IAAkC;IAElC,OAAO,aAAa,CAAC,IAAI,CAAC,KAAK,WAAW,CAAC;AAC5C,CAAC;AAED,SAAS,aAAa,CAAC,IAAa;IACnC,OAAO,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,KAAK,IAAI,IAAI,eAAe,IAAI,IAAI;QAC1E,CAAC,CAAE,IAAgD,CAAC,eAAe,CAAC;QACpE,CAAC,CAAC,SAAS,CAAC;AACd,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,71 @@
1
1
  {
2
2
  "name": "@ac-kit/format-xpath",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.1",
4
+ "description": "XPath 1.0: parse and evaluate over any markup tree shape.",
5
+ "keywords": [
6
+ "html",
7
+ "query",
8
+ "xml",
9
+ "xpath"
10
+ ],
11
+ "homepage": "https://anthochamp.pages.grenadine.io/node-essentials/api/format-xpath/",
12
+ "bugs": {
13
+ "url": "https://git.grenadine.io/anthochamp/node-essentials/issues"
14
+ },
15
+ "license": "BSD-3-Clause",
16
+ "author": "Anthony Champagne <dev@anthonychampagne.fr>",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://git.grenadine.io/anthochamp/node-essentials.git",
20
+ "directory": "packages/format-xpath"
21
+ },
22
+ "files": [
23
+ "dist"
24
+ ],
25
+ "type": "module",
26
+ "sideEffects": false,
27
+ "main": "./dist/index.js",
28
+ "exports": {
29
+ ".": "./dist/index.js"
30
+ },
31
+ "publishConfig": {
32
+ "main": "./dist/index.js",
33
+ "exports": {
34
+ ".": "./dist/index.js"
35
+ },
36
+ "access": "public"
37
+ },
38
+ "scripts": {
39
+ "compile": "tsc -p ./tsconfig.build.json -outDir dist",
40
+ "compile:check": "tsc -noEmit -p ./tsconfig.json",
41
+ "clean": "rimraf dist",
42
+ "build": "yarn clean && yarn compile",
43
+ "test": "yarn run -T vitest run",
44
+ "prepack": "yarn build"
45
+ },
46
+ "dependencies": {
47
+ "@ac-kit/algo": "^0.4.1",
48
+ "@ac-kit/core": "^0.11.0",
49
+ "@ac-kit/format-xml": "^0.2.1",
50
+ "@ac-kit/math-numbers": "^0.2.8",
51
+ "@ac-kit/model-markup": "^0.1.1",
52
+ "@types/web": "^0.0.367"
53
+ },
54
+ "devDependencies": {
55
+ "@ac-kit/math-random": "0.3.0",
56
+ "@ac-kit/test-util": "0.3.1",
57
+ "@ac-kit/tsconfig": "0.1.0",
58
+ "@ac-kit/typedoc-config": "0.1.0",
59
+ "@ac-kit/vitest-config": "0.1.1",
60
+ "@types/node": "26.6.6",
61
+ "@typescript/native": "typescript@7.0.2",
62
+ "parse5": "8.1.1",
63
+ "rimraf": "6.1.4",
64
+ "tsx": "4.23.15",
65
+ "typedoc": "0.28.20",
66
+ "typescript": "@typescript/typescript6@6.0.2"
67
+ },
68
+ "engines": {
69
+ "node": ">=24"
70
+ }
6
71
  }