From 99dc2aef41024d4a8444497d7b1afa36933af8d0 Mon Sep 17 00:00:00 2001 From: Magnus Jacobsson Date: Fri, 29 Jul 2022 15:55:27 +0200 Subject: [PATCH] tests: SvgAnalyzer: add storage of the Graphviz graph ID also on 'svg' element --- tests/svg_analyzer.cpp | 18 ++++++++++++++++++ tests/svg_analyzer.h | 3 +++ 2 files changed, 21 insertions(+) diff --git a/tests/svg_analyzer.cpp b/tests/svg_analyzer.cpp index 67b1d8fee..239ea91ff 100644 --- a/tests/svg_analyzer.cpp +++ b/tests/svg_analyzer.cpp @@ -69,6 +69,19 @@ void SVGAnalyzer::on_enter_element_title() { void SVGAnalyzer::on_exit_element() { m_elements_in_process.pop_back(); } +SVG::SVGElement &SVGAnalyzer::grandparent_element() { + if (m_elements_in_process.empty()) { + throw std::runtime_error{"No current element to get grandparent of"}; + } + if (m_elements_in_process.size() == 1) { + throw std::runtime_error{"No parent element"}; + } + if (m_elements_in_process.size() == 2) { + throw std::runtime_error{"No grandparent element"}; + } + return *m_elements_in_process.end()[-3]; +} + SVG::SVGElement &SVGAnalyzer::parent_element() { if (m_elements_in_process.empty()) { throw std::runtime_error{"No current element to get parent of"}; @@ -110,6 +123,11 @@ void SVGAnalyzer::set_text(std::string_view text) { throw std::runtime_error{"Unexpected parent element of 'title' element"}; } parent_element().graphviz_id = text; + // If the 'g' element corresponds to the graph, set the Graphviz ID also on + // the top level 'svg' element. + if (grandparent_element().type == SVG::SVGElementType::Svg) { + grandparent_element().graphviz_id = text; + } } } diff --git a/tests/svg_analyzer.h b/tests/svg_analyzer.h index b788ced99..6a005876c 100644 --- a/tests/svg_analyzer.h +++ b/tests/svg_analyzer.h @@ -49,6 +49,9 @@ private: /// Enter a new SVG element retrieved by the SVG document traverser into the /// list of elements currently being processed void enter_element(SVG::SVGElementType type); + /// Get the grandparent element of the current element being processed by the + /// SVG document traverser + SVG::SVGElement &grandparent_element(); /// Get the parent element of the current element being processed by the SVG /// document traverser SVG::SVGElement &parent_element(); -- 2.40.0