Skip to content

fix: preserve literal entities in image alt text - #4232

Open
hakarrr wants to merge 4 commits into
markedjs:masterfrom
hakarrr:fix/image-alt-literal-entities
Open

hakarrr wants to merge 4 commits into
markedjs:masterfrom
hakarrr:fix/image-alt-literal-entities

Conversation

@hakarrr

@hakarrr hakarrr commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Marked version: master c6b22e8 (18.2.1)

Markdown flavor: CommonMark / GFM

Description

Code spans and backslash escapes in an image description lose literal entity text when the flattened text is escaped for the alt attribute.

For ![`&`](u), current master emits alt="&", whose DOM value is &. The code span contains the literal text &, so the expected HTML is alt="&", whose DOM value is &. Numeric-looking entities and escaped ampersands have the same problem. This changes the actual alt text, not only the spelling of equivalent HTML entities.

Before image text is flattened, copy its inline tokens and encode literal code-span and escape text. Keep the existing attribute escaping. Nested formatting, links and images are handled through their child tokens without mutating the original token tree.

The exported TextRenderer and the parser.textRenderer field keep their existing plain-text behavior outside image rendering. Image rendering still uses that renderer, including custom methods; within that image-only call, literal code/escape token copies now contain encoded text. Tests cover the plain-text API, a custom strong renderer, and token immutability.

Seven exact-output cases cover named/numeric-looking code literals, escaped ampersands, normal entity controls and nested formatting/images. The decoded alt values agree with CommonMark.

Validation on Node 22.23.4:

  • Final tests with original implementation: 2035 specs passed, 2 failed; 258 unit tests passed, 1 failed.
  • With the fix: 2037 specs and 259 unit tests passed.
  • Full npm test passed, including builds/docs, UMD/CJS, types and lint.
  • git diff --check passed.

Contributor

  • Tests exist to ensure functionality and minimize regression.
  • No tests required.
  • If submitting a new feature, it has been documented.

Committer

  • CI is green (no forced merge required).
  • Squash and Merge PR following conventional commit guidelines.

hakarrr and others added 4 commits November 17, 2026 02:16
Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com>
Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com>
Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com>
Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com>
@vercel

vercel Bot commented Nov 16, 2026

Copy link
Copy Markdown

@hakarrr is attempting to deploy a commit to the MarkedJS Team on Vercel.

A member of the Team first needs to authorize it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant