@loaders.gl/xml 4.0.0-beta.2 → 4.0.0-beta.4

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.
Files changed (51) hide show
  1. package/dist/{dist.min.js → dist.dev.js} +2077 -2057
  2. package/dist/{esm/html-loader.js → html-loader.js} +1 -1
  3. package/dist/html-loader.js.map +1 -0
  4. package/dist/index.cjs +1530 -0
  5. package/dist/index.js +6 -0
  6. package/dist/index.js.map +1 -0
  7. package/dist/{esm/lib → lib}/parsers/parse-xml.js +2 -2
  8. package/dist/lib/parsers/parse-xml.js.map +1 -0
  9. package/dist/{esm/lib → lib}/parsers/streaming-xml-parser.js +6 -7
  10. package/dist/lib/parsers/streaming-xml-parser.js.map +1 -0
  11. package/dist/lib/xml-utils/uncapitalize.js.map +1 -0
  12. package/dist/lib/xml-utils/xml-utils.js.map +1 -0
  13. package/dist/{esm/sax-ts → sax-ts}/sax.js +75 -76
  14. package/dist/sax-ts/sax.js.map +1 -0
  15. package/dist/{esm/xml-loader.js → xml-loader.js} +2 -2
  16. package/dist/xml-loader.js.map +1 -0
  17. package/package.json +16 -8
  18. package/dist/bundle.d.ts +0 -2
  19. package/dist/bundle.d.ts.map +0 -1
  20. package/dist/es5/bundle.js +0 -6
  21. package/dist/es5/bundle.js.map +0 -1
  22. package/dist/es5/html-loader.js +0 -58
  23. package/dist/es5/html-loader.js.map +0 -1
  24. package/dist/es5/index.js +0 -53
  25. package/dist/es5/index.js.map +0 -1
  26. package/dist/es5/lib/parsers/parse-xml.js +0 -53
  27. package/dist/es5/lib/parsers/parse-xml.js.map +0 -1
  28. package/dist/es5/lib/parsers/streaming-xml-parser.js +0 -134
  29. package/dist/es5/lib/parsers/streaming-xml-parser.js.map +0 -1
  30. package/dist/es5/lib/xml-utils/uncapitalize.js +0 -32
  31. package/dist/es5/lib/xml-utils/uncapitalize.js.map +0 -1
  32. package/dist/es5/lib/xml-utils/xml-utils.js +0 -23
  33. package/dist/es5/lib/xml-utils/xml-utils.js.map +0 -1
  34. package/dist/es5/sax-ts/sax.js +0 -1372
  35. package/dist/es5/sax-ts/sax.js.map +0 -1
  36. package/dist/es5/xml-loader.js +0 -58
  37. package/dist/es5/xml-loader.js.map +0 -1
  38. package/dist/esm/bundle.js +0 -4
  39. package/dist/esm/bundle.js.map +0 -1
  40. package/dist/esm/html-loader.js.map +0 -1
  41. package/dist/esm/index.js +0 -6
  42. package/dist/esm/index.js.map +0 -1
  43. package/dist/esm/lib/parsers/parse-xml.js.map +0 -1
  44. package/dist/esm/lib/parsers/streaming-xml-parser.js.map +0 -1
  45. package/dist/esm/lib/xml-utils/uncapitalize.js.map +0 -1
  46. package/dist/esm/lib/xml-utils/xml-utils.js.map +0 -1
  47. package/dist/esm/sax-ts/sax.js.map +0 -1
  48. package/dist/esm/xml-loader.js.map +0 -1
  49. package/src/bundle.ts +0 -4
  50. /package/dist/{esm/lib → lib}/xml-utils/uncapitalize.js +0 -0
  51. /package/dist/{esm/lib → lib}/xml-utils/xml-utils.js +0 -0
package/dist/index.cjs ADDED
@@ -0,0 +1,1530 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ HTMLLoader: () => HTMLLoader,
24
+ SAXParser: () => SAXParser,
25
+ XMLLoader: () => XMLLoader,
26
+ _uncapitalize: () => uncapitalize,
27
+ _uncapitalizeKeys: () => uncapitalizeKeys,
28
+ convertXMLFieldToArrayInPlace: () => convertXMLFieldToArrayInPlace,
29
+ convertXMLValueToArray: () => convertXMLValueToArray
30
+ });
31
+ module.exports = __toCommonJS(src_exports);
32
+
33
+ // src/sax-ts/sax.ts
34
+ var DEFAULT_SAX_EVENTS = {
35
+ ontext: () => {
36
+ },
37
+ onprocessinginstruction: () => {
38
+ },
39
+ onsgmldeclaration: () => {
40
+ },
41
+ ondoctype: () => {
42
+ },
43
+ oncomment: () => {
44
+ },
45
+ onopentagstart: () => {
46
+ },
47
+ onattribute: () => {
48
+ },
49
+ onopentag: () => {
50
+ },
51
+ onclosetag: () => {
52
+ },
53
+ onopencdata: () => {
54
+ },
55
+ oncdata: () => {
56
+ },
57
+ onclosecdata: () => {
58
+ },
59
+ onerror: () => {
60
+ },
61
+ onend: () => {
62
+ },
63
+ onready: () => {
64
+ },
65
+ onscript: () => {
66
+ },
67
+ onopennamespace: () => {
68
+ },
69
+ onclosenamespace: () => {
70
+ }
71
+ };
72
+ var DEFAULT_SAX_PARSER_OPTIONS = {
73
+ ...DEFAULT_SAX_EVENTS,
74
+ strict: false,
75
+ MAX_BUFFER_LENGTH: 64 * 1024,
76
+ lowercase: false,
77
+ lowercasetags: false,
78
+ noscript: false,
79
+ strictEntities: false,
80
+ xmlns: void 0,
81
+ position: void 0,
82
+ trim: void 0,
83
+ normalize: void 0
84
+ };
85
+ var EVENTS = [
86
+ "text",
87
+ "processinginstruction",
88
+ "sgmldeclaration",
89
+ "doctype",
90
+ "comment",
91
+ "opentagstart",
92
+ "attribute",
93
+ "opentag",
94
+ "closetag",
95
+ "opencdata",
96
+ "cdata",
97
+ "closecdata",
98
+ "error",
99
+ "end",
100
+ "ready",
101
+ "script",
102
+ "opennamespace",
103
+ "closenamespace"
104
+ ];
105
+ var BUFFERS = [
106
+ "comment",
107
+ "sgmlDecl",
108
+ "textNode",
109
+ "tagName",
110
+ "doctype",
111
+ "procInstName",
112
+ "procInstBody",
113
+ "entity",
114
+ "attribName",
115
+ "attribValue",
116
+ "cdata",
117
+ "script"
118
+ ];
119
+ var 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]/;
120
+ var 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-]/;
121
+ var 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]/;
122
+ var 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-]/;
123
+ var ENTITIES = {
124
+ amp: "&",
125
+ gt: ">",
126
+ lt: "<",
127
+ quot: '"',
128
+ apos: "'",
129
+ AElig: 198,
130
+ Aacute: 193,
131
+ Acirc: 194,
132
+ Agrave: 192,
133
+ Aring: 197,
134
+ Atilde: 195,
135
+ Auml: 196,
136
+ Ccedil: 199,
137
+ ETH: 208,
138
+ Eacute: 201,
139
+ Ecirc: 202,
140
+ Egrave: 200,
141
+ Euml: 203,
142
+ Iacute: 205,
143
+ Icirc: 206,
144
+ Igrave: 204,
145
+ Iuml: 207,
146
+ Ntilde: 209,
147
+ Oacute: 211,
148
+ Ocirc: 212,
149
+ Ograve: 210,
150
+ Oslash: 216,
151
+ Otilde: 213,
152
+ Ouml: 214,
153
+ THORN: 222,
154
+ Uacute: 218,
155
+ Ucirc: 219,
156
+ Ugrave: 217,
157
+ Uuml: 220,
158
+ Yacute: 221,
159
+ aacute: 225,
160
+ acirc: 226,
161
+ aelig: 230,
162
+ agrave: 224,
163
+ aring: 229,
164
+ atilde: 227,
165
+ auml: 228,
166
+ ccedil: 231,
167
+ eacute: 233,
168
+ ecirc: 234,
169
+ egrave: 232,
170
+ eth: 240,
171
+ euml: 235,
172
+ iacute: 237,
173
+ icirc: 238,
174
+ igrave: 236,
175
+ iuml: 239,
176
+ ntilde: 241,
177
+ oacute: 243,
178
+ ocirc: 244,
179
+ ograve: 242,
180
+ oslash: 248,
181
+ otilde: 245,
182
+ ouml: 246,
183
+ szlig: 223,
184
+ thorn: 254,
185
+ uacute: 250,
186
+ ucirc: 251,
187
+ ugrave: 249,
188
+ uuml: 252,
189
+ yacute: 253,
190
+ yuml: 255,
191
+ copy: 169,
192
+ reg: 174,
193
+ nbsp: 160,
194
+ iexcl: 161,
195
+ cent: 162,
196
+ pound: 163,
197
+ curren: 164,
198
+ yen: 165,
199
+ brvbar: 166,
200
+ sect: 167,
201
+ uml: 168,
202
+ ordf: 170,
203
+ laquo: 171,
204
+ not: 172,
205
+ shy: 173,
206
+ macr: 175,
207
+ deg: 176,
208
+ plusmn: 177,
209
+ sup1: 185,
210
+ sup2: 178,
211
+ sup3: 179,
212
+ acute: 180,
213
+ micro: 181,
214
+ para: 182,
215
+ middot: 183,
216
+ cedil: 184,
217
+ ordm: 186,
218
+ raquo: 187,
219
+ frac14: 188,
220
+ frac12: 189,
221
+ frac34: 190,
222
+ iquest: 191,
223
+ times: 215,
224
+ divide: 247,
225
+ OElig: 338,
226
+ oelig: 339,
227
+ Scaron: 352,
228
+ scaron: 353,
229
+ Yuml: 376,
230
+ fnof: 402,
231
+ circ: 710,
232
+ tilde: 732,
233
+ Alpha: 913,
234
+ Beta: 914,
235
+ Gamma: 915,
236
+ Delta: 916,
237
+ Epsilon: 917,
238
+ Zeta: 918,
239
+ Eta: 919,
240
+ Theta: 920,
241
+ Iota: 921,
242
+ Kappa: 922,
243
+ Lambda: 923,
244
+ Mu: 924,
245
+ Nu: 925,
246
+ Xi: 926,
247
+ Omicron: 927,
248
+ Pi: 928,
249
+ Rho: 929,
250
+ Sigma: 931,
251
+ Tau: 932,
252
+ Upsilon: 933,
253
+ Phi: 934,
254
+ Chi: 935,
255
+ Psi: 936,
256
+ Omega: 937,
257
+ alpha: 945,
258
+ beta: 946,
259
+ gamma: 947,
260
+ delta: 948,
261
+ epsilon: 949,
262
+ zeta: 950,
263
+ eta: 951,
264
+ theta: 952,
265
+ iota: 953,
266
+ kappa: 954,
267
+ lambda: 955,
268
+ mu: 956,
269
+ nu: 957,
270
+ xi: 958,
271
+ omicron: 959,
272
+ pi: 960,
273
+ rho: 961,
274
+ sigmaf: 962,
275
+ sigma: 963,
276
+ tau: 964,
277
+ upsilon: 965,
278
+ phi: 966,
279
+ chi: 967,
280
+ psi: 968,
281
+ omega: 969,
282
+ thetasym: 977,
283
+ upsih: 978,
284
+ piv: 982,
285
+ ensp: 8194,
286
+ emsp: 8195,
287
+ thinsp: 8201,
288
+ zwnj: 8204,
289
+ zwj: 8205,
290
+ lrm: 8206,
291
+ rlm: 8207,
292
+ ndash: 8211,
293
+ mdash: 8212,
294
+ lsquo: 8216,
295
+ rsquo: 8217,
296
+ sbquo: 8218,
297
+ ldquo: 8220,
298
+ rdquo: 8221,
299
+ bdquo: 8222,
300
+ dagger: 8224,
301
+ Dagger: 8225,
302
+ bull: 8226,
303
+ hellip: 8230,
304
+ permil: 8240,
305
+ prime: 8242,
306
+ Prime: 8243,
307
+ lsaquo: 8249,
308
+ rsaquo: 8250,
309
+ oline: 8254,
310
+ frasl: 8260,
311
+ euro: 8364,
312
+ image: 8465,
313
+ weierp: 8472,
314
+ real: 8476,
315
+ trade: 8482,
316
+ alefsym: 8501,
317
+ larr: 8592,
318
+ uarr: 8593,
319
+ rarr: 8594,
320
+ darr: 8595,
321
+ harr: 8596,
322
+ crarr: 8629,
323
+ lArr: 8656,
324
+ uArr: 8657,
325
+ rArr: 8658,
326
+ dArr: 8659,
327
+ hArr: 8660,
328
+ forall: 8704,
329
+ part: 8706,
330
+ exist: 8707,
331
+ empty: 8709,
332
+ nabla: 8711,
333
+ isin: 8712,
334
+ notin: 8713,
335
+ ni: 8715,
336
+ prod: 8719,
337
+ sum: 8721,
338
+ minus: 8722,
339
+ lowast: 8727,
340
+ radic: 8730,
341
+ prop: 8733,
342
+ infin: 8734,
343
+ ang: 8736,
344
+ and: 8743,
345
+ or: 8744,
346
+ cap: 8745,
347
+ cup: 8746,
348
+ int: 8747,
349
+ there4: 8756,
350
+ sim: 8764,
351
+ cong: 8773,
352
+ asymp: 8776,
353
+ ne: 8800,
354
+ equiv: 8801,
355
+ le: 8804,
356
+ ge: 8805,
357
+ sub: 8834,
358
+ sup: 8835,
359
+ nsub: 8836,
360
+ sube: 8838,
361
+ supe: 8839,
362
+ oplus: 8853,
363
+ otimes: 8855,
364
+ perp: 8869,
365
+ sdot: 8901,
366
+ lceil: 8968,
367
+ rceil: 8969,
368
+ lfloor: 8970,
369
+ rfloor: 8971,
370
+ lang: 9001,
371
+ rang: 9002,
372
+ loz: 9674,
373
+ spades: 9824,
374
+ clubs: 9827,
375
+ hearts: 9829,
376
+ diams: 9830
377
+ };
378
+ Object.keys(ENTITIES).forEach((key) => {
379
+ const e = ENTITIES[key];
380
+ ENTITIES[key] = typeof e === "number" ? String.fromCharCode(e) : e;
381
+ });
382
+ var SAX = class {
383
+ constructor() {
384
+ this.EVENTS = EVENTS;
385
+ this.ENTITIES = {
386
+ // TODO: make it readonly, needed for entity-mega test
387
+ // amp, gt, lt, quot and apos are resolved to strings instead of numerical
388
+ // codes, IDK why
389
+ ...ENTITIES
390
+ };
391
+ this.XML_ENTITIES = {
392
+ amp: "&",
393
+ gt: ">",
394
+ lt: "<",
395
+ quot: '"',
396
+ apos: "'"
397
+ };
398
+ this.S = 0;
399
+ this.trackPosition = false;
400
+ this.column = 0;
401
+ this.line = 0;
402
+ this.c = "";
403
+ this.q = "";
404
+ this.closed = false;
405
+ this.tags = [];
406
+ this.looseCase = "";
407
+ this.closedRoot = false;
408
+ this.sawRoot = false;
409
+ this.strict = false;
410
+ this.noscript = false;
411
+ this.attribList = [];
412
+ this.position = 0;
413
+ this.STATE = {
414
+ BEGIN: this.S++,
415
+ // leading byte order mark or whitespace
416
+ BEGIN_WHITESPACE: this.S++,
417
+ // leading whitespace
418
+ TEXT: this.S++,
419
+ // general stuff
420
+ TEXT_ENTITY: this.S++,
421
+ // &amp and such.
422
+ OPEN_WAKA: this.S++,
423
+ // <
424
+ SGML_DECL: this.S++,
425
+ // <!BLARG
426
+ SGML_DECL_QUOTED: this.S++,
427
+ // <!BLARG foo "bar
428
+ DOCTYPE: this.S++,
429
+ // <!DOCTYPE
430
+ DOCTYPE_QUOTED: this.S++,
431
+ // <!DOCTYPE "//blah
432
+ DOCTYPE_DTD: this.S++,
433
+ // <!DOCTYPE "//blah" [ ...
434
+ DOCTYPE_DTD_QUOTED: this.S++,
435
+ // <!DOCTYPE "//blah" [ "foo
436
+ COMMENT_STARTING: this.S++,
437
+ // <!-
438
+ COMMENT: this.S++,
439
+ // <!--
440
+ COMMENT_ENDING: this.S++,
441
+ // <!-- blah -
442
+ COMMENT_ENDED: this.S++,
443
+ // <!-- blah --
444
+ CDATA: this.S++,
445
+ // <![CDATA[ something
446
+ CDATA_ENDING: this.S++,
447
+ // ]
448
+ CDATA_ENDING_2: this.S++,
449
+ // ]]
450
+ PROC_INST: this.S++,
451
+ // <?hi
452
+ PROC_INST_BODY: this.S++,
453
+ // <?hi there
454
+ PROC_INST_ENDING: this.S++,
455
+ // <?hi "there" ?
456
+ OPEN_TAG: this.S++,
457
+ // <strong
458
+ OPEN_TAG_SLASH: this.S++,
459
+ // <strong /
460
+ ATTRIB: this.S++,
461
+ // <a
462
+ ATTRIB_NAME: this.S++,
463
+ // <a foo
464
+ ATTRIB_NAME_SAW_WHITE: this.S++,
465
+ // <a foo _
466
+ ATTRIB_VALUE: this.S++,
467
+ // <a foo=
468
+ ATTRIB_VALUE_QUOTED: this.S++,
469
+ // <a foo="bar
470
+ ATTRIB_VALUE_CLOSED: this.S++,
471
+ // <a foo="bar"
472
+ ATTRIB_VALUE_UNQUOTED: this.S++,
473
+ // <a foo=bar
474
+ ATTRIB_VALUE_ENTITY_Q: this.S++,
475
+ // <foo bar="&quot;"
476
+ ATTRIB_VALUE_ENTITY_U: this.S++,
477
+ // <foo bar=&quot
478
+ CLOSE_TAG: this.S++,
479
+ // </a
480
+ CLOSE_TAG_SAW_WHITE: this.S++,
481
+ // </a >
482
+ SCRIPT: this.S++,
483
+ // <script> ...
484
+ SCRIPT_ENDING: this.S++
485
+ // <script> ... <
486
+ };
487
+ this.BUFFERS = BUFFERS;
488
+ // private parser: (strict: boolean, opt: any) => SAXParser;
489
+ this.CDATA = "[CDATA[";
490
+ this.DOCTYPE = "DOCTYPE";
491
+ this.XML_NAMESPACE = "http://www.w3.org/XML/1998/namespace";
492
+ this.XMLNS_NAMESPACE = "http://www.w3.org/2000/xmlns/";
493
+ this.rootNS = {
494
+ xml: this.XML_NAMESPACE,
495
+ xmlns: this.XMLNS_NAMESPACE
496
+ };
497
+ this.textNode = "";
498
+ this.entity = "";
499
+ this.cdata = "";
500
+ this.script = "";
501
+ this.startTagPosition = 0;
502
+ this.S = 0;
503
+ for (const s in this.STATE) {
504
+ if (this.STATE.hasOwnProperty(s)) {
505
+ this.STATE[this.STATE[s]] = s;
506
+ }
507
+ }
508
+ this.S = this.STATE;
509
+ }
510
+ static charAt(chunk, i) {
511
+ let result = "";
512
+ if (i < chunk.length) {
513
+ result = chunk.charAt(i);
514
+ }
515
+ return result;
516
+ }
517
+ static isWhitespace(c) {
518
+ return c === " " || c === "\n" || c === "\r" || c === " ";
519
+ }
520
+ static isQuote(c) {
521
+ return c === '"' || c === "'";
522
+ }
523
+ static isAttribEnd(c) {
524
+ return c === ">" || SAX.isWhitespace(c);
525
+ }
526
+ static isMatch(regex, c) {
527
+ return regex.test(c);
528
+ }
529
+ static notMatch(regex, c) {
530
+ return !SAX.isMatch(regex, c);
531
+ }
532
+ static qname(name, attribute) {
533
+ const i = name.indexOf(":");
534
+ const qualName = i < 0 ? ["", name] : name.split(":");
535
+ let prefix = qualName[0];
536
+ let local = qualName[1];
537
+ if (attribute && name === "xmlns") {
538
+ prefix = "xmlns";
539
+ local = "";
540
+ }
541
+ return { prefix, local };
542
+ }
543
+ write(chunk) {
544
+ if (this.error) {
545
+ throw this.error;
546
+ }
547
+ if (this.closed) {
548
+ return this.errorFunction("Cannot write after close. Assign an onready handler.");
549
+ }
550
+ if (chunk === null) {
551
+ return this.end();
552
+ }
553
+ if (typeof chunk === "object") {
554
+ chunk = chunk.toString();
555
+ }
556
+ let i = 0;
557
+ let c;
558
+ while (true) {
559
+ c = SAX.charAt(chunk, i++);
560
+ this.c = c;
561
+ if (!c) {
562
+ break;
563
+ }
564
+ if (this.trackPosition) {
565
+ this.position++;
566
+ if (c === "\n") {
567
+ this.line++;
568
+ this.column = 0;
569
+ } else {
570
+ this.column++;
571
+ }
572
+ }
573
+ switch (this.state) {
574
+ case this.S.BEGIN:
575
+ this.state = this.S.BEGIN_WHITESPACE;
576
+ if (c === "\uFEFF") {
577
+ continue;
578
+ }
579
+ this.beginWhiteSpace(c);
580
+ continue;
581
+ case this.S.BEGIN_WHITESPACE:
582
+ this.beginWhiteSpace(c);
583
+ continue;
584
+ case this.S.TEXT:
585
+ if (this.sawRoot && !this.closedRoot) {
586
+ const starti = i - 1;
587
+ while (c && c !== "<" && c !== "&") {
588
+ c = SAX.charAt(chunk, i++);
589
+ if (c && this.trackPosition) {
590
+ this.position++;
591
+ if (c === "\n") {
592
+ this.line++;
593
+ this.column = 0;
594
+ } else {
595
+ this.column++;
596
+ }
597
+ }
598
+ }
599
+ this.textNode += chunk.substring(starti, i - 1);
600
+ }
601
+ if (c === "<" && !(this.sawRoot && this.closedRoot && !this.strict)) {
602
+ this.state = this.S.OPEN_WAKA;
603
+ this.startTagPosition = this.position;
604
+ } else {
605
+ if (!SAX.isWhitespace(c) && (!this.sawRoot || this.closedRoot)) {
606
+ this.strictFail("Text data outside of root node.");
607
+ }
608
+ if (c === "&") {
609
+ this.state = this.S.TEXT_ENTITY;
610
+ } else {
611
+ this.textNode += c;
612
+ }
613
+ }
614
+ continue;
615
+ case this.S.SCRIPT:
616
+ if (c === "<") {
617
+ this.state = this.S.SCRIPT_ENDING;
618
+ } else {
619
+ this.script += c;
620
+ }
621
+ continue;
622
+ case this.S.SCRIPT_ENDING:
623
+ if (c === "/") {
624
+ this.state = this.S.CLOSE_TAG;
625
+ } else {
626
+ this.script += `<${c}`;
627
+ this.state = this.S.SCRIPT;
628
+ }
629
+ continue;
630
+ case this.S.OPEN_WAKA:
631
+ if (c === "!") {
632
+ this.state = this.S.SGML_DECL;
633
+ this.sgmlDecl = "";
634
+ } else if (SAX.isWhitespace(c)) {
635
+ } else if (SAX.isMatch(nameStart, c)) {
636
+ this.state = this.S.OPEN_TAG;
637
+ this.tagName = c;
638
+ } else if (c === "/") {
639
+ this.state = this.S.CLOSE_TAG;
640
+ this.tagName = "";
641
+ } else if (c === "?") {
642
+ this.state = this.S.PROC_INST;
643
+ this.procInstName = this.procInstBody = "";
644
+ } else {
645
+ this.strictFail("Unencoded <");
646
+ if (this.startTagPosition + 1 < this.position) {
647
+ const pad = this.position - this.startTagPosition;
648
+ c = new Array(pad).join(" ") + c;
649
+ }
650
+ this.textNode += `<${c}`;
651
+ this.state = this.S.TEXT;
652
+ }
653
+ continue;
654
+ case this.S.SGML_DECL:
655
+ if ((this.sgmlDecl + c).toUpperCase() === this.CDATA) {
656
+ this.emitNode("onopencdata");
657
+ this.state = this.S.CDATA;
658
+ this.sgmlDecl = "";
659
+ this.cdata = "";
660
+ } else if (this.sgmlDecl + c === "--") {
661
+ this.state = this.S.COMMENT;
662
+ this.comment = "";
663
+ this.sgmlDecl = "";
664
+ } else if ((this.sgmlDecl + c).toUpperCase() === this.DOCTYPE) {
665
+ this.state = this.S.DOCTYPE;
666
+ if (this.doctype || this.sawRoot) {
667
+ this.strictFail("Inappropriately located doctype declaration");
668
+ }
669
+ this.doctype = "";
670
+ this.sgmlDecl = "";
671
+ } else if (c === ">") {
672
+ this.emitNode("onsgmldeclaration", this.sgmlDecl);
673
+ this.sgmlDecl = "";
674
+ this.state = this.S.TEXT;
675
+ } else if (SAX.isQuote(c)) {
676
+ this.state = this.S.SGML_DECL_QUOTED;
677
+ this.sgmlDecl += c;
678
+ } else {
679
+ this.sgmlDecl += c;
680
+ }
681
+ continue;
682
+ case this.S.SGML_DECL_QUOTED:
683
+ if (c === this.q) {
684
+ this.state = this.S.SGML_DECL;
685
+ this.q = "";
686
+ }
687
+ this.sgmlDecl += c;
688
+ continue;
689
+ case this.S.DOCTYPE:
690
+ if (c === ">") {
691
+ this.state = this.S.TEXT;
692
+ this.emitNode("ondoctype", this.doctype);
693
+ this.doctype = true;
694
+ } else {
695
+ this.doctype += c;
696
+ if (c === "[") {
697
+ this.state = this.S.DOCTYPE_DTD;
698
+ } else if (SAX.isQuote(c)) {
699
+ this.state = this.S.DOCTYPE_QUOTED;
700
+ this.q = c;
701
+ }
702
+ }
703
+ continue;
704
+ case this.S.DOCTYPE_QUOTED:
705
+ this.doctype += c;
706
+ if (c === this.q) {
707
+ this.q = "";
708
+ this.state = this.S.DOCTYPE;
709
+ }
710
+ continue;
711
+ case this.S.DOCTYPE_DTD:
712
+ this.doctype += c;
713
+ if (c === "]") {
714
+ this.state = this.S.DOCTYPE;
715
+ } else if (SAX.isQuote(c)) {
716
+ this.state = this.S.DOCTYPE_DTD_QUOTED;
717
+ this.q = c;
718
+ }
719
+ continue;
720
+ case this.S.DOCTYPE_DTD_QUOTED:
721
+ this.doctype += c;
722
+ if (c === this.q) {
723
+ this.state = this.S.DOCTYPE_DTD;
724
+ this.q = "";
725
+ }
726
+ continue;
727
+ case this.S.COMMENT:
728
+ if (c === "-") {
729
+ this.state = this.S.COMMENT_ENDING;
730
+ } else {
731
+ this.comment += c;
732
+ }
733
+ continue;
734
+ case this.S.COMMENT_ENDING:
735
+ if (c === "-") {
736
+ this.state = this.S.COMMENT_ENDED;
737
+ this.comment = this.textApplyOptions(this.comment);
738
+ if (this.comment) {
739
+ this.emitNode("oncomment", this.comment);
740
+ }
741
+ this.comment = "";
742
+ } else {
743
+ this.comment += `-${c}`;
744
+ this.state = this.S.COMMENT;
745
+ }
746
+ continue;
747
+ case this.S.COMMENT_ENDED:
748
+ if (c !== ">") {
749
+ this.strictFail("Malformed comment");
750
+ this.comment += `--${c}`;
751
+ this.state = this.S.COMMENT;
752
+ } else {
753
+ this.state = this.S.TEXT;
754
+ }
755
+ continue;
756
+ case this.S.CDATA:
757
+ if (c === "]") {
758
+ this.state = this.S.CDATA_ENDING;
759
+ } else {
760
+ this.cdata += c;
761
+ }
762
+ continue;
763
+ case this.S.CDATA_ENDING:
764
+ if (c === "]") {
765
+ this.state = this.S.CDATA_ENDING_2;
766
+ } else {
767
+ this.cdata += `]${c}`;
768
+ this.state = this.S.CDATA;
769
+ }
770
+ continue;
771
+ case this.S.CDATA_ENDING_2:
772
+ if (c === ">") {
773
+ if (this.cdata) {
774
+ this.emitNode("oncdata", this.cdata);
775
+ }
776
+ this.emitNode("onclosecdata");
777
+ this.cdata = "";
778
+ this.state = this.S.TEXT;
779
+ } else if (c === "]") {
780
+ this.cdata += "]";
781
+ } else {
782
+ this.cdata += `]]${c}`;
783
+ this.state = this.S.CDATA;
784
+ }
785
+ continue;
786
+ case this.S.PROC_INST:
787
+ if (c === "?") {
788
+ this.state = this.S.PROC_INST_ENDING;
789
+ } else if (SAX.isWhitespace(c)) {
790
+ this.state = this.S.PROC_INST_BODY;
791
+ } else {
792
+ this.procInstName += c;
793
+ }
794
+ continue;
795
+ case this.S.PROC_INST_BODY:
796
+ if (!this.procInstBody && SAX.isWhitespace(c)) {
797
+ continue;
798
+ } else if (c === "?") {
799
+ this.state = this.S.PROC_INST_ENDING;
800
+ } else {
801
+ this.procInstBody += c;
802
+ }
803
+ continue;
804
+ case this.S.PROC_INST_ENDING:
805
+ if (c === ">") {
806
+ this.emitNode("onprocessinginstruction", {
807
+ name: this.procInstName,
808
+ body: this.procInstBody
809
+ });
810
+ this.procInstName = this.procInstBody = "";
811
+ this.state = this.S.TEXT;
812
+ } else {
813
+ this.procInstBody += `?${c}`;
814
+ this.state = this.S.PROC_INST_BODY;
815
+ }
816
+ continue;
817
+ case this.S.OPEN_TAG:
818
+ if (SAX.isMatch(nameBody, c)) {
819
+ this.tagName += c;
820
+ } else {
821
+ this.newTag();
822
+ if (c === ">") {
823
+ this.openTag();
824
+ } else if (c === "/") {
825
+ this.state = this.S.OPEN_TAG_SLASH;
826
+ } else {
827
+ if (!SAX.isWhitespace(c)) {
828
+ this.strictFail("Invalid character in tag name");
829
+ }
830
+ this.state = this.S.ATTRIB;
831
+ }
832
+ }
833
+ continue;
834
+ case this.S.OPEN_TAG_SLASH:
835
+ if (c === ">") {
836
+ this.openTag(true);
837
+ this.closeTag();
838
+ } else {
839
+ this.strictFail("Forward-slash in opening tag not followed by >");
840
+ this.state = this.S.ATTRIB;
841
+ }
842
+ continue;
843
+ case this.S.ATTRIB:
844
+ if (SAX.isWhitespace(c)) {
845
+ continue;
846
+ } else if (c === ">") {
847
+ this.openTag();
848
+ } else if (c === "/") {
849
+ this.state = this.S.OPEN_TAG_SLASH;
850
+ } else if (SAX.isMatch(nameStart, c)) {
851
+ this.attribName = c;
852
+ this.attribValue = "";
853
+ this.state = this.S.ATTRIB_NAME;
854
+ } else {
855
+ this.strictFail("Invalid attribute name");
856
+ }
857
+ continue;
858
+ case this.S.ATTRIB_NAME:
859
+ if (c === "=") {
860
+ this.state = this.S.ATTRIB_VALUE;
861
+ } else if (c === ">") {
862
+ this.strictFail("Attribute without value");
863
+ this.attribValue = this.attribName;
864
+ this.attrib();
865
+ this.openTag();
866
+ } else if (SAX.isWhitespace(c)) {
867
+ this.state = this.S.ATTRIB_NAME_SAW_WHITE;
868
+ } else if (SAX.isMatch(nameBody, c)) {
869
+ this.attribName += c;
870
+ } else {
871
+ this.strictFail("Invalid attribute name");
872
+ }
873
+ continue;
874
+ case this.S.ATTRIB_NAME_SAW_WHITE:
875
+ if (c === "=") {
876
+ this.state = this.S.ATTRIB_VALUE;
877
+ } else if (SAX.isWhitespace(c)) {
878
+ continue;
879
+ } else {
880
+ this.strictFail("Attribute without value");
881
+ this.tag.attributes[this.attribName] = "";
882
+ this.attribValue = "";
883
+ this.emitNode("onattribute", {
884
+ name: this.attribName,
885
+ value: ""
886
+ });
887
+ this.attribName = "";
888
+ if (c === ">") {
889
+ this.openTag();
890
+ } else if (SAX.isMatch(nameStart, c)) {
891
+ this.attribName = c;
892
+ this.state = this.S.ATTRIB_NAME;
893
+ } else {
894
+ this.strictFail("Invalid attribute name");
895
+ this.state = this.S.ATTRIB;
896
+ }
897
+ }
898
+ continue;
899
+ case this.S.ATTRIB_VALUE:
900
+ if (SAX.isWhitespace(c)) {
901
+ continue;
902
+ } else if (SAX.isQuote(c)) {
903
+ this.q = c;
904
+ this.state = this.S.ATTRIB_VALUE_QUOTED;
905
+ } else {
906
+ this.strictFail("Unquoted attribute value");
907
+ this.state = this.S.ATTRIB_VALUE_UNQUOTED;
908
+ this.attribValue = c;
909
+ }
910
+ continue;
911
+ case this.S.ATTRIB_VALUE_QUOTED:
912
+ if (c !== this.q) {
913
+ if (c === "&") {
914
+ this.state = this.S.ATTRIB_VALUE_ENTITY_Q;
915
+ } else {
916
+ this.attribValue += c;
917
+ }
918
+ continue;
919
+ }
920
+ this.attrib();
921
+ this.q = "";
922
+ this.state = this.S.ATTRIB_VALUE_CLOSED;
923
+ continue;
924
+ case this.S.ATTRIB_VALUE_CLOSED:
925
+ if (SAX.isWhitespace(c)) {
926
+ this.state = this.S.ATTRIB;
927
+ } else if (c === ">") {
928
+ this.openTag();
929
+ } else if (c === "/") {
930
+ this.state = this.S.OPEN_TAG_SLASH;
931
+ } else if (SAX.isMatch(nameStart, c)) {
932
+ this.strictFail("No whitespace between attributes");
933
+ this.attribName = c;
934
+ this.attribValue = "";
935
+ this.state = this.S.ATTRIB_NAME;
936
+ } else {
937
+ this.strictFail("Invalid attribute name");
938
+ }
939
+ continue;
940
+ case this.S.ATTRIB_VALUE_UNQUOTED:
941
+ if (!SAX.isAttribEnd(c)) {
942
+ if (c === "&") {
943
+ this.state = this.S.ATTRIB_VALUE_ENTITY_U;
944
+ } else {
945
+ this.attribValue += c;
946
+ }
947
+ continue;
948
+ }
949
+ this.attrib();
950
+ if (c === ">") {
951
+ this.openTag();
952
+ } else {
953
+ this.state = this.S.ATTRIB;
954
+ }
955
+ continue;
956
+ case this.S.CLOSE_TAG:
957
+ if (!this.tagName) {
958
+ if (SAX.isWhitespace(c)) {
959
+ continue;
960
+ } else if (SAX.notMatch(nameStart, c)) {
961
+ if (this.script) {
962
+ this.script += `</${c}`;
963
+ this.state = this.S.SCRIPT;
964
+ } else {
965
+ this.strictFail("Invalid tagname in closing tag.");
966
+ }
967
+ } else {
968
+ this.tagName = c;
969
+ }
970
+ } else if (c === ">") {
971
+ this.closeTag();
972
+ } else if (SAX.isMatch(nameBody, c)) {
973
+ this.tagName += c;
974
+ } else if (this.script) {
975
+ this.script += `</${this.tagName}`;
976
+ this.tagName = "";
977
+ this.state = this.S.SCRIPT;
978
+ } else {
979
+ if (!SAX.isWhitespace(c)) {
980
+ this.strictFail("Invalid tagname in closing tag");
981
+ }
982
+ this.state = this.S.CLOSE_TAG_SAW_WHITE;
983
+ }
984
+ continue;
985
+ case this.S.CLOSE_TAG_SAW_WHITE:
986
+ if (SAX.isWhitespace(c)) {
987
+ continue;
988
+ }
989
+ if (c === ">") {
990
+ this.closeTag();
991
+ } else {
992
+ this.strictFail("Invalid characters in closing tag");
993
+ }
994
+ continue;
995
+ case this.S.TEXT_ENTITY:
996
+ case this.S.ATTRIB_VALUE_ENTITY_Q:
997
+ case this.S.ATTRIB_VALUE_ENTITY_U:
998
+ let returnState;
999
+ let buffer;
1000
+ switch (this.state) {
1001
+ case this.S.TEXT_ENTITY:
1002
+ returnState = this.S.TEXT;
1003
+ buffer = "textNode";
1004
+ break;
1005
+ case this.S.ATTRIB_VALUE_ENTITY_Q:
1006
+ returnState = this.S.ATTRIB_VALUE_QUOTED;
1007
+ buffer = "attribValue";
1008
+ break;
1009
+ case this.S.ATTRIB_VALUE_ENTITY_U:
1010
+ returnState = this.S.ATTRIB_VALUE_UNQUOTED;
1011
+ buffer = "attribValue";
1012
+ break;
1013
+ default:
1014
+ throw new Error(`Unknown state: ${this.state}`);
1015
+ }
1016
+ if (c === ";") {
1017
+ this[buffer] += this.parseEntity();
1018
+ this.entity = "";
1019
+ this.state = returnState;
1020
+ } else if (SAX.isMatch(this.entity.length ? entityBody : entityStart, c)) {
1021
+ this.entity += c;
1022
+ } else {
1023
+ this.strictFail("Invalid character in entity name");
1024
+ this[buffer] += `&${this.entity}${c}`;
1025
+ this.entity = "";
1026
+ this.state = returnState;
1027
+ }
1028
+ continue;
1029
+ default:
1030
+ throw new Error(`Unknown state: ${this.state}`);
1031
+ }
1032
+ }
1033
+ if (this.position >= this.bufferCheckPosition) {
1034
+ this.checkBufferLength();
1035
+ }
1036
+ return this;
1037
+ }
1038
+ emit(event, data) {
1039
+ if (this.events.hasOwnProperty(event)) {
1040
+ const eventName = event.replace(/^on/, "");
1041
+ this.events[event](data, eventName, this);
1042
+ }
1043
+ }
1044
+ clearBuffers() {
1045
+ for (let i = 0, l = this.BUFFERS.length; i < l; i++) {
1046
+ this[this[i]] = "";
1047
+ }
1048
+ }
1049
+ flushBuffers() {
1050
+ this.closeText();
1051
+ if (this.cdata !== "") {
1052
+ this.emitNode("oncdata", this.cdata);
1053
+ this.cdata = "";
1054
+ }
1055
+ if (this.script !== "") {
1056
+ this.emitNode("onscript", this.script);
1057
+ this.script = "";
1058
+ }
1059
+ }
1060
+ end() {
1061
+ if (this.sawRoot && !this.closedRoot)
1062
+ this.strictFail("Unclosed root tag");
1063
+ if (this.state !== this.S.BEGIN && this.state !== this.S.BEGIN_WHITESPACE && this.state !== this.S.TEXT) {
1064
+ this.errorFunction("Unexpected end");
1065
+ }
1066
+ this.closeText();
1067
+ this.c = "";
1068
+ this.closed = true;
1069
+ this.emit("onend");
1070
+ return new SAXParser(this.opt);
1071
+ }
1072
+ errorFunction(er) {
1073
+ this.closeText();
1074
+ if (this.trackPosition) {
1075
+ er += `
1076
+ Line: ${this.line}
1077
+ Column: ${this.column}
1078
+ Char: ${this.c}`;
1079
+ }
1080
+ const error = new Error(er);
1081
+ this.error = error;
1082
+ this.emit("onerror", error);
1083
+ return this;
1084
+ }
1085
+ attrib() {
1086
+ if (!this.strict) {
1087
+ this.attribName = this.attribName[this.looseCase]();
1088
+ }
1089
+ if (this.attribList.indexOf(this.attribName) !== -1 || this.tag.attributes.hasOwnProperty(this.attribName)) {
1090
+ this.attribName = this.attribValue = "";
1091
+ return;
1092
+ }
1093
+ if (this.opt.xmlns) {
1094
+ const qn = SAX.qname(this.attribName, true);
1095
+ const prefix = qn.prefix;
1096
+ const local = qn.local;
1097
+ if (prefix === "xmlns") {
1098
+ if (local === "xml" && this.attribValue !== this.XML_NAMESPACE) {
1099
+ this.strictFail(
1100
+ `xml: prefix must be bound to ${this.XML_NAMESPACE}
1101
+ Actual: ${this.attribValue}`
1102
+ );
1103
+ } else if (local === "xmlns" && this.attribValue !== this.XMLNS_NAMESPACE) {
1104
+ this.strictFail(
1105
+ `xmlns: prefix must be bound to ${this.XMLNS_NAMESPACE}
1106
+ Actual: ${this.attribValue}`
1107
+ );
1108
+ } else {
1109
+ const tag = this.tag;
1110
+ const parent = this.tags[this.tags.length - 1] || this;
1111
+ if (tag.ns === parent.ns) {
1112
+ tag.ns = Object.create(parent.ns);
1113
+ }
1114
+ tag.ns[local] = this.attribValue;
1115
+ }
1116
+ }
1117
+ this.attribList.push([this.attribName, this.attribValue]);
1118
+ } else {
1119
+ this.tag.attributes[this.attribName] = this.attribValue;
1120
+ this.emitNode("onattribute", {
1121
+ name: this.attribName,
1122
+ value: this.attribValue
1123
+ });
1124
+ }
1125
+ this.attribName = this.attribValue = "";
1126
+ }
1127
+ newTag() {
1128
+ if (!this.strict)
1129
+ this.tagName = this.tagName[this.looseCase]();
1130
+ const parent = this.tags[this.tags.length - 1] || this;
1131
+ const tag = this.tag = { name: this.tagName, attributes: {} };
1132
+ if (this.opt.xmlns) {
1133
+ tag.ns = parent.ns;
1134
+ }
1135
+ this.attribList.length = 0;
1136
+ this.emitNode("onopentagstart", tag);
1137
+ }
1138
+ parseEntity() {
1139
+ let entity = this.entity;
1140
+ const entityLC = entity.toLowerCase();
1141
+ let num = NaN;
1142
+ let numStr = "";
1143
+ if (this.ENTITIES[entity]) {
1144
+ return this.ENTITIES[entity];
1145
+ }
1146
+ if (this.ENTITIES[entityLC]) {
1147
+ return this.ENTITIES[entityLC];
1148
+ }
1149
+ entity = entityLC;
1150
+ if (entity.charAt(0) === "#") {
1151
+ if (entity.charAt(1) === "x") {
1152
+ entity = entity.slice(2);
1153
+ num = parseInt(entity, 16);
1154
+ numStr = num.toString(16);
1155
+ } else {
1156
+ entity = entity.slice(1);
1157
+ num = parseInt(entity, 10);
1158
+ numStr = num.toString(10);
1159
+ }
1160
+ }
1161
+ entity = entity.replace(/^0+/, "");
1162
+ if (isNaN(num) || numStr.toLowerCase() !== entity) {
1163
+ this.strictFail("Invalid character entity");
1164
+ return `&${this.entity};`;
1165
+ }
1166
+ return String.fromCodePoint(num);
1167
+ }
1168
+ beginWhiteSpace(c) {
1169
+ if (c === "<") {
1170
+ this.state = this.S.OPEN_WAKA;
1171
+ this.startTagPosition = this.position;
1172
+ } else if (!SAX.isWhitespace(c)) {
1173
+ this.strictFail("Non-whitespace before first tag.");
1174
+ this.textNode = c;
1175
+ this.state = this.S.TEXT;
1176
+ } else {
1177
+ }
1178
+ }
1179
+ strictFail(message) {
1180
+ if (typeof this !== "object" || !(this instanceof SAXParser)) {
1181
+ throw new Error("bad call to strictFail");
1182
+ }
1183
+ if (this.strict) {
1184
+ this.errorFunction(message);
1185
+ }
1186
+ }
1187
+ textApplyOptions(text) {
1188
+ if (this.opt.trim)
1189
+ text = text.trim();
1190
+ if (this.opt.normalize)
1191
+ text = text.replace(/\s+/g, " ");
1192
+ return text;
1193
+ }
1194
+ emitNode(nodeType, data) {
1195
+ if (this.textNode)
1196
+ this.closeText();
1197
+ this.emit(nodeType, data);
1198
+ }
1199
+ closeText() {
1200
+ this.textNode = this.textApplyOptions(this.textNode);
1201
+ if (this.textNode !== void 0 && this.textNode !== "" && this.textNode !== "undefined") {
1202
+ this.emit("ontext", this.textNode);
1203
+ }
1204
+ this.textNode = "";
1205
+ }
1206
+ checkBufferLength() {
1207
+ var _a;
1208
+ const maxAllowed = Math.max(this.opt.MAX_BUFFER_LENGTH, 10);
1209
+ let maxActual = 0;
1210
+ for (let i = 0, l = this.BUFFERS.length; i < l; i++) {
1211
+ const len = ((_a = this[this.BUFFERS[i]]) == null ? void 0 : _a.length) || 0;
1212
+ if (len > maxAllowed) {
1213
+ switch (this.BUFFERS[i]) {
1214
+ case "textNode":
1215
+ this.closeText();
1216
+ break;
1217
+ case "cdata":
1218
+ this.emitNode("oncdata", this.cdata);
1219
+ this.cdata = "";
1220
+ break;
1221
+ case "script":
1222
+ this.emitNode("onscript", this.script);
1223
+ this.script = "";
1224
+ break;
1225
+ default:
1226
+ this.errorFunction(`Max buffer length exceeded: ${this.BUFFERS[i]}`);
1227
+ }
1228
+ }
1229
+ maxActual = Math.max(maxActual, len);
1230
+ }
1231
+ const m = this.opt.MAX_BUFFER_LENGTH - maxActual;
1232
+ this.bufferCheckPosition = m + this.position;
1233
+ }
1234
+ openTag(selfClosing) {
1235
+ if (this.opt.xmlns) {
1236
+ const tag = this.tag;
1237
+ const qn = SAX.qname(this.tagName);
1238
+ tag.prefix = qn.prefix;
1239
+ tag.local = qn.local;
1240
+ tag.uri = tag.ns[qn.prefix] || "";
1241
+ if (tag.prefix && !tag.uri) {
1242
+ this.strictFail(`Unbound namespace prefix: ${JSON.stringify(this.tagName)}`);
1243
+ tag.uri = qn.prefix;
1244
+ }
1245
+ const parent = this.tags[this.tags.length - 1] || this;
1246
+ if (tag.ns && parent.ns !== tag.ns) {
1247
+ const that = this;
1248
+ Object.keys(tag.ns).forEach((p) => {
1249
+ that.emitNode("onopennamespace", {
1250
+ prefix: p,
1251
+ uri: tag.ns[p]
1252
+ });
1253
+ });
1254
+ }
1255
+ for (let i = 0, l = this.attribList.length; i < l; i++) {
1256
+ const nv = this.attribList[i];
1257
+ const name = nv[0];
1258
+ const value = nv[1];
1259
+ const qualName = SAX.qname(name, true);
1260
+ const prefix = qualName.prefix;
1261
+ const local = qualName.local;
1262
+ const uri = prefix === "" ? "" : tag.ns[prefix] || "";
1263
+ const a = {
1264
+ name,
1265
+ value,
1266
+ prefix,
1267
+ local,
1268
+ uri
1269
+ };
1270
+ if (prefix && prefix !== "xmlns" && !uri) {
1271
+ this.strictFail(`Unbound namespace prefix: ${JSON.stringify(prefix)}`);
1272
+ a.uri = prefix;
1273
+ }
1274
+ this.tag.attributes[name] = a;
1275
+ this.emitNode("onattribute", a);
1276
+ }
1277
+ this.attribList.length = 0;
1278
+ }
1279
+ this.tag.isSelfClosing = Boolean(selfClosing);
1280
+ this.sawRoot = true;
1281
+ this.tags.push(this.tag);
1282
+ this.emitNode("onopentag", this.tag);
1283
+ if (!selfClosing) {
1284
+ if (!this.noscript && this.tagName.toLowerCase() === "script") {
1285
+ this.state = this.S.SCRIPT;
1286
+ } else {
1287
+ this.state = this.S.TEXT;
1288
+ }
1289
+ this.tag = null;
1290
+ this.tagName = "";
1291
+ }
1292
+ this.attribName = this.attribValue = "";
1293
+ this.attribList.length = 0;
1294
+ }
1295
+ closeTag() {
1296
+ if (!this.tagName) {
1297
+ this.strictFail("Weird empty close tag.");
1298
+ this.textNode += "</>";
1299
+ this.state = this.S.TEXT;
1300
+ return;
1301
+ }
1302
+ if (this.script) {
1303
+ if (this.tagName !== "script") {
1304
+ this.script += `</${this.tagName}>`;
1305
+ this.tagName = "";
1306
+ this.state = this.S.SCRIPT;
1307
+ return;
1308
+ }
1309
+ this.emitNode("onscript", this.script);
1310
+ this.script = "";
1311
+ }
1312
+ let t = this.tags.length;
1313
+ let tagName = this.tagName;
1314
+ if (!this.strict) {
1315
+ tagName = tagName[this.looseCase]();
1316
+ }
1317
+ while (t--) {
1318
+ const close = this.tags[t];
1319
+ if (close.name !== tagName) {
1320
+ this.strictFail("Unexpected close tag");
1321
+ } else {
1322
+ break;
1323
+ }
1324
+ }
1325
+ if (t < 0) {
1326
+ this.strictFail(`Unmatched closing tag: ${this.tagName}`);
1327
+ this.textNode += `</${this.tagName}>`;
1328
+ this.state = this.S.TEXT;
1329
+ return;
1330
+ }
1331
+ this.tagName = tagName;
1332
+ let s = this.tags.length;
1333
+ while (s-- > t) {
1334
+ const tag = this.tag = this.tags.pop();
1335
+ this.tagName = this.tag.name;
1336
+ this.emitNode("onclosetag", this.tagName);
1337
+ const x = {};
1338
+ for (const i in tag.ns) {
1339
+ if (tag.ns.hasOwnProperty(i)) {
1340
+ x[i] = tag.ns[i];
1341
+ }
1342
+ }
1343
+ const parent = this.tags[this.tags.length - 1] || this;
1344
+ if (this.opt.xmlns && tag.ns !== parent.ns) {
1345
+ const that = this;
1346
+ Object.keys(tag.ns).forEach((p) => {
1347
+ const n = tag.ns[p];
1348
+ that.emitNode("onclosenamespace", { prefix: p, uri: n });
1349
+ });
1350
+ }
1351
+ }
1352
+ if (t === 0)
1353
+ this.closedRoot = true;
1354
+ this.tagName = this.attribValue = this.attribName = "";
1355
+ this.attribList.length = 0;
1356
+ this.state = this.S.TEXT;
1357
+ }
1358
+ };
1359
+ var SAXParser = class extends SAX {
1360
+ constructor(opt) {
1361
+ super();
1362
+ this.opt = DEFAULT_SAX_PARSER_OPTIONS;
1363
+ this.events = DEFAULT_SAX_EVENTS;
1364
+ this.clearBuffers();
1365
+ this.opt = opt = { ...this.opt, ...opt };
1366
+ this.events = { ...this.events, ...opt };
1367
+ this.q = this.c = "";
1368
+ this.opt.lowercase = this.opt.lowercase || this.opt.lowercasetags;
1369
+ this.bufferCheckPosition = this.opt.MAX_BUFFER_LENGTH;
1370
+ this.looseCase = this.opt.lowercase ? "toLowerCase" : "toUpperCase";
1371
+ this.tags = [];
1372
+ this.closed = this.closedRoot = this.sawRoot = false;
1373
+ this.tag = this.error = null;
1374
+ this.strict = Boolean(this.opt.strict);
1375
+ this.noscript = Boolean(this.opt.strict || this.opt.noscript);
1376
+ this.state = this.S.BEGIN;
1377
+ this.strictEntities = this.opt.strictEntities;
1378
+ this.ENTITIES = this.strictEntities ? Object.create(this.XML_ENTITIES) : Object.create(this.ENTITIES);
1379
+ this.attribList = [];
1380
+ if (this.opt.xmlns) {
1381
+ this.ns = Object.create(this.rootNS);
1382
+ }
1383
+ this.trackPosition = this.opt.position !== false;
1384
+ if (this.trackPosition) {
1385
+ this.position = this.line = this.column = 0;
1386
+ }
1387
+ this.emit("onready");
1388
+ }
1389
+ resume() {
1390
+ this.error = null;
1391
+ return this;
1392
+ }
1393
+ close() {
1394
+ return this.write(null);
1395
+ }
1396
+ flush() {
1397
+ this.flushBuffers();
1398
+ }
1399
+ };
1400
+ SAXParser.ENTITIES = ENTITIES;
1401
+
1402
+ // src/lib/xml-utils/uncapitalize.ts
1403
+ function uncapitalize(str) {
1404
+ return typeof str === "string" ? str.charAt(0).toLowerCase() + str.slice(1) : str;
1405
+ }
1406
+ function uncapitalizeKeys(object) {
1407
+ if (Array.isArray(object)) {
1408
+ return object.map((element) => uncapitalizeKeys(element));
1409
+ }
1410
+ if (object && typeof object === "object") {
1411
+ const newObject = {};
1412
+ for (const [key, value] of Object.entries(object)) {
1413
+ newObject[uncapitalize(key)] = uncapitalizeKeys(value);
1414
+ }
1415
+ return newObject;
1416
+ }
1417
+ return object;
1418
+ }
1419
+
1420
+ // src/lib/parsers/parse-xml.ts
1421
+ var import_fast_xml_parser = require("fast-xml-parser");
1422
+ function parseXMLSync(text, options) {
1423
+ if ((options == null ? void 0 : options._parser) && options._parser !== "fast-xml-parser") {
1424
+ throw new Error(options == null ? void 0 : options._parser);
1425
+ }
1426
+ const fastXMLOptions = {
1427
+ // Default FastXML options
1428
+ // https://github.com/NaturalIntelligence/fast-xml-parser/blob/master/docs/v4/2.XMLparseOptions.md#allowbooleanattributes
1429
+ allowBooleanAttributes: true,
1430
+ // https://github.com/NaturalIntelligence/fast-xml-parser/blob/master/docs/v4/2.XMLparseOptions.md#ignoredeclaration
1431
+ ignoreDeclaration: true,
1432
+ // https://github.com/NaturalIntelligence/fast-xml-parser/blob/master/docs/v4/2.XMLparseOptions.md#removensprefix
1433
+ removeNSPrefix: options == null ? void 0 : options.removeNSPrefix,
1434
+ // https://github.com/NaturalIntelligence/fast-xml-parser/blob/master/docs/v4/2.XMLparseOptions.md#textnodename
1435
+ textNodeName: options == null ? void 0 : options.textNodeName,
1436
+ // Let's application specify keys that are always arrays
1437
+ isArray: (name, jpath, isLeafNode, isAttribute) => {
1438
+ var _a;
1439
+ const array = Boolean((_a = options == null ? void 0 : options.arrayPaths) == null ? void 0 : _a.some((path) => jpath === path));
1440
+ return array;
1441
+ },
1442
+ // Application overrides
1443
+ ...options == null ? void 0 : options._fastXML
1444
+ };
1445
+ const xml = fastParseXML(text, fastXMLOptions);
1446
+ return (options == null ? void 0 : options.uncapitalizeKeys) ? uncapitalizeKeys(xml) : xml;
1447
+ }
1448
+ function fastParseXML(text, options) {
1449
+ const parser = new import_fast_xml_parser.XMLParser({
1450
+ ignoreAttributes: false,
1451
+ attributeNamePrefix: "",
1452
+ ...options
1453
+ });
1454
+ const parsedXML = parser.parse(text);
1455
+ return parsedXML;
1456
+ }
1457
+
1458
+ // src/xml-loader.ts
1459
+ var VERSION = typeof __VERSION__ !== "undefined" ? __VERSION__ : "latest";
1460
+ var XMLLoader = {
1461
+ name: "XML",
1462
+ id: "xml",
1463
+ module: "xml",
1464
+ version: VERSION,
1465
+ worker: false,
1466
+ extensions: ["xml"],
1467
+ mimeTypes: ["application/xml", "text/xml"],
1468
+ testText: testXMLFile,
1469
+ options: {
1470
+ xml: {
1471
+ _parser: "fast-xml-parser",
1472
+ uncapitalizeKeys: false,
1473
+ removeNSPrefix: false,
1474
+ textNodeName: "value",
1475
+ arrayPaths: []
1476
+ }
1477
+ },
1478
+ parse: async (arrayBuffer, options) => parseXMLSync(new TextDecoder().decode(arrayBuffer), {
1479
+ ...XMLLoader.options.xml,
1480
+ ...options == null ? void 0 : options.xml
1481
+ }),
1482
+ parseTextSync: (text, options) => parseXMLSync(text, { ...XMLLoader.options.xml, ...options == null ? void 0 : options.xml })
1483
+ };
1484
+ function testXMLFile(text) {
1485
+ return text.startsWith("<?xml");
1486
+ }
1487
+
1488
+ // src/html-loader.ts
1489
+ var import_loader_utils = require("@loaders.gl/loader-utils");
1490
+ var HTMLLoader = {
1491
+ ...XMLLoader,
1492
+ name: "HTML",
1493
+ id: "html",
1494
+ extensions: ["html", "htm"],
1495
+ mimeTypes: ["text/html"],
1496
+ testText: testHTMLFile,
1497
+ parse: async (arrayBuffer, options) => parseTextSync(new TextDecoder().decode(arrayBuffer), options),
1498
+ parseTextSync: (text, options) => parseTextSync(text, options)
1499
+ };
1500
+ function testHTMLFile(text) {
1501
+ return text.startsWith("<html");
1502
+ }
1503
+ function parseTextSync(text, options) {
1504
+ var _a, _b;
1505
+ options = (0, import_loader_utils.mergeLoaderOptions)(options, {
1506
+ xml: {
1507
+ _parser: "fast-xml-parser",
1508
+ _fastXML: {
1509
+ htmlEntities: true
1510
+ }
1511
+ }
1512
+ });
1513
+ return (_b = (_a = XMLLoader).parseTextSync) == null ? void 0 : _b.call(_a, text, options);
1514
+ }
1515
+
1516
+ // src/lib/xml-utils/xml-utils.ts
1517
+ function convertXMLValueToArray(xmlValue) {
1518
+ if (Array.isArray(xmlValue)) {
1519
+ return xmlValue;
1520
+ }
1521
+ if (xmlValue && typeof xmlValue === "object" && xmlValue["0"]) {
1522
+ }
1523
+ if (xmlValue) {
1524
+ return [xmlValue];
1525
+ }
1526
+ return [];
1527
+ }
1528
+ function convertXMLFieldToArrayInPlace(xml, key) {
1529
+ xml[key] = convertXMLValueToArray(xml[key]);
1530
+ }