Parser.js 18 KB

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