fix: preserve literal entities in image alt text (#4232)

* fix: preserve literal entities in image alt text

Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com>

* test: cover image literal entities and text renderer compatibility

Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com>

* test: add image alt literal entity inputs

Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com>

* test: assert literal entity image alt output

Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com>

---------

Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com>
This commit is contained in:
generaltony007
2026-11-17 08:38:15 -07:00
committed by GitHub
co-authored by GeneralTony007
parent 171f177a42
commit 04f1c6e518
4 changed files with 63 additions and 3 deletions
+16 -2
View File
@@ -5,9 +5,23 @@ import {
} from './helpers.ts';
import { other } from './rules.ts';
import type { MarkedOptions } from './MarkedOptions.ts';
import type { Tokens } from './Tokens.ts';
import type { Token, Tokens } from './Tokens.ts';
import type { _Parser } from './Parser.ts';
// Literal code and escaped punctuation must survive the later alt-attribute
// escaping. Copy tokens so rendering the image cannot change the token tree.
function imageTextTokens(tokens: Token[]): Token[] {
return tokens.map((token) => {
if (token.type === 'codespan' || token.type === 'escape') {
return { ...token, text: escapeHtmlEntities(token.text, true) };
}
if ('tokens' in token && token.tokens) {
return { ...token, tokens: imageTextTokens(token.tokens) };
}
return token;
});
}
/**
* Renderer
*/
@@ -179,7 +193,7 @@ export class _Renderer<ParserOutput = string, RendererOutput = string> {
image({ href, title, text, tokens }: Tokens.Image): RendererOutput {
if (tokens) {
text = this.parser.parseInline(tokens, this.parser.textRenderer) as string;
text = this.parser.parseInline(imageTextTokens(tokens), this.parser.textRenderer) as string;
}
const cleanHref = cleanUrl(href);
if (cleanHref === null) {
@@ -0,0 +1,7 @@
<p><img src="u" alt="&amp;amp;"></p>
<p><img src="u" alt="&amp;#38;"></p>
<p><img src="u" alt="&amp;amp;"></p>
<p><img src="u" alt="&amp;"></p>
<p><img src="u" alt="&amp;amp;"></p>
<p><img src="u" alt="a &amp;amp;"></p>
<p><img src="u" alt="&amp;amp;"></p>
@@ -0,0 +1,17 @@
---
renderExact: true
---
![`&amp;`](u)
![`&#38;`](u)
![\&amp;](u)
![&amp;](u)
![&#38;amp;](u)
![**a `&amp;`**](u)
![![`&amp;`](i)](u)
+23 -1
View File
@@ -1,4 +1,4 @@
import { Parser } from '../../lib/marked.esm.js';
import { Lexer, Parser, TextRenderer } from '../../lib/marked.esm.js';
import { htmlIsEqual, firstDiff } from '@markedjs/testutils';
import assert from 'node:assert';
import { describe, it } from 'node:test';
@@ -11,6 +11,28 @@ async function expectHtml({ tokens, options, html, inline }) {
}
describe('Parser', () => {
it('keeps the public TextRenderer output unencoded', () => {
const renderer = new TextRenderer();
assert.strictEqual(renderer.codespan({ type: 'codespan', raw: '`&amp;`', text: '&amp;' }), '&amp;');
assert.strictEqual(renderer.text({ type: 'escape', raw: '\\&', text: '&' }), '&');
});
it('keeps parser.textRenderer plain outside image rendering', () => {
const parser = new Parser();
const tokens = Lexer.lexInline('`&amp;`\\&');
assert.strictEqual(parser.parseInline(tokens, parser.textRenderer), '&amp;&');
});
it('keeps custom image text rendering and the original tokens', () => {
const parser = new Parser();
parser.textRenderer.strong = () => 'custom';
const tokens = Lexer.lexInline('![**bold** `&amp;` \\&amp;](u)');
const before = structuredClone(tokens);
assert.strictEqual(parser.parseInline(tokens), '<img src="u" alt="custom &amp;amp; &amp;amp;">');
assert.deepStrictEqual(tokens, before);
assert.strictEqual(parser.textRenderer.codespan(tokens[0].tokens[2]), '&amp;');
});
describe('block', () => {
it('space between paragraphs', async() => {
await expectHtml({