@rpgjs/tiledmap 5.0.0-beta.2 → 5.0.0-beta.3

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.
@@ -1,4943 +1,53 @@
1
- var TiledLayerType = /* @__PURE__ */ ((TiledLayerType2) => {
2
- TiledLayerType2["Tile"] = "tilelayer";
3
- TiledLayerType2["ObjectGroup"] = "objectgroup";
4
- TiledLayerType2["Image"] = "imagelayer";
5
- TiledLayerType2["Group"] = "group";
6
- return TiledLayerType2;
7
- })(TiledLayerType || {});
8
- function getAugmentedNamespace(n) {
9
- if (Object.prototype.hasOwnProperty.call(n, "__esModule")) return n;
10
- var f = n.default;
11
- if (typeof f == "function") {
12
- var a = function a2() {
13
- if (this instanceof a2) {
14
- return Reflect.construct(f, arguments, this.constructor);
15
- }
16
- return f.apply(this, arguments);
17
- };
18
- a.prototype = f.prototype;
19
- } else a = {};
20
- Object.defineProperty(a, "__esModule", { value: true });
21
- Object.keys(n).forEach(function(k) {
22
- var d = Object.getOwnPropertyDescriptor(n, k);
23
- Object.defineProperty(a, k, d.get ? d : {
24
- enumerable: true,
25
- get: function() {
26
- return n[k];
27
- }
28
- });
29
- });
30
- return a;
31
- }
32
- var sax = {};
33
- const __viteBrowserExternal = {};
34
- const __viteBrowserExternal$1 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
35
- __proto__: null,
36
- default: __viteBrowserExternal
37
- }, Symbol.toStringTag, { value: "Module" }));
38
- const require$$0 = /* @__PURE__ */ getAugmentedNamespace(__viteBrowserExternal$1);
39
- var string_decoder = {};
40
- var safeBuffer = { exports: {} };
41
- var buffer = {};
42
- var base64Js = {};
43
- var hasRequiredBase64Js;
44
- function requireBase64Js() {
45
- if (hasRequiredBase64Js) return base64Js;
46
- hasRequiredBase64Js = 1;
47
- base64Js.byteLength = byteLength;
48
- base64Js.toByteArray = toByteArray;
49
- base64Js.fromByteArray = fromByteArray;
50
- var lookup = [];
51
- var revLookup = [];
52
- var Arr = typeof Uint8Array !== "undefined" ? Uint8Array : Array;
53
- var code = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
54
- for (var i = 0, len = code.length; i < len; ++i) {
55
- lookup[i] = code[i];
56
- revLookup[code.charCodeAt(i)] = i;
57
- }
58
- revLookup["-".charCodeAt(0)] = 62;
59
- revLookup["_".charCodeAt(0)] = 63;
60
- function getLens(b64) {
61
- var len2 = b64.length;
62
- if (len2 % 4 > 0) {
63
- throw new Error("Invalid string. Length must be a multiple of 4");
64
- }
65
- var validLen = b64.indexOf("=");
66
- if (validLen === -1) validLen = len2;
67
- var placeHoldersLen = validLen === len2 ? 0 : 4 - validLen % 4;
68
- return [validLen, placeHoldersLen];
69
- }
70
- function byteLength(b64) {
71
- var lens = getLens(b64);
72
- var validLen = lens[0];
73
- var placeHoldersLen = lens[1];
74
- return (validLen + placeHoldersLen) * 3 / 4 - placeHoldersLen;
75
- }
76
- function _byteLength(b64, validLen, placeHoldersLen) {
77
- return (validLen + placeHoldersLen) * 3 / 4 - placeHoldersLen;
78
- }
79
- function toByteArray(b64) {
80
- var tmp;
81
- var lens = getLens(b64);
82
- var validLen = lens[0];
83
- var placeHoldersLen = lens[1];
84
- var arr = new Arr(_byteLength(b64, validLen, placeHoldersLen));
85
- var curByte = 0;
86
- var len2 = placeHoldersLen > 0 ? validLen - 4 : validLen;
87
- var i2;
88
- for (i2 = 0; i2 < len2; i2 += 4) {
89
- tmp = revLookup[b64.charCodeAt(i2)] << 18 | revLookup[b64.charCodeAt(i2 + 1)] << 12 | revLookup[b64.charCodeAt(i2 + 2)] << 6 | revLookup[b64.charCodeAt(i2 + 3)];
90
- arr[curByte++] = tmp >> 16 & 255;
91
- arr[curByte++] = tmp >> 8 & 255;
92
- arr[curByte++] = tmp & 255;
93
- }
94
- if (placeHoldersLen === 2) {
95
- tmp = revLookup[b64.charCodeAt(i2)] << 2 | revLookup[b64.charCodeAt(i2 + 1)] >> 4;
96
- arr[curByte++] = tmp & 255;
97
- }
98
- if (placeHoldersLen === 1) {
99
- tmp = revLookup[b64.charCodeAt(i2)] << 10 | revLookup[b64.charCodeAt(i2 + 1)] << 4 | revLookup[b64.charCodeAt(i2 + 2)] >> 2;
100
- arr[curByte++] = tmp >> 8 & 255;
101
- arr[curByte++] = tmp & 255;
102
- }
103
- return arr;
104
- }
105
- function tripletToBase64(num) {
106
- return lookup[num >> 18 & 63] + lookup[num >> 12 & 63] + lookup[num >> 6 & 63] + lookup[num & 63];
107
- }
108
- function encodeChunk(uint8, start, end) {
109
- var tmp;
110
- var output = [];
111
- for (var i2 = start; i2 < end; i2 += 3) {
112
- tmp = (uint8[i2] << 16 & 16711680) + (uint8[i2 + 1] << 8 & 65280) + (uint8[i2 + 2] & 255);
113
- output.push(tripletToBase64(tmp));
114
- }
115
- return output.join("");
116
- }
117
- function fromByteArray(uint8) {
118
- var tmp;
119
- var len2 = uint8.length;
120
- var extraBytes = len2 % 3;
121
- var parts = [];
122
- var maxChunkLength = 16383;
123
- for (var i2 = 0, len22 = len2 - extraBytes; i2 < len22; i2 += maxChunkLength) {
124
- parts.push(encodeChunk(uint8, i2, i2 + maxChunkLength > len22 ? len22 : i2 + maxChunkLength));
125
- }
126
- if (extraBytes === 1) {
127
- tmp = uint8[len2 - 1];
128
- parts.push(
129
- lookup[tmp >> 2] + lookup[tmp << 4 & 63] + "=="
130
- );
131
- } else if (extraBytes === 2) {
132
- tmp = (uint8[len2 - 2] << 8) + uint8[len2 - 1];
133
- parts.push(
134
- lookup[tmp >> 10] + lookup[tmp >> 4 & 63] + lookup[tmp << 2 & 63] + "="
135
- );
136
- }
137
- return parts.join("");
138
- }
139
- return base64Js;
140
- }
141
- var ieee754 = {};
142
- var hasRequiredIeee754;
143
- function requireIeee754() {
144
- if (hasRequiredIeee754) return ieee754;
145
- hasRequiredIeee754 = 1;
146
- ieee754.read = function(buffer2, offset, isLE, mLen, nBytes) {
147
- var e, m;
148
- var eLen = nBytes * 8 - mLen - 1;
149
- var eMax = (1 << eLen) - 1;
150
- var eBias = eMax >> 1;
151
- var nBits = -7;
152
- var i = isLE ? nBytes - 1 : 0;
153
- var d = isLE ? -1 : 1;
154
- var s = buffer2[offset + i];
155
- i += d;
156
- e = s & (1 << -nBits) - 1;
157
- s >>= -nBits;
158
- nBits += eLen;
159
- for (; nBits > 0; e = e * 256 + buffer2[offset + i], i += d, nBits -= 8) {
160
- }
161
- m = e & (1 << -nBits) - 1;
162
- e >>= -nBits;
163
- nBits += mLen;
164
- for (; nBits > 0; m = m * 256 + buffer2[offset + i], i += d, nBits -= 8) {
165
- }
166
- if (e === 0) {
167
- e = 1 - eBias;
168
- } else if (e === eMax) {
169
- return m ? NaN : (s ? -1 : 1) * Infinity;
170
- } else {
171
- m = m + Math.pow(2, mLen);
172
- e = e - eBias;
173
- }
174
- return (s ? -1 : 1) * m * Math.pow(2, e - mLen);
175
- };
176
- ieee754.write = function(buffer2, value, offset, isLE, mLen, nBytes) {
177
- var e, m, c;
178
- var eLen = nBytes * 8 - mLen - 1;
179
- var eMax = (1 << eLen) - 1;
180
- var eBias = eMax >> 1;
181
- var rt = mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0;
182
- var i = isLE ? 0 : nBytes - 1;
183
- var d = isLE ? 1 : -1;
184
- var s = value < 0 || value === 0 && 1 / value < 0 ? 1 : 0;
185
- value = Math.abs(value);
186
- if (isNaN(value) || value === Infinity) {
187
- m = isNaN(value) ? 1 : 0;
188
- e = eMax;
189
- } else {
190
- e = Math.floor(Math.log(value) / Math.LN2);
191
- if (value * (c = Math.pow(2, -e)) < 1) {
192
- e--;
193
- c *= 2;
194
- }
195
- if (e + eBias >= 1) {
196
- value += rt / c;
197
- } else {
198
- value += rt * Math.pow(2, 1 - eBias);
199
- }
200
- if (value * c >= 2) {
201
- e++;
202
- c /= 2;
203
- }
204
- if (e + eBias >= eMax) {
205
- m = 0;
206
- e = eMax;
207
- } else if (e + eBias >= 1) {
208
- m = (value * c - 1) * Math.pow(2, mLen);
209
- e = e + eBias;
210
- } else {
211
- m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen);
212
- e = 0;
213
- }
214
- }
215
- for (; mLen >= 8; buffer2[offset + i] = m & 255, i += d, m /= 256, mLen -= 8) {
216
- }
217
- e = e << mLen | m;
218
- eLen += mLen;
219
- for (; eLen > 0; buffer2[offset + i] = e & 255, i += d, e /= 256, eLen -= 8) {
220
- }
221
- buffer2[offset + i - d] |= s * 128;
222
- };
223
- return ieee754;
224
- }
225
- var hasRequiredBuffer;
226
- function requireBuffer() {
227
- if (hasRequiredBuffer) return buffer;
228
- hasRequiredBuffer = 1;
229
- (function(exports) {
230
- const base64 = requireBase64Js();
231
- const ieee7542 = requireIeee754();
232
- const customInspectSymbol = typeof Symbol === "function" && typeof Symbol["for"] === "function" ? Symbol["for"]("nodejs.util.inspect.custom") : null;
233
- exports.Buffer = Buffer2;
234
- exports.SlowBuffer = SlowBuffer;
235
- exports.INSPECT_MAX_BYTES = 50;
236
- const K_MAX_LENGTH = 2147483647;
237
- exports.kMaxLength = K_MAX_LENGTH;
238
- Buffer2.TYPED_ARRAY_SUPPORT = typedArraySupport();
239
- if (!Buffer2.TYPED_ARRAY_SUPPORT && typeof console !== "undefined" && typeof console.error === "function") {
240
- console.error(
241
- "This browser lacks typed array (Uint8Array) support which is required by `buffer` v5.x. Use `buffer` v4.x if you require old browser support."
242
- );
243
- }
244
- function typedArraySupport() {
245
- try {
246
- const arr = new Uint8Array(1);
247
- const proto = { foo: function() {
248
- return 42;
249
- } };
250
- Object.setPrototypeOf(proto, Uint8Array.prototype);
251
- Object.setPrototypeOf(arr, proto);
252
- return arr.foo() === 42;
253
- } catch (e) {
254
- return false;
255
- }
256
- }
257
- Object.defineProperty(Buffer2.prototype, "parent", {
258
- enumerable: true,
259
- get: function() {
260
- if (!Buffer2.isBuffer(this)) return void 0;
261
- return this.buffer;
262
- }
263
- });
264
- Object.defineProperty(Buffer2.prototype, "offset", {
265
- enumerable: true,
266
- get: function() {
267
- if (!Buffer2.isBuffer(this)) return void 0;
268
- return this.byteOffset;
269
- }
270
- });
271
- function createBuffer(length) {
272
- if (length > K_MAX_LENGTH) {
273
- throw new RangeError('The value "' + length + '" is invalid for option "size"');
274
- }
275
- const buf = new Uint8Array(length);
276
- Object.setPrototypeOf(buf, Buffer2.prototype);
277
- return buf;
278
- }
279
- function Buffer2(arg, encodingOrOffset, length) {
280
- if (typeof arg === "number") {
281
- if (typeof encodingOrOffset === "string") {
282
- throw new TypeError(
283
- 'The "string" argument must be of type string. Received type number'
284
- );
285
- }
286
- return allocUnsafe(arg);
287
- }
288
- return from(arg, encodingOrOffset, length);
289
- }
290
- Buffer2.poolSize = 8192;
291
- function from(value, encodingOrOffset, length) {
292
- if (typeof value === "string") {
293
- return fromString(value, encodingOrOffset);
294
- }
295
- if (ArrayBuffer.isView(value)) {
296
- return fromArrayView(value);
297
- }
298
- if (value == null) {
299
- throw new TypeError(
300
- "The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type " + typeof value
301
- );
302
- }
303
- if (isInstance(value, ArrayBuffer) || value && isInstance(value.buffer, ArrayBuffer)) {
304
- return fromArrayBuffer(value, encodingOrOffset, length);
305
- }
306
- if (typeof SharedArrayBuffer !== "undefined" && (isInstance(value, SharedArrayBuffer) || value && isInstance(value.buffer, SharedArrayBuffer))) {
307
- return fromArrayBuffer(value, encodingOrOffset, length);
308
- }
309
- if (typeof value === "number") {
310
- throw new TypeError(
311
- 'The "value" argument must not be of type number. Received type number'
312
- );
313
- }
314
- const valueOf = value.valueOf && value.valueOf();
315
- if (valueOf != null && valueOf !== value) {
316
- return Buffer2.from(valueOf, encodingOrOffset, length);
317
- }
318
- const b = fromObject(value);
319
- if (b) return b;
320
- if (typeof Symbol !== "undefined" && Symbol.toPrimitive != null && typeof value[Symbol.toPrimitive] === "function") {
321
- return Buffer2.from(value[Symbol.toPrimitive]("string"), encodingOrOffset, length);
322
- }
323
- throw new TypeError(
324
- "The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type " + typeof value
325
- );
326
- }
327
- Buffer2.from = function(value, encodingOrOffset, length) {
328
- return from(value, encodingOrOffset, length);
329
- };
330
- Object.setPrototypeOf(Buffer2.prototype, Uint8Array.prototype);
331
- Object.setPrototypeOf(Buffer2, Uint8Array);
332
- function assertSize(size) {
333
- if (typeof size !== "number") {
334
- throw new TypeError('"size" argument must be of type number');
335
- } else if (size < 0) {
336
- throw new RangeError('The value "' + size + '" is invalid for option "size"');
337
- }
338
- }
339
- function alloc(size, fill, encoding) {
340
- assertSize(size);
341
- if (size <= 0) {
342
- return createBuffer(size);
343
- }
344
- if (fill !== void 0) {
345
- return typeof encoding === "string" ? createBuffer(size).fill(fill, encoding) : createBuffer(size).fill(fill);
346
- }
347
- return createBuffer(size);
348
- }
349
- Buffer2.alloc = function(size, fill, encoding) {
350
- return alloc(size, fill, encoding);
351
- };
352
- function allocUnsafe(size) {
353
- assertSize(size);
354
- return createBuffer(size < 0 ? 0 : checked(size) | 0);
355
- }
356
- Buffer2.allocUnsafe = function(size) {
357
- return allocUnsafe(size);
358
- };
359
- Buffer2.allocUnsafeSlow = function(size) {
360
- return allocUnsafe(size);
361
- };
362
- function fromString(string, encoding) {
363
- if (typeof encoding !== "string" || encoding === "") {
364
- encoding = "utf8";
365
- }
366
- if (!Buffer2.isEncoding(encoding)) {
367
- throw new TypeError("Unknown encoding: " + encoding);
368
- }
369
- const length = byteLength(string, encoding) | 0;
370
- let buf = createBuffer(length);
371
- const actual = buf.write(string, encoding);
372
- if (actual !== length) {
373
- buf = buf.slice(0, actual);
374
- }
375
- return buf;
376
- }
377
- function fromArrayLike(array) {
378
- const length = array.length < 0 ? 0 : checked(array.length) | 0;
379
- const buf = createBuffer(length);
380
- for (let i = 0; i < length; i += 1) {
381
- buf[i] = array[i] & 255;
382
- }
383
- return buf;
384
- }
385
- function fromArrayView(arrayView) {
386
- if (isInstance(arrayView, Uint8Array)) {
387
- const copy = new Uint8Array(arrayView);
388
- return fromArrayBuffer(copy.buffer, copy.byteOffset, copy.byteLength);
389
- }
390
- return fromArrayLike(arrayView);
391
- }
392
- function fromArrayBuffer(array, byteOffset, length) {
393
- if (byteOffset < 0 || array.byteLength < byteOffset) {
394
- throw new RangeError('"offset" is outside of buffer bounds');
395
- }
396
- if (array.byteLength < byteOffset + (length || 0)) {
397
- throw new RangeError('"length" is outside of buffer bounds');
398
- }
399
- let buf;
400
- if (byteOffset === void 0 && length === void 0) {
401
- buf = new Uint8Array(array);
402
- } else if (length === void 0) {
403
- buf = new Uint8Array(array, byteOffset);
404
- } else {
405
- buf = new Uint8Array(array, byteOffset, length);
406
- }
407
- Object.setPrototypeOf(buf, Buffer2.prototype);
408
- return buf;
409
- }
410
- function fromObject(obj) {
411
- if (Buffer2.isBuffer(obj)) {
412
- const len = checked(obj.length) | 0;
413
- const buf = createBuffer(len);
414
- if (buf.length === 0) {
415
- return buf;
416
- }
417
- obj.copy(buf, 0, 0, len);
418
- return buf;
419
- }
420
- if (obj.length !== void 0) {
421
- if (typeof obj.length !== "number" || numberIsNaN(obj.length)) {
422
- return createBuffer(0);
423
- }
424
- return fromArrayLike(obj);
425
- }
426
- if (obj.type === "Buffer" && Array.isArray(obj.data)) {
427
- return fromArrayLike(obj.data);
428
- }
429
- }
430
- function checked(length) {
431
- if (length >= K_MAX_LENGTH) {
432
- throw new RangeError("Attempt to allocate Buffer larger than maximum size: 0x" + K_MAX_LENGTH.toString(16) + " bytes");
433
- }
434
- return length | 0;
435
- }
436
- function SlowBuffer(length) {
437
- if (+length != length) {
438
- length = 0;
439
- }
440
- return Buffer2.alloc(+length);
441
- }
442
- Buffer2.isBuffer = function isBuffer(b) {
443
- return b != null && b._isBuffer === true && b !== Buffer2.prototype;
444
- };
445
- Buffer2.compare = function compare(a, b) {
446
- if (isInstance(a, Uint8Array)) a = Buffer2.from(a, a.offset, a.byteLength);
447
- if (isInstance(b, Uint8Array)) b = Buffer2.from(b, b.offset, b.byteLength);
448
- if (!Buffer2.isBuffer(a) || !Buffer2.isBuffer(b)) {
449
- throw new TypeError(
450
- 'The "buf1", "buf2" arguments must be one of type Buffer or Uint8Array'
451
- );
452
- }
453
- if (a === b) return 0;
454
- let x = a.length;
455
- let y = b.length;
456
- for (let i = 0, len = Math.min(x, y); i < len; ++i) {
457
- if (a[i] !== b[i]) {
458
- x = a[i];
459
- y = b[i];
460
- break;
461
- }
462
- }
463
- if (x < y) return -1;
464
- if (y < x) return 1;
465
- return 0;
466
- };
467
- Buffer2.isEncoding = function isEncoding(encoding) {
468
- switch (String(encoding).toLowerCase()) {
469
- case "hex":
470
- case "utf8":
471
- case "utf-8":
472
- case "ascii":
473
- case "latin1":
474
- case "binary":
475
- case "base64":
476
- case "ucs2":
477
- case "ucs-2":
478
- case "utf16le":
479
- case "utf-16le":
480
- return true;
481
- default:
482
- return false;
483
- }
484
- };
485
- Buffer2.concat = function concat(list, length) {
486
- if (!Array.isArray(list)) {
487
- throw new TypeError('"list" argument must be an Array of Buffers');
488
- }
489
- if (list.length === 0) {
490
- return Buffer2.alloc(0);
491
- }
492
- let i;
493
- if (length === void 0) {
494
- length = 0;
495
- for (i = 0; i < list.length; ++i) {
496
- length += list[i].length;
497
- }
498
- }
499
- const buffer2 = Buffer2.allocUnsafe(length);
500
- let pos = 0;
501
- for (i = 0; i < list.length; ++i) {
502
- let buf = list[i];
503
- if (isInstance(buf, Uint8Array)) {
504
- if (pos + buf.length > buffer2.length) {
505
- if (!Buffer2.isBuffer(buf)) buf = Buffer2.from(buf);
506
- buf.copy(buffer2, pos);
507
- } else {
508
- Uint8Array.prototype.set.call(
509
- buffer2,
510
- buf,
511
- pos
512
- );
513
- }
514
- } else if (!Buffer2.isBuffer(buf)) {
515
- throw new TypeError('"list" argument must be an Array of Buffers');
516
- } else {
517
- buf.copy(buffer2, pos);
518
- }
519
- pos += buf.length;
520
- }
521
- return buffer2;
522
- };
523
- function byteLength(string, encoding) {
524
- if (Buffer2.isBuffer(string)) {
525
- return string.length;
526
- }
527
- if (ArrayBuffer.isView(string) || isInstance(string, ArrayBuffer)) {
528
- return string.byteLength;
529
- }
530
- if (typeof string !== "string") {
531
- throw new TypeError(
532
- 'The "string" argument must be one of type string, Buffer, or ArrayBuffer. Received type ' + typeof string
533
- );
534
- }
535
- const len = string.length;
536
- const mustMatch = arguments.length > 2 && arguments[2] === true;
537
- if (!mustMatch && len === 0) return 0;
538
- let loweredCase = false;
539
- for (; ; ) {
540
- switch (encoding) {
541
- case "ascii":
542
- case "latin1":
543
- case "binary":
544
- return len;
545
- case "utf8":
546
- case "utf-8":
547
- return utf8ToBytes(string).length;
548
- case "ucs2":
549
- case "ucs-2":
550
- case "utf16le":
551
- case "utf-16le":
552
- return len * 2;
553
- case "hex":
554
- return len >>> 1;
555
- case "base64":
556
- return base64ToBytes(string).length;
557
- default:
558
- if (loweredCase) {
559
- return mustMatch ? -1 : utf8ToBytes(string).length;
560
- }
561
- encoding = ("" + encoding).toLowerCase();
562
- loweredCase = true;
563
- }
564
- }
565
- }
566
- Buffer2.byteLength = byteLength;
567
- function slowToString(encoding, start, end) {
568
- let loweredCase = false;
569
- if (start === void 0 || start < 0) {
570
- start = 0;
571
- }
572
- if (start > this.length) {
573
- return "";
574
- }
575
- if (end === void 0 || end > this.length) {
576
- end = this.length;
577
- }
578
- if (end <= 0) {
579
- return "";
580
- }
581
- end >>>= 0;
582
- start >>>= 0;
583
- if (end <= start) {
584
- return "";
585
- }
586
- if (!encoding) encoding = "utf8";
587
- while (true) {
588
- switch (encoding) {
589
- case "hex":
590
- return hexSlice(this, start, end);
591
- case "utf8":
592
- case "utf-8":
593
- return utf8Slice(this, start, end);
594
- case "ascii":
595
- return asciiSlice(this, start, end);
596
- case "latin1":
597
- case "binary":
598
- return latin1Slice(this, start, end);
599
- case "base64":
600
- return base64Slice(this, start, end);
601
- case "ucs2":
602
- case "ucs-2":
603
- case "utf16le":
604
- case "utf-16le":
605
- return utf16leSlice(this, start, end);
606
- default:
607
- if (loweredCase) throw new TypeError("Unknown encoding: " + encoding);
608
- encoding = (encoding + "").toLowerCase();
609
- loweredCase = true;
610
- }
611
- }
612
- }
613
- Buffer2.prototype._isBuffer = true;
614
- function swap(b, n, m) {
615
- const i = b[n];
616
- b[n] = b[m];
617
- b[m] = i;
618
- }
619
- Buffer2.prototype.swap16 = function swap16() {
620
- const len = this.length;
621
- if (len % 2 !== 0) {
622
- throw new RangeError("Buffer size must be a multiple of 16-bits");
623
- }
624
- for (let i = 0; i < len; i += 2) {
625
- swap(this, i, i + 1);
626
- }
627
- return this;
628
- };
629
- Buffer2.prototype.swap32 = function swap32() {
630
- const len = this.length;
631
- if (len % 4 !== 0) {
632
- throw new RangeError("Buffer size must be a multiple of 32-bits");
633
- }
634
- for (let i = 0; i < len; i += 4) {
635
- swap(this, i, i + 3);
636
- swap(this, i + 1, i + 2);
637
- }
638
- return this;
639
- };
640
- Buffer2.prototype.swap64 = function swap64() {
641
- const len = this.length;
642
- if (len % 8 !== 0) {
643
- throw new RangeError("Buffer size must be a multiple of 64-bits");
644
- }
645
- for (let i = 0; i < len; i += 8) {
646
- swap(this, i, i + 7);
647
- swap(this, i + 1, i + 6);
648
- swap(this, i + 2, i + 5);
649
- swap(this, i + 3, i + 4);
650
- }
651
- return this;
652
- };
653
- Buffer2.prototype.toString = function toString() {
654
- const length = this.length;
655
- if (length === 0) return "";
656
- if (arguments.length === 0) return utf8Slice(this, 0, length);
657
- return slowToString.apply(this, arguments);
658
- };
659
- Buffer2.prototype.toLocaleString = Buffer2.prototype.toString;
660
- Buffer2.prototype.equals = function equals(b) {
661
- if (!Buffer2.isBuffer(b)) throw new TypeError("Argument must be a Buffer");
662
- if (this === b) return true;
663
- return Buffer2.compare(this, b) === 0;
664
- };
665
- Buffer2.prototype.inspect = function inspect() {
666
- let str = "";
667
- const max = exports.INSPECT_MAX_BYTES;
668
- str = this.toString("hex", 0, max).replace(/(.{2})/g, "$1 ").trim();
669
- if (this.length > max) str += " ... ";
670
- return "<Buffer " + str + ">";
671
- };
672
- if (customInspectSymbol) {
673
- Buffer2.prototype[customInspectSymbol] = Buffer2.prototype.inspect;
674
- }
675
- Buffer2.prototype.compare = function compare(target, start, end, thisStart, thisEnd) {
676
- if (isInstance(target, Uint8Array)) {
677
- target = Buffer2.from(target, target.offset, target.byteLength);
678
- }
679
- if (!Buffer2.isBuffer(target)) {
680
- throw new TypeError(
681
- 'The "target" argument must be one of type Buffer or Uint8Array. Received type ' + typeof target
682
- );
683
- }
684
- if (start === void 0) {
685
- start = 0;
686
- }
687
- if (end === void 0) {
688
- end = target ? target.length : 0;
689
- }
690
- if (thisStart === void 0) {
691
- thisStart = 0;
692
- }
693
- if (thisEnd === void 0) {
694
- thisEnd = this.length;
695
- }
696
- if (start < 0 || end > target.length || thisStart < 0 || thisEnd > this.length) {
697
- throw new RangeError("out of range index");
698
- }
699
- if (thisStart >= thisEnd && start >= end) {
700
- return 0;
701
- }
702
- if (thisStart >= thisEnd) {
703
- return -1;
704
- }
705
- if (start >= end) {
706
- return 1;
707
- }
708
- start >>>= 0;
709
- end >>>= 0;
710
- thisStart >>>= 0;
711
- thisEnd >>>= 0;
712
- if (this === target) return 0;
713
- let x = thisEnd - thisStart;
714
- let y = end - start;
715
- const len = Math.min(x, y);
716
- const thisCopy = this.slice(thisStart, thisEnd);
717
- const targetCopy = target.slice(start, end);
718
- for (let i = 0; i < len; ++i) {
719
- if (thisCopy[i] !== targetCopy[i]) {
720
- x = thisCopy[i];
721
- y = targetCopy[i];
722
- break;
723
- }
724
- }
725
- if (x < y) return -1;
726
- if (y < x) return 1;
727
- return 0;
728
- };
729
- function bidirectionalIndexOf(buffer2, val, byteOffset, encoding, dir) {
730
- if (buffer2.length === 0) return -1;
731
- if (typeof byteOffset === "string") {
732
- encoding = byteOffset;
733
- byteOffset = 0;
734
- } else if (byteOffset > 2147483647) {
735
- byteOffset = 2147483647;
736
- } else if (byteOffset < -2147483648) {
737
- byteOffset = -2147483648;
738
- }
739
- byteOffset = +byteOffset;
740
- if (numberIsNaN(byteOffset)) {
741
- byteOffset = dir ? 0 : buffer2.length - 1;
742
- }
743
- if (byteOffset < 0) byteOffset = buffer2.length + byteOffset;
744
- if (byteOffset >= buffer2.length) {
745
- if (dir) return -1;
746
- else byteOffset = buffer2.length - 1;
747
- } else if (byteOffset < 0) {
748
- if (dir) byteOffset = 0;
749
- else return -1;
750
- }
751
- if (typeof val === "string") {
752
- val = Buffer2.from(val, encoding);
753
- }
754
- if (Buffer2.isBuffer(val)) {
755
- if (val.length === 0) {
756
- return -1;
757
- }
758
- return arrayIndexOf(buffer2, val, byteOffset, encoding, dir);
759
- } else if (typeof val === "number") {
760
- val = val & 255;
761
- if (typeof Uint8Array.prototype.indexOf === "function") {
762
- if (dir) {
763
- return Uint8Array.prototype.indexOf.call(buffer2, val, byteOffset);
764
- } else {
765
- return Uint8Array.prototype.lastIndexOf.call(buffer2, val, byteOffset);
766
- }
767
- }
768
- return arrayIndexOf(buffer2, [val], byteOffset, encoding, dir);
769
- }
770
- throw new TypeError("val must be string, number or Buffer");
771
- }
772
- function arrayIndexOf(arr, val, byteOffset, encoding, dir) {
773
- let indexSize = 1;
774
- let arrLength = arr.length;
775
- let valLength = val.length;
776
- if (encoding !== void 0) {
777
- encoding = String(encoding).toLowerCase();
778
- if (encoding === "ucs2" || encoding === "ucs-2" || encoding === "utf16le" || encoding === "utf-16le") {
779
- if (arr.length < 2 || val.length < 2) {
780
- return -1;
781
- }
782
- indexSize = 2;
783
- arrLength /= 2;
784
- valLength /= 2;
785
- byteOffset /= 2;
786
- }
787
- }
788
- function read(buf, i2) {
789
- if (indexSize === 1) {
790
- return buf[i2];
791
- } else {
792
- return buf.readUInt16BE(i2 * indexSize);
793
- }
794
- }
795
- let i;
796
- if (dir) {
797
- let foundIndex = -1;
798
- for (i = byteOffset; i < arrLength; i++) {
799
- if (read(arr, i) === read(val, foundIndex === -1 ? 0 : i - foundIndex)) {
800
- if (foundIndex === -1) foundIndex = i;
801
- if (i - foundIndex + 1 === valLength) return foundIndex * indexSize;
802
- } else {
803
- if (foundIndex !== -1) i -= i - foundIndex;
804
- foundIndex = -1;
805
- }
806
- }
807
- } else {
808
- if (byteOffset + valLength > arrLength) byteOffset = arrLength - valLength;
809
- for (i = byteOffset; i >= 0; i--) {
810
- let found = true;
811
- for (let j = 0; j < valLength; j++) {
812
- if (read(arr, i + j) !== read(val, j)) {
813
- found = false;
814
- break;
815
- }
816
- }
817
- if (found) return i;
818
- }
819
- }
820
- return -1;
821
- }
822
- Buffer2.prototype.includes = function includes(val, byteOffset, encoding) {
823
- return this.indexOf(val, byteOffset, encoding) !== -1;
824
- };
825
- Buffer2.prototype.indexOf = function indexOf(val, byteOffset, encoding) {
826
- return bidirectionalIndexOf(this, val, byteOffset, encoding, true);
827
- };
828
- Buffer2.prototype.lastIndexOf = function lastIndexOf(val, byteOffset, encoding) {
829
- return bidirectionalIndexOf(this, val, byteOffset, encoding, false);
830
- };
831
- function hexWrite(buf, string, offset, length) {
832
- offset = Number(offset) || 0;
833
- const remaining = buf.length - offset;
834
- if (!length) {
835
- length = remaining;
836
- } else {
837
- length = Number(length);
838
- if (length > remaining) {
839
- length = remaining;
840
- }
841
- }
842
- const strLen = string.length;
843
- if (length > strLen / 2) {
844
- length = strLen / 2;
845
- }
846
- let i;
847
- for (i = 0; i < length; ++i) {
848
- const parsed = parseInt(string.substr(i * 2, 2), 16);
849
- if (numberIsNaN(parsed)) return i;
850
- buf[offset + i] = parsed;
851
- }
852
- return i;
853
- }
854
- function utf8Write(buf, string, offset, length) {
855
- return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length);
856
- }
857
- function asciiWrite(buf, string, offset, length) {
858
- return blitBuffer(asciiToBytes(string), buf, offset, length);
859
- }
860
- function base64Write(buf, string, offset, length) {
861
- return blitBuffer(base64ToBytes(string), buf, offset, length);
862
- }
863
- function ucs2Write(buf, string, offset, length) {
864
- return blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length);
865
- }
866
- Buffer2.prototype.write = function write(string, offset, length, encoding) {
867
- if (offset === void 0) {
868
- encoding = "utf8";
869
- length = this.length;
870
- offset = 0;
871
- } else if (length === void 0 && typeof offset === "string") {
872
- encoding = offset;
873
- length = this.length;
874
- offset = 0;
875
- } else if (isFinite(offset)) {
876
- offset = offset >>> 0;
877
- if (isFinite(length)) {
878
- length = length >>> 0;
879
- if (encoding === void 0) encoding = "utf8";
880
- } else {
881
- encoding = length;
882
- length = void 0;
883
- }
884
- } else {
885
- throw new Error(
886
- "Buffer.write(string, encoding, offset[, length]) is no longer supported"
887
- );
888
- }
889
- const remaining = this.length - offset;
890
- if (length === void 0 || length > remaining) length = remaining;
891
- if (string.length > 0 && (length < 0 || offset < 0) || offset > this.length) {
892
- throw new RangeError("Attempt to write outside buffer bounds");
893
- }
894
- if (!encoding) encoding = "utf8";
895
- let loweredCase = false;
896
- for (; ; ) {
897
- switch (encoding) {
898
- case "hex":
899
- return hexWrite(this, string, offset, length);
900
- case "utf8":
901
- case "utf-8":
902
- return utf8Write(this, string, offset, length);
903
- case "ascii":
904
- case "latin1":
905
- case "binary":
906
- return asciiWrite(this, string, offset, length);
907
- case "base64":
908
- return base64Write(this, string, offset, length);
909
- case "ucs2":
910
- case "ucs-2":
911
- case "utf16le":
912
- case "utf-16le":
913
- return ucs2Write(this, string, offset, length);
914
- default:
915
- if (loweredCase) throw new TypeError("Unknown encoding: " + encoding);
916
- encoding = ("" + encoding).toLowerCase();
917
- loweredCase = true;
918
- }
919
- }
920
- };
921
- Buffer2.prototype.toJSON = function toJSON() {
922
- return {
923
- type: "Buffer",
924
- data: Array.prototype.slice.call(this._arr || this, 0)
925
- };
926
- };
927
- function base64Slice(buf, start, end) {
928
- if (start === 0 && end === buf.length) {
929
- return base64.fromByteArray(buf);
930
- } else {
931
- return base64.fromByteArray(buf.slice(start, end));
932
- }
933
- }
934
- function utf8Slice(buf, start, end) {
935
- end = Math.min(buf.length, end);
936
- const res = [];
937
- let i = start;
938
- while (i < end) {
939
- const firstByte = buf[i];
940
- let codePoint = null;
941
- let bytesPerSequence = firstByte > 239 ? 4 : firstByte > 223 ? 3 : firstByte > 191 ? 2 : 1;
942
- if (i + bytesPerSequence <= end) {
943
- let secondByte, thirdByte, fourthByte, tempCodePoint;
944
- switch (bytesPerSequence) {
945
- case 1:
946
- if (firstByte < 128) {
947
- codePoint = firstByte;
948
- }
949
- break;
950
- case 2:
951
- secondByte = buf[i + 1];
952
- if ((secondByte & 192) === 128) {
953
- tempCodePoint = (firstByte & 31) << 6 | secondByte & 63;
954
- if (tempCodePoint > 127) {
955
- codePoint = tempCodePoint;
956
- }
957
- }
958
- break;
959
- case 3:
960
- secondByte = buf[i + 1];
961
- thirdByte = buf[i + 2];
962
- if ((secondByte & 192) === 128 && (thirdByte & 192) === 128) {
963
- tempCodePoint = (firstByte & 15) << 12 | (secondByte & 63) << 6 | thirdByte & 63;
964
- if (tempCodePoint > 2047 && (tempCodePoint < 55296 || tempCodePoint > 57343)) {
965
- codePoint = tempCodePoint;
966
- }
967
- }
968
- break;
969
- case 4:
970
- secondByte = buf[i + 1];
971
- thirdByte = buf[i + 2];
972
- fourthByte = buf[i + 3];
973
- if ((secondByte & 192) === 128 && (thirdByte & 192) === 128 && (fourthByte & 192) === 128) {
974
- tempCodePoint = (firstByte & 15) << 18 | (secondByte & 63) << 12 | (thirdByte & 63) << 6 | fourthByte & 63;
975
- if (tempCodePoint > 65535 && tempCodePoint < 1114112) {
976
- codePoint = tempCodePoint;
977
- }
978
- }
979
- }
980
- }
981
- if (codePoint === null) {
982
- codePoint = 65533;
983
- bytesPerSequence = 1;
984
- } else if (codePoint > 65535) {
985
- codePoint -= 65536;
986
- res.push(codePoint >>> 10 & 1023 | 55296);
987
- codePoint = 56320 | codePoint & 1023;
988
- }
989
- res.push(codePoint);
990
- i += bytesPerSequence;
991
- }
992
- return decodeCodePointsArray(res);
993
- }
994
- const MAX_ARGUMENTS_LENGTH = 4096;
995
- function decodeCodePointsArray(codePoints) {
996
- const len = codePoints.length;
997
- if (len <= MAX_ARGUMENTS_LENGTH) {
998
- return String.fromCharCode.apply(String, codePoints);
999
- }
1000
- let res = "";
1001
- let i = 0;
1002
- while (i < len) {
1003
- res += String.fromCharCode.apply(
1004
- String,
1005
- codePoints.slice(i, i += MAX_ARGUMENTS_LENGTH)
1006
- );
1007
- }
1008
- return res;
1009
- }
1010
- function asciiSlice(buf, start, end) {
1011
- let ret = "";
1012
- end = Math.min(buf.length, end);
1013
- for (let i = start; i < end; ++i) {
1014
- ret += String.fromCharCode(buf[i] & 127);
1015
- }
1016
- return ret;
1017
- }
1018
- function latin1Slice(buf, start, end) {
1019
- let ret = "";
1020
- end = Math.min(buf.length, end);
1021
- for (let i = start; i < end; ++i) {
1022
- ret += String.fromCharCode(buf[i]);
1023
- }
1024
- return ret;
1025
- }
1026
- function hexSlice(buf, start, end) {
1027
- const len = buf.length;
1028
- if (!start || start < 0) start = 0;
1029
- if (!end || end < 0 || end > len) end = len;
1030
- let out = "";
1031
- for (let i = start; i < end; ++i) {
1032
- out += hexSliceLookupTable[buf[i]];
1033
- }
1034
- return out;
1035
- }
1036
- function utf16leSlice(buf, start, end) {
1037
- const bytes = buf.slice(start, end);
1038
- let res = "";
1039
- for (let i = 0; i < bytes.length - 1; i += 2) {
1040
- res += String.fromCharCode(bytes[i] + bytes[i + 1] * 256);
1041
- }
1042
- return res;
1043
- }
1044
- Buffer2.prototype.slice = function slice(start, end) {
1045
- const len = this.length;
1046
- start = ~~start;
1047
- end = end === void 0 ? len : ~~end;
1048
- if (start < 0) {
1049
- start += len;
1050
- if (start < 0) start = 0;
1051
- } else if (start > len) {
1052
- start = len;
1053
- }
1054
- if (end < 0) {
1055
- end += len;
1056
- if (end < 0) end = 0;
1057
- } else if (end > len) {
1058
- end = len;
1059
- }
1060
- if (end < start) end = start;
1061
- const newBuf = this.subarray(start, end);
1062
- Object.setPrototypeOf(newBuf, Buffer2.prototype);
1063
- return newBuf;
1064
- };
1065
- function checkOffset(offset, ext, length) {
1066
- if (offset % 1 !== 0 || offset < 0) throw new RangeError("offset is not uint");
1067
- if (offset + ext > length) throw new RangeError("Trying to access beyond buffer length");
1068
- }
1069
- Buffer2.prototype.readUintLE = Buffer2.prototype.readUIntLE = function readUIntLE(offset, byteLength2, noAssert) {
1070
- offset = offset >>> 0;
1071
- byteLength2 = byteLength2 >>> 0;
1072
- if (!noAssert) checkOffset(offset, byteLength2, this.length);
1073
- let val = this[offset];
1074
- let mul = 1;
1075
- let i = 0;
1076
- while (++i < byteLength2 && (mul *= 256)) {
1077
- val += this[offset + i] * mul;
1078
- }
1079
- return val;
1080
- };
1081
- Buffer2.prototype.readUintBE = Buffer2.prototype.readUIntBE = function readUIntBE(offset, byteLength2, noAssert) {
1082
- offset = offset >>> 0;
1083
- byteLength2 = byteLength2 >>> 0;
1084
- if (!noAssert) {
1085
- checkOffset(offset, byteLength2, this.length);
1086
- }
1087
- let val = this[offset + --byteLength2];
1088
- let mul = 1;
1089
- while (byteLength2 > 0 && (mul *= 256)) {
1090
- val += this[offset + --byteLength2] * mul;
1091
- }
1092
- return val;
1093
- };
1094
- Buffer2.prototype.readUint8 = Buffer2.prototype.readUInt8 = function readUInt8(offset, noAssert) {
1095
- offset = offset >>> 0;
1096
- if (!noAssert) checkOffset(offset, 1, this.length);
1097
- return this[offset];
1098
- };
1099
- Buffer2.prototype.readUint16LE = Buffer2.prototype.readUInt16LE = function readUInt16LE(offset, noAssert) {
1100
- offset = offset >>> 0;
1101
- if (!noAssert) checkOffset(offset, 2, this.length);
1102
- return this[offset] | this[offset + 1] << 8;
1103
- };
1104
- Buffer2.prototype.readUint16BE = Buffer2.prototype.readUInt16BE = function readUInt16BE(offset, noAssert) {
1105
- offset = offset >>> 0;
1106
- if (!noAssert) checkOffset(offset, 2, this.length);
1107
- return this[offset] << 8 | this[offset + 1];
1108
- };
1109
- Buffer2.prototype.readUint32LE = Buffer2.prototype.readUInt32LE = function readUInt32LE(offset, noAssert) {
1110
- offset = offset >>> 0;
1111
- if (!noAssert) checkOffset(offset, 4, this.length);
1112
- return (this[offset] | this[offset + 1] << 8 | this[offset + 2] << 16) + this[offset + 3] * 16777216;
1113
- };
1114
- Buffer2.prototype.readUint32BE = Buffer2.prototype.readUInt32BE = function readUInt32BE(offset, noAssert) {
1115
- offset = offset >>> 0;
1116
- if (!noAssert) checkOffset(offset, 4, this.length);
1117
- return this[offset] * 16777216 + (this[offset + 1] << 16 | this[offset + 2] << 8 | this[offset + 3]);
1118
- };
1119
- Buffer2.prototype.readBigUInt64LE = defineBigIntMethod(function readBigUInt64LE(offset) {
1120
- offset = offset >>> 0;
1121
- validateNumber(offset, "offset");
1122
- const first = this[offset];
1123
- const last = this[offset + 7];
1124
- if (first === void 0 || last === void 0) {
1125
- boundsError(offset, this.length - 8);
1126
- }
1127
- const lo = first + this[++offset] * 2 ** 8 + this[++offset] * 2 ** 16 + this[++offset] * 2 ** 24;
1128
- const hi = this[++offset] + this[++offset] * 2 ** 8 + this[++offset] * 2 ** 16 + last * 2 ** 24;
1129
- return BigInt(lo) + (BigInt(hi) << BigInt(32));
1130
- });
1131
- Buffer2.prototype.readBigUInt64BE = defineBigIntMethod(function readBigUInt64BE(offset) {
1132
- offset = offset >>> 0;
1133
- validateNumber(offset, "offset");
1134
- const first = this[offset];
1135
- const last = this[offset + 7];
1136
- if (first === void 0 || last === void 0) {
1137
- boundsError(offset, this.length - 8);
1138
- }
1139
- const hi = first * 2 ** 24 + this[++offset] * 2 ** 16 + this[++offset] * 2 ** 8 + this[++offset];
1140
- const lo = this[++offset] * 2 ** 24 + this[++offset] * 2 ** 16 + this[++offset] * 2 ** 8 + last;
1141
- return (BigInt(hi) << BigInt(32)) + BigInt(lo);
1142
- });
1143
- Buffer2.prototype.readIntLE = function readIntLE(offset, byteLength2, noAssert) {
1144
- offset = offset >>> 0;
1145
- byteLength2 = byteLength2 >>> 0;
1146
- if (!noAssert) checkOffset(offset, byteLength2, this.length);
1147
- let val = this[offset];
1148
- let mul = 1;
1149
- let i = 0;
1150
- while (++i < byteLength2 && (mul *= 256)) {
1151
- val += this[offset + i] * mul;
1152
- }
1153
- mul *= 128;
1154
- if (val >= mul) val -= Math.pow(2, 8 * byteLength2);
1155
- return val;
1156
- };
1157
- Buffer2.prototype.readIntBE = function readIntBE(offset, byteLength2, noAssert) {
1158
- offset = offset >>> 0;
1159
- byteLength2 = byteLength2 >>> 0;
1160
- if (!noAssert) checkOffset(offset, byteLength2, this.length);
1161
- let i = byteLength2;
1162
- let mul = 1;
1163
- let val = this[offset + --i];
1164
- while (i > 0 && (mul *= 256)) {
1165
- val += this[offset + --i] * mul;
1166
- }
1167
- mul *= 128;
1168
- if (val >= mul) val -= Math.pow(2, 8 * byteLength2);
1169
- return val;
1170
- };
1171
- Buffer2.prototype.readInt8 = function readInt8(offset, noAssert) {
1172
- offset = offset >>> 0;
1173
- if (!noAssert) checkOffset(offset, 1, this.length);
1174
- if (!(this[offset] & 128)) return this[offset];
1175
- return (255 - this[offset] + 1) * -1;
1176
- };
1177
- Buffer2.prototype.readInt16LE = function readInt16LE(offset, noAssert) {
1178
- offset = offset >>> 0;
1179
- if (!noAssert) checkOffset(offset, 2, this.length);
1180
- const val = this[offset] | this[offset + 1] << 8;
1181
- return val & 32768 ? val | 4294901760 : val;
1182
- };
1183
- Buffer2.prototype.readInt16BE = function readInt16BE(offset, noAssert) {
1184
- offset = offset >>> 0;
1185
- if (!noAssert) checkOffset(offset, 2, this.length);
1186
- const val = this[offset + 1] | this[offset] << 8;
1187
- return val & 32768 ? val | 4294901760 : val;
1188
- };
1189
- Buffer2.prototype.readInt32LE = function readInt32LE(offset, noAssert) {
1190
- offset = offset >>> 0;
1191
- if (!noAssert) checkOffset(offset, 4, this.length);
1192
- return this[offset] | this[offset + 1] << 8 | this[offset + 2] << 16 | this[offset + 3] << 24;
1193
- };
1194
- Buffer2.prototype.readInt32BE = function readInt32BE(offset, noAssert) {
1195
- offset = offset >>> 0;
1196
- if (!noAssert) checkOffset(offset, 4, this.length);
1197
- return this[offset] << 24 | this[offset + 1] << 16 | this[offset + 2] << 8 | this[offset + 3];
1198
- };
1199
- Buffer2.prototype.readBigInt64LE = defineBigIntMethod(function readBigInt64LE(offset) {
1200
- offset = offset >>> 0;
1201
- validateNumber(offset, "offset");
1202
- const first = this[offset];
1203
- const last = this[offset + 7];
1204
- if (first === void 0 || last === void 0) {
1205
- boundsError(offset, this.length - 8);
1206
- }
1207
- const val = this[offset + 4] + this[offset + 5] * 2 ** 8 + this[offset + 6] * 2 ** 16 + (last << 24);
1208
- return (BigInt(val) << BigInt(32)) + BigInt(first + this[++offset] * 2 ** 8 + this[++offset] * 2 ** 16 + this[++offset] * 2 ** 24);
1209
- });
1210
- Buffer2.prototype.readBigInt64BE = defineBigIntMethod(function readBigInt64BE(offset) {
1211
- offset = offset >>> 0;
1212
- validateNumber(offset, "offset");
1213
- const first = this[offset];
1214
- const last = this[offset + 7];
1215
- if (first === void 0 || last === void 0) {
1216
- boundsError(offset, this.length - 8);
1217
- }
1218
- const val = (first << 24) + // Overflow
1219
- this[++offset] * 2 ** 16 + this[++offset] * 2 ** 8 + this[++offset];
1220
- return (BigInt(val) << BigInt(32)) + BigInt(this[++offset] * 2 ** 24 + this[++offset] * 2 ** 16 + this[++offset] * 2 ** 8 + last);
1221
- });
1222
- Buffer2.prototype.readFloatLE = function readFloatLE(offset, noAssert) {
1223
- offset = offset >>> 0;
1224
- if (!noAssert) checkOffset(offset, 4, this.length);
1225
- return ieee7542.read(this, offset, true, 23, 4);
1226
- };
1227
- Buffer2.prototype.readFloatBE = function readFloatBE(offset, noAssert) {
1228
- offset = offset >>> 0;
1229
- if (!noAssert) checkOffset(offset, 4, this.length);
1230
- return ieee7542.read(this, offset, false, 23, 4);
1231
- };
1232
- Buffer2.prototype.readDoubleLE = function readDoubleLE(offset, noAssert) {
1233
- offset = offset >>> 0;
1234
- if (!noAssert) checkOffset(offset, 8, this.length);
1235
- return ieee7542.read(this, offset, true, 52, 8);
1236
- };
1237
- Buffer2.prototype.readDoubleBE = function readDoubleBE(offset, noAssert) {
1238
- offset = offset >>> 0;
1239
- if (!noAssert) checkOffset(offset, 8, this.length);
1240
- return ieee7542.read(this, offset, false, 52, 8);
1241
- };
1242
- function checkInt(buf, value, offset, ext, max, min) {
1243
- if (!Buffer2.isBuffer(buf)) throw new TypeError('"buffer" argument must be a Buffer instance');
1244
- if (value > max || value < min) throw new RangeError('"value" argument is out of bounds');
1245
- if (offset + ext > buf.length) throw new RangeError("Index out of range");
1246
- }
1247
- Buffer2.prototype.writeUintLE = Buffer2.prototype.writeUIntLE = function writeUIntLE(value, offset, byteLength2, noAssert) {
1248
- value = +value;
1249
- offset = offset >>> 0;
1250
- byteLength2 = byteLength2 >>> 0;
1251
- if (!noAssert) {
1252
- const maxBytes = Math.pow(2, 8 * byteLength2) - 1;
1253
- checkInt(this, value, offset, byteLength2, maxBytes, 0);
1254
- }
1255
- let mul = 1;
1256
- let i = 0;
1257
- this[offset] = value & 255;
1258
- while (++i < byteLength2 && (mul *= 256)) {
1259
- this[offset + i] = value / mul & 255;
1260
- }
1261
- return offset + byteLength2;
1262
- };
1263
- Buffer2.prototype.writeUintBE = Buffer2.prototype.writeUIntBE = function writeUIntBE(value, offset, byteLength2, noAssert) {
1264
- value = +value;
1265
- offset = offset >>> 0;
1266
- byteLength2 = byteLength2 >>> 0;
1267
- if (!noAssert) {
1268
- const maxBytes = Math.pow(2, 8 * byteLength2) - 1;
1269
- checkInt(this, value, offset, byteLength2, maxBytes, 0);
1270
- }
1271
- let i = byteLength2 - 1;
1272
- let mul = 1;
1273
- this[offset + i] = value & 255;
1274
- while (--i >= 0 && (mul *= 256)) {
1275
- this[offset + i] = value / mul & 255;
1276
- }
1277
- return offset + byteLength2;
1278
- };
1279
- Buffer2.prototype.writeUint8 = Buffer2.prototype.writeUInt8 = function writeUInt8(value, offset, noAssert) {
1280
- value = +value;
1281
- offset = offset >>> 0;
1282
- if (!noAssert) checkInt(this, value, offset, 1, 255, 0);
1283
- this[offset] = value & 255;
1284
- return offset + 1;
1285
- };
1286
- Buffer2.prototype.writeUint16LE = Buffer2.prototype.writeUInt16LE = function writeUInt16LE(value, offset, noAssert) {
1287
- value = +value;
1288
- offset = offset >>> 0;
1289
- if (!noAssert) checkInt(this, value, offset, 2, 65535, 0);
1290
- this[offset] = value & 255;
1291
- this[offset + 1] = value >>> 8;
1292
- return offset + 2;
1293
- };
1294
- Buffer2.prototype.writeUint16BE = Buffer2.prototype.writeUInt16BE = function writeUInt16BE(value, offset, noAssert) {
1295
- value = +value;
1296
- offset = offset >>> 0;
1297
- if (!noAssert) checkInt(this, value, offset, 2, 65535, 0);
1298
- this[offset] = value >>> 8;
1299
- this[offset + 1] = value & 255;
1300
- return offset + 2;
1301
- };
1302
- Buffer2.prototype.writeUint32LE = Buffer2.prototype.writeUInt32LE = function writeUInt32LE(value, offset, noAssert) {
1303
- value = +value;
1304
- offset = offset >>> 0;
1305
- if (!noAssert) checkInt(this, value, offset, 4, 4294967295, 0);
1306
- this[offset + 3] = value >>> 24;
1307
- this[offset + 2] = value >>> 16;
1308
- this[offset + 1] = value >>> 8;
1309
- this[offset] = value & 255;
1310
- return offset + 4;
1311
- };
1312
- Buffer2.prototype.writeUint32BE = Buffer2.prototype.writeUInt32BE = function writeUInt32BE(value, offset, noAssert) {
1313
- value = +value;
1314
- offset = offset >>> 0;
1315
- if (!noAssert) checkInt(this, value, offset, 4, 4294967295, 0);
1316
- this[offset] = value >>> 24;
1317
- this[offset + 1] = value >>> 16;
1318
- this[offset + 2] = value >>> 8;
1319
- this[offset + 3] = value & 255;
1320
- return offset + 4;
1321
- };
1322
- function wrtBigUInt64LE(buf, value, offset, min, max) {
1323
- checkIntBI(value, min, max, buf, offset, 7);
1324
- let lo = Number(value & BigInt(4294967295));
1325
- buf[offset++] = lo;
1326
- lo = lo >> 8;
1327
- buf[offset++] = lo;
1328
- lo = lo >> 8;
1329
- buf[offset++] = lo;
1330
- lo = lo >> 8;
1331
- buf[offset++] = lo;
1332
- let hi = Number(value >> BigInt(32) & BigInt(4294967295));
1333
- buf[offset++] = hi;
1334
- hi = hi >> 8;
1335
- buf[offset++] = hi;
1336
- hi = hi >> 8;
1337
- buf[offset++] = hi;
1338
- hi = hi >> 8;
1339
- buf[offset++] = hi;
1340
- return offset;
1341
- }
1342
- function wrtBigUInt64BE(buf, value, offset, min, max) {
1343
- checkIntBI(value, min, max, buf, offset, 7);
1344
- let lo = Number(value & BigInt(4294967295));
1345
- buf[offset + 7] = lo;
1346
- lo = lo >> 8;
1347
- buf[offset + 6] = lo;
1348
- lo = lo >> 8;
1349
- buf[offset + 5] = lo;
1350
- lo = lo >> 8;
1351
- buf[offset + 4] = lo;
1352
- let hi = Number(value >> BigInt(32) & BigInt(4294967295));
1353
- buf[offset + 3] = hi;
1354
- hi = hi >> 8;
1355
- buf[offset + 2] = hi;
1356
- hi = hi >> 8;
1357
- buf[offset + 1] = hi;
1358
- hi = hi >> 8;
1359
- buf[offset] = hi;
1360
- return offset + 8;
1361
- }
1362
- Buffer2.prototype.writeBigUInt64LE = defineBigIntMethod(function writeBigUInt64LE(value, offset = 0) {
1363
- return wrtBigUInt64LE(this, value, offset, BigInt(0), BigInt("0xffffffffffffffff"));
1364
- });
1365
- Buffer2.prototype.writeBigUInt64BE = defineBigIntMethod(function writeBigUInt64BE(value, offset = 0) {
1366
- return wrtBigUInt64BE(this, value, offset, BigInt(0), BigInt("0xffffffffffffffff"));
1367
- });
1368
- Buffer2.prototype.writeIntLE = function writeIntLE(value, offset, byteLength2, noAssert) {
1369
- value = +value;
1370
- offset = offset >>> 0;
1371
- if (!noAssert) {
1372
- const limit = Math.pow(2, 8 * byteLength2 - 1);
1373
- checkInt(this, value, offset, byteLength2, limit - 1, -limit);
1374
- }
1375
- let i = 0;
1376
- let mul = 1;
1377
- let sub = 0;
1378
- this[offset] = value & 255;
1379
- while (++i < byteLength2 && (mul *= 256)) {
1380
- if (value < 0 && sub === 0 && this[offset + i - 1] !== 0) {
1381
- sub = 1;
1382
- }
1383
- this[offset + i] = (value / mul >> 0) - sub & 255;
1384
- }
1385
- return offset + byteLength2;
1386
- };
1387
- Buffer2.prototype.writeIntBE = function writeIntBE(value, offset, byteLength2, noAssert) {
1388
- value = +value;
1389
- offset = offset >>> 0;
1390
- if (!noAssert) {
1391
- const limit = Math.pow(2, 8 * byteLength2 - 1);
1392
- checkInt(this, value, offset, byteLength2, limit - 1, -limit);
1393
- }
1394
- let i = byteLength2 - 1;
1395
- let mul = 1;
1396
- let sub = 0;
1397
- this[offset + i] = value & 255;
1398
- while (--i >= 0 && (mul *= 256)) {
1399
- if (value < 0 && sub === 0 && this[offset + i + 1] !== 0) {
1400
- sub = 1;
1401
- }
1402
- this[offset + i] = (value / mul >> 0) - sub & 255;
1403
- }
1404
- return offset + byteLength2;
1405
- };
1406
- Buffer2.prototype.writeInt8 = function writeInt8(value, offset, noAssert) {
1407
- value = +value;
1408
- offset = offset >>> 0;
1409
- if (!noAssert) checkInt(this, value, offset, 1, 127, -128);
1410
- if (value < 0) value = 255 + value + 1;
1411
- this[offset] = value & 255;
1412
- return offset + 1;
1413
- };
1414
- Buffer2.prototype.writeInt16LE = function writeInt16LE(value, offset, noAssert) {
1415
- value = +value;
1416
- offset = offset >>> 0;
1417
- if (!noAssert) checkInt(this, value, offset, 2, 32767, -32768);
1418
- this[offset] = value & 255;
1419
- this[offset + 1] = value >>> 8;
1420
- return offset + 2;
1421
- };
1422
- Buffer2.prototype.writeInt16BE = function writeInt16BE(value, offset, noAssert) {
1423
- value = +value;
1424
- offset = offset >>> 0;
1425
- if (!noAssert) checkInt(this, value, offset, 2, 32767, -32768);
1426
- this[offset] = value >>> 8;
1427
- this[offset + 1] = value & 255;
1428
- return offset + 2;
1429
- };
1430
- Buffer2.prototype.writeInt32LE = function writeInt32LE(value, offset, noAssert) {
1431
- value = +value;
1432
- offset = offset >>> 0;
1433
- if (!noAssert) checkInt(this, value, offset, 4, 2147483647, -2147483648);
1434
- this[offset] = value & 255;
1435
- this[offset + 1] = value >>> 8;
1436
- this[offset + 2] = value >>> 16;
1437
- this[offset + 3] = value >>> 24;
1438
- return offset + 4;
1439
- };
1440
- Buffer2.prototype.writeInt32BE = function writeInt32BE(value, offset, noAssert) {
1441
- value = +value;
1442
- offset = offset >>> 0;
1443
- if (!noAssert) checkInt(this, value, offset, 4, 2147483647, -2147483648);
1444
- if (value < 0) value = 4294967295 + value + 1;
1445
- this[offset] = value >>> 24;
1446
- this[offset + 1] = value >>> 16;
1447
- this[offset + 2] = value >>> 8;
1448
- this[offset + 3] = value & 255;
1449
- return offset + 4;
1450
- };
1451
- Buffer2.prototype.writeBigInt64LE = defineBigIntMethod(function writeBigInt64LE(value, offset = 0) {
1452
- return wrtBigUInt64LE(this, value, offset, -BigInt("0x8000000000000000"), BigInt("0x7fffffffffffffff"));
1453
- });
1454
- Buffer2.prototype.writeBigInt64BE = defineBigIntMethod(function writeBigInt64BE(value, offset = 0) {
1455
- return wrtBigUInt64BE(this, value, offset, -BigInt("0x8000000000000000"), BigInt("0x7fffffffffffffff"));
1456
- });
1457
- function checkIEEE754(buf, value, offset, ext, max, min) {
1458
- if (offset + ext > buf.length) throw new RangeError("Index out of range");
1459
- if (offset < 0) throw new RangeError("Index out of range");
1460
- }
1461
- function writeFloat(buf, value, offset, littleEndian, noAssert) {
1462
- value = +value;
1463
- offset = offset >>> 0;
1464
- if (!noAssert) {
1465
- checkIEEE754(buf, value, offset, 4);
1466
- }
1467
- ieee7542.write(buf, value, offset, littleEndian, 23, 4);
1468
- return offset + 4;
1469
- }
1470
- Buffer2.prototype.writeFloatLE = function writeFloatLE(value, offset, noAssert) {
1471
- return writeFloat(this, value, offset, true, noAssert);
1472
- };
1473
- Buffer2.prototype.writeFloatBE = function writeFloatBE(value, offset, noAssert) {
1474
- return writeFloat(this, value, offset, false, noAssert);
1475
- };
1476
- function writeDouble(buf, value, offset, littleEndian, noAssert) {
1477
- value = +value;
1478
- offset = offset >>> 0;
1479
- if (!noAssert) {
1480
- checkIEEE754(buf, value, offset, 8);
1481
- }
1482
- ieee7542.write(buf, value, offset, littleEndian, 52, 8);
1483
- return offset + 8;
1484
- }
1485
- Buffer2.prototype.writeDoubleLE = function writeDoubleLE(value, offset, noAssert) {
1486
- return writeDouble(this, value, offset, true, noAssert);
1487
- };
1488
- Buffer2.prototype.writeDoubleBE = function writeDoubleBE(value, offset, noAssert) {
1489
- return writeDouble(this, value, offset, false, noAssert);
1490
- };
1491
- Buffer2.prototype.copy = function copy(target, targetStart, start, end) {
1492
- if (!Buffer2.isBuffer(target)) throw new TypeError("argument should be a Buffer");
1493
- if (!start) start = 0;
1494
- if (!end && end !== 0) end = this.length;
1495
- if (targetStart >= target.length) targetStart = target.length;
1496
- if (!targetStart) targetStart = 0;
1497
- if (end > 0 && end < start) end = start;
1498
- if (end === start) return 0;
1499
- if (target.length === 0 || this.length === 0) return 0;
1500
- if (targetStart < 0) {
1501
- throw new RangeError("targetStart out of bounds");
1502
- }
1503
- if (start < 0 || start >= this.length) throw new RangeError("Index out of range");
1504
- if (end < 0) throw new RangeError("sourceEnd out of bounds");
1505
- if (end > this.length) end = this.length;
1506
- if (target.length - targetStart < end - start) {
1507
- end = target.length - targetStart + start;
1508
- }
1509
- const len = end - start;
1510
- if (this === target && typeof Uint8Array.prototype.copyWithin === "function") {
1511
- this.copyWithin(targetStart, start, end);
1512
- } else {
1513
- Uint8Array.prototype.set.call(
1514
- target,
1515
- this.subarray(start, end),
1516
- targetStart
1517
- );
1518
- }
1519
- return len;
1520
- };
1521
- Buffer2.prototype.fill = function fill(val, start, end, encoding) {
1522
- if (typeof val === "string") {
1523
- if (typeof start === "string") {
1524
- encoding = start;
1525
- start = 0;
1526
- end = this.length;
1527
- } else if (typeof end === "string") {
1528
- encoding = end;
1529
- end = this.length;
1530
- }
1531
- if (encoding !== void 0 && typeof encoding !== "string") {
1532
- throw new TypeError("encoding must be a string");
1533
- }
1534
- if (typeof encoding === "string" && !Buffer2.isEncoding(encoding)) {
1535
- throw new TypeError("Unknown encoding: " + encoding);
1536
- }
1537
- if (val.length === 1) {
1538
- const code = val.charCodeAt(0);
1539
- if (encoding === "utf8" && code < 128 || encoding === "latin1") {
1540
- val = code;
1541
- }
1542
- }
1543
- } else if (typeof val === "number") {
1544
- val = val & 255;
1545
- } else if (typeof val === "boolean") {
1546
- val = Number(val);
1547
- }
1548
- if (start < 0 || this.length < start || this.length < end) {
1549
- throw new RangeError("Out of range index");
1550
- }
1551
- if (end <= start) {
1552
- return this;
1553
- }
1554
- start = start >>> 0;
1555
- end = end === void 0 ? this.length : end >>> 0;
1556
- if (!val) val = 0;
1557
- let i;
1558
- if (typeof val === "number") {
1559
- for (i = start; i < end; ++i) {
1560
- this[i] = val;
1561
- }
1562
- } else {
1563
- const bytes = Buffer2.isBuffer(val) ? val : Buffer2.from(val, encoding);
1564
- const len = bytes.length;
1565
- if (len === 0) {
1566
- throw new TypeError('The value "' + val + '" is invalid for argument "value"');
1567
- }
1568
- for (i = 0; i < end - start; ++i) {
1569
- this[i + start] = bytes[i % len];
1570
- }
1571
- }
1572
- return this;
1573
- };
1574
- const errors = {};
1575
- function E(sym, getMessage, Base) {
1576
- errors[sym] = class NodeError extends Base {
1577
- constructor() {
1578
- super();
1579
- Object.defineProperty(this, "message", {
1580
- value: getMessage.apply(this, arguments),
1581
- writable: true,
1582
- configurable: true
1583
- });
1584
- this.name = `${this.name} [${sym}]`;
1585
- this.stack;
1586
- delete this.name;
1587
- }
1588
- get code() {
1589
- return sym;
1590
- }
1591
- set code(value) {
1592
- Object.defineProperty(this, "code", {
1593
- configurable: true,
1594
- enumerable: true,
1595
- value,
1596
- writable: true
1597
- });
1598
- }
1599
- toString() {
1600
- return `${this.name} [${sym}]: ${this.message}`;
1601
- }
1602
- };
1603
- }
1604
- E(
1605
- "ERR_BUFFER_OUT_OF_BOUNDS",
1606
- function(name) {
1607
- if (name) {
1608
- return `${name} is outside of buffer bounds`;
1609
- }
1610
- return "Attempt to access memory outside buffer bounds";
1611
- },
1612
- RangeError
1613
- );
1614
- E(
1615
- "ERR_INVALID_ARG_TYPE",
1616
- function(name, actual) {
1617
- return `The "${name}" argument must be of type number. Received type ${typeof actual}`;
1618
- },
1619
- TypeError
1620
- );
1621
- E(
1622
- "ERR_OUT_OF_RANGE",
1623
- function(str, range, input) {
1624
- let msg = `The value of "${str}" is out of range.`;
1625
- let received = input;
1626
- if (Number.isInteger(input) && Math.abs(input) > 2 ** 32) {
1627
- received = addNumericalSeparator(String(input));
1628
- } else if (typeof input === "bigint") {
1629
- received = String(input);
1630
- if (input > BigInt(2) ** BigInt(32) || input < -(BigInt(2) ** BigInt(32))) {
1631
- received = addNumericalSeparator(received);
1632
- }
1633
- received += "n";
1634
- }
1635
- msg += ` It must be ${range}. Received ${received}`;
1636
- return msg;
1637
- },
1638
- RangeError
1639
- );
1640
- function addNumericalSeparator(val) {
1641
- let res = "";
1642
- let i = val.length;
1643
- const start = val[0] === "-" ? 1 : 0;
1644
- for (; i >= start + 4; i -= 3) {
1645
- res = `_${val.slice(i - 3, i)}${res}`;
1646
- }
1647
- return `${val.slice(0, i)}${res}`;
1648
- }
1649
- function checkBounds(buf, offset, byteLength2) {
1650
- validateNumber(offset, "offset");
1651
- if (buf[offset] === void 0 || buf[offset + byteLength2] === void 0) {
1652
- boundsError(offset, buf.length - (byteLength2 + 1));
1653
- }
1654
- }
1655
- function checkIntBI(value, min, max, buf, offset, byteLength2) {
1656
- if (value > max || value < min) {
1657
- const n = typeof min === "bigint" ? "n" : "";
1658
- let range;
1659
- {
1660
- if (min === 0 || min === BigInt(0)) {
1661
- range = `>= 0${n} and < 2${n} ** ${(byteLength2 + 1) * 8}${n}`;
1662
- } else {
1663
- range = `>= -(2${n} ** ${(byteLength2 + 1) * 8 - 1}${n}) and < 2 ** ${(byteLength2 + 1) * 8 - 1}${n}`;
1664
- }
1665
- }
1666
- throw new errors.ERR_OUT_OF_RANGE("value", range, value);
1667
- }
1668
- checkBounds(buf, offset, byteLength2);
1669
- }
1670
- function validateNumber(value, name) {
1671
- if (typeof value !== "number") {
1672
- throw new errors.ERR_INVALID_ARG_TYPE(name, "number", value);
1673
- }
1674
- }
1675
- function boundsError(value, length, type) {
1676
- if (Math.floor(value) !== value) {
1677
- validateNumber(value, type);
1678
- throw new errors.ERR_OUT_OF_RANGE("offset", "an integer", value);
1679
- }
1680
- if (length < 0) {
1681
- throw new errors.ERR_BUFFER_OUT_OF_BOUNDS();
1682
- }
1683
- throw new errors.ERR_OUT_OF_RANGE(
1684
- "offset",
1685
- `>= ${0} and <= ${length}`,
1686
- value
1687
- );
1688
- }
1689
- const INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g;
1690
- function base64clean(str) {
1691
- str = str.split("=")[0];
1692
- str = str.trim().replace(INVALID_BASE64_RE, "");
1693
- if (str.length < 2) return "";
1694
- while (str.length % 4 !== 0) {
1695
- str = str + "=";
1696
- }
1697
- return str;
1698
- }
1699
- function utf8ToBytes(string, units) {
1700
- units = units || Infinity;
1701
- let codePoint;
1702
- const length = string.length;
1703
- let leadSurrogate = null;
1704
- const bytes = [];
1705
- for (let i = 0; i < length; ++i) {
1706
- codePoint = string.charCodeAt(i);
1707
- if (codePoint > 55295 && codePoint < 57344) {
1708
- if (!leadSurrogate) {
1709
- if (codePoint > 56319) {
1710
- if ((units -= 3) > -1) bytes.push(239, 191, 189);
1711
- continue;
1712
- } else if (i + 1 === length) {
1713
- if ((units -= 3) > -1) bytes.push(239, 191, 189);
1714
- continue;
1715
- }
1716
- leadSurrogate = codePoint;
1717
- continue;
1718
- }
1719
- if (codePoint < 56320) {
1720
- if ((units -= 3) > -1) bytes.push(239, 191, 189);
1721
- leadSurrogate = codePoint;
1722
- continue;
1723
- }
1724
- codePoint = (leadSurrogate - 55296 << 10 | codePoint - 56320) + 65536;
1725
- } else if (leadSurrogate) {
1726
- if ((units -= 3) > -1) bytes.push(239, 191, 189);
1727
- }
1728
- leadSurrogate = null;
1729
- if (codePoint < 128) {
1730
- if ((units -= 1) < 0) break;
1731
- bytes.push(codePoint);
1732
- } else if (codePoint < 2048) {
1733
- if ((units -= 2) < 0) break;
1734
- bytes.push(
1735
- codePoint >> 6 | 192,
1736
- codePoint & 63 | 128
1737
- );
1738
- } else if (codePoint < 65536) {
1739
- if ((units -= 3) < 0) break;
1740
- bytes.push(
1741
- codePoint >> 12 | 224,
1742
- codePoint >> 6 & 63 | 128,
1743
- codePoint & 63 | 128
1744
- );
1745
- } else if (codePoint < 1114112) {
1746
- if ((units -= 4) < 0) break;
1747
- bytes.push(
1748
- codePoint >> 18 | 240,
1749
- codePoint >> 12 & 63 | 128,
1750
- codePoint >> 6 & 63 | 128,
1751
- codePoint & 63 | 128
1752
- );
1753
- } else {
1754
- throw new Error("Invalid code point");
1755
- }
1756
- }
1757
- return bytes;
1758
- }
1759
- function asciiToBytes(str) {
1760
- const byteArray = [];
1761
- for (let i = 0; i < str.length; ++i) {
1762
- byteArray.push(str.charCodeAt(i) & 255);
1763
- }
1764
- return byteArray;
1765
- }
1766
- function utf16leToBytes(str, units) {
1767
- let c, hi, lo;
1768
- const byteArray = [];
1769
- for (let i = 0; i < str.length; ++i) {
1770
- if ((units -= 2) < 0) break;
1771
- c = str.charCodeAt(i);
1772
- hi = c >> 8;
1773
- lo = c % 256;
1774
- byteArray.push(lo);
1775
- byteArray.push(hi);
1776
- }
1777
- return byteArray;
1778
- }
1779
- function base64ToBytes(str) {
1780
- return base64.toByteArray(base64clean(str));
1781
- }
1782
- function blitBuffer(src, dst, offset, length) {
1783
- let i;
1784
- for (i = 0; i < length; ++i) {
1785
- if (i + offset >= dst.length || i >= src.length) break;
1786
- dst[i + offset] = src[i];
1787
- }
1788
- return i;
1789
- }
1790
- function isInstance(obj, type) {
1791
- return obj instanceof type || obj != null && obj.constructor != null && obj.constructor.name != null && obj.constructor.name === type.name;
1792
- }
1793
- function numberIsNaN(obj) {
1794
- return obj !== obj;
1795
- }
1796
- const hexSliceLookupTable = (function() {
1797
- const alphabet = "0123456789abcdef";
1798
- const table = new Array(256);
1799
- for (let i = 0; i < 16; ++i) {
1800
- const i16 = i * 16;
1801
- for (let j = 0; j < 16; ++j) {
1802
- table[i16 + j] = alphabet[i] + alphabet[j];
1803
- }
1804
- }
1805
- return table;
1806
- })();
1807
- function defineBigIntMethod(fn) {
1808
- return typeof BigInt === "undefined" ? BufferBigIntNotDefined : fn;
1809
- }
1810
- function BufferBigIntNotDefined() {
1811
- throw new Error("BigInt not supported");
1812
- }
1813
- })(buffer);
1814
- return buffer;
1815
- }
1816
- var hasRequiredSafeBuffer;
1817
- function requireSafeBuffer() {
1818
- if (hasRequiredSafeBuffer) return safeBuffer.exports;
1819
- hasRequiredSafeBuffer = 1;
1820
- (function(module, exports) {
1821
- var buffer2 = requireBuffer();
1822
- var Buffer2 = buffer2.Buffer;
1823
- function copyProps(src, dst) {
1824
- for (var key in src) {
1825
- dst[key] = src[key];
1826
- }
1827
- }
1828
- if (Buffer2.from && Buffer2.alloc && Buffer2.allocUnsafe && Buffer2.allocUnsafeSlow) {
1829
- module.exports = buffer2;
1830
- } else {
1831
- copyProps(buffer2, exports);
1832
- exports.Buffer = SafeBuffer;
1833
- }
1834
- function SafeBuffer(arg, encodingOrOffset, length) {
1835
- return Buffer2(arg, encodingOrOffset, length);
1836
- }
1837
- SafeBuffer.prototype = Object.create(Buffer2.prototype);
1838
- copyProps(Buffer2, SafeBuffer);
1839
- SafeBuffer.from = function(arg, encodingOrOffset, length) {
1840
- if (typeof arg === "number") {
1841
- throw new TypeError("Argument must not be a number");
1842
- }
1843
- return Buffer2(arg, encodingOrOffset, length);
1844
- };
1845
- SafeBuffer.alloc = function(size, fill, encoding) {
1846
- if (typeof size !== "number") {
1847
- throw new TypeError("Argument must be a number");
1848
- }
1849
- var buf = Buffer2(size);
1850
- if (fill !== void 0) {
1851
- if (typeof encoding === "string") {
1852
- buf.fill(fill, encoding);
1853
- } else {
1854
- buf.fill(fill);
1855
- }
1856
- } else {
1857
- buf.fill(0);
1858
- }
1859
- return buf;
1860
- };
1861
- SafeBuffer.allocUnsafe = function(size) {
1862
- if (typeof size !== "number") {
1863
- throw new TypeError("Argument must be a number");
1864
- }
1865
- return Buffer2(size);
1866
- };
1867
- SafeBuffer.allocUnsafeSlow = function(size) {
1868
- if (typeof size !== "number") {
1869
- throw new TypeError("Argument must be a number");
1870
- }
1871
- return buffer2.SlowBuffer(size);
1872
- };
1873
- })(safeBuffer, safeBuffer.exports);
1874
- return safeBuffer.exports;
1875
- }
1876
- var hasRequiredString_decoder;
1877
- function requireString_decoder() {
1878
- if (hasRequiredString_decoder) return string_decoder;
1879
- hasRequiredString_decoder = 1;
1880
- var Buffer2 = requireSafeBuffer().Buffer;
1881
- var isEncoding = Buffer2.isEncoding || function(encoding) {
1882
- encoding = "" + encoding;
1883
- switch (encoding && encoding.toLowerCase()) {
1884
- case "hex":
1885
- case "utf8":
1886
- case "utf-8":
1887
- case "ascii":
1888
- case "binary":
1889
- case "base64":
1890
- case "ucs2":
1891
- case "ucs-2":
1892
- case "utf16le":
1893
- case "utf-16le":
1894
- case "raw":
1895
- return true;
1896
- default:
1897
- return false;
1898
- }
1899
- };
1900
- function _normalizeEncoding(enc) {
1901
- if (!enc) return "utf8";
1902
- var retried;
1903
- while (true) {
1904
- switch (enc) {
1905
- case "utf8":
1906
- case "utf-8":
1907
- return "utf8";
1908
- case "ucs2":
1909
- case "ucs-2":
1910
- case "utf16le":
1911
- case "utf-16le":
1912
- return "utf16le";
1913
- case "latin1":
1914
- case "binary":
1915
- return "latin1";
1916
- case "base64":
1917
- case "ascii":
1918
- case "hex":
1919
- return enc;
1920
- default:
1921
- if (retried) return;
1922
- enc = ("" + enc).toLowerCase();
1923
- retried = true;
1924
- }
1925
- }
1926
- }
1927
- function normalizeEncoding(enc) {
1928
- var nenc = _normalizeEncoding(enc);
1929
- if (typeof nenc !== "string" && (Buffer2.isEncoding === isEncoding || !isEncoding(enc))) throw new Error("Unknown encoding: " + enc);
1930
- return nenc || enc;
1931
- }
1932
- string_decoder.StringDecoder = StringDecoder;
1933
- function StringDecoder(encoding) {
1934
- this.encoding = normalizeEncoding(encoding);
1935
- var nb;
1936
- switch (this.encoding) {
1937
- case "utf16le":
1938
- this.text = utf16Text;
1939
- this.end = utf16End;
1940
- nb = 4;
1941
- break;
1942
- case "utf8":
1943
- this.fillLast = utf8FillLast;
1944
- nb = 4;
1945
- break;
1946
- case "base64":
1947
- this.text = base64Text;
1948
- this.end = base64End;
1949
- nb = 3;
1950
- break;
1951
- default:
1952
- this.write = simpleWrite;
1953
- this.end = simpleEnd;
1954
- return;
1955
- }
1956
- this.lastNeed = 0;
1957
- this.lastTotal = 0;
1958
- this.lastChar = Buffer2.allocUnsafe(nb);
1959
- }
1960
- StringDecoder.prototype.write = function(buf) {
1961
- if (buf.length === 0) return "";
1962
- var r;
1963
- var i;
1964
- if (this.lastNeed) {
1965
- r = this.fillLast(buf);
1966
- if (r === void 0) return "";
1967
- i = this.lastNeed;
1968
- this.lastNeed = 0;
1969
- } else {
1970
- i = 0;
1971
- }
1972
- if (i < buf.length) return r ? r + this.text(buf, i) : this.text(buf, i);
1973
- return r || "";
1974
- };
1975
- StringDecoder.prototype.end = utf8End;
1976
- StringDecoder.prototype.text = utf8Text;
1977
- StringDecoder.prototype.fillLast = function(buf) {
1978
- if (this.lastNeed <= buf.length) {
1979
- buf.copy(this.lastChar, this.lastTotal - this.lastNeed, 0, this.lastNeed);
1980
- return this.lastChar.toString(this.encoding, 0, this.lastTotal);
1981
- }
1982
- buf.copy(this.lastChar, this.lastTotal - this.lastNeed, 0, buf.length);
1983
- this.lastNeed -= buf.length;
1984
- };
1985
- function utf8CheckByte(byte) {
1986
- if (byte <= 127) return 0;
1987
- else if (byte >> 5 === 6) return 2;
1988
- else if (byte >> 4 === 14) return 3;
1989
- else if (byte >> 3 === 30) return 4;
1990
- return byte >> 6 === 2 ? -1 : -2;
1991
- }
1992
- function utf8CheckIncomplete(self, buf, i) {
1993
- var j = buf.length - 1;
1994
- if (j < i) return 0;
1995
- var nb = utf8CheckByte(buf[j]);
1996
- if (nb >= 0) {
1997
- if (nb > 0) self.lastNeed = nb - 1;
1998
- return nb;
1999
- }
2000
- if (--j < i || nb === -2) return 0;
2001
- nb = utf8CheckByte(buf[j]);
2002
- if (nb >= 0) {
2003
- if (nb > 0) self.lastNeed = nb - 2;
2004
- return nb;
2005
- }
2006
- if (--j < i || nb === -2) return 0;
2007
- nb = utf8CheckByte(buf[j]);
2008
- if (nb >= 0) {
2009
- if (nb > 0) {
2010
- if (nb === 2) nb = 0;
2011
- else self.lastNeed = nb - 3;
2012
- }
2013
- return nb;
2014
- }
2015
- return 0;
2016
- }
2017
- function utf8CheckExtraBytes(self, buf, p) {
2018
- if ((buf[0] & 192) !== 128) {
2019
- self.lastNeed = 0;
2020
- return "�";
2021
- }
2022
- if (self.lastNeed > 1 && buf.length > 1) {
2023
- if ((buf[1] & 192) !== 128) {
2024
- self.lastNeed = 1;
2025
- return "�";
2026
- }
2027
- if (self.lastNeed > 2 && buf.length > 2) {
2028
- if ((buf[2] & 192) !== 128) {
2029
- self.lastNeed = 2;
2030
- return "�";
2031
- }
2032
- }
2033
- }
2034
- }
2035
- function utf8FillLast(buf) {
2036
- var p = this.lastTotal - this.lastNeed;
2037
- var r = utf8CheckExtraBytes(this, buf);
2038
- if (r !== void 0) return r;
2039
- if (this.lastNeed <= buf.length) {
2040
- buf.copy(this.lastChar, p, 0, this.lastNeed);
2041
- return this.lastChar.toString(this.encoding, 0, this.lastTotal);
2042
- }
2043
- buf.copy(this.lastChar, p, 0, buf.length);
2044
- this.lastNeed -= buf.length;
2045
- }
2046
- function utf8Text(buf, i) {
2047
- var total = utf8CheckIncomplete(this, buf, i);
2048
- if (!this.lastNeed) return buf.toString("utf8", i);
2049
- this.lastTotal = total;
2050
- var end = buf.length - (total - this.lastNeed);
2051
- buf.copy(this.lastChar, 0, end);
2052
- return buf.toString("utf8", i, end);
2053
- }
2054
- function utf8End(buf) {
2055
- var r = buf && buf.length ? this.write(buf) : "";
2056
- if (this.lastNeed) return r + "�";
2057
- return r;
2058
- }
2059
- function utf16Text(buf, i) {
2060
- if ((buf.length - i) % 2 === 0) {
2061
- var r = buf.toString("utf16le", i);
2062
- if (r) {
2063
- var c = r.charCodeAt(r.length - 1);
2064
- if (c >= 55296 && c <= 56319) {
2065
- this.lastNeed = 2;
2066
- this.lastTotal = 4;
2067
- this.lastChar[0] = buf[buf.length - 2];
2068
- this.lastChar[1] = buf[buf.length - 1];
2069
- return r.slice(0, -1);
2070
- }
2071
- }
2072
- return r;
2073
- }
2074
- this.lastNeed = 1;
2075
- this.lastTotal = 2;
2076
- this.lastChar[0] = buf[buf.length - 1];
2077
- return buf.toString("utf16le", i, buf.length - 1);
2078
- }
2079
- function utf16End(buf) {
2080
- var r = buf && buf.length ? this.write(buf) : "";
2081
- if (this.lastNeed) {
2082
- var end = this.lastTotal - this.lastNeed;
2083
- return r + this.lastChar.toString("utf16le", 0, end);
2084
- }
2085
- return r;
2086
- }
2087
- function base64Text(buf, i) {
2088
- var n = (buf.length - i) % 3;
2089
- if (n === 0) return buf.toString("base64", i);
2090
- this.lastNeed = 3 - n;
2091
- this.lastTotal = 3;
2092
- if (n === 1) {
2093
- this.lastChar[0] = buf[buf.length - 1];
2094
- } else {
2095
- this.lastChar[0] = buf[buf.length - 2];
2096
- this.lastChar[1] = buf[buf.length - 1];
2097
- }
2098
- return buf.toString("base64", i, buf.length - n);
2099
- }
2100
- function base64End(buf) {
2101
- var r = buf && buf.length ? this.write(buf) : "";
2102
- if (this.lastNeed) return r + this.lastChar.toString("base64", 0, 3 - this.lastNeed);
2103
- return r;
2104
- }
2105
- function simpleWrite(buf) {
2106
- return buf.toString(this.encoding);
2107
- }
2108
- function simpleEnd(buf) {
2109
- return buf && buf.length ? this.write(buf) : "";
2110
- }
2111
- return string_decoder;
2112
- }
2113
- var hasRequiredSax;
2114
- function requireSax() {
2115
- if (hasRequiredSax) return sax;
2116
- hasRequiredSax = 1;
2117
- (function(exports) {
2118
- (function(sax2) {
2119
- sax2.parser = function(strict, opt) {
2120
- return new SAXParser(strict, opt);
2121
- };
2122
- sax2.SAXParser = SAXParser;
2123
- sax2.SAXStream = SAXStream;
2124
- sax2.createStream = createStream;
2125
- sax2.MAX_BUFFER_LENGTH = 64 * 1024;
2126
- var buffers = [
2127
- "comment",
2128
- "sgmlDecl",
2129
- "textNode",
2130
- "tagName",
2131
- "doctype",
2132
- "procInstName",
2133
- "procInstBody",
2134
- "entity",
2135
- "attribName",
2136
- "attribValue",
2137
- "cdata",
2138
- "script"
2139
- ];
2140
- sax2.EVENTS = [
2141
- "text",
2142
- "processinginstruction",
2143
- "sgmldeclaration",
2144
- "doctype",
2145
- "comment",
2146
- "opentagstart",
2147
- "attribute",
2148
- "opentag",
2149
- "closetag",
2150
- "opencdata",
2151
- "cdata",
2152
- "closecdata",
2153
- "error",
2154
- "end",
2155
- "ready",
2156
- "script",
2157
- "opennamespace",
2158
- "closenamespace"
2159
- ];
2160
- function SAXParser(strict, opt) {
2161
- if (!(this instanceof SAXParser)) {
2162
- return new SAXParser(strict, opt);
2163
- }
2164
- var parser = this;
2165
- clearBuffers(parser);
2166
- parser.q = parser.c = "";
2167
- parser.bufferCheckPosition = sax2.MAX_BUFFER_LENGTH;
2168
- parser.opt = opt || {};
2169
- parser.opt.lowercase = parser.opt.lowercase || parser.opt.lowercasetags;
2170
- parser.looseCase = parser.opt.lowercase ? "toLowerCase" : "toUpperCase";
2171
- parser.tags = [];
2172
- parser.closed = parser.closedRoot = parser.sawRoot = false;
2173
- parser.tag = parser.error = null;
2174
- parser.strict = !!strict;
2175
- parser.noscript = !!(strict || parser.opt.noscript);
2176
- parser.state = S.BEGIN;
2177
- parser.strictEntities = parser.opt.strictEntities;
2178
- parser.ENTITIES = parser.strictEntities ? Object.create(sax2.XML_ENTITIES) : Object.create(sax2.ENTITIES);
2179
- parser.attribList = [];
2180
- if (parser.opt.xmlns) {
2181
- parser.ns = Object.create(rootNS);
2182
- }
2183
- if (parser.opt.unquotedAttributeValues === void 0) {
2184
- parser.opt.unquotedAttributeValues = !strict;
2185
- }
2186
- parser.trackPosition = parser.opt.position !== false;
2187
- if (parser.trackPosition) {
2188
- parser.position = parser.line = parser.column = 0;
2189
- }
2190
- emit(parser, "onready");
2191
- }
2192
- if (!Object.create) {
2193
- Object.create = function(o) {
2194
- function F() {
2195
- }
2196
- F.prototype = o;
2197
- var newf = new F();
2198
- return newf;
2199
- };
2200
- }
2201
- if (!Object.keys) {
2202
- Object.keys = function(o) {
2203
- var a = [];
2204
- for (var i in o) if (o.hasOwnProperty(i)) a.push(i);
2205
- return a;
2206
- };
2207
- }
2208
- function checkBufferLength(parser) {
2209
- var maxAllowed = Math.max(sax2.MAX_BUFFER_LENGTH, 10);
2210
- var maxActual = 0;
2211
- for (var i = 0, l = buffers.length; i < l; i++) {
2212
- var len = parser[buffers[i]].length;
2213
- if (len > maxAllowed) {
2214
- switch (buffers[i]) {
2215
- case "textNode":
2216
- closeText(parser);
2217
- break;
2218
- case "cdata":
2219
- emitNode(parser, "oncdata", parser.cdata);
2220
- parser.cdata = "";
2221
- break;
2222
- case "script":
2223
- emitNode(parser, "onscript", parser.script);
2224
- parser.script = "";
2225
- break;
2226
- default:
2227
- error(parser, "Max buffer length exceeded: " + buffers[i]);
2228
- }
2229
- }
2230
- maxActual = Math.max(maxActual, len);
2231
- }
2232
- var m = sax2.MAX_BUFFER_LENGTH - maxActual;
2233
- parser.bufferCheckPosition = m + parser.position;
2234
- }
2235
- function clearBuffers(parser) {
2236
- for (var i = 0, l = buffers.length; i < l; i++) {
2237
- parser[buffers[i]] = "";
2238
- }
2239
- }
2240
- function flushBuffers(parser) {
2241
- closeText(parser);
2242
- if (parser.cdata !== "") {
2243
- emitNode(parser, "oncdata", parser.cdata);
2244
- parser.cdata = "";
2245
- }
2246
- if (parser.script !== "") {
2247
- emitNode(parser, "onscript", parser.script);
2248
- parser.script = "";
2249
- }
2250
- }
2251
- SAXParser.prototype = {
2252
- end: function() {
2253
- end(this);
2254
- },
2255
- write,
2256
- resume: function() {
2257
- this.error = null;
2258
- return this;
2259
- },
2260
- close: function() {
2261
- return this.write(null);
2262
- },
2263
- flush: function() {
2264
- flushBuffers(this);
2265
- }
2266
- };
2267
- var Stream;
2268
- try {
2269
- Stream = require$$0.Stream;
2270
- } catch (ex) {
2271
- Stream = function() {
2272
- };
2273
- }
2274
- if (!Stream) Stream = function() {
2275
- };
2276
- var streamWraps = sax2.EVENTS.filter(function(ev) {
2277
- return ev !== "error" && ev !== "end";
2278
- });
2279
- function createStream(strict, opt) {
2280
- return new SAXStream(strict, opt);
2281
- }
2282
- function SAXStream(strict, opt) {
2283
- if (!(this instanceof SAXStream)) {
2284
- return new SAXStream(strict, opt);
2285
- }
2286
- Stream.apply(this);
2287
- this._parser = new SAXParser(strict, opt);
2288
- this.writable = true;
2289
- this.readable = true;
2290
- var me = this;
2291
- this._parser.onend = function() {
2292
- me.emit("end");
2293
- };
2294
- this._parser.onerror = function(er) {
2295
- me.emit("error", er);
2296
- me._parser.error = null;
2297
- };
2298
- this._decoder = null;
2299
- streamWraps.forEach(function(ev) {
2300
- Object.defineProperty(me, "on" + ev, {
2301
- get: function() {
2302
- return me._parser["on" + ev];
2303
- },
2304
- set: function(h) {
2305
- if (!h) {
2306
- me.removeAllListeners(ev);
2307
- me._parser["on" + ev] = h;
2308
- return h;
2309
- }
2310
- me.on(ev, h);
2311
- },
2312
- enumerable: true,
2313
- configurable: false
2314
- });
2315
- });
2316
- }
2317
- SAXStream.prototype = Object.create(Stream.prototype, {
2318
- constructor: {
2319
- value: SAXStream
2320
- }
2321
- });
2322
- SAXStream.prototype.write = function(data) {
2323
- if (typeof Buffer === "function" && typeof Buffer.isBuffer === "function" && Buffer.isBuffer(data)) {
2324
- if (!this._decoder) {
2325
- var SD = requireString_decoder().StringDecoder;
2326
- this._decoder = new SD("utf8");
2327
- }
2328
- data = this._decoder.write(data);
2329
- }
2330
- this._parser.write(data.toString());
2331
- this.emit("data", data);
2332
- return true;
2333
- };
2334
- SAXStream.prototype.end = function(chunk) {
2335
- if (chunk && chunk.length) {
2336
- this.write(chunk);
2337
- }
2338
- this._parser.end();
2339
- return true;
2340
- };
2341
- SAXStream.prototype.on = function(ev, handler) {
2342
- var me = this;
2343
- if (!me._parser["on" + ev] && streamWraps.indexOf(ev) !== -1) {
2344
- me._parser["on" + ev] = function() {
2345
- var args = arguments.length === 1 ? [arguments[0]] : Array.apply(null, arguments);
2346
- args.splice(0, 0, ev);
2347
- me.emit.apply(me, args);
2348
- };
2349
- }
2350
- return Stream.prototype.on.call(me, ev, handler);
2351
- };
2352
- var CDATA = "[CDATA[";
2353
- var DOCTYPE = "DOCTYPE";
2354
- var XML_NAMESPACE = "http://www.w3.org/XML/1998/namespace";
2355
- var XMLNS_NAMESPACE = "http://www.w3.org/2000/xmlns/";
2356
- var rootNS = { xml: XML_NAMESPACE, xmlns: XMLNS_NAMESPACE };
2357
- 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]/;
2358
- 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-]/;
2359
- 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]/;
2360
- 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-]/;
2361
- function isWhitespace(c) {
2362
- return c === " " || c === "\n" || c === "\r" || c === " ";
2363
- }
2364
- function isQuote(c) {
2365
- return c === '"' || c === "'";
2366
- }
2367
- function isAttribEnd(c) {
2368
- return c === ">" || isWhitespace(c);
2369
- }
2370
- function isMatch(regex, c) {
2371
- return regex.test(c);
2372
- }
2373
- function notMatch(regex, c) {
2374
- return !isMatch(regex, c);
2375
- }
2376
- var S = 0;
2377
- sax2.STATE = {
2378
- BEGIN: S++,
2379
- // leading byte order mark or whitespace
2380
- BEGIN_WHITESPACE: S++,
2381
- // leading whitespace
2382
- TEXT: S++,
2383
- // general stuff
2384
- TEXT_ENTITY: S++,
2385
- // &amp and such.
2386
- OPEN_WAKA: S++,
2387
- // <
2388
- SGML_DECL: S++,
2389
- // <!BLARG
2390
- SGML_DECL_QUOTED: S++,
2391
- // <!BLARG foo "bar
2392
- DOCTYPE: S++,
2393
- // <!DOCTYPE
2394
- DOCTYPE_QUOTED: S++,
2395
- // <!DOCTYPE "//blah
2396
- DOCTYPE_DTD: S++,
2397
- // <!DOCTYPE "//blah" [ ...
2398
- DOCTYPE_DTD_QUOTED: S++,
2399
- // <!DOCTYPE "//blah" [ "foo
2400
- COMMENT_STARTING: S++,
2401
- // <!-
2402
- COMMENT: S++,
2403
- // <!--
2404
- COMMENT_ENDING: S++,
2405
- // <!-- blah -
2406
- COMMENT_ENDED: S++,
2407
- // <!-- blah --
2408
- CDATA: S++,
2409
- // <![CDATA[ something
2410
- CDATA_ENDING: S++,
2411
- // ]
2412
- CDATA_ENDING_2: S++,
2413
- // ]]
2414
- PROC_INST: S++,
2415
- // <?hi
2416
- PROC_INST_BODY: S++,
2417
- // <?hi there
2418
- PROC_INST_ENDING: S++,
2419
- // <?hi "there" ?
2420
- OPEN_TAG: S++,
2421
- // <strong
2422
- OPEN_TAG_SLASH: S++,
2423
- // <strong /
2424
- ATTRIB: S++,
2425
- // <a
2426
- ATTRIB_NAME: S++,
2427
- // <a foo
2428
- ATTRIB_NAME_SAW_WHITE: S++,
2429
- // <a foo _
2430
- ATTRIB_VALUE: S++,
2431
- // <a foo=
2432
- ATTRIB_VALUE_QUOTED: S++,
2433
- // <a foo="bar
2434
- ATTRIB_VALUE_CLOSED: S++,
2435
- // <a foo="bar"
2436
- ATTRIB_VALUE_UNQUOTED: S++,
2437
- // <a foo=bar
2438
- ATTRIB_VALUE_ENTITY_Q: S++,
2439
- // <foo bar="&quot;"
2440
- ATTRIB_VALUE_ENTITY_U: S++,
2441
- // <foo bar=&quot
2442
- CLOSE_TAG: S++,
2443
- // </a
2444
- CLOSE_TAG_SAW_WHITE: S++,
2445
- // </a >
2446
- SCRIPT: S++,
2447
- // <script> ...
2448
- SCRIPT_ENDING: S++
2449
- // <script> ... <
2450
- };
2451
- sax2.XML_ENTITIES = {
2452
- "amp": "&",
2453
- "gt": ">",
2454
- "lt": "<",
2455
- "quot": '"',
2456
- "apos": "'"
2457
- };
2458
- sax2.ENTITIES = {
2459
- "amp": "&",
2460
- "gt": ">",
2461
- "lt": "<",
2462
- "quot": '"',
2463
- "apos": "'",
2464
- "AElig": 198,
2465
- "Aacute": 193,
2466
- "Acirc": 194,
2467
- "Agrave": 192,
2468
- "Aring": 197,
2469
- "Atilde": 195,
2470
- "Auml": 196,
2471
- "Ccedil": 199,
2472
- "ETH": 208,
2473
- "Eacute": 201,
2474
- "Ecirc": 202,
2475
- "Egrave": 200,
2476
- "Euml": 203,
2477
- "Iacute": 205,
2478
- "Icirc": 206,
2479
- "Igrave": 204,
2480
- "Iuml": 207,
2481
- "Ntilde": 209,
2482
- "Oacute": 211,
2483
- "Ocirc": 212,
2484
- "Ograve": 210,
2485
- "Oslash": 216,
2486
- "Otilde": 213,
2487
- "Ouml": 214,
2488
- "THORN": 222,
2489
- "Uacute": 218,
2490
- "Ucirc": 219,
2491
- "Ugrave": 217,
2492
- "Uuml": 220,
2493
- "Yacute": 221,
2494
- "aacute": 225,
2495
- "acirc": 226,
2496
- "aelig": 230,
2497
- "agrave": 224,
2498
- "aring": 229,
2499
- "atilde": 227,
2500
- "auml": 228,
2501
- "ccedil": 231,
2502
- "eacute": 233,
2503
- "ecirc": 234,
2504
- "egrave": 232,
2505
- "eth": 240,
2506
- "euml": 235,
2507
- "iacute": 237,
2508
- "icirc": 238,
2509
- "igrave": 236,
2510
- "iuml": 239,
2511
- "ntilde": 241,
2512
- "oacute": 243,
2513
- "ocirc": 244,
2514
- "ograve": 242,
2515
- "oslash": 248,
2516
- "otilde": 245,
2517
- "ouml": 246,
2518
- "szlig": 223,
2519
- "thorn": 254,
2520
- "uacute": 250,
2521
- "ucirc": 251,
2522
- "ugrave": 249,
2523
- "uuml": 252,
2524
- "yacute": 253,
2525
- "yuml": 255,
2526
- "copy": 169,
2527
- "reg": 174,
2528
- "nbsp": 160,
2529
- "iexcl": 161,
2530
- "cent": 162,
2531
- "pound": 163,
2532
- "curren": 164,
2533
- "yen": 165,
2534
- "brvbar": 166,
2535
- "sect": 167,
2536
- "uml": 168,
2537
- "ordf": 170,
2538
- "laquo": 171,
2539
- "not": 172,
2540
- "shy": 173,
2541
- "macr": 175,
2542
- "deg": 176,
2543
- "plusmn": 177,
2544
- "sup1": 185,
2545
- "sup2": 178,
2546
- "sup3": 179,
2547
- "acute": 180,
2548
- "micro": 181,
2549
- "para": 182,
2550
- "middot": 183,
2551
- "cedil": 184,
2552
- "ordm": 186,
2553
- "raquo": 187,
2554
- "frac14": 188,
2555
- "frac12": 189,
2556
- "frac34": 190,
2557
- "iquest": 191,
2558
- "times": 215,
2559
- "divide": 247,
2560
- "OElig": 338,
2561
- "oelig": 339,
2562
- "Scaron": 352,
2563
- "scaron": 353,
2564
- "Yuml": 376,
2565
- "fnof": 402,
2566
- "circ": 710,
2567
- "tilde": 732,
2568
- "Alpha": 913,
2569
- "Beta": 914,
2570
- "Gamma": 915,
2571
- "Delta": 916,
2572
- "Epsilon": 917,
2573
- "Zeta": 918,
2574
- "Eta": 919,
2575
- "Theta": 920,
2576
- "Iota": 921,
2577
- "Kappa": 922,
2578
- "Lambda": 923,
2579
- "Mu": 924,
2580
- "Nu": 925,
2581
- "Xi": 926,
2582
- "Omicron": 927,
2583
- "Pi": 928,
2584
- "Rho": 929,
2585
- "Sigma": 931,
2586
- "Tau": 932,
2587
- "Upsilon": 933,
2588
- "Phi": 934,
2589
- "Chi": 935,
2590
- "Psi": 936,
2591
- "Omega": 937,
2592
- "alpha": 945,
2593
- "beta": 946,
2594
- "gamma": 947,
2595
- "delta": 948,
2596
- "epsilon": 949,
2597
- "zeta": 950,
2598
- "eta": 951,
2599
- "theta": 952,
2600
- "iota": 953,
2601
- "kappa": 954,
2602
- "lambda": 955,
2603
- "mu": 956,
2604
- "nu": 957,
2605
- "xi": 958,
2606
- "omicron": 959,
2607
- "pi": 960,
2608
- "rho": 961,
2609
- "sigmaf": 962,
2610
- "sigma": 963,
2611
- "tau": 964,
2612
- "upsilon": 965,
2613
- "phi": 966,
2614
- "chi": 967,
2615
- "psi": 968,
2616
- "omega": 969,
2617
- "thetasym": 977,
2618
- "upsih": 978,
2619
- "piv": 982,
2620
- "ensp": 8194,
2621
- "emsp": 8195,
2622
- "thinsp": 8201,
2623
- "zwnj": 8204,
2624
- "zwj": 8205,
2625
- "lrm": 8206,
2626
- "rlm": 8207,
2627
- "ndash": 8211,
2628
- "mdash": 8212,
2629
- "lsquo": 8216,
2630
- "rsquo": 8217,
2631
- "sbquo": 8218,
2632
- "ldquo": 8220,
2633
- "rdquo": 8221,
2634
- "bdquo": 8222,
2635
- "dagger": 8224,
2636
- "Dagger": 8225,
2637
- "bull": 8226,
2638
- "hellip": 8230,
2639
- "permil": 8240,
2640
- "prime": 8242,
2641
- "Prime": 8243,
2642
- "lsaquo": 8249,
2643
- "rsaquo": 8250,
2644
- "oline": 8254,
2645
- "frasl": 8260,
2646
- "euro": 8364,
2647
- "image": 8465,
2648
- "weierp": 8472,
2649
- "real": 8476,
2650
- "trade": 8482,
2651
- "alefsym": 8501,
2652
- "larr": 8592,
2653
- "uarr": 8593,
2654
- "rarr": 8594,
2655
- "darr": 8595,
2656
- "harr": 8596,
2657
- "crarr": 8629,
2658
- "lArr": 8656,
2659
- "uArr": 8657,
2660
- "rArr": 8658,
2661
- "dArr": 8659,
2662
- "hArr": 8660,
2663
- "forall": 8704,
2664
- "part": 8706,
2665
- "exist": 8707,
2666
- "empty": 8709,
2667
- "nabla": 8711,
2668
- "isin": 8712,
2669
- "notin": 8713,
2670
- "ni": 8715,
2671
- "prod": 8719,
2672
- "sum": 8721,
2673
- "minus": 8722,
2674
- "lowast": 8727,
2675
- "radic": 8730,
2676
- "prop": 8733,
2677
- "infin": 8734,
2678
- "ang": 8736,
2679
- "and": 8743,
2680
- "or": 8744,
2681
- "cap": 8745,
2682
- "cup": 8746,
2683
- "int": 8747,
2684
- "there4": 8756,
2685
- "sim": 8764,
2686
- "cong": 8773,
2687
- "asymp": 8776,
2688
- "ne": 8800,
2689
- "equiv": 8801,
2690
- "le": 8804,
2691
- "ge": 8805,
2692
- "sub": 8834,
2693
- "sup": 8835,
2694
- "nsub": 8836,
2695
- "sube": 8838,
2696
- "supe": 8839,
2697
- "oplus": 8853,
2698
- "otimes": 8855,
2699
- "perp": 8869,
2700
- "sdot": 8901,
2701
- "lceil": 8968,
2702
- "rceil": 8969,
2703
- "lfloor": 8970,
2704
- "rfloor": 8971,
2705
- "lang": 9001,
2706
- "rang": 9002,
2707
- "loz": 9674,
2708
- "spades": 9824,
2709
- "clubs": 9827,
2710
- "hearts": 9829,
2711
- "diams": 9830
2712
- };
2713
- Object.keys(sax2.ENTITIES).forEach(function(key) {
2714
- var e = sax2.ENTITIES[key];
2715
- var s2 = typeof e === "number" ? String.fromCharCode(e) : e;
2716
- sax2.ENTITIES[key] = s2;
2717
- });
2718
- for (var s in sax2.STATE) {
2719
- sax2.STATE[sax2.STATE[s]] = s;
2720
- }
2721
- S = sax2.STATE;
2722
- function emit(parser, event, data) {
2723
- parser[event] && parser[event](data);
2724
- }
2725
- function emitNode(parser, nodeType, data) {
2726
- if (parser.textNode) closeText(parser);
2727
- emit(parser, nodeType, data);
2728
- }
2729
- function closeText(parser) {
2730
- parser.textNode = textopts(parser.opt, parser.textNode);
2731
- if (parser.textNode) emit(parser, "ontext", parser.textNode);
2732
- parser.textNode = "";
2733
- }
2734
- function textopts(opt, text) {
2735
- if (opt.trim) text = text.trim();
2736
- if (opt.normalize) text = text.replace(/\s+/g, " ");
2737
- return text;
2738
- }
2739
- function error(parser, er) {
2740
- closeText(parser);
2741
- if (parser.trackPosition) {
2742
- er += "\nLine: " + parser.line + "\nColumn: " + parser.column + "\nChar: " + parser.c;
2743
- }
2744
- er = new Error(er);
2745
- parser.error = er;
2746
- emit(parser, "onerror", er);
2747
- return parser;
2748
- }
2749
- function end(parser) {
2750
- if (parser.sawRoot && !parser.closedRoot) strictFail(parser, "Unclosed root tag");
2751
- if (parser.state !== S.BEGIN && parser.state !== S.BEGIN_WHITESPACE && parser.state !== S.TEXT) {
2752
- error(parser, "Unexpected end");
2753
- }
2754
- closeText(parser);
2755
- parser.c = "";
2756
- parser.closed = true;
2757
- emit(parser, "onend");
2758
- SAXParser.call(parser, parser.strict, parser.opt);
2759
- return parser;
2760
- }
2761
- function strictFail(parser, message) {
2762
- if (typeof parser !== "object" || !(parser instanceof SAXParser)) {
2763
- throw new Error("bad call to strictFail");
2764
- }
2765
- if (parser.strict) {
2766
- error(parser, message);
2767
- }
2768
- }
2769
- function newTag(parser) {
2770
- if (!parser.strict) parser.tagName = parser.tagName[parser.looseCase]();
2771
- var parent = parser.tags[parser.tags.length - 1] || parser;
2772
- var tag = parser.tag = { name: parser.tagName, attributes: {} };
2773
- if (parser.opt.xmlns) {
2774
- tag.ns = parent.ns;
2775
- }
2776
- parser.attribList.length = 0;
2777
- emitNode(parser, "onopentagstart", tag);
2778
- }
2779
- function qname(name, attribute) {
2780
- var i = name.indexOf(":");
2781
- var qualName = i < 0 ? ["", name] : name.split(":");
2782
- var prefix = qualName[0];
2783
- var local = qualName[1];
2784
- if (attribute && name === "xmlns") {
2785
- prefix = "xmlns";
2786
- local = "";
2787
- }
2788
- return { prefix, local };
2789
- }
2790
- function attrib(parser) {
2791
- if (!parser.strict) {
2792
- parser.attribName = parser.attribName[parser.looseCase]();
2793
- }
2794
- if (parser.attribList.indexOf(parser.attribName) !== -1 || parser.tag.attributes.hasOwnProperty(parser.attribName)) {
2795
- parser.attribName = parser.attribValue = "";
2796
- return;
2797
- }
2798
- if (parser.opt.xmlns) {
2799
- var qn = qname(parser.attribName, true);
2800
- var prefix = qn.prefix;
2801
- var local = qn.local;
2802
- if (prefix === "xmlns") {
2803
- if (local === "xml" && parser.attribValue !== XML_NAMESPACE) {
2804
- strictFail(
2805
- parser,
2806
- "xml: prefix must be bound to " + XML_NAMESPACE + "\nActual: " + parser.attribValue
2807
- );
2808
- } else if (local === "xmlns" && parser.attribValue !== XMLNS_NAMESPACE) {
2809
- strictFail(
2810
- parser,
2811
- "xmlns: prefix must be bound to " + XMLNS_NAMESPACE + "\nActual: " + parser.attribValue
2812
- );
2813
- } else {
2814
- var tag = parser.tag;
2815
- var parent = parser.tags[parser.tags.length - 1] || parser;
2816
- if (tag.ns === parent.ns) {
2817
- tag.ns = Object.create(parent.ns);
2818
- }
2819
- tag.ns[local] = parser.attribValue;
2820
- }
2821
- }
2822
- parser.attribList.push([parser.attribName, parser.attribValue]);
2823
- } else {
2824
- parser.tag.attributes[parser.attribName] = parser.attribValue;
2825
- emitNode(parser, "onattribute", {
2826
- name: parser.attribName,
2827
- value: parser.attribValue
2828
- });
2829
- }
2830
- parser.attribName = parser.attribValue = "";
2831
- }
2832
- function openTag(parser, selfClosing) {
2833
- if (parser.opt.xmlns) {
2834
- var tag = parser.tag;
2835
- var qn = qname(parser.tagName);
2836
- tag.prefix = qn.prefix;
2837
- tag.local = qn.local;
2838
- tag.uri = tag.ns[qn.prefix] || "";
2839
- if (tag.prefix && !tag.uri) {
2840
- strictFail(parser, "Unbound namespace prefix: " + JSON.stringify(parser.tagName));
2841
- tag.uri = qn.prefix;
2842
- }
2843
- var parent = parser.tags[parser.tags.length - 1] || parser;
2844
- if (tag.ns && parent.ns !== tag.ns) {
2845
- Object.keys(tag.ns).forEach(function(p) {
2846
- emitNode(parser, "onopennamespace", {
2847
- prefix: p,
2848
- uri: tag.ns[p]
2849
- });
2850
- });
2851
- }
2852
- for (var i = 0, l = parser.attribList.length; i < l; i++) {
2853
- var nv = parser.attribList[i];
2854
- var name = nv[0];
2855
- var value = nv[1];
2856
- var qualName = qname(name, true);
2857
- var prefix = qualName.prefix;
2858
- var local = qualName.local;
2859
- var uri = prefix === "" ? "" : tag.ns[prefix] || "";
2860
- var a = {
2861
- name,
2862
- value,
2863
- prefix,
2864
- local,
2865
- uri
2866
- };
2867
- if (prefix && prefix !== "xmlns" && !uri) {
2868
- strictFail(parser, "Unbound namespace prefix: " + JSON.stringify(prefix));
2869
- a.uri = prefix;
2870
- }
2871
- parser.tag.attributes[name] = a;
2872
- emitNode(parser, "onattribute", a);
2873
- }
2874
- parser.attribList.length = 0;
2875
- }
2876
- parser.tag.isSelfClosing = !!selfClosing;
2877
- parser.sawRoot = true;
2878
- parser.tags.push(parser.tag);
2879
- emitNode(parser, "onopentag", parser.tag);
2880
- if (!selfClosing) {
2881
- if (!parser.noscript && parser.tagName.toLowerCase() === "script") {
2882
- parser.state = S.SCRIPT;
2883
- } else {
2884
- parser.state = S.TEXT;
2885
- }
2886
- parser.tag = null;
2887
- parser.tagName = "";
2888
- }
2889
- parser.attribName = parser.attribValue = "";
2890
- parser.attribList.length = 0;
2891
- }
2892
- function closeTag(parser) {
2893
- if (!parser.tagName) {
2894
- strictFail(parser, "Weird empty close tag.");
2895
- parser.textNode += "</>";
2896
- parser.state = S.TEXT;
2897
- return;
2898
- }
2899
- if (parser.script) {
2900
- if (parser.tagName !== "script") {
2901
- parser.script += "</" + parser.tagName + ">";
2902
- parser.tagName = "";
2903
- parser.state = S.SCRIPT;
2904
- return;
2905
- }
2906
- emitNode(parser, "onscript", parser.script);
2907
- parser.script = "";
2908
- }
2909
- var t = parser.tags.length;
2910
- var tagName = parser.tagName;
2911
- if (!parser.strict) {
2912
- tagName = tagName[parser.looseCase]();
2913
- }
2914
- var closeTo = tagName;
2915
- while (t--) {
2916
- var close = parser.tags[t];
2917
- if (close.name !== closeTo) {
2918
- strictFail(parser, "Unexpected close tag");
2919
- } else {
2920
- break;
2921
- }
2922
- }
2923
- if (t < 0) {
2924
- strictFail(parser, "Unmatched closing tag: " + parser.tagName);
2925
- parser.textNode += "</" + parser.tagName + ">";
2926
- parser.state = S.TEXT;
2927
- return;
2928
- }
2929
- parser.tagName = tagName;
2930
- var s2 = parser.tags.length;
2931
- while (s2-- > t) {
2932
- var tag = parser.tag = parser.tags.pop();
2933
- parser.tagName = parser.tag.name;
2934
- emitNode(parser, "onclosetag", parser.tagName);
2935
- var x = {};
2936
- for (var i in tag.ns) {
2937
- x[i] = tag.ns[i];
2938
- }
2939
- var parent = parser.tags[parser.tags.length - 1] || parser;
2940
- if (parser.opt.xmlns && tag.ns !== parent.ns) {
2941
- Object.keys(tag.ns).forEach(function(p) {
2942
- var n = tag.ns[p];
2943
- emitNode(parser, "onclosenamespace", { prefix: p, uri: n });
2944
- });
2945
- }
2946
- }
2947
- if (t === 0) parser.closedRoot = true;
2948
- parser.tagName = parser.attribValue = parser.attribName = "";
2949
- parser.attribList.length = 0;
2950
- parser.state = S.TEXT;
2951
- }
2952
- function parseEntity(parser) {
2953
- var entity = parser.entity;
2954
- var entityLC = entity.toLowerCase();
2955
- var num;
2956
- var numStr = "";
2957
- if (parser.ENTITIES[entity]) {
2958
- return parser.ENTITIES[entity];
2959
- }
2960
- if (parser.ENTITIES[entityLC]) {
2961
- return parser.ENTITIES[entityLC];
2962
- }
2963
- entity = entityLC;
2964
- if (entity.charAt(0) === "#") {
2965
- if (entity.charAt(1) === "x") {
2966
- entity = entity.slice(2);
2967
- num = parseInt(entity, 16);
2968
- numStr = num.toString(16);
2969
- } else {
2970
- entity = entity.slice(1);
2971
- num = parseInt(entity, 10);
2972
- numStr = num.toString(10);
2973
- }
2974
- }
2975
- entity = entity.replace(/^0+/, "");
2976
- if (isNaN(num) || numStr.toLowerCase() !== entity) {
2977
- strictFail(parser, "Invalid character entity");
2978
- return "&" + parser.entity + ";";
2979
- }
2980
- return String.fromCodePoint(num);
2981
- }
2982
- function beginWhiteSpace(parser, c) {
2983
- if (c === "<") {
2984
- parser.state = S.OPEN_WAKA;
2985
- parser.startTagPosition = parser.position;
2986
- } else if (!isWhitespace(c)) {
2987
- strictFail(parser, "Non-whitespace before first tag.");
2988
- parser.textNode = c;
2989
- parser.state = S.TEXT;
2990
- }
2991
- }
2992
- function charAt(chunk, i) {
2993
- var result = "";
2994
- if (i < chunk.length) {
2995
- result = chunk.charAt(i);
2996
- }
2997
- return result;
2998
- }
2999
- function write(chunk) {
3000
- var parser = this;
3001
- if (this.error) {
3002
- throw this.error;
3003
- }
3004
- if (parser.closed) {
3005
- return error(
3006
- parser,
3007
- "Cannot write after close. Assign an onready handler."
3008
- );
3009
- }
3010
- if (chunk === null) {
3011
- return end(parser);
3012
- }
3013
- if (typeof chunk === "object") {
3014
- chunk = chunk.toString();
3015
- }
3016
- var i = 0;
3017
- var c = "";
3018
- while (true) {
3019
- c = charAt(chunk, i++);
3020
- parser.c = c;
3021
- if (!c) {
3022
- break;
3023
- }
3024
- if (parser.trackPosition) {
3025
- parser.position++;
3026
- if (c === "\n") {
3027
- parser.line++;
3028
- parser.column = 0;
3029
- } else {
3030
- parser.column++;
3031
- }
3032
- }
3033
- switch (parser.state) {
3034
- case S.BEGIN:
3035
- parser.state = S.BEGIN_WHITESPACE;
3036
- if (c === "\uFEFF") {
3037
- continue;
3038
- }
3039
- beginWhiteSpace(parser, c);
3040
- continue;
3041
- case S.BEGIN_WHITESPACE:
3042
- beginWhiteSpace(parser, c);
3043
- continue;
3044
- case S.TEXT:
3045
- if (parser.sawRoot && !parser.closedRoot) {
3046
- var starti = i - 1;
3047
- while (c && c !== "<" && c !== "&") {
3048
- c = charAt(chunk, i++);
3049
- if (c && parser.trackPosition) {
3050
- parser.position++;
3051
- if (c === "\n") {
3052
- parser.line++;
3053
- parser.column = 0;
3054
- } else {
3055
- parser.column++;
3056
- }
3057
- }
3058
- }
3059
- parser.textNode += chunk.substring(starti, i - 1);
3060
- }
3061
- if (c === "<" && !(parser.sawRoot && parser.closedRoot && !parser.strict)) {
3062
- parser.state = S.OPEN_WAKA;
3063
- parser.startTagPosition = parser.position;
3064
- } else {
3065
- if (!isWhitespace(c) && (!parser.sawRoot || parser.closedRoot)) {
3066
- strictFail(parser, "Text data outside of root node.");
3067
- }
3068
- if (c === "&") {
3069
- parser.state = S.TEXT_ENTITY;
3070
- } else {
3071
- parser.textNode += c;
3072
- }
3073
- }
3074
- continue;
3075
- case S.SCRIPT:
3076
- if (c === "<") {
3077
- parser.state = S.SCRIPT_ENDING;
3078
- } else {
3079
- parser.script += c;
3080
- }
3081
- continue;
3082
- case S.SCRIPT_ENDING:
3083
- if (c === "/") {
3084
- parser.state = S.CLOSE_TAG;
3085
- } else {
3086
- parser.script += "<" + c;
3087
- parser.state = S.SCRIPT;
3088
- }
3089
- continue;
3090
- case S.OPEN_WAKA:
3091
- if (c === "!") {
3092
- parser.state = S.SGML_DECL;
3093
- parser.sgmlDecl = "";
3094
- } else if (isWhitespace(c)) ;
3095
- else if (isMatch(nameStart, c)) {
3096
- parser.state = S.OPEN_TAG;
3097
- parser.tagName = c;
3098
- } else if (c === "/") {
3099
- parser.state = S.CLOSE_TAG;
3100
- parser.tagName = "";
3101
- } else if (c === "?") {
3102
- parser.state = S.PROC_INST;
3103
- parser.procInstName = parser.procInstBody = "";
3104
- } else {
3105
- strictFail(parser, "Unencoded <");
3106
- if (parser.startTagPosition + 1 < parser.position) {
3107
- var pad = parser.position - parser.startTagPosition;
3108
- c = new Array(pad).join(" ") + c;
3109
- }
3110
- parser.textNode += "<" + c;
3111
- parser.state = S.TEXT;
3112
- }
3113
- continue;
3114
- case S.SGML_DECL:
3115
- if (parser.sgmlDecl + c === "--") {
3116
- parser.state = S.COMMENT;
3117
- parser.comment = "";
3118
- parser.sgmlDecl = "";
3119
- continue;
3120
- }
3121
- if (parser.doctype && parser.doctype !== true && parser.sgmlDecl) {
3122
- parser.state = S.DOCTYPE_DTD;
3123
- parser.doctype += "<!" + parser.sgmlDecl + c;
3124
- parser.sgmlDecl = "";
3125
- } else if ((parser.sgmlDecl + c).toUpperCase() === CDATA) {
3126
- emitNode(parser, "onopencdata");
3127
- parser.state = S.CDATA;
3128
- parser.sgmlDecl = "";
3129
- parser.cdata = "";
3130
- } else if ((parser.sgmlDecl + c).toUpperCase() === DOCTYPE) {
3131
- parser.state = S.DOCTYPE;
3132
- if (parser.doctype || parser.sawRoot) {
3133
- strictFail(
3134
- parser,
3135
- "Inappropriately located doctype declaration"
3136
- );
3137
- }
3138
- parser.doctype = "";
3139
- parser.sgmlDecl = "";
3140
- } else if (c === ">") {
3141
- emitNode(parser, "onsgmldeclaration", parser.sgmlDecl);
3142
- parser.sgmlDecl = "";
3143
- parser.state = S.TEXT;
3144
- } else if (isQuote(c)) {
3145
- parser.state = S.SGML_DECL_QUOTED;
3146
- parser.sgmlDecl += c;
3147
- } else {
3148
- parser.sgmlDecl += c;
3149
- }
3150
- continue;
3151
- case S.SGML_DECL_QUOTED:
3152
- if (c === parser.q) {
3153
- parser.state = S.SGML_DECL;
3154
- parser.q = "";
3155
- }
3156
- parser.sgmlDecl += c;
3157
- continue;
3158
- case S.DOCTYPE:
3159
- if (c === ">") {
3160
- parser.state = S.TEXT;
3161
- emitNode(parser, "ondoctype", parser.doctype);
3162
- parser.doctype = true;
3163
- } else {
3164
- parser.doctype += c;
3165
- if (c === "[") {
3166
- parser.state = S.DOCTYPE_DTD;
3167
- } else if (isQuote(c)) {
3168
- parser.state = S.DOCTYPE_QUOTED;
3169
- parser.q = c;
3170
- }
3171
- }
3172
- continue;
3173
- case S.DOCTYPE_QUOTED:
3174
- parser.doctype += c;
3175
- if (c === parser.q) {
3176
- parser.q = "";
3177
- parser.state = S.DOCTYPE;
3178
- }
3179
- continue;
3180
- case S.DOCTYPE_DTD:
3181
- if (c === "]") {
3182
- parser.doctype += c;
3183
- parser.state = S.DOCTYPE;
3184
- } else if (c === "<") {
3185
- parser.state = S.OPEN_WAKA;
3186
- parser.startTagPosition = parser.position;
3187
- } else if (isQuote(c)) {
3188
- parser.doctype += c;
3189
- parser.state = S.DOCTYPE_DTD_QUOTED;
3190
- parser.q = c;
3191
- } else {
3192
- parser.doctype += c;
3193
- }
3194
- continue;
3195
- case S.DOCTYPE_DTD_QUOTED:
3196
- parser.doctype += c;
3197
- if (c === parser.q) {
3198
- parser.state = S.DOCTYPE_DTD;
3199
- parser.q = "";
3200
- }
3201
- continue;
3202
- case S.COMMENT:
3203
- if (c === "-") {
3204
- parser.state = S.COMMENT_ENDING;
3205
- } else {
3206
- parser.comment += c;
3207
- }
3208
- continue;
3209
- case S.COMMENT_ENDING:
3210
- if (c === "-") {
3211
- parser.state = S.COMMENT_ENDED;
3212
- parser.comment = textopts(parser.opt, parser.comment);
3213
- if (parser.comment) {
3214
- emitNode(parser, "oncomment", parser.comment);
3215
- }
3216
- parser.comment = "";
3217
- } else {
3218
- parser.comment += "-" + c;
3219
- parser.state = S.COMMENT;
3220
- }
3221
- continue;
3222
- case S.COMMENT_ENDED:
3223
- if (c !== ">") {
3224
- strictFail(parser, "Malformed comment");
3225
- parser.comment += "--" + c;
3226
- parser.state = S.COMMENT;
3227
- } else if (parser.doctype && parser.doctype !== true) {
3228
- parser.state = S.DOCTYPE_DTD;
3229
- } else {
3230
- parser.state = S.TEXT;
3231
- }
3232
- continue;
3233
- case S.CDATA:
3234
- if (c === "]") {
3235
- parser.state = S.CDATA_ENDING;
3236
- } else {
3237
- parser.cdata += c;
3238
- }
3239
- continue;
3240
- case S.CDATA_ENDING:
3241
- if (c === "]") {
3242
- parser.state = S.CDATA_ENDING_2;
3243
- } else {
3244
- parser.cdata += "]" + c;
3245
- parser.state = S.CDATA;
3246
- }
3247
- continue;
3248
- case S.CDATA_ENDING_2:
3249
- if (c === ">") {
3250
- if (parser.cdata) {
3251
- emitNode(parser, "oncdata", parser.cdata);
3252
- }
3253
- emitNode(parser, "onclosecdata");
3254
- parser.cdata = "";
3255
- parser.state = S.TEXT;
3256
- } else if (c === "]") {
3257
- parser.cdata += "]";
3258
- } else {
3259
- parser.cdata += "]]" + c;
3260
- parser.state = S.CDATA;
3261
- }
3262
- continue;
3263
- case S.PROC_INST:
3264
- if (c === "?") {
3265
- parser.state = S.PROC_INST_ENDING;
3266
- } else if (isWhitespace(c)) {
3267
- parser.state = S.PROC_INST_BODY;
3268
- } else {
3269
- parser.procInstName += c;
3270
- }
3271
- continue;
3272
- case S.PROC_INST_BODY:
3273
- if (!parser.procInstBody && isWhitespace(c)) {
3274
- continue;
3275
- } else if (c === "?") {
3276
- parser.state = S.PROC_INST_ENDING;
3277
- } else {
3278
- parser.procInstBody += c;
3279
- }
3280
- continue;
3281
- case S.PROC_INST_ENDING:
3282
- if (c === ">") {
3283
- emitNode(parser, "onprocessinginstruction", {
3284
- name: parser.procInstName,
3285
- body: parser.procInstBody
3286
- });
3287
- parser.procInstName = parser.procInstBody = "";
3288
- parser.state = S.TEXT;
3289
- } else {
3290
- parser.procInstBody += "?" + c;
3291
- parser.state = S.PROC_INST_BODY;
3292
- }
3293
- continue;
3294
- case S.OPEN_TAG:
3295
- if (isMatch(nameBody, c)) {
3296
- parser.tagName += c;
3297
- } else {
3298
- newTag(parser);
3299
- if (c === ">") {
3300
- openTag(parser);
3301
- } else if (c === "/") {
3302
- parser.state = S.OPEN_TAG_SLASH;
3303
- } else {
3304
- if (!isWhitespace(c)) {
3305
- strictFail(parser, "Invalid character in tag name");
3306
- }
3307
- parser.state = S.ATTRIB;
3308
- }
3309
- }
3310
- continue;
3311
- case S.OPEN_TAG_SLASH:
3312
- if (c === ">") {
3313
- openTag(parser, true);
3314
- closeTag(parser);
3315
- } else {
3316
- strictFail(parser, "Forward-slash in opening tag not followed by >");
3317
- parser.state = S.ATTRIB;
3318
- }
3319
- continue;
3320
- case S.ATTRIB:
3321
- if (isWhitespace(c)) {
3322
- continue;
3323
- } else if (c === ">") {
3324
- openTag(parser);
3325
- } else if (c === "/") {
3326
- parser.state = S.OPEN_TAG_SLASH;
3327
- } else if (isMatch(nameStart, c)) {
3328
- parser.attribName = c;
3329
- parser.attribValue = "";
3330
- parser.state = S.ATTRIB_NAME;
3331
- } else {
3332
- strictFail(parser, "Invalid attribute name");
3333
- }
3334
- continue;
3335
- case S.ATTRIB_NAME:
3336
- if (c === "=") {
3337
- parser.state = S.ATTRIB_VALUE;
3338
- } else if (c === ">") {
3339
- strictFail(parser, "Attribute without value");
3340
- parser.attribValue = parser.attribName;
3341
- attrib(parser);
3342
- openTag(parser);
3343
- } else if (isWhitespace(c)) {
3344
- parser.state = S.ATTRIB_NAME_SAW_WHITE;
3345
- } else if (isMatch(nameBody, c)) {
3346
- parser.attribName += c;
3347
- } else {
3348
- strictFail(parser, "Invalid attribute name");
3349
- }
3350
- continue;
3351
- case S.ATTRIB_NAME_SAW_WHITE:
3352
- if (c === "=") {
3353
- parser.state = S.ATTRIB_VALUE;
3354
- } else if (isWhitespace(c)) {
3355
- continue;
3356
- } else {
3357
- strictFail(parser, "Attribute without value");
3358
- parser.tag.attributes[parser.attribName] = "";
3359
- parser.attribValue = "";
3360
- emitNode(parser, "onattribute", {
3361
- name: parser.attribName,
3362
- value: ""
3363
- });
3364
- parser.attribName = "";
3365
- if (c === ">") {
3366
- openTag(parser);
3367
- } else if (isMatch(nameStart, c)) {
3368
- parser.attribName = c;
3369
- parser.state = S.ATTRIB_NAME;
3370
- } else {
3371
- strictFail(parser, "Invalid attribute name");
3372
- parser.state = S.ATTRIB;
3373
- }
3374
- }
3375
- continue;
3376
- case S.ATTRIB_VALUE:
3377
- if (isWhitespace(c)) {
3378
- continue;
3379
- } else if (isQuote(c)) {
3380
- parser.q = c;
3381
- parser.state = S.ATTRIB_VALUE_QUOTED;
3382
- } else {
3383
- if (!parser.opt.unquotedAttributeValues) {
3384
- error(parser, "Unquoted attribute value");
3385
- }
3386
- parser.state = S.ATTRIB_VALUE_UNQUOTED;
3387
- parser.attribValue = c;
3388
- }
3389
- continue;
3390
- case S.ATTRIB_VALUE_QUOTED:
3391
- if (c !== parser.q) {
3392
- if (c === "&") {
3393
- parser.state = S.ATTRIB_VALUE_ENTITY_Q;
3394
- } else {
3395
- parser.attribValue += c;
3396
- }
3397
- continue;
3398
- }
3399
- attrib(parser);
3400
- parser.q = "";
3401
- parser.state = S.ATTRIB_VALUE_CLOSED;
3402
- continue;
3403
- case S.ATTRIB_VALUE_CLOSED:
3404
- if (isWhitespace(c)) {
3405
- parser.state = S.ATTRIB;
3406
- } else if (c === ">") {
3407
- openTag(parser);
3408
- } else if (c === "/") {
3409
- parser.state = S.OPEN_TAG_SLASH;
3410
- } else if (isMatch(nameStart, c)) {
3411
- strictFail(parser, "No whitespace between attributes");
3412
- parser.attribName = c;
3413
- parser.attribValue = "";
3414
- parser.state = S.ATTRIB_NAME;
3415
- } else {
3416
- strictFail(parser, "Invalid attribute name");
3417
- }
3418
- continue;
3419
- case S.ATTRIB_VALUE_UNQUOTED:
3420
- if (!isAttribEnd(c)) {
3421
- if (c === "&") {
3422
- parser.state = S.ATTRIB_VALUE_ENTITY_U;
3423
- } else {
3424
- parser.attribValue += c;
3425
- }
3426
- continue;
3427
- }
3428
- attrib(parser);
3429
- if (c === ">") {
3430
- openTag(parser);
3431
- } else {
3432
- parser.state = S.ATTRIB;
3433
- }
3434
- continue;
3435
- case S.CLOSE_TAG:
3436
- if (!parser.tagName) {
3437
- if (isWhitespace(c)) {
3438
- continue;
3439
- } else if (notMatch(nameStart, c)) {
3440
- if (parser.script) {
3441
- parser.script += "</" + c;
3442
- parser.state = S.SCRIPT;
3443
- } else {
3444
- strictFail(parser, "Invalid tagname in closing tag.");
3445
- }
3446
- } else {
3447
- parser.tagName = c;
3448
- }
3449
- } else if (c === ">") {
3450
- closeTag(parser);
3451
- } else if (isMatch(nameBody, c)) {
3452
- parser.tagName += c;
3453
- } else if (parser.script) {
3454
- parser.script += "</" + parser.tagName;
3455
- parser.tagName = "";
3456
- parser.state = S.SCRIPT;
3457
- } else {
3458
- if (!isWhitespace(c)) {
3459
- strictFail(parser, "Invalid tagname in closing tag");
3460
- }
3461
- parser.state = S.CLOSE_TAG_SAW_WHITE;
3462
- }
3463
- continue;
3464
- case S.CLOSE_TAG_SAW_WHITE:
3465
- if (isWhitespace(c)) {
3466
- continue;
3467
- }
3468
- if (c === ">") {
3469
- closeTag(parser);
3470
- } else {
3471
- strictFail(parser, "Invalid characters in closing tag");
3472
- }
3473
- continue;
3474
- case S.TEXT_ENTITY:
3475
- case S.ATTRIB_VALUE_ENTITY_Q:
3476
- case S.ATTRIB_VALUE_ENTITY_U:
3477
- var returnState;
3478
- var buffer2;
3479
- switch (parser.state) {
3480
- case S.TEXT_ENTITY:
3481
- returnState = S.TEXT;
3482
- buffer2 = "textNode";
3483
- break;
3484
- case S.ATTRIB_VALUE_ENTITY_Q:
3485
- returnState = S.ATTRIB_VALUE_QUOTED;
3486
- buffer2 = "attribValue";
3487
- break;
3488
- case S.ATTRIB_VALUE_ENTITY_U:
3489
- returnState = S.ATTRIB_VALUE_UNQUOTED;
3490
- buffer2 = "attribValue";
3491
- break;
3492
- }
3493
- if (c === ";") {
3494
- var parsedEntity = parseEntity(parser);
3495
- if (parser.opt.unparsedEntities && !Object.values(sax2.XML_ENTITIES).includes(parsedEntity)) {
3496
- parser.entity = "";
3497
- parser.state = returnState;
3498
- parser.write(parsedEntity);
3499
- } else {
3500
- parser[buffer2] += parsedEntity;
3501
- parser.entity = "";
3502
- parser.state = returnState;
3503
- }
3504
- } else if (isMatch(parser.entity.length ? entityBody : entityStart, c)) {
3505
- parser.entity += c;
3506
- } else {
3507
- strictFail(parser, "Invalid character in entity name");
3508
- parser[buffer2] += "&" + parser.entity + c;
3509
- parser.entity = "";
3510
- parser.state = returnState;
3511
- }
3512
- continue;
3513
- default: {
3514
- throw new Error(parser, "Unknown state: " + parser.state);
3515
- }
3516
- }
3517
- }
3518
- if (parser.position >= parser.bufferCheckPosition) {
3519
- checkBufferLength(parser);
3520
- }
3521
- return parser;
3522
- }
3523
- if (!String.fromCodePoint) {
3524
- (function() {
3525
- var stringFromCharCode = String.fromCharCode;
3526
- var floor = Math.floor;
3527
- var fromCodePoint = function() {
3528
- var MAX_SIZE = 16384;
3529
- var codeUnits = [];
3530
- var highSurrogate;
3531
- var lowSurrogate;
3532
- var index = -1;
3533
- var length = arguments.length;
3534
- if (!length) {
3535
- return "";
3536
- }
3537
- var result = "";
3538
- while (++index < length) {
3539
- var codePoint = Number(arguments[index]);
3540
- if (!isFinite(codePoint) || // `NaN`, `+Infinity`, or `-Infinity`
3541
- codePoint < 0 || // not a valid Unicode code point
3542
- codePoint > 1114111 || // not a valid Unicode code point
3543
- floor(codePoint) !== codePoint) {
3544
- throw RangeError("Invalid code point: " + codePoint);
3545
- }
3546
- if (codePoint <= 65535) {
3547
- codeUnits.push(codePoint);
3548
- } else {
3549
- codePoint -= 65536;
3550
- highSurrogate = (codePoint >> 10) + 55296;
3551
- lowSurrogate = codePoint % 1024 + 56320;
3552
- codeUnits.push(highSurrogate, lowSurrogate);
3553
- }
3554
- if (index + 1 === length || codeUnits.length > MAX_SIZE) {
3555
- result += stringFromCharCode.apply(null, codeUnits);
3556
- codeUnits.length = 0;
3557
- }
3558
- }
3559
- return result;
3560
- };
3561
- if (Object.defineProperty) {
3562
- Object.defineProperty(String, "fromCodePoint", {
3563
- value: fromCodePoint,
3564
- configurable: true,
3565
- writable: true
3566
- });
3567
- } else {
3568
- String.fromCodePoint = fromCodePoint;
3569
- }
3570
- })();
3571
- }
3572
- })(exports);
3573
- })(sax);
3574
- return sax;
3575
- }
3576
- var arrayHelper;
3577
- var hasRequiredArrayHelper;
3578
- function requireArrayHelper() {
3579
- if (hasRequiredArrayHelper) return arrayHelper;
3580
- hasRequiredArrayHelper = 1;
3581
- arrayHelper = {
3582
- isArray: function(value) {
3583
- if (Array.isArray) {
3584
- return Array.isArray(value);
3585
- }
3586
- return Object.prototype.toString.call(value) === "[object Array]";
3587
- }
3588
- };
3589
- return arrayHelper;
3590
- }
3591
- var optionsHelper;
3592
- var hasRequiredOptionsHelper;
3593
- function requireOptionsHelper() {
3594
- if (hasRequiredOptionsHelper) return optionsHelper;
3595
- hasRequiredOptionsHelper = 1;
3596
- var isArray = requireArrayHelper().isArray;
3597
- optionsHelper = {
3598
- copyOptions: function(options) {
3599
- var key, copy = {};
3600
- for (key in options) {
3601
- if (options.hasOwnProperty(key)) {
3602
- copy[key] = options[key];
3603
- }
3604
- }
3605
- return copy;
3606
- },
3607
- ensureFlagExists: function(item, options) {
3608
- if (!(item in options) || typeof options[item] !== "boolean") {
3609
- options[item] = false;
3610
- }
3611
- },
3612
- ensureSpacesExists: function(options) {
3613
- if (!("spaces" in options) || typeof options.spaces !== "number" && typeof options.spaces !== "string") {
3614
- options.spaces = 0;
3615
- }
3616
- },
3617
- ensureAlwaysArrayExists: function(options) {
3618
- if (!("alwaysArray" in options) || typeof options.alwaysArray !== "boolean" && !isArray(options.alwaysArray)) {
3619
- options.alwaysArray = false;
3620
- }
3621
- },
3622
- ensureKeyExists: function(key, options) {
3623
- if (!(key + "Key" in options) || typeof options[key + "Key"] !== "string") {
3624
- options[key + "Key"] = options.compact ? "_" + key : key;
3625
- }
3626
- },
3627
- checkFnExists: function(key, options) {
3628
- return key + "Fn" in options;
3629
- }
3630
- };
3631
- return optionsHelper;
3632
- }
3633
- var xml2js;
3634
- var hasRequiredXml2js;
3635
- function requireXml2js() {
3636
- if (hasRequiredXml2js) return xml2js;
3637
- hasRequiredXml2js = 1;
3638
- var sax2 = requireSax();
3639
- var helper = requireOptionsHelper();
3640
- var isArray = requireArrayHelper().isArray;
3641
- var options;
3642
- var currentElement;
3643
- function validateOptions(userOptions) {
3644
- options = helper.copyOptions(userOptions);
3645
- helper.ensureFlagExists("ignoreDeclaration", options);
3646
- helper.ensureFlagExists("ignoreInstruction", options);
3647
- helper.ensureFlagExists("ignoreAttributes", options);
3648
- helper.ensureFlagExists("ignoreText", options);
3649
- helper.ensureFlagExists("ignoreComment", options);
3650
- helper.ensureFlagExists("ignoreCdata", options);
3651
- helper.ensureFlagExists("ignoreDoctype", options);
3652
- helper.ensureFlagExists("compact", options);
3653
- helper.ensureFlagExists("alwaysChildren", options);
3654
- helper.ensureFlagExists("addParent", options);
3655
- helper.ensureFlagExists("trim", options);
3656
- helper.ensureFlagExists("nativeType", options);
3657
- helper.ensureFlagExists("nativeTypeAttributes", options);
3658
- helper.ensureFlagExists("sanitize", options);
3659
- helper.ensureFlagExists("instructionHasAttributes", options);
3660
- helper.ensureFlagExists("captureSpacesBetweenElements", options);
3661
- helper.ensureAlwaysArrayExists(options);
3662
- helper.ensureKeyExists("declaration", options);
3663
- helper.ensureKeyExists("instruction", options);
3664
- helper.ensureKeyExists("attributes", options);
3665
- helper.ensureKeyExists("text", options);
3666
- helper.ensureKeyExists("comment", options);
3667
- helper.ensureKeyExists("cdata", options);
3668
- helper.ensureKeyExists("doctype", options);
3669
- helper.ensureKeyExists("type", options);
3670
- helper.ensureKeyExists("name", options);
3671
- helper.ensureKeyExists("elements", options);
3672
- helper.ensureKeyExists("parent", options);
3673
- helper.checkFnExists("doctype", options);
3674
- helper.checkFnExists("instruction", options);
3675
- helper.checkFnExists("cdata", options);
3676
- helper.checkFnExists("comment", options);
3677
- helper.checkFnExists("text", options);
3678
- helper.checkFnExists("instructionName", options);
3679
- helper.checkFnExists("elementName", options);
3680
- helper.checkFnExists("attributeName", options);
3681
- helper.checkFnExists("attributeValue", options);
3682
- helper.checkFnExists("attributes", options);
3683
- return options;
3684
- }
3685
- function nativeType(value) {
3686
- var nValue = Number(value);
3687
- if (!isNaN(nValue)) {
3688
- return nValue;
3689
- }
3690
- var bValue = value.toLowerCase();
3691
- if (bValue === "true") {
3692
- return true;
3693
- } else if (bValue === "false") {
3694
- return false;
3695
- }
3696
- return value;
3697
- }
3698
- function addField(type, value) {
3699
- var key;
3700
- if (options.compact) {
3701
- if (!currentElement[options[type + "Key"]] && (isArray(options.alwaysArray) ? options.alwaysArray.indexOf(options[type + "Key"]) !== -1 : options.alwaysArray)) {
3702
- currentElement[options[type + "Key"]] = [];
3703
- }
3704
- if (currentElement[options[type + "Key"]] && !isArray(currentElement[options[type + "Key"]])) {
3705
- currentElement[options[type + "Key"]] = [currentElement[options[type + "Key"]]];
3706
- }
3707
- if (type + "Fn" in options && typeof value === "string") {
3708
- value = options[type + "Fn"](value, currentElement);
3709
- }
3710
- if (type === "instruction" && ("instructionFn" in options || "instructionNameFn" in options)) {
3711
- for (key in value) {
3712
- if (value.hasOwnProperty(key)) {
3713
- if ("instructionFn" in options) {
3714
- value[key] = options.instructionFn(value[key], key, currentElement);
3715
- } else {
3716
- var temp = value[key];
3717
- delete value[key];
3718
- value[options.instructionNameFn(key, temp, currentElement)] = temp;
3719
- }
3720
- }
3721
- }
3722
- }
3723
- if (isArray(currentElement[options[type + "Key"]])) {
3724
- currentElement[options[type + "Key"]].push(value);
3725
- } else {
3726
- currentElement[options[type + "Key"]] = value;
3727
- }
3728
- } else {
3729
- if (!currentElement[options.elementsKey]) {
3730
- currentElement[options.elementsKey] = [];
3731
- }
3732
- var element = {};
3733
- element[options.typeKey] = type;
3734
- if (type === "instruction") {
3735
- for (key in value) {
3736
- if (value.hasOwnProperty(key)) {
3737
- break;
3738
- }
3739
- }
3740
- element[options.nameKey] = "instructionNameFn" in options ? options.instructionNameFn(key, value, currentElement) : key;
3741
- if (options.instructionHasAttributes) {
3742
- element[options.attributesKey] = value[key][options.attributesKey];
3743
- if ("instructionFn" in options) {
3744
- element[options.attributesKey] = options.instructionFn(element[options.attributesKey], key, currentElement);
3745
- }
3746
- } else {
3747
- if ("instructionFn" in options) {
3748
- value[key] = options.instructionFn(value[key], key, currentElement);
3749
- }
3750
- element[options.instructionKey] = value[key];
3751
- }
3752
- } else {
3753
- if (type + "Fn" in options) {
3754
- value = options[type + "Fn"](value, currentElement);
3755
- }
3756
- element[options[type + "Key"]] = value;
3757
- }
3758
- if (options.addParent) {
3759
- element[options.parentKey] = currentElement;
3760
- }
3761
- currentElement[options.elementsKey].push(element);
3762
- }
3763
- }
3764
- function manipulateAttributes(attributes) {
3765
- if ("attributesFn" in options && attributes) {
3766
- attributes = options.attributesFn(attributes, currentElement);
3767
- }
3768
- if ((options.trim || "attributeValueFn" in options || "attributeNameFn" in options || options.nativeTypeAttributes) && attributes) {
3769
- var key;
3770
- for (key in attributes) {
3771
- if (attributes.hasOwnProperty(key)) {
3772
- if (options.trim) attributes[key] = attributes[key].trim();
3773
- if (options.nativeTypeAttributes) {
3774
- attributes[key] = nativeType(attributes[key]);
3775
- }
3776
- if ("attributeValueFn" in options) attributes[key] = options.attributeValueFn(attributes[key], key, currentElement);
3777
- if ("attributeNameFn" in options) {
3778
- var temp = attributes[key];
3779
- delete attributes[key];
3780
- attributes[options.attributeNameFn(key, attributes[key], currentElement)] = temp;
3781
- }
3782
- }
3783
- }
3784
- }
3785
- return attributes;
3786
- }
3787
- function onInstruction(instruction) {
3788
- var attributes = {};
3789
- if (instruction.body && (instruction.name.toLowerCase() === "xml" || options.instructionHasAttributes)) {
3790
- var attrsRegExp = /([\w:-]+)\s*=\s*(?:"([^"]*)"|'([^']*)'|(\w+))\s*/g;
3791
- var match;
3792
- while ((match = attrsRegExp.exec(instruction.body)) !== null) {
3793
- attributes[match[1]] = match[2] || match[3] || match[4];
3794
- }
3795
- attributes = manipulateAttributes(attributes);
3796
- }
3797
- if (instruction.name.toLowerCase() === "xml") {
3798
- if (options.ignoreDeclaration) {
3799
- return;
3800
- }
3801
- currentElement[options.declarationKey] = {};
3802
- if (Object.keys(attributes).length) {
3803
- currentElement[options.declarationKey][options.attributesKey] = attributes;
3804
- }
3805
- if (options.addParent) {
3806
- currentElement[options.declarationKey][options.parentKey] = currentElement;
3807
- }
3808
- } else {
3809
- if (options.ignoreInstruction) {
3810
- return;
3811
- }
3812
- if (options.trim) {
3813
- instruction.body = instruction.body.trim();
3814
- }
3815
- var value = {};
3816
- if (options.instructionHasAttributes && Object.keys(attributes).length) {
3817
- value[instruction.name] = {};
3818
- value[instruction.name][options.attributesKey] = attributes;
3819
- } else {
3820
- value[instruction.name] = instruction.body;
3821
- }
3822
- addField("instruction", value);
3823
- }
3824
- }
3825
- function onStartElement(name, attributes) {
3826
- var element;
3827
- if (typeof name === "object") {
3828
- attributes = name.attributes;
3829
- name = name.name;
3830
- }
3831
- attributes = manipulateAttributes(attributes);
3832
- if ("elementNameFn" in options) {
3833
- name = options.elementNameFn(name, currentElement);
3834
- }
3835
- if (options.compact) {
3836
- element = {};
3837
- if (!options.ignoreAttributes && attributes && Object.keys(attributes).length) {
3838
- element[options.attributesKey] = {};
3839
- var key;
3840
- for (key in attributes) {
3841
- if (attributes.hasOwnProperty(key)) {
3842
- element[options.attributesKey][key] = attributes[key];
3843
- }
3844
- }
3845
- }
3846
- if (!(name in currentElement) && (isArray(options.alwaysArray) ? options.alwaysArray.indexOf(name) !== -1 : options.alwaysArray)) {
3847
- currentElement[name] = [];
3848
- }
3849
- if (currentElement[name] && !isArray(currentElement[name])) {
3850
- currentElement[name] = [currentElement[name]];
3851
- }
3852
- if (isArray(currentElement[name])) {
3853
- currentElement[name].push(element);
3854
- } else {
3855
- currentElement[name] = element;
3856
- }
3857
- } else {
3858
- if (!currentElement[options.elementsKey]) {
3859
- currentElement[options.elementsKey] = [];
3860
- }
3861
- element = {};
3862
- element[options.typeKey] = "element";
3863
- element[options.nameKey] = name;
3864
- if (!options.ignoreAttributes && attributes && Object.keys(attributes).length) {
3865
- element[options.attributesKey] = attributes;
3866
- }
3867
- if (options.alwaysChildren) {
3868
- element[options.elementsKey] = [];
3869
- }
3870
- currentElement[options.elementsKey].push(element);
3871
- }
3872
- element[options.parentKey] = currentElement;
3873
- currentElement = element;
3874
- }
3875
- function onText(text) {
3876
- if (options.ignoreText) {
3877
- return;
3878
- }
3879
- if (!text.trim() && !options.captureSpacesBetweenElements) {
3880
- return;
3881
- }
3882
- if (options.trim) {
3883
- text = text.trim();
3884
- }
3885
- if (options.nativeType) {
3886
- text = nativeType(text);
3887
- }
3888
- if (options.sanitize) {
3889
- text = text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
3890
- }
3891
- addField("text", text);
3892
- }
3893
- function onComment(comment) {
3894
- if (options.ignoreComment) {
3895
- return;
3896
- }
3897
- if (options.trim) {
3898
- comment = comment.trim();
3899
- }
3900
- addField("comment", comment);
3901
- }
3902
- function onEndElement(name) {
3903
- var parentElement = currentElement[options.parentKey];
3904
- if (!options.addParent) {
3905
- delete currentElement[options.parentKey];
3906
- }
3907
- currentElement = parentElement;
3908
- }
3909
- function onCdata(cdata) {
3910
- if (options.ignoreCdata) {
3911
- return;
3912
- }
3913
- if (options.trim) {
3914
- cdata = cdata.trim();
3915
- }
3916
- addField("cdata", cdata);
3917
- }
3918
- function onDoctype(doctype) {
3919
- if (options.ignoreDoctype) {
3920
- return;
3921
- }
3922
- doctype = doctype.replace(/^ /, "");
3923
- if (options.trim) {
3924
- doctype = doctype.trim();
3925
- }
3926
- addField("doctype", doctype);
3927
- }
3928
- function onError(error) {
3929
- error.note = error;
3930
- }
3931
- xml2js = function(xml, userOptions) {
3932
- var parser = sax2.parser(true, {});
3933
- var result = {};
3934
- currentElement = result;
3935
- options = validateOptions(userOptions);
3936
- {
3937
- parser.opt = { strictEntities: true };
3938
- parser.onopentag = onStartElement;
3939
- parser.ontext = onText;
3940
- parser.oncomment = onComment;
3941
- parser.onclosetag = onEndElement;
3942
- parser.onerror = onError;
3943
- parser.oncdata = onCdata;
3944
- parser.ondoctype = onDoctype;
3945
- parser.onprocessinginstruction = onInstruction;
3946
- }
3947
- {
3948
- parser.write(xml).close();
3949
- }
3950
- if (result[options.elementsKey]) {
3951
- var temp = result[options.elementsKey];
3952
- delete result[options.elementsKey];
3953
- result[options.elementsKey] = temp;
3954
- delete result.text;
3955
- }
3956
- return result;
3957
- };
3958
- return xml2js;
3959
- }
3960
- var xml2json;
3961
- var hasRequiredXml2json;
3962
- function requireXml2json() {
3963
- if (hasRequiredXml2json) return xml2json;
3964
- hasRequiredXml2json = 1;
3965
- var helper = requireOptionsHelper();
3966
- var xml2js2 = requireXml2js();
3967
- function validateOptions(userOptions) {
3968
- var options = helper.copyOptions(userOptions);
3969
- helper.ensureSpacesExists(options);
3970
- return options;
3971
- }
3972
- xml2json = function(xml, userOptions) {
3973
- var options, js, json, parentKey;
3974
- options = validateOptions(userOptions);
3975
- js = xml2js2(xml, options);
3976
- parentKey = "compact" in options && options.compact ? "_parent" : "parent";
3977
- if ("addParent" in options && options.addParent) {
3978
- json = JSON.stringify(js, function(k, v) {
3979
- return k === parentKey ? "_" : v;
3980
- }, options.spaces);
3981
- } else {
3982
- json = JSON.stringify(js, null, options.spaces);
3983
- }
3984
- return json.replace(/\u2028/g, "\\u2028").replace(/\u2029/g, "\\u2029");
3985
- };
3986
- return xml2json;
3987
- }
3988
- var js2xml;
3989
- var hasRequiredJs2xml;
3990
- function requireJs2xml() {
3991
- if (hasRequiredJs2xml) return js2xml;
3992
- hasRequiredJs2xml = 1;
3993
- var helper = requireOptionsHelper();
3994
- var isArray = requireArrayHelper().isArray;
3995
- var currentElement, currentElementName;
3996
- function validateOptions(userOptions) {
3997
- var options = helper.copyOptions(userOptions);
3998
- helper.ensureFlagExists("ignoreDeclaration", options);
3999
- helper.ensureFlagExists("ignoreInstruction", options);
4000
- helper.ensureFlagExists("ignoreAttributes", options);
4001
- helper.ensureFlagExists("ignoreText", options);
4002
- helper.ensureFlagExists("ignoreComment", options);
4003
- helper.ensureFlagExists("ignoreCdata", options);
4004
- helper.ensureFlagExists("ignoreDoctype", options);
4005
- helper.ensureFlagExists("compact", options);
4006
- helper.ensureFlagExists("indentText", options);
4007
- helper.ensureFlagExists("indentCdata", options);
4008
- helper.ensureFlagExists("indentAttributes", options);
4009
- helper.ensureFlagExists("indentInstruction", options);
4010
- helper.ensureFlagExists("fullTagEmptyElement", options);
4011
- helper.ensureFlagExists("noQuotesForNativeAttributes", options);
4012
- helper.ensureSpacesExists(options);
4013
- if (typeof options.spaces === "number") {
4014
- options.spaces = Array(options.spaces + 1).join(" ");
4015
- }
4016
- helper.ensureKeyExists("declaration", options);
4017
- helper.ensureKeyExists("instruction", options);
4018
- helper.ensureKeyExists("attributes", options);
4019
- helper.ensureKeyExists("text", options);
4020
- helper.ensureKeyExists("comment", options);
4021
- helper.ensureKeyExists("cdata", options);
4022
- helper.ensureKeyExists("doctype", options);
4023
- helper.ensureKeyExists("type", options);
4024
- helper.ensureKeyExists("name", options);
4025
- helper.ensureKeyExists("elements", options);
4026
- helper.checkFnExists("doctype", options);
4027
- helper.checkFnExists("instruction", options);
4028
- helper.checkFnExists("cdata", options);
4029
- helper.checkFnExists("comment", options);
4030
- helper.checkFnExists("text", options);
4031
- helper.checkFnExists("instructionName", options);
4032
- helper.checkFnExists("elementName", options);
4033
- helper.checkFnExists("attributeName", options);
4034
- helper.checkFnExists("attributeValue", options);
4035
- helper.checkFnExists("attributes", options);
4036
- helper.checkFnExists("fullTagEmptyElement", options);
4037
- return options;
4038
- }
4039
- function writeIndentation(options, depth, firstLine) {
4040
- return (!firstLine && options.spaces ? "\n" : "") + Array(depth + 1).join(options.spaces);
4041
- }
4042
- function writeAttributes(attributes, options, depth) {
4043
- if (options.ignoreAttributes) {
4044
- return "";
4045
- }
4046
- if ("attributesFn" in options) {
4047
- attributes = options.attributesFn(attributes, currentElementName, currentElement);
4048
- }
4049
- var key, attr, attrName, quote, result = [];
4050
- for (key in attributes) {
4051
- if (attributes.hasOwnProperty(key) && attributes[key] !== null && attributes[key] !== void 0) {
4052
- quote = options.noQuotesForNativeAttributes && typeof attributes[key] !== "string" ? "" : '"';
4053
- attr = "" + attributes[key];
4054
- attr = attr.replace(/"/g, "&quot;");
4055
- attrName = "attributeNameFn" in options ? options.attributeNameFn(key, attr, currentElementName, currentElement) : key;
4056
- result.push(options.spaces && options.indentAttributes ? writeIndentation(options, depth + 1, false) : " ");
4057
- result.push(attrName + "=" + quote + ("attributeValueFn" in options ? options.attributeValueFn(attr, key, currentElementName, currentElement) : attr) + quote);
4058
- }
4059
- }
4060
- if (attributes && Object.keys(attributes).length && options.spaces && options.indentAttributes) {
4061
- result.push(writeIndentation(options, depth, false));
4062
- }
4063
- return result.join("");
4064
- }
4065
- function writeDeclaration(declaration, options, depth) {
4066
- currentElement = declaration;
4067
- currentElementName = "xml";
4068
- return options.ignoreDeclaration ? "" : "<?xml" + writeAttributes(declaration[options.attributesKey], options, depth) + "?>";
4069
- }
4070
- function writeInstruction(instruction, options, depth) {
4071
- if (options.ignoreInstruction) {
4072
- return "";
4073
- }
4074
- var key;
4075
- for (key in instruction) {
4076
- if (instruction.hasOwnProperty(key)) {
4077
- break;
4078
- }
4079
- }
4080
- var instructionName = "instructionNameFn" in options ? options.instructionNameFn(key, instruction[key], currentElementName, currentElement) : key;
4081
- if (typeof instruction[key] === "object") {
4082
- currentElement = instruction;
4083
- currentElementName = instructionName;
4084
- return "<?" + instructionName + writeAttributes(instruction[key][options.attributesKey], options, depth) + "?>";
4085
- } else {
4086
- var instructionValue = instruction[key] ? instruction[key] : "";
4087
- if ("instructionFn" in options) instructionValue = options.instructionFn(instructionValue, key, currentElementName, currentElement);
4088
- return "<?" + instructionName + (instructionValue ? " " + instructionValue : "") + "?>";
4089
- }
4090
- }
4091
- function writeComment(comment, options) {
4092
- return options.ignoreComment ? "" : "<!--" + ("commentFn" in options ? options.commentFn(comment, currentElementName, currentElement) : comment) + "-->";
4093
- }
4094
- function writeCdata(cdata, options) {
4095
- return options.ignoreCdata ? "" : "<![CDATA[" + ("cdataFn" in options ? options.cdataFn(cdata, currentElementName, currentElement) : cdata.replace("]]>", "]]]]><![CDATA[>")) + "]]>";
4096
- }
4097
- function writeDoctype(doctype, options) {
4098
- return options.ignoreDoctype ? "" : "<!DOCTYPE " + ("doctypeFn" in options ? options.doctypeFn(doctype, currentElementName, currentElement) : doctype) + ">";
4099
- }
4100
- function writeText(text, options) {
4101
- if (options.ignoreText) return "";
4102
- text = "" + text;
4103
- text = text.replace(/&amp;/g, "&");
4104
- text = text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
4105
- return "textFn" in options ? options.textFn(text, currentElementName, currentElement) : text;
4106
- }
4107
- function hasContent(element, options) {
4108
- var i;
4109
- if (element.elements && element.elements.length) {
4110
- for (i = 0; i < element.elements.length; ++i) {
4111
- switch (element.elements[i][options.typeKey]) {
4112
- case "text":
4113
- if (options.indentText) {
4114
- return true;
4115
- }
4116
- break;
4117
- // skip to next key
4118
- case "cdata":
4119
- if (options.indentCdata) {
4120
- return true;
4121
- }
4122
- break;
4123
- // skip to next key
4124
- case "instruction":
4125
- if (options.indentInstruction) {
4126
- return true;
4127
- }
4128
- break;
4129
- // skip to next key
4130
- case "doctype":
4131
- case "comment":
4132
- case "element":
4133
- return true;
4134
- default:
4135
- return true;
4136
- }
4137
- }
4138
- }
4139
- return false;
4140
- }
4141
- function writeElement(element, options, depth) {
4142
- currentElement = element;
4143
- currentElementName = element.name;
4144
- var xml = [], elementName = "elementNameFn" in options ? options.elementNameFn(element.name, element) : element.name;
4145
- xml.push("<" + elementName);
4146
- if (element[options.attributesKey]) {
4147
- xml.push(writeAttributes(element[options.attributesKey], options, depth));
4148
- }
4149
- var withClosingTag = element[options.elementsKey] && element[options.elementsKey].length || element[options.attributesKey] && element[options.attributesKey]["xml:space"] === "preserve";
4150
- if (!withClosingTag) {
4151
- if ("fullTagEmptyElementFn" in options) {
4152
- withClosingTag = options.fullTagEmptyElementFn(element.name, element);
4153
- } else {
4154
- withClosingTag = options.fullTagEmptyElement;
4155
- }
4156
- }
4157
- if (withClosingTag) {
4158
- xml.push(">");
4159
- if (element[options.elementsKey] && element[options.elementsKey].length) {
4160
- xml.push(writeElements(element[options.elementsKey], options, depth + 1));
4161
- currentElement = element;
4162
- currentElementName = element.name;
4163
- }
4164
- xml.push(options.spaces && hasContent(element, options) ? "\n" + Array(depth + 1).join(options.spaces) : "");
4165
- xml.push("</" + elementName + ">");
4166
- } else {
4167
- xml.push("/>");
4168
- }
4169
- return xml.join("");
4170
- }
4171
- function writeElements(elements, options, depth, firstLine) {
4172
- return elements.reduce(function(xml, element) {
4173
- var indent = writeIndentation(options, depth, firstLine && !xml);
4174
- switch (element.type) {
4175
- case "element":
4176
- return xml + indent + writeElement(element, options, depth);
4177
- case "comment":
4178
- return xml + indent + writeComment(element[options.commentKey], options);
4179
- case "doctype":
4180
- return xml + indent + writeDoctype(element[options.doctypeKey], options);
4181
- case "cdata":
4182
- return xml + (options.indentCdata ? indent : "") + writeCdata(element[options.cdataKey], options);
4183
- case "text":
4184
- return xml + (options.indentText ? indent : "") + writeText(element[options.textKey], options);
4185
- case "instruction":
4186
- var instruction = {};
4187
- instruction[element[options.nameKey]] = element[options.attributesKey] ? element : element[options.instructionKey];
4188
- return xml + (options.indentInstruction ? indent : "") + writeInstruction(instruction, options, depth);
4189
- }
4190
- }, "");
4191
- }
4192
- function hasContentCompact(element, options, anyContent) {
4193
- var key;
4194
- for (key in element) {
4195
- if (element.hasOwnProperty(key)) {
4196
- switch (key) {
4197
- case options.parentKey:
4198
- case options.attributesKey:
4199
- break;
4200
- // skip to next key
4201
- case options.textKey:
4202
- if (options.indentText || anyContent) {
4203
- return true;
4204
- }
4205
- break;
4206
- // skip to next key
4207
- case options.cdataKey:
4208
- if (options.indentCdata || anyContent) {
4209
- return true;
4210
- }
4211
- break;
4212
- // skip to next key
4213
- case options.instructionKey:
4214
- if (options.indentInstruction || anyContent) {
4215
- return true;
4216
- }
4217
- break;
4218
- // skip to next key
4219
- case options.doctypeKey:
4220
- case options.commentKey:
4221
- return true;
4222
- default:
4223
- return true;
4224
- }
4225
- }
4226
- }
4227
- return false;
4228
- }
4229
- function writeElementCompact(element, name, options, depth, indent) {
4230
- currentElement = element;
4231
- currentElementName = name;
4232
- var elementName = "elementNameFn" in options ? options.elementNameFn(name, element) : name;
4233
- if (typeof element === "undefined" || element === null || element === "") {
4234
- return "fullTagEmptyElementFn" in options && options.fullTagEmptyElementFn(name, element) || options.fullTagEmptyElement ? "<" + elementName + "></" + elementName + ">" : "<" + elementName + "/>";
4235
- }
4236
- var xml = [];
4237
- if (name) {
4238
- xml.push("<" + elementName);
4239
- if (typeof element !== "object") {
4240
- xml.push(">" + writeText(element, options) + "</" + elementName + ">");
4241
- return xml.join("");
4242
- }
4243
- if (element[options.attributesKey]) {
4244
- xml.push(writeAttributes(element[options.attributesKey], options, depth));
4245
- }
4246
- var withClosingTag = hasContentCompact(element, options, true) || element[options.attributesKey] && element[options.attributesKey]["xml:space"] === "preserve";
4247
- if (!withClosingTag) {
4248
- if ("fullTagEmptyElementFn" in options) {
4249
- withClosingTag = options.fullTagEmptyElementFn(name, element);
4250
- } else {
4251
- withClosingTag = options.fullTagEmptyElement;
4252
- }
4253
- }
4254
- if (withClosingTag) {
4255
- xml.push(">");
4256
- } else {
4257
- xml.push("/>");
4258
- return xml.join("");
4259
- }
4260
- }
4261
- xml.push(writeElementsCompact(element, options, depth + 1, false));
4262
- currentElement = element;
4263
- currentElementName = name;
4264
- if (name) {
4265
- xml.push((indent ? writeIndentation(options, depth, false) : "") + "</" + elementName + ">");
4266
- }
4267
- return xml.join("");
4268
- }
4269
- function writeElementsCompact(element, options, depth, firstLine) {
4270
- var i, key, nodes, xml = [];
4271
- for (key in element) {
4272
- if (element.hasOwnProperty(key)) {
4273
- nodes = isArray(element[key]) ? element[key] : [element[key]];
4274
- for (i = 0; i < nodes.length; ++i) {
4275
- switch (key) {
4276
- case options.declarationKey:
4277
- xml.push(writeDeclaration(nodes[i], options, depth));
4278
- break;
4279
- case options.instructionKey:
4280
- xml.push((options.indentInstruction ? writeIndentation(options, depth, firstLine) : "") + writeInstruction(nodes[i], options, depth));
4281
- break;
4282
- case options.attributesKey:
4283
- case options.parentKey:
4284
- break;
4285
- // skip
4286
- case options.textKey:
4287
- xml.push((options.indentText ? writeIndentation(options, depth, firstLine) : "") + writeText(nodes[i], options));
4288
- break;
4289
- case options.cdataKey:
4290
- xml.push((options.indentCdata ? writeIndentation(options, depth, firstLine) : "") + writeCdata(nodes[i], options));
4291
- break;
4292
- case options.doctypeKey:
4293
- xml.push(writeIndentation(options, depth, firstLine) + writeDoctype(nodes[i], options));
4294
- break;
4295
- case options.commentKey:
4296
- xml.push(writeIndentation(options, depth, firstLine) + writeComment(nodes[i], options));
4297
- break;
4298
- default:
4299
- xml.push(writeIndentation(options, depth, firstLine) + writeElementCompact(nodes[i], key, options, depth, hasContentCompact(nodes[i], options)));
4300
- }
4301
- firstLine = firstLine && !xml.length;
4302
- }
4303
- }
4304
- }
4305
- return xml.join("");
4306
- }
4307
- js2xml = function(js, options) {
4308
- options = validateOptions(options);
4309
- var xml = [];
4310
- currentElement = js;
4311
- currentElementName = "_root_";
4312
- if (options.compact) {
4313
- xml.push(writeElementsCompact(js, options, 0, true));
4314
- } else {
4315
- if (js[options.declarationKey]) {
4316
- xml.push(writeDeclaration(js[options.declarationKey], options, 0));
4317
- }
4318
- if (js[options.elementsKey] && js[options.elementsKey].length) {
4319
- xml.push(writeElements(js[options.elementsKey], options, 0, !xml.length));
4320
- }
4321
- }
4322
- return xml.join("");
4323
- };
4324
- return js2xml;
4325
- }
4326
- var json2xml;
4327
- var hasRequiredJson2xml;
4328
- function requireJson2xml() {
4329
- if (hasRequiredJson2xml) return json2xml;
4330
- hasRequiredJson2xml = 1;
4331
- var js2xml2 = requireJs2xml();
4332
- json2xml = function(json, options) {
4333
- if (json instanceof Buffer) {
4334
- json = json.toString();
4335
- }
4336
- var js = null;
4337
- if (typeof json === "string") {
4338
- try {
4339
- js = JSON.parse(json);
4340
- } catch (e) {
4341
- throw new Error("The JSON structure is invalid");
4342
- }
4343
- } else {
4344
- js = json;
4345
- }
4346
- return js2xml2(js, options);
4347
- };
4348
- return json2xml;
4349
- }
4350
- var lib;
4351
- var hasRequiredLib;
4352
- function requireLib() {
4353
- if (hasRequiredLib) return lib;
4354
- hasRequiredLib = 1;
4355
- var xml2js2 = requireXml2js();
4356
- var xml2json2 = requireXml2json();
4357
- var js2xml2 = requireJs2xml();
4358
- var json2xml2 = requireJson2xml();
4359
- lib = {
4360
- xml2js: xml2js2,
4361
- xml2json: xml2json2,
4362
- js2xml: js2xml2,
4363
- json2xml: json2xml2
4364
- };
4365
- return lib;
4366
- }
4367
- requireLib();
4368
- requireBuffer();
4369
- class TiledProperties {
4370
- constructor(data) {
4371
- this.properties = {};
4372
- this.properties = data?.properties ?? {};
4373
- }
4374
- getProperty(name, defaultValue) {
4375
- const val = this.properties[name];
4376
- if (val === void 0) {
4377
- return defaultValue;
4378
- }
4379
- return val;
4380
- }
4381
- hasProperty(name) {
4382
- return !!this.properties[name];
4383
- }
4384
- setProperty(name, value) {
4385
- this.properties[name] = value;
4386
- }
4387
- getType() {
4388
- return this.class || this["type"];
4389
- }
4390
- }
4391
- const FLIPPED_HORIZONTALLY_FLAG = 2147483648;
4392
- const FLIPPED_VERTICALLY_FLAG = 1073741824;
4393
- const FLIPPED_DIAGONALLY_FLAG = 536870912;
4394
- const ROTATED_HEXAGONAL_120_FLAG = 268435456;
4395
- class TileGid extends TiledProperties {
4396
- constructor(obj) {
4397
- super(obj);
4398
- this.obj = obj;
4399
- this._gid = obj?.gid;
4400
- }
4401
- static getRealGid(gid) {
4402
- return gid & 268435455;
4403
- }
4404
- get horizontalFlip() {
4405
- return !!(this._gid & FLIPPED_HORIZONTALLY_FLAG);
4406
- }
4407
- get verticalFlip() {
4408
- return !!(this._gid & FLIPPED_VERTICALLY_FLAG);
4409
- }
4410
- get diagonalFlip() {
4411
- return !!(this._gid & FLIPPED_DIAGONALLY_FLAG);
4412
- }
4413
- get rotatedHex120() {
4414
- return !!(this._gid & ROTATED_HEXAGONAL_120_FLAG);
4415
- }
4416
- get gid() {
4417
- return TileGid.getRealGid(this._gid);
4418
- }
4419
- set gid(val) {
4420
- this._gid = val;
4421
- }
4422
- }
4423
- class Tile extends TileGid {
4424
- constructor(tile) {
4425
- super(tile);
4426
- this.tile = tile;
4427
- const preservedProperties = this.properties;
4428
- Reflect.deleteProperty(tile, "gid");
4429
- Object.assign(this, tile);
4430
- if (preservedProperties && Object.keys(preservedProperties).length > 0) {
4431
- this.properties = { ...preservedProperties, ...this.properties };
4432
- }
4433
- }
4434
- }
4435
- class TiledObjectClass extends TileGid {
4436
- constructor(object) {
4437
- super(object);
4438
- this.layerName = "";
4439
- Object.assign(this, object);
4440
- if (object?.gid) {
4441
- this.y -= this.height;
4442
- }
4443
- }
4444
- }
4445
- class Layer extends TiledProperties {
4446
- constructor(layer, tilesets, parent) {
4447
- super(layer);
4448
- this.tilesets = tilesets;
4449
- this.parent = parent;
4450
- this.cacheTiles = false;
4451
- this.tiles = [];
4452
- Object.assign(this, layer);
4453
- this.mapObjects();
4454
- this.mergePropertiesWithParent();
4455
- this.cacheTiles = this.getProperty("cache-tiles", false);
4456
- if (this.cacheTiles) this.propertiesTiles();
4457
- }
4458
- get size() {
4459
- return this.data.length;
4460
- }
4461
- createTile(gid, tileIndex, layerIndex) {
4462
- if (gid == 0) {
4463
- return;
4464
- }
4465
- const realGid = TileGid.getRealGid(gid);
4466
- const tileset = Layer.findTileSet(realGid, this.tilesets);
4467
- if (!tileset) {
4468
- return void 0;
4469
- }
4470
- const tile = tileset.getTile(realGid - tileset.firstgid);
4471
- if (tile) {
4472
- return new Tile({
4473
- ...tile.tile,
4474
- gid,
4475
- index: tileIndex,
4476
- layerIndex
4477
- });
4478
- }
4479
- return new Tile({
4480
- gid,
4481
- index: tileIndex,
4482
- layerIndex
4483
- });
4484
- }
4485
- mergePropertiesWithParent() {
4486
- const parent = this.getLayerParent();
4487
- if (!this.properties) this.properties = {};
4488
- if (!parent) return;
4489
- for (let key in parent.properties) {
4490
- const val = parent.properties[key];
4491
- const valChild = this.properties[key];
4492
- if (valChild === void 0) {
4493
- this.properties[key] = val;
4494
- } else {
4495
- if (key == "z") {
4496
- this.properties[key] += val;
4497
- } else {
4498
- continue;
4499
- }
4500
- }
4501
- }
4502
- this.opacity = Math.round((parent.opacity ?? 1) * (this.opacity ?? 1) * 100) / 100;
4503
- this.offsetx = (parent.offsetx ?? 0) + (this.offsetx ?? 0);
4504
- this.offsety = (parent.offsety ?? 0) + (this.offsety ?? 0);
4505
- this.locked = parent.locked ?? false;
4506
- }
4507
- propertiesTiles() {
4508
- if (!this.data) return;
4509
- const data = this.data;
4510
- for (let i = 0; i < data.length; i++) {
4511
- const id = data[i];
4512
- this.tiles.push(this.createTile(id, i));
4513
- }
4514
- }
4515
- mapObjects() {
4516
- if (this.objects) {
4517
- this.objects = this.objects.map((object) => {
4518
- const obj = new TiledObjectClass(object);
4519
- obj.layerName = this.name;
4520
- return obj;
4521
- });
4522
- }
4523
- }
4524
- getTileByIndex(tileIndex) {
4525
- if (this.cacheTiles) {
4526
- return this.tiles[tileIndex];
4527
- }
4528
- return this.createTile(this.data[tileIndex], tileIndex);
4529
- }
4530
- static findTileSet(gid, tileSets) {
4531
- let tileset;
4532
- for (let i = tileSets.length - 1; i >= 0; i--) {
4533
- tileset = tileSets[i];
4534
- if (tileset.firstgid && tileset.firstgid <= gid) {
4535
- break;
4536
- }
4537
- }
4538
- return tileset;
4539
- }
4540
- getLayerParent() {
4541
- return this.parent;
4542
- }
4543
- tilesForEach(cb) {
4544
- for (let i = 0; i < this.data.length; i++) {
4545
- if (this.cacheTiles) {
4546
- cb(this.tiles[i], i);
4547
- continue;
4548
- }
4549
- cb(this.createTile(this.data[i], i), i);
4550
- }
4551
- }
4552
- setData(tileIndex, gid) {
4553
- this.data[tileIndex] = gid;
4554
- }
4555
- }
4556
- class Tileset extends TiledProperties {
4557
- constructor(tileset) {
4558
- super(tileset);
4559
- this.tileset = tileset;
4560
- this.cacheTileId = /* @__PURE__ */ new Map();
4561
- Object.assign(this, tileset);
4562
- this.margin = this.margin ?? 0;
4563
- this.spacing = this.spacing ?? 0;
4564
- const tilesArray = tileset.tiles || tileset.tile || [];
4565
- for (let tile of tilesArray) {
4566
- this.addTile(tile);
4567
- }
4568
- Reflect.deleteProperty(this, "tiles");
4569
- Reflect.deleteProperty(this, "tile");
4570
- }
4571
- addTile(tileObj) {
4572
- const tile = new Tile(tileObj);
4573
- this.cacheTileId.set(tile.id, tile);
4574
- return tile;
4575
- }
4576
- getTile(id) {
4577
- return this.cacheTileId.get(+id);
4578
- }
4579
- }
4580
- let bufferTilesets = {};
4581
- class MapClass extends TiledProperties {
4582
- constructor(map) {
4583
- super(map ?? {});
4584
- this.tilesets = [];
4585
- this.layers = [];
4586
- this.tmpLayers = [];
4587
- this.tilesIndex = {};
4588
- this.allocateMemory = 0;
4589
- this.lowMemory = false;
4590
- if (map) this.load(map);
4591
- }
4592
- load(map) {
4593
- Object.assign(this, map);
4594
- if (this.hasProperty("low-memory")) {
4595
- this.lowMemory = this.getProperty("low-memory", false);
4596
- }
4597
- this.tmpLayers = [];
4598
- this.mapTilesets();
4599
- this.mapLayers(this.layers);
4600
- this.layers = [...this.tmpLayers];
4601
- Reflect.deleteProperty(this, "tmpLayers");
4602
- this.setTilesIndex();
4603
- this.data = map;
4604
- }
4605
- /**
4606
- * @title Width of the map in pixels
4607
- * @prop {number} [widthPx]
4608
- * @readonly
4609
- * @memberof Map
4610
- * @memberof RpgSceneMap
4611
- * */
4612
- get widthPx() {
4613
- return this.width * this.tilewidth;
4614
- }
4615
- /**
4616
- * @title Height of the map in pixels
4617
- * @prop {number} [heightPx]
4618
- * @readonly
4619
- * @memberof Map
4620
- * @memberof RpgSceneMap
4621
- * */
4622
- get heightPx() {
4623
- return this.height * this.tileheight;
4624
- }
4625
- /**
4626
- * @title The depth of the map in pixels (this is the height of a tile ;))
4627
- * @prop {number} map.zTileHeight
4628
- * @readonly
4629
- * @memberof Map
4630
- * @memberof RpgSceneMap
4631
- * */
4632
- get zTileHeight() {
4633
- return this.tileheight;
4634
- }
4635
- /**
4636
- * Find a layer by name. Returns `undefined` is the layer is not found
4637
-
4638
- * @title Get Layer by name
4639
- * @method map.getLayerByName(name)
4640
- * @param {string} name layer name
4641
- * @returns {LayerInfo | undefined}
4642
- * @example
4643
- * ```ts
4644
- * const tiles = map.getLayerByName(0, 0)
4645
- * ```
4646
- * @memberof Map
4647
- * @memberof RpgSceneMap
4648
- */
4649
- getLayerByName(name) {
4650
- return this.layers.find((layer) => layer.name == name);
4651
- }
4652
- /**
4653
- * Get the tile index on the tileset
4654
- *
4655
- * @title Get index of tile
4656
- * @method map.getTileIndex(x,y)
4657
- * @param {number} x Position X
4658
- * @param {number} x Position Y
4659
- * @returns {number}
4660
- * @memberof Map
4661
- * @memberof RpgSceneMap
4662
- */
4663
- getTileIndex(x, y, [z] = [0]) {
4664
- return this.width * Math.floor((y - z) / this.tileheight) + Math.floor(x / this.tilewidth);
4665
- }
4666
- getTilePosition(index) {
4667
- return {
4668
- y: Math.floor(index / this.width) * this.tileheight,
4669
- x: index % this.width * this.tilewidth
4670
- };
4671
- }
4672
- /**
4673
- * Find the point of origin (top left) of a tile. Of course, its position depends on the size of the tile
4674
-
4675
- * @title Get origin position of tile
4676
- * @method map.getTileOriginPosition(x,y)
4677
- * @param {number} x Position X
4678
- * @param {number} x Position Y
4679
- * @returns { {x: number, y: number }}
4680
- * @example
4681
- * ```ts
4682
- * // If the size of a tile is 32x32px
4683
- * const position = map.getTileOriginPosition(35, 12)
4684
- * console.log(position) // { x: 32, y: 0 }
4685
- * ```
4686
- * @memberof Map
4687
- * @memberof RpgSceneMap
4688
- */
4689
- getTileOriginPosition(x, y) {
4690
- return {
4691
- x: Math.floor(x / this.tilewidth) * this.tilewidth,
4692
- y: Math.floor(y / this.tileheight) * this.tileheight
4693
- };
4694
- }
4695
- /**
4696
- * Recover tiles according to a position
4697
-
4698
- * @title Get tile by position
4699
- * @method map.getTileByPosition(x,y)
4700
- * @param {number} x Position X
4701
- * @param {number} x Position Y
4702
- * @returns {TileInfo}
4703
- * @example
4704
- * ```ts
4705
- * const tiles = map.getTileByPosition(0, 0)
4706
- * ```
4707
- * @memberof Map
4708
- * @memberof RpgSceneMap
4709
- */
4710
- getTileByPosition(x, y, z = [0, 0], options = {}) {
4711
- const tileIndex = this.getTileIndex(x, y, [z[0]]);
4712
- return this.getTileByIndex(tileIndex, z, options);
4713
- }
4714
- /**
4715
- * Retrieves tiles according to its index
4716
-
4717
- * @title Get tile by index
4718
- * @method map.getTileByIndex(tileIndex)
4719
- * @param {number} tileIndex tile index
4720
- * @returns {TileInfo}
4721
- * @example
4722
- * ```ts
4723
- * const index = map.getTileIndex(0, 0)
4724
- * const tiles = map.getTileByIndex(index)
4725
- * ```
4726
- * @memberof Map
4727
- * @memberof RpgSceneMap
4728
- */
4729
- getTileByIndex(tileIndex, zPlayer = [0, 0], options = {
4730
- populateTiles: true
4731
- }) {
4732
- const zA = Math.floor(zPlayer[0] / this.zTileHeight);
4733
- Math.floor(zPlayer[1] / this.zTileHeight);
4734
- const level = this.tilesIndex[zA];
4735
- const obj = {
4736
- tiles: [],
4737
- hasCollision: false,
4738
- isOverlay: false,
4739
- objectGroups: [],
4740
- tileIndex
4741
- };
4742
- if (!level) {
4743
- return obj;
4744
- }
4745
- const [layer] = this.layers;
4746
- const getTileByPointer = (pointer = 0) => {
4747
- const pos = tileIndex * this.realAllocateMemory + pointer;
4748
- const gid = level[pos];
4749
- if (gid === 0) {
4750
- return obj;
4751
- }
4752
- const tile2 = layer.createTile(gid, tileIndex, level[pos + 1]);
4753
- if (tile2) obj.tiles.push(tile2);
4754
- };
4755
- if (options.populateTiles) {
4756
- for (let i = 0; i < this.realAllocateMemory; i += 2) {
4757
- getTileByPointer(i);
4758
- }
4759
- } else {
4760
- getTileByPointer();
4761
- }
4762
- const [tile] = obj.tiles;
4763
- if (tile) {
4764
- obj.hasCollision = tile.getProperty("collision", false);
4765
- obj.objectGroups = tile.objects ?? [];
4766
- }
4767
- return obj;
4768
- }
4769
- getAllObjects() {
4770
- return this.layers.reduce((prev, current) => {
4771
- if (!current.objects) return prev;
4772
- return prev.concat(...current.objects);
4773
- }, []);
4774
- }
4775
- getData() {
4776
- return {
4777
- ...this.data,
4778
- layers: this.layers
4779
- };
4780
- }
4781
- setTile(x, y, layerFilter, tileInfo) {
4782
- if (this.lowMemory) {
4783
- throw "Impossible to change a tile with the lowMemory option";
4784
- }
4785
- const tileIndex = this.getTileIndex(x, y);
4786
- let fnFilter;
4787
- let tilesEdited = {};
4788
- if (typeof layerFilter == "string") {
4789
- fnFilter = (layer) => layer.name == layerFilter;
4790
- } else {
4791
- fnFilter = layerFilter;
4792
- }
4793
- for (let i = 0; i < this.layers.length; i++) {
4794
- const layer = this.layers[i];
4795
- if (!fnFilter(layer)) continue;
4796
- let tile;
4797
- const oldTile = this.getTileByIndex(tileIndex);
4798
- if (tileInfo.gid) {
4799
- tile = layer.createTile(tileInfo.gid, tileIndex);
4800
- }
4801
- if (!tile) continue;
4802
- for (let key in tileInfo) {
4803
- if (key == "gid") continue;
4804
- tile[key] = tileInfo[key];
4805
- }
4806
- tilesEdited[layer.name] = {
4807
- gid: tile.gid,
4808
- properties: tile.properties
4809
- };
4810
- this.setTileIndex(layer, oldTile.tiles[0], tile, tileIndex, i);
4811
- layer.setData(tileIndex, tile.gid);
4812
- }
4813
- return {
4814
- x,
4815
- y,
4816
- tiles: tilesEdited
4817
- };
4818
- }
4819
- removeCacheTileset(name) {
4820
- delete bufferTilesets[name];
4821
- }
4822
- clearCacheTilesets() {
4823
- bufferTilesets = {};
4824
- }
4825
- mapTilesets() {
4826
- this.tilesets = this.tilesets.map((tileset) => {
4827
- if (bufferTilesets[tileset.name]) {
4828
- const instance = bufferTilesets[tileset.name];
4829
- instance.firstgid = tileset.firstgid;
4830
- return instance;
4831
- }
4832
- const _tileset = new Tileset(tileset);
4833
- bufferTilesets[_tileset.name] = _tileset;
4834
- return _tileset;
4835
- });
4836
- }
4837
- mapLayers(layers = [], parent) {
4838
- for (let layer of layers) {
4839
- const layerInstance = new Layer(layer, this.tilesets, parent);
4840
- this.tmpLayers.push(layerInstance);
4841
- if (layer.layers) {
4842
- this.mapLayers(layer.layers, layerInstance);
4843
- }
4844
- }
4845
- if (this.lowMemory) this.allocateMemory = 1;
4846
- if (!this.allocateMemory) this.allocateMemory = this.layers.length;
4847
- }
4848
- setTileIndex(layer, oldTile, newTile, tileIndex, layerIndex) {
4849
- const startPos = tileIndex * this.realAllocateMemory;
4850
- let pointer = startPos + this.realAllocateMemory - 2;
4851
- const zLayer = layer.getProperty("z", 0);
4852
- const zTile = oldTile.getProperty("z", 0);
4853
- let z = zLayer + zTile;
4854
- while (pointer >= startPos) {
4855
- const zlayer = this.tilesIndex[z];
4856
- if (zlayer[pointer] === oldTile.gid && zlayer[pointer + 1] === layerIndex) {
4857
- this.tilesIndex[z][pointer] = newTile.gid;
4858
- }
4859
- pointer -= 2;
4860
- }
4861
- }
4862
- /**
4863
- * We multiply by 2 because 2 entries are stored for a tile: its GID and the Layer Index
4864
- *
4865
- * Example If I have 3 layers, The array will have the following form
4866
- *
4867
- * [
4868
- * GID of Layer 3,
4869
- * Layer Index of Layer 3,
4870
- * GID of Layer 2,
4871
- * Layer Index of Layer 2,
4872
- * GID of Layer 1,
4873
- * Layer Index of Layer 1,
4874
- * ... others tiles
4875
- * ]
4876
- *
4877
- * The size in memory of the map is therefore:
4878
- *
4879
- * `(map width * map height * number of layers * 4) bytes`
4880
- *
4881
- * > We multiply by 4, because an element takes 2 bytes and has 2 elements for a tile is 4 bytes in all
4882
- *
4883
- * Example (a 100x100 map with 5 layers)
4884
- *
4885
- * `100 * 100 * 5 * 4 = 200000 bytes = ~195 Kb`
4886
- *
4887
- * If we define on lowMemory then the calculation is the following
4888
- *
4889
- * `(map width * map height * 4) bytes`
4890
- *
4891
- * Example
4892
- *
4893
- * `100 * 100 * 4 = 40000 bytes = ~39 Kb`
4894
- */
4895
- get realAllocateMemory() {
4896
- return this.allocateMemory * 2;
4897
- }
4898
- /**
4899
- * We keep each tile in memory classified by z value. The values are ordered from the end to the beginning so that the first element of the array (when retrieved with getTileByIndex() is the tile on the highest layer. This way, the tile search is very fast for collisions
4900
- *
4901
- */
4902
- addTileIndex(layer, tile, tileIndex, layerIndex) {
4903
- if (!tile || tile && tile.gid == 0) {
4904
- return;
4905
- }
4906
- const zLayer = layer.getProperty("z", 0);
4907
- const zTile = tile.getProperty("z", 0);
4908
- let z = zLayer + zTile;
4909
- if (!this.tilesIndex[z]) {
4910
- const buffer2 = new ArrayBuffer(layer.size * this.realAllocateMemory * 2);
4911
- this.tilesIndex[z] = new Uint16Array(buffer2);
4912
- }
4913
- const startPos = tileIndex * this.realAllocateMemory;
4914
- let pointer = startPos + this.realAllocateMemory - 2;
4915
- while (this.tilesIndex[z][pointer] !== 0 && pointer > startPos) {
4916
- pointer -= 2;
4917
- }
4918
- this.tilesIndex[z][pointer] = tile.gid;
4919
- this.tilesIndex[z][pointer + 1] = layerIndex;
4920
- this.tilesIndex[z][startPos] = tile.gid;
4921
- this.tilesIndex[z][startPos + 1] = layerIndex;
4922
- }
4923
- setTilesIndex() {
4924
- for (let i = 0; i < this.layers.length; i++) {
4925
- const layer = this.layers[i];
4926
- if (layer.type != TiledLayerType.Tile) {
4927
- continue;
4928
- }
4929
- layer.tilesForEach((tile, index) => {
4930
- this.addTileIndex(layer, tile, index, i);
4931
- });
4932
- }
4933
- }
4934
- }
4935
- export {
4936
- Layer,
4937
- MapClass,
4938
- Tile,
4939
- TiledLayerType,
4940
- TiledObjectClass,
4941
- TiledProperties,
4942
- Tileset
4943
- };
1
+ import { MapClass } from "./index2.js";
2
+ //#region src/physics.ts
3
+ var TILED_HITBOX_ID_PREFIX = "__tiled_collision__:";
4
+ function prepareTiledPhysicsData(mapData, map) {
5
+ if (!mapData?.parsedMap) return;
6
+ const tiledMap = new MapClass(mapData.parsedMap);
7
+ map.tiled = tiledMap;
8
+ const tiledHitboxes = collectBlockedTileHitboxes(tiledMap);
9
+ mapData.hitboxes = mergeTiledHitboxes(mapData.hitboxes, tiledHitboxes);
10
+ mapData.width = tiledMap.widthPx;
11
+ mapData.height = tiledMap.heightPx;
12
+ }
13
+ function applyTiledPointEvents(mapData) {
14
+ const objects = mapData?.parsedMap?.objects;
15
+ if (!Array.isArray(objects) || !Array.isArray(mapData?.events)) return;
16
+ for (const obj of objects) {
17
+ if (!obj?.point) continue;
18
+ mapData.events = mapData.events.map((eventEntry) => {
19
+ if (eventEntry?.name === obj.name) return {
20
+ event: eventEntry,
21
+ x: obj.x,
22
+ y: obj.y
23
+ };
24
+ return eventEntry;
25
+ }).filter((eventEntry) => eventEntry !== null);
26
+ }
27
+ }
28
+ function collectBlockedTileHitboxes(tiledMap) {
29
+ const hitboxes = [];
30
+ const mapWidth = tiledMap.width;
31
+ const mapHeight = tiledMap.height;
32
+ const tileWidth = tiledMap.tilewidth;
33
+ const tileHeight = tiledMap.tileheight;
34
+ for (let y = 0; y < mapHeight; y++) for (let x = 0; x < mapWidth; x++) if (tiledMap.getTileByPosition(x * tileWidth, y * tileHeight, [0, 0], { populateTiles: true }).hasCollision) hitboxes.push({
35
+ id: createTiledHitboxId(x, y),
36
+ x: x * tileWidth,
37
+ y: y * tileHeight,
38
+ width: tileWidth,
39
+ height: tileHeight
40
+ });
41
+ return hitboxes;
42
+ }
43
+ function mergeTiledHitboxes(existingHitboxes, tiledHitboxes) {
44
+ return [...Array.isArray(existingHitboxes) ? existingHitboxes.filter((hitbox) => !isGeneratedTiledHitbox(hitbox)) : [], ...tiledHitboxes];
45
+ }
46
+ function isGeneratedTiledHitbox(hitbox) {
47
+ return typeof hitbox?.id === "string" && hitbox.id.startsWith(TILED_HITBOX_ID_PREFIX);
48
+ }
49
+ function createTiledHitboxId(x, y) {
50
+ return `${TILED_HITBOX_ID_PREFIX}${x},${y}`;
51
+ }
52
+ //#endregion
53
+ export { applyTiledPointEvents, prepareTiledPhysicsData };