From: Magnus Jacobsson Date: Wed, 24 Aug 2022 09:50:35 +0000 (+0200) Subject: tests: SVGAnalyzer: add finding of child of specific type to SVGElement X-Git-Tag: 6.0.2~34^2~6 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1a7a816d54d3aec4b00714e9213d02b3fd98c7eb;p=graphviz tests: SVGAnalyzer: add finding of child of specific type to SVGElement --- diff --git a/tests/svg_element.cpp b/tests/svg_element.cpp index ab1018b7b..42c0a77f8 100644 --- a/tests/svg_element.cpp +++ b/tests/svg_element.cpp @@ -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"); diff --git a/tests/svg_element.h b/tests/svg_element.h index a2371064f..9c329e592 100644 --- a/tests/svg_element.h +++ b/tests/svg_element.h @@ -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