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

Add getElementsByClass function

parent 46951fac
Pipeline #158533886 passed with stages
in 3 minutes and 38 seconds
......@@ -99,6 +99,11 @@ class SvgDocumentElement(DeprecatedSvgMixin, BaseElement):
"""Get an element by it's inkscape:label (aka name)"""
return self.getElement(f'//{elm}[@inkscape:label="{name}"]')
def getElementsByClass(self, class_name): # pylint: disable=invalid-name
"""Get elements by it's class name"""
from inkex.styles import ConditionalRule
return self.xpath(ConditionalRule(f".{class_name}").to_xpath())
def getElementsByHref(self, eid): # pylint: disable=invalid-name
"""Get elements by their href xlink attribute"""
return self.xpath('//*[@xlink:href="#{}"]'.format(eid))
......
......@@ -55,6 +55,7 @@
<g
id="g3930">
<path
class="oranger bee frog"
transform="matrix(-0.97176714,-0.23594199,0.23594199,-0.97176714,541.8979,1030.862)"
inkscape:transform-center-y="21.296232"
inkscape:transform-center-x="0.78818963"
......@@ -74,6 +75,7 @@
sodipodi:type="star" />
<text
sodipodi:linespacing="125%"
class="frog apple"
id="text3926"
y="723.79077"
x="245.71429"
......@@ -97,6 +99,7 @@
style="fill:#ffaaaa;fill-opacity:1"
d="m 522.85714,488.07648 c 0,73.37498 -71.63444,132.85715 -160,132.85715 -88.36556,0 -159.99999,-59.48217 -159.99999,-132.85715 0,-73.37497 71.63443,-132.85714 159.99999,-132.85714 88.36556,0 160,59.48217 160,132.85714 z"
id="path3900"
class="frog"
inkscape:connector-curvature="0" />
<text
xml:space="preserve"
......@@ -116,6 +119,7 @@
height="134.28572"
width="385.71429"
id="rect3898"
class="apple frog"
style="fill:#cccccc;fill-opacity:1" />
<text
sodipodi:linespacing="125%"
......
......@@ -66,6 +66,15 @@ class BasicSvgTest(TestCase):
self.assertEqual(doc.selection['apples'], doc)
self.assertEqual(doc.selection.first(), doc)
def test_svg_by_class(self):
"""Select elements by class"""
doc = svg_file(self.data_file('svg', 'multilayered-test.svg'))
elems = doc.getElementsByClass('frog')
self.assertEqual([elem.get_id() for elem in elems],
['path3902', 'text3926', 'path3900', 'rect3898'])
elems = doc.getElementsByClass('apple')
self.assertEqual([elem.get_id() for elem in elems], ['text3926', 'rect3898'])
def test_svg_by_href(self):
"""Select element by xlink href"""
doc = svg_file(self.data_file('svg', 'multilayered-test.svg'))
......
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