fxp.d.cts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. type X2jOptions = {
  2. /**
  3. * Preserve the order of tags in resulting JS object
  4. *
  5. * Defaults to `false`
  6. */
  7. preserveOrder?: boolean;
  8. /**
  9. * Give a prefix to the attribute name in the resulting JS object
  10. *
  11. * Defaults to '@_'
  12. */
  13. attributeNamePrefix?: string;
  14. /**
  15. * A name to group all attributes of a tag under, or `false` to disable
  16. *
  17. * Defaults to `false`
  18. */
  19. attributesGroupName?: false | string;
  20. /**
  21. * The name of the next node in the resulting JS
  22. *
  23. * Defaults to `#text`
  24. */
  25. textNodeName?: string;
  26. /**
  27. * Whether to ignore attributes when parsing
  28. *
  29. * When `true` - ignores all the attributes
  30. *
  31. * When `false` - parses all the attributes
  32. *
  33. * When `Array<string | RegExp>` - filters out attributes that match provided patterns
  34. *
  35. * When `Function` - calls the function for each attribute and filters out those for which the function returned `true`
  36. *
  37. * Defaults to `true`
  38. */
  39. ignoreAttributes?: boolean | (string | RegExp)[] | ((attrName: string, jPath: string) => boolean);
  40. /**
  41. * Whether to remove namespace string from tag and attribute names
  42. *
  43. * Defaults to `false`
  44. */
  45. removeNSPrefix?: boolean;
  46. /**
  47. * Whether to allow attributes without value
  48. *
  49. * Defaults to `false`
  50. */
  51. allowBooleanAttributes?: boolean;
  52. /**
  53. * Whether to parse tag value with `strnum` package
  54. *
  55. * Defaults to `true`
  56. */
  57. parseTagValue?: boolean;
  58. /**
  59. * Whether to parse tag value with `strnum` package
  60. *
  61. * Defaults to `false`
  62. */
  63. parseAttributeValue?: boolean;
  64. /**
  65. * Whether to remove surrounding whitespace from tag or attribute value
  66. *
  67. * Defaults to `true`
  68. */
  69. trimValues?: boolean;
  70. /**
  71. * Give a property name to set CDATA values to instead of merging to tag's text value
  72. *
  73. * Defaults to `false`
  74. */
  75. cdataPropName?: false | string;
  76. /**
  77. * If set, parse comments and set as this property
  78. *
  79. * Defaults to `false`
  80. */
  81. commentPropName?: false | string;
  82. /**
  83. * Control how tag value should be parsed. Called only if tag value is not empty
  84. *
  85. * @returns {undefined|null} `undefined` or `null` to set original value.
  86. * @returns {unknown}
  87. *
  88. * 1. Different value or value with different data type to set new value.
  89. * 2. Same value to set parsed value if `parseTagValue: true`.
  90. *
  91. * Defaults to `(tagName, val, jPath, hasAttributes, isLeafNode) => val`
  92. */
  93. tagValueProcessor?: (tagName: string, tagValue: string, jPath: string, hasAttributes: boolean, isLeafNode: boolean) => unknown;
  94. /**
  95. * Control how attribute value should be parsed
  96. *
  97. * @param attrName
  98. * @param attrValue
  99. * @param jPath
  100. * @returns {undefined|null} `undefined` or `null` to set original value
  101. * @returns {unknown}
  102. *
  103. * Defaults to `(attrName, val, jPath) => val`
  104. */
  105. attributeValueProcessor?: (attrName: string, attrValue: string, jPath: string) => unknown;
  106. /**
  107. * Options to pass to `strnum` for parsing numbers
  108. *
  109. * Defaults to `{ hex: true, leadingZeros: true, eNotation: true }`
  110. */
  111. numberParseOptions?: strnumOptions;
  112. /**
  113. * Nodes to stop parsing at
  114. *
  115. * Defaults to `[]`
  116. */
  117. stopNodes?: string[];
  118. /**
  119. * List of tags without closing tags
  120. *
  121. * Defaults to `[]`
  122. */
  123. unpairedTags?: string[];
  124. /**
  125. * Whether to always create a text node
  126. *
  127. * Defaults to `false`
  128. */
  129. alwaysCreateTextNode?: boolean;
  130. /**
  131. * Determine whether a tag should be parsed as an array
  132. *
  133. * @param tagName
  134. * @param jPath
  135. * @param isLeafNode
  136. * @param isAttribute
  137. * @returns {boolean}
  138. *
  139. * Defaults to `() => false`
  140. */
  141. isArray?: (tagName: string, jPath: string, isLeafNode: boolean, isAttribute: boolean) => boolean;
  142. /**
  143. * Whether to process default and DOCTYPE entities
  144. *
  145. * Defaults to `true`
  146. */
  147. processEntities?: boolean;
  148. /**
  149. * Whether to process HTML entities
  150. *
  151. * Defaults to `false`
  152. */
  153. htmlEntities?: boolean;
  154. /**
  155. * Whether to ignore the declaration tag from output
  156. *
  157. * Defaults to `false`
  158. */
  159. ignoreDeclaration?: boolean;
  160. /**
  161. * Whether to ignore Pi tags
  162. *
  163. * Defaults to `false`
  164. */
  165. ignorePiTags?: boolean;
  166. /**
  167. * Transform tag names
  168. *
  169. * Defaults to `false`
  170. */
  171. transformTagName?: ((tagName: string) => string) | false;
  172. /**
  173. * Transform attribute names
  174. *
  175. * Defaults to `false`
  176. */
  177. transformAttributeName?: ((attributeName: string) => string) | false;
  178. /**
  179. * Change the tag name when a different name is returned. Skip the tag from parsed result when false is returned.
  180. * Modify `attrs` object to control attributes for the given tag.
  181. *
  182. * @returns {string} new tag name.
  183. * @returns false to skip the tag
  184. *
  185. * Defaults to `(tagName, jPath, attrs) => tagName`
  186. */
  187. updateTag?: (tagName: string, jPath: string, attrs: {[k: string]: string}) => string | boolean;
  188. /**
  189. * If true, adds a Symbol to all object nodes, accessible by {@link XMLParser.getMetaDataSymbol} with
  190. * metadata about each the node in the XML file.
  191. */
  192. captureMetaData?: boolean;
  193. };
  194. type strnumOptions = {
  195. hex: boolean;
  196. leadingZeros: boolean,
  197. skipLike?: RegExp,
  198. eNotation?: boolean
  199. }
  200. type validationOptions = {
  201. /**
  202. * Whether to allow attributes without value
  203. *
  204. * Defaults to `false`
  205. */
  206. allowBooleanAttributes?: boolean;
  207. /**
  208. * List of tags without closing tags
  209. *
  210. * Defaults to `[]`
  211. */
  212. unpairedTags?: string[];
  213. };
  214. type XmlBuilderOptions = {
  215. /**
  216. * Give a prefix to the attribute name in the resulting JS object
  217. *
  218. * Defaults to '@_'
  219. */
  220. attributeNamePrefix?: string;
  221. /**
  222. * A name to group all attributes of a tag under, or `false` to disable
  223. *
  224. * Defaults to `false`
  225. */
  226. attributesGroupName?: false | string;
  227. /**
  228. * The name of the next node in the resulting JS
  229. *
  230. * Defaults to `#text`
  231. */
  232. textNodeName?: string;
  233. /**
  234. * Whether to ignore attributes when building
  235. *
  236. * When `true` - ignores all the attributes
  237. *
  238. * When `false` - builds all the attributes
  239. *
  240. * When `Array<string | RegExp>` - filters out attributes that match provided patterns
  241. *
  242. * When `Function` - calls the function for each attribute and filters out those for which the function returned `true`
  243. *
  244. * Defaults to `true`
  245. */
  246. ignoreAttributes?: boolean | (string | RegExp)[] | ((attrName: string, jPath: string) => boolean);
  247. /**
  248. * Give a property name to set CDATA values to instead of merging to tag's text value
  249. *
  250. * Defaults to `false`
  251. */
  252. cdataPropName?: false | string;
  253. /**
  254. * If set, parse comments and set as this property
  255. *
  256. * Defaults to `false`
  257. */
  258. commentPropName?: false | string;
  259. /**
  260. * Whether to make output pretty instead of single line
  261. *
  262. * Defaults to `false`
  263. */
  264. format?: boolean;
  265. /**
  266. * If `format` is set to `true`, sets the indent string
  267. *
  268. * Defaults to ` `
  269. */
  270. indentBy?: string;
  271. /**
  272. * Give a name to a top-level array
  273. *
  274. * Defaults to `undefined`
  275. */
  276. arrayNodeName?: string;
  277. /**
  278. * Create empty tags for tags with no text value
  279. *
  280. * Defaults to `false`
  281. */
  282. suppressEmptyNode?: boolean;
  283. /**
  284. * Suppress an unpaired tag
  285. *
  286. * Defaults to `true`
  287. */
  288. suppressUnpairedNode?: boolean;
  289. /**
  290. * Don't put a value for boolean attributes
  291. *
  292. * Defaults to `true`
  293. */
  294. suppressBooleanAttributes?: boolean;
  295. /**
  296. * Preserve the order of tags in resulting JS object
  297. *
  298. * Defaults to `false`
  299. */
  300. preserveOrder?: boolean;
  301. /**
  302. * List of tags without closing tags
  303. *
  304. * Defaults to `[]`
  305. */
  306. unpairedTags?: string[];
  307. /**
  308. * Nodes to stop parsing at
  309. *
  310. * Defaults to `[]`
  311. */
  312. stopNodes?: string[];
  313. /**
  314. * Control how tag value should be parsed. Called only if tag value is not empty
  315. *
  316. * @returns {undefined|null} `undefined` or `null` to set original value.
  317. * @returns {unknown}
  318. *
  319. * 1. Different value or value with different data type to set new value.
  320. * 2. Same value to set parsed value if `parseTagValue: true`.
  321. *
  322. * Defaults to `(tagName, val, jPath, hasAttributes, isLeafNode) => val`
  323. */
  324. tagValueProcessor?: (name: string, value: unknown) => unknown;
  325. /**
  326. * Control how attribute value should be parsed
  327. *
  328. * @param attrName
  329. * @param attrValue
  330. * @param jPath
  331. * @returns {undefined|null} `undefined` or `null` to set original value
  332. * @returns {unknown}
  333. *
  334. * Defaults to `(attrName, val, jPath) => val`
  335. */
  336. attributeValueProcessor?: (name: string, value: unknown) => unknown;
  337. /**
  338. * Whether to process default and DOCTYPE entities
  339. *
  340. * Defaults to `true`
  341. */
  342. processEntities?: boolean;
  343. oneListGroup?: boolean;
  344. };
  345. type ESchema = string | object | Array<string|object>;
  346. type ValidationError = {
  347. err: {
  348. code: string;
  349. msg: string,
  350. line: number,
  351. col: number
  352. };
  353. };
  354. declare class XMLParser {
  355. constructor(options?: X2jOptions);
  356. parse(xmlData: string | Buffer ,validationOptions?: validationOptions | boolean): any;
  357. /**
  358. * Add Entity which is not by default supported by this library
  359. * @param entityIdentifier {string} Eg: 'ent' for &ent;
  360. * @param entityValue {string} Eg: '\r'
  361. */
  362. addEntity(entityIdentifier: string, entityValue: string): void;
  363. /**
  364. * Returns a Symbol that can be used to access the {@link XMLMetaData}
  365. * property on a node.
  366. *
  367. * If Symbol is not available in the environment, an ordinary property is used
  368. * and the name of the property is here returned.
  369. *
  370. * The XMLMetaData property is only present when {@link X2jOptions.captureMetaData}
  371. * is true in the options.
  372. */
  373. static getMetaDataSymbol() : Symbol;
  374. }
  375. declare class XMLValidator{
  376. static validate(xmlData: string, options?: validationOptions): true | ValidationError;
  377. }
  378. declare class XMLBuilder {
  379. constructor(options?: XmlBuilderOptions);
  380. build(jObj: any): string;
  381. }
  382. /**
  383. * This object is available on nodes via the symbol {@link XMLParser.getMetaDataSymbol}
  384. * when {@link X2jOptions.captureMetaData} is true.
  385. */
  386. declare interface XMLMetaData {
  387. /** The index, if available, of the character where the XML node began in the input stream. */
  388. startIndex?: number;
  389. }
  390. declare namespace fxp {
  391. export {
  392. XMLParser,
  393. XMLValidator,
  394. XMLBuilder,
  395. XMLMetaData
  396. }
  397. }
  398. export = fxp;