Parser.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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, _f;
  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.buffers = [];
  118. this.bufferOffset = 0;
  119. /** The index of the last written buffer. Used when resuming after a `pause()`. */
  120. this.writeIndex = 0;
  121. /** Indicates whether the parser has finished running / `.end` has been called. */
  122. this.ended = false;
  123. this.cbs = cbs !== null && cbs !== void 0 ? cbs : {};
  124. this.htmlMode = !this.options.xmlMode;
  125. this.lowerCaseTagNames = (_a = options.lowerCaseTags) !== null && _a !== void 0 ? _a : this.htmlMode;
  126. this.lowerCaseAttributeNames =
  127. (_b = options.lowerCaseAttributeNames) !== null && _b !== void 0 ? _b : this.htmlMode;
  128. this.recognizeSelfClosing =
  129. (_c = options.recognizeSelfClosing) !== null && _c !== void 0 ? _c : !this.htmlMode;
  130. this.tokenizer = new ((_d = options.Tokenizer) !== null && _d !== void 0 ? _d : Tokenizer)(this.options, this);
  131. this.foreignContext = [!this.htmlMode];
  132. (_f = (_e = this.cbs).onparserinit) === null || _f === void 0 ? void 0 : _f.call(_e, this);
  133. }
  134. // Tokenizer event handlers
  135. /** @internal */
  136. ontext(start, endIndex) {
  137. var _a, _b;
  138. const data = this.getSlice(start, endIndex);
  139. this.endIndex = endIndex - 1;
  140. (_b = (_a = this.cbs).ontext) === null || _b === void 0 ? void 0 : _b.call(_a, data);
  141. this.startIndex = endIndex;
  142. }
  143. /** @internal */
  144. ontextentity(cp, endIndex) {
  145. var _a, _b;
  146. this.endIndex = endIndex - 1;
  147. (_b = (_a = this.cbs).ontext) === null || _b === void 0 ? void 0 : _b.call(_a, fromCodePoint(cp));
  148. this.startIndex = endIndex;
  149. }
  150. /**
  151. * Checks if the current tag is a void element. Override this if you want
  152. * to specify your own additional void elements.
  153. */
  154. isVoidElement(name) {
  155. return this.htmlMode && voidElements.has(name);
  156. }
  157. /** @internal */
  158. onopentagname(start, endIndex) {
  159. this.endIndex = endIndex;
  160. let name = this.getSlice(start, endIndex);
  161. if (this.lowerCaseTagNames) {
  162. name = name.toLowerCase();
  163. }
  164. this.emitOpenTag(name);
  165. }
  166. emitOpenTag(name) {
  167. var _a, _b, _c, _d;
  168. this.openTagStart = this.startIndex;
  169. this.tagname = name;
  170. const impliesClose = this.htmlMode && openImpliesClose.get(name);
  171. if (impliesClose) {
  172. while (this.stack.length > 0 && impliesClose.has(this.stack[0])) {
  173. const element = this.stack.shift();
  174. (_b = (_a = this.cbs).onclosetag) === null || _b === void 0 ? void 0 : _b.call(_a, element, true);
  175. }
  176. }
  177. if (!this.isVoidElement(name)) {
  178. this.stack.unshift(name);
  179. if (this.htmlMode) {
  180. if (foreignContextElements.has(name)) {
  181. this.foreignContext.unshift(true);
  182. }
  183. else if (htmlIntegrationElements.has(name)) {
  184. this.foreignContext.unshift(false);
  185. }
  186. }
  187. }
  188. (_d = (_c = this.cbs).onopentagname) === null || _d === void 0 ? void 0 : _d.call(_c, name);
  189. if (this.cbs.onopentag)
  190. this.attribs = {};
  191. }
  192. endOpenTag(isImplied) {
  193. var _a, _b;
  194. this.startIndex = this.openTagStart;
  195. if (this.attribs) {
  196. (_b = (_a = this.cbs).onopentag) === null || _b === void 0 ? void 0 : _b.call(_a, this.tagname, this.attribs, isImplied);
  197. this.attribs = null;
  198. }
  199. if (this.cbs.onclosetag && this.isVoidElement(this.tagname)) {
  200. this.cbs.onclosetag(this.tagname, true);
  201. }
  202. this.tagname = "";
  203. }
  204. /** @internal */
  205. onopentagend(endIndex) {
  206. this.endIndex = endIndex;
  207. this.endOpenTag(false);
  208. // Set `startIndex` for next node
  209. this.startIndex = endIndex + 1;
  210. }
  211. /** @internal */
  212. onclosetag(start, endIndex) {
  213. var _a, _b, _c, _d, _e, _f, _g, _h;
  214. this.endIndex = endIndex;
  215. let name = this.getSlice(start, endIndex);
  216. if (this.lowerCaseTagNames) {
  217. name = name.toLowerCase();
  218. }
  219. if (this.htmlMode &&
  220. (foreignContextElements.has(name) ||
  221. htmlIntegrationElements.has(name))) {
  222. this.foreignContext.shift();
  223. }
  224. if (!this.isVoidElement(name)) {
  225. const pos = this.stack.indexOf(name);
  226. if (pos !== -1) {
  227. for (let index = 0; index <= pos; index++) {
  228. const element = this.stack.shift();
  229. // We know the stack has sufficient elements.
  230. (_b = (_a = this.cbs).onclosetag) === null || _b === void 0 ? void 0 : _b.call(_a, element, index !== pos);
  231. }
  232. }
  233. else if (this.htmlMode && name === "p") {
  234. // Implicit open before close
  235. this.emitOpenTag("p");
  236. this.closeCurrentTag(true);
  237. }
  238. }
  239. else if (this.htmlMode && name === "br") {
  240. // We can't use `emitOpenTag` for implicit open, as `br` would be implicitly closed.
  241. (_d = (_c = this.cbs).onopentagname) === null || _d === void 0 ? void 0 : _d.call(_c, "br");
  242. (_f = (_e = this.cbs).onopentag) === null || _f === void 0 ? void 0 : _f.call(_e, "br", {}, true);
  243. (_h = (_g = this.cbs).onclosetag) === null || _h === void 0 ? void 0 : _h.call(_g, "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.recognizeSelfClosing || this.foreignContext[0]) {
  252. this.closeCurrentTag(false);
  253. // Set `startIndex` for next node
  254. this.startIndex = endIndex + 1;
  255. }
  256. else {
  257. // Ignore the fact that the tag is self-closing.
  258. this.onopentagend(endIndex);
  259. }
  260. }
  261. closeCurrentTag(isOpenImplied) {
  262. var _a, _b;
  263. const name = this.tagname;
  264. this.endOpenTag(isOpenImplied);
  265. // Self-closing tags will be on the top of the stack
  266. if (this.stack[0] === name) {
  267. // If the opening tag isn't implied, the closing tag has to be implied.
  268. (_b = (_a = this.cbs).onclosetag) === null || _b === void 0 ? void 0 : _b.call(_a, name, !isOpenImplied);
  269. this.stack.shift();
  270. }
  271. }
  272. /** @internal */
  273. onattribname(start, endIndex) {
  274. this.startIndex = start;
  275. const name = this.getSlice(start, endIndex);
  276. this.attribname = this.lowerCaseAttributeNames
  277. ? name.toLowerCase()
  278. : name;
  279. }
  280. /** @internal */
  281. onattribdata(start, endIndex) {
  282. this.attribvalue += this.getSlice(start, endIndex);
  283. }
  284. /** @internal */
  285. onattribentity(cp) {
  286. this.attribvalue += fromCodePoint(cp);
  287. }
  288. /** @internal */
  289. onattribend(quote, endIndex) {
  290. var _a, _b;
  291. this.endIndex = endIndex;
  292. (_b = (_a = this.cbs).onattribute) === null || _b === void 0 ? void 0 : _b.call(_a, this.attribname, this.attribvalue, quote === QuoteType.Double
  293. ? '"'
  294. : quote === QuoteType.Single
  295. ? "'"
  296. : quote === QuoteType.NoValue
  297. ? undefined
  298. : null);
  299. if (this.attribs &&
  300. !Object.prototype.hasOwnProperty.call(this.attribs, this.attribname)) {
  301. this.attribs[this.attribname] = this.attribvalue;
  302. }
  303. this.attribvalue = "";
  304. }
  305. getInstructionName(value) {
  306. const index = value.search(reNameEnd);
  307. let name = index < 0 ? value : value.substr(0, index);
  308. if (this.lowerCaseTagNames) {
  309. name = name.toLowerCase();
  310. }
  311. return name;
  312. }
  313. /** @internal */
  314. ondeclaration(start, endIndex) {
  315. this.endIndex = endIndex;
  316. const value = this.getSlice(start, endIndex);
  317. if (this.cbs.onprocessinginstruction) {
  318. const name = this.getInstructionName(value);
  319. this.cbs.onprocessinginstruction(`!${name}`, `!${value}`);
  320. }
  321. // Set `startIndex` for next node
  322. this.startIndex = endIndex + 1;
  323. }
  324. /** @internal */
  325. onprocessinginstruction(start, endIndex) {
  326. this.endIndex = endIndex;
  327. const value = this.getSlice(start, endIndex);
  328. if (this.cbs.onprocessinginstruction) {
  329. const name = this.getInstructionName(value);
  330. this.cbs.onprocessinginstruction(`?${name}`, `?${value}`);
  331. }
  332. // Set `startIndex` for next node
  333. this.startIndex = endIndex + 1;
  334. }
  335. /** @internal */
  336. oncomment(start, endIndex, offset) {
  337. var _a, _b, _c, _d;
  338. this.endIndex = endIndex;
  339. (_b = (_a = this.cbs).oncomment) === null || _b === void 0 ? void 0 : _b.call(_a, this.getSlice(start, endIndex - offset));
  340. (_d = (_c = this.cbs).oncommentend) === null || _d === void 0 ? void 0 : _d.call(_c);
  341. // Set `startIndex` for next node
  342. this.startIndex = endIndex + 1;
  343. }
  344. /** @internal */
  345. oncdata(start, endIndex, offset) {
  346. var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
  347. this.endIndex = endIndex;
  348. const value = this.getSlice(start, endIndex - offset);
  349. if (!this.htmlMode || this.options.recognizeCDATA) {
  350. (_b = (_a = this.cbs).oncdatastart) === null || _b === void 0 ? void 0 : _b.call(_a);
  351. (_d = (_c = this.cbs).ontext) === null || _d === void 0 ? void 0 : _d.call(_c, value);
  352. (_f = (_e = this.cbs).oncdataend) === null || _f === void 0 ? void 0 : _f.call(_e);
  353. }
  354. else {
  355. (_h = (_g = this.cbs).oncomment) === null || _h === void 0 ? void 0 : _h.call(_g, `[CDATA[${value}]]`);
  356. (_k = (_j = this.cbs).oncommentend) === null || _k === void 0 ? void 0 : _k.call(_j);
  357. }
  358. // Set `startIndex` for next node
  359. this.startIndex = endIndex + 1;
  360. }
  361. /** @internal */
  362. onend() {
  363. var _a, _b;
  364. if (this.cbs.onclosetag) {
  365. // Set the end index for all remaining tags
  366. this.endIndex = this.startIndex;
  367. for (let index = 0; index < this.stack.length; index++) {
  368. this.cbs.onclosetag(this.stack[index], true);
  369. }
  370. }
  371. (_b = (_a = this.cbs).onend) === null || _b === void 0 ? void 0 : _b.call(_a);
  372. }
  373. /**
  374. * Resets the parser to a blank state, ready to parse a new HTML document
  375. */
  376. reset() {
  377. var _a, _b, _c, _d;
  378. (_b = (_a = this.cbs).onreset) === null || _b === void 0 ? void 0 : _b.call(_a);
  379. this.tokenizer.reset();
  380. this.tagname = "";
  381. this.attribname = "";
  382. this.attribs = null;
  383. this.stack.length = 0;
  384. this.startIndex = 0;
  385. this.endIndex = 0;
  386. (_d = (_c = this.cbs).onparserinit) === null || _d === void 0 ? void 0 : _d.call(_c, this);
  387. this.buffers.length = 0;
  388. this.foreignContext.length = 0;
  389. this.foreignContext.unshift(!this.htmlMode);
  390. this.bufferOffset = 0;
  391. this.writeIndex = 0;
  392. this.ended = false;
  393. }
  394. /**
  395. * Resets the parser, then parses a complete document and
  396. * pushes it to the handler.
  397. *
  398. * @param data Document to parse.
  399. */
  400. parseComplete(data) {
  401. this.reset();
  402. this.end(data);
  403. }
  404. getSlice(start, end) {
  405. while (start - this.bufferOffset >= this.buffers[0].length) {
  406. this.shiftBuffer();
  407. }
  408. let slice = this.buffers[0].slice(start - this.bufferOffset, end - this.bufferOffset);
  409. while (end - this.bufferOffset > this.buffers[0].length) {
  410. this.shiftBuffer();
  411. slice += this.buffers[0].slice(0, end - this.bufferOffset);
  412. }
  413. return slice;
  414. }
  415. shiftBuffer() {
  416. this.bufferOffset += this.buffers[0].length;
  417. this.writeIndex--;
  418. this.buffers.shift();
  419. }
  420. /**
  421. * Parses a chunk of data and calls the corresponding callbacks.
  422. *
  423. * @param chunk Chunk to parse.
  424. */
  425. write(chunk) {
  426. var _a, _b;
  427. if (this.ended) {
  428. (_b = (_a = this.cbs).onerror) === null || _b === void 0 ? void 0 : _b.call(_a, new Error(".write() after done!"));
  429. return;
  430. }
  431. this.buffers.push(chunk);
  432. if (this.tokenizer.running) {
  433. this.tokenizer.write(chunk);
  434. this.writeIndex++;
  435. }
  436. }
  437. /**
  438. * Parses the end of the buffer and clears the stack, calls onend.
  439. *
  440. * @param chunk Optional final chunk to parse.
  441. */
  442. end(chunk) {
  443. var _a, _b;
  444. if (this.ended) {
  445. (_b = (_a = this.cbs).onerror) === null || _b === void 0 ? void 0 : _b.call(_a, new Error(".end() after done!"));
  446. return;
  447. }
  448. if (chunk)
  449. this.write(chunk);
  450. this.ended = true;
  451. this.tokenizer.end();
  452. }
  453. /**
  454. * Pauses parsing. The parser won't emit events until `resume` is called.
  455. */
  456. pause() {
  457. this.tokenizer.pause();
  458. }
  459. /**
  460. * Resumes parsing after `pause` was called.
  461. */
  462. resume() {
  463. this.tokenizer.resume();
  464. while (this.tokenizer.running &&
  465. this.writeIndex < this.buffers.length) {
  466. this.tokenizer.write(this.buffers[this.writeIndex++]);
  467. }
  468. if (this.ended)
  469. this.tokenizer.end();
  470. }
  471. /**
  472. * Alias of `write`, for backwards compatibility.
  473. *
  474. * @param chunk Chunk to parse.
  475. * @deprecated
  476. */
  477. parseChunk(chunk) {
  478. this.write(chunk);
  479. }
  480. /**
  481. * Alias of `end`, for backwards compatibility.
  482. *
  483. * @param chunk Optional final chunk to parse.
  484. * @deprecated
  485. */
  486. done(chunk) {
  487. this.end(chunk);
  488. }
  489. }
  490. //# sourceMappingURL=Parser.js.map