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
x7
x7
x1
x7
x7
x95
x285
x95
x95
x285
x95
x95
x95
x95
x99
x408
x102
x99
x100
x100
x99
x182
x182
x101
x101
x176
x176
x176
x176
x176
x176
x176
x176
x2930878
x2930878
x5861775
x2930897
x2930897
x2930897
x2930897
x2930897
x2930897
x2930897
x2930897
x2930914
x2930914
x2930897
x2930897
x2930897
x2930898
x2930898
x2930897
x2930897
x2930878
x5861769
x8792673
x2930891
x2930891
x2930878
x5861770
x2930892
x8792676
x2930892
x2930892
x2930892
x2930893
x2930893
x2930892
x11723572
x2930893
x2930892
x2930904
x2930892
x2930892
x2930892
x2930878
x6838676
x3907798
x3907877
x3907878
x3907878
x3907955
x3907955
x4884717
x14654151
x4884717
x3907798
x4884330
x4884330
x3907798
x15631304
x3907826
x3907798
x3908157
x3907798
x4884717
x4884717
x4884717
x2930878
x6838675
x3907797
x3907797
x3907797
x2930878
x5861847
x2930969
x2930969
x2930969
x2930878
x6838542
x15630656
x3907664
x3907664
x2930878
x5861760
x11723528
x2930882
x2930882
x2930878
x5861773
x11723580
x2930895
x2930895
x2930878
x2930878
x175
x98
x98
x172
x172
x7
x7
x21
x21
x49
x49
x21
x21
x7
x7
x20
x20
x20
x20
x20
x20
x20
x20
x20
x20
x20
x34
x34
x20
x20
x7
x7
x976814
x4884070
x4884070
x976814
x976814
x976814
x976814
x976814
x976814
x976814
x7
x7
x977041
x4885205
x4885205
x4885205
x977041
x977041
x977041
x977041
x3907409
x3907409
x3907409
x6837759
x6837759
x6837408
x6837408
x6837408
x6837408
x6837414
x6837414
x6837414
x3907427
x3907427
x977041
x977041
x977041
x977041
x977041
x977072
x977072
x977041
x977041
x977041
x1953987
x1953987
x977041
x977041
x7
x7
x977036
x977036
x977113
x977115
x977115
x977113
x977115
x977115
x977115
x977113
x977036
x977036
x1954054
x1954060
x1954060
x1954054
x23446911
x2931063
x1954054
x11723352
x2930838
x1954054
x7816260
x1954065
x1954054
x977036
x977036
x1954413
x1954413
x1954424
x1954424
x2931779
x1954413
x1954419
x1954419
x1954419
x2931773
x2931773
x1954585
x1954585
x1954586
x1954586
x1954585
x1954585
x2931601
x2931601
x1954442
x9772210
x9772210
x1954442
x1954413
x2931933
x2931933
x2931601
x1954414
x1954414
x1954413
x977036
x977036
x977036
x13676740
x9769100
x1953820
x977036
x977036
x5862246
x977042
x977042
x977041
x1954064
x977036
x1953768
x1953768
x977036
x977037
x977039
x977039
x977039
x977037
x977037
x977331
x4885370
x977070
x977297
x977297
x7
x7
x7
x7
x7
x7
x7
x7
x7
x7
x1953762
x1953762
x3907498
x3907498
x1953762
x2930710
x2930710
x7815450
x7815450
x2930710
x1953762
x1953831
x1953831
x1953762
x1953768
x1953768
x1953762
x11722584
x1953764
x3907515
x3907515 |
|
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
}
|