Commit d45bb4e9 authored by Martin Owens's avatar Martin Owens 🤖

Add getElementByName function for getting things by inkscape label

parent a346c800
Pipeline #152645881 passed with stages
in 3 minutes and 55 seconds
......@@ -93,7 +93,11 @@ class SvgDocumentElement(DeprecatedSvgMixin, BaseElement):
if eid is not None:
eid = eid.strip()[4:-1] if eid.startswith('url(') else eid
eid = eid.lstrip('#')
return self.getElement('//{}[@id="{}"]'.format(elm, eid))
return self.getElement(f'//{elm}[@id="{eid}"]')
def getElementByName(self, name, elm='*'): # pylint: disable=invalid-name
"""Get an element by it's inkscape:label (aka name)"""
return self.getElement(f'//{elm}[@inkscape:label="{name}"]')
def getElementsByHref(self, eid): # pylint: disable=invalid-name
"""Get elements by their href xlink attribute"""
......@@ -61,9 +61,9 @@
<polygon points="0,100 50,25 50,75 100,0" id="J" />
<g id="K">
<g id="L">
<line x1="5" y1="5" x2="5.5" y2="10" id="M" transform="translate(10, 10);" />
<g id="K" inkscape:label="Key">
<g id="L" inkscape:label="Elm">
<line x1="5" y1="5" x2="5.5" y2="10" id="M" transform="translate(10, 10);" inkscape:label="Mine" />
......@@ -214,6 +214,15 @@ class AttributeHandelingTestCase(SvgTestCase):
self.assertEqual(self.svg.getElementById('G').href, elem)
self.assertEqual(str(self.svg.getElementById('B').style), 'fill:#eee;joker:url(#plant54)')
def test_get_element_by_name(self):
"""Get elements by name"""
self.assertEqual(self.svg.getElementByName('Key').get('id'), 'K')
self.assertEqual(self.svg.getElementByName('Elm', 'svg:g').get('id'), 'L')
self.assertEqual(self.svg.getElementByName('Mine').get('id'), 'M')
self.assertEqual(self.svg.getElementByName('doesntexist'), None)
self.assertEqual(self.svg.getElementByName('Key', 'rect'), None)
class TransformationTestCase(SvgTestCase):
"""Test transformative functions"""
def test_bounding_box(self):
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment