Tokenizer.js 36 KB

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