@loaders.gl/xml
Version:
Framework-independent loaders for the XML (eXtensible Markup Language) format
4 lines • 86.7 kB
Source Map (JSON)
{
"version": 3,
"sources": ["index.js", "sax-ts/sax.js", "lib/xml-utils/uncapitalize.js", "lib/parsers/parse-xml.js", "xml-loader.js", "html-loader.js", "lib/xml-utils/xml-utils.js"],
"sourcesContent": ["// loaders.gl\n// SPDX-License-Identifier: MIT\n// Copyright (c) vis.gl contributors\nexport { XMLLoader } from \"./xml-loader.js\";\nexport { HTMLLoader } from \"./html-loader.js\";\nexport { SAXParser as SAXParser } from \"./sax-ts/sax.js\";\n// Utilities\nexport { convertXMLValueToArray, convertXMLFieldToArrayInPlace } from \"./lib/xml-utils/xml-utils.js\";\n// Experimental\nexport { uncapitalize as _uncapitalize, uncapitalizeKeys as _uncapitalizeKeys } from \"./lib/xml-utils/uncapitalize.js\";\n", "// loaders.gl\n// SPDX-License-Identifier: MIT\n// Copyright (c) vis.gl contributors\nconst DEFAULT_SAX_EVENTS = {\n ontext: () => { },\n onprocessinginstruction: () => { },\n onsgmldeclaration: () => { },\n ondoctype: () => { },\n oncomment: () => { },\n onopentagstart: () => { },\n onattribute: () => { },\n onopentag: () => { },\n onclosetag: () => { },\n onopencdata: () => { },\n oncdata: () => { },\n onclosecdata: () => { },\n onerror: () => { },\n onend: () => { },\n onready: () => { },\n onscript: () => { },\n onopennamespace: () => { },\n onclosenamespace: () => { }\n};\nconst DEFAULT_SAX_PARSER_OPTIONS = {\n ...DEFAULT_SAX_EVENTS,\n strict: false,\n MAX_BUFFER_LENGTH: 64 * 1024,\n lowercase: false,\n lowercasetags: false,\n noscript: false,\n strictEntities: false,\n xmlns: undefined,\n position: undefined,\n trim: undefined,\n normalize: undefined\n};\nconst EVENTS = [\n 'text',\n 'processinginstruction',\n 'sgmldeclaration',\n 'doctype',\n 'comment',\n 'opentagstart',\n 'attribute',\n 'opentag',\n 'closetag',\n 'opencdata',\n 'cdata',\n 'closecdata',\n 'error',\n 'end',\n 'ready',\n 'script',\n 'opennamespace',\n 'closenamespace'\n];\nconst BUFFERS = [\n 'comment',\n 'sgmlDecl',\n 'textNode',\n 'tagName',\n 'doctype',\n 'procInstName',\n 'procInstBody',\n 'entity',\n 'attribName',\n 'attribValue',\n 'cdata',\n 'script'\n];\nconst 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]/;\nconst 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-]/;\nconst 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]/;\nconst 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-]/;\nexport const ENTITIES = {\n amp: '&',\n gt: '>',\n lt: '<',\n quot: '\"',\n apos: \"'\",\n AElig: 198,\n Aacute: 193,\n Acirc: 194,\n Agrave: 192,\n Aring: 197,\n Atilde: 195,\n Auml: 196,\n Ccedil: 199,\n ETH: 208,\n Eacute: 201,\n Ecirc: 202,\n Egrave: 200,\n Euml: 203,\n Iacute: 205,\n Icirc: 206,\n Igrave: 204,\n Iuml: 207,\n Ntilde: 209,\n Oacute: 211,\n Ocirc: 212,\n Ograve: 210,\n Oslash: 216,\n Otilde: 213,\n Ouml: 214,\n THORN: 222,\n Uacute: 218,\n Ucirc: 219,\n Ugrave: 217,\n Uuml: 220,\n Yacute: 221,\n aacute: 225,\n acirc: 226,\n aelig: 230,\n agrave: 224,\n aring: 229,\n atilde: 227,\n auml: 228,\n ccedil: 231,\n eacute: 233,\n ecirc: 234,\n egrave: 232,\n eth: 240,\n euml: 235,\n iacute: 237,\n icirc: 238,\n igrave: 236,\n iuml: 239,\n ntilde: 241,\n oacute: 243,\n ocirc: 244,\n ograve: 242,\n oslash: 248,\n otilde: 245,\n ouml: 246,\n szlig: 223,\n thorn: 254,\n uacute: 250,\n ucirc: 251,\n ugrave: 249,\n uuml: 252,\n yacute: 253,\n yuml: 255,\n copy: 169,\n reg: 174,\n nbsp: 160,\n iexcl: 161,\n cent: 162,\n pound: 163,\n curren: 164,\n yen: 165,\n brvbar: 166,\n sect: 167,\n uml: 168,\n ordf: 170,\n laquo: 171,\n not: 172,\n shy: 173,\n macr: 175,\n deg: 176,\n plusmn: 177,\n sup1: 185,\n sup2: 178,\n sup3: 179,\n acute: 180,\n micro: 181,\n para: 182,\n middot: 183,\n cedil: 184,\n ordm: 186,\n raquo: 187,\n frac14: 188,\n frac12: 189,\n frac34: 190,\n iquest: 191,\n times: 215,\n divide: 247,\n OElig: 338,\n oelig: 339,\n Scaron: 352,\n scaron: 353,\n Yuml: 376,\n fnof: 402,\n circ: 710,\n tilde: 732,\n Alpha: 913,\n Beta: 914,\n Gamma: 915,\n Delta: 916,\n Epsilon: 917,\n Zeta: 918,\n Eta: 919,\n Theta: 920,\n Iota: 921,\n Kappa: 922,\n Lambda: 923,\n Mu: 924,\n Nu: 925,\n Xi: 926,\n Omicron: 927,\n Pi: 928,\n Rho: 929,\n Sigma: 931,\n Tau: 932,\n Upsilon: 933,\n Phi: 934,\n Chi: 935,\n Psi: 936,\n Omega: 937,\n alpha: 945,\n beta: 946,\n gamma: 947,\n delta: 948,\n epsilon: 949,\n zeta: 950,\n eta: 951,\n theta: 952,\n iota: 953,\n kappa: 954,\n lambda: 955,\n mu: 956,\n nu: 957,\n xi: 958,\n omicron: 959,\n pi: 960,\n rho: 961,\n sigmaf: 962,\n sigma: 963,\n tau: 964,\n upsilon: 965,\n phi: 966,\n chi: 967,\n psi: 968,\n omega: 969,\n thetasym: 977,\n upsih: 978,\n piv: 982,\n ensp: 8194,\n emsp: 8195,\n thinsp: 8201,\n zwnj: 8204,\n zwj: 8205,\n lrm: 8206,\n rlm: 8207,\n ndash: 8211,\n mdash: 8212,\n lsquo: 8216,\n rsquo: 8217,\n sbquo: 8218,\n ldquo: 8220,\n rdquo: 8221,\n bdquo: 8222,\n dagger: 8224,\n Dagger: 8225,\n bull: 8226,\n hellip: 8230,\n permil: 8240,\n prime: 8242,\n Prime: 8243,\n lsaquo: 8249,\n rsaquo: 8250,\n oline: 8254,\n frasl: 8260,\n euro: 8364,\n image: 8465,\n weierp: 8472,\n real: 8476,\n trade: 8482,\n alefsym: 8501,\n larr: 8592,\n uarr: 8593,\n rarr: 8594,\n darr: 8595,\n harr: 8596,\n crarr: 8629,\n lArr: 8656,\n uArr: 8657,\n rArr: 8658,\n dArr: 8659,\n hArr: 8660,\n forall: 8704,\n part: 8706,\n exist: 8707,\n empty: 8709,\n nabla: 8711,\n isin: 8712,\n notin: 8713,\n ni: 8715,\n prod: 8719,\n sum: 8721,\n minus: 8722,\n lowast: 8727,\n radic: 8730,\n prop: 8733,\n infin: 8734,\n ang: 8736,\n and: 8743,\n or: 8744,\n cap: 8745,\n cup: 8746,\n int: 8747,\n there4: 8756,\n sim: 8764,\n cong: 8773,\n asymp: 8776,\n ne: 8800,\n equiv: 8801,\n le: 8804,\n ge: 8805,\n sub: 8834,\n sup: 8835,\n nsub: 8836,\n sube: 8838,\n supe: 8839,\n oplus: 8853,\n otimes: 8855,\n perp: 8869,\n sdot: 8901,\n lceil: 8968,\n rceil: 8969,\n lfloor: 8970,\n rfloor: 8971,\n lang: 9001,\n rang: 9002,\n loz: 9674,\n spades: 9824,\n clubs: 9827,\n hearts: 9829,\n diams: 9830\n};\nObject.keys(ENTITIES).forEach((key) => {\n const e = ENTITIES[key];\n ENTITIES[key] = typeof e === 'number' ? String.fromCharCode(e) : e;\n});\n/**\n * Internal helper class\n */\nclass SAX {\n EVENTS = EVENTS;\n ENTITIES = {\n // TODO: make it readonly, needed for entity-mega test\n // amp, gt, lt, quot and apos are resolved to strings instead of numerical\n // codes, IDK why\n ...ENTITIES\n };\n XML_ENTITIES = {\n amp: '&',\n gt: '>',\n lt: '<',\n quot: '\"',\n apos: \"'\"\n };\n S = 0;\n opt;\n trackPosition = false;\n column = 0;\n line = 0;\n c = '';\n error;\n q = '';\n bufferCheckPosition;\n closed = false;\n tags = [];\n looseCase = '';\n closedRoot = false;\n sawRoot = false;\n strict = false;\n tag;\n strictEntities;\n state;\n noscript = false;\n attribList = [];\n ns;\n position = 0;\n STATE = {\n BEGIN: this.S++, // leading byte order mark or whitespace\n BEGIN_WHITESPACE: this.S++, // leading whitespace\n TEXT: this.S++, // general stuff\n TEXT_ENTITY: this.S++, // & and such.\n OPEN_WAKA: this.S++, // <\n SGML_DECL: this.S++, // <!BLARG\n SGML_DECL_QUOTED: this.S++, // <!BLARG foo \"bar\n DOCTYPE: this.S++, // <!DOCTYPE\n DOCTYPE_QUOTED: this.S++, // <!DOCTYPE \"//blah\n DOCTYPE_DTD: this.S++, // <!DOCTYPE \"//blah\" [ ...\n DOCTYPE_DTD_QUOTED: this.S++, // <!DOCTYPE \"//blah\" [ \"foo\n COMMENT_STARTING: this.S++, // <!-\n COMMENT: this.S++, // <!--\n COMMENT_ENDING: this.S++, // <!-- blah -\n COMMENT_ENDED: this.S++, // <!-- blah --\n CDATA: this.S++, // <![CDATA[ something\n CDATA_ENDING: this.S++, // ]\n CDATA_ENDING_2: this.S++, // ]]\n PROC_INST: this.S++, // <?hi\n PROC_INST_BODY: this.S++, // <?hi there\n PROC_INST_ENDING: this.S++, // <?hi \"there\" ?\n OPEN_TAG: this.S++, // <strong\n OPEN_TAG_SLASH: this.S++, // <strong /\n ATTRIB: this.S++, // <a\n ATTRIB_NAME: this.S++, // <a foo\n ATTRIB_NAME_SAW_WHITE: this.S++, // <a foo _\n ATTRIB_VALUE: this.S++, // <a foo=\n ATTRIB_VALUE_QUOTED: this.S++, // <a foo=\"bar\n ATTRIB_VALUE_CLOSED: this.S++, // <a foo=\"bar\"\n ATTRIB_VALUE_UNQUOTED: this.S++, // <a foo=bar\n ATTRIB_VALUE_ENTITY_Q: this.S++, // <foo bar=\""\"\n ATTRIB_VALUE_ENTITY_U: this.S++, // <foo bar="\n CLOSE_TAG: this.S++, // </a\n CLOSE_TAG_SAW_WHITE: this.S++, // </a >\n SCRIPT: this.S++, // <script> ...\n SCRIPT_ENDING: this.S++ // <script> ... <\n };\n BUFFERS = BUFFERS;\n // private parser: (strict: boolean, opt: any) => SAXParser;\n CDATA = '[CDATA[';\n DOCTYPE = 'DOCTYPE';\n XML_NAMESPACE = 'http://www.w3.org/XML/1998/namespace';\n XMLNS_NAMESPACE = 'http://www.w3.org/2000/xmlns/';\n rootNS = {\n xml: this.XML_NAMESPACE,\n xmlns: this.XMLNS_NAMESPACE\n };\n comment;\n sgmlDecl;\n textNode = '';\n tagName;\n doctype;\n procInstName;\n procInstBody;\n entity = '';\n attribName;\n attribValue;\n cdata = '';\n script = '';\n startTagPosition = 0;\n constructor() {\n this.S = 0;\n for (const s in this.STATE) {\n if (this.STATE.hasOwnProperty(s)) {\n this.STATE[this.STATE[s]] = s;\n }\n }\n // shorthand\n this.S = this.STATE;\n }\n static charAt(chunk, i) {\n let result = '';\n if (i < chunk.length) {\n result = chunk.charAt(i);\n }\n return result;\n }\n static isWhitespace(c) {\n return c === ' ' || c === '\\n' || c === '\\r' || c === '\\t';\n }\n static isQuote(c) {\n return c === '\"' || c === \"'\";\n }\n static isAttribEnd(c) {\n return c === '>' || SAX.isWhitespace(c);\n }\n static isMatch(regex, c) {\n return regex.test(c);\n }\n static notMatch(regex, c) {\n return !SAX.isMatch(regex, c);\n }\n static qname(name, attribute) {\n const i = name.indexOf(':');\n const qualName = i < 0 ? ['', name] : name.split(':');\n let prefix = qualName[0];\n let local = qualName[1];\n // <x \"xmlns\"=\"http://foo\">\n if (attribute && name === 'xmlns') {\n prefix = 'xmlns';\n local = '';\n }\n return { prefix, local };\n }\n write(chunk) {\n if (this.error) {\n throw this.error;\n }\n if (this.closed) {\n return this.errorFunction('Cannot write after close. Assign an onready handler.');\n }\n if (chunk === null) {\n return this.end();\n }\n if (typeof chunk === 'object') {\n chunk = chunk.toString();\n }\n let i = 0;\n let c;\n while (true) {\n c = SAX.charAt(chunk, i++);\n this.c = c;\n if (!c) {\n break;\n }\n if (this.trackPosition) {\n this.position++;\n if (c === '\\n') {\n this.line++;\n this.column = 0;\n }\n else {\n this.column++;\n }\n }\n switch (this.state) {\n case this.S.BEGIN:\n this.state = this.S.BEGIN_WHITESPACE;\n if (c === '\\uFEFF') {\n continue;\n }\n this.beginWhiteSpace(c);\n continue;\n case this.S.BEGIN_WHITESPACE:\n this.beginWhiteSpace(c);\n continue;\n case this.S.TEXT:\n if (this.sawRoot && !this.closedRoot) {\n const starti = i - 1;\n while (c && c !== '<' && c !== '&') {\n c = SAX.charAt(chunk, i++);\n if (c && this.trackPosition) {\n this.position++;\n if (c === '\\n') {\n this.line++;\n this.column = 0;\n }\n else {\n this.column++;\n }\n }\n }\n this.textNode += chunk.substring(starti, i - 1);\n }\n if (c === '<' && !(this.sawRoot && this.closedRoot && !this.strict)) {\n this.state = this.S.OPEN_WAKA;\n this.startTagPosition = this.position;\n }\n else {\n if (!SAX.isWhitespace(c) && (!this.sawRoot || this.closedRoot)) {\n this.strictFail('Text data outside of root node.');\n }\n if (c === '&') {\n this.state = this.S.TEXT_ENTITY;\n }\n else {\n this.textNode += c;\n }\n }\n continue;\n case this.S.SCRIPT:\n // only non-strict\n if (c === '<') {\n this.state = this.S.SCRIPT_ENDING;\n }\n else {\n this.script += c;\n }\n continue;\n case this.S.SCRIPT_ENDING:\n if (c === '/') {\n this.state = this.S.CLOSE_TAG;\n }\n else {\n this.script += `<${c}`;\n this.state = this.S.SCRIPT;\n }\n continue;\n case this.S.OPEN_WAKA:\n // either a /, ?, !, or text is coming next.\n if (c === '!') {\n this.state = this.S.SGML_DECL;\n this.sgmlDecl = '';\n }\n else if (SAX.isWhitespace(c)) {\n // wait for it...\n }\n else if (SAX.isMatch(nameStart, c)) {\n this.state = this.S.OPEN_TAG;\n this.tagName = c;\n }\n else if (c === '/') {\n this.state = this.S.CLOSE_TAG;\n this.tagName = '';\n }\n else if (c === '?') {\n this.state = this.S.PROC_INST;\n this.procInstName = this.procInstBody = '';\n }\n else {\n this.strictFail('Unencoded <');\n // if there was some whitespace, then add that in.\n if (this.startTagPosition + 1 < this.position) {\n const pad = this.position - this.startTagPosition;\n c = new Array(pad).join(' ') + c;\n }\n this.textNode += `<${c}`;\n this.state = this.S.TEXT;\n }\n continue;\n case this.S.SGML_DECL:\n if ((this.sgmlDecl + c).toUpperCase() === this.CDATA) {\n this.emitNode('onopencdata');\n this.state = this.S.CDATA;\n this.sgmlDecl = '';\n this.cdata = '';\n }\n else if (this.sgmlDecl + c === '--') {\n this.state = this.S.COMMENT;\n this.comment = '';\n this.sgmlDecl = '';\n }\n else if ((this.sgmlDecl + c).toUpperCase() === this.DOCTYPE) {\n this.state = this.S.DOCTYPE;\n if (this.doctype || this.sawRoot) {\n this.strictFail('Inappropriately located doctype declaration');\n }\n this.doctype = '';\n this.sgmlDecl = '';\n }\n else if (c === '>') {\n this.emitNode('onsgmldeclaration', this.sgmlDecl);\n this.sgmlDecl = '';\n this.state = this.S.TEXT;\n }\n else if (SAX.isQuote(c)) {\n this.state = this.S.SGML_DECL_QUOTED;\n this.sgmlDecl += c;\n }\n else {\n this.sgmlDecl += c;\n }\n continue;\n case this.S.SGML_DECL_QUOTED:\n if (c === this.q) {\n this.state = this.S.SGML_DECL;\n this.q = '';\n }\n this.sgmlDecl += c;\n continue;\n case this.S.DOCTYPE:\n if (c === '>') {\n this.state = this.S.TEXT;\n this.emitNode('ondoctype', this.doctype);\n this.doctype = true; // just remember that we saw it.\n }\n else {\n this.doctype += c;\n if (c === '[') {\n this.state = this.S.DOCTYPE_DTD;\n }\n else if (SAX.isQuote(c)) {\n this.state = this.S.DOCTYPE_QUOTED;\n this.q = c;\n }\n }\n continue;\n case this.S.DOCTYPE_QUOTED:\n this.doctype += c;\n if (c === this.q) {\n this.q = '';\n this.state = this.S.DOCTYPE;\n }\n continue;\n case this.S.DOCTYPE_DTD:\n this.doctype += c;\n if (c === ']') {\n this.state = this.S.DOCTYPE;\n }\n else if (SAX.isQuote(c)) {\n this.state = this.S.DOCTYPE_DTD_QUOTED;\n this.q = c;\n }\n continue;\n case this.S.DOCTYPE_DTD_QUOTED:\n this.doctype += c;\n if (c === this.q) {\n this.state = this.S.DOCTYPE_DTD;\n this.q = '';\n }\n continue;\n case this.S.COMMENT:\n if (c === '-') {\n this.state = this.S.COMMENT_ENDING;\n }\n else {\n this.comment += c;\n }\n continue;\n case this.S.COMMENT_ENDING:\n if (c === '-') {\n this.state = this.S.COMMENT_ENDED;\n this.comment = this.textApplyOptions(this.comment);\n if (this.comment) {\n this.emitNode('oncomment', this.comment);\n }\n this.comment = '';\n }\n else {\n this.comment += `-${c}`;\n this.state = this.S.COMMENT;\n }\n continue;\n case this.S.COMMENT_ENDED:\n if (c !== '>') {\n this.strictFail('Malformed comment');\n // allow <!-- blah -- bloo --> in non-strict mode,\n // which is a comment of \" blah -- bloo \"\n this.comment += `--${c}`;\n this.state = this.S.COMMENT;\n }\n else {\n this.state = this.S.TEXT;\n }\n continue;\n case this.S.CDATA:\n if (c === ']') {\n this.state = this.S.CDATA_ENDING;\n }\n else {\n this.cdata += c;\n }\n continue;\n case this.S.CDATA_ENDING:\n if (c === ']') {\n this.state = this.S.CDATA_ENDING_2;\n }\n else {\n this.cdata += `]${c}`;\n this.state = this.S.CDATA;\n }\n continue;\n case this.S.CDATA_ENDING_2:\n if (c === '>') {\n if (this.cdata) {\n this.emitNode('oncdata', this.cdata);\n }\n this.emitNode('onclosecdata');\n this.cdata = '';\n this.state = this.S.TEXT;\n }\n else if (c === ']') {\n this.cdata += ']';\n }\n else {\n this.cdata += `]]${c}`;\n this.state = this.S.CDATA;\n }\n continue;\n case this.S.PROC_INST:\n if (c === '?') {\n this.state = this.S.PROC_INST_ENDING;\n }\n else if (SAX.isWhitespace(c)) {\n this.state = this.S.PROC_INST_BODY;\n }\n else {\n this.procInstName += c;\n }\n continue;\n case this.S.PROC_INST_BODY:\n if (!this.procInstBody && SAX.isWhitespace(c)) {\n continue;\n }\n else if (c === '?') {\n this.state = this.S.PROC_INST_ENDING;\n }\n else {\n this.procInstBody += c;\n }\n continue;\n case this.S.PROC_INST_ENDING:\n if (c === '>') {\n this.emitNode('onprocessinginstruction', {\n name: this.procInstName,\n body: this.procInstBody\n });\n this.procInstName = this.procInstBody = '';\n this.state = this.S.TEXT;\n }\n else {\n this.procInstBody += `?${c}`;\n this.state = this.S.PROC_INST_BODY;\n }\n continue;\n case this.S.OPEN_TAG:\n if (SAX.isMatch(nameBody, c)) {\n this.tagName += c;\n }\n else {\n this.newTag();\n if (c === '>') {\n this.openTag();\n }\n else if (c === '/') {\n this.state = this.S.OPEN_TAG_SLASH;\n }\n else {\n if (!SAX.isWhitespace(c)) {\n this.strictFail('Invalid character in tag name');\n }\n this.state = this.S.ATTRIB;\n }\n }\n continue;\n case this.S.OPEN_TAG_SLASH:\n if (c === '>') {\n this.openTag(true);\n this.closeTag();\n }\n else {\n this.strictFail('Forward-slash in opening tag not followed by >');\n this.state = this.S.ATTRIB;\n }\n continue;\n case this.S.ATTRIB:\n // haven't read the attribute name yet.\n if (SAX.isWhitespace(c)) {\n continue;\n }\n else if (c === '>') {\n this.openTag();\n }\n else if (c === '/') {\n this.state = this.S.OPEN_TAG_SLASH;\n }\n else if (SAX.isMatch(nameStart, c)) {\n this.attribName = c;\n this.attribValue = '';\n this.state = this.S.ATTRIB_NAME;\n }\n else {\n this.strictFail('Invalid attribute name');\n }\n continue;\n case this.S.ATTRIB_NAME:\n if (c === '=') {\n this.state = this.S.ATTRIB_VALUE;\n }\n else if (c === '>') {\n this.strictFail('Attribute without value');\n this.attribValue = this.attribName;\n this.attrib();\n this.openTag();\n }\n else if (SAX.isWhitespace(c)) {\n this.state = this.S.ATTRIB_NAME_SAW_WHITE;\n }\n else if (SAX.isMatch(nameBody, c)) {\n this.attribName += c;\n }\n else {\n this.strictFail('Invalid attribute name');\n }\n continue;\n case this.S.ATTRIB_NAME_SAW_WHITE:\n if (c === '=') {\n this.state = this.S.ATTRIB_VALUE;\n }\n else if (SAX.isWhitespace(c)) {\n continue;\n }\n else {\n this.strictFail('Attribute without value');\n this.tag.attributes[this.attribName] = '';\n this.attribValue = '';\n this.emitNode('onattribute', {\n name: this.attribName,\n value: ''\n });\n this.attribName = '';\n if (c === '>') {\n this.openTag();\n }\n else if (SAX.isMatch(nameStart, c)) {\n this.attribName = c;\n this.state = this.S.ATTRIB_NAME;\n }\n else {\n this.strictFail('Invalid attribute name');\n this.state = this.S.ATTRIB;\n }\n }\n continue;\n case this.S.ATTRIB_VALUE:\n if (SAX.isWhitespace(c)) {\n continue;\n }\n else if (SAX.isQuote(c)) {\n this.q = c;\n this.state = this.S.ATTRIB_VALUE_QUOTED;\n }\n else {\n this.strictFail('Unquoted attribute value');\n this.state = this.S.ATTRIB_VALUE_UNQUOTED;\n this.attribValue = c;\n }\n continue;\n case this.S.ATTRIB_VALUE_QUOTED:\n if (c !== this.q) {\n if (c === '&') {\n this.state = this.S.ATTRIB_VALUE_ENTITY_Q;\n }\n else {\n this.attribValue += c;\n }\n continue;\n }\n this.attrib();\n this.q = '';\n this.state = this.S.ATTRIB_VALUE_CLOSED;\n continue;\n case this.S.ATTRIB_VALUE_CLOSED:\n if (SAX.isWhitespace(c)) {\n this.state = this.S.ATTRIB;\n }\n else if (c === '>') {\n this.openTag();\n }\n else if (c === '/') {\n this.state = this.S.OPEN_TAG_SLASH;\n }\n else if (SAX.isMatch(nameStart, c)) {\n this.strictFail('No whitespace between attributes');\n this.attribName = c;\n this.attribValue = '';\n this.state = this.S.ATTRIB_NAME;\n }\n else {\n this.strictFail('Invalid attribute name');\n }\n continue;\n case this.S.ATTRIB_VALUE_UNQUOTED:\n if (!SAX.isAttribEnd(c)) {\n if (c === '&') {\n this.state = this.S.ATTRIB_VALUE_ENTITY_U;\n }\n else {\n this.attribValue += c;\n }\n continue;\n }\n this.attrib();\n if (c === '>') {\n this.openTag();\n }\n else {\n this.state = this.S.ATTRIB;\n }\n continue;\n case this.S.CLOSE_TAG:\n if (!this.tagName) {\n if (SAX.isWhitespace(c)) {\n continue;\n }\n else if (SAX.notMatch(nameStart, c)) {\n if (this.script) {\n this.script += `</${c}`;\n this.state = this.S.SCRIPT;\n }\n else {\n this.strictFail('Invalid tagname in closing tag.');\n }\n }\n else {\n this.tagName = c;\n }\n }\n else if (c === '>') {\n this.closeTag();\n }\n else if (SAX.isMatch(nameBody, c)) {\n this.tagName += c;\n }\n else if (this.script) {\n this.script += `</${this.tagName}`;\n this.tagName = '';\n this.state = this.S.SCRIPT;\n }\n else {\n if (!SAX.isWhitespace(c)) {\n this.strictFail('Invalid tagname in closing tag');\n }\n this.state = this.S.CLOSE_TAG_SAW_WHITE;\n }\n continue;\n case this.S.CLOSE_TAG_SAW_WHITE:\n if (SAX.isWhitespace(c)) {\n continue;\n }\n if (c === '>') {\n this.closeTag();\n }\n else {\n this.strictFail('Invalid characters in closing tag');\n }\n continue;\n case this.S.TEXT_ENTITY:\n case this.S.ATTRIB_VALUE_ENTITY_Q:\n case this.S.ATTRIB_VALUE_ENTITY_U:\n let returnState;\n let buffer;\n switch (this.state) {\n case this.S.TEXT_ENTITY:\n returnState = this.S.TEXT;\n buffer = 'textNode';\n break;\n case this.S.ATTRIB_VALUE_ENTITY_Q:\n returnState = this.S.ATTRIB_VALUE_QUOTED;\n buffer = 'attribValue';\n break;\n case this.S.ATTRIB_VALUE_ENTITY_U:\n returnState = this.S.ATTRIB_VALUE_UNQUOTED;\n buffer = 'attribValue';\n break;\n default:\n throw new Error(`Unknown state: ${this.state}`);\n }\n if (c === ';') {\n this[buffer] += this.parseEntity();\n this.entity = '';\n this.state = returnState;\n }\n else if (SAX.isMatch(this.entity.length ? entityBody : entityStart, c)) {\n this.entity += c;\n }\n else {\n this.strictFail('Invalid character in entity name');\n this[buffer] += `&${this.entity}${c}`;\n this.entity = '';\n this.state = returnState;\n }\n continue;\n default:\n throw new Error(`Unknown state: ${this.state}`);\n }\n } // while\n if (this.position >= this.bufferCheckPosition) {\n this.checkBufferLength();\n }\n return this;\n }\n emit(event, data) {\n if (this.events.hasOwnProperty(event)) {\n const eventName = event.replace(/^on/, '');\n this.events[event](data, eventName, this);\n }\n }\n clearBuffers() {\n for (let i = 0, l = this.BUFFERS.length; i < l; i++) {\n this[this[i]] = '';\n }\n }\n flushBuffers() {\n this.closeText();\n if (this.cdata !== '') {\n this.emitNode('oncdata', this.cdata);\n this.cdata = '';\n }\n if (this.script !== '') {\n this.emitNode('onscript', this.script);\n this.script = '';\n }\n }\n end() {\n if (this.sawRoot && !this.closedRoot)\n this.strictFail('Unclosed root tag');\n if (this.state !== this.S.BEGIN &&\n this.state !== this.S.BEGIN_WHITESPACE &&\n this.state !== this.S.TEXT) {\n this.errorFunction('Unexpected end');\n }\n this.closeText();\n this.c = '';\n this.closed = true;\n this.emit('onend');\n return new SAXParser(this.opt);\n }\n errorFunction(er) {\n this.closeText();\n if (this.trackPosition) {\n er += `\\nLine: ${this.line}\\nColumn: ${this.column}\\nChar: ${this.c}`;\n }\n const error = new Error(er);\n this.error = error;\n this.emit('onerror', error);\n return this;\n }\n attrib() {\n if (!this.strict) {\n this.attribName = this.attribName[this.looseCase]();\n }\n if (this.attribList.indexOf(this.attribName) !== -1 ||\n this.tag.attributes.hasOwnProperty(this.attribName)) {\n this.attribName = this.attribValue = '';\n return;\n }\n if (this.opt.xmlns) {\n const qn = SAX.qname(this.attribName, true);\n const prefix = qn.prefix;\n const local = qn.local;\n if (prefix === 'xmlns') {\n // namespace binding attribute. push the binding into scope\n if (local === 'xml' && this.attribValue !== this.XML_NAMESPACE) {\n this.strictFail(`xml: prefix must be bound to ${this.XML_NAMESPACE}\\n` + `Actual: ${this.attribValue}`);\n }\n else if (local === 'xmlns' && this.attribValue !== this.XMLNS_NAMESPACE) {\n this.strictFail(`xmlns: prefix must be bound to ${this.XMLNS_NAMESPACE}\\n` +\n `Actual: ${this.attribValue}`);\n }\n else {\n const tag = this.tag;\n const parent = this.tags[this.tags.length - 1] || this;\n if (tag.ns === parent.ns) {\n tag.ns = Object.create(parent.ns);\n }\n tag.ns[local] = this.attribValue;\n }\n }\n // defer onattribute events until all attributes have been seen\n // so any new bindings can take effect. preserve attribute order\n // so deferred events can be emitted in document order\n this.attribList.push([this.attribName, this.attribValue]);\n }\n else {\n // in non-xmlns mode, we can emit the event right away\n this.tag.attributes[this.attribName] = this.attribValue;\n this.emitNode('onattribute', {\n name: this.attribName,\n value: this.attribValue\n });\n }\n this.attribName = this.attribValue = '';\n }\n newTag() {\n if (!this.strict)\n this.tagName = this.tagName[this.looseCase]();\n const parent = this.tags[this.tags.length - 1] || this;\n const tag = (this.tag = { name: this.tagName, attributes: {} });\n // will be overridden if tag contains an xmlns=\"foo\" or xmlns:foo=\"bar\"\n if (this.opt.xmlns) {\n tag.ns = parent.ns;\n }\n this.attribList.length = 0;\n this.emitNode('onopentagstart', tag);\n }\n parseEntity() {\n let entity = this.entity;\n const entityLC = entity.toLowerCase();\n let num = NaN;\n let numStr = '';\n if (this.ENTITIES[entity]) {\n return this.ENTITIES[entity];\n }\n if (this.ENTITIES[entityLC]) {\n return this.ENTITIES[entityLC];\n }\n entity = entityLC;\n if (entity.charAt(0) === '#') {\n if (entity.charAt(1) === 'x') {\n entity = entity.slice(2);\n // TODO: remove tslint:disable\n // tslint:disable-next-line\n num = parseInt(entity, 16);\n numStr = num.toString(16);\n }\n else {\n entity = entity.slice(1);\n // TODO: remove tslint:disable\n // tslint:disable-next-line\n num = parseInt(entity, 10);\n numStr = num.toString(10);\n }\n }\n entity = entity.replace(/^0+/, '');\n if (isNaN(num) || numStr.toLowerCase() !== entity) {\n this.strictFail('Invalid character entity');\n return `&${this.entity};`;\n }\n return String.fromCodePoint(num);\n }\n beginWhiteSpace(c) {\n if (c === '<') {\n this.state = this.S.OPEN_WAKA;\n this.startTagPosition = this.position;\n }\n else if (!SAX.isWhitespace(c)) {\n // have to process this as a text node.\n // weird, but happens.\n this.strictFail('Non-whitespace before first tag.');\n this.textNode = c;\n this.state = this.S.TEXT;\n }\n else {\n }\n }\n strictFail(message) {\n if (typeof this !== 'object' || !(this instanceof SAXParser)) {\n throw new Error('bad call to strictFail');\n }\n if (this.strict) {\n this.errorFunction(message);\n }\n }\n textApplyOptions(text) {\n if (this.opt.trim)\n text = text.trim();\n if (this.opt.normalize)\n text = text.replace(/\\s+/g, ' ');\n return text;\n }\n emitNode(nodeType, data) {\n if (this.textNode)\n this.closeText();\n this.emit(nodeType, data);\n }\n closeText() {\n this.textNode = this.textApplyOptions(this.textNode);\n // TODO: figure out why this.textNode can be \"\" and \"undefined\"\n if (this.textNode !== undefined && this.textNode !== '' && this.textNode !== 'undefined') {\n this.emit('ontext', this.textNode);\n }\n this.textNode = '';\n }\n checkBufferLength() {\n const maxAllowed = Math.max(this.opt.MAX_BUFFER_LENGTH, 10);\n let maxActual = 0;\n for (let i = 0, l = this.BUFFERS.length; i < l; i++) {\n const len = this[this.BUFFERS[i]]?.length || 0;\n if (len > maxAllowed) {\n // Text/cdata nodes can get big, and since they're buffered,\n // we can get here under normal conditions.\n // Avoid issues by emitting the text node now,\n // so at least it won't get any bigger.\n switch (this.BUFFERS[i]) {\n case 'textNode':\n this.closeText();\n break;\n case 'cdata':\n this.emitNode('oncdata', this.cdata);\n this.cdata = '';\n break;\n case 'script':\n this.emitNode('onscript', this.script);\n this.script = '';\n break;\n default:\n this.errorFunction(`Max buffer length exceeded: ${this.BUFFERS[i]}`);\n }\n }\n maxActual = Math.max(maxActual, len);\n }\n // schedule the next check for the earliest possible buffer overrun.\n const m = this.opt.MAX_BUFFER_LENGTH - maxActual;\n this.bufferCheckPosition = m + this.position;\n }\n openTag(selfClosing) {\n if (this.opt.xmlns) {\n // emit namespace binding events\n const tag = this.tag;\n // add namespace info to tag\n const qn = SAX.qname(this.tagName);\n tag.prefix = qn.prefix;\n tag.local = qn.local;\n tag.uri = tag.ns[qn.prefix] || '';\n if (tag.prefix && !tag.uri) {\n this.strictFail(`Unbound namespace prefix: ${JSON.stringify(this.tagName)}`);\n tag.uri = qn.prefix;\n }\n const parent = this.tags[this.tags.length - 1] || this;\n if (tag.ns && parent.ns !== tag.ns) {\n const that = this;\n Object.keys(tag.ns).forEach((p) => {\n that.emitNode('onopennamespace', {\n prefix: p,\n uri: tag.ns[p]\n });\n });\n }\n // handle deferred onattribute events\n // Note: do not apply default ns to attributes:\n // http://www.w3.org/TR/REC-xml-names/#defaulting\n for (let i = 0, l = this.attribList.length; i < l; i++) {\n const nv = this.attribList[i];\n const name = nv[0];\n const value = nv[1];\n const qualName = SAX.qname(name, true);\n const prefix = qualName.prefix;\n const local = qualName.local;\n const uri = prefix === '' ? '' : tag.ns[prefix] || '';\n const a = {\n name,\n value,\n prefix,\n local,\n uri\n };\n // if there's any attributes with an undefined namespace,\n // then fail on them now.\n if (prefix && prefix !== 'xmlns' && !uri) {\n this.strictFail(`Unbound namespace prefix: ${JSON.stringify(prefix)}`);\n a.uri = prefix;\n }\n this.tag.attributes[name] = a;\n this.emitNode('onattribute', a);\n }\n this.attribList.length = 0;\n }\n this.tag.isSelfClosing = Boolean(selfClosing);\n // process the tag\n this.sawRoot = true;\n this.tags.push(this.tag);\n this.emitNode('onopentag', this.tag);\n if (!selfClosing) {\n // special case for <script> in non-strict mode.\n if (!this.noscript && this.tagName.toLowerCase() === 'script') {\n this.state = this.S.SCRIPT;\n }\n else {\n this.state = this.S.TEXT;\n }\n this.tag = null;\n this.tagName = '';\n }\n this.attribName = this.attribValue = '';\n this.attribList.length = 0;\n }\n closeTag() {\n if (!this.tagName) {\n this.strictFail('Weird empty close tag.');\n this.textNode += '</>';\n this.state = this.S.TEXT;\n return;\n }\n if (this.script) {\n if (this.tagName !== 'script') {\n this.script += `</${this.tagName}>`;\n this.tagName = '';\n this.state = this.S.SCRIPT;\n return;\n }\n this.emitNode('onscript', this.script);\n this.script = '';\n }\n // first make sure that the closing tag actually exists.\n // <a><b></c></b></a> will close everything, otherwise.\n let t = this.tags.length;\n let tagName = this.tagName;\n if (!this.strict) {\n tagName = tagName[this.looseCase]();\n }\n while (t--) {\n const close = this.tags[t];\n if (close.name !== tagName) {\n // fail the first time in strict mode\n this.strictFail('Unexpected close tag');\n }\n else {\n break;\n }\n }\n // didn't find it. we already failed for strict, so just abort.\n if (t < 0) {\n this.strictFail(`Unmatched closing tag: ${this.tagName}`);\n this.textNode += `</${this.tagName}>`;\n this.state = this.S.TEXT;\n return;\n }\n this.tagName = tagName;\n let s = this.tags.length;\n while (s-- > t) {\n const tag = (this.tag = this.tags.pop());\n this.tagName = this.tag.name;\n this.emitNode('onclosetag', this.tagName);\n const x = {};\n for (const i in tag.ns) {\n if (tag.ns.hasOwnProperty(i)) {\n x[i] = tag.ns[i];\n }\n }\n const parent = this.tags[this.tags.length - 1] || this;\n if (this.opt.xmlns && tag.ns !== parent.ns) {\n // remove namespace bindings introduced by tag\n const that = this;\n Object.keys(tag.ns).forEach((p) => {\n const n = tag.ns[p];\n that.emitNode('onclosenamespace', { prefix: p, uri: n });\n });\n }\n }\n if (t === 0)\n this.closedRoot = true;\n this.tagName = this.attribValue = this.attribName = '';\n this.attribList.length = 0;\n this.state = this.S.TEXT;\n }\n}\n/**\n *\n * @todo Weird inheritance, with some variables initialized in subclass\n */\nexport class SAXParser extends SAX {\n static ENTITIES = ENTITIES;\n opt = DEFAULT_SAX_PARSER_OPTIONS;\n events = DEFAULT_SAX_EVENTS;\n constructor(opt) {\n super();\n this.clearBuffers();\n this.opt = opt = { ...this.opt, ...opt };\n this.events = { ...this.events, ...opt };\n this.q = this.c = '';\n this.opt.lowercase = this.opt.lowercase || this.opt.lowercasetags;\n this.bufferCheckPosition = this.opt.MAX_BUFFER_LENGTH;\n this.looseCase = this.opt.lowercase ? 'toLo