Repository navigation
Fix image resolution when the SVG file is given as a relative file name - #458
Open
entity-0-0-2 wants to merge 1 commit into
Open
entity-0-0-2 wants to merge 1 commit into
entity-0-0-2 wants to merge 1 commit into
Conversation
os.path.dirname turned a bare file name into an empty string, and the appended slash made the base URL the root directory, so relative image hrefs were looked up at / instead of next to the SVG file. Pass the document URL as base and let parse_url resolve against it.
Member
|
Pull requests are not advertisements. |
This branch has not been deployed
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.
Fixes #420.
When the SVG file is passed as a bare relative file name, any relative image href used to fail with a file-not-found error pointing at the root of the filesystem, so users had to pass absolute paths to get images to load.
The image drawing code built the base URL as os.path.dirname(node.url) + '/' (cairosvg/image.py:24). With node.url equal to bug.svg, dirname returns an empty string and the base becomes /, which parse_url then treats as an existing directory and joins the href onto, giving /bug_photo.jpg. This came in with 0a73c39 (Add URLFetcher, 2016-11-02); before that the code used os.path.join(os.path.dirname(node.url), url), where an empty dirname is harmless. Passing a directory as input hid the problem, which is why it kept resurfacing only for plain file names.
The fix passes node.url itself as the base and lets parse_url do its documented job of taking the folder part of the base. Absolute paths, file URLs, nested folders and remote URLs resolve exactly as before.
A test renders an SVG referencing a sibling image from a bare relative file name; it failed with a URLError on /image.png before the change and passes after it.