mirror of
https://github.com/markedjs/marked.git
synced 2026-11-30 16:48:05 +01:00
fix: check own properties on extensions.renderers in Parser (#4271)
This commit is contained in:
+6
-4
@@ -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;
|
||||
|
||||
@@ -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\./,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user