1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442 |
x1
x3
x3
x1
x3
x3
x91
x273
x91
x91
x273
x91
x91
x91
x91
x95
x392
x98
x95
x96
x96
x95
x178
x178
x97
x97
x172
x172
x172
x172
x172
x172
x172
x172
x2930874
x2930874
x5861767
x2930893
x2930893
x2930893
x2930893
x2930893
x2930893
x2930893
x2930893
x2930910
x2930910
x2930893
x2930893
x2930893
x2930894
x2930894
x2930893
x2930893
x2930874
x5861761
x8792661
x2930887
x2930887
x2930874
x5861762
x2930888
x8792664
x2930888
x2930888
x2930888
x2930889
x2930889
x2930888
x11723556
x2930889
x2930888
x2930900
x2930888
x2930888
x2930888
x2930874
x6838668
x3907794
x3907873
x3907874
x3907874
x3907951
x3907951
x4884713
x14654139
x4884713
x3907794
x4884326
x4884326
x3907794
x15631288
x3907822
x3907794
x3908153
x3907794
x4884713
x4884713
x4884713
x2930874
x6838667
x3907793
x3907793
x3907793
x2930874
x5861839
x2930965
x2930965
x2930965
x2930874
x6838534
x15630640
x3907660
x3907660
x2930874
x5861752
x11723512
x2930878
x2930878
x2930874
x5861765
x11723564
x2930891
x2930891
x2930874
x2930874
x171
x94
x94
x168
x168
x3
x3
x17
x17
x45
x45
x17
x17
x3
x3
x16
x16
x16
x16
x16
x16
x16
x16
x16
x16
x16
x30
x30
x16
x16
x3
x3
x976810
x4884050
x4884050
x976810
x976810
x976810
x976810
x976810
x976810
x976810
x3
x3
x977037
x4885185
x4885185
x4885185
x977037
x977037
x977037
x977037
x3907405
x3907405
x3907405
x6837755
x6837755
x6837404
x6837404
x6837404
x6837404
x6837410
x6837410
x6837410
x3907423
x3907423
x977037
x977037
x977037
x977037
x977037
x977068
x977068
x977037
x977037
x977037
x1953983
x1953983
x977037
x977037
x3
x3
x977032
x977032
x977109
x977111
x977111
x977109
x977111
x977111
x977111
x977109
x977032
x977032
x1954050
x1954056
x1954056
x1954050
x23446891
x2931059
x1954050
x11723336
x2930834
x1954050
x7816244
x1954061
x1954050
x977032
x977032
x1954409
x1954409
x1954420
x1954420
x2931775
x1954409
x1954415
x1954415
x1954415
x2931769
x2931769
x1954581
x1954581
x1954582
x1954582
x1954581
x1954581
x2931597
x2931597
x1954438
x9772190
x9772190
x1954438
x1954409
x2931929
x2931929
x2931597
x1954410
x1954410
x1954409
x977032
x977032
x977032
x13676712
x9769080
x1953816
x977032
x977032
x5862222
x977038
x977038
x977037
x1954060
x977032
x1953764
x1953764
x977032
x977033
x977035
x977035
x977035
x977033
x977033
x977327
x4885350
x977066
x977293
x977293
x3
x3
x3
x3
x3
x3
x3
x3
x3
x3
x1953758
x1953758
x3907494
x3907494
x1953758
x2930706
x2930706
x7815446
x7815446
x2930706
x1953758
x1953827
x1953827
x1953758
x1953764
x1953764
x1953758
x11722560
x1953760
x3907511
x3907511 |
|
import { initSync, JsReader, source, Token, tokenize } from "./wasm_xml_parser/wasm_xml_parser.js"
import type { record, rw } from "@libs/typing"
import type { Nullable, ReaderSync, xml_document, xml_node, xml_text } from "./_types.ts"
export type { Nullable, ReaderSync, xml_document, xml_node, xml_text }
initSync(source())
export type options = {
clean?: {
attributes?: boolean
comments?: boolean
doctype?: boolean
instructions?: boolean
}
flatten?: {
attributes?: boolean
text?: boolean
empty?: boolean
}
revive?: {
trim?: boolean
entities?: boolean
booleans?: boolean
numbers?: boolean
custom?: (args: { name: string; key: Nullable<string>; value: Nullable<string>; node: Readonly<xml_node> }) => unknown
}
mode?: "xml" | "html"
}
export function parse(content: string | ReaderSync, options?: options): xml_document {
const xml = xml_node("~xml") as xml_document
const stack = [xml] as Array<xml_node>
const tokens = [] as Array<[number, string, string?]>
const states = [] as Array<[number, number]>
const flags = { root: false }
try {
const reader = new JsReader(new TextEncoder().encode(content as string), typeof content === "object" ? content : undefined)
tokenize(reader, tokens, states, options?.mode === "html")
} catch (error) {
if (states.at(-1)?.[0] === Token.StateParseAttribute) {
tokens.push([Token.Error, `Failed to parse attribute around position ${states.at(-1)![1]}`])
}
if (!states.length) {
throw new EvalError(`WASM XML parser crashed: ${error}`)
}
}
const errors = tokens.find(([token]) => token === Token.Error)
if (errors) {
throw new SyntaxError(`Malformed XML document: ${errors[1]}`)
}
options ??= {}
options.revive ??= {}
options.revive.trim ??= true
options.revive.entities ??= true
options.flatten ??= {}
options.flatten.text ??= true
options.flatten.empty ??= true
for (const [token, name, value = name] of tokens) {
switch (token) {
case Token.XMLDeclaration: {
const version = value.match(/version=(["'])(?<version>1\.\d+)(\1)/)?.groups?.version
if (version) {
xml["@version"] = version as typeof xml["@version"]
}
const encoding = value.match(/encoding=(["'])(?<encoding>[A-Za-z][-\w.]*)(\1)/)?.groups?.encoding
if (encoding) {
xml["@encoding"] = encoding as typeof xml["@encoding"]
}
const standalone = value.match(/standalone=(["'])(?<standalone>yes|no)(\1)/)?.groups?.standalone
if (standalone) {
xml["@standalone"] = standalone as typeof xml["@standalone"]
}
break
}
case Token.XMLDoctype: {
xml["#doctype"] = Object.assign(xml_node("~doctype", { parent: xml }), xml_doctype(value))
break
}
case Token.XMLInstruction: {
const [name, ...raw] = value.split(" ")
const instruction = Object.assign(xml_node(name, { parent: xml }), xml_attributes(raw.join(" ")))
xml["#instructions"] ??= {}
switch (true) {
case Array.isArray(xml["#instructions"][name]):
;(xml["#instructions"][name] as Array<xml_node>).push(instruction)
break
case name in xml["#instructions"]:
xml["#instructions"][name] = [xml["#instructions"][name] as xml_node, instruction]
break
default:
xml["#instructions"][name] = instruction
}
break
}
case Token.TagOpen: {
if (stack.length === 1) {
if (flags.root) {
throw new SyntaxError("Multiple root node detected")
}
flags.root = true
}
const parent = stack.at(-1)!
const node = xml_node(name, { parent })
switch (true) {
case Array.isArray(parent[node["~name"]]):
;(parent[node["~name"]] as Array<xml_node>).push(node)
break
case node["~name"] in parent:
parent[node["~name"]] = [parent[node["~name"]], node]
break
default:
parent[node["~name"]] = node
}
stack.push(node)
break
}
case Token.TagClose: {
stack.pop()
break
}
case Token.TagAttribute: {
stack.at(-1)![`@${name}`] = value
break
}
case Token.Text: {
xml_text(value, { type: "~text", parent: stack.at(-1)! })
break
}
case Token.CData: {
xml_text(value, { type: "~cdata", parent: stack.at(-1)! })
break
}
case Token.Comment: {
xml_text(value, { type: "~comment", parent: stack.at(-1)! })
break
}
}
}
if (!Object.keys(xml).length) {
throw new SyntaxError("Malformed XML document: empty document or no root node detected")
}
return postprocess(xml, options) as xml_document
}
function xml_attributes(raw: string) {
const attributes = {} as record<string>
for (const [_, name, __, value] of raw.matchAll(/(?<name>[A-Za-z_][-\w.:]*)=(["'])(?<value>(?:(?!\2).)*)(\2)/g)) {
attributes[`@${name}`] = value
}
return attributes
}
function xml_doctype(raw: string) {
const node = {} as xml_node
const { attributes: _attributes, elements: _elements = "" } = raw.match(/^(?<attributes>[^\[]*)(?:\[(?<elements>[\s\S]*)\])?/)?.groups!
raw = raw.replace(`[${_elements}]`, "")
for (const [match, __, name] of _attributes.matchAll(/(["'])(?<name>(?:(?!\1).)*)(\1)/g)) {
node[`@${name}`] = ""
raw = raw.replace(match, "")
}
raw.split(/\s+/).filter(Boolean).forEach((name) => node[`@${name}`] = "")
for (const [_, name, value] of _elements.matchAll(/<!ELEMENT\s+(?<name>\w+)\s+\((?<value>[^\)]+)\)/g)) {
node[name] = value
}
return node
}
function xml_text(value: string, { type = "~text" as "~text" | "~cdata" | "~comment", parent = null as Nullable<xml_node> } = {}): xml_text {
const text = Object.defineProperties({}, {
["~parent"]: { enumerable: false, writable: false, value: parent },
["~name"]: { enumerable: false, writable: false, value: type },
}) as xml_text
text["#text"] = value
if (parent) {
parent["~children"].push(text)
}
return text
}
function xml_node(name: string, { parent = null as Nullable<xml_node> } = {}): xml_node {
const node = Object.defineProperties({}, {
["~parent"]: { enumerable: false, writable: false, value: parent },
["~name"]: { enumerable: false, writable: false, value: name },
["~children"]: { enumerable: false, writable: true, value: [] },
["#text"]: {
enumerable: false,
configurable: true,
get(this: xml_node) {
const children = this["~children"].filter((node) => node["~name"] !== "~comment")
if (this["@xml:space"] !== "preserve") {
return children.map((child) => child["#text"]).filter(Boolean).join(" ")
}
let text = ""
for (let i = 0; i < children.length; i++) {
const spaced = i && (+children[i - 1]["~name"].startsWith("~") ^ +children[i]["~name"].startsWith("~")) && (!children[i - 1]["#text"].endsWith(" ")) && (!children[i]["#text"].startsWith(" "))
text += `${spaced ? " " : ""}${children[i]["#text"]}`
}
return text
},
},
["#comments"]: {
enumerable: false,
configurable: true,
get(this: xml_node) {
return this["~children"].filter((node) => node["~name"] === "~comment").map((node) => node["#text"]!)
},
},
}) as xml_node
if (parent) {
parent["~children"].push(node)
}
return node
}
function postprocess(node: xml_node, options: options) {
if (node["~name"] === "~xml") {
if (options?.clean?.doctype) {
delete node["#doctype"]
}
if (options?.clean?.instructions) {
;(node as rw)["~children"] = node["~children"].filter((child) => !(child["~name"] in ((node as xml_document)["#instructions"] ?? {})))
delete node["#instructions"]
}
}
if (node["~children"]) {
if (options?.clean?.comments) {
;(node as rw)["~children"] = node["~children"].filter((child) => child["~name"] !== "~comment")
}
if (options?.revive?.trim) {
node["~children"].forEach((child) => /^~(?:text|cdata|comment)$/.test(child["~name"]) ? (child as rw)["#text"] = revive(child, "#text", { revive: { trim: node["@xml:space"] !== "preserve" } }) : null)
}
if (node["~children"].some((child) => (/^~(?:text|cdata)$/.test(child["~name"])) && (child["#text"].trim().length + (node["@xml:space"] === "preserve" ? 1 : 0) * child["#text"].length))) {
Object.defineProperty(node, "#text", { enumerable: true, configurable: true })
}
if (node["~children"].some((child) => child["~name"] === "~comment")) {
Object.defineProperty(node, "#comments", { enumerable: true, configurable: true })
}
}
for (const [key, value] of Object.entries(node)) {
if (key === "#comments") {
continue
}
if ((options?.clean?.attributes) && (key.startsWith("@"))) {
delete node[key]
continue
}
if (key.startsWith("@")) {
node[key] = revive(node, key, options)
if (node[key] === undefined) {
delete node[key]
}
continue
}
if (Array.isArray(value)) {
node[key] = Object.defineProperties(value.map((child) => postprocess(child, options)), {
["~parent"]: { enumerable: false, writable: false, value: node },
["~name"]: { enumerable: false, writable: false, value: key },
})
} else if ((typeof value === "object") && value) {
node[key] = postprocess(value as xml_node, options)
}
if (node[key] === undefined) {
delete node[key]
}
}
const keys = Object.keys(node)
if (keys.includes("#text")) {
const _options = { ...options, revive: { ...options?.revive, trim: (options?.revive?.trim) && (node["@xml:space"] !== "preserve") } }
Object.defineProperty(node, "#text", { enumerable: true, configurable: true, value: revive(node, "#text", _options) })
}
if (options?.revive?.custom) {
if (options.revive.custom({ name: node["~name"], key: null, value: null, node: node as xml_node }) === undefined) {
return undefined
}
}
if ((options?.flatten?.text) && (keys.length === 1) && (keys.includes("#text"))) {
return node["#text"]
}
if ((options?.flatten?.attributes) && (keys.length) && (keys.every((key) => key.startsWith("@")))) {
for (const key of keys) {
node[key.slice(1)] = node[key]
delete node[key]
}
return node
}
if (!keys.length) {
return (options?.flatten?.empty) ? null : (options?.flatten?.text) ? "" : Object.defineProperty(node, "#text", { enumerable: true, configurable: true, value: "" })
}
return node
}
const entities = {
"<": "<",
">": ">",
"'": "'",
""": '"',
"&": "&",
} as const
function revive(node: xml_node | xml_text, key: string, options: options) {
let value = (node as xml_node)[key] as string
if (options?.revive?.trim) {
value = value.trim()
}
if (options?.revive?.entities) {
value = value.replaceAll(/&#(?<hex>x?)(?<code>\d+);/g, (_, hex, code) => String.fromCharCode(Number.parseInt(code, hex ? 16 : 10)))
for (const [entity, character] of Object.entries(entities)) {
value = value.replaceAll(entity, character)
}
}
if ((options?.revive?.numbers) && (value.length) && (Number.isFinite(Number(value))) && (!((node["~name"] === "~xml") && (key === "@version")))) {
value = Number(value) as unknown as string
}
if ((options?.revive?.booleans) && (/^(?:[Tt]rue|[Ff]alse)$/.test(value))) {
value = /^[Tt]rue$/.test(value) as unknown as string
}
if (options?.revive?.custom) {
return options.revive.custom({ name: node["~name"], key, value, node: node as xml_node })
}
return value
}
|