Skip to content

Commit

Permalink
Always use absolute paths to get hrefs in SVG
Browse files Browse the repository at this point in the history
Also be more fault-tolerant when trying to render use tags.

Fix #1531.
  • Loading branch information
liZe committed Jan 3, 2022
1 parent fad0f35 commit 302f09d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
8 changes: 6 additions & 2 deletions weasyprint/svg/__init__.py
Expand Up @@ -12,6 +12,7 @@

from cssselect2 import ElementWrapper

from ..urls import get_url_attribute
from .bounding_box import bounding_box, is_valid_bounding_box
from .css import parse_declarations, parse_stylesheets
from .defs import (
Expand Down Expand Up @@ -175,9 +176,12 @@ def get_viewbox(self):
return tuple(
float(number) for number in normalize(viewbox).split())

def get_href(self):
def get_href(self, base_url):
"""Get the href attribute, with or without a namespace."""
return self.get('{http://www.w3.org/1999/xlink}href', self.get('href'))
for attr_name in ('{http://www.w3.org/1999/xlink}href', 'href'):
url = get_url_attribute(self, attr_name, base_url)
if url:
return url

@staticmethod
def process_whitespace(string, preserve):
Expand Down
8 changes: 4 additions & 4 deletions weasyprint/svg/defs.py
Expand Up @@ -25,14 +25,14 @@ def use(svg, node, font_size):
if attribute in node.attrib:
del node.attrib[attribute]

parsed_url = parse_url(node.get_href())
if parsed_url.fragment and not parsed_url.path:
parsed_url = parse_url(node.get_href(svg.url))
if parsed_url.fragment and parsed_url[:3] == parse_url(svg.url)[:3]:
if parsed_url.fragment in svg.use_cache:
tree = svg.use_cache[parsed_url.fragment].copy()
else:
try:
tree = svg.tree.get_child(parsed_url.fragment).copy()
except AttributeError:
except Exception:
return
else:
svg.use_cache[parsed_url.fragment] = tree
Expand All @@ -41,7 +41,7 @@ def use(svg, node, font_size):
try:
bytestring_svg = svg.url_fetcher(url)
use_svg = SVG(bytestring_svg, url)
except TypeError:
except Exception:
return
else:
use_svg.get_intrinsic_size(font_size)
Expand Down
4 changes: 1 addition & 3 deletions weasyprint/svg/images.py
Expand Up @@ -6,8 +6,6 @@
"""

from urllib.parse import urljoin

from .utils import preserve_ratio


Expand All @@ -34,7 +32,7 @@ def image(svg, node, font_size):
x, y = svg.point(node.get('x'), node.get('y'), font_size)
svg.stream.transform(e=x, f=y)
base_url = node.get('{http://www.w3.org/XML/1998/namespace}base')
url = urljoin(base_url or svg.url, node.get_href())
url = node.get_href(base_url or svg.url)
image = svg.context.get_image_from_uri(url=url, forced_mime_type='image/*')
if image is None:
return
Expand Down

0 comments on commit 302f09d

Please sign in to comment.