fix: check own properties on extensions.renderers in Parser (#4271)

This commit is contained in:
Ragib MRB
2026-11-23 18:21:05 -07:00
committed by GitHub
parent 283f74f24b
commit c57230757e
2 changed files with 18 additions and 4 deletions
+6 -4
View File
@@ -48,9 +48,10 @@ export class _Parser<ParserOutput = string, RendererOutput = string> {
const anyToken = tokens[i];
// Run any renderer extensions
if (this.options.extensions?.renderers?.[anyToken.type]) {
const rendererExt = this.options.extensions?.renderers;
if (rendererExt && Object.hasOwn(rendererExt, anyToken.type) && rendererExt[anyToken.type]) {
const genericToken = anyToken as Tokens.Generic;
const ret = this.options.extensions.renderers[genericToken.type].call({ parser: this }, genericToken);
const ret = rendererExt[genericToken.type].call({ parser: this }, genericToken);
if (ret !== false || !['space', 'hr', 'heading', 'code', 'table', 'blockquote', 'list', 'checkbox', 'html', 'def', 'paragraph', 'text'].includes(genericToken.type)) {
out += ret === false ? '' : ret ?? '';
continue;
@@ -135,8 +136,9 @@ export class _Parser<ParserOutput = string, RendererOutput = string> {
const anyToken = tokens[i];
// Run any renderer extensions
if (this.options.extensions?.renderers?.[anyToken.type]) {
const ret = this.options.extensions.renderers[anyToken.type].call({ parser: this }, anyToken);
const rendererExt = this.options.extensions?.renderers;
if (rendererExt && Object.hasOwn(rendererExt, anyToken.type) && rendererExt[anyToken.type]) {
const ret = rendererExt[anyToken.type].call({ parser: this }, anyToken);
if (ret !== false || !['escape', 'html', 'link', 'image', 'checkbox', 'strong', 'em', 'codespan', 'br', 'del', 'text'].includes(anyToken.type)) {
out += ret === false ? '' : ret ?? '';
continue;
+12
View File
@@ -516,4 +516,16 @@ describe('Parser', () => {
});
});
});
it('ignores inherited Object.prototype properties on extensions.renderers', () => {
const parser = new Parser({ extensions: { renderers: {} } });
assert.throws(
() => parser.parse([{ type: 'toString', raw: '' }]),
/Token with "toString" type was not found\./,
);
assert.throws(
() => parser.parseInline([{ type: 'toString', raw: '' }]),
/Token with "toString" type was not found\./,
);
});
});