Parser.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. import Tokenizer, { QuoteType } from "./Tokenizer.js";
  2. import { fromCodePoint } from "entities/lib/decode.js";
  3. const formTags = new Set([
  4. "input",
  5. "option",
  6. "optgroup",
  7. "select",
  8. "button",
  9. "datalist",
  10. "textarea",
  11. ]);
  12. const pTag = new Set(["p"]);
  13. const tableSectionTags = new Set(["thead", "tbody"]);
  14. const ddtTags = new Set(["dd", "dt"]);
  15. const rtpTags = new Set(["rt", "rp"]);
  16. const openImpliesClose = new Map([
  17. ["tr", new Set(["tr", "th", "td"])],
  18. ["th", new Set(["th"])],
  19. ["td", new Set(["thead", "th", "td"])],
  20. ["body", new Set(["head", "link", "script"])],
  21. ["li", new Set(["li"])],
  22. ["p", pTag],
  23. ["h1", pTag],
  24. ["h2", pTag],
  25. ["h3", pTag],
  26. ["h4", pTag],
  27. ["h5", pTag],
  28. ["h6", pTag],
  29. ["select", formTags],
  30. ["input", formTags],
  31. ["output", formTags],
  32. ["button", formTags],
  33. ["datalist", formTags],
  34. ["textarea", formTags],
  35. ["option", new Set(["option"])],
  36. ["optgroup", new Set(["optgroup", "option"])],
  37. ["dd", ddtTags],
  38. ["dt", ddtTags],
  39. ["address", pTag],
  40. ["article", pTag],
  41. ["aside", pTag],
  42. ["blockquote", pTag],
  43. ["details", pTag],
  44. ["div", pTag],
  45. ["dl", pTag],
  46. ["fieldset", pTag],
  47. ["figcaption", pTag],
  48. ["figure", pTag],
  49. ["footer", pTag],
  50. ["form", pTag],
  51. ["header", pTag],
  52. ["hr", pTag],
  53. ["main", pTag],
  54. ["nav", pTag],
  55. ["ol", pTag],
  56. ["pre", pTag],
  57. ["section", pTag],
  58. ["table", pTag],
  59. ["ul", pTag],
  60. ["rt", rtpTags],
  61. ["rp", rtpTags],
  62. ["tbody", tableSectionTags],
  63. ["tfoot", tableSectionTags],
  64. ]);
  65. const voidElements = new Set([
  66. "area",
  67. "base",
  68. "basefont",
  69. "br",
  70. "col",
  71. "command",
  72. "embed",
  73. "frame",
  74. "hr",
  75. "img",
  76. "input",
  77. "isindex",
  78. "keygen",
  79. "link",
  80. "meta",
  81. "param",
  82. "source",
  83. "track",
  84. "wbr",
  85. ]);
  86. const foreignContextElements = new Set(["math", "svg"]);
  87. const htmlIntegrationElements = new Set([
  88. "mi",
  89. "mo",
  90. "mn",
  91. "ms",
  92. "mtext",
  93. "annotation-xml",
  94. "foreignobject",
  95. "desc",
  96. "title",
  97. ]);
  98. const reNameEnd = /\s|\//;
  99. export class Parser {
  100. constructor(cbs, options = {}) {
  101. var _a, _b, _c, _d, _e;
  102. this.options = options;
  103. /** The start index of the last event. */
  104. this.startIndex = 0;
  105. /** The end index of the last event. */
  106. this.endIndex = 0;
  107. /**
  108. * Store the start index of the current open tag,
  109. * so we can update the start index for attributes.
  110. */
  111. this.openTagStart = 0;
  112. this.tagname = "";
  113. this.attribname = "";
  114. this.attribvalue = "";
  115. this.attribs = null;
  116. this.stack = [];
  117. this.foreignContext = [];
  118. this.buffers = [];
  119. this.bufferOffset = 0;
  120. /** The index of the last written buffer. Used when resuming after a `pause()`. */
  121. this.writeIndex = 0;
  122. /** Indicates whether the parser has finished running / `.end` has been called. */
  123. this.ended = false;
  124. this.cbs = cbs !== null && cbs !== void 0 ? cbs : {};
  125. this.lowerCaseTagNames = (_a = options.lowerCaseTags) !== null && _a !== void 0 ? _a : !options.xmlMode;
  126. this.lowerCaseAttributeNames =
  127. (_b = options.lowerCaseAttributeNames) !== null && _b !== void 0 ? _b : !options.xmlMode;
  128. this.tokenizer = new ((_c = options.Tokenizer) !== null && _c !== void 0 ? _c : Tokenizer)(this.options, this);
  129. (_e = (_d = this.cbs).onparserinit) === null || _e === void 0 ? void 0 : _e.call(_d, this);
  130. }
  131. // Tokenizer event handlers
  132. /** @internal */
  133. ontext(start, endIndex) {
  134. var _a, _b;
  135. const data = this.getSlice(start, endIndex);
  136. this.endIndex = endIndex - 1;
  137. (_b = (_a = this.cbs).ontext) === null || _b === void 0 ? void 0 : _b.call(_a, data);
  138. this.startIndex = endIndex;
  139. }
  140. /** @internal */
  141. ontextentity(cp) {
  142. var _a, _b;
  143. /*
  144. * Entities can be emitted on the character, or directly after.
  145. * We use the section start here to get accurate indices.
  146. */
  147. const idx = this.tokenizer.getSectionStart();
  148. this.endIndex = idx - 1;
  149. (_b = (_a = this.cbs).ontext) === null || _b === void 0 ? void 0 : _b.call(_a, fromCodePoint(cp));
  150. this.startIndex = idx;
  151. }
  152. isVoidElement(name) {
  153. return !this.options.xmlMode && voidElements.has(name);
  154. }
  155. /** @internal */
  156. onopentagname(start, endIndex) {
  157. this.endIndex = endIndex;
  158. let name = this.getSlice(start, endIndex);
  159. if (this.lowerCaseTagNames) {
  160. name = name.toLowerCase();
  161. }
  162. this.emitOpenTag(name);
  163. }
  164. emitOpenTag(name) {
  165. var _a, _b, _c, _d;
  166. this.openTagStart = this.startIndex;
  167. this.tagname = name;
  168. const impliesClose = !this.options.xmlMode && openImpliesClose.get(name);
  169. if (impliesClose) {
  170. while (this.stack.length > 0 &&
  171. impliesClose.has(this.stack[this.stack.length - 1])) {
  172. const el = this.stack.pop();
  173. (_b = (_a = this.cbs).onclosetag) === null || _b === void 0 ? void 0 : _b.call(_a, el, true);
  174. }
  175. }
  176. if (!this.isVoidElement(name)) {
  177. this.stack.push(name);
  178. if (foreignContextElements.has(name)) {
  179. this.foreignContext.push(true);
  180. }
  181. else if (htmlIntegrationElements.has(name)) {
  182. this.foreignContext.push(false);
  183. }
  184. }
  185. (_d = (_c = this.cbs).onopentagname) === null || _d === void 0 ? void 0 : _d.call(_c, name);
  186. if (this.cbs.onopentag)
  187. this.attribs = {};
  188. }
  189. endOpenTag(isImplied) {
  190. var _a, _b;
  191. this.startIndex = this.openTagStart;
  192. if (this.attribs) {
  193. (_b = (_a = this.cbs).onopentag) === null || _b === void 0 ? void 0 : _b.call(_a, this.tagname, this.attribs, isImplied);
  194. this.attribs = null;
  195. }
  196. if (this.cbs.onclosetag && this.isVoidElement(this.tagname)) {
  197. this.cbs.onclosetag(this.tagname, true);
  198. }
  199. this.tagname = "";
  200. }
  201. /** @internal */
  202. onopentagend(endIndex) {
  203. this.endIndex = endIndex;
  204. this.endOpenTag(false);
  205. // Set `startIndex` for next node
  206. this.startIndex = endIndex + 1;
  207. }
  208. /** @internal */
  209. onclosetag(start, endIndex) {
  210. var _a, _b, _c, _d, _e, _f;
  211. this.endIndex = endIndex;
  212. let name = this.getSlice(start, endIndex);
  213. if (this.lowerCaseTagNames) {
  214. name = name.toLowerCase();
  215. }
  216. if (foreignContextElements.has(name) ||
  217. htmlIntegrationElements.has(name)) {
  218. this.foreignContext.pop();
  219. }
  220. if (!this.isVoidElement(name)) {
  221. const pos = this.stack.lastIndexOf(name);
  222. if (pos !== -1) {
  223. if (this.cbs.onclosetag) {
  224. let count = this.stack.length - pos;
  225. while (count--) {
  226. // We know the stack has sufficient elements.
  227. this.cbs.onclosetag(this.stack.pop(), count !== 0);
  228. }
  229. }
  230. else
  231. this.stack.length = pos;
  232. }
  233. else if (!this.options.xmlMode && name === "p") {
  234. // Implicit open before close
  235. this.emitOpenTag("p");
  236. this.closeCurrentTag(true);
  237. }
  238. }
  239. else if (!this.options.xmlMode && name === "br") {
  240. // We can't use `emitOpenTag` for implicit open, as `br` would be implicitly closed.
  241. (_b = (_a = this.cbs).onopentagname) === null || _b === void 0 ? void 0 : _b.call(_a, "br");
  242. (_d = (_c = this.cbs).onopentag) === null || _d === void 0 ? void 0 : _d.call(_c, "br", {}, true);
  243. (_f = (_e = this.cbs).onclosetag) === null || _f === void 0 ? void 0 : _f.call(_e, "br", false);
  244. }
  245. // Set `startIndex` for next node
  246. this.startIndex = endIndex + 1;
  247. }
  248. /** @internal */
  249. onselfclosingtag(endIndex) {
  250. this.endIndex = endIndex;
  251. if (this.options.xmlMode ||
  252. this.options.recognizeSelfClosing ||
  253. this.foreignContext[this.foreignContext.length - 1]) {
  254. this.closeCurrentTag(false);
  255. // Set `startIndex` for next node
  256. this.startIndex = endIndex + 1;
  257. }
  258. else {
  259. // Ignore the fact that the tag is self-closing.
  260. this.onopentagend(endIndex);
  261. }
  262. }
  263. closeCurrentTag(isOpenImplied) {
  264. var _a, _b;
  265. const name = this.tagname;
  266. this.endOpenTag(isOpenImplied);
  267. // Self-closing tags will be on the top of the stack
  268. if (this.stack[this.stack.length - 1] === name) {
  269. // If the opening tag isn't implied, the closing tag has to be implied.
  270. (_b = (_a = this.cbs).onclosetag) === null || _b === void 0 ? void 0 : _b.call(_a, name, !isOpenImplied);
  271. this.stack.pop();
  272. }
  273. }
  274. /** @internal */
  275. onattribname(start, endIndex) {
  276. this.startIndex = start;
  277. const name = this.getSlice(start, endIndex);
  278. this.attribname = this.lowerCaseAttributeNames
  279. ? name.toLowerCase()
  280. : name;
  281. }
  282. /** @internal */
  283. onattribdata(start, endIndex) {
  284. this.attribvalue += this.getSlice(start, endIndex);
  285. }
  286. /** @internal */
  287. onattribentity(cp) {
  288. this.attribvalue += fromCodePoint(cp);
  289. }
  290. /** @internal */
  291. onattribend(quote, endIndex) {
  292. var _a, _b;
  293. this.endIndex = endIndex;
  294. (_b = (_a = this.cbs).onattribute) === null || _b === void 0 ? void 0 : _b.call(_a, this.attribname, this.attribvalue, quote === QuoteType.Double
  295. ? '"'
  296. : quote === QuoteType.Single
  297. ? "'"
  298. : quote === QuoteType.NoValue
  299. ? undefined
  300. : null);
  301. if (this.attribs &&
  302. !Object.prototype.hasOwnProperty.call(this.attribs, this.attribname)) {
  303. this.attribs[this.attribname] = this.attribvalue;
  304. }
  305. this.attribvalue = "";
  306. }
  307. getInstructionName(value) {
  308. const idx = value.search(reNameEnd);
  309. let name = idx < 0 ? value : value.substr(0, idx);
  310. if (this.lowerCaseTagNames) {
  311. name = name.toLowerCase();
  312. }
  313. return name;
  314. }
  315. /** @internal */
  316. ondeclaration(start, endIndex) {
  317. this.endIndex = endIndex;
  318. const value = this.getSlice(start, endIndex);
  319. if (this.cbs.onprocessinginstruction) {
  320. const name = this.getInstructionName(value);
  321. this.cbs.onprocessinginstruction(`!${name}`, `!${value}`);
  322. }
  323. // Set `startIndex` for next node
  324. this.startIndex = endIndex + 1;
  325. }
  326. /** @internal */
  327. onprocessinginstruction(start, endIndex) {
  328. this.endIndex = endIndex;
  329. const value = this.getSlice(start, endIndex);
  330. if (this.cbs.onprocessinginstruction) {
  331. const name = this.getInstructionName(value);
  332. this.cbs.onprocessinginstruction(`?${name}`, `?${value}`);
  333. }
  334. // Set `startIndex` for next node
  335. this.startIndex = endIndex + 1;
  336. }
  337. /** @internal */
  338. oncomment(start, endIndex, offset) {
  339. var _a, _b, _c, _d;
  340. this.endIndex = endIndex;
  341. (_b = (_a = this.cbs).oncomment) === null || _b === void 0 ? void 0 : _b.call(_a, this.getSlice(start, endIndex - offset));
  342. (_d = (_c = this.cbs).oncommentend) === null || _d === void 0 ? void 0 : _d.call(_c);
  343. // Set `startIndex` for next node
  344. this.startIndex = endIndex + 1;
  345. }
  346. /** @internal */
  347. oncdata(start, endIndex, offset) {
  348. var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
  349. this.endIndex = endIndex;
  350. const value = this.getSlice(start, endIndex - offset);
  351. if (this.options.xmlMode || this.options.recognizeCDATA) {
  352. (_b = (_a = this.cbs).oncdatastart) === null || _b === void 0 ? void 0 : _b.call(_a);
  353. (_d = (_c = this.cbs).ontext) === null || _d === void 0 ? void 0 : _d.call(_c, value);
  354. (_f = (_e = this.cbs).oncdataend) === null || _f === void 0 ? void 0 : _f.call(_e);
  355. }
  356. else {
  357. (_h = (_g = this.cbs).oncomment) === null || _h === void 0 ? void 0 : _h.call(_g, `[CDATA[${value}]]`);
  358. (_k = (_j = this.cbs).oncommentend) === null || _k === void 0 ? void 0 : _k.call(_j);
  359. }
  360. // Set `startIndex` for next node
  361. this.startIndex = endIndex + 1;
  362. }
  363. /** @internal */
  364. onend() {
  365. var _a, _b;
  366. if (this.cbs.onclosetag) {
  367. // Set the end index for all remaining tags
  368. this.endIndex = this.startIndex;
  369. for (let i = this.stack.length; i > 0; this.cbs.onclosetag(this.stack[--i], true))
  370. ;
  371. }
  372. (_b = (_a = this.cbs).onend) === null || _b === void 0 ? void 0 : _b.call(_a);
  373. }
  374. /**
  375. * Resets the parser to a blank state, ready to parse a new HTML document
  376. */
  377. reset() {
  378. var _a, _b, _c, _d;
  379. (_b = (_a = this.cbs).onreset) === null || _b === void 0 ? void 0 : _b.call(_a);
  380. this.tokenizer.reset();
  381. this.tagname = "";
  382. this.attribname = "";
  383. this.attribs = null;
  384. this.stack.length = 0;
  385. this.startIndex = 0;
  386. this.endIndex = 0;
  387. (_d = (_c = this.cbs).onparserinit) === null || _d === void 0 ? void 0 : _d.call(_c, this);
  388. this.buffers.length = 0;
  389. this.bufferOffset = 0;
  390. this.writeIndex = 0;
  391. this.ended = false;
  392. }
  393. /**
  394. * Resets the parser, then parses a complete document and
  395. * pushes it to the handler.
  396. *
  397. * @param data Document to parse.
  398. */
  399. parseComplete(data) {
  400. this.reset();
  401. this.end(data);
  402. }
  403. getSlice(start, end) {
  404. while (start - this.bufferOffset >= this.buffers[0].length) {
  405. this.shiftBuffer();
  406. }
  407. let str = this.buffers[0].slice(start - this.bufferOffset, end - this.bufferOffset);
  408. while (end - this.bufferOffset > this.buffers[0].length) {
  409. this.shiftBuffer();
  410. str += this.buffers[0].slice(0, end - this.bufferOffset);
  411. }
  412. return str;
  413. }
  414. shiftBuffer() {
  415. this.bufferOffset += this.buffers[0].length;
  416. this.writeIndex--;
  417. this.buffers.shift();
  418. }
  419. /**
  420. * Parses a chunk of data and calls the corresponding callbacks.
  421. *
  422. * @param chunk Chunk to parse.
  423. */
  424. write(chunk) {
  425. var _a, _b;
  426. if (this.ended) {
  427. (_b = (_a = this.cbs).onerror) === null || _b === void 0 ? void 0 : _b.call(_a, new Error(".write() after done!"));
  428. return;
  429. }
  430. this.buffers.push(chunk);
  431. if (this.tokenizer.running) {
  432. this.tokenizer.write(chunk);
  433. this.writeIndex++;
  434. }
  435. }
  436. /**
  437. * Parses the end of the buffer and clears the stack, calls onend.
  438. *
  439. * @param chunk Optional final chunk to parse.
  440. */
  441. end(chunk) {
  442. var _a, _b;
  443. if (this.ended) {
  444. (_b = (_a = this.cbs).onerror) === null || _b === void 0 ? void 0 : _b.call(_a, Error(".end() after done!"));
  445. return;
  446. }
  447. if (chunk)
  448. this.write(chunk);
  449. this.ended = true;
  450. this.tokenizer.end();
  451. }
  452. /**
  453. * Pauses parsing. The parser won't emit events until `resume` is called.
  454. */
  455. pause() {
  456. this.tokenizer.pause();
  457. }
  458. /**
  459. * Resumes parsing after `pause` was called.
  460. */
  461. resume() {
  462. this.tokenizer.resume();
  463. while (this.tokenizer.running &&
  464. this.writeIndex < this.buffers.length) {
  465. this.tokenizer.write(this.buffers[this.writeIndex++]);
  466. }
  467. if (this.ended)
  468. this.tokenizer.end();
  469. }
  470. /**
  471. * Alias of `write`, for backwards compatibility.
  472. *
  473. * @param chunk Chunk to parse.
  474. * @deprecated
  475. */
  476. parseChunk(chunk) {
  477. this.write(chunk);
  478. }
  479. /**
  480. * Alias of `end`, for backwards compatibility.
  481. *
  482. * @param chunk Optional final chunk to parse.
  483. * @deprecated
  484. */
  485. done(chunk) {
  486. this.end(chunk);
  487. }
  488. }
  489. //# sourceMappingURL=Parser.js.map