Tokenizer.js 35 KB

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