meerk40t / svgelements

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

id gets removed from Text element #237

Closed sfraczek closed 12 months ago

sfraczek commented 1 year ago

Hi,

I'm trying to parse like this:

for svg_edge_label in svg.get_element_by_id('edge-labels'):
                        if isinstance(svg_edge_label, Text):
                            if svg_edge_label.id == edge_label_id:

an svg Text element like this:

<g id="edge-labels" class="edge-labels">
        <text class="edge-label" id="edge-label-edge-eltwise_Y"
            transform="translate(24.49658203125,48)"
            style="opacity: 1; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe WPC&quot;, &quot;Segoe UI&quot;, Ubuntu, &quot;Droid Sans&quot;, sans-serif, &quot;PingFang SC&quot;; font-size: 10px;">
            <tspan xml:space="preserve" dy="1em" x="1">32×3×100×100</tspan>
        </text>
</g>

and I found that id is not there. It is removed here: https://github.com/meerk40t/svgelements/blob/89a6333c052b11c9f569205321ca8e9abaaf92a9/svgelements/svgelements.py#L9055-L9056

I wonder why it is removed and how can I solve this? I need this id to match the text to the edge. Do you have any suggestions on how to proceed with this?

Thanks

sfraczek commented 1 year ago

I've figured out that svg.get_element_by_id('edge-labels') returns text and tspan side by side so I get two elements per single text object with nested tspan object. I solved the problem by pairwise iteration so I can extract id from text and text from tspan. I still wonder if this is a bug or not (I don't know anything about svg).

tatarize commented 12 months ago

It's removed because only the object in question gets that id. Ids unlike some other elements does not propagate to children.

The problem there is weirdly that the text has the ID but tspan has the actual text. It's a bit weird but it's actually correct. The text object is called "edge-label-edge-eltwise_Y" the tspan inside that does not have that ID and shouldn't.

tatarize commented 12 months ago

Even with javascript I think looking up that particular tspan based on id would be decidedly hard.

sfraczek commented 12 months ago

Thanks