@@ -284,6 +284,13 @@ def pop_rotation(self, original_rotate, rotate):
284
284
str (rotate .pop (0 ) if rotate else original_rotate [- 1 ])
285
285
for i in range (len (self .text )))
286
286
287
+ def override_iter (self , iterator ):
288
+ """Override node’s children iterator."""
289
+ # As special methods are bound to classes and not instances, we have to
290
+ # create and assign a new type.
291
+ self .__class__ = type (
292
+ 'Node' , (Node ,), {'__iter__' : lambda _ : iterator })
293
+
287
294
288
295
class SVG :
289
296
"""An SVG document."""
@@ -737,11 +744,7 @@ def inherit_element(self, element, defs):
737
744
if key not in element .attrib :
738
745
element .attrib [key ] = value
739
746
if next (iter (element ), None ) is None :
740
- # Override element’s __iter__ with parent’s __iter__. As special
741
- # methods are bound to classes and not instances, we have to create
742
- # and assign a new type.
743
- element .__class__ = type (
744
- 'Node' , (Node ,), {'__iter__' : lambda self : parent .__iter__ ()})
747
+ element .override_iter (parent .__iter__ ())
745
748
746
749
def calculate_bounding_box (self , node , font_size , stroke = True ):
747
750
"""Calculate the bounding box of a node."""
0 commit comments