Decode HTML entities in image src when read back from HTML#349
Open
kirthi-b wants to merge 1 commit into
Open
Conversation
The transform plugin reads image sources back out of built HTML. An HTML attribute like src="rose&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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
When the plugin read that attribute it passed the value through literally, so it tried to stat a file called
rose&rose.jpg, which does not exist, and the build failed:Repro: an Eleventy site using eleventyImageTransformPlugin with an image whose filename contains an ampersand, referenced from HTML as
src="rose&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
&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