not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1577 lines
43 KiB

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