Skip to content

Commit

Permalink
Add a get_href function allowing href with no namespace
Browse files Browse the repository at this point in the history
The namespace is now deprecated in CSS2.
  • Loading branch information
liZe committed May 31, 2019
1 parent c030149 commit bcc808e
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cairosvg/bounding_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ def bounding_box_group(surface, node):

def bounding_box_use(surface, node):
"""Get the bounding box of a ``use`` node."""
href = parse_url(node.get('{http://www.w3.org/1999/xlink}href')).geturl()
href = parse_url(node.get_href()).geturl()
tree = Tree(
url=href, url_fetcher=node.url_fetcher, parent=node,
unsafe=node.unsafe)
Expand Down
5 changes: 2 additions & 3 deletions cairosvg/defs.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@
def update_def_href(surface, def_name, def_dict):
"""Update the attributes of the def according to its href attribute."""
def_node = def_dict[def_name]
href = parse_url(
def_node.get('{http://www.w3.org/1999/xlink}href')).fragment
href = parse_url(def_node.get_href()).fragment
if href in def_dict:
update_def_href(surface, href, def_dict)
href_node = def_dict[href]
Expand Down Expand Up @@ -359,7 +358,7 @@ def use(surface, node):
del node['viewBox']
if 'mask' in node:
del node['mask']
href = parse_url(node.get('{http://www.w3.org/1999/xlink}href')).geturl()
href = parse_url(node.get_href()).geturl()
tree = Tree(
url=href, url_fetcher=node.url_fetcher, parent=node,
tree_cache=surface.tree_cache, unsafe=node.unsafe)
Expand Down
2 changes: 1 addition & 1 deletion cairosvg/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def image(surface, node):
base_url = node.get('{http://www.w3.org/XML/1998/namespace}base')
if not base_url and node.url:
base_url = os.path.dirname(node.url) + '/'
url = parse_url(node.get('{http://www.w3.org/1999/xlink}href'), base_url)
url = parse_url(node.get_href(), base_url)
image_bytes = node.fetch_url(url, 'image/*')

if len(image_bytes) < 5:
Expand Down
11 changes: 8 additions & 3 deletions cairosvg/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
'dx',
'dy',
'{http://www.w3.org/1999/xlink}href',
'href',
))

COLOR_ATTRIBUTES = frozenset((
Expand Down Expand Up @@ -272,9 +273,10 @@ def text_children(self, element, trailing_space, text_root=False):
trailing_space = self.text.endswith(' ')
for child_element in element.iter_children():
child = child_element.etree_element
if child.tag == '{http://www.w3.org/2000/svg}tref':
url = parse_url(child.get(
'{http://www.w3.org/1999/xlink}href')).geturl()
if child.tag in ('{http://www.w3.org/2000/svg}tref', 'tref'):
href = child.get(
'{http://www.w3.org/1999/xlink}href', child.get('href'))
url = parse_url(href).geturl()
child_tree = Tree(
url=url, url_fetcher=self.url_fetcher, parent=self,
unsafe=self.unsafe)
Expand Down Expand Up @@ -322,6 +324,9 @@ def text_children(self, element, trailing_space, text_root=False):

return children, trailing_space

def get_href(self):
return self.get('{http://www.w3.org/1999/xlink}href', self.get('href'))


class Tree(Node):
"""SVG tree."""
Expand Down
4 changes: 1 addition & 3 deletions cairosvg/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ def text(surface, node, draw_as_text=False):
ascent, descent, _, max_x_advance, max_y_advance = (
surface.context.font_extents())

text_path_href = parse_url(
node.get('{http://www.w3.org/1999/xlink}href', '') or
node.parent.get('{http://www.w3.org/1999/xlink}href', ''))
text_path_href = parse_url(node.get_href() or node.parent.get_href() or '')
if text_path_href.fragment:
text_path = surface.paths.get(text_path_href.fragment)
else:
Expand Down

0 comments on commit bcc808e

Please sign in to comment.