Skip to content

Decode HTML entities in image src when read back from HTML#349

Open
kirthi-b wants to merge 1 commit into
11ty:mainfrom
kirthi-b:fix/unescape-html-image-src
Open

Decode HTML entities in image src when read back from HTML#349
kirthi-b wants to merge 1 commit into
11ty:mainfrom
kirthi-b:fix/unescape-html-image-src

Conversation

@kirthi-b

Copy link
Copy Markdown

The transform plugin (eleventyImageTransformPlugin) reads image sources back out of the built HTML. A file whose name contains an ampersand, like rose&rose.jpg, has to be written into HTML as an escaped attribute:

<img src="rose&amp;rose.jpg" alt="By any other name">

When the plugin read that attribute it passed the value through literally, so it tried to stat a file called rose&amp;rose.jpg, which does not exist, and the build failed:

[11ty] 3. ENOENT: no such file or directory, stat 'rose&amp;rose.jpg'

Repro: an Eleventy site using eleventyImageTransformPlugin with an image whose filename contains an ampersand, referenced from HTML as src="rose&amp;rose.jpg".

Util.normalizeImageSource already had an isViaHtml branch meant to decode the value coming from HTML, but it only ran decodeURIComponent, which handles URL percent-encoding and leaves HTML entities like &amp; untouched. This decodes the HTML entities first, using decodeHTMLAttribute from the entities package that is already a dependency (and is already used for the matching escape on the way out in generate-html.js). That mirrors how a browser reads the attribute: entity-decode the attribute value, then treat it as a path.

Added a unit test in test/test.js covering a named entity, a numeric entity, and the unflagged case that should not be decoded.

Fixes #307

The transform plugin reads image sources back out of built HTML. An HTML
attribute like src="rose&amp;rose.jpg" refers to a file named
rose&rose.jpg on disk, but the entity was passed through literally, so the
path was wrong and the build failed with ENOENT. Decode HTML entities in
the attribute value before resolving the path.

Fixes 11ty#307
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

eleventy-img doesn't unescape HTML-escaped image names when reading them from HTML to transform

1 participant