mirror of
https://github.com/markedjs/marked.git
synced 2026-11-30 16:48:05 +01:00
fix: isolate extension names from Object.prototype (#4177)
Co-authored-by: Minjung <minjungsung@users.noreply.github.com>
This commit is contained in:
+1
-1
@@ -109,7 +109,7 @@ export class Marked<ParserOutput = string, RendererOutput = string> {
|
||||
* Use this method when supplying only some hook methods.
|
||||
*/
|
||||
use(...args: MarkedExtension<ParserOutput, RendererOutput>[]) {
|
||||
const extensions: MarkedOptions<ParserOutput, RendererOutput>['extensions'] = this.defaults.extensions || { renderers: {}, childTokens: {} };
|
||||
const extensions: MarkedOptions<ParserOutput, RendererOutput>['extensions'] = this.defaults.extensions || { renderers: Object.create(null), childTokens: Object.create(null) };
|
||||
|
||||
args.forEach((pack) => {
|
||||
// copy options to new object
|
||||
|
||||
@@ -449,6 +449,76 @@ describe('marked unit', () => {
|
||||
assert.strictEqual(html, '<ul>\n<li><p><input checked="" disabled="" type="checkbox"> one</p>\n</li>\n<li><p><input disabled="" type="checkbox"> two</p>\n</li>\n</ul>\n');
|
||||
});
|
||||
|
||||
describe('extension names matching Object.prototype properties', () => {
|
||||
for (const level of ['block', 'inline']) {
|
||||
for (const name of ['constructor', 'toString', '__proto__', 'custom']) {
|
||||
it(`should render and walk ${level} extension ${name}`, () => {
|
||||
const seen = [];
|
||||
marked.use({
|
||||
extensions: [{
|
||||
name,
|
||||
level,
|
||||
tokenizer(src) {
|
||||
return {
|
||||
type: name,
|
||||
raw: src,
|
||||
tokens: [{ type: 'text', raw: 'child', text: 'child' }],
|
||||
};
|
||||
},
|
||||
renderer(token) {
|
||||
return this.parser.parseInline(token.tokens);
|
||||
},
|
||||
}],
|
||||
walkTokens(token) {
|
||||
seen.push(token.type);
|
||||
},
|
||||
});
|
||||
|
||||
const html = level === 'block' ? marked.parse('example') : marked.parseInline('example');
|
||||
assert.strictEqual(html, 'child');
|
||||
assert.deepStrictEqual(seen, [name, 'text']);
|
||||
});
|
||||
}
|
||||
|
||||
for (const name of ['constructor', 'toString', '__proto__']) {
|
||||
it(`should not fall back to an inherited ${level} renderer for ${name}`, () => {
|
||||
marked.use({
|
||||
extensions: [{
|
||||
name,
|
||||
level,
|
||||
tokenizer(src) {
|
||||
return { type: name, raw: src };
|
||||
},
|
||||
renderer() {
|
||||
return false;
|
||||
},
|
||||
}],
|
||||
});
|
||||
|
||||
const html = level === 'block' ? marked.parse('example') : marked.parseInline('example');
|
||||
assert.strictEqual(html, '');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
it('should fall back to a registered __proto__ renderer', () => {
|
||||
marked.use({
|
||||
extensions: [{
|
||||
name: '__proto__',
|
||||
level: 'block',
|
||||
tokenizer(src) {
|
||||
return { type: '__proto__', raw: src };
|
||||
},
|
||||
renderer() {
|
||||
return 'previous';
|
||||
},
|
||||
}],
|
||||
});
|
||||
marked.use({ extensions: [{ name: '__proto__', renderer: () => false }] });
|
||||
assert.strictEqual(marked.parse('example'), 'previous');
|
||||
});
|
||||
});
|
||||
|
||||
it('should walk only specified child tokens', () => {
|
||||
const walkableDescription = {
|
||||
extensions: [{
|
||||
|
||||
Reference in New Issue
Block a user