Tokenizer.js 34 KB

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