Skip to content

fix: encode parentheses in image src#86

Open
greymoth-jp wants to merge 1 commit into
crosstype:masterfrom
greymoth-jp:fix/encode-img-src-parens
Open

fix: encode parentheses in image src#86
greymoth-jp wants to merge 1 commit into
crosstype:masterfrom
greymoth-jp:fix/encode-img-src-parens

Conversation

@greymoth-jp

Copy link
Copy Markdown

The img translator writes the src attribute straight into the markdown destination with no escaping. When the URL contains a parenthesis, the output is broken markdown.

For example:

<img src="http://example.com/foo)bar.png" alt="x">

currently produces:

![x](http://example.com/foo)bar.png)

A parser reads the first ) as the end of the destination, so this renders as an image of http://example.com/foo followed by the literal text bar.png).

The a translator already guards href against this (added in #26). This applies the same protection to image src by percent-encoding ( and ).

I kept the encoding to parentheses only. The link rule also encodes _ and *, but those are not special inside a destination, and encoding them would change common image URLs (the existing keepDataImages test expects normal_img.jpg to stay as-is).

The Image (img) test now covers a parenthesised URL.

An <img> whose src contains parentheses produced an unescaped markdown
destination: `<img src="a)b.png">` became `![](a)b.png)`, which a parser
reads as an image of `a` followed by the literal text `b.png)`. Encode the
parens in the image src, mirroring the guard the link translator already
applies to href.
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