sax.js 43 KB

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