Tokenizer.js 29 KB

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