fix: re-assign tokenizer.lexer and renderer.parser at start of each parse call (#3907)

This commit is contained in:
Veer Shah
2026-03-20 09:07:37 -06:00
committed by GitHub
parent 4625980454
commit f3a3ec05cb
3 changed files with 27 additions and 0 deletions
+2
View File
@@ -105,6 +105,7 @@ export class _Lexer<ParserOutput = string, RendererOutput = string> {
blockTokens(src: string, tokens?: Token[], lastParagraphClipped?: boolean): Token[];
blockTokens(src: string, tokens?: TokensList, lastParagraphClipped?: boolean): TokensList;
blockTokens(src: string, tokens: Token[] = [], lastParagraphClipped = false) {
this.tokenizer.lexer = this;
if (this.options.pedantic) {
src = src.replace(other.tabCharGlobal, ' ').replace(other.spaceLine, '');
}
@@ -297,6 +298,7 @@ export class _Lexer<ParserOutput = string, RendererOutput = string> {
* Lexing/Compiling
*/
inlineTokens(src: string, tokens: Token[] = []): Token[] {
this.tokenizer.lexer = this;
// String with links masked to avoid interference with em and strong
let maskedSrc = src;
let match: RegExpExecArray | null = null;
+2
View File
@@ -40,6 +40,7 @@ export class _Parser<ParserOutput = string, RendererOutput = string> {
* Parse Loop
*/
parse(tokens: Token[]): ParserOutput {
this.renderer.parser = this;
let out = '';
for (let i = 0; i < tokens.length; i++) {
@@ -126,6 +127,7 @@ export class _Parser<ParserOutput = string, RendererOutput = string> {
* Parse Inline Tokens
*/
parseInline(tokens: Token[], renderer: _Renderer<ParserOutput, RendererOutput> | _TextRenderer<RendererOutput> = this.renderer): ParserOutput {
this.renderer.parser = this;
let out = '';
for (let i = 0; i < tokens.length; i++) {
+23
View File
@@ -1929,4 +1929,27 @@ paragraph
});
});
});
describe('multiple instances', () => {
it('should produce identical tokens when multiple Lexer instances lex the same input (issue #3854)', () => {
// Create the first instance and lex — this may mutate shared _defaults.tokenizer.lexer
const lexer1 = new Lexer();
const tokensA = lexer1.lex('Test');
// Create a second instance after the first — previously tokensB.tokens would be empty
// because lexer1's constructor overwrote _defaults.tokenizer.lexer to point to lexer1
const lexer2 = new Lexer();
const tokensB = lexer2.lex('Test');
assert.deepEqual(tokensA, tokensB);
});
it('should produce identical tokens using static lex and instance lex (issue #3854)', () => {
const lexer = new Lexer();
const tokensA = Lexer.lex('Test');
const tokensB = lexer.lex('Test');
assert.deepEqual(tokensA, tokensB);
});
});
});