Tokenizer.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. var decode_codepoint_1 = __importDefault(require("entities/lib/decode_codepoint"));
  7. var decode_1 = require("entities/lib/decode");
  8. function isWhitespace(c) {
  9. return (c === 32 /* Space */ ||
  10. c === 10 /* NewLine */ ||
  11. c === 9 /* Tab */ ||
  12. c === 12 /* FormFeed */ ||
  13. c === 13 /* CarriageReturn */);
  14. }
  15. function isEndOfTagSection(c) {
  16. return c === 47 /* Slash */ || c === 62 /* Gt */ || isWhitespace(c);
  17. }
  18. function isNumber(c) {
  19. return c >= 48 /* Zero */ && c <= 57 /* Nine */;
  20. }
  21. function isASCIIAlpha(c) {
  22. return ((c >= 97 /* LowerA */ && c <= 122 /* LowerZ */) ||
  23. (c >= 65 /* UpperA */ && c <= 90 /* UpperZ */));
  24. }
  25. /**
  26. * Sequences used to match longer strings.
  27. *
  28. * We don't have `Script`, `Style`, or `Title` here. Instead, we re-use the *End
  29. * sequences with an increased offset.
  30. */
  31. var Sequences = {
  32. Cdata: new Uint16Array([0x43, 0x44, 0x41, 0x54, 0x41, 0x5b]),
  33. CdataEnd: new Uint16Array([0x5d, 0x5d, 0x3e]),
  34. CommentEnd: new Uint16Array([0x2d, 0x2d, 0x3e]),
  35. ScriptEnd: new Uint16Array([
  36. 0x3c, 0x2f, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74,
  37. ]),
  38. StyleEnd: new Uint16Array([0x3c, 0x2f, 0x73, 0x74, 0x79, 0x6c, 0x65]),
  39. TitleEnd: new Uint16Array([0x3c, 0x2f, 0x74, 0x69, 0x74, 0x6c, 0x65]), // `</title`
  40. };
  41. var Tokenizer = /** @class */ (function () {
  42. function Tokenizer(_a, cbs) {
  43. var _b = _a.xmlMode, xmlMode = _b === void 0 ? false : _b, _c = _a.decodeEntities, decodeEntities = _c === void 0 ? true : _c;
  44. this.cbs = cbs;
  45. /** The current state the tokenizer is in. */
  46. this._state = 1 /* Text */;
  47. /** The read buffer. */
  48. this.buffer = "";
  49. /** The beginning of the section that is currently being read. */
  50. this.sectionStart = 0;
  51. /** The index within the buffer that we are currently looking at. */
  52. this._index = 0;
  53. /**
  54. * Data that has already been processed will be removed from the buffer occasionally.
  55. * `_bufferOffset` keeps track of how many characters have been removed, to make sure position information is accurate.
  56. */
  57. this.bufferOffset = 0;
  58. /** Some behavior, eg. when decoding entities, is done while we are in another state. This keeps track of the other state type. */
  59. this.baseState = 1 /* Text */;
  60. /** For special parsing behavior inside of script and style tags. */
  61. this.isSpecial = false;
  62. /** Indicates whether the tokenizer has been paused. */
  63. this.running = true;
  64. /** Indicates whether the tokenizer has finished running / `.end` has been called. */
  65. this.ended = false;
  66. this.sequenceIndex = 0;
  67. this.trieIndex = 0;
  68. this.trieCurrent = 0;
  69. this.trieResult = null;
  70. this.entityExcess = 0;
  71. this.xmlMode = xmlMode;
  72. this.decodeEntities = decodeEntities;
  73. this.entityTrie = xmlMode ? decode_1.xmlDecodeTree : decode_1.htmlDecodeTree;
  74. }
  75. Tokenizer.prototype.reset = function () {
  76. this._state = 1 /* Text */;
  77. this.buffer = "";
  78. this.sectionStart = 0;
  79. this._index = 0;
  80. this.bufferOffset = 0;
  81. this.baseState = 1 /* Text */;
  82. this.currentSequence = undefined;
  83. this.running = true;
  84. this.ended = false;
  85. };
  86. Tokenizer.prototype.write = function (chunk) {
  87. if (this.ended)
  88. return this.cbs.onerror(Error(".write() after done!"));
  89. this.buffer += chunk;
  90. this.parse();
  91. };
  92. Tokenizer.prototype.end = function (chunk) {
  93. if (this.ended)
  94. return this.cbs.onerror(Error(".end() after done!"));
  95. if (chunk)
  96. this.write(chunk);
  97. this.ended = true;
  98. if (this.running)
  99. this.finish();
  100. };
  101. Tokenizer.prototype.pause = function () {
  102. this.running = false;
  103. };
  104. Tokenizer.prototype.resume = function () {
  105. this.running = true;
  106. if (this._index < this.buffer.length) {
  107. this.parse();
  108. }
  109. if (this.ended) {
  110. this.finish();
  111. }
  112. };
  113. /**
  114. * The start of the current section.
  115. */
  116. Tokenizer.prototype.getAbsoluteSectionStart = function () {
  117. return this.sectionStart + this.bufferOffset;
  118. };
  119. /**
  120. * The current index within all of the written data.
  121. */
  122. Tokenizer.prototype.getAbsoluteIndex = function () {
  123. return this.bufferOffset + this._index;
  124. };
  125. Tokenizer.prototype.stateText = function (c) {
  126. if (c === 60 /* Lt */ ||
  127. (!this.decodeEntities && this.fastForwardTo(60 /* Lt */))) {
  128. if (this._index > this.sectionStart) {
  129. this.cbs.ontext(this.getSection());
  130. }
  131. this._state = 2 /* BeforeTagName */;
  132. this.sectionStart = this._index;
  133. }
  134. else if (this.decodeEntities && c === 38 /* Amp */) {
  135. this._state = 25 /* BeforeEntity */;
  136. }
  137. };
  138. Tokenizer.prototype.stateSpecialStartSequence = function (c) {
  139. var isEnd = this.sequenceIndex === this.currentSequence.length;
  140. var isMatch = isEnd
  141. ? // If we are at the end of the sequence, make sure the tag name has ended
  142. isEndOfTagSection(c)
  143. : // Otherwise, do a case-insensitive comparison
  144. (c | 0x20) === this.currentSequence[this.sequenceIndex];
  145. if (!isMatch) {
  146. this.isSpecial = false;
  147. }
  148. else if (!isEnd) {
  149. this.sequenceIndex++;
  150. return;
  151. }
  152. this.sequenceIndex = 0;
  153. this._state = 3 /* InTagName */;
  154. this.stateInTagName(c);
  155. };
  156. /** Look for an end tag. For <title> tags, also decode entities. */
  157. Tokenizer.prototype.stateInSpecialTag = function (c) {
  158. if (this.sequenceIndex === this.currentSequence.length) {
  159. if (c === 62 /* Gt */ || isWhitespace(c)) {
  160. var endOfText = this._index - this.currentSequence.length;
  161. if (this.sectionStart < endOfText) {
  162. // Spoof the index so that reported locations match up.
  163. var actualIndex = this._index;
  164. this._index = endOfText;
  165. this.cbs.ontext(this.getSection());
  166. this._index = actualIndex;
  167. }
  168. this.isSpecial = false;
  169. this.sectionStart = endOfText + 2; // Skip over the `</`
  170. this.stateInClosingTagName(c);
  171. return; // We are done; skip the rest of the function.
  172. }
  173. this.sequenceIndex = 0;
  174. }
  175. if ((c | 0x20) === this.currentSequence[this.sequenceIndex]) {
  176. this.sequenceIndex += 1;
  177. }
  178. else if (this.sequenceIndex === 0) {
  179. if (this.currentSequence === Sequences.TitleEnd) {
  180. // We have to parse entities in <title> tags.
  181. if (this.decodeEntities && c === 38 /* Amp */) {
  182. this._state = 25 /* BeforeEntity */;
  183. }
  184. }
  185. else if (this.fastForwardTo(60 /* Lt */)) {
  186. // Outside of <title> tags, we can fast-forward.
  187. this.sequenceIndex = 1;
  188. }
  189. }
  190. else {
  191. // If we see a `<`, set the sequence index to 1; useful for eg. `<</script>`.
  192. this.sequenceIndex = Number(c === 60 /* Lt */);
  193. }
  194. };
  195. Tokenizer.prototype.stateCDATASequence = function (c) {
  196. if (c === Sequences.Cdata[this.sequenceIndex]) {
  197. if (++this.sequenceIndex === Sequences.Cdata.length) {
  198. this._state = 21 /* InCommentLike */;
  199. this.currentSequence = Sequences.CdataEnd;
  200. this.sequenceIndex = 0;
  201. this.sectionStart = this._index + 1;
  202. }
  203. }
  204. else {
  205. this.sequenceIndex = 0;
  206. this._state = 16 /* InDeclaration */;
  207. this.stateInDeclaration(c); // Reconsume the character
  208. }
  209. };
  210. /**
  211. * When we wait for one specific character, we can speed things up
  212. * by skipping through the buffer until we find it.
  213. *
  214. * @returns Whether the character was found.
  215. */
  216. Tokenizer.prototype.fastForwardTo = function (c) {
  217. while (++this._index < this.buffer.length) {
  218. if (this.buffer.charCodeAt(this._index) === c) {
  219. return true;
  220. }
  221. }
  222. /*
  223. * We increment the index at the end of the `parse` loop,
  224. * so set it to `buffer.length - 1` here.
  225. *
  226. * TODO: Refactor `parse` to increment index before calling states.
  227. */
  228. this._index = this.buffer.length - 1;
  229. return false;
  230. };
  231. /**
  232. * Comments and CDATA end with `-->` and `]]>`.
  233. *
  234. * Their common qualities are:
  235. * - Their end sequences have a distinct character they start with.
  236. * - That character is then repeated, so we have to check multiple repeats.
  237. * - All characters but the start character of the sequence can be skipped.
  238. */
  239. Tokenizer.prototype.stateInCommentLike = function (c) {
  240. if (c === this.currentSequence[this.sequenceIndex]) {
  241. if (++this.sequenceIndex === this.currentSequence.length) {
  242. // Remove 2 trailing chars
  243. var section = this.buffer.slice(this.sectionStart, this._index - 2);
  244. if (this.currentSequence === Sequences.CdataEnd) {
  245. this.cbs.oncdata(section);
  246. }
  247. else {
  248. this.cbs.oncomment(section);
  249. }
  250. this.sequenceIndex = 0;
  251. this.sectionStart = this._index + 1;
  252. this._state = 1 /* Text */;
  253. }
  254. }
  255. else if (this.sequenceIndex === 0) {
  256. // Fast-forward to the first character of the sequence
  257. if (this.fastForwardTo(this.currentSequence[0])) {
  258. this.sequenceIndex = 1;
  259. }
  260. }
  261. else if (c !== this.currentSequence[this.sequenceIndex - 1]) {
  262. // Allow long sequences, eg. --->, ]]]>
  263. this.sequenceIndex = 0;
  264. }
  265. };
  266. /**
  267. * HTML only allows ASCII alpha characters (a-z and A-Z) at the beginning of a tag name.
  268. *
  269. * XML allows a lot more characters here (@see https://www.w3.org/TR/REC-xml/#NT-NameStartChar).
  270. * We allow anything that wouldn't end the tag.
  271. */
  272. Tokenizer.prototype.isTagStartChar = function (c) {
  273. return this.xmlMode ? !isEndOfTagSection(c) : isASCIIAlpha(c);
  274. };
  275. Tokenizer.prototype.startSpecial = function (sequence, offset) {
  276. this.isSpecial = true;
  277. this.currentSequence = sequence;
  278. this.sequenceIndex = offset;
  279. this._state = 23 /* SpecialStartSequence */;
  280. };
  281. Tokenizer.prototype.stateBeforeTagName = function (c) {
  282. if (c === 33 /* ExclamationMark */) {
  283. this._state = 15 /* BeforeDeclaration */;
  284. this.sectionStart = this._index + 1;
  285. }
  286. else if (c === 63 /* Questionmark */) {
  287. this._state = 17 /* InProcessingInstruction */;
  288. this.sectionStart = this._index + 1;
  289. }
  290. else if (this.isTagStartChar(c)) {
  291. var lower = c | 0x20;
  292. this.sectionStart = this._index;
  293. if (!this.xmlMode && lower === Sequences.TitleEnd[2]) {
  294. this.startSpecial(Sequences.TitleEnd, 3);
  295. }
  296. else {
  297. this._state =
  298. !this.xmlMode && lower === Sequences.ScriptEnd[2]
  299. ? 22 /* BeforeSpecialS */
  300. : 3 /* InTagName */;
  301. }
  302. }
  303. else if (c === 47 /* Slash */) {
  304. this._state = 5 /* BeforeClosingTagName */;
  305. }
  306. else {
  307. this._state = 1 /* Text */;
  308. this.stateText(c);
  309. }
  310. };
  311. Tokenizer.prototype.stateInTagName = function (c) {
  312. if (isEndOfTagSection(c)) {
  313. this.cbs.onopentagname(this.getSection());
  314. this.sectionStart = -1;
  315. this._state = 8 /* BeforeAttributeName */;
  316. this.stateBeforeAttributeName(c);
  317. }
  318. };
  319. Tokenizer.prototype.stateBeforeClosingTagName = function (c) {
  320. if (isWhitespace(c)) {
  321. // Ignore
  322. }
  323. else if (c === 62 /* Gt */) {
  324. this._state = 1 /* Text */;
  325. }
  326. else {
  327. this._state = this.isTagStartChar(c)
  328. ? 6 /* InClosingTagName */
  329. : 20 /* InSpecialComment */;
  330. this.sectionStart = this._index;
  331. }
  332. };
  333. Tokenizer.prototype.stateInClosingTagName = function (c) {
  334. if (c === 62 /* Gt */ || isWhitespace(c)) {
  335. this.cbs.onclosetag(this.getSection());
  336. this.sectionStart = -1;
  337. this._state = 7 /* AfterClosingTagName */;
  338. this.stateAfterClosingTagName(c);
  339. }
  340. };
  341. Tokenizer.prototype.stateAfterClosingTagName = function (c) {
  342. // Skip everything until ">"
  343. if (c === 62 /* Gt */ || this.fastForwardTo(62 /* Gt */)) {
  344. this._state = 1 /* Text */;
  345. this.sectionStart = this._index + 1;
  346. }
  347. };
  348. Tokenizer.prototype.stateBeforeAttributeName = function (c) {
  349. if (c === 62 /* Gt */) {
  350. this.cbs.onopentagend();
  351. if (this.isSpecial) {
  352. this._state = 24 /* InSpecialTag */;
  353. this.sequenceIndex = 0;
  354. }
  355. else {
  356. this._state = 1 /* Text */;
  357. }
  358. this.baseState = this._state;
  359. this.sectionStart = this._index + 1;
  360. }
  361. else if (c === 47 /* Slash */) {
  362. this._state = 4 /* InSelfClosingTag */;
  363. }
  364. else if (!isWhitespace(c)) {
  365. this._state = 9 /* InAttributeName */;
  366. this.sectionStart = this._index;
  367. }
  368. };
  369. Tokenizer.prototype.stateInSelfClosingTag = function (c) {
  370. if (c === 62 /* Gt */) {
  371. this.cbs.onselfclosingtag();
  372. this._state = 1 /* Text */;
  373. this.baseState = 1 /* Text */;
  374. this.sectionStart = this._index + 1;
  375. this.isSpecial = false; // Reset special state, in case of self-closing special tags
  376. }
  377. else if (!isWhitespace(c)) {
  378. this._state = 8 /* BeforeAttributeName */;
  379. this.stateBeforeAttributeName(c);
  380. }
  381. };
  382. Tokenizer.prototype.stateInAttributeName = function (c) {
  383. if (c === 61 /* Eq */ || isEndOfTagSection(c)) {
  384. this.cbs.onattribname(this.getSection());
  385. this.sectionStart = -1;
  386. this._state = 10 /* AfterAttributeName */;
  387. this.stateAfterAttributeName(c);
  388. }
  389. };
  390. Tokenizer.prototype.stateAfterAttributeName = function (c) {
  391. if (c === 61 /* Eq */) {
  392. this._state = 11 /* BeforeAttributeValue */;
  393. }
  394. else if (c === 47 /* Slash */ || c === 62 /* Gt */) {
  395. this.cbs.onattribend(undefined);
  396. this._state = 8 /* BeforeAttributeName */;
  397. this.stateBeforeAttributeName(c);
  398. }
  399. else if (!isWhitespace(c)) {
  400. this.cbs.onattribend(undefined);
  401. this._state = 9 /* InAttributeName */;
  402. this.sectionStart = this._index;
  403. }
  404. };
  405. Tokenizer.prototype.stateBeforeAttributeValue = function (c) {
  406. if (c === 34 /* DoubleQuote */) {
  407. this._state = 12 /* InAttributeValueDq */;
  408. this.sectionStart = this._index + 1;
  409. }
  410. else if (c === 39 /* SingleQuote */) {
  411. this._state = 13 /* InAttributeValueSq */;
  412. this.sectionStart = this._index + 1;
  413. }
  414. else if (!isWhitespace(c)) {
  415. this.sectionStart = this._index;
  416. this._state = 14 /* InAttributeValueNq */;
  417. this.stateInAttributeValueNoQuotes(c); // Reconsume token
  418. }
  419. };
  420. Tokenizer.prototype.handleInAttributeValue = function (c, quote) {
  421. if (c === quote ||
  422. (!this.decodeEntities && this.fastForwardTo(quote))) {
  423. this.cbs.onattribdata(this.getSection());
  424. this.sectionStart = -1;
  425. this.cbs.onattribend(String.fromCharCode(quote));
  426. this._state = 8 /* BeforeAttributeName */;
  427. }
  428. else if (this.decodeEntities && c === 38 /* Amp */) {
  429. this.baseState = this._state;
  430. this._state = 25 /* BeforeEntity */;
  431. }
  432. };
  433. Tokenizer.prototype.stateInAttributeValueDoubleQuotes = function (c) {
  434. this.handleInAttributeValue(c, 34 /* DoubleQuote */);
  435. };
  436. Tokenizer.prototype.stateInAttributeValueSingleQuotes = function (c) {
  437. this.handleInAttributeValue(c, 39 /* SingleQuote */);
  438. };
  439. Tokenizer.prototype.stateInAttributeValueNoQuotes = function (c) {
  440. if (isWhitespace(c) || c === 62 /* Gt */) {
  441. this.cbs.onattribdata(this.getSection());
  442. this.sectionStart = -1;
  443. this.cbs.onattribend(null);
  444. this._state = 8 /* BeforeAttributeName */;
  445. this.stateBeforeAttributeName(c);
  446. }
  447. else if (this.decodeEntities && c === 38 /* Amp */) {
  448. this.baseState = this._state;
  449. this._state = 25 /* BeforeEntity */;
  450. }
  451. };
  452. Tokenizer.prototype.stateBeforeDeclaration = function (c) {
  453. if (c === 91 /* OpeningSquareBracket */) {
  454. this._state = 19 /* CDATASequence */;
  455. this.sequenceIndex = 0;
  456. }
  457. else {
  458. this._state =
  459. c === 45 /* Dash */
  460. ? 18 /* BeforeComment */
  461. : 16 /* InDeclaration */;
  462. }
  463. };
  464. Tokenizer.prototype.stateInDeclaration = function (c) {
  465. if (c === 62 /* Gt */ || this.fastForwardTo(62 /* Gt */)) {
  466. this.cbs.ondeclaration(this.getSection());
  467. this._state = 1 /* Text */;
  468. this.sectionStart = this._index + 1;
  469. }
  470. };
  471. Tokenizer.prototype.stateInProcessingInstruction = function (c) {
  472. if (c === 62 /* Gt */ || this.fastForwardTo(62 /* Gt */)) {
  473. this.cbs.onprocessinginstruction(this.getSection());
  474. this._state = 1 /* Text */;
  475. this.sectionStart = this._index + 1;
  476. }
  477. };
  478. Tokenizer.prototype.stateBeforeComment = function (c) {
  479. if (c === 45 /* Dash */) {
  480. this._state = 21 /* InCommentLike */;
  481. this.currentSequence = Sequences.CommentEnd;
  482. // Allow short comments (eg. <!-->)
  483. this.sequenceIndex = 2;
  484. this.sectionStart = this._index + 1;
  485. }
  486. else {
  487. this._state = 16 /* InDeclaration */;
  488. }
  489. };
  490. Tokenizer.prototype.stateInSpecialComment = function (c) {
  491. if (c === 62 /* Gt */ || this.fastForwardTo(62 /* Gt */)) {
  492. this.cbs.oncomment(this.getSection());
  493. this._state = 1 /* Text */;
  494. this.sectionStart = this._index + 1;
  495. }
  496. };
  497. Tokenizer.prototype.stateBeforeSpecialS = function (c) {
  498. var lower = c | 0x20;
  499. if (lower === Sequences.ScriptEnd[3]) {
  500. this.startSpecial(Sequences.ScriptEnd, 4);
  501. }
  502. else if (lower === Sequences.StyleEnd[3]) {
  503. this.startSpecial(Sequences.StyleEnd, 4);
  504. }
  505. else {
  506. this._state = 3 /* InTagName */;
  507. this.stateInTagName(c); // Consume the token again
  508. }
  509. };
  510. Tokenizer.prototype.stateBeforeEntity = function (c) {
  511. // Start excess with 1 to include the '&'
  512. this.entityExcess = 1;
  513. if (c === 35 /* Num */) {
  514. this._state = 26 /* BeforeNumericEntity */;
  515. }
  516. else if (c === 38 /* Amp */) {
  517. // We have two `&` characters in a row. Stay in the current state.
  518. }
  519. else {
  520. this.trieIndex = 0;
  521. this.trieCurrent = this.entityTrie[0];
  522. this.trieResult = null;
  523. this._state = 27 /* InNamedEntity */;
  524. this.stateInNamedEntity(c);
  525. }
  526. };
  527. Tokenizer.prototype.stateInNamedEntity = function (c) {
  528. this.entityExcess += 1;
  529. this.trieIndex = (0, decode_1.determineBranch)(this.entityTrie, this.trieCurrent, this.trieIndex + 1, c);
  530. if (this.trieIndex < 0) {
  531. this.emitNamedEntity();
  532. this._index--;
  533. return;
  534. }
  535. this.trieCurrent = this.entityTrie[this.trieIndex];
  536. // If the branch is a value, store it and continue
  537. if (this.trieCurrent & decode_1.BinTrieFlags.HAS_VALUE) {
  538. // If we have a legacy entity while parsing strictly, just skip the number of bytes
  539. if (!this.allowLegacyEntity() && c !== 59 /* Semi */) {
  540. // No need to consider multi-byte values, as the legacy entity is always a single byte
  541. this.trieIndex += 1;
  542. }
  543. else {
  544. // Add 1 as we have already incremented the excess
  545. var entityStart = this._index - this.entityExcess + 1;
  546. if (entityStart > this.sectionStart) {
  547. this.emitPartial(this.buffer.substring(this.sectionStart, entityStart));
  548. }
  549. // If this is a surrogate pair, combine the higher bits from the node with the next byte
  550. this.trieResult =
  551. this.trieCurrent & decode_1.BinTrieFlags.MULTI_BYTE
  552. ? String.fromCharCode(this.entityTrie[++this.trieIndex], this.entityTrie[++this.trieIndex])
  553. : String.fromCharCode(this.entityTrie[++this.trieIndex]);
  554. this.entityExcess = 0;
  555. this.sectionStart = this._index + 1;
  556. }
  557. }
  558. };
  559. Tokenizer.prototype.emitNamedEntity = function () {
  560. if (this.trieResult) {
  561. this.emitPartial(this.trieResult);
  562. }
  563. this._state = this.baseState;
  564. };
  565. Tokenizer.prototype.stateBeforeNumericEntity = function (c) {
  566. if ((c | 0x20) === 120 /* LowerX */) {
  567. this.entityExcess++;
  568. this._state = 29 /* InHexEntity */;
  569. }
  570. else {
  571. this._state = 28 /* InNumericEntity */;
  572. this.stateInNumericEntity(c);
  573. }
  574. };
  575. Tokenizer.prototype.decodeNumericEntity = function (base, strict) {
  576. var entityStart = this._index - this.entityExcess - 1;
  577. var numberStart = entityStart + 2 + (base >> 4);
  578. if (numberStart !== this._index) {
  579. // Emit leading data if any
  580. if (entityStart > this.sectionStart) {
  581. this.emitPartial(this.buffer.substring(this.sectionStart, entityStart));
  582. }
  583. // Parse entity
  584. var entity = this.buffer.substring(numberStart, this._index);
  585. var parsed = parseInt(entity, base);
  586. this.emitPartial((0, decode_codepoint_1.default)(parsed));
  587. this.sectionStart = this._index + Number(strict);
  588. }
  589. this._state = this.baseState;
  590. };
  591. Tokenizer.prototype.stateInNumericEntity = function (c) {
  592. if (c === 59 /* Semi */) {
  593. this.decodeNumericEntity(10, true);
  594. }
  595. else if (!isNumber(c)) {
  596. if (this.allowLegacyEntity()) {
  597. this.decodeNumericEntity(10, false);
  598. }
  599. else {
  600. this._state = this.baseState;
  601. }
  602. this._index--;
  603. }
  604. else {
  605. this.entityExcess++;
  606. }
  607. };
  608. Tokenizer.prototype.stateInHexEntity = function (c) {
  609. if (c === 59 /* Semi */) {
  610. this.decodeNumericEntity(16, true);
  611. }
  612. else if ((c < 97 /* LowerA */ || c > 102 /* LowerF */) &&
  613. (c < 65 /* UpperA */ || c > 70 /* UpperF */) &&
  614. !isNumber(c)) {
  615. if (this.allowLegacyEntity()) {
  616. this.decodeNumericEntity(16, false);
  617. }
  618. else {
  619. this._state = this.baseState;
  620. }
  621. this._index--;
  622. }
  623. else {
  624. this.entityExcess++;
  625. }
  626. };
  627. Tokenizer.prototype.allowLegacyEntity = function () {
  628. return (!this.xmlMode &&
  629. (this.baseState === 1 /* Text */ ||
  630. this.baseState === 24 /* InSpecialTag */));
  631. };
  632. /**
  633. * Remove data that has already been consumed from the buffer.
  634. */
  635. Tokenizer.prototype.cleanup = function () {
  636. // If we are inside of text, emit what we already have.
  637. if (this.running &&
  638. this.sectionStart !== this._index &&
  639. (this._state === 1 /* Text */ ||
  640. (this._state === 24 /* InSpecialTag */ &&
  641. this.sequenceIndex === 0))) {
  642. // TODO: We could emit attribute data here as well.
  643. this.cbs.ontext(this.buffer.substr(this.sectionStart));
  644. this.sectionStart = this._index;
  645. }
  646. var start = this.sectionStart < 0 ? this._index : this.sectionStart;
  647. this.buffer =
  648. start === this.buffer.length ? "" : this.buffer.substr(start);
  649. this._index -= start;
  650. this.bufferOffset += start;
  651. if (this.sectionStart > 0) {
  652. this.sectionStart = 0;
  653. }
  654. };
  655. Tokenizer.prototype.shouldContinue = function () {
  656. return this._index < this.buffer.length && this.running;
  657. };
  658. /**
  659. * Iterates through the buffer, calling the function corresponding to the current state.
  660. *
  661. * States that are more likely to be hit are higher up, as a performance improvement.
  662. */
  663. Tokenizer.prototype.parse = function () {
  664. while (this.shouldContinue()) {
  665. var c = this.buffer.charCodeAt(this._index);
  666. if (this._state === 1 /* Text */) {
  667. this.stateText(c);
  668. }
  669. else if (this._state === 23 /* SpecialStartSequence */) {
  670. this.stateSpecialStartSequence(c);
  671. }
  672. else if (this._state === 24 /* InSpecialTag */) {
  673. this.stateInSpecialTag(c);
  674. }
  675. else if (this._state === 19 /* CDATASequence */) {
  676. this.stateCDATASequence(c);
  677. }
  678. else if (this._state === 12 /* InAttributeValueDq */) {
  679. this.stateInAttributeValueDoubleQuotes(c);
  680. }
  681. else if (this._state === 9 /* InAttributeName */) {
  682. this.stateInAttributeName(c);
  683. }
  684. else if (this._state === 21 /* InCommentLike */) {
  685. this.stateInCommentLike(c);
  686. }
  687. else if (this._state === 20 /* InSpecialComment */) {
  688. this.stateInSpecialComment(c);
  689. }
  690. else if (this._state === 8 /* BeforeAttributeName */) {
  691. this.stateBeforeAttributeName(c);
  692. }
  693. else if (this._state === 3 /* InTagName */) {
  694. this.stateInTagName(c);
  695. }
  696. else if (this._state === 6 /* InClosingTagName */) {
  697. this.stateInClosingTagName(c);
  698. }
  699. else if (this._state === 2 /* BeforeTagName */) {
  700. this.stateBeforeTagName(c);
  701. }
  702. else if (this._state === 10 /* AfterAttributeName */) {
  703. this.stateAfterAttributeName(c);
  704. }
  705. else if (this._state === 13 /* InAttributeValueSq */) {
  706. this.stateInAttributeValueSingleQuotes(c);
  707. }
  708. else if (this._state === 11 /* BeforeAttributeValue */) {
  709. this.stateBeforeAttributeValue(c);
  710. }
  711. else if (this._state === 5 /* BeforeClosingTagName */) {
  712. this.stateBeforeClosingTagName(c);
  713. }
  714. else if (this._state === 7 /* AfterClosingTagName */) {
  715. this.stateAfterClosingTagName(c);
  716. }
  717. else if (this._state === 22 /* BeforeSpecialS */) {
  718. this.stateBeforeSpecialS(c);
  719. }
  720. else if (this._state === 14 /* InAttributeValueNq */) {
  721. this.stateInAttributeValueNoQuotes(c);
  722. }
  723. else if (this._state === 4 /* InSelfClosingTag */) {
  724. this.stateInSelfClosingTag(c);
  725. }
  726. else if (this._state === 16 /* InDeclaration */) {
  727. this.stateInDeclaration(c);
  728. }
  729. else if (this._state === 15 /* BeforeDeclaration */) {
  730. this.stateBeforeDeclaration(c);
  731. }
  732. else if (this._state === 18 /* BeforeComment */) {
  733. this.stateBeforeComment(c);
  734. }
  735. else if (this._state === 17 /* InProcessingInstruction */) {
  736. this.stateInProcessingInstruction(c);
  737. }
  738. else if (this._state === 27 /* InNamedEntity */) {
  739. this.stateInNamedEntity(c);
  740. }
  741. else if (this._state === 25 /* BeforeEntity */) {
  742. this.stateBeforeEntity(c);
  743. }
  744. else if (this._state === 29 /* InHexEntity */) {
  745. this.stateInHexEntity(c);
  746. }
  747. else if (this._state === 28 /* InNumericEntity */) {
  748. this.stateInNumericEntity(c);
  749. }
  750. else {
  751. // `this._state === State.BeforeNumericEntity`
  752. this.stateBeforeNumericEntity(c);
  753. }
  754. this._index++;
  755. }
  756. this.cleanup();
  757. };
  758. Tokenizer.prototype.finish = function () {
  759. if (this._state === 27 /* InNamedEntity */) {
  760. this.emitNamedEntity();
  761. }
  762. // If there is remaining data, emit it in a reasonable way
  763. if (this.sectionStart < this._index) {
  764. this.handleTrailingData();
  765. }
  766. this.cbs.onend();
  767. };
  768. /** Handle any trailing data. */
  769. Tokenizer.prototype.handleTrailingData = function () {
  770. var data = this.buffer.substr(this.sectionStart);
  771. if (this._state === 21 /* InCommentLike */) {
  772. if (this.currentSequence === Sequences.CdataEnd) {
  773. this.cbs.oncdata(data);
  774. }
  775. else {
  776. this.cbs.oncomment(data);
  777. }
  778. }
  779. else if (this._state === 28 /* InNumericEntity */ &&
  780. this.allowLegacyEntity()) {
  781. this.decodeNumericEntity(10, false);
  782. // All trailing data will have been consumed
  783. }
  784. else if (this._state === 29 /* InHexEntity */ &&
  785. this.allowLegacyEntity()) {
  786. this.decodeNumericEntity(16, false);
  787. // All trailing data will have been consumed
  788. }
  789. else if (this._state === 3 /* InTagName */ ||
  790. this._state === 8 /* BeforeAttributeName */ ||
  791. this._state === 11 /* BeforeAttributeValue */ ||
  792. this._state === 10 /* AfterAttributeName */ ||
  793. this._state === 9 /* InAttributeName */ ||
  794. this._state === 13 /* InAttributeValueSq */ ||
  795. this._state === 12 /* InAttributeValueDq */ ||
  796. this._state === 14 /* InAttributeValueNq */ ||
  797. this._state === 6 /* InClosingTagName */) {
  798. /*
  799. * If we are currently in an opening or closing tag, us not calling the
  800. * respective callback signals that the tag should be ignored.
  801. */
  802. }
  803. else {
  804. this.cbs.ontext(data);
  805. }
  806. };
  807. Tokenizer.prototype.getSection = function () {
  808. return this.buffer.substring(this.sectionStart, this._index);
  809. };
  810. Tokenizer.prototype.emitPartial = function (value) {
  811. if (this.baseState !== 1 /* Text */ &&
  812. this.baseState !== 24 /* InSpecialTag */) {
  813. this.cbs.onattribdata(value);
  814. }
  815. else {
  816. this.cbs.ontext(value);
  817. }
  818. };
  819. return Tokenizer;
  820. }());
  821. exports.default = Tokenizer;