fxp.d.cts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. /**
  2. * Types copied from path-expression-matcher
  3. * @version <version>
  4. * @updated <date>
  5. *
  6. * Update this file when path-expression-matcher releases a new version.
  7. * Source: https://github.com/NaturalIntelligence/path-expression-matcher
  8. */
  9. /**
  10. * Options for creating an Expression
  11. */
  12. interface ExpressionOptions {
  13. /**
  14. * Path separator character
  15. * @default '.'
  16. */
  17. separator?: string;
  18. }
  19. /**
  20. * Parsed segment from an expression pattern
  21. */
  22. interface Segment {
  23. type: 'tag' | 'deep-wildcard';
  24. tag?: string;
  25. namespace?: string;
  26. attrName?: string;
  27. attrValue?: string;
  28. position?: 'first' | 'last' | 'odd' | 'even' | 'nth';
  29. positionValue?: number;
  30. }
  31. /**
  32. * Expression - Parses and stores a tag pattern expression.
  33. * Patterns are parsed once and stored in an optimized structure for fast matching.
  34. *
  35. * @example
  36. * ```typescript
  37. * const expr = new Expression("root.users.user");
  38. * const expr2 = new Expression("..user[id]:first");
  39. * const expr3 = new Expression("root/users/user", { separator: '/' });
  40. * ```
  41. *
  42. * Pattern Syntax:
  43. * - `root.users.user` — Match exact path
  44. * - `..user` — Match "user" at any depth (deep wildcard)
  45. * - `user[id]` — Match user tag with "id" attribute
  46. * - `user[id=123]` — Match user tag where id="123"
  47. * - `user:first` — Match first occurrence of user tag
  48. * - `ns::user` — Match user tag with namespace "ns"
  49. * - `ns::user[id]:first` — Combine namespace, attribute, and position
  50. */
  51. declare class Expression {
  52. readonly pattern: string;
  53. readonly separator: string;
  54. readonly segments: Segment[];
  55. constructor(pattern: string, options?: ExpressionOptions);
  56. get length(): number;
  57. hasDeepWildcard(): boolean;
  58. hasAttributeCondition(): boolean;
  59. hasPositionSelector(): boolean;
  60. toString(): string;
  61. }
  62. // ---------------------------------------------------------------------------
  63. // MatcherView
  64. // ---------------------------------------------------------------------------
  65. /**
  66. * A lightweight, live read-only view of a Matcher instance.
  67. *
  68. * Returned by `Matcher.readOnly()`. The same instance is reused across every
  69. * callback invocation — no allocation overhead per call. Reads directly from
  70. * the parent Matcher's internal state so it always reflects the current parser
  71. * position with no copying or freezing.
  72. *
  73. * Mutation methods (`push`, `pop`, `reset`, `updateCurrent`, `restore`) are
  74. * simply absent — misuse is caught at compile time by TypeScript.
  75. *
  76. * This is the type received by all FXP user callbacks when `jPath: false`.
  77. */
  78. interface MatcherView {
  79. readonly separator: string;
  80. /** Check if current path matches an Expression. */
  81. matches(expression: Expression): boolean;
  82. /** Get current tag name, or `undefined` if path is empty. */
  83. getCurrentTag(): string | undefined;
  84. /** Get current namespace, or `undefined` if not present. */
  85. getCurrentNamespace(): string | undefined;
  86. /** Get attribute value of the current node. */
  87. getAttrValue(attrName: string): any;
  88. /** Check if the current node has a given attribute. */
  89. hasAttr(attrName: string): boolean;
  90. /** Sibling position of the current node (child index in parent). */
  91. getPosition(): number;
  92. /** Occurrence counter of the current tag name at this level. */
  93. getCounter(): number;
  94. /** Number of nodes in the current path. */
  95. getDepth(): number;
  96. /** Current path as a string (e.g. `"root.users.user"`). */
  97. toString(separator?: string, includeNamespace?: boolean): string;
  98. /** Current path as an array of tag names. */
  99. toArray(): string[];
  100. /**
  101. * Create a snapshot of the current state.
  102. * The snapshot can be passed to the real Matcher.restore() if needed.
  103. */
  104. snapshot(): MatcherSnapshot;
  105. }
  106. /**
  107. * @deprecated Use {@link MatcherView} instead.
  108. * Alias kept for backward compatibility.
  109. */
  110. type ReadonlyMatcher = MatcherView;
  111. /** Internal node structure — exposed via snapshot only. */
  112. interface PathNode {
  113. tag: string;
  114. namespace?: string;
  115. position: number;
  116. counter: number;
  117. values?: Record<string, any>;
  118. }
  119. /** Snapshot of matcher state returned by `snapshot()` and `readOnly().snapshot()`. */
  120. interface MatcherSnapshot {
  121. path: PathNode[];
  122. siblingStacks: Map<string, number>[];
  123. }
  124. /**********************************************************************
  125. *
  126. * END of path-expression-matcher relevant typings
  127. *
  128. **********************************************************************/
  129. // jPath: true → string
  130. // jPath: false → MatcherView
  131. type JPathOrMatcher = string | MatcherView;
  132. type JPathOrExpression = string | Expression;
  133. type ProcessEntitiesOptions = {
  134. /**
  135. * Whether to enable entity processing
  136. *
  137. * Defaults to `true`
  138. */
  139. enabled?: boolean;
  140. /**
  141. * Maximum size in characters for a single entity definition
  142. *
  143. * Defaults to `10000`
  144. */
  145. maxEntitySize?: number;
  146. /**
  147. * Maximum depth for nested entity references (reserved for future use)
  148. *
  149. * Defaults to `10`
  150. */
  151. maxExpansionDepth?: number;
  152. /**
  153. * Maximum total number of entity expansions allowed
  154. *
  155. * Defaults to `1000`
  156. */
  157. maxTotalExpansions?: number;
  158. /**
  159. * Maximum total expanded content length in characters
  160. *
  161. * Defaults to `100000`
  162. */
  163. maxExpandedLength?: number;
  164. /**
  165. * Maximum number of entities allowed in the XML
  166. *
  167. * Defaults to `100`
  168. */
  169. maxEntityCount?: number;
  170. /**
  171. * Array of tag names where entity replacement is allowed.
  172. * If null, entities are replaced in all tags.
  173. *
  174. * Defaults to `null`
  175. */
  176. allowedTags?: string[] | null;
  177. /**
  178. * Custom filter function to determine if entities should be replaced in a tag
  179. *
  180. * @param tagName - The name of the current tag
  181. * @param jPathOrMatcher - The jPath string (if jPath: true) or Matcher instance (if jPath: false)
  182. * @returns `true` to allow entity replacement, `false` to skip
  183. *
  184. * Defaults to `null`
  185. */
  186. tagFilter?: ((tagName: string, jPathOrMatcher: JPathOrMatcher) => boolean) | null;
  187. };
  188. type EntityDecoderOptions = {
  189. setExternalEntities: (entities: Record<string, string>) => void;
  190. addInputEntities: (entities: Record<string, string>) => void;
  191. reset: () => void;
  192. decode: (text: string) => string;
  193. setXmlVersion: (version: string) => void;
  194. }
  195. type X2jOptions = {
  196. /**
  197. * Preserve the order of tags in resulting JS object
  198. *
  199. * Defaults to `false`
  200. */
  201. preserveOrder?: boolean;
  202. /**
  203. * Give a prefix to the attribute name in the resulting JS object
  204. *
  205. * Defaults to '@_'
  206. */
  207. attributeNamePrefix?: string;
  208. /**
  209. * A name to group all attributes of a tag under, or `false` to disable
  210. *
  211. * Defaults to `false`
  212. */
  213. attributesGroupName?: false | string;
  214. /**
  215. * The name of the next node in the resulting JS
  216. *
  217. * Defaults to `#text`
  218. */
  219. textNodeName?: string;
  220. /**
  221. * Whether to ignore attributes when parsing
  222. *
  223. * When `true` - ignores all the attributes
  224. *
  225. * When `false` - parses all the attributes
  226. *
  227. * When `Array<string | RegExp>` - filters out attributes that match provided patterns
  228. *
  229. * When `Function` - calls the function for each attribute and filters out those for which the function returned `true`
  230. *
  231. * Defaults to `true`
  232. */
  233. ignoreAttributes?: boolean | (string | RegExp)[] | ((attrName: string, jPathOrMatcher: JPathOrMatcher) => boolean);
  234. /**
  235. * Whether to remove namespace string from tag and attribute names
  236. *
  237. * Defaults to `false`
  238. */
  239. removeNSPrefix?: boolean;
  240. /**
  241. * Whether to allow attributes without value
  242. *
  243. * Defaults to `false`
  244. */
  245. allowBooleanAttributes?: boolean;
  246. /**
  247. * Whether to parse tag value with `strnum` package
  248. *
  249. * Defaults to `true`
  250. */
  251. parseTagValue?: boolean;
  252. /**
  253. * Whether to parse attribute value with `strnum` package
  254. *
  255. * Defaults to `false`
  256. */
  257. parseAttributeValue?: boolean;
  258. /**
  259. * Whether to remove surrounding whitespace from tag or attribute value
  260. *
  261. * Defaults to `true`
  262. */
  263. trimValues?: boolean;
  264. /**
  265. * Give a property name to set CDATA values to instead of merging to tag's text value
  266. *
  267. * Defaults to `false`
  268. */
  269. cdataPropName?: false | string;
  270. /**
  271. * If set, parse comments and set as this property
  272. *
  273. * Defaults to `false`
  274. */
  275. commentPropName?: false | string;
  276. /**
  277. * Control how tag value should be parsed. Called only if tag value is not empty
  278. *
  279. * @param tagName - The name of the tag
  280. * @param tagValue - The value of the tag
  281. * @param jPathOrMatcher - The jPath string (if jPath: true) or Matcher instance (if jPath: false)
  282. * @param hasAttributes - Whether the tag has attributes
  283. * @param isLeafNode - Whether the tag is a leaf node
  284. * @returns {undefined|null} `undefined` or `null` to set original value.
  285. * @returns {unknown}
  286. *
  287. * 1. Different value or value with different data type to set new value.
  288. * 2. Same value to set parsed value if `parseTagValue: true`.
  289. *
  290. * Defaults to `(tagName, val, jPathOrMatcher, hasAttributes, isLeafNode) => val`
  291. */
  292. tagValueProcessor?: (tagName: string, tagValue: string, jPathOrMatcher: JPathOrMatcher, hasAttributes: boolean, isLeafNode: boolean) => unknown;
  293. /**
  294. * Control how attribute value should be parsed
  295. *
  296. * @param attrName - The name of the attribute
  297. * @param attrValue - The value of the attribute
  298. * @param jPathOrMatcher - The jPath string (if jPath: true) or Matcher instance (if jPath: false)
  299. * @returns {undefined|null} `undefined` or `null` to set original value
  300. * @returns {unknown}
  301. *
  302. * Defaults to `(attrName, val, jPathOrMatcher) => val`
  303. */
  304. attributeValueProcessor?: (attrName: string, attrValue: string, jPathOrMatcher: JPathOrMatcher) => unknown;
  305. /**
  306. * Options to pass to `strnum` for parsing numbers
  307. *
  308. * Defaults to `{ hex: true, leadingZeros: true, eNotation: true }`
  309. */
  310. numberParseOptions?: strnumOptions;
  311. /**
  312. * Nodes to stop parsing at
  313. *
  314. * Accepts string patterns or Expression objects from path-expression-matcher
  315. *
  316. * String patterns starting with "*." are automatically converted to ".." for backward compatibility
  317. *
  318. * Defaults to `[]`
  319. */
  320. stopNodes?: JPathOrExpression[];
  321. /**
  322. * List of tags without closing tags
  323. *
  324. * Defaults to `[]`
  325. */
  326. unpairedTags?: string[];
  327. /**
  328. * Whether to always create a text node
  329. *
  330. * Defaults to `false`
  331. */
  332. alwaysCreateTextNode?: boolean;
  333. /**
  334. * Determine whether a tag should be parsed as an array
  335. *
  336. * @param tagName - The name of the tag
  337. * @param jPathOrMatcher - The jPath string (if jPath: true) or Matcher instance (if jPath: false)
  338. * @param isLeafNode - Whether the tag is a leaf node
  339. * @param isAttribute - Whether this is an attribute
  340. * @returns {boolean}
  341. *
  342. * Defaults to `() => false`
  343. */
  344. isArray?: (tagName: string, jPathOrMatcher: JPathOrMatcher, isLeafNode: boolean, isAttribute: boolean) => boolean;
  345. /**
  346. * Whether to process default and DOCTYPE entities
  347. *
  348. * When `true` - enables entity processing with default limits
  349. *
  350. * When `false` - disables all entity processing
  351. *
  352. * When `ProcessEntitiesOptions` - enables entity processing with custom configuration
  353. *
  354. * Defaults to `true`
  355. */
  356. processEntities?: boolean | ProcessEntitiesOptions;
  357. /**
  358. * Whether to process HTML entities
  359. *
  360. * Defaults to `false`
  361. * @deprecated Use `entityDecoder` instead
  362. */
  363. htmlEntities?: boolean;
  364. /**
  365. * Custom entity decoder
  366. */
  367. entityDecoder?: EntityDecoderOptions;
  368. /**
  369. * Whether to ignore the declaration tag from output
  370. *
  371. * Defaults to `false`
  372. */
  373. ignoreDeclaration?: boolean;
  374. /**
  375. * Whether to ignore Pi tags
  376. *
  377. * Defaults to `false`
  378. */
  379. ignorePiTags?: boolean;
  380. /**
  381. * Transform tag names
  382. *
  383. * Defaults to `false`
  384. */
  385. transformTagName?: ((tagName: string) => string) | false;
  386. /**
  387. * Transform attribute names
  388. *
  389. * Defaults to `false`
  390. */
  391. transformAttributeName?: ((attributeName: string) => string) | false;
  392. /**
  393. * Change the tag name when a different name is returned. Skip the tag from parsed result when false is returned.
  394. * Modify `attrs` object to control attributes for the given tag.
  395. *
  396. * @param tagName - The name of the tag
  397. * @param jPathOrMatcher - The jPath string (if jPath: true) or Matcher instance (if jPath: false)
  398. * @param attrs - The attributes object
  399. * @returns {string} new tag name.
  400. * @returns false to skip the tag
  401. *
  402. * Defaults to `(tagName, jPathOrMatcher, attrs) => tagName`
  403. */
  404. updateTag?: (tagName: string, jPathOrMatcher: JPathOrMatcher, attrs: { [k: string]: string }) => string | boolean;
  405. /**
  406. * If true, adds a Symbol to all object nodes, accessible by {@link XMLParser.getMetaDataSymbol} with
  407. * metadata about each the node in the XML file.
  408. */
  409. captureMetaData?: boolean;
  410. /**
  411. * Maximum number of nested tags
  412. *
  413. * Defaults to `100`
  414. */
  415. maxNestedTags?: number;
  416. /**
  417. * Whether to strictly validate tag names
  418. *
  419. * Defaults to `true`
  420. */
  421. strictReservedNames?: boolean;
  422. /**
  423. * Controls whether callbacks receive jPath as string or Matcher instance
  424. *
  425. * When `true` - callbacks receive jPath as string (backward compatible)
  426. *
  427. * When `false` - callbacks receive Matcher instance for advanced pattern matching
  428. *
  429. * Defaults to `true`
  430. */
  431. jPath?: boolean;
  432. /**
  433. * Function to sanitize dangerous property names
  434. *
  435. * @param name - The name of the property
  436. * @returns {string} The sanitized name
  437. *
  438. * Defaults to `(name) => __name`
  439. */
  440. onDangerousProperty?: (name: string) => string;
  441. };
  442. type strnumOptions = {
  443. hex: boolean;
  444. leadingZeros: boolean,
  445. skipLike?: RegExp,
  446. eNotation?: boolean
  447. }
  448. type validationOptions = {
  449. /**
  450. * Whether to allow attributes without value
  451. *
  452. * Defaults to `false`
  453. */
  454. allowBooleanAttributes?: boolean;
  455. /**
  456. * List of tags without closing tags
  457. *
  458. * Defaults to `[]`
  459. */
  460. unpairedTags?: string[];
  461. };
  462. type XmlBuilderOptions = {
  463. /**
  464. * Give a prefix to the attribute name in the resulting JS object
  465. *
  466. * Defaults to '@_'
  467. */
  468. attributeNamePrefix?: string;
  469. /**
  470. * A name to group all attributes of a tag under, or `false` to disable
  471. *
  472. * Defaults to `false`
  473. */
  474. attributesGroupName?: false | string;
  475. /**
  476. * The name of the next node in the resulting JS
  477. *
  478. * Defaults to `#text`
  479. */
  480. textNodeName?: string;
  481. /**
  482. * Whether to ignore attributes when building
  483. *
  484. * When `true` - ignores all the attributes
  485. *
  486. * When `false` - builds all the attributes
  487. *
  488. * When `Array<string | RegExp>` - filters out attributes that match provided patterns
  489. *
  490. * When `Function` - calls the function for each attribute and filters out those for which the function returned `true`
  491. *
  492. * Defaults to `true`
  493. */
  494. ignoreAttributes?: boolean | (string | RegExp)[] | ((attrName: string, jPath: string) => boolean);
  495. /**
  496. * Give a property name to set CDATA values to instead of merging to tag's text value
  497. *
  498. * Defaults to `false`
  499. */
  500. cdataPropName?: false | string;
  501. /**
  502. * If set, parse comments and set as this property
  503. *
  504. * Defaults to `false`
  505. */
  506. commentPropName?: false | string;
  507. /**
  508. * Whether to make output pretty instead of single line
  509. *
  510. * Defaults to `false`
  511. */
  512. format?: boolean;
  513. /**
  514. * If `format` is set to `true`, sets the indent string
  515. *
  516. * Defaults to ` `
  517. */
  518. indentBy?: string;
  519. /**
  520. * Give a name to a top-level array
  521. *
  522. * Defaults to `undefined`
  523. */
  524. arrayNodeName?: string;
  525. /**
  526. * Create empty tags for tags with no text value
  527. *
  528. * Defaults to `false`
  529. */
  530. suppressEmptyNode?: boolean;
  531. /**
  532. * Suppress an unpaired tag
  533. *
  534. * Defaults to `true`
  535. */
  536. suppressUnpairedNode?: boolean;
  537. /**
  538. * Don't put a value for boolean attributes
  539. *
  540. * Defaults to `true`
  541. */
  542. suppressBooleanAttributes?: boolean;
  543. /**
  544. * Preserve the order of tags in resulting JS object
  545. *
  546. * Defaults to `false`
  547. */
  548. preserveOrder?: boolean;
  549. /**
  550. * List of tags without closing tags
  551. *
  552. * Defaults to `[]`
  553. */
  554. unpairedTags?: string[];
  555. /**
  556. * Nodes to stop parsing at
  557. *
  558. * Accepts string patterns or Expression objects from path-expression-matcher
  559. *
  560. * Defaults to `[]`
  561. */
  562. stopNodes?: JPathOrExpression[];
  563. /**
  564. * Control how tag value should be parsed. Called only if tag value is not empty
  565. *
  566. * @returns {undefined|null} `undefined` or `null` to set original value.
  567. * @returns {unknown}
  568. *
  569. * 1. Different value or value with different data type to set new value.
  570. * 2. Same value to set parsed value if `parseTagValue: true`.
  571. *
  572. * Defaults to `(tagName, val, jPath, hasAttributes, isLeafNode) => val`
  573. */
  574. tagValueProcessor?: (name: string, value: unknown) => unknown;
  575. /**
  576. * Control how attribute value should be parsed
  577. *
  578. * @param attrName
  579. * @param attrValue
  580. * @param jPath
  581. * @returns {undefined|null} `undefined` or `null` to set original value
  582. * @returns {unknown}
  583. *
  584. * Defaults to `(attrName, val, jPath) => val`
  585. */
  586. attributeValueProcessor?: (name: string, value: unknown) => unknown;
  587. /**
  588. * Whether to process default and DOCTYPE entities
  589. *
  590. * Defaults to `true`
  591. */
  592. processEntities?: boolean;
  593. oneListGroup?: boolean;
  594. /**
  595. * Maximum number of nested tags
  596. *
  597. * Defaults to `100`
  598. */
  599. maxNestedTags?: number;
  600. };
  601. type ESchema = string | object | Array<string | object>;
  602. type ValidationError = {
  603. err: {
  604. code: string;
  605. msg: string,
  606. line: number,
  607. col: number
  608. };
  609. };
  610. declare class XMLParser {
  611. constructor(options?: X2jOptions);
  612. parse(xmlData: string | Uint8Array, validationOptions?: validationOptions | boolean): any;
  613. /**
  614. * Add Entity which is not by default supported by this library
  615. * @param entityIdentifier {string} Eg: 'ent' for &ent;
  616. * @param entityValue {string} Eg: '\r'
  617. */
  618. addEntity(entityIdentifier: string, entityValue: string): void;
  619. /**
  620. * Returns a Symbol that can be used to access the {@link XMLMetaData}
  621. * property on a node.
  622. *
  623. * If Symbol is not available in the environment, an ordinary property is used
  624. * and the name of the property is here returned.
  625. *
  626. * The XMLMetaData property is only present when {@link X2jOptions.captureMetaData}
  627. * is true in the options.
  628. */
  629. static getMetaDataSymbol(): Symbol;
  630. }
  631. declare class XMLValidator {
  632. static validate(xmlData: string, options?: validationOptions): true | ValidationError;
  633. }
  634. /**
  635. * @deprecated Use npm package 'fast-xml-builder' instead
  636. */
  637. declare class XMLBuilder {
  638. constructor(options?: XmlBuilderOptions);
  639. build(jObj: any): string;
  640. }
  641. /**
  642. * This object is available on nodes via the symbol {@link XMLParser.getMetaDataSymbol}
  643. * when {@link X2jOptions.captureMetaData} is true.
  644. */
  645. declare interface XMLMetaData {
  646. /** The index, if available, of the character where the XML node began in the input stream. */
  647. startIndex?: number;
  648. }
  649. declare namespace fxp {
  650. export {
  651. XMLParser,
  652. XMLValidator,
  653. XMLBuilder,
  654. XMLMetaData,
  655. XmlBuilderOptions,
  656. X2jOptions,
  657. ESchema,
  658. ValidationError,
  659. strnumOptions,
  660. validationOptions,
  661. ProcessEntitiesOptions,
  662. Expression,
  663. ReadonlyMatcher,
  664. MatcherView,
  665. JPathOrMatcher,
  666. JPathOrExpression,
  667. EntityDecoderOptions
  668. }
  669. }
  670. export = fxp;