mirror of
https://github.com/markedjs/marked.git
synced 2026-11-30 16:48:05 +01:00
fix: re-assign tokenizer.lexer and renderer.parser at start of each parse call (#3907)
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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++) {
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user