@loaders.gl/xml 3.3.0-alpha.8

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