Skip to content

Commit

Permalink
Fix relative paths for SVG files stored as data URLs
Browse files Browse the repository at this point in the history
Fix #1641.
  • Loading branch information
liZe committed May 10, 2022
1 parent bc40535 commit 50ad4d1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 23 deletions.
61 changes: 39 additions & 22 deletions tests/draw/svg/test_defs.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,51 @@
"""Test how SVG definitions are drawn."""

from base64 import b64encode

from ...testing_utils import assert_no_logs
from .. import assert_pixels

SVG = '''
<svg width="10px" height="10px" xmlns="http://www.w3.org/2000/svg">
<defs>
<rect id="rectangle" width="5" height="2" fill="red" />
</defs>
<use href="#rectangle" />
<use href="#rectangle" x="3" y="3" />
<use href="#rectangle" x="5" y="6" />
</svg>
'''

RESULT = '''
RRRRR_____
RRRRR_____
__________
___RRRRR__
___RRRRR__
__________
_____RRRRR
_____RRRRR
__________
__________
'''


@assert_no_logs
def test_use():
assert_pixels('use', 10, 10, '''
RRRRR_____
RRRRR_____
__________
___RRRRR__
___RRRRR__
__________
_____RRRRR
_____RRRRR
__________
__________
''', '''
assert_pixels('use', 10, 10, RESULT, '''
<style>
@page { size: 10px }
svg { display: block }
</style>
<svg width="10px" height="10px" xmlns="http://www.w3.org/2000/svg"
xlink="http://www.w3.org/1999/xlink">
<defs>
<rect id="rectangle" width="5" height="2" fill="red" />
</defs>
<use xlink:href="#rectangle" />
<use xlink:href="#rectangle" x="3" y="3" />
<use xlink:href="#rectangle" x="5" y="6" />
</svg>
''')
''' + SVG)


@assert_no_logs
def test_use_base64():
base64_svg = b64encode(SVG.encode()).decode()
assert_pixels('use_base64', 10, 10, RESULT, '''
<style>
@page { size: 10px }
img { display: block }
</style>
<img src="data:image/svg+xml;base64,''' + base64_svg + '"/>')
5 changes: 4 additions & 1 deletion weasyprint/svg/defs.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ def use(svg, node, font_size):
del node.attrib[attribute]

parsed_url = parse_url(node.get_href(svg.url))
if parsed_url.fragment and parsed_url[:3] == parse_url(svg.url)[:3]:
svg_url = parse_url(svg.url)
if svg_url.scheme == 'data':
svg_url = parse_url('')
if parsed_url.fragment and parsed_url[:3] == svg_url[:3]:
if parsed_url.fragment in svg.use_cache:
tree = svg.use_cache[parsed_url.fragment].copy()
else:
Expand Down

0 comments on commit 50ad4d1

Please sign in to comment.