@loaders.gl/graphs 5.0.0-alpha.9 → 5.0.0-beta.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +24 -0
- package/README.md +123 -4
- package/THIRD-PARTY-NOTICES.md +17 -0
- package/dist/dot-loader-types.cjs +60 -0
- package/dist/dot-loader-types.cjs.map +7 -0
- package/dist/dot-loader-types.d.ts +27 -8
- package/dist/dot-loader-types.d.ts.map +1 -1
- package/dist/dot-loader-types.js +3 -2
- package/dist/dot-loader-types.js.map +1 -1
- package/dist/dot-loader.cjs +358 -47
- package/dist/dot-loader.cjs.map +3 -3
- package/dist/dot-loader.d.ts +11 -6
- package/dist/dot-loader.d.ts.map +1 -1
- package/dist/dot-loader.js +3 -3
- package/dist/dot-loader.js.map +1 -1
- package/dist/dot-types.d.ts +6 -1
- package/dist/dot-types.d.ts.map +1 -1
- package/dist/gexf-loader-types.cjs +60 -0
- package/dist/gexf-loader-types.cjs.map +7 -0
- package/dist/gexf-loader-types.d.ts +55 -0
- package/dist/gexf-loader-types.d.ts.map +1 -0
- package/dist/gexf-loader-types.js +27 -0
- package/dist/gexf-loader-types.js.map +1 -0
- package/dist/gexf-loader.cjs +2669 -0
- package/dist/gexf-loader.cjs.map +6 -0
- package/dist/gexf-loader.d.ts +25 -0
- package/dist/gexf-loader.d.ts.map +1 -0
- package/dist/gexf-loader.js +13 -0
- package/dist/gexf-loader.js.map +1 -0
- package/dist/gexf-types.d.ts +16 -0
- package/dist/gexf-types.d.ts.map +1 -0
- package/dist/gexf-types.js +5 -0
- package/dist/gexf-types.js.map +1 -0
- package/dist/graph-types.d.ts +29 -0
- package/dist/graph-types.d.ts.map +1 -1
- package/dist/graphml-loader-types.cjs +60 -0
- package/dist/graphml-loader-types.cjs.map +7 -0
- package/dist/graphml-loader-types.d.ts +26 -8
- package/dist/graphml-loader-types.d.ts.map +1 -1
- package/dist/graphml-loader-types.js +3 -2
- package/dist/graphml-loader-types.js.map +1 -1
- package/dist/graphml-loader.cjs +2032 -69
- package/dist/graphml-loader.cjs.map +3 -3
- package/dist/graphml-loader.d.ts +11 -6
- package/dist/graphml-loader.d.ts.map +1 -1
- package/dist/graphml-loader.js +3 -3
- package/dist/graphml-loader.js.map +1 -1
- package/dist/index.cjs +59 -1085
- package/dist/index.cjs.map +4 -3
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/lib/build-graph-tables.d.ts +4 -0
- package/dist/lib/build-graph-tables.d.ts.map +1 -0
- package/dist/lib/build-graph-tables.js +141 -0
- package/dist/lib/build-graph-tables.js.map +1 -0
- package/dist/lib/parse-dot.d.ts +3 -1
- package/dist/lib/parse-dot.d.ts.map +1 -1
- package/dist/lib/parse-dot.js +38 -21
- package/dist/lib/parse-dot.js.map +1 -1
- package/dist/lib/parse-gexf.d.ts +5 -0
- package/dist/lib/parse-gexf.d.ts.map +1 -0
- package/dist/lib/parse-gexf.js +382 -0
- package/dist/lib/parse-gexf.js.map +1 -0
- package/dist/lib/parse-graphml.d.ts +2 -1
- package/dist/lib/parse-graphml.d.ts.map +1 -1
- package/dist/lib/parse-graphml.js +54 -31
- package/dist/lib/parse-graphml.js.map +1 -1
- package/package.json +18 -8
- package/src/dot-loader-types.ts +16 -5
- package/src/dot-loader.ts +7 -4
- package/src/dot-types.ts +7 -1
- package/src/gexf-loader-types.ts +42 -0
- package/src/gexf-loader.ts +17 -0
- package/src/gexf-types.ts +21 -0
- package/src/graph-types.ts +36 -0
- package/src/graphml-loader-types.ts +15 -5
- package/src/graphml-loader.ts +7 -4
- package/src/index.ts +9 -0
- package/src/lib/build-graph-tables.ts +206 -0
- package/src/lib/parse-dot.ts +44 -21
- package/src/lib/parse-gexf.ts +390 -0
- package/src/lib/parse-graphml.ts +92 -40
package/dist/graphml-loader.cjs
CHANGED
|
@@ -44,18 +44,1795 @@ var init_graphml_loader_types = __esm({
|
|
|
44
44
|
mimeTypes: ["application/graphml+xml"],
|
|
45
45
|
text: true,
|
|
46
46
|
worker: false,
|
|
47
|
+
supportsNativeArrow: true,
|
|
47
48
|
dataType: null,
|
|
48
49
|
batchType: null,
|
|
49
|
-
options: {},
|
|
50
|
+
options: { graphml: { shape: "arrow" } },
|
|
50
51
|
preload
|
|
51
52
|
};
|
|
52
53
|
}
|
|
53
54
|
});
|
|
54
55
|
|
|
56
|
+
// ../documents/src/lib/xml-utils/uncapitalize.ts
|
|
57
|
+
function uncapitalize(str) {
|
|
58
|
+
return typeof str === "string" ? str.charAt(0).toLowerCase() + str.slice(1) : str;
|
|
59
|
+
}
|
|
60
|
+
function uncapitalizeKeys(object) {
|
|
61
|
+
if (Array.isArray(object)) {
|
|
62
|
+
return object.map((element) => uncapitalizeKeys(element));
|
|
63
|
+
}
|
|
64
|
+
if (object && typeof object === "object") {
|
|
65
|
+
const newObject = {};
|
|
66
|
+
for (const [key, value] of Object.entries(object)) {
|
|
67
|
+
Object.defineProperty(newObject, uncapitalize(key), {
|
|
68
|
+
value: uncapitalizeKeys(value),
|
|
69
|
+
enumerable: true,
|
|
70
|
+
configurable: true,
|
|
71
|
+
writable: true
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
return newObject;
|
|
75
|
+
}
|
|
76
|
+
return object;
|
|
77
|
+
}
|
|
78
|
+
var init_uncapitalize = __esm({
|
|
79
|
+
"../documents/src/lib/xml-utils/uncapitalize.ts"() {
|
|
80
|
+
"use strict";
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
// ../documents/src/sax-ts/sax.ts
|
|
85
|
+
var DEFAULT_SAX_EVENTS, DEFAULT_SAX_PARSER_OPTIONS, EVENTS, BUFFERS, nameStart, nameBody, entityStart, entityBody, ENTITIES, SAX, SAXParser;
|
|
86
|
+
var init_sax = __esm({
|
|
87
|
+
"../documents/src/sax-ts/sax.ts"() {
|
|
88
|
+
"use strict";
|
|
89
|
+
DEFAULT_SAX_EVENTS = {
|
|
90
|
+
ontext: () => {
|
|
91
|
+
},
|
|
92
|
+
onprocessinginstruction: () => {
|
|
93
|
+
},
|
|
94
|
+
onsgmldeclaration: () => {
|
|
95
|
+
},
|
|
96
|
+
ondoctype: () => {
|
|
97
|
+
},
|
|
98
|
+
oncomment: () => {
|
|
99
|
+
},
|
|
100
|
+
onopentagstart: () => {
|
|
101
|
+
},
|
|
102
|
+
onattribute: () => {
|
|
103
|
+
},
|
|
104
|
+
onopentag: () => {
|
|
105
|
+
},
|
|
106
|
+
onclosetag: () => {
|
|
107
|
+
},
|
|
108
|
+
onopencdata: () => {
|
|
109
|
+
},
|
|
110
|
+
oncdata: () => {
|
|
111
|
+
},
|
|
112
|
+
onclosecdata: () => {
|
|
113
|
+
},
|
|
114
|
+
onerror: () => {
|
|
115
|
+
},
|
|
116
|
+
onend: () => {
|
|
117
|
+
},
|
|
118
|
+
onready: () => {
|
|
119
|
+
},
|
|
120
|
+
onscript: () => {
|
|
121
|
+
},
|
|
122
|
+
onopennamespace: () => {
|
|
123
|
+
},
|
|
124
|
+
onclosenamespace: () => {
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
DEFAULT_SAX_PARSER_OPTIONS = {
|
|
128
|
+
...DEFAULT_SAX_EVENTS,
|
|
129
|
+
strict: false,
|
|
130
|
+
MAX_BUFFER_LENGTH: 64 * 1024,
|
|
131
|
+
lowercase: false,
|
|
132
|
+
lowercasetags: false,
|
|
133
|
+
noscript: false,
|
|
134
|
+
strictEntities: false,
|
|
135
|
+
preserveCase: false,
|
|
136
|
+
caseInsensitiveTags: false,
|
|
137
|
+
processEntities: true,
|
|
138
|
+
xmlns: void 0,
|
|
139
|
+
position: void 0,
|
|
140
|
+
trim: void 0,
|
|
141
|
+
normalize: void 0
|
|
142
|
+
};
|
|
143
|
+
EVENTS = [
|
|
144
|
+
"text",
|
|
145
|
+
"processinginstruction",
|
|
146
|
+
"sgmldeclaration",
|
|
147
|
+
"doctype",
|
|
148
|
+
"comment",
|
|
149
|
+
"opentagstart",
|
|
150
|
+
"attribute",
|
|
151
|
+
"opentag",
|
|
152
|
+
"closetag",
|
|
153
|
+
"opencdata",
|
|
154
|
+
"cdata",
|
|
155
|
+
"closecdata",
|
|
156
|
+
"error",
|
|
157
|
+
"end",
|
|
158
|
+
"ready",
|
|
159
|
+
"script",
|
|
160
|
+
"opennamespace",
|
|
161
|
+
"closenamespace"
|
|
162
|
+
];
|
|
163
|
+
BUFFERS = [
|
|
164
|
+
"comment",
|
|
165
|
+
"sgmlDecl",
|
|
166
|
+
"textNode",
|
|
167
|
+
"tagName",
|
|
168
|
+
"doctype",
|
|
169
|
+
"procInstName",
|
|
170
|
+
"procInstBody",
|
|
171
|
+
"entity",
|
|
172
|
+
"attribName",
|
|
173
|
+
"attribValue",
|
|
174
|
+
"cdata",
|
|
175
|
+
"script"
|
|
176
|
+
];
|
|
177
|
+
nameStart = /[:_A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]/;
|
|
178
|
+
nameBody = /[:_A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD\u00B7\u0300-\u036F\u203F-\u2040.\d-]/;
|
|
179
|
+
entityStart = /[#:_A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]/;
|
|
180
|
+
entityBody = /[#:_A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD\u00B7\u0300-\u036F\u203F-\u2040.\d-]/;
|
|
181
|
+
ENTITIES = {
|
|
182
|
+
amp: "&",
|
|
183
|
+
gt: ">",
|
|
184
|
+
lt: "<",
|
|
185
|
+
quot: '"',
|
|
186
|
+
apos: "'",
|
|
187
|
+
AElig: 198,
|
|
188
|
+
Aacute: 193,
|
|
189
|
+
Acirc: 194,
|
|
190
|
+
Agrave: 192,
|
|
191
|
+
Aring: 197,
|
|
192
|
+
Atilde: 195,
|
|
193
|
+
Auml: 196,
|
|
194
|
+
Ccedil: 199,
|
|
195
|
+
ETH: 208,
|
|
196
|
+
Eacute: 201,
|
|
197
|
+
Ecirc: 202,
|
|
198
|
+
Egrave: 200,
|
|
199
|
+
Euml: 203,
|
|
200
|
+
Iacute: 205,
|
|
201
|
+
Icirc: 206,
|
|
202
|
+
Igrave: 204,
|
|
203
|
+
Iuml: 207,
|
|
204
|
+
Ntilde: 209,
|
|
205
|
+
Oacute: 211,
|
|
206
|
+
Ocirc: 212,
|
|
207
|
+
Ograve: 210,
|
|
208
|
+
Oslash: 216,
|
|
209
|
+
Otilde: 213,
|
|
210
|
+
Ouml: 214,
|
|
211
|
+
THORN: 222,
|
|
212
|
+
Uacute: 218,
|
|
213
|
+
Ucirc: 219,
|
|
214
|
+
Ugrave: 217,
|
|
215
|
+
Uuml: 220,
|
|
216
|
+
Yacute: 221,
|
|
217
|
+
aacute: 225,
|
|
218
|
+
acirc: 226,
|
|
219
|
+
aelig: 230,
|
|
220
|
+
agrave: 224,
|
|
221
|
+
aring: 229,
|
|
222
|
+
atilde: 227,
|
|
223
|
+
auml: 228,
|
|
224
|
+
ccedil: 231,
|
|
225
|
+
eacute: 233,
|
|
226
|
+
ecirc: 234,
|
|
227
|
+
egrave: 232,
|
|
228
|
+
eth: 240,
|
|
229
|
+
euml: 235,
|
|
230
|
+
iacute: 237,
|
|
231
|
+
icirc: 238,
|
|
232
|
+
igrave: 236,
|
|
233
|
+
iuml: 239,
|
|
234
|
+
ntilde: 241,
|
|
235
|
+
oacute: 243,
|
|
236
|
+
ocirc: 244,
|
|
237
|
+
ograve: 242,
|
|
238
|
+
oslash: 248,
|
|
239
|
+
otilde: 245,
|
|
240
|
+
ouml: 246,
|
|
241
|
+
szlig: 223,
|
|
242
|
+
thorn: 254,
|
|
243
|
+
uacute: 250,
|
|
244
|
+
ucirc: 251,
|
|
245
|
+
ugrave: 249,
|
|
246
|
+
uuml: 252,
|
|
247
|
+
yacute: 253,
|
|
248
|
+
yuml: 255,
|
|
249
|
+
copy: 169,
|
|
250
|
+
reg: 174,
|
|
251
|
+
nbsp: 160,
|
|
252
|
+
iexcl: 161,
|
|
253
|
+
cent: 162,
|
|
254
|
+
pound: 163,
|
|
255
|
+
curren: 164,
|
|
256
|
+
yen: 165,
|
|
257
|
+
brvbar: 166,
|
|
258
|
+
sect: 167,
|
|
259
|
+
uml: 168,
|
|
260
|
+
ordf: 170,
|
|
261
|
+
laquo: 171,
|
|
262
|
+
not: 172,
|
|
263
|
+
shy: 173,
|
|
264
|
+
macr: 175,
|
|
265
|
+
deg: 176,
|
|
266
|
+
plusmn: 177,
|
|
267
|
+
sup1: 185,
|
|
268
|
+
sup2: 178,
|
|
269
|
+
sup3: 179,
|
|
270
|
+
acute: 180,
|
|
271
|
+
micro: 181,
|
|
272
|
+
para: 182,
|
|
273
|
+
middot: 183,
|
|
274
|
+
cedil: 184,
|
|
275
|
+
ordm: 186,
|
|
276
|
+
raquo: 187,
|
|
277
|
+
frac14: 188,
|
|
278
|
+
frac12: 189,
|
|
279
|
+
frac34: 190,
|
|
280
|
+
iquest: 191,
|
|
281
|
+
times: 215,
|
|
282
|
+
divide: 247,
|
|
283
|
+
OElig: 338,
|
|
284
|
+
oelig: 339,
|
|
285
|
+
Scaron: 352,
|
|
286
|
+
scaron: 353,
|
|
287
|
+
Yuml: 376,
|
|
288
|
+
fnof: 402,
|
|
289
|
+
circ: 710,
|
|
290
|
+
tilde: 732,
|
|
291
|
+
Alpha: 913,
|
|
292
|
+
Beta: 914,
|
|
293
|
+
Gamma: 915,
|
|
294
|
+
Delta: 916,
|
|
295
|
+
Epsilon: 917,
|
|
296
|
+
Zeta: 918,
|
|
297
|
+
Eta: 919,
|
|
298
|
+
Theta: 920,
|
|
299
|
+
Iota: 921,
|
|
300
|
+
Kappa: 922,
|
|
301
|
+
Lambda: 923,
|
|
302
|
+
Mu: 924,
|
|
303
|
+
Nu: 925,
|
|
304
|
+
Xi: 926,
|
|
305
|
+
Omicron: 927,
|
|
306
|
+
Pi: 928,
|
|
307
|
+
Rho: 929,
|
|
308
|
+
Sigma: 931,
|
|
309
|
+
Tau: 932,
|
|
310
|
+
Upsilon: 933,
|
|
311
|
+
Phi: 934,
|
|
312
|
+
Chi: 935,
|
|
313
|
+
Psi: 936,
|
|
314
|
+
Omega: 937,
|
|
315
|
+
alpha: 945,
|
|
316
|
+
beta: 946,
|
|
317
|
+
gamma: 947,
|
|
318
|
+
delta: 948,
|
|
319
|
+
epsilon: 949,
|
|
320
|
+
zeta: 950,
|
|
321
|
+
eta: 951,
|
|
322
|
+
theta: 952,
|
|
323
|
+
iota: 953,
|
|
324
|
+
kappa: 954,
|
|
325
|
+
lambda: 955,
|
|
326
|
+
mu: 956,
|
|
327
|
+
nu: 957,
|
|
328
|
+
xi: 958,
|
|
329
|
+
omicron: 959,
|
|
330
|
+
pi: 960,
|
|
331
|
+
rho: 961,
|
|
332
|
+
sigmaf: 962,
|
|
333
|
+
sigma: 963,
|
|
334
|
+
tau: 964,
|
|
335
|
+
upsilon: 965,
|
|
336
|
+
phi: 966,
|
|
337
|
+
chi: 967,
|
|
338
|
+
psi: 968,
|
|
339
|
+
omega: 969,
|
|
340
|
+
thetasym: 977,
|
|
341
|
+
upsih: 978,
|
|
342
|
+
piv: 982,
|
|
343
|
+
ensp: 8194,
|
|
344
|
+
emsp: 8195,
|
|
345
|
+
thinsp: 8201,
|
|
346
|
+
zwnj: 8204,
|
|
347
|
+
zwj: 8205,
|
|
348
|
+
lrm: 8206,
|
|
349
|
+
rlm: 8207,
|
|
350
|
+
ndash: 8211,
|
|
351
|
+
mdash: 8212,
|
|
352
|
+
lsquo: 8216,
|
|
353
|
+
rsquo: 8217,
|
|
354
|
+
sbquo: 8218,
|
|
355
|
+
ldquo: 8220,
|
|
356
|
+
rdquo: 8221,
|
|
357
|
+
bdquo: 8222,
|
|
358
|
+
dagger: 8224,
|
|
359
|
+
Dagger: 8225,
|
|
360
|
+
bull: 8226,
|
|
361
|
+
hellip: 8230,
|
|
362
|
+
permil: 8240,
|
|
363
|
+
prime: 8242,
|
|
364
|
+
Prime: 8243,
|
|
365
|
+
lsaquo: 8249,
|
|
366
|
+
rsaquo: 8250,
|
|
367
|
+
oline: 8254,
|
|
368
|
+
frasl: 8260,
|
|
369
|
+
euro: 8364,
|
|
370
|
+
image: 8465,
|
|
371
|
+
weierp: 8472,
|
|
372
|
+
real: 8476,
|
|
373
|
+
trade: 8482,
|
|
374
|
+
alefsym: 8501,
|
|
375
|
+
larr: 8592,
|
|
376
|
+
uarr: 8593,
|
|
377
|
+
rarr: 8594,
|
|
378
|
+
darr: 8595,
|
|
379
|
+
harr: 8596,
|
|
380
|
+
crarr: 8629,
|
|
381
|
+
lArr: 8656,
|
|
382
|
+
uArr: 8657,
|
|
383
|
+
rArr: 8658,
|
|
384
|
+
dArr: 8659,
|
|
385
|
+
hArr: 8660,
|
|
386
|
+
forall: 8704,
|
|
387
|
+
part: 8706,
|
|
388
|
+
exist: 8707,
|
|
389
|
+
empty: 8709,
|
|
390
|
+
nabla: 8711,
|
|
391
|
+
isin: 8712,
|
|
392
|
+
notin: 8713,
|
|
393
|
+
ni: 8715,
|
|
394
|
+
prod: 8719,
|
|
395
|
+
sum: 8721,
|
|
396
|
+
minus: 8722,
|
|
397
|
+
lowast: 8727,
|
|
398
|
+
radic: 8730,
|
|
399
|
+
prop: 8733,
|
|
400
|
+
infin: 8734,
|
|
401
|
+
ang: 8736,
|
|
402
|
+
and: 8743,
|
|
403
|
+
or: 8744,
|
|
404
|
+
cap: 8745,
|
|
405
|
+
cup: 8746,
|
|
406
|
+
int: 8747,
|
|
407
|
+
there4: 8756,
|
|
408
|
+
sim: 8764,
|
|
409
|
+
cong: 8773,
|
|
410
|
+
asymp: 8776,
|
|
411
|
+
ne: 8800,
|
|
412
|
+
equiv: 8801,
|
|
413
|
+
le: 8804,
|
|
414
|
+
ge: 8805,
|
|
415
|
+
sub: 8834,
|
|
416
|
+
sup: 8835,
|
|
417
|
+
nsub: 8836,
|
|
418
|
+
sube: 8838,
|
|
419
|
+
supe: 8839,
|
|
420
|
+
oplus: 8853,
|
|
421
|
+
otimes: 8855,
|
|
422
|
+
perp: 8869,
|
|
423
|
+
sdot: 8901,
|
|
424
|
+
lceil: 8968,
|
|
425
|
+
rceil: 8969,
|
|
426
|
+
lfloor: 8970,
|
|
427
|
+
rfloor: 8971,
|
|
428
|
+
lang: 9001,
|
|
429
|
+
rang: 9002,
|
|
430
|
+
loz: 9674,
|
|
431
|
+
spades: 9824,
|
|
432
|
+
clubs: 9827,
|
|
433
|
+
hearts: 9829,
|
|
434
|
+
diams: 9830
|
|
435
|
+
};
|
|
436
|
+
Object.keys(ENTITIES).forEach((key) => {
|
|
437
|
+
const e = ENTITIES[key];
|
|
438
|
+
ENTITIES[key] = typeof e === "number" ? String.fromCharCode(e) : e;
|
|
439
|
+
});
|
|
440
|
+
SAX = class _SAX {
|
|
441
|
+
EVENTS = EVENTS;
|
|
442
|
+
ENTITIES = {
|
|
443
|
+
// TODO: make it readonly, needed for entity-mega test
|
|
444
|
+
// amp, gt, lt, quot and apos are resolved to strings instead of numerical
|
|
445
|
+
// codes, IDK why
|
|
446
|
+
...ENTITIES
|
|
447
|
+
};
|
|
448
|
+
XML_ENTITIES = {
|
|
449
|
+
amp: "&",
|
|
450
|
+
gt: ">",
|
|
451
|
+
lt: "<",
|
|
452
|
+
quot: '"',
|
|
453
|
+
apos: "'"
|
|
454
|
+
};
|
|
455
|
+
S = 0;
|
|
456
|
+
opt;
|
|
457
|
+
trackPosition = false;
|
|
458
|
+
column = 0;
|
|
459
|
+
line = 0;
|
|
460
|
+
c = "";
|
|
461
|
+
error;
|
|
462
|
+
q = "";
|
|
463
|
+
bufferCheckPosition;
|
|
464
|
+
closed = false;
|
|
465
|
+
tags = [];
|
|
466
|
+
looseCase = "";
|
|
467
|
+
closedRoot = false;
|
|
468
|
+
sawRoot = false;
|
|
469
|
+
strict = false;
|
|
470
|
+
tag;
|
|
471
|
+
strictEntities;
|
|
472
|
+
state;
|
|
473
|
+
noscript = false;
|
|
474
|
+
attribList = [];
|
|
475
|
+
ns;
|
|
476
|
+
position = 0;
|
|
477
|
+
STATE = {
|
|
478
|
+
BEGIN: this.S++,
|
|
479
|
+
// leading byte order mark or whitespace
|
|
480
|
+
BEGIN_WHITESPACE: this.S++,
|
|
481
|
+
// leading whitespace
|
|
482
|
+
TEXT: this.S++,
|
|
483
|
+
// general stuff
|
|
484
|
+
TEXT_ENTITY: this.S++,
|
|
485
|
+
// & and such.
|
|
486
|
+
OPEN_WAKA: this.S++,
|
|
487
|
+
// <
|
|
488
|
+
SGML_DECL: this.S++,
|
|
489
|
+
// <!BLARG
|
|
490
|
+
SGML_DECL_QUOTED: this.S++,
|
|
491
|
+
// <!BLARG foo "bar
|
|
492
|
+
DOCTYPE: this.S++,
|
|
493
|
+
// <!DOCTYPE
|
|
494
|
+
DOCTYPE_QUOTED: this.S++,
|
|
495
|
+
// <!DOCTYPE "//blah
|
|
496
|
+
DOCTYPE_DTD: this.S++,
|
|
497
|
+
// <!DOCTYPE "//blah" [ ...
|
|
498
|
+
DOCTYPE_DTD_QUOTED: this.S++,
|
|
499
|
+
// <!DOCTYPE "//blah" [ "foo
|
|
500
|
+
COMMENT_STARTING: this.S++,
|
|
501
|
+
// <!-
|
|
502
|
+
COMMENT: this.S++,
|
|
503
|
+
// <!--
|
|
504
|
+
COMMENT_ENDING: this.S++,
|
|
505
|
+
// <!-- blah -
|
|
506
|
+
COMMENT_ENDED: this.S++,
|
|
507
|
+
// <!-- blah --
|
|
508
|
+
CDATA: this.S++,
|
|
509
|
+
// <![CDATA[ something
|
|
510
|
+
CDATA_ENDING: this.S++,
|
|
511
|
+
// ]
|
|
512
|
+
CDATA_ENDING_2: this.S++,
|
|
513
|
+
// ]]
|
|
514
|
+
PROC_INST: this.S++,
|
|
515
|
+
// <?hi
|
|
516
|
+
PROC_INST_BODY: this.S++,
|
|
517
|
+
// <?hi there
|
|
518
|
+
PROC_INST_ENDING: this.S++,
|
|
519
|
+
// <?hi "there" ?
|
|
520
|
+
OPEN_TAG: this.S++,
|
|
521
|
+
// <strong
|
|
522
|
+
OPEN_TAG_SLASH: this.S++,
|
|
523
|
+
// <strong /
|
|
524
|
+
ATTRIB: this.S++,
|
|
525
|
+
// <a
|
|
526
|
+
ATTRIB_NAME: this.S++,
|
|
527
|
+
// <a foo
|
|
528
|
+
ATTRIB_NAME_SAW_WHITE: this.S++,
|
|
529
|
+
// <a foo _
|
|
530
|
+
ATTRIB_VALUE: this.S++,
|
|
531
|
+
// <a foo=
|
|
532
|
+
ATTRIB_VALUE_QUOTED: this.S++,
|
|
533
|
+
// <a foo="bar
|
|
534
|
+
ATTRIB_VALUE_CLOSED: this.S++,
|
|
535
|
+
// <a foo="bar"
|
|
536
|
+
ATTRIB_VALUE_UNQUOTED: this.S++,
|
|
537
|
+
// <a foo=bar
|
|
538
|
+
ATTRIB_VALUE_ENTITY_Q: this.S++,
|
|
539
|
+
// <foo bar="""
|
|
540
|
+
ATTRIB_VALUE_ENTITY_U: this.S++,
|
|
541
|
+
// <foo bar="
|
|
542
|
+
CLOSE_TAG: this.S++,
|
|
543
|
+
// </a
|
|
544
|
+
CLOSE_TAG_SAW_WHITE: this.S++,
|
|
545
|
+
// </a >
|
|
546
|
+
SCRIPT: this.S++,
|
|
547
|
+
// <script> ...
|
|
548
|
+
SCRIPT_ENDING: this.S++
|
|
549
|
+
// <script> ... <
|
|
550
|
+
};
|
|
551
|
+
BUFFERS = BUFFERS;
|
|
552
|
+
// private parser: (strict: boolean, opt: any) => SAXParser;
|
|
553
|
+
CDATA = "[CDATA[";
|
|
554
|
+
DOCTYPE = "DOCTYPE";
|
|
555
|
+
XML_NAMESPACE = "http://www.w3.org/XML/1998/namespace";
|
|
556
|
+
XMLNS_NAMESPACE = "http://www.w3.org/2000/xmlns/";
|
|
557
|
+
rootNS = {
|
|
558
|
+
xml: this.XML_NAMESPACE,
|
|
559
|
+
xmlns: this.XMLNS_NAMESPACE
|
|
560
|
+
};
|
|
561
|
+
comment;
|
|
562
|
+
sgmlDecl;
|
|
563
|
+
textNode = "";
|
|
564
|
+
tagName;
|
|
565
|
+
doctype;
|
|
566
|
+
procInstName;
|
|
567
|
+
procInstBody;
|
|
568
|
+
entity = "";
|
|
569
|
+
attribName;
|
|
570
|
+
attribValue;
|
|
571
|
+
cdata = "";
|
|
572
|
+
script = "";
|
|
573
|
+
startTagPosition = 0;
|
|
574
|
+
constructor() {
|
|
575
|
+
this.S = 0;
|
|
576
|
+
for (const s in this.STATE) {
|
|
577
|
+
if (this.STATE.hasOwnProperty(s)) {
|
|
578
|
+
this.STATE[this.STATE[s]] = s;
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
this.S = this.STATE;
|
|
582
|
+
}
|
|
583
|
+
static charAt(chunk, i) {
|
|
584
|
+
let result = "";
|
|
585
|
+
if (i < chunk.length) {
|
|
586
|
+
result = chunk.charAt(i);
|
|
587
|
+
}
|
|
588
|
+
return result;
|
|
589
|
+
}
|
|
590
|
+
static isWhitespace(c) {
|
|
591
|
+
return c === " " || c === "\n" || c === "\r" || c === " ";
|
|
592
|
+
}
|
|
593
|
+
static isQuote(c) {
|
|
594
|
+
return c === '"' || c === "'";
|
|
595
|
+
}
|
|
596
|
+
static isAttribEnd(c) {
|
|
597
|
+
return c === ">" || _SAX.isWhitespace(c);
|
|
598
|
+
}
|
|
599
|
+
static isMatch(regex, c) {
|
|
600
|
+
return regex.test(c);
|
|
601
|
+
}
|
|
602
|
+
static notMatch(regex, c) {
|
|
603
|
+
return !_SAX.isMatch(regex, c);
|
|
604
|
+
}
|
|
605
|
+
static qname(name, attribute) {
|
|
606
|
+
const i = name.indexOf(":");
|
|
607
|
+
const qualName = i < 0 ? ["", name] : name.split(":");
|
|
608
|
+
let prefix = qualName[0];
|
|
609
|
+
let local = qualName[1];
|
|
610
|
+
if (attribute && name === "xmlns") {
|
|
611
|
+
prefix = "xmlns";
|
|
612
|
+
local = "";
|
|
613
|
+
}
|
|
614
|
+
return { prefix, local };
|
|
615
|
+
}
|
|
616
|
+
write(chunk) {
|
|
617
|
+
if (this.error) {
|
|
618
|
+
throw this.error;
|
|
619
|
+
}
|
|
620
|
+
if (this.closed) {
|
|
621
|
+
return this.errorFunction("Cannot write after close. Assign an onready handler.");
|
|
622
|
+
}
|
|
623
|
+
if (chunk === null) {
|
|
624
|
+
return this.end();
|
|
625
|
+
}
|
|
626
|
+
if (typeof chunk === "object") {
|
|
627
|
+
chunk = chunk.toString();
|
|
628
|
+
}
|
|
629
|
+
let i = 0;
|
|
630
|
+
let c;
|
|
631
|
+
while (true) {
|
|
632
|
+
c = _SAX.charAt(chunk, i++);
|
|
633
|
+
this.c = c;
|
|
634
|
+
if (!c) {
|
|
635
|
+
break;
|
|
636
|
+
}
|
|
637
|
+
if (this.trackPosition) {
|
|
638
|
+
this.position++;
|
|
639
|
+
if (c === "\n") {
|
|
640
|
+
this.line++;
|
|
641
|
+
this.column = 0;
|
|
642
|
+
} else {
|
|
643
|
+
this.column++;
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
switch (this.state) {
|
|
647
|
+
case this.S.BEGIN:
|
|
648
|
+
this.state = this.S.BEGIN_WHITESPACE;
|
|
649
|
+
if (c === "\uFEFF") {
|
|
650
|
+
continue;
|
|
651
|
+
}
|
|
652
|
+
this.beginWhiteSpace(c);
|
|
653
|
+
continue;
|
|
654
|
+
case this.S.BEGIN_WHITESPACE:
|
|
655
|
+
this.beginWhiteSpace(c);
|
|
656
|
+
continue;
|
|
657
|
+
case this.S.TEXT:
|
|
658
|
+
if (this.sawRoot && !this.closedRoot) {
|
|
659
|
+
const starti = i - 1;
|
|
660
|
+
while (c && c !== "<" && c !== "&") {
|
|
661
|
+
c = _SAX.charAt(chunk, i++);
|
|
662
|
+
if (c && this.trackPosition) {
|
|
663
|
+
this.position++;
|
|
664
|
+
if (c === "\n") {
|
|
665
|
+
this.line++;
|
|
666
|
+
this.column = 0;
|
|
667
|
+
} else {
|
|
668
|
+
this.column++;
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
this.textNode += chunk.substring(starti, i - 1);
|
|
673
|
+
}
|
|
674
|
+
if (c === "<" && !(this.sawRoot && this.closedRoot && !this.strict)) {
|
|
675
|
+
this.state = this.S.OPEN_WAKA;
|
|
676
|
+
this.startTagPosition = this.position;
|
|
677
|
+
} else {
|
|
678
|
+
if (!_SAX.isWhitespace(c) && (!this.sawRoot || this.closedRoot)) {
|
|
679
|
+
this.strictFail("Text data outside of root node.");
|
|
680
|
+
}
|
|
681
|
+
if (c === "&") {
|
|
682
|
+
this.state = this.S.TEXT_ENTITY;
|
|
683
|
+
} else {
|
|
684
|
+
this.textNode += c;
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
continue;
|
|
688
|
+
case this.S.SCRIPT:
|
|
689
|
+
if (c === "<") {
|
|
690
|
+
this.state = this.S.SCRIPT_ENDING;
|
|
691
|
+
} else {
|
|
692
|
+
this.script += c;
|
|
693
|
+
}
|
|
694
|
+
continue;
|
|
695
|
+
case this.S.SCRIPT_ENDING:
|
|
696
|
+
if (c === "/") {
|
|
697
|
+
this.state = this.S.CLOSE_TAG;
|
|
698
|
+
} else {
|
|
699
|
+
this.script += `<${c}`;
|
|
700
|
+
this.state = this.S.SCRIPT;
|
|
701
|
+
}
|
|
702
|
+
continue;
|
|
703
|
+
case this.S.OPEN_WAKA:
|
|
704
|
+
if (c === "!") {
|
|
705
|
+
this.state = this.S.SGML_DECL;
|
|
706
|
+
this.sgmlDecl = "";
|
|
707
|
+
} else if (_SAX.isWhitespace(c)) {
|
|
708
|
+
} else if (_SAX.isMatch(nameStart, c)) {
|
|
709
|
+
this.state = this.S.OPEN_TAG;
|
|
710
|
+
this.tagName = c;
|
|
711
|
+
} else if (c === "/") {
|
|
712
|
+
this.state = this.S.CLOSE_TAG;
|
|
713
|
+
this.tagName = "";
|
|
714
|
+
} else if (c === "?") {
|
|
715
|
+
this.state = this.S.PROC_INST;
|
|
716
|
+
this.procInstName = this.procInstBody = "";
|
|
717
|
+
} else {
|
|
718
|
+
this.strictFail("Unencoded <");
|
|
719
|
+
if (this.startTagPosition + 1 < this.position) {
|
|
720
|
+
const pad = this.position - this.startTagPosition;
|
|
721
|
+
c = new Array(pad).join(" ") + c;
|
|
722
|
+
}
|
|
723
|
+
this.textNode += `<${c}`;
|
|
724
|
+
this.state = this.S.TEXT;
|
|
725
|
+
}
|
|
726
|
+
continue;
|
|
727
|
+
case this.S.SGML_DECL:
|
|
728
|
+
if ((this.sgmlDecl + c).toUpperCase() === this.CDATA) {
|
|
729
|
+
this.emitNode("onopencdata");
|
|
730
|
+
this.state = this.S.CDATA;
|
|
731
|
+
this.sgmlDecl = "";
|
|
732
|
+
this.cdata = "";
|
|
733
|
+
} else if (this.sgmlDecl + c === "--") {
|
|
734
|
+
this.state = this.S.COMMENT;
|
|
735
|
+
this.comment = "";
|
|
736
|
+
this.sgmlDecl = "";
|
|
737
|
+
} else if ((this.sgmlDecl + c).toUpperCase() === this.DOCTYPE) {
|
|
738
|
+
this.state = this.S.DOCTYPE;
|
|
739
|
+
if (this.doctype || this.sawRoot) {
|
|
740
|
+
this.strictFail("Inappropriately located doctype declaration");
|
|
741
|
+
}
|
|
742
|
+
this.doctype = "";
|
|
743
|
+
this.sgmlDecl = "";
|
|
744
|
+
} else if (c === ">") {
|
|
745
|
+
this.emitNode("onsgmldeclaration", this.sgmlDecl);
|
|
746
|
+
this.sgmlDecl = "";
|
|
747
|
+
this.state = this.S.TEXT;
|
|
748
|
+
} else if (_SAX.isQuote(c)) {
|
|
749
|
+
this.state = this.S.SGML_DECL_QUOTED;
|
|
750
|
+
this.sgmlDecl += c;
|
|
751
|
+
} else {
|
|
752
|
+
this.sgmlDecl += c;
|
|
753
|
+
}
|
|
754
|
+
continue;
|
|
755
|
+
case this.S.SGML_DECL_QUOTED:
|
|
756
|
+
if (c === this.q) {
|
|
757
|
+
this.state = this.S.SGML_DECL;
|
|
758
|
+
this.q = "";
|
|
759
|
+
}
|
|
760
|
+
this.sgmlDecl += c;
|
|
761
|
+
continue;
|
|
762
|
+
case this.S.DOCTYPE:
|
|
763
|
+
if (c === ">") {
|
|
764
|
+
this.state = this.S.TEXT;
|
|
765
|
+
this.emitNode("ondoctype", this.doctype);
|
|
766
|
+
this.doctype = true;
|
|
767
|
+
} else {
|
|
768
|
+
this.doctype += c;
|
|
769
|
+
if (c === "[") {
|
|
770
|
+
this.state = this.S.DOCTYPE_DTD;
|
|
771
|
+
} else if (_SAX.isQuote(c)) {
|
|
772
|
+
this.state = this.S.DOCTYPE_QUOTED;
|
|
773
|
+
this.q = c;
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
continue;
|
|
777
|
+
case this.S.DOCTYPE_QUOTED:
|
|
778
|
+
this.doctype += c;
|
|
779
|
+
if (c === this.q) {
|
|
780
|
+
this.q = "";
|
|
781
|
+
this.state = this.S.DOCTYPE;
|
|
782
|
+
}
|
|
783
|
+
continue;
|
|
784
|
+
case this.S.DOCTYPE_DTD:
|
|
785
|
+
this.doctype += c;
|
|
786
|
+
if (c === "]") {
|
|
787
|
+
this.state = this.S.DOCTYPE;
|
|
788
|
+
} else if (_SAX.isQuote(c)) {
|
|
789
|
+
this.state = this.S.DOCTYPE_DTD_QUOTED;
|
|
790
|
+
this.q = c;
|
|
791
|
+
}
|
|
792
|
+
continue;
|
|
793
|
+
case this.S.DOCTYPE_DTD_QUOTED:
|
|
794
|
+
this.doctype += c;
|
|
795
|
+
if (c === this.q) {
|
|
796
|
+
this.state = this.S.DOCTYPE_DTD;
|
|
797
|
+
this.q = "";
|
|
798
|
+
}
|
|
799
|
+
continue;
|
|
800
|
+
case this.S.COMMENT:
|
|
801
|
+
if (c === "-") {
|
|
802
|
+
this.state = this.S.COMMENT_ENDING;
|
|
803
|
+
} else {
|
|
804
|
+
this.comment += c;
|
|
805
|
+
}
|
|
806
|
+
continue;
|
|
807
|
+
case this.S.COMMENT_ENDING:
|
|
808
|
+
if (c === "-") {
|
|
809
|
+
this.state = this.S.COMMENT_ENDED;
|
|
810
|
+
this.comment = this.textApplyOptions(this.comment);
|
|
811
|
+
if (this.comment) {
|
|
812
|
+
this.emitNode("oncomment", this.comment);
|
|
813
|
+
}
|
|
814
|
+
this.comment = "";
|
|
815
|
+
} else {
|
|
816
|
+
this.comment += `-${c}`;
|
|
817
|
+
this.state = this.S.COMMENT;
|
|
818
|
+
}
|
|
819
|
+
continue;
|
|
820
|
+
case this.S.COMMENT_ENDED:
|
|
821
|
+
if (c !== ">") {
|
|
822
|
+
this.strictFail("Malformed comment");
|
|
823
|
+
this.comment += `--${c}`;
|
|
824
|
+
this.state = this.S.COMMENT;
|
|
825
|
+
} else {
|
|
826
|
+
this.state = this.S.TEXT;
|
|
827
|
+
}
|
|
828
|
+
continue;
|
|
829
|
+
case this.S.CDATA:
|
|
830
|
+
if (c === "]") {
|
|
831
|
+
this.state = this.S.CDATA_ENDING;
|
|
832
|
+
} else {
|
|
833
|
+
this.cdata += c;
|
|
834
|
+
}
|
|
835
|
+
continue;
|
|
836
|
+
case this.S.CDATA_ENDING:
|
|
837
|
+
if (c === "]") {
|
|
838
|
+
this.state = this.S.CDATA_ENDING_2;
|
|
839
|
+
} else {
|
|
840
|
+
this.cdata += `]${c}`;
|
|
841
|
+
this.state = this.S.CDATA;
|
|
842
|
+
}
|
|
843
|
+
continue;
|
|
844
|
+
case this.S.CDATA_ENDING_2:
|
|
845
|
+
if (c === ">") {
|
|
846
|
+
if (this.cdata) {
|
|
847
|
+
this.emitNode("oncdata", this.cdata);
|
|
848
|
+
}
|
|
849
|
+
this.emitNode("onclosecdata");
|
|
850
|
+
this.cdata = "";
|
|
851
|
+
this.state = this.S.TEXT;
|
|
852
|
+
} else if (c === "]") {
|
|
853
|
+
this.cdata += "]";
|
|
854
|
+
} else {
|
|
855
|
+
this.cdata += `]]${c}`;
|
|
856
|
+
this.state = this.S.CDATA;
|
|
857
|
+
}
|
|
858
|
+
continue;
|
|
859
|
+
case this.S.PROC_INST:
|
|
860
|
+
if (c === "?") {
|
|
861
|
+
this.state = this.S.PROC_INST_ENDING;
|
|
862
|
+
} else if (_SAX.isWhitespace(c)) {
|
|
863
|
+
this.state = this.S.PROC_INST_BODY;
|
|
864
|
+
} else {
|
|
865
|
+
this.procInstName += c;
|
|
866
|
+
}
|
|
867
|
+
continue;
|
|
868
|
+
case this.S.PROC_INST_BODY:
|
|
869
|
+
if (!this.procInstBody && _SAX.isWhitespace(c)) {
|
|
870
|
+
continue;
|
|
871
|
+
} else if (c === "?") {
|
|
872
|
+
this.state = this.S.PROC_INST_ENDING;
|
|
873
|
+
} else {
|
|
874
|
+
this.procInstBody += c;
|
|
875
|
+
}
|
|
876
|
+
continue;
|
|
877
|
+
case this.S.PROC_INST_ENDING:
|
|
878
|
+
if (c === ">") {
|
|
879
|
+
this.emitNode("onprocessinginstruction", {
|
|
880
|
+
name: this.procInstName,
|
|
881
|
+
body: this.procInstBody
|
|
882
|
+
});
|
|
883
|
+
this.procInstName = this.procInstBody = "";
|
|
884
|
+
this.state = this.S.TEXT;
|
|
885
|
+
} else {
|
|
886
|
+
this.procInstBody += `?${c}`;
|
|
887
|
+
this.state = this.S.PROC_INST_BODY;
|
|
888
|
+
}
|
|
889
|
+
continue;
|
|
890
|
+
case this.S.OPEN_TAG:
|
|
891
|
+
if (_SAX.isMatch(nameBody, c)) {
|
|
892
|
+
this.tagName += c;
|
|
893
|
+
} else {
|
|
894
|
+
this.newTag();
|
|
895
|
+
if (c === ">") {
|
|
896
|
+
this.openTag();
|
|
897
|
+
} else if (c === "/") {
|
|
898
|
+
this.state = this.S.OPEN_TAG_SLASH;
|
|
899
|
+
} else {
|
|
900
|
+
if (!_SAX.isWhitespace(c)) {
|
|
901
|
+
this.strictFail("Invalid character in tag name");
|
|
902
|
+
}
|
|
903
|
+
this.state = this.S.ATTRIB;
|
|
904
|
+
}
|
|
905
|
+
}
|
|
906
|
+
continue;
|
|
907
|
+
case this.S.OPEN_TAG_SLASH:
|
|
908
|
+
if (c === ">") {
|
|
909
|
+
this.openTag(true);
|
|
910
|
+
this.closeTag();
|
|
911
|
+
} else {
|
|
912
|
+
this.strictFail("Forward-slash in opening tag not followed by >");
|
|
913
|
+
this.state = this.S.ATTRIB;
|
|
914
|
+
}
|
|
915
|
+
continue;
|
|
916
|
+
case this.S.ATTRIB:
|
|
917
|
+
if (_SAX.isWhitespace(c)) {
|
|
918
|
+
continue;
|
|
919
|
+
} else if (c === ">") {
|
|
920
|
+
this.openTag();
|
|
921
|
+
} else if (c === "/") {
|
|
922
|
+
this.state = this.S.OPEN_TAG_SLASH;
|
|
923
|
+
} else if (_SAX.isMatch(nameStart, c)) {
|
|
924
|
+
this.attribName = c;
|
|
925
|
+
this.attribValue = "";
|
|
926
|
+
this.state = this.S.ATTRIB_NAME;
|
|
927
|
+
} else {
|
|
928
|
+
this.strictFail("Invalid attribute name");
|
|
929
|
+
}
|
|
930
|
+
continue;
|
|
931
|
+
case this.S.ATTRIB_NAME:
|
|
932
|
+
if (c === "=") {
|
|
933
|
+
this.state = this.S.ATTRIB_VALUE;
|
|
934
|
+
} else if (c === ">") {
|
|
935
|
+
this.strictFail("Attribute without value");
|
|
936
|
+
this.attribValue = this.attribName;
|
|
937
|
+
this.attrib();
|
|
938
|
+
this.openTag();
|
|
939
|
+
} else if (_SAX.isWhitespace(c)) {
|
|
940
|
+
this.state = this.S.ATTRIB_NAME_SAW_WHITE;
|
|
941
|
+
} else if (_SAX.isMatch(nameBody, c)) {
|
|
942
|
+
this.attribName += c;
|
|
943
|
+
} else {
|
|
944
|
+
this.strictFail("Invalid attribute name");
|
|
945
|
+
}
|
|
946
|
+
continue;
|
|
947
|
+
case this.S.ATTRIB_NAME_SAW_WHITE:
|
|
948
|
+
if (c === "=") {
|
|
949
|
+
this.state = this.S.ATTRIB_VALUE;
|
|
950
|
+
} else if (_SAX.isWhitespace(c)) {
|
|
951
|
+
continue;
|
|
952
|
+
} else {
|
|
953
|
+
this.strictFail("Attribute without value");
|
|
954
|
+
Object.defineProperty(this.tag.attributes, this.attribName, {
|
|
955
|
+
value: "",
|
|
956
|
+
enumerable: true,
|
|
957
|
+
configurable: true,
|
|
958
|
+
writable: true
|
|
959
|
+
});
|
|
960
|
+
this.attribValue = "";
|
|
961
|
+
this.emitNode("onattribute", {
|
|
962
|
+
name: this.attribName,
|
|
963
|
+
value: ""
|
|
964
|
+
});
|
|
965
|
+
this.attribName = "";
|
|
966
|
+
if (c === ">") {
|
|
967
|
+
this.openTag();
|
|
968
|
+
} else if (_SAX.isMatch(nameStart, c)) {
|
|
969
|
+
this.attribName = c;
|
|
970
|
+
this.state = this.S.ATTRIB_NAME;
|
|
971
|
+
} else {
|
|
972
|
+
this.strictFail("Invalid attribute name");
|
|
973
|
+
this.state = this.S.ATTRIB;
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
continue;
|
|
977
|
+
case this.S.ATTRIB_VALUE:
|
|
978
|
+
if (_SAX.isWhitespace(c)) {
|
|
979
|
+
continue;
|
|
980
|
+
} else if (_SAX.isQuote(c)) {
|
|
981
|
+
this.q = c;
|
|
982
|
+
this.state = this.S.ATTRIB_VALUE_QUOTED;
|
|
983
|
+
} else {
|
|
984
|
+
this.strictFail("Unquoted attribute value");
|
|
985
|
+
this.state = this.S.ATTRIB_VALUE_UNQUOTED;
|
|
986
|
+
this.attribValue = c;
|
|
987
|
+
}
|
|
988
|
+
continue;
|
|
989
|
+
case this.S.ATTRIB_VALUE_QUOTED:
|
|
990
|
+
if (c !== this.q) {
|
|
991
|
+
if (c === "&") {
|
|
992
|
+
this.state = this.S.ATTRIB_VALUE_ENTITY_Q;
|
|
993
|
+
} else {
|
|
994
|
+
this.attribValue += c;
|
|
995
|
+
}
|
|
996
|
+
continue;
|
|
997
|
+
}
|
|
998
|
+
this.attrib();
|
|
999
|
+
this.q = "";
|
|
1000
|
+
this.state = this.S.ATTRIB_VALUE_CLOSED;
|
|
1001
|
+
continue;
|
|
1002
|
+
case this.S.ATTRIB_VALUE_CLOSED:
|
|
1003
|
+
if (_SAX.isWhitespace(c)) {
|
|
1004
|
+
this.state = this.S.ATTRIB;
|
|
1005
|
+
} else if (c === ">") {
|
|
1006
|
+
this.openTag();
|
|
1007
|
+
} else if (c === "/") {
|
|
1008
|
+
this.state = this.S.OPEN_TAG_SLASH;
|
|
1009
|
+
} else if (_SAX.isMatch(nameStart, c)) {
|
|
1010
|
+
this.strictFail("No whitespace between attributes");
|
|
1011
|
+
this.attribName = c;
|
|
1012
|
+
this.attribValue = "";
|
|
1013
|
+
this.state = this.S.ATTRIB_NAME;
|
|
1014
|
+
} else {
|
|
1015
|
+
this.strictFail("Invalid attribute name");
|
|
1016
|
+
}
|
|
1017
|
+
continue;
|
|
1018
|
+
case this.S.ATTRIB_VALUE_UNQUOTED:
|
|
1019
|
+
if (!_SAX.isAttribEnd(c)) {
|
|
1020
|
+
if (c === "&") {
|
|
1021
|
+
this.state = this.S.ATTRIB_VALUE_ENTITY_U;
|
|
1022
|
+
} else {
|
|
1023
|
+
this.attribValue += c;
|
|
1024
|
+
}
|
|
1025
|
+
continue;
|
|
1026
|
+
}
|
|
1027
|
+
this.attrib();
|
|
1028
|
+
if (c === ">") {
|
|
1029
|
+
this.openTag();
|
|
1030
|
+
} else {
|
|
1031
|
+
this.state = this.S.ATTRIB;
|
|
1032
|
+
}
|
|
1033
|
+
continue;
|
|
1034
|
+
case this.S.CLOSE_TAG:
|
|
1035
|
+
if (!this.tagName) {
|
|
1036
|
+
if (_SAX.isWhitespace(c)) {
|
|
1037
|
+
continue;
|
|
1038
|
+
} else if (_SAX.notMatch(nameStart, c)) {
|
|
1039
|
+
if (this.script) {
|
|
1040
|
+
this.script += `</${c}`;
|
|
1041
|
+
this.state = this.S.SCRIPT;
|
|
1042
|
+
} else {
|
|
1043
|
+
this.strictFail("Invalid tagname in closing tag.");
|
|
1044
|
+
}
|
|
1045
|
+
} else {
|
|
1046
|
+
this.tagName = c;
|
|
1047
|
+
}
|
|
1048
|
+
} else if (c === ">") {
|
|
1049
|
+
this.closeTag();
|
|
1050
|
+
} else if (_SAX.isMatch(nameBody, c)) {
|
|
1051
|
+
this.tagName += c;
|
|
1052
|
+
} else if (this.script) {
|
|
1053
|
+
this.script += `</${this.tagName}`;
|
|
1054
|
+
this.tagName = "";
|
|
1055
|
+
this.state = this.S.SCRIPT;
|
|
1056
|
+
} else {
|
|
1057
|
+
if (!_SAX.isWhitespace(c)) {
|
|
1058
|
+
this.strictFail("Invalid tagname in closing tag");
|
|
1059
|
+
}
|
|
1060
|
+
this.state = this.S.CLOSE_TAG_SAW_WHITE;
|
|
1061
|
+
}
|
|
1062
|
+
continue;
|
|
1063
|
+
case this.S.CLOSE_TAG_SAW_WHITE:
|
|
1064
|
+
if (_SAX.isWhitespace(c)) {
|
|
1065
|
+
continue;
|
|
1066
|
+
}
|
|
1067
|
+
if (c === ">") {
|
|
1068
|
+
this.closeTag();
|
|
1069
|
+
} else {
|
|
1070
|
+
this.strictFail("Invalid characters in closing tag");
|
|
1071
|
+
}
|
|
1072
|
+
continue;
|
|
1073
|
+
case this.S.TEXT_ENTITY:
|
|
1074
|
+
case this.S.ATTRIB_VALUE_ENTITY_Q:
|
|
1075
|
+
case this.S.ATTRIB_VALUE_ENTITY_U:
|
|
1076
|
+
let returnState;
|
|
1077
|
+
let buffer;
|
|
1078
|
+
switch (this.state) {
|
|
1079
|
+
case this.S.TEXT_ENTITY:
|
|
1080
|
+
returnState = this.S.TEXT;
|
|
1081
|
+
buffer = "textNode";
|
|
1082
|
+
break;
|
|
1083
|
+
case this.S.ATTRIB_VALUE_ENTITY_Q:
|
|
1084
|
+
returnState = this.S.ATTRIB_VALUE_QUOTED;
|
|
1085
|
+
buffer = "attribValue";
|
|
1086
|
+
break;
|
|
1087
|
+
case this.S.ATTRIB_VALUE_ENTITY_U:
|
|
1088
|
+
returnState = this.S.ATTRIB_VALUE_UNQUOTED;
|
|
1089
|
+
buffer = "attribValue";
|
|
1090
|
+
break;
|
|
1091
|
+
default:
|
|
1092
|
+
throw new Error(`Unknown state: ${this.state}`);
|
|
1093
|
+
}
|
|
1094
|
+
if (c === ";") {
|
|
1095
|
+
this[buffer] += this.parseEntity();
|
|
1096
|
+
this.entity = "";
|
|
1097
|
+
this.state = returnState;
|
|
1098
|
+
} else if (_SAX.isMatch(this.entity.length ? entityBody : entityStart, c)) {
|
|
1099
|
+
this.entity += c;
|
|
1100
|
+
} else {
|
|
1101
|
+
this.strictFail("Invalid character in entity name");
|
|
1102
|
+
this[buffer] += `&${this.entity}${c}`;
|
|
1103
|
+
this.entity = "";
|
|
1104
|
+
this.state = returnState;
|
|
1105
|
+
}
|
|
1106
|
+
continue;
|
|
1107
|
+
default:
|
|
1108
|
+
throw new Error(`Unknown state: ${this.state}`);
|
|
1109
|
+
}
|
|
1110
|
+
}
|
|
1111
|
+
if (this.position >= this.bufferCheckPosition) {
|
|
1112
|
+
this.checkBufferLength();
|
|
1113
|
+
}
|
|
1114
|
+
return this;
|
|
1115
|
+
}
|
|
1116
|
+
emit(event, data) {
|
|
1117
|
+
if (this.events.hasOwnProperty(event)) {
|
|
1118
|
+
const eventName = event.replace(/^on/, "");
|
|
1119
|
+
this.events[event](data, eventName, this);
|
|
1120
|
+
}
|
|
1121
|
+
}
|
|
1122
|
+
clearBuffers() {
|
|
1123
|
+
for (let i = 0, l = this.BUFFERS.length; i < l; i++) {
|
|
1124
|
+
this[this[i]] = "";
|
|
1125
|
+
}
|
|
1126
|
+
}
|
|
1127
|
+
flushBuffers() {
|
|
1128
|
+
this.closeText();
|
|
1129
|
+
if (this.cdata !== "") {
|
|
1130
|
+
this.emitNode("oncdata", this.cdata);
|
|
1131
|
+
this.cdata = "";
|
|
1132
|
+
}
|
|
1133
|
+
if (this.script !== "") {
|
|
1134
|
+
this.emitNode("onscript", this.script);
|
|
1135
|
+
this.script = "";
|
|
1136
|
+
}
|
|
1137
|
+
}
|
|
1138
|
+
end() {
|
|
1139
|
+
if (this.sawRoot && !this.closedRoot) this.strictFail("Unclosed root tag");
|
|
1140
|
+
if (this.strict && this.state !== this.S.BEGIN && this.state !== this.S.BEGIN_WHITESPACE && this.state !== this.S.TEXT) {
|
|
1141
|
+
this.errorFunction("Unexpected end");
|
|
1142
|
+
}
|
|
1143
|
+
this.closeText();
|
|
1144
|
+
this.c = "";
|
|
1145
|
+
this.closed = true;
|
|
1146
|
+
this.emit("onend");
|
|
1147
|
+
return new SAXParser(this.opt);
|
|
1148
|
+
}
|
|
1149
|
+
errorFunction(er) {
|
|
1150
|
+
this.closeText();
|
|
1151
|
+
if (this.trackPosition) {
|
|
1152
|
+
er += `
|
|
1153
|
+
Line: ${this.line}
|
|
1154
|
+
Column: ${this.column}
|
|
1155
|
+
Char: ${this.c}`;
|
|
1156
|
+
}
|
|
1157
|
+
const error = new Error(er);
|
|
1158
|
+
this.error = error;
|
|
1159
|
+
this.emit("onerror", error);
|
|
1160
|
+
return this;
|
|
1161
|
+
}
|
|
1162
|
+
attrib() {
|
|
1163
|
+
if (!this.strict && !this.opt.preserveCase) {
|
|
1164
|
+
this.attribName = this.attribName[this.looseCase]();
|
|
1165
|
+
}
|
|
1166
|
+
if (this.attribList.indexOf(this.attribName) !== -1 || Object.hasOwn(this.tag.attributes, this.attribName)) {
|
|
1167
|
+
this.strictFail("Duplicate attribute");
|
|
1168
|
+
this.attribName = this.attribValue = "";
|
|
1169
|
+
return;
|
|
1170
|
+
}
|
|
1171
|
+
if (this.opt.xmlns) {
|
|
1172
|
+
const qn = _SAX.qname(this.attribName, true);
|
|
1173
|
+
const prefix = qn.prefix;
|
|
1174
|
+
const local = qn.local;
|
|
1175
|
+
if (prefix === "xmlns") {
|
|
1176
|
+
if (local === "xml" && this.attribValue !== this.XML_NAMESPACE) {
|
|
1177
|
+
this.strictFail(
|
|
1178
|
+
`xml: prefix must be bound to ${this.XML_NAMESPACE}
|
|
1179
|
+
Actual: ${this.attribValue}`
|
|
1180
|
+
);
|
|
1181
|
+
} else if (local === "xmlns" && this.attribValue !== this.XMLNS_NAMESPACE) {
|
|
1182
|
+
this.strictFail(
|
|
1183
|
+
`xmlns: prefix must be bound to ${this.XMLNS_NAMESPACE}
|
|
1184
|
+
Actual: ${this.attribValue}`
|
|
1185
|
+
);
|
|
1186
|
+
} else {
|
|
1187
|
+
const tag = this.tag;
|
|
1188
|
+
const parent = this.tags[this.tags.length - 1] || this;
|
|
1189
|
+
if (tag.ns === parent.ns) {
|
|
1190
|
+
tag.ns = Object.create(parent.ns);
|
|
1191
|
+
}
|
|
1192
|
+
tag.ns[local] = this.attribValue;
|
|
1193
|
+
}
|
|
1194
|
+
}
|
|
1195
|
+
this.attribList.push([this.attribName, this.attribValue]);
|
|
1196
|
+
} else {
|
|
1197
|
+
Object.defineProperty(this.tag.attributes, this.attribName, {
|
|
1198
|
+
value: this.attribValue,
|
|
1199
|
+
enumerable: true,
|
|
1200
|
+
configurable: true,
|
|
1201
|
+
writable: true
|
|
1202
|
+
});
|
|
1203
|
+
this.emitNode("onattribute", {
|
|
1204
|
+
name: this.attribName,
|
|
1205
|
+
value: this.attribValue
|
|
1206
|
+
});
|
|
1207
|
+
}
|
|
1208
|
+
this.attribName = this.attribValue = "";
|
|
1209
|
+
}
|
|
1210
|
+
newTag() {
|
|
1211
|
+
if (!this.strict && !this.opt.preserveCase) this.tagName = this.tagName[this.looseCase]();
|
|
1212
|
+
const parent = this.tags[this.tags.length - 1] || this;
|
|
1213
|
+
const tag = { name: this.tagName, attributes: {} };
|
|
1214
|
+
this.tag = tag;
|
|
1215
|
+
if (this.opt.xmlns) {
|
|
1216
|
+
tag.ns = parent.ns;
|
|
1217
|
+
}
|
|
1218
|
+
this.attribList.length = 0;
|
|
1219
|
+
this.emitNode("onopentagstart", tag);
|
|
1220
|
+
}
|
|
1221
|
+
/** Validates an entity before optionally preserving its authored spelling. */
|
|
1222
|
+
parseEntity() {
|
|
1223
|
+
const decoded = this.decodeEntity();
|
|
1224
|
+
return this.opt.processEntities === false ? `&${this.entity};` : decoded;
|
|
1225
|
+
}
|
|
1226
|
+
/** Decodes predefined, configured, or numeric entities without resolving document types. */
|
|
1227
|
+
decodeEntity() {
|
|
1228
|
+
let entity = this.entity;
|
|
1229
|
+
const entityLC = entity.toLowerCase();
|
|
1230
|
+
let num = NaN;
|
|
1231
|
+
let numStr = "";
|
|
1232
|
+
if (typeof this.ENTITIES[entity] === "string" || typeof this.ENTITIES[entity] === "number") {
|
|
1233
|
+
return this.ENTITIES[entity];
|
|
1234
|
+
}
|
|
1235
|
+
if (typeof this.ENTITIES[entityLC] === "string" || typeof this.ENTITIES[entityLC] === "number") {
|
|
1236
|
+
return this.ENTITIES[entityLC];
|
|
1237
|
+
}
|
|
1238
|
+
entity = entityLC;
|
|
1239
|
+
if (entity.charAt(0) === "#") {
|
|
1240
|
+
if (entity.charAt(1) === "x") {
|
|
1241
|
+
entity = entity.slice(2);
|
|
1242
|
+
num = parseInt(entity, 16);
|
|
1243
|
+
numStr = num.toString(16);
|
|
1244
|
+
} else {
|
|
1245
|
+
entity = entity.slice(1);
|
|
1246
|
+
num = parseInt(entity, 10);
|
|
1247
|
+
numStr = num.toString(10);
|
|
1248
|
+
}
|
|
1249
|
+
}
|
|
1250
|
+
entity = entity.replace(/^0+/, "");
|
|
1251
|
+
if (Number.isNaN(num) || numStr.toLowerCase() !== entity) {
|
|
1252
|
+
this.strictFail("Invalid character entity");
|
|
1253
|
+
return `&${this.entity};`;
|
|
1254
|
+
}
|
|
1255
|
+
if (num < 0 || num > 1114111 || num >= 55296 && num <= 57343 || num < 32 && ![9, 10, 13].includes(num) || num === 65534 || num === 65535) {
|
|
1256
|
+
this.strictFail("Invalid character entity");
|
|
1257
|
+
return `&${this.entity};`;
|
|
1258
|
+
}
|
|
1259
|
+
return String.fromCodePoint(num);
|
|
1260
|
+
}
|
|
1261
|
+
beginWhiteSpace(c) {
|
|
1262
|
+
if (c === "<") {
|
|
1263
|
+
this.state = this.S.OPEN_WAKA;
|
|
1264
|
+
this.startTagPosition = this.position;
|
|
1265
|
+
} else if (!_SAX.isWhitespace(c)) {
|
|
1266
|
+
this.strictFail("Non-whitespace before first tag.");
|
|
1267
|
+
this.textNode = c;
|
|
1268
|
+
this.state = this.S.TEXT;
|
|
1269
|
+
} else {
|
|
1270
|
+
}
|
|
1271
|
+
}
|
|
1272
|
+
strictFail(message) {
|
|
1273
|
+
if (typeof this !== "object" || !(this instanceof SAXParser)) {
|
|
1274
|
+
throw new Error("bad call to strictFail");
|
|
1275
|
+
}
|
|
1276
|
+
if (this.strict) {
|
|
1277
|
+
this.errorFunction(message);
|
|
1278
|
+
}
|
|
1279
|
+
}
|
|
1280
|
+
textApplyOptions(text) {
|
|
1281
|
+
if (this.opt.trim) text = text.trim();
|
|
1282
|
+
if (this.opt.normalize) text = text.replace(/\s+/g, " ");
|
|
1283
|
+
return text;
|
|
1284
|
+
}
|
|
1285
|
+
emitNode(nodeType, data) {
|
|
1286
|
+
if (this.textNode) this.closeText();
|
|
1287
|
+
this.emit(nodeType, data);
|
|
1288
|
+
}
|
|
1289
|
+
closeText() {
|
|
1290
|
+
this.textNode = this.textApplyOptions(this.textNode);
|
|
1291
|
+
if (this.textNode !== void 0 && this.textNode !== "" && this.textNode !== "undefined") {
|
|
1292
|
+
this.emit("ontext", this.textNode);
|
|
1293
|
+
}
|
|
1294
|
+
this.textNode = "";
|
|
1295
|
+
}
|
|
1296
|
+
checkBufferLength() {
|
|
1297
|
+
var _a;
|
|
1298
|
+
const maxAllowed = Math.max(this.opt.MAX_BUFFER_LENGTH, 10);
|
|
1299
|
+
let maxActual = 0;
|
|
1300
|
+
for (let i = 0, l = this.BUFFERS.length; i < l; i++) {
|
|
1301
|
+
const len = ((_a = this[this.BUFFERS[i]]) == null ? void 0 : _a.length) || 0;
|
|
1302
|
+
if (len > maxAllowed) {
|
|
1303
|
+
switch (this.BUFFERS[i]) {
|
|
1304
|
+
case "textNode":
|
|
1305
|
+
this.closeText();
|
|
1306
|
+
break;
|
|
1307
|
+
case "cdata":
|
|
1308
|
+
this.emitNode("oncdata", this.cdata);
|
|
1309
|
+
this.cdata = "";
|
|
1310
|
+
break;
|
|
1311
|
+
case "script":
|
|
1312
|
+
this.emitNode("onscript", this.script);
|
|
1313
|
+
this.script = "";
|
|
1314
|
+
break;
|
|
1315
|
+
default:
|
|
1316
|
+
this.errorFunction(`Max buffer length exceeded: ${this.BUFFERS[i]}`);
|
|
1317
|
+
}
|
|
1318
|
+
}
|
|
1319
|
+
maxActual = Math.max(maxActual, len);
|
|
1320
|
+
}
|
|
1321
|
+
const m = this.opt.MAX_BUFFER_LENGTH - maxActual;
|
|
1322
|
+
this.bufferCheckPosition = m + this.position;
|
|
1323
|
+
}
|
|
1324
|
+
openTag(selfClosing) {
|
|
1325
|
+
if (this.opt.xmlns) {
|
|
1326
|
+
const tag = this.tag;
|
|
1327
|
+
const qn = _SAX.qname(this.tagName);
|
|
1328
|
+
tag.prefix = qn.prefix;
|
|
1329
|
+
tag.local = qn.local;
|
|
1330
|
+
tag.uri = tag.ns[qn.prefix] || "";
|
|
1331
|
+
if (tag.prefix && !tag.uri) {
|
|
1332
|
+
this.strictFail(`Unbound namespace prefix: ${JSON.stringify(this.tagName)}`);
|
|
1333
|
+
tag.uri = qn.prefix;
|
|
1334
|
+
}
|
|
1335
|
+
const parent = this.tags[this.tags.length - 1] || this;
|
|
1336
|
+
if (tag.ns && parent.ns !== tag.ns) {
|
|
1337
|
+
const that = this;
|
|
1338
|
+
Object.keys(tag.ns).forEach((p) => {
|
|
1339
|
+
that.emitNode("onopennamespace", {
|
|
1340
|
+
prefix: p,
|
|
1341
|
+
uri: tag.ns[p]
|
|
1342
|
+
});
|
|
1343
|
+
});
|
|
1344
|
+
}
|
|
1345
|
+
for (let i = 0, l = this.attribList.length; i < l; i++) {
|
|
1346
|
+
const nv = this.attribList[i];
|
|
1347
|
+
const name = nv[0];
|
|
1348
|
+
const value = nv[1];
|
|
1349
|
+
const qualName = _SAX.qname(name, true);
|
|
1350
|
+
const prefix = qualName.prefix;
|
|
1351
|
+
const local = qualName.local;
|
|
1352
|
+
const uri = prefix === "" ? "" : tag.ns[prefix] || "";
|
|
1353
|
+
const a = {
|
|
1354
|
+
name,
|
|
1355
|
+
value,
|
|
1356
|
+
prefix,
|
|
1357
|
+
local,
|
|
1358
|
+
uri
|
|
1359
|
+
};
|
|
1360
|
+
if (prefix && prefix !== "xmlns" && !uri) {
|
|
1361
|
+
this.strictFail(`Unbound namespace prefix: ${JSON.stringify(prefix)}`);
|
|
1362
|
+
a.uri = prefix;
|
|
1363
|
+
}
|
|
1364
|
+
Object.defineProperty(this.tag.attributes, name, {
|
|
1365
|
+
value: a,
|
|
1366
|
+
enumerable: true,
|
|
1367
|
+
configurable: true,
|
|
1368
|
+
writable: true
|
|
1369
|
+
});
|
|
1370
|
+
this.emitNode("onattribute", a);
|
|
1371
|
+
}
|
|
1372
|
+
this.attribList.length = 0;
|
|
1373
|
+
}
|
|
1374
|
+
this.tag.isSelfClosing = Boolean(selfClosing);
|
|
1375
|
+
this.sawRoot = true;
|
|
1376
|
+
this.tags.push(this.tag);
|
|
1377
|
+
this.emitNode("onopentag", this.tag);
|
|
1378
|
+
if (!selfClosing) {
|
|
1379
|
+
if (!this.noscript && this.tagName.toLowerCase() === "script") {
|
|
1380
|
+
this.state = this.S.SCRIPT;
|
|
1381
|
+
} else {
|
|
1382
|
+
this.state = this.S.TEXT;
|
|
1383
|
+
}
|
|
1384
|
+
this.tag = null;
|
|
1385
|
+
this.tagName = "";
|
|
1386
|
+
}
|
|
1387
|
+
this.attribName = this.attribValue = "";
|
|
1388
|
+
this.attribList.length = 0;
|
|
1389
|
+
}
|
|
1390
|
+
closeTag() {
|
|
1391
|
+
if (!this.tagName) {
|
|
1392
|
+
this.strictFail("Weird empty close tag.");
|
|
1393
|
+
this.textNode += "</>";
|
|
1394
|
+
this.state = this.S.TEXT;
|
|
1395
|
+
return;
|
|
1396
|
+
}
|
|
1397
|
+
const caseInsensitiveTags = !this.strict && (this.opt.caseInsensitiveTags || !this.opt.preserveCase);
|
|
1398
|
+
if (this.script) {
|
|
1399
|
+
const normalizedScriptCloseTag = caseInsensitiveTags ? this.tagName.toLowerCase() : this.tagName;
|
|
1400
|
+
if (normalizedScriptCloseTag !== "script") {
|
|
1401
|
+
this.script += `</${this.tagName}>`;
|
|
1402
|
+
this.tagName = "";
|
|
1403
|
+
this.state = this.S.SCRIPT;
|
|
1404
|
+
return;
|
|
1405
|
+
}
|
|
1406
|
+
this.emitNode("onscript", this.script);
|
|
1407
|
+
this.script = "";
|
|
1408
|
+
}
|
|
1409
|
+
let t = this.tags.length;
|
|
1410
|
+
let tagName = this.tagName;
|
|
1411
|
+
const normalizedTagName = caseInsensitiveTags ? tagName.toLowerCase() : tagName;
|
|
1412
|
+
while (t--) {
|
|
1413
|
+
const close = this.tags[t];
|
|
1414
|
+
const normalizedOpenTagName = caseInsensitiveTags ? close.name.toLowerCase() : close.name;
|
|
1415
|
+
if (normalizedOpenTagName !== normalizedTagName) {
|
|
1416
|
+
this.strictFail("Unexpected close tag");
|
|
1417
|
+
} else {
|
|
1418
|
+
break;
|
|
1419
|
+
}
|
|
1420
|
+
}
|
|
1421
|
+
if (t < 0) {
|
|
1422
|
+
this.strictFail(`Unmatched closing tag: ${this.tagName}`);
|
|
1423
|
+
this.textNode += `</${this.tagName}>`;
|
|
1424
|
+
this.state = this.S.TEXT;
|
|
1425
|
+
return;
|
|
1426
|
+
}
|
|
1427
|
+
this.tagName = tagName;
|
|
1428
|
+
let s = this.tags.length;
|
|
1429
|
+
while (s-- > t) {
|
|
1430
|
+
const tag = this.tags.pop();
|
|
1431
|
+
this.tag = tag;
|
|
1432
|
+
this.tagName = this.tag.name;
|
|
1433
|
+
this.emitNode("onclosetag", this.tagName);
|
|
1434
|
+
const x = {};
|
|
1435
|
+
for (const i in tag.ns) {
|
|
1436
|
+
if (tag.ns.hasOwnProperty(i)) {
|
|
1437
|
+
x[i] = tag.ns[i];
|
|
1438
|
+
}
|
|
1439
|
+
}
|
|
1440
|
+
const parent = this.tags[this.tags.length - 1] || this;
|
|
1441
|
+
if (this.opt.xmlns && tag.ns !== parent.ns) {
|
|
1442
|
+
const that = this;
|
|
1443
|
+
Object.keys(tag.ns).forEach((p) => {
|
|
1444
|
+
const n = tag.ns[p];
|
|
1445
|
+
that.emitNode("onclosenamespace", { prefix: p, uri: n });
|
|
1446
|
+
});
|
|
1447
|
+
}
|
|
1448
|
+
}
|
|
1449
|
+
if (t === 0) this.closedRoot = true;
|
|
1450
|
+
this.tagName = this.attribValue = this.attribName = "";
|
|
1451
|
+
this.attribList.length = 0;
|
|
1452
|
+
this.state = this.S.TEXT;
|
|
1453
|
+
}
|
|
1454
|
+
};
|
|
1455
|
+
SAXParser = class extends SAX {
|
|
1456
|
+
static ENTITIES = ENTITIES;
|
|
1457
|
+
opt = DEFAULT_SAX_PARSER_OPTIONS;
|
|
1458
|
+
events = DEFAULT_SAX_EVENTS;
|
|
1459
|
+
constructor(opt) {
|
|
1460
|
+
super();
|
|
1461
|
+
this.clearBuffers();
|
|
1462
|
+
this.opt = opt = { ...this.opt, ...opt };
|
|
1463
|
+
this.events = { ...this.events, ...opt };
|
|
1464
|
+
this.q = this.c = "";
|
|
1465
|
+
this.opt.lowercase = this.opt.lowercase || this.opt.lowercasetags;
|
|
1466
|
+
this.bufferCheckPosition = this.opt.MAX_BUFFER_LENGTH;
|
|
1467
|
+
this.looseCase = this.opt.lowercase ? "toLowerCase" : "toUpperCase";
|
|
1468
|
+
this.tags = [];
|
|
1469
|
+
this.closed = this.closedRoot = this.sawRoot = false;
|
|
1470
|
+
this.tag = this.error = null;
|
|
1471
|
+
this.strict = Boolean(this.opt.strict);
|
|
1472
|
+
this.noscript = Boolean(this.opt.strict || this.opt.noscript);
|
|
1473
|
+
this.state = this.S.BEGIN;
|
|
1474
|
+
this.strictEntities = this.opt.strictEntities;
|
|
1475
|
+
this.ENTITIES = this.strictEntities ? Object.create(this.XML_ENTITIES) : Object.create(this.ENTITIES);
|
|
1476
|
+
this.attribList = [];
|
|
1477
|
+
if (this.opt.xmlns) {
|
|
1478
|
+
this.ns = Object.create(this.rootNS);
|
|
1479
|
+
}
|
|
1480
|
+
this.trackPosition = this.opt.position !== false;
|
|
1481
|
+
if (this.trackPosition) {
|
|
1482
|
+
this.position = this.line = this.column = 0;
|
|
1483
|
+
}
|
|
1484
|
+
this.emit("onready");
|
|
1485
|
+
}
|
|
1486
|
+
resume() {
|
|
1487
|
+
this.error = null;
|
|
1488
|
+
return this;
|
|
1489
|
+
}
|
|
1490
|
+
close() {
|
|
1491
|
+
return this.write(null);
|
|
1492
|
+
}
|
|
1493
|
+
flush() {
|
|
1494
|
+
this.flushBuffers();
|
|
1495
|
+
}
|
|
1496
|
+
};
|
|
1497
|
+
}
|
|
1498
|
+
});
|
|
1499
|
+
|
|
1500
|
+
// ../documents/src/lib/parsers/parse-xml-internal.ts
|
|
1501
|
+
function setXMLField(object, name, value) {
|
|
1502
|
+
Object.defineProperty(object, name, {
|
|
1503
|
+
value,
|
|
1504
|
+
enumerable: true,
|
|
1505
|
+
configurable: true,
|
|
1506
|
+
writable: true
|
|
1507
|
+
});
|
|
1508
|
+
}
|
|
1509
|
+
function parseXMLInternal(text, options = {}) {
|
|
1510
|
+
if (options.strict !== false && /[^\u0009\u000a\u000d\u0020-\ud7ff\ue000-\ufffd\u{10000}-\u{10ffff}]/u.test(text)) {
|
|
1511
|
+
throw new Error("XML: invalid XML character");
|
|
1512
|
+
}
|
|
1513
|
+
const maximumDepth = options.maxDepth ?? 256;
|
|
1514
|
+
if (!Number.isSafeInteger(maximumDepth) || maximumDepth < 1 || maximumDepth > 4096) {
|
|
1515
|
+
throw new Error("XML: maxDepth must be an integer between 1 and 4096");
|
|
1516
|
+
}
|
|
1517
|
+
const state = new InternalXMLParserState(options, maximumDepth);
|
|
1518
|
+
const parser = new SAXParser({
|
|
1519
|
+
strict: options.strict !== false,
|
|
1520
|
+
strictEntities: !options.htmlEntities,
|
|
1521
|
+
preserveCase: true,
|
|
1522
|
+
caseInsensitiveTags: options.strict === false,
|
|
1523
|
+
processEntities: options.processEntities !== false,
|
|
1524
|
+
onopentag: (tag) => state.openElement(tag.name, tag.attributes || {}),
|
|
1525
|
+
ontext: (value) => state.addText(value),
|
|
1526
|
+
oncdata: (value) => state.addText(value),
|
|
1527
|
+
onscript: (value) => state.addText(value),
|
|
1528
|
+
onclosetag: () => state.closeElement(),
|
|
1529
|
+
onerror: (error) => {
|
|
1530
|
+
throw error;
|
|
1531
|
+
}
|
|
1532
|
+
});
|
|
1533
|
+
parser.write(text).close();
|
|
1534
|
+
if (options.strict === false) {
|
|
1535
|
+
while (state.stack.length) state.closeElement();
|
|
1536
|
+
}
|
|
1537
|
+
if (state.result === void 0) throw new Error("XML: document requires a root element");
|
|
1538
|
+
return state.result;
|
|
1539
|
+
}
|
|
1540
|
+
var InternalXMLParserState;
|
|
1541
|
+
var init_parse_xml_internal = __esm({
|
|
1542
|
+
"../documents/src/lib/parsers/parse-xml-internal.ts"() {
|
|
1543
|
+
"use strict";
|
|
1544
|
+
init_sax();
|
|
1545
|
+
InternalXMLParserState = class {
|
|
1546
|
+
/** Initializes conversion options and the element nesting budget. */
|
|
1547
|
+
constructor(options, maximumDepth) {
|
|
1548
|
+
this.options = options;
|
|
1549
|
+
this.maximumDepth = maximumDepth;
|
|
1550
|
+
}
|
|
1551
|
+
options;
|
|
1552
|
+
maximumDepth;
|
|
1553
|
+
/** Open element stack. */
|
|
1554
|
+
stack = [];
|
|
1555
|
+
/** Parsed root document. */
|
|
1556
|
+
result;
|
|
1557
|
+
/** Starts an element, rejecting multiple roots and excessive nesting. */
|
|
1558
|
+
openElement(name, attributes) {
|
|
1559
|
+
if (this.stack.length >= this.maximumDepth)
|
|
1560
|
+
throw new Error("XML: maximum nesting depth exceeded");
|
|
1561
|
+
if (!this.stack.length && this.result !== void 0 && this.options.strict !== false) {
|
|
1562
|
+
throw new Error("XML: document requires a single root element");
|
|
1563
|
+
}
|
|
1564
|
+
const elementName = this.normalizeName(name);
|
|
1565
|
+
const parent = this.stack[this.stack.length - 1];
|
|
1566
|
+
this.stack.push({
|
|
1567
|
+
name: elementName,
|
|
1568
|
+
path: parent ? `${parent.path}.${elementName}` : elementName,
|
|
1569
|
+
attributes: this.normalizeAttributes(attributes),
|
|
1570
|
+
children: {},
|
|
1571
|
+
orderedChildren: [],
|
|
1572
|
+
text: ""
|
|
1573
|
+
});
|
|
1574
|
+
}
|
|
1575
|
+
/** Accumulates text without losing whitespace between text and CDATA chunks. */
|
|
1576
|
+
addText(text) {
|
|
1577
|
+
const current = this.stack[this.stack.length - 1];
|
|
1578
|
+
if (current) current.text += text;
|
|
1579
|
+
}
|
|
1580
|
+
/** Finishes an element and attaches it to its parent or the document root. */
|
|
1581
|
+
closeElement() {
|
|
1582
|
+
const node = this.stack.pop();
|
|
1583
|
+
if (!node) return;
|
|
1584
|
+
const value = this.finalizeElement(node);
|
|
1585
|
+
const parent = this.stack[this.stack.length - 1];
|
|
1586
|
+
if (this.options.preserveOrder) {
|
|
1587
|
+
if (parent) parent.orderedChildren.push(value);
|
|
1588
|
+
else this.result = value;
|
|
1589
|
+
return;
|
|
1590
|
+
}
|
|
1591
|
+
if (parent) this.addChild(parent.children, node.name, node.path, value);
|
|
1592
|
+
else {
|
|
1593
|
+
if (this.result === void 0) this.result = {};
|
|
1594
|
+
this.addChild(this.result, node.name, node.path, value);
|
|
1595
|
+
}
|
|
1596
|
+
}
|
|
1597
|
+
/** Converts a completed element, keeping grouping and coercion independent. */
|
|
1598
|
+
finalizeElement(node) {
|
|
1599
|
+
const text = this.options.trimValues === false ? node.text : node.text.trim();
|
|
1600
|
+
if (this.options.preserveOrder) {
|
|
1601
|
+
return { name: node.name, attributes: node.attributes, children: node.orderedChildren, text };
|
|
1602
|
+
}
|
|
1603
|
+
const hasAttributes = Object.keys(node.attributes).length > 0;
|
|
1604
|
+
if (!hasAttributes && !Object.keys(node.children).length) {
|
|
1605
|
+
return this.parseValue(text, this.options.parseTagValue !== false);
|
|
1606
|
+
}
|
|
1607
|
+
const object = node.children;
|
|
1608
|
+
if (text)
|
|
1609
|
+
setXMLField(
|
|
1610
|
+
object,
|
|
1611
|
+
this.options.textNodeName ?? "value",
|
|
1612
|
+
this.parseValue(text, this.options.parseTagValue !== false)
|
|
1613
|
+
);
|
|
1614
|
+
if (hasAttributes && this.options.attributesGroupName) {
|
|
1615
|
+
setXMLField(object, this.options.attributesGroupName, node.attributes);
|
|
1616
|
+
} else {
|
|
1617
|
+
for (const [name, value] of Object.entries(node.attributes)) setXMLField(object, name, value);
|
|
1618
|
+
}
|
|
1619
|
+
return object;
|
|
1620
|
+
}
|
|
1621
|
+
/** Appends an element while preserving repeated and explicitly array-valued fields. */
|
|
1622
|
+
addChild(object, name, path, value) {
|
|
1623
|
+
var _a;
|
|
1624
|
+
if (!Object.hasOwn(object, name)) {
|
|
1625
|
+
setXMLField(object, name, ((_a = this.options.arrayPaths) == null ? void 0 : _a.includes(path)) ? [value] : value);
|
|
1626
|
+
} else {
|
|
1627
|
+
const existing = object[name];
|
|
1628
|
+
if (Array.isArray(existing)) existing.push(value);
|
|
1629
|
+
else setXMLField(object, name, [existing, value]);
|
|
1630
|
+
}
|
|
1631
|
+
}
|
|
1632
|
+
/** Applies namespace, prefix, grouping and scalar options to attributes. */
|
|
1633
|
+
normalizeAttributes(attributes) {
|
|
1634
|
+
const normalized = {};
|
|
1635
|
+
if (this.options.ignoreAttributes) return normalized;
|
|
1636
|
+
for (const [name, value] of Object.entries(attributes)) {
|
|
1637
|
+
if (this.options.removeNSPrefix && (name === "xmlns" || name.startsWith("xmlns:"))) continue;
|
|
1638
|
+
const text = this.options.trimValues === false ? value : value.trim();
|
|
1639
|
+
setXMLField(
|
|
1640
|
+
normalized,
|
|
1641
|
+
(this.options.attributeNamePrefix ?? "") + this.normalizeName(name),
|
|
1642
|
+
this.parseValue(text, this.options.parseAttributeValue === true)
|
|
1643
|
+
);
|
|
1644
|
+
}
|
|
1645
|
+
return normalized;
|
|
1646
|
+
}
|
|
1647
|
+
/** Removes a namespace prefix only when requested. */
|
|
1648
|
+
normalizeName(name) {
|
|
1649
|
+
return this.options.removeNSPrefix ? name.slice(name.indexOf(":") + 1) : name;
|
|
1650
|
+
}
|
|
1651
|
+
/** Coerces only finite numeric values and lowercase XML boolean literals. */
|
|
1652
|
+
parseValue(value, convert) {
|
|
1653
|
+
if (!convert) return value;
|
|
1654
|
+
if (value === "true" || value === "false") return value === "true";
|
|
1655
|
+
if (value !== "" && value.trim() !== "" && Number.isFinite(Number(value))) return Number(value);
|
|
1656
|
+
return value;
|
|
1657
|
+
}
|
|
1658
|
+
};
|
|
1659
|
+
}
|
|
1660
|
+
});
|
|
1661
|
+
|
|
1662
|
+
// ../documents/src/lib/parsers/parse-xml.ts
|
|
1663
|
+
function parseXMLSync(text, options = {}) {
|
|
1664
|
+
if (Object.hasOwn(options, "_fastXML") || Object.hasOwn(options, "_sax")) {
|
|
1665
|
+
throw new Error("XML: backend-specific options were removed; use native XML options");
|
|
1666
|
+
}
|
|
1667
|
+
if (options._parser && !["internal", "sax"].includes(options._parser)) {
|
|
1668
|
+
throw new Error("XML: unsupported parser backend; use the native documents parser");
|
|
1669
|
+
}
|
|
1670
|
+
const parsed = parseXMLInternal(text, options);
|
|
1671
|
+
return options.uncapitalizeKeys ? uncapitalizeKeys(parsed) : parsed;
|
|
1672
|
+
}
|
|
1673
|
+
var init_parse_xml = __esm({
|
|
1674
|
+
"../documents/src/lib/parsers/parse-xml.ts"() {
|
|
1675
|
+
"use strict";
|
|
1676
|
+
init_uncapitalize();
|
|
1677
|
+
init_parse_xml_internal();
|
|
1678
|
+
}
|
|
1679
|
+
});
|
|
1680
|
+
|
|
1681
|
+
// ../documents/src/xml-parser.ts
|
|
1682
|
+
var init_xml_parser = __esm({
|
|
1683
|
+
"../documents/src/xml-parser.ts"() {
|
|
1684
|
+
"use strict";
|
|
1685
|
+
init_parse_xml();
|
|
1686
|
+
}
|
|
1687
|
+
});
|
|
1688
|
+
|
|
1689
|
+
// src/lib/build-graph-tables.ts
|
|
1690
|
+
function buildGraphTables(nodes, edges, shape, declarations) {
|
|
1691
|
+
if (shape === "plain-graph-data") {
|
|
1692
|
+
return { shape, nodes: Array.from(nodes), edges: Array.from(edges) };
|
|
1693
|
+
}
|
|
1694
|
+
if (shape !== "arrow" && shape !== "object-row-table") {
|
|
1695
|
+
throw new Error(`Unsupported graph shape: ${shape}`);
|
|
1696
|
+
}
|
|
1697
|
+
const nodeTable = buildGraphTable(nodes, false, shape, declarations == null ? void 0 : declarations.nodes);
|
|
1698
|
+
const edgeTable = buildGraphTable(edges, true, shape, declarations == null ? void 0 : declarations.edges);
|
|
1699
|
+
return {
|
|
1700
|
+
shape: "tables",
|
|
1701
|
+
tables: [
|
|
1702
|
+
{ name: "nodes", table: nodeTable },
|
|
1703
|
+
{ name: "edges", table: edgeTable }
|
|
1704
|
+
]
|
|
1705
|
+
};
|
|
1706
|
+
}
|
|
1707
|
+
function buildGraphTable(records, isEdge, shape, declarations) {
|
|
1708
|
+
const columnNames = isEdge ? ["id", "sourceId", "targetId", "directed", "label", "attributes"] : ["id", "label", "attributes"];
|
|
1709
|
+
const columns = new Map(columnNames.map((name) => [name, []]));
|
|
1710
|
+
for (const record of records) {
|
|
1711
|
+
for (const name of columnNames) {
|
|
1712
|
+
const value = record[name];
|
|
1713
|
+
columns.get(name).push(
|
|
1714
|
+
name === "id" || name === "sourceId" || name === "targetId" ? String(value) : value ?? null
|
|
1715
|
+
);
|
|
1716
|
+
}
|
|
1717
|
+
}
|
|
1718
|
+
if (shape === "object-row-table") {
|
|
1719
|
+
return {
|
|
1720
|
+
shape,
|
|
1721
|
+
data: columns.get("id").map(
|
|
1722
|
+
(_, index) => Object.fromEntries(columnNames.map((name) => [name, columns.get(name)[index]]))
|
|
1723
|
+
)
|
|
1724
|
+
};
|
|
1725
|
+
}
|
|
1726
|
+
const vectors = /* @__PURE__ */ Object.create(null);
|
|
1727
|
+
for (const [name, values] of columns) {
|
|
1728
|
+
const type = name === "attributes" ? inferAttributeType(values, declarations) : name === "directed" ? new import_apache_arrow.Bool() : new import_apache_arrow.Utf8();
|
|
1729
|
+
vectors[name] = (0, import_apache_arrow.vectorFromArray)(
|
|
1730
|
+
values.map((value) => normalizeValue(value, type)),
|
|
1731
|
+
type
|
|
1732
|
+
);
|
|
1733
|
+
}
|
|
1734
|
+
return new import_apache_arrow.Table(vectors);
|
|
1735
|
+
}
|
|
1736
|
+
function inferAttributeType(values, declarations) {
|
|
1737
|
+
const names = new Set(declarations == null ? void 0 : declarations.keys());
|
|
1738
|
+
for (const value of values) {
|
|
1739
|
+
if (value && typeof value === "object") {
|
|
1740
|
+
for (const name of Object.keys(value)) {
|
|
1741
|
+
names.add(name);
|
|
1742
|
+
}
|
|
1743
|
+
}
|
|
1744
|
+
}
|
|
1745
|
+
return new import_apache_arrow.Struct(
|
|
1746
|
+
Array.from(names, (name) => {
|
|
1747
|
+
const fieldValues = values.map(
|
|
1748
|
+
(value) => (value == null ? void 0 : value[name]) ?? null
|
|
1749
|
+
);
|
|
1750
|
+
return new import_apache_arrow.Field(name, inferValueType(fieldValues, declarations == null ? void 0 : declarations.get(name)), true);
|
|
1751
|
+
})
|
|
1752
|
+
);
|
|
1753
|
+
}
|
|
1754
|
+
function inferValueType(values, declaration) {
|
|
1755
|
+
const present = values.filter((value) => value !== null && value !== void 0);
|
|
1756
|
+
if (declaration && typeof declaration === "object") {
|
|
1757
|
+
if (!present.every((value) => Array.isArray(value))) return new import_apache_arrow.Utf8();
|
|
1758
|
+
return new import_apache_arrow.List(
|
|
1759
|
+
new import_apache_arrow.Field(
|
|
1760
|
+
"item",
|
|
1761
|
+
inferValueType(
|
|
1762
|
+
present.flatMap((value) => value),
|
|
1763
|
+
declaration.list
|
|
1764
|
+
),
|
|
1765
|
+
true
|
|
1766
|
+
)
|
|
1767
|
+
);
|
|
1768
|
+
}
|
|
1769
|
+
if (declaration) {
|
|
1770
|
+
const declaredTypes = {
|
|
1771
|
+
boolean: new import_apache_arrow.Bool(),
|
|
1772
|
+
int: new import_apache_arrow.Int32(),
|
|
1773
|
+
long: new import_apache_arrow.Int64(),
|
|
1774
|
+
float: new import_apache_arrow.Float32(),
|
|
1775
|
+
double: new import_apache_arrow.Float64(),
|
|
1776
|
+
string: new import_apache_arrow.Utf8()
|
|
1777
|
+
};
|
|
1778
|
+
const valid = present.every((value) => {
|
|
1779
|
+
switch (declaration) {
|
|
1780
|
+
case "boolean":
|
|
1781
|
+
return typeof value === "boolean";
|
|
1782
|
+
case "int":
|
|
1783
|
+
return typeof value === "number" && Number.isInteger(value) && value >= -2147483648 && value <= 2147483647;
|
|
1784
|
+
case "long":
|
|
1785
|
+
return typeof value === "bigint" && value >= -(2n ** 63n) && value < 2n ** 63n;
|
|
1786
|
+
case "float":
|
|
1787
|
+
case "double":
|
|
1788
|
+
return typeof value === "number";
|
|
1789
|
+
case "string":
|
|
1790
|
+
return typeof value === "string";
|
|
1791
|
+
}
|
|
1792
|
+
});
|
|
1793
|
+
return valid ? declaredTypes[declaration] : new import_apache_arrow.Utf8();
|
|
1794
|
+
}
|
|
1795
|
+
if (present.length && present.every((value) => typeof value === "boolean")) return new import_apache_arrow.Bool();
|
|
1796
|
+
if (present.length && present.every((value) => typeof value === "number")) return new import_apache_arrow.Float64();
|
|
1797
|
+
if (present.length && present.every((value) => Array.isArray(value))) {
|
|
1798
|
+
return new import_apache_arrow.List(
|
|
1799
|
+
new import_apache_arrow.Field("item", inferValueType(present.flatMap((value) => value)), true)
|
|
1800
|
+
);
|
|
1801
|
+
}
|
|
1802
|
+
if (present.length && present.every((value) => typeof value === "object" && !Array.isArray(value))) {
|
|
1803
|
+
return inferAttributeType(present);
|
|
1804
|
+
}
|
|
1805
|
+
return new import_apache_arrow.Utf8();
|
|
1806
|
+
}
|
|
1807
|
+
function normalizeValue(value, type) {
|
|
1808
|
+
if (value === null || value === void 0) return null;
|
|
1809
|
+
if (type instanceof import_apache_arrow.Struct) {
|
|
1810
|
+
return Object.fromEntries(
|
|
1811
|
+
type.children.map((field) => [
|
|
1812
|
+
field.name,
|
|
1813
|
+
normalizeValue(value[field.name], field.type)
|
|
1814
|
+
])
|
|
1815
|
+
);
|
|
1816
|
+
}
|
|
1817
|
+
if (type instanceof import_apache_arrow.List)
|
|
1818
|
+
return value.map((item) => normalizeValue(item, type.valueType));
|
|
1819
|
+
if (type instanceof import_apache_arrow.Utf8 && typeof value !== "string") {
|
|
1820
|
+
return typeof value === "object" ? JSON.stringify(value) : String(value);
|
|
1821
|
+
}
|
|
1822
|
+
return value;
|
|
1823
|
+
}
|
|
1824
|
+
var import_apache_arrow;
|
|
1825
|
+
var init_build_graph_tables = __esm({
|
|
1826
|
+
"src/lib/build-graph-tables.ts"() {
|
|
1827
|
+
"use strict";
|
|
1828
|
+
import_apache_arrow = require("apache-arrow");
|
|
1829
|
+
}
|
|
1830
|
+
});
|
|
1831
|
+
|
|
55
1832
|
// src/lib/parse-graphml.ts
|
|
56
|
-
function parseGraphML(graphml) {
|
|
1833
|
+
function parseGraphML(graphml, shape = "plain-graph-data") {
|
|
57
1834
|
const xmlText = decodeGraphML(graphml);
|
|
58
|
-
const document =
|
|
1835
|
+
const document = parseXMLSync(xmlText, GRAPHML_XML_OPTIONS);
|
|
59
1836
|
const graphmlRoot = getGraphMLRoot(document);
|
|
60
1837
|
if (!graphmlRoot) {
|
|
61
1838
|
throw new Error("GraphML document does not contain a <graphml> element.");
|
|
@@ -64,19 +1841,38 @@ function parseGraphML(graphml) {
|
|
|
64
1841
|
if (!graphElement) {
|
|
65
1842
|
throw new Error("GraphML document does not contain a <graph> element.");
|
|
66
1843
|
}
|
|
67
|
-
const keyDefinitions = collectKeyDefinitions(
|
|
1844
|
+
const keyDefinitions = collectKeyDefinitions(
|
|
1845
|
+
graphmlRoot,
|
|
1846
|
+
graphElement,
|
|
1847
|
+
shape !== "plain-graph-data"
|
|
1848
|
+
);
|
|
68
1849
|
const defaultDirected = parseEdgeDefault(graphElement[`${XML_ATTRIBUTE_PREFIX}edgedefault`]);
|
|
69
|
-
const
|
|
70
|
-
const
|
|
71
|
-
(
|
|
1850
|
+
const declarations = { nodes: /* @__PURE__ */ new Map(), edges: /* @__PURE__ */ new Map() };
|
|
1851
|
+
for (const definition of keyDefinitions.values()) {
|
|
1852
|
+
if (definition.domain === "all" || definition.domain === "node")
|
|
1853
|
+
declarations.nodes.set(definition.name, definition.type);
|
|
1854
|
+
if (definition.domain === "all" || definition.domain === "edge")
|
|
1855
|
+
declarations.edges.set(definition.name, definition.type);
|
|
1856
|
+
}
|
|
1857
|
+
return buildGraphTables(
|
|
1858
|
+
iterateNodes(graphElement.node, keyDefinitions),
|
|
1859
|
+
iterateEdges(graphElement.edge, keyDefinitions, defaultDirected),
|
|
1860
|
+
shape,
|
|
1861
|
+
declarations
|
|
72
1862
|
);
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
1863
|
+
}
|
|
1864
|
+
function* iterateNodes(nodes, definitions) {
|
|
1865
|
+
for (const node of normalizeArray(nodes)) {
|
|
1866
|
+
const record = parseNode(node, definitions);
|
|
1867
|
+
if (record) yield record;
|
|
1868
|
+
}
|
|
1869
|
+
}
|
|
1870
|
+
function* iterateEdges(edges, definitions, directed) {
|
|
1871
|
+
let index = 0;
|
|
1872
|
+
for (const edge of normalizeArray(edges)) {
|
|
1873
|
+
const record = parseEdge(edge, index++, definitions, directed);
|
|
1874
|
+
if (record) yield record;
|
|
1875
|
+
}
|
|
80
1876
|
}
|
|
81
1877
|
function decodeGraphML(graphml) {
|
|
82
1878
|
if (typeof graphml === "string") {
|
|
@@ -99,16 +1895,21 @@ function getGraphMLRoot(document) {
|
|
|
99
1895
|
}
|
|
100
1896
|
function getGraphElement(graphmlRoot) {
|
|
101
1897
|
const graph = graphmlRoot.graph;
|
|
1898
|
+
if (typeof graph === "string" && !graph.trim()) {
|
|
1899
|
+
return {};
|
|
1900
|
+
}
|
|
102
1901
|
if (!graph) {
|
|
103
1902
|
return null;
|
|
104
1903
|
}
|
|
105
1904
|
if (Array.isArray(graph)) {
|
|
106
|
-
const firstGraph = graph.find(
|
|
107
|
-
|
|
1905
|
+
const firstGraph = graph.find(
|
|
1906
|
+
(entry) => isObject(entry) || typeof entry === "string" && !entry.trim()
|
|
1907
|
+
);
|
|
1908
|
+
return isObject(firstGraph) ? firstGraph : typeof firstGraph === "string" ? {} : null;
|
|
108
1909
|
}
|
|
109
1910
|
return isObject(graph) ? graph : null;
|
|
110
1911
|
}
|
|
111
|
-
function collectKeyDefinitions(graphmlRoot, graphElement) {
|
|
1912
|
+
function collectKeyDefinitions(graphmlRoot, graphElement, lossless) {
|
|
112
1913
|
const keys = /* @__PURE__ */ new Map();
|
|
113
1914
|
for (const candidate of [
|
|
114
1915
|
...normalizeArray(graphmlRoot.key),
|
|
@@ -121,8 +1922,8 @@ function collectKeyDefinitions(graphmlRoot, graphElement) {
|
|
|
121
1922
|
const name = String(candidate[`${XML_ATTRIBUTE_PREFIX}attr.name`] ?? id).trim();
|
|
122
1923
|
const type = normalizeType(candidate[`${XML_ATTRIBUTE_PREFIX}attr.type`]);
|
|
123
1924
|
const defaultNode = candidate.default ?? null;
|
|
124
|
-
const defaultValue = defaultNode !== null ? castDataValue(defaultNode, type) : void 0;
|
|
125
|
-
keys.set(id, { id, domain, name, type, defaultValue });
|
|
1925
|
+
const defaultValue = defaultNode !== null ? castDataValue(defaultNode, type, lossless) : void 0;
|
|
1926
|
+
keys.set(id, { id, domain, name, type, defaultValue, lossless });
|
|
126
1927
|
}
|
|
127
1928
|
}
|
|
128
1929
|
}
|
|
@@ -200,12 +2001,12 @@ function assignAttributeFromDataEntry(entry, keyDefinitions, attributes) {
|
|
|
200
2001
|
}
|
|
201
2002
|
const definition = keyDefinitions.get(keyId);
|
|
202
2003
|
const attributeName = (definition == null ? void 0 : definition.name) ?? keyId;
|
|
203
|
-
const value = castDataValue(entry, (definition == null ? void 0 : definition.type) ?? "string");
|
|
2004
|
+
const value = (definition == null ? void 0 : definition.type) === "boolean" && extractTextContent(entry) === void 0 ? false : castDataValue(entry, (definition == null ? void 0 : definition.type) ?? "string", definition == null ? void 0 : definition.lossless);
|
|
204
2005
|
if (value !== void 0) {
|
|
205
2006
|
attributes[attributeName] = value;
|
|
206
2007
|
}
|
|
207
2008
|
}
|
|
208
|
-
function castDataValue(value, type) {
|
|
2009
|
+
function castDataValue(value, type, lossless = false) {
|
|
209
2010
|
if (value === null || typeof value === "undefined") {
|
|
210
2011
|
return void 0;
|
|
211
2012
|
}
|
|
@@ -216,6 +2017,9 @@ function castDataValue(value, type) {
|
|
|
216
2017
|
if (type === "boolean") {
|
|
217
2018
|
return parseBoolean(text);
|
|
218
2019
|
}
|
|
2020
|
+
if (type === "long" && lossless) {
|
|
2021
|
+
return /^[+-]?\d+$/.test(text.trim()) ? BigInt(text.trim()) : text;
|
|
2022
|
+
}
|
|
219
2023
|
if (type === "int" || type === "long") {
|
|
220
2024
|
const parsed = Number.parseInt(text, 10);
|
|
221
2025
|
return Number.isNaN(parsed) ? text : parsed;
|
|
@@ -238,14 +2042,14 @@ function extractTextContent(value) {
|
|
|
238
2042
|
}
|
|
239
2043
|
if (isObject(value)) {
|
|
240
2044
|
const text = value[XML_TEXT_KEY];
|
|
241
|
-
if (typeof text === "string") {
|
|
242
|
-
return text;
|
|
243
|
-
}
|
|
244
2045
|
const nonAttributeEntries = Object.entries(value).filter(
|
|
245
|
-
([key]) => !key.startsWith(XML_ATTRIBUTE_PREFIX)
|
|
2046
|
+
([key]) => !key.startsWith(XML_ATTRIBUTE_PREFIX) && key !== XML_TEXT_KEY
|
|
246
2047
|
);
|
|
247
2048
|
if (nonAttributeEntries.length === 0) {
|
|
248
|
-
return void 0;
|
|
2049
|
+
return typeof text === "string" ? text : void 0;
|
|
2050
|
+
}
|
|
2051
|
+
if (typeof text === "string" && text.trim()) {
|
|
2052
|
+
nonAttributeEntries.push([XML_TEXT_KEY, text]);
|
|
249
2053
|
}
|
|
250
2054
|
return JSON.stringify(Object.fromEntries(nonAttributeEntries));
|
|
251
2055
|
}
|
|
@@ -306,24 +2110,25 @@ function parseBoolean(value) {
|
|
|
306
2110
|
if (normalized === "false" || normalized === "0" || normalized === "no" || normalized === "n") {
|
|
307
2111
|
return false;
|
|
308
2112
|
}
|
|
309
|
-
return Boolean(
|
|
2113
|
+
return Boolean(normalized);
|
|
310
2114
|
}
|
|
311
|
-
var
|
|
2115
|
+
var XML_ATTRIBUTE_PREFIX, XML_TEXT_KEY, GRAPHML_XML_OPTIONS;
|
|
312
2116
|
var init_parse_graphml = __esm({
|
|
313
2117
|
"src/lib/parse-graphml.ts"() {
|
|
314
2118
|
"use strict";
|
|
315
|
-
|
|
2119
|
+
init_xml_parser();
|
|
2120
|
+
init_build_graph_tables();
|
|
316
2121
|
XML_ATTRIBUTE_PREFIX = "@_";
|
|
317
2122
|
XML_TEXT_KEY = "#text";
|
|
318
|
-
|
|
2123
|
+
GRAPHML_XML_OPTIONS = {
|
|
319
2124
|
ignoreAttributes: false,
|
|
320
2125
|
attributeNamePrefix: XML_ATTRIBUTE_PREFIX,
|
|
321
2126
|
textNodeName: XML_TEXT_KEY,
|
|
322
|
-
trimValues:
|
|
2127
|
+
trimValues: false,
|
|
323
2128
|
parseAttributeValue: false,
|
|
324
2129
|
parseTagValue: false,
|
|
325
2130
|
removeNSPrefix: true
|
|
326
|
-
}
|
|
2131
|
+
};
|
|
327
2132
|
}
|
|
328
2133
|
});
|
|
329
2134
|
|
|
@@ -340,9 +2145,18 @@ var init_graphml_loader = __esm({
|
|
|
340
2145
|
init_parse_graphml();
|
|
341
2146
|
GraphMLLoaderWithParser = {
|
|
342
2147
|
...GraphMLLoader,
|
|
343
|
-
parse: async (data) =>
|
|
344
|
-
|
|
345
|
-
|
|
2148
|
+
parse: async (data, options) => {
|
|
2149
|
+
var _a;
|
|
2150
|
+
return parseGraphML(data, ((_a = options == null ? void 0 : options.graphml) == null ? void 0 : _a.shape) ?? "arrow");
|
|
2151
|
+
},
|
|
2152
|
+
parseSync: (data, options) => {
|
|
2153
|
+
var _a;
|
|
2154
|
+
return parseGraphML(data, ((_a = options == null ? void 0 : options.graphml) == null ? void 0 : _a.shape) ?? "arrow");
|
|
2155
|
+
},
|
|
2156
|
+
parseTextSync: (data, options) => {
|
|
2157
|
+
var _a;
|
|
2158
|
+
return parseGraphML(data, ((_a = options == null ? void 0 : options.graphml) == null ? void 0 : _a.shape) ?? "arrow");
|
|
2159
|
+
}
|
|
346
2160
|
};
|
|
347
2161
|
}
|
|
348
2162
|
});
|
|
@@ -355,7 +2169,7 @@ __export(graphml_loader_exports2, {
|
|
|
355
2169
|
module.exports = __toCommonJS(graphml_loader_exports2);
|
|
356
2170
|
|
|
357
2171
|
// dist/graphml-loader-types.js
|
|
358
|
-
var VERSION2 = true ? "5.0.0-
|
|
2172
|
+
var VERSION2 = true ? "5.0.0-beta.10" : "latest";
|
|
359
2173
|
async function preload2() {
|
|
360
2174
|
const { GraphMLLoaderWithParser: GraphMLLoaderWithParser3 } = await Promise.resolve().then(() => (init_graphml_loader(), graphml_loader_exports));
|
|
361
2175
|
return GraphMLLoaderWithParser3;
|
|
@@ -369,28 +2183,148 @@ var GraphMLLoader2 = {
|
|
|
369
2183
|
mimeTypes: ["application/graphml+xml"],
|
|
370
2184
|
text: true,
|
|
371
2185
|
worker: false,
|
|
2186
|
+
supportsNativeArrow: true,
|
|
372
2187
|
dataType: null,
|
|
373
2188
|
batchType: null,
|
|
374
|
-
options: {},
|
|
2189
|
+
options: { graphml: { shape: "arrow" } },
|
|
375
2190
|
preload: preload2
|
|
376
2191
|
};
|
|
377
2192
|
|
|
378
2193
|
// dist/lib/parse-graphml.js
|
|
379
|
-
|
|
2194
|
+
init_xml_parser();
|
|
2195
|
+
|
|
2196
|
+
// dist/lib/build-graph-tables.js
|
|
2197
|
+
var import_apache_arrow2 = require("apache-arrow");
|
|
2198
|
+
function buildGraphTables2(nodes, edges, shape, declarations) {
|
|
2199
|
+
if (shape === "plain-graph-data") {
|
|
2200
|
+
return { shape, nodes: Array.from(nodes), edges: Array.from(edges) };
|
|
2201
|
+
}
|
|
2202
|
+
if (shape !== "arrow" && shape !== "object-row-table") {
|
|
2203
|
+
throw new Error(`Unsupported graph shape: ${shape}`);
|
|
2204
|
+
}
|
|
2205
|
+
const nodeTable = buildGraphTable2(nodes, false, shape, declarations == null ? void 0 : declarations.nodes);
|
|
2206
|
+
const edgeTable = buildGraphTable2(edges, true, shape, declarations == null ? void 0 : declarations.edges);
|
|
2207
|
+
return {
|
|
2208
|
+
shape: "tables",
|
|
2209
|
+
tables: [
|
|
2210
|
+
{ name: "nodes", table: nodeTable },
|
|
2211
|
+
{ name: "edges", table: edgeTable }
|
|
2212
|
+
]
|
|
2213
|
+
};
|
|
2214
|
+
}
|
|
2215
|
+
function buildGraphTable2(records, isEdge, shape, declarations) {
|
|
2216
|
+
const columnNames = isEdge ? ["id", "sourceId", "targetId", "directed", "label", "attributes"] : ["id", "label", "attributes"];
|
|
2217
|
+
const columns = new Map(columnNames.map((name) => [name, []]));
|
|
2218
|
+
for (const record of records) {
|
|
2219
|
+
for (const name of columnNames) {
|
|
2220
|
+
const value = record[name];
|
|
2221
|
+
columns.get(name).push(name === "id" || name === "sourceId" || name === "targetId" ? String(value) : value ?? null);
|
|
2222
|
+
}
|
|
2223
|
+
}
|
|
2224
|
+
if (shape === "object-row-table") {
|
|
2225
|
+
return {
|
|
2226
|
+
shape,
|
|
2227
|
+
data: columns.get("id").map((_, index) => Object.fromEntries(columnNames.map((name) => [name, columns.get(name)[index]])))
|
|
2228
|
+
};
|
|
2229
|
+
}
|
|
2230
|
+
const vectors = /* @__PURE__ */ Object.create(null);
|
|
2231
|
+
for (const [name, values] of columns) {
|
|
2232
|
+
const type = name === "attributes" ? inferAttributeType2(values, declarations) : name === "directed" ? new import_apache_arrow2.Bool() : new import_apache_arrow2.Utf8();
|
|
2233
|
+
vectors[name] = (0, import_apache_arrow2.vectorFromArray)(values.map((value) => normalizeValue2(value, type)), type);
|
|
2234
|
+
}
|
|
2235
|
+
return new import_apache_arrow2.Table(vectors);
|
|
2236
|
+
}
|
|
2237
|
+
function inferAttributeType2(values, declarations) {
|
|
2238
|
+
const names = new Set(declarations == null ? void 0 : declarations.keys());
|
|
2239
|
+
for (const value of values) {
|
|
2240
|
+
if (value && typeof value === "object") {
|
|
2241
|
+
for (const name of Object.keys(value)) {
|
|
2242
|
+
names.add(name);
|
|
2243
|
+
}
|
|
2244
|
+
}
|
|
2245
|
+
}
|
|
2246
|
+
return new import_apache_arrow2.Struct(Array.from(names, (name) => {
|
|
2247
|
+
const fieldValues = values.map((value) => (value == null ? void 0 : value[name]) ?? null);
|
|
2248
|
+
return new import_apache_arrow2.Field(name, inferValueType2(fieldValues, declarations == null ? void 0 : declarations.get(name)), true);
|
|
2249
|
+
}));
|
|
2250
|
+
}
|
|
2251
|
+
function inferValueType2(values, declaration) {
|
|
2252
|
+
const present = values.filter((value) => value !== null && value !== void 0);
|
|
2253
|
+
if (declaration && typeof declaration === "object") {
|
|
2254
|
+
if (!present.every((value) => Array.isArray(value)))
|
|
2255
|
+
return new import_apache_arrow2.Utf8();
|
|
2256
|
+
return new import_apache_arrow2.List(new import_apache_arrow2.Field("item", inferValueType2(present.flatMap((value) => value), declaration.list), true));
|
|
2257
|
+
}
|
|
2258
|
+
if (declaration) {
|
|
2259
|
+
const declaredTypes = {
|
|
2260
|
+
boolean: new import_apache_arrow2.Bool(),
|
|
2261
|
+
int: new import_apache_arrow2.Int32(),
|
|
2262
|
+
long: new import_apache_arrow2.Int64(),
|
|
2263
|
+
float: new import_apache_arrow2.Float32(),
|
|
2264
|
+
double: new import_apache_arrow2.Float64(),
|
|
2265
|
+
string: new import_apache_arrow2.Utf8()
|
|
2266
|
+
};
|
|
2267
|
+
const valid = present.every((value) => {
|
|
2268
|
+
switch (declaration) {
|
|
2269
|
+
case "boolean":
|
|
2270
|
+
return typeof value === "boolean";
|
|
2271
|
+
case "int":
|
|
2272
|
+
return typeof value === "number" && Number.isInteger(value) && value >= -2147483648 && value <= 2147483647;
|
|
2273
|
+
case "long":
|
|
2274
|
+
return typeof value === "bigint" && value >= -(2n ** 63n) && value < 2n ** 63n;
|
|
2275
|
+
case "float":
|
|
2276
|
+
case "double":
|
|
2277
|
+
return typeof value === "number";
|
|
2278
|
+
case "string":
|
|
2279
|
+
return typeof value === "string";
|
|
2280
|
+
}
|
|
2281
|
+
});
|
|
2282
|
+
return valid ? declaredTypes[declaration] : new import_apache_arrow2.Utf8();
|
|
2283
|
+
}
|
|
2284
|
+
if (present.length && present.every((value) => typeof value === "boolean"))
|
|
2285
|
+
return new import_apache_arrow2.Bool();
|
|
2286
|
+
if (present.length && present.every((value) => typeof value === "number"))
|
|
2287
|
+
return new import_apache_arrow2.Float64();
|
|
2288
|
+
if (present.length && present.every((value) => Array.isArray(value))) {
|
|
2289
|
+
return new import_apache_arrow2.List(new import_apache_arrow2.Field("item", inferValueType2(present.flatMap((value) => value)), true));
|
|
2290
|
+
}
|
|
2291
|
+
if (present.length && present.every((value) => typeof value === "object" && !Array.isArray(value))) {
|
|
2292
|
+
return inferAttributeType2(present);
|
|
2293
|
+
}
|
|
2294
|
+
return new import_apache_arrow2.Utf8();
|
|
2295
|
+
}
|
|
2296
|
+
function normalizeValue2(value, type) {
|
|
2297
|
+
if (value === null || value === void 0)
|
|
2298
|
+
return null;
|
|
2299
|
+
if (type instanceof import_apache_arrow2.Struct) {
|
|
2300
|
+
return Object.fromEntries(type.children.map((field) => [
|
|
2301
|
+
field.name,
|
|
2302
|
+
normalizeValue2(value[field.name], field.type)
|
|
2303
|
+
]));
|
|
2304
|
+
}
|
|
2305
|
+
if (type instanceof import_apache_arrow2.List)
|
|
2306
|
+
return value.map((item) => normalizeValue2(item, type.valueType));
|
|
2307
|
+
if (type instanceof import_apache_arrow2.Utf8 && typeof value !== "string") {
|
|
2308
|
+
return typeof value === "object" ? JSON.stringify(value) : String(value);
|
|
2309
|
+
}
|
|
2310
|
+
return value;
|
|
2311
|
+
}
|
|
2312
|
+
|
|
2313
|
+
// dist/lib/parse-graphml.js
|
|
380
2314
|
var XML_ATTRIBUTE_PREFIX2 = "@_";
|
|
381
2315
|
var XML_TEXT_KEY2 = "#text";
|
|
382
|
-
var
|
|
2316
|
+
var GRAPHML_XML_OPTIONS2 = {
|
|
383
2317
|
ignoreAttributes: false,
|
|
384
2318
|
attributeNamePrefix: XML_ATTRIBUTE_PREFIX2,
|
|
385
2319
|
textNodeName: XML_TEXT_KEY2,
|
|
386
|
-
trimValues:
|
|
2320
|
+
trimValues: false,
|
|
387
2321
|
parseAttributeValue: false,
|
|
388
2322
|
parseTagValue: false,
|
|
389
2323
|
removeNSPrefix: true
|
|
390
|
-
}
|
|
391
|
-
function parseGraphML2(graphml) {
|
|
2324
|
+
};
|
|
2325
|
+
function parseGraphML2(graphml, shape = "plain-graph-data") {
|
|
392
2326
|
const xmlText = decodeGraphML2(graphml);
|
|
393
|
-
const document =
|
|
2327
|
+
const document = parseXMLSync(xmlText, GRAPHML_XML_OPTIONS2);
|
|
394
2328
|
const graphmlRoot = getGraphMLRoot2(document);
|
|
395
2329
|
if (!graphmlRoot) {
|
|
396
2330
|
throw new Error("GraphML document does not contain a <graphml> element.");
|
|
@@ -399,17 +2333,31 @@ function parseGraphML2(graphml) {
|
|
|
399
2333
|
if (!graphElement) {
|
|
400
2334
|
throw new Error("GraphML document does not contain a <graph> element.");
|
|
401
2335
|
}
|
|
402
|
-
const keyDefinitions = collectKeyDefinitions2(graphmlRoot, graphElement);
|
|
2336
|
+
const keyDefinitions = collectKeyDefinitions2(graphmlRoot, graphElement, shape !== "plain-graph-data");
|
|
403
2337
|
const defaultDirected = parseEdgeDefault2(graphElement[`${XML_ATTRIBUTE_PREFIX2}edgedefault`]);
|
|
404
|
-
const
|
|
405
|
-
const
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
2338
|
+
const declarations = { nodes: /* @__PURE__ */ new Map(), edges: /* @__PURE__ */ new Map() };
|
|
2339
|
+
for (const definition of keyDefinitions.values()) {
|
|
2340
|
+
if (definition.domain === "all" || definition.domain === "node")
|
|
2341
|
+
declarations.nodes.set(definition.name, definition.type);
|
|
2342
|
+
if (definition.domain === "all" || definition.domain === "edge")
|
|
2343
|
+
declarations.edges.set(definition.name, definition.type);
|
|
2344
|
+
}
|
|
2345
|
+
return buildGraphTables2(iterateNodes2(graphElement.node, keyDefinitions), iterateEdges2(graphElement.edge, keyDefinitions, defaultDirected), shape, declarations);
|
|
2346
|
+
}
|
|
2347
|
+
function* iterateNodes2(nodes, definitions) {
|
|
2348
|
+
for (const node of normalizeArray2(nodes)) {
|
|
2349
|
+
const record = parseNode2(node, definitions);
|
|
2350
|
+
if (record)
|
|
2351
|
+
yield record;
|
|
2352
|
+
}
|
|
2353
|
+
}
|
|
2354
|
+
function* iterateEdges2(edges, definitions, directed) {
|
|
2355
|
+
let index = 0;
|
|
2356
|
+
for (const edge of normalizeArray2(edges)) {
|
|
2357
|
+
const record = parseEdge2(edge, index++, definitions, directed);
|
|
2358
|
+
if (record)
|
|
2359
|
+
yield record;
|
|
2360
|
+
}
|
|
413
2361
|
}
|
|
414
2362
|
function decodeGraphML2(graphml) {
|
|
415
2363
|
if (typeof graphml === "string") {
|
|
@@ -432,16 +2380,19 @@ function getGraphMLRoot2(document) {
|
|
|
432
2380
|
}
|
|
433
2381
|
function getGraphElement2(graphmlRoot) {
|
|
434
2382
|
const graph = graphmlRoot.graph;
|
|
2383
|
+
if (typeof graph === "string" && !graph.trim()) {
|
|
2384
|
+
return {};
|
|
2385
|
+
}
|
|
435
2386
|
if (!graph) {
|
|
436
2387
|
return null;
|
|
437
2388
|
}
|
|
438
2389
|
if (Array.isArray(graph)) {
|
|
439
|
-
const firstGraph = graph.find((entry) => isObject2(entry));
|
|
440
|
-
return isObject2(firstGraph) ? firstGraph : null;
|
|
2390
|
+
const firstGraph = graph.find((entry) => isObject2(entry) || typeof entry === "string" && !entry.trim());
|
|
2391
|
+
return isObject2(firstGraph) ? firstGraph : typeof firstGraph === "string" ? {} : null;
|
|
441
2392
|
}
|
|
442
2393
|
return isObject2(graph) ? graph : null;
|
|
443
2394
|
}
|
|
444
|
-
function collectKeyDefinitions2(graphmlRoot, graphElement) {
|
|
2395
|
+
function collectKeyDefinitions2(graphmlRoot, graphElement, lossless) {
|
|
445
2396
|
const keys = /* @__PURE__ */ new Map();
|
|
446
2397
|
for (const candidate of [
|
|
447
2398
|
...normalizeArray2(graphmlRoot.key),
|
|
@@ -454,8 +2405,8 @@ function collectKeyDefinitions2(graphmlRoot, graphElement) {
|
|
|
454
2405
|
const name = String(candidate[`${XML_ATTRIBUTE_PREFIX2}attr.name`] ?? id).trim();
|
|
455
2406
|
const type = normalizeType2(candidate[`${XML_ATTRIBUTE_PREFIX2}attr.type`]);
|
|
456
2407
|
const defaultNode = candidate.default ?? null;
|
|
457
|
-
const defaultValue = defaultNode !== null ? castDataValue2(defaultNode, type) : void 0;
|
|
458
|
-
keys.set(id, { id, domain, name, type, defaultValue });
|
|
2408
|
+
const defaultValue = defaultNode !== null ? castDataValue2(defaultNode, type, lossless) : void 0;
|
|
2409
|
+
keys.set(id, { id, domain, name, type, defaultValue, lossless });
|
|
459
2410
|
}
|
|
460
2411
|
}
|
|
461
2412
|
}
|
|
@@ -533,12 +2484,12 @@ function assignAttributeFromDataEntry2(entry, keyDefinitions, attributes) {
|
|
|
533
2484
|
}
|
|
534
2485
|
const definition = keyDefinitions.get(keyId);
|
|
535
2486
|
const attributeName = (definition == null ? void 0 : definition.name) ?? keyId;
|
|
536
|
-
const value = castDataValue2(entry, (definition == null ? void 0 : definition.type) ?? "string");
|
|
2487
|
+
const value = (definition == null ? void 0 : definition.type) === "boolean" && extractTextContent2(entry) === void 0 ? false : castDataValue2(entry, (definition == null ? void 0 : definition.type) ?? "string", definition == null ? void 0 : definition.lossless);
|
|
537
2488
|
if (value !== void 0) {
|
|
538
2489
|
attributes[attributeName] = value;
|
|
539
2490
|
}
|
|
540
2491
|
}
|
|
541
|
-
function castDataValue2(value, type) {
|
|
2492
|
+
function castDataValue2(value, type, lossless = false) {
|
|
542
2493
|
if (value === null || typeof value === "undefined") {
|
|
543
2494
|
return void 0;
|
|
544
2495
|
}
|
|
@@ -549,6 +2500,9 @@ function castDataValue2(value, type) {
|
|
|
549
2500
|
if (type === "boolean") {
|
|
550
2501
|
return parseBoolean2(text);
|
|
551
2502
|
}
|
|
2503
|
+
if (type === "long" && lossless) {
|
|
2504
|
+
return /^[+-]?\d+$/.test(text.trim()) ? BigInt(text.trim()) : text;
|
|
2505
|
+
}
|
|
552
2506
|
if (type === "int" || type === "long") {
|
|
553
2507
|
const parsed = Number.parseInt(text, 10);
|
|
554
2508
|
return Number.isNaN(parsed) ? text : parsed;
|
|
@@ -571,12 +2525,12 @@ function extractTextContent2(value) {
|
|
|
571
2525
|
}
|
|
572
2526
|
if (isObject2(value)) {
|
|
573
2527
|
const text = value[XML_TEXT_KEY2];
|
|
574
|
-
|
|
575
|
-
return text;
|
|
576
|
-
}
|
|
577
|
-
const nonAttributeEntries = Object.entries(value).filter(([key]) => !key.startsWith(XML_ATTRIBUTE_PREFIX2));
|
|
2528
|
+
const nonAttributeEntries = Object.entries(value).filter(([key]) => !key.startsWith(XML_ATTRIBUTE_PREFIX2) && key !== XML_TEXT_KEY2);
|
|
578
2529
|
if (nonAttributeEntries.length === 0) {
|
|
579
|
-
return void 0;
|
|
2530
|
+
return typeof text === "string" ? text : void 0;
|
|
2531
|
+
}
|
|
2532
|
+
if (typeof text === "string" && text.trim()) {
|
|
2533
|
+
nonAttributeEntries.push([XML_TEXT_KEY2, text]);
|
|
580
2534
|
}
|
|
581
2535
|
return JSON.stringify(Object.fromEntries(nonAttributeEntries));
|
|
582
2536
|
}
|
|
@@ -637,14 +2591,23 @@ function parseBoolean2(value) {
|
|
|
637
2591
|
if (normalized === "false" || normalized === "0" || normalized === "no" || normalized === "n") {
|
|
638
2592
|
return false;
|
|
639
2593
|
}
|
|
640
|
-
return Boolean(
|
|
2594
|
+
return Boolean(normalized);
|
|
641
2595
|
}
|
|
642
2596
|
|
|
643
2597
|
// dist/graphml-loader.js
|
|
644
2598
|
var GraphMLLoaderWithParser2 = {
|
|
645
2599
|
...GraphMLLoader2,
|
|
646
|
-
parse: async (data) =>
|
|
647
|
-
|
|
648
|
-
|
|
2600
|
+
parse: async (data, options) => {
|
|
2601
|
+
var _a;
|
|
2602
|
+
return parseGraphML2(data, ((_a = options == null ? void 0 : options.graphml) == null ? void 0 : _a.shape) ?? "arrow");
|
|
2603
|
+
},
|
|
2604
|
+
parseSync: (data, options) => {
|
|
2605
|
+
var _a;
|
|
2606
|
+
return parseGraphML2(data, ((_a = options == null ? void 0 : options.graphml) == null ? void 0 : _a.shape) ?? "arrow");
|
|
2607
|
+
},
|
|
2608
|
+
parseTextSync: (data, options) => {
|
|
2609
|
+
var _a;
|
|
2610
|
+
return parseGraphML2(data, ((_a = options == null ? void 0 : options.graphml) == null ? void 0 : _a.shape) ?? "arrow");
|
|
2611
|
+
}
|
|
649
2612
|
};
|
|
650
2613
|
//# sourceMappingURL=graphml-loader.cjs.map
|