fxp.d.ts 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. type strnumOptions = {
  190. hex: boolean;
  191. leadingZeros: boolean,
  192. skipLike?: RegExp,
  193. eNotation?: boolean
  194. }
  195. type validationOptions = {
  196. /**
  197. * Whether to allow attributes without value
  198. *
  199. * Defaults to `false`
  200. */
  201. allowBooleanAttributes?: boolean;
  202. /**
  203. * List of tags without closing tags
  204. *
  205. * Defaults to `[]`
  206. */
  207. unpairedTags?: string[];
  208. };
  209. type XmlBuilderOptions = {
  210. /**
  211. * Give a prefix to the attribute name in the resulting JS object
  212. *
  213. * Defaults to '@_'
  214. */
  215. attributeNamePrefix?: string;
  216. /**
  217. * A name to group all attributes of a tag under, or `false` to disable
  218. *
  219. * Defaults to `false`
  220. */
  221. attributesGroupName?: false | string;
  222. /**
  223. * The name of the next node in the resulting JS
  224. *
  225. * Defaults to `#text`
  226. */
  227. textNodeName?: string;
  228. /**
  229. * Whether to ignore attributes when building
  230. *
  231. * When `true` - ignores all the attributes
  232. *
  233. * When `false` - builds all the attributes
  234. *
  235. * When `Array<string | RegExp>` - filters out attributes that match provided patterns
  236. *
  237. * When `Function` - calls the function for each attribute and filters out those for which the function returned `true`
  238. *
  239. * Defaults to `true`
  240. */
  241. ignoreAttributes?: boolean | (string | RegExp)[] | ((attrName: string, jPath: string) => boolean);
  242. /**
  243. * Give a property name to set CDATA values to instead of merging to tag's text value
  244. *
  245. * Defaults to `false`
  246. */
  247. cdataPropName?: false | string;
  248. /**
  249. * If set, parse comments and set as this property
  250. *
  251. * Defaults to `false`
  252. */
  253. commentPropName?: false | string;
  254. /**
  255. * Whether to make output pretty instead of single line
  256. *
  257. * Defaults to `false`
  258. */
  259. format?: boolean;
  260. /**
  261. * If `format` is set to `true`, sets the indent string
  262. *
  263. * Defaults to ` `
  264. */
  265. indentBy?: string;
  266. /**
  267. * Give a name to a top-level array
  268. *
  269. * Defaults to `undefined`
  270. */
  271. arrayNodeName?: string;
  272. /**
  273. * Create empty tags for tags with no text value
  274. *
  275. * Defaults to `false`
  276. */
  277. suppressEmptyNode?: boolean;
  278. /**
  279. * Suppress an unpaired tag
  280. *
  281. * Defaults to `true`
  282. */
  283. suppressUnpairedNode?: boolean;
  284. /**
  285. * Don't put a value for boolean attributes
  286. *
  287. * Defaults to `true`
  288. */
  289. suppressBooleanAttributes?: boolean;
  290. /**
  291. * Preserve the order of tags in resulting JS object
  292. *
  293. * Defaults to `false`
  294. */
  295. preserveOrder?: boolean;
  296. /**
  297. * List of tags without closing tags
  298. *
  299. * Defaults to `[]`
  300. */
  301. unpairedTags?: string[];
  302. /**
  303. * Nodes to stop parsing at
  304. *
  305. * Defaults to `[]`
  306. */
  307. stopNodes?: string[];
  308. /**
  309. * Control how tag value should be parsed. Called only if tag value is not empty
  310. *
  311. * @returns {undefined|null} `undefined` or `null` to set original value.
  312. * @returns {unknown}
  313. *
  314. * 1. Different value or value with different data type to set new value.
  315. * 2. Same value to set parsed value if `parseTagValue: true`.
  316. *
  317. * Defaults to `(tagName, val, jPath, hasAttributes, isLeafNode) => val`
  318. */
  319. tagValueProcessor?: (name: string, value: unknown) => unknown;
  320. /**
  321. * Control how attribute value should be parsed
  322. *
  323. * @param attrName
  324. * @param attrValue
  325. * @param jPath
  326. * @returns {undefined|null} `undefined` or `null` to set original value
  327. * @returns {unknown}
  328. *
  329. * Defaults to `(attrName, val, jPath) => val`
  330. */
  331. attributeValueProcessor?: (name: string, value: unknown) => unknown;
  332. /**
  333. * Whether to process default and DOCTYPE entities
  334. *
  335. * Defaults to `true`
  336. */
  337. processEntities?: boolean;
  338. oneListGroup?: boolean;
  339. };
  340. type ESchema = string | object | Array<string|object>;
  341. type ValidationError = {
  342. err: {
  343. code: string;
  344. msg: string,
  345. line: number,
  346. col: number
  347. };
  348. };
  349. export class XMLParser {
  350. constructor(options?: X2jOptions);
  351. parse(xmlData: string | Buffer ,validationOptions?: validationOptions | boolean): any;
  352. /**
  353. * Add Entity which is not by default supported by this library
  354. * @param entityIdentifier {string} Eg: 'ent' for &ent;
  355. * @param entityValue {string} Eg: '\r'
  356. */
  357. addEntity(entityIdentifier: string, entityValue: string): void;
  358. }
  359. export class XMLValidator{
  360. static validate( xmlData: string, options?: validationOptions): true | ValidationError;
  361. }
  362. export class XMLBuilder {
  363. constructor(options?: XmlBuilderOptions);
  364. build(jObj: any): any;
  365. }