Tokenizer.js 31 KB

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