6
6
// Imports
7
7
import { initSync , JsReader , source , Token , tokenize } from "./wasm_xml_parser/wasm_xml_parser.js"
8
8
import type { Nullable , ReaderSync , xml_document , xml_node , xml_text } from "./_types.ts"
9
- export type { Nullable , ReaderSync , xml_document , xml_node , xml_text }
9
+ export type * from "./_types.ts"
10
10
initSync ( source ( ) )
11
11
12
12
/** XML parser options. */
13
- export type options = {
13
+ export type parse_options = {
14
14
/** Remove elements from result. */
15
- clean ?: {
16
- /** Remove attributes from result. */
17
- attributes ?: boolean
18
- /** Remove comments from result. */
19
- comments ?: boolean
20
- /** Remove XML doctype from result. */
21
- doctype ?: boolean
22
- /** Remove XML processing instructions from result. */
23
- instructions ?: boolean
24
- }
15
+ clean ?: clean_options
25
16
/** Flatten result depending on node content. */
26
- flatten ?: {
27
- /** If node only contains attributes values (i.e. with key starting with `@`), it'll be flattened as a regular object without `@` prefixes. */
28
- attributes ?: boolean
29
- /** If node only contains a `#text` value, it'll be flattened as a string (defaults to `true`). */
30
- text ?: boolean
31
- /** If node does not contains any attribute or text, it'll be flattened to `null` (defaults to `true`). */
32
- empty ?: boolean
33
- }
17
+ flatten ?: flatten_options
34
18
/** Revive result. */
35
- revive ?: {
36
- /**
37
- * Trim texts (this is applied before other revivals, defaults to `true`).
38
- * It honors `xml:space="preserve"` attribute.
39
- */
40
- trim ?: boolean
41
- /**
42
- * Revive XML entities (defaults to `true`).
43
- * Automatically unescape XML entities and replace common entities with their respective characters.
44
- */
45
- entities ?: boolean
46
- /** Revive booleans (matching `/^(?:[Tt]rue|[Ff]alse)$/`).*/
47
- booleans ?: boolean
48
- /**
49
- * Revive finite numbers.
50
- * Note that the version of the XML prolog is always treated as a string to avoid breaking documents.
51
- */
52
- numbers ?: boolean
53
- /**
54
- * Custom reviver (this is applied after other revivals).
55
- * When it is applied on an attribute, `key` and `value` will be given.
56
- * When it is applied on a node, both `key` and `value` will be `null`.
57
- * Return `undefined` to delete either the attribute or the tag.
58
- */
59
- custom ?: ( args : { name : string ; key : Nullable < string > ; value : Nullable < string > ; node : Readonly < xml_node > } ) => unknown
60
- }
19
+ revive ?: revive_options
61
20
/**
62
21
* Parsing mode.
63
22
* Using `html` is more permissive and will not throw on some invalid XML syntax.
@@ -66,6 +25,62 @@ export type options = {
66
25
mode ?: "xml" | "html"
67
26
}
68
27
28
+ /** XML parser {@linkcode parse_options}`.clean` */
29
+ export type clean_options = {
30
+ /** Remove attributes from result. */
31
+ attributes ?: boolean
32
+ /** Remove comments from result. */
33
+ comments ?: boolean
34
+ /** Remove XML doctype from result. */
35
+ doctype ?: boolean
36
+ /** Remove XML processing instructions from result. */
37
+ instructions ?: boolean
38
+ }
39
+
40
+ /** XML parser {@linkcode parse_options}`.flatten` */
41
+ export type flatten_options = {
42
+ /** If node only contains attributes values (i.e. with key starting with `@`), it'll be flattened as a regular object without `@` prefixes. */
43
+ attributes ?: boolean
44
+ /** If node only contains a `#text` value, it'll be flattened as a string (defaults to `true`). */
45
+ text ?: boolean
46
+ /** If node does not contains any attribute or text, it'll be flattened to `null` (defaults to `true`). */
47
+ empty ?: boolean
48
+ }
49
+
50
+ /** XML parser {@linkcode parse_options}`.revive` */
51
+ export type revive_options = {
52
+ /**
53
+ * Trim texts (this is applied before other revivals, defaults to `true`).
54
+ * It honors `xml:space="preserve"` attribute.
55
+ */
56
+ trim ?: boolean
57
+ /**
58
+ * Revive XML entities (defaults to `true`).
59
+ * Automatically unescape XML entities and replace common entities with their respective characters.
60
+ */
61
+ entities ?: boolean
62
+ /** Revive booleans (matching `/^(?:[Tt]rue|[Ff]alse)$/`).*/
63
+ booleans ?: boolean
64
+ /**
65
+ * Revive finite numbers.
66
+ * Note that the version of the XML prolog is always treated as a string to avoid breaking documents.
67
+ */
68
+ numbers ?: boolean
69
+ /**
70
+ * Custom reviver (this is applied after other revivals).
71
+ * When it is applied on an attribute, `key` and `value` will be given.
72
+ * When it is applied on a node, both `key` and `value` will be `null`.
73
+ * Return `undefined` to delete either the attribute or the tag.
74
+ */
75
+ custom ?: reviver
76
+ }
77
+
78
+ /**
79
+ * Custom XML parser reviver.
80
+ * It can be used to change the way some nodes are parsed.
81
+ */
82
+ export type reviver = ( args : { name : string ; key : Nullable < string > ; value : Nullable < string > ; node : Readonly < xml_node > } ) => unknown
83
+
69
84
/**
70
85
* Parse a XML string into an object.
71
86
*
@@ -115,7 +130,7 @@ export type options = {
115
130
* console.log(parse(file))
116
131
* ```
117
132
*/
118
- export function parse ( content : string | ReaderSync , options ?: options ) : xml_document {
133
+ export function parse ( content : string | ReaderSync , options ?: parse_options ) : xml_document {
119
134
const xml = xml_node ( "~xml" ) as xml_document
120
135
const stack = [ xml ] as Array < xml_node >
121
136
const tokens = [ ] as Array < [ number , string , string ?] >
@@ -322,7 +337,7 @@ function xml_node(name: string, { parent = null as Nullable<xml_node> } = {}): x
322
337
}
323
338
324
339
/** Post-process xml node. */
325
- function postprocess ( node : xml_node , options : options ) {
340
+ function postprocess ( node : xml_node , options : parse_options ) {
326
341
// Clean XML document if required
327
342
if ( node [ "~name" ] === "~xml" ) {
328
343
if ( options ?. clean ?. doctype ) {
@@ -419,7 +434,7 @@ const entities = {
419
434
} as const
420
435
421
436
/** Revive value. */
422
- function revive ( node : xml_node | xml_text , key : string , options : options ) {
437
+ function revive ( node : xml_node | xml_text , key : string , options : parse_options ) {
423
438
let value = ( node as xml_node ) [ key ] as string
424
439
if ( options ?. revive ?. trim ) {
425
440
value = value . trim ( )
0 commit comments