meerk40t / svgelements

SVG Parsing for Elements, Paths, and other SVG Objects.
MIT License
132 stars 29 forks source link

may be bug with conditional in SVG.elements() #114

Closed derVedro closed 3 years ago

derVedro commented 3 years ago

If I understand it right, elements method takes a test function as conditional argument and makes a generator that returns only the elements that satisfy the test condition. Let me show a example:

import io
from svgelements import SVG, Path

svg_str = io.StringIO("""<svg version="1.0" xmlns="http://www.w3.org/2000/svg"
 width="500" height="500" viewBox="0 0 500 500" preserveAspectRatio="xMidYMid meet">
 <path d="M50 150 C50 50 200 50 200 150 C200 50 350 50 350 150 z"/>
 <path d="M350 250 C50 50 200 50 200 150 C200 50 350 50 350 150 z"/> 
</svg>""")

for el in for s in SVG.parse(svg_str).elements(conditional=lambda el: isinstance(el, Path)):
    print(type(s))

I expect to get only Path objects. But that's not the case. I get three elements, first of them is a SVG that contains all the Paths.

tatarize commented 3 years ago

That should be the use of conditional there. Yes. it does not fail.

    def elements(self, conditional=None):
        yield self
        for q in self.select(conditional):
            yield q

The offending code here tests the conditional with the select sub-elements but not with self which is merely yielded without qualification. I've corrected that and It'll be out in the next version due out after the rest of the test code passes including the new test which basically consists of your issue here.

tatarize commented 3 years ago

Fixed in latest version.

Thank you for bringing this to my attention. Sorry I was late in reviewing it.