mirror of
https://github.com/markedjs/marked.git
synced 2026-11-30 16:48:05 +01:00
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:
co-authored by
GeneralTony007
parent
171f177a42
commit
04f1c6e518
+16
-2
@@ -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;"></p>
|
||||
<p><img src="u" alt="&#38;"></p>
|
||||
<p><img src="u" alt="&amp;"></p>
|
||||
<p><img src="u" alt="&"></p>
|
||||
<p><img src="u" alt="&amp;"></p>
|
||||
<p><img src="u" alt="a &amp;"></p>
|
||||
<p><img src="u" alt="&amp;"></p>
|
||||
@@ -0,0 +1,17 @@
|
||||
---
|
||||
renderExact: true
|
||||
---
|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||
](u)
|
||||
@@ -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: '`&`', text: '&' }), '&');
|
||||
assert.strictEqual(renderer.text({ type: 'escape', raw: '\\&', text: '&' }), '&');
|
||||
});
|
||||
|
||||
it('keeps parser.textRenderer plain outside image rendering', () => {
|
||||
const parser = new Parser();
|
||||
const tokens = Lexer.lexInline('`&`\\&');
|
||||
assert.strictEqual(parser.parseInline(tokens, parser.textRenderer), '&&');
|
||||
});
|
||||
|
||||
it('keeps custom image text rendering and the original tokens', () => {
|
||||
const parser = new Parser();
|
||||
parser.textRenderer.strong = () => 'custom';
|
||||
const tokens = Lexer.lexInline('');
|
||||
const before = structuredClone(tokens);
|
||||
assert.strictEqual(parser.parseInline(tokens), '<img src="u" alt="custom &amp; &amp;">');
|
||||
assert.deepStrictEqual(tokens, before);
|
||||
assert.strictEqual(parser.textRenderer.codespan(tokens[0].tokens[2]), '&');
|
||||
});
|
||||
|
||||
describe('block', () => {
|
||||
it('space between paragraphs', async() => {
|
||||
await expectHtml({
|
||||
|
||||
Reference in New Issue
Block a user