Tokenizer.js 36 KB

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