Parser.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. var Tokenizer_js_1 = require("./Tokenizer.js");
  2. var decode_js_1 = require("./entities/decode.js");
  3. var formTags = new Set([
  4. "input",
  5. "option",
  6. "optgroup",
  7. "select",
  8. "button",
  9. "datalist",
  10. "textarea",
  11. ]);
  12. var pTag = new Set(["p"]);
  13. var tableSectionTags = new Set(["thead", "tbody"]);
  14. var ddtTags = new Set(["dd", "dt"]);
  15. var rtpTags = new Set(["rt", "rp"]);
  16. var 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. var 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. var foreignContextElements = new Set(["math", "svg"]);
  87. var htmlIntegrationElements = new Set([
  88. "mi",
  89. "mo",
  90. "mn",
  91. "ms",
  92. "mtext",
  93. "annotation-xml",
  94. "foreignobject",
  95. "desc",
  96. "title",
  97. ]);
  98. var reNameEnd = /\s|\//;
  99. var Parser = /** @class */ (function () {
  100. function Parser(cbs, options) {
  101. if (options === void 0) { options = {}; }
  102. var _a, _b, _c, _d, _e;
  103. this.options = options;
  104. /** The start index of the last event. */
  105. this.startIndex = 0;
  106. /** The end index of the last event. */
  107. this.endIndex = 0;
  108. /**
  109. * Store the start index of the current open tag,
  110. * so we can update the start index for attributes.
  111. */
  112. this.openTagStart = 0;
  113. this.tagname = "";
  114. this.attribname = "";
  115. this.attribvalue = "";
  116. this.attribs = null;
  117. this.stack = [];
  118. this.foreignContext = [];
  119. this.buffers = [];
  120. this.bufferOffset = 0;
  121. /** The index of the last written buffer. Used when resuming after a `pause()`. */
  122. this.writeIndex = 0;
  123. /** Indicates whether the parser has finished running / `.end` has been called. */
  124. this.ended = false;
  125. this.cbs = cbs !== null && cbs !== void 0 ? cbs : {};
  126. this.lowerCaseTagNames = (_a = options.lowerCaseTags) !== null && _a !== void 0 ? _a : !options.xmlMode;
  127. this.lowerCaseAttributeNames =
  128. (_b = options.lowerCaseAttributeNames) !== null && _b !== void 0 ? _b : !options.xmlMode;
  129. this.tokenizer = new ((_c = options.Tokenizer) !== null && _c !== void 0 ? _c : Tokenizer_js_1.default)(this.options, this);
  130. (_e = (_d = this.cbs).onparserinit) === null || _e === void 0 ? void 0 : _e.call(_d, this);
  131. }
  132. // Tokenizer event handlers
  133. /** @internal */
  134. Parser.prototype.ontext = function (start, endIndex) {
  135. var _a, _b;
  136. var data = this.getSlice(start, endIndex);
  137. this.endIndex = endIndex - 1;
  138. (_b = (_a = this.cbs).ontext) === null || _b === void 0 ? void 0 : _b.call(_a, data);
  139. this.startIndex = endIndex;
  140. };
  141. /** @internal */
  142. Parser.prototype.ontextentity = function (cp) {
  143. var _a, _b;
  144. /*
  145. * Entities can be emitted on the character, or directly after.
  146. * We use the section start here to get accurate indices.
  147. */
  148. var idx = this.tokenizer.getSectionStart();
  149. this.endIndex = idx - 1;
  150. (_b = (_a = this.cbs).ontext) === null || _b === void 0 ? void 0 : _b.call(_a, (0, decode_js_1.fromCodePoint)(cp));
  151. this.startIndex = idx;
  152. };
  153. Parser.prototype.isVoidElement = function (name) {
  154. return !this.options.xmlMode && voidElements.has(name);
  155. };
  156. /** @internal */
  157. Parser.prototype.onopentagname = function (start, endIndex) {
  158. this.endIndex = endIndex;
  159. var name = this.getSlice(start, endIndex);
  160. if (this.lowerCaseTagNames) {
  161. name = name.toLowerCase();
  162. }
  163. this.emitOpenTag(name);
  164. };
  165. Parser.prototype.emitOpenTag = function (name) {
  166. var _a, _b, _c, _d;
  167. this.openTagStart = this.startIndex;
  168. this.tagname = name;
  169. var impliesClose = !this.options.xmlMode && openImpliesClose.get(name);
  170. if (impliesClose) {
  171. while (this.stack.length > 0 &&
  172. impliesClose.has(this.stack[this.stack.length - 1])) {
  173. var el = this.stack.pop();
  174. (_b = (_a = this.cbs).onclosetag) === null || _b === void 0 ? void 0 : _b.call(_a, el, true);
  175. }
  176. }
  177. if (!this.isVoidElement(name)) {
  178. this.stack.push(name);
  179. if (foreignContextElements.has(name)) {
  180. this.foreignContext.push(true);
  181. }
  182. else if (htmlIntegrationElements.has(name)) {
  183. this.foreignContext.push(false);
  184. }
  185. }
  186. (_d = (_c = this.cbs).onopentagname) === null || _d === void 0 ? void 0 : _d.call(_c, name);
  187. if (this.cbs.onopentag)
  188. this.attribs = {};
  189. };
  190. Parser.prototype.endOpenTag = function (isImplied) {
  191. var _a, _b;
  192. this.startIndex = this.openTagStart;
  193. if (this.attribs) {
  194. (_b = (_a = this.cbs).onopentag) === null || _b === void 0 ? void 0 : _b.call(_a, this.tagname, this.attribs, isImplied);
  195. this.attribs = null;
  196. }
  197. if (this.cbs.onclosetag && this.isVoidElement(this.tagname)) {
  198. this.cbs.onclosetag(this.tagname, true);
  199. }
  200. this.tagname = "";
  201. };
  202. /** @internal */
  203. Parser.prototype.onopentagend = function (endIndex) {
  204. this.endIndex = endIndex;
  205. this.endOpenTag(false);
  206. // Set `startIndex` for next node
  207. this.startIndex = endIndex + 1;
  208. };
  209. /** @internal */
  210. Parser.prototype.onclosetag = function (start, endIndex) {
  211. var _a, _b, _c, _d, _e, _f;
  212. this.endIndex = endIndex;
  213. var name = this.getSlice(start, endIndex);
  214. if (this.lowerCaseTagNames) {
  215. name = name.toLowerCase();
  216. }
  217. if (foreignContextElements.has(name) ||
  218. htmlIntegrationElements.has(name)) {
  219. this.foreignContext.pop();
  220. }
  221. if (!this.isVoidElement(name)) {
  222. var pos = this.stack.lastIndexOf(name);
  223. if (pos !== -1) {
  224. if (this.cbs.onclosetag) {
  225. var count = this.stack.length - pos;
  226. while (count--) {
  227. // We know the stack has sufficient elements.
  228. this.cbs.onclosetag(this.stack.pop(), count !== 0);
  229. }
  230. }
  231. else
  232. this.stack.length = pos;
  233. }
  234. else if (!this.options.xmlMode && name === "p") {
  235. // Implicit open before close
  236. this.emitOpenTag("p");
  237. this.closeCurrentTag(true);
  238. }
  239. }
  240. else if (!this.options.xmlMode && name === "br") {
  241. // We can't use `emitOpenTag` for implicit open, as `br` would be implicitly closed.
  242. (_b = (_a = this.cbs).onopentagname) === null || _b === void 0 ? void 0 : _b.call(_a, "br");
  243. (_d = (_c = this.cbs).onopentag) === null || _d === void 0 ? void 0 : _d.call(_c, "br", {}, true);
  244. (_f = (_e = this.cbs).onclosetag) === null || _f === void 0 ? void 0 : _f.call(_e, "br", false);
  245. }
  246. // Set `startIndex` for next node
  247. this.startIndex = endIndex + 1;
  248. };
  249. /** @internal */
  250. Parser.prototype.onselfclosingtag = function (endIndex) {
  251. this.endIndex = endIndex;
  252. if (this.options.xmlMode ||
  253. this.options.recognizeSelfClosing ||
  254. this.foreignContext[this.foreignContext.length - 1]) {
  255. this.closeCurrentTag(false);
  256. // Set `startIndex` for next node
  257. this.startIndex = endIndex + 1;
  258. }
  259. else {
  260. // Ignore the fact that the tag is self-closing.
  261. this.onopentagend(endIndex);
  262. }
  263. };
  264. Parser.prototype.closeCurrentTag = function (isOpenImplied) {
  265. var _a, _b;
  266. var name = this.tagname;
  267. this.endOpenTag(isOpenImplied);
  268. // Self-closing tags will be on the top of the stack
  269. if (this.stack[this.stack.length - 1] === name) {
  270. // If the opening tag isn't implied, the closing tag has to be implied.
  271. (_b = (_a = this.cbs).onclosetag) === null || _b === void 0 ? void 0 : _b.call(_a, name, !isOpenImplied);
  272. this.stack.pop();
  273. }
  274. };
  275. /** @internal */
  276. Parser.prototype.onattribname = function (start, endIndex) {
  277. this.startIndex = start;
  278. var name = this.getSlice(start, endIndex);
  279. this.attribname = this.lowerCaseAttributeNames
  280. ? name.toLowerCase()
  281. : name;
  282. };
  283. /** @internal */
  284. Parser.prototype.onattribdata = function (start, endIndex) {
  285. this.attribvalue += this.getSlice(start, endIndex);
  286. };
  287. /** @internal */
  288. Parser.prototype.onattribentity = function (cp) {
  289. this.attribvalue += (0, decode_js_1.fromCodePoint)(cp);
  290. };
  291. /** @internal */
  292. Parser.prototype.onattribend = function (quote, endIndex) {
  293. var _a, _b;
  294. this.endIndex = endIndex;
  295. (_b = (_a = this.cbs).onattribute) === null || _b === void 0 ? void 0 : _b.call(_a, this.attribname, this.attribvalue, quote === Tokenizer_js_1.QuoteType.Double
  296. ? '"'
  297. : quote === Tokenizer_js_1.QuoteType.Single
  298. ? "'"
  299. : quote === Tokenizer_js_1.QuoteType.NoValue
  300. ? undefined
  301. : null);
  302. if (this.attribs &&
  303. !Object.prototype.hasOwnProperty.call(this.attribs, this.attribname)) {
  304. this.attribs[this.attribname] = this.attribvalue;
  305. }
  306. this.attribvalue = "";
  307. };
  308. Parser.prototype.getInstructionName = function (value) {
  309. var idx = value.search(reNameEnd);
  310. var name = idx < 0 ? value : value.substr(0, idx);
  311. if (this.lowerCaseTagNames) {
  312. name = name.toLowerCase();
  313. }
  314. return name;
  315. };
  316. /** @internal */
  317. Parser.prototype.ondeclaration = function (start, endIndex) {
  318. this.endIndex = endIndex;
  319. var value = this.getSlice(start, endIndex);
  320. if (this.cbs.onprocessinginstruction) {
  321. var name = this.getInstructionName(value);
  322. this.cbs.onprocessinginstruction("!".concat(name), "!".concat(value));
  323. }
  324. // Set `startIndex` for next node
  325. this.startIndex = endIndex + 1;
  326. };
  327. /** @internal */
  328. Parser.prototype.onprocessinginstruction = function (start, endIndex) {
  329. this.endIndex = endIndex;
  330. var value = this.getSlice(start, endIndex);
  331. if (this.cbs.onprocessinginstruction) {
  332. var name = this.getInstructionName(value);
  333. this.cbs.onprocessinginstruction("?".concat(name), "?".concat(value));
  334. }
  335. // Set `startIndex` for next node
  336. this.startIndex = endIndex + 1;
  337. };
  338. /** @internal */
  339. Parser.prototype.oncomment = function (start, endIndex, offset) {
  340. var _a, _b, _c, _d;
  341. this.endIndex = endIndex;
  342. (_b = (_a = this.cbs).oncomment) === null || _b === void 0 ? void 0 : _b.call(_a, this.getSlice(start, endIndex - offset));
  343. (_d = (_c = this.cbs).oncommentend) === null || _d === void 0 ? void 0 : _d.call(_c);
  344. // Set `startIndex` for next node
  345. this.startIndex = endIndex + 1;
  346. };
  347. /** @internal */
  348. Parser.prototype.oncdata = function (start, endIndex, offset) {
  349. var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
  350. this.endIndex = endIndex;
  351. var value = this.getSlice(start, endIndex - offset);
  352. if (this.options.xmlMode || this.options.recognizeCDATA) {
  353. (_b = (_a = this.cbs).oncdatastart) === null || _b === void 0 ? void 0 : _b.call(_a);
  354. (_d = (_c = this.cbs).ontext) === null || _d === void 0 ? void 0 : _d.call(_c, value);
  355. (_f = (_e = this.cbs).oncdataend) === null || _f === void 0 ? void 0 : _f.call(_e);
  356. }
  357. else {
  358. (_h = (_g = this.cbs).oncomment) === null || _h === void 0 ? void 0 : _h.call(_g, "[CDATA[".concat(value, "]]"));
  359. (_k = (_j = this.cbs).oncommentend) === null || _k === void 0 ? void 0 : _k.call(_j);
  360. }
  361. // Set `startIndex` for next node
  362. this.startIndex = endIndex + 1;
  363. };
  364. /** @internal */
  365. Parser.prototype.onend = function () {
  366. var _a, _b;
  367. if (this.cbs.onclosetag) {
  368. // Set the end index for all remaining tags
  369. this.endIndex = this.startIndex;
  370. for (var i = this.stack.length; i > 0; this.cbs.onclosetag(this.stack[--i], true))
  371. ;
  372. }
  373. (_b = (_a = this.cbs).onend) === null || _b === void 0 ? void 0 : _b.call(_a);
  374. };
  375. /**
  376. * Resets the parser to a blank state, ready to parse a new HTML document
  377. */
  378. Parser.prototype.reset = function () {
  379. var _a, _b, _c, _d;
  380. (_b = (_a = this.cbs).onreset) === null || _b === void 0 ? void 0 : _b.call(_a);
  381. this.tokenizer.reset();
  382. this.tagname = "";
  383. this.attribname = "";
  384. this.attribs = null;
  385. this.stack.length = 0;
  386. this.startIndex = 0;
  387. this.endIndex = 0;
  388. (_d = (_c = this.cbs).onparserinit) === null || _d === void 0 ? void 0 : _d.call(_c, this);
  389. this.buffers.length = 0;
  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. Parser.prototype.parseComplete = function (data) {
  401. this.reset();
  402. this.end(data);
  403. };
  404. Parser.prototype.getSlice = function (start, end) {
  405. while (start - this.bufferOffset >= this.buffers[0].length) {
  406. this.shiftBuffer();
  407. }
  408. var str = this.buffers[0].slice(start - this.bufferOffset, end - this.bufferOffset);
  409. while (end - this.bufferOffset > this.buffers[0].length) {
  410. this.shiftBuffer();
  411. str += this.buffers[0].slice(0, end - this.bufferOffset);
  412. }
  413. return str;
  414. };
  415. Parser.prototype.shiftBuffer = function () {
  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. Parser.prototype.write = function (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. Parser.prototype.end = function (chunk) {
  443. var _a, _b;
  444. if (this.ended) {
  445. (_b = (_a = this.cbs).onerror) === null || _b === void 0 ? void 0 : _b.call(_a, 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. Parser.prototype.pause = function () {
  457. this.tokenizer.pause();
  458. };
  459. /**
  460. * Resumes parsing after `pause` was called.
  461. */
  462. Parser.prototype.resume = function () {
  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. Parser.prototype.parseChunk = function (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. Parser.prototype.done = function (chunk) {
  487. this.end(chunk);
  488. };
  489. return Parser;
  490. }());
  491. module.exports = Parser;