]> granicus.if.org Git - graphviz/commitdiff
tests: SVGAnalyzer: add finding of child of specific type to SVGElement
authorMagnus Jacobsson <Magnus.Jacobsson@berotec.se>
Wed, 24 Aug 2022 09:50:35 +0000 (11:50 +0200)
committerMagnus Jacobsson <Magnus.Jacobsson@berotec.se>
Tue, 20 Sep 2022 15:53:06 +0000 (17:53 +0200)
tests/svg_element.cpp
tests/svg_element.h

index ab1018b7bae46b58f366ba03d3552a5ff94785a5..42c0a77f8dd7bca1bb47d764c94e03f75198daa4 100644 (file)
@@ -416,6 +416,22 @@ SVG::SVGRect SVG::SVGElement::outline_bbox(bool throw_if_bbox_not_defined) {
   return *m_bbox;
 }
 
+SVG::SVGElement &SVG::SVGElement::find_child(const SVG::SVGElementType type,
+                                             std::size_t index) {
+  std::size_t i = 0;
+  for (auto &child : children) {
+    if (child.type == type) {
+      if (i == index) {
+        return child;
+      }
+      ++i;
+    }
+  }
+  throw std::runtime_error(
+      fmt::format("SVG element only has {} \"{}\" children, index {} not found",
+                  i, tag(type), index));
+}
+
 SVG::SVGRect SVG::SVGElement::text_bbox() const {
   assert(type == SVG::SVGElementType::Text && "Not a 'text' element");
 
index a2371064fcefca6c0866a2d353f756670ab8ffb8..9c329e5925ad7a5998d2739a4cc0a604b5cd1075 100644 (file)
@@ -156,6 +156,10 @@ public:
   /// throw an exception unless the `throw_if_bbox_not_defined` parameter is
   /// `false`.
   SVG::SVGRect bbox(bool throw_if_bbox_not_defined = true);
+  /// Return the element of the given type with the specified index found among
+  /// the element's children. Throwns an exception if there's no such element
+  /// with the specified index.
+  SVG::SVGElement &find_child(SVG::SVGElementType type, std::size_t index = 0);
   bool is_closed_shape_element() const;
   bool is_shape_element() const;
   /// Return the outline bounding box of the element. The outline bounding box