From: Magnus Jacobsson Date: Mon, 25 Jul 2022 06:49:33 +0000 (+0200) Subject: tests: SvgAnalyzer: add handling of SVG element text node contents X-Git-Tag: 5.0.1~7^2~25 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=809d79df511c1a326f84741c46f9de32eafee8d7;p=graphviz tests: SvgAnalyzer: add handling of SVG element text node contents --- diff --git a/tests/svg_analyzer.cpp b/tests/svg_analyzer.cpp index 9a1e5a106..6e18fc91d 100644 --- a/tests/svg_analyzer.cpp +++ b/tests/svg_analyzer.cpp @@ -88,6 +88,11 @@ void SVGAnalyzer::enter_element(SVG::SVGElementType type) { m_elements_in_process.push_back(&element.children.back()); } +void SVGAnalyzer::set_text(std::string_view text) { + auto &element = current_element(); + element.text = text; +} + std::string SVGAnalyzer::svg_string(std::size_t indent_size) const { std::string output{}; output += m_svg.to_string(indent_size); diff --git a/tests/svg_analyzer.h b/tests/svg_analyzer.h index 31f9507df..208a57b2f 100644 --- a/tests/svg_analyzer.h +++ b/tests/svg_analyzer.h @@ -2,6 +2,7 @@ #include #include +#include #include #include "svg_analyzer_interface.h" @@ -26,6 +27,7 @@ public: void on_enter_element_text() override; void on_enter_element_title() override; void on_exit_element() override; + void set_text(std::string_view text) override; std::size_t num_svgs() const { return m_num_svgs; }; std::size_t num_groups() const { return m_num_groups; }; std::size_t num_circles() const { return m_num_circles; }; diff --git a/tests/svg_analyzer_interface.h b/tests/svg_analyzer_interface.h index 57d24ab74..7ea80454a 100644 --- a/tests/svg_analyzer_interface.h +++ b/tests/svg_analyzer_interface.h @@ -1,5 +1,7 @@ #pragma once +#include + /** * @brief The ISVGAnalyzer class is an interface class declaring * callbacks that can be implemented by an SVGAnalyzer class. Its @@ -24,4 +26,5 @@ public: virtual void on_enter_element_text() = 0; virtual void on_enter_element_title() = 0; virtual void on_exit_element() = 0; + virtual void set_text(std::string_view text) = 0; }; diff --git a/tests/svg_element.cpp b/tests/svg_element.cpp index 20bac09e0..df0517f00 100644 --- a/tests/svg_element.cpp +++ b/tests/svg_element.cpp @@ -5,6 +5,29 @@ SVG::SVGElement::SVGElement(SVGElementType type) : type(type) {} +static std::string xml_encode(const std::string &text) { + std::string out; + for (const char &ch : text) { + switch (ch) { + case '>': + out += ">"; + break; + case '<': + out += "<"; + break; + case '-': + out += "-"; + break; + case '&': + out += "&"; + break; + default: + out += ch; + } + } + return out; +} + std::string SVG::SVGElement::to_string(std::size_t indent_size = 2) const { std::string output; output += R"()" @@ -26,14 +49,20 @@ void SVG::SVGElement::to_string_impl(std::string &output, output += "<"; output += tag(type); - if (children.empty()) { + if (children.empty() && text.empty()) { output += "/>\n"; } else { - output += ">\n"; - for (const auto &child : children) { - child.to_string_impl(output, indent_size, current_indent + indent_size); + output += ">"; + if (!text.empty()) { + output += xml_encode(text); + } + if (!children.empty()) { + output += "\n"; + for (const auto &child : children) { + child.to_string_impl(output, indent_size, current_indent + indent_size); + } + output += indent_str; } - output += indent_str; output += "\n"; diff --git a/tests/svg_element.h b/tests/svg_element.h index cd9cc4ecc..7ab2210bb 100644 --- a/tests/svg_element.h +++ b/tests/svg_element.h @@ -34,6 +34,9 @@ public: std::string to_string(std::size_t indent_size) const; std::vector children; + /// The SVG element text node contents. Not to be confused with an SVG `text` + /// element + std::string text; /// The type of SVG element const SVGElementType type; diff --git a/tests/svgpp_context.cpp b/tests/svgpp_context.cpp index f3915e6f6..e87a5cdbc 100644 --- a/tests/svgpp_context.cpp +++ b/tests/svgpp_context.cpp @@ -179,7 +179,6 @@ void SvgppContext::set_impl(svgpp::tag::attribute::points &points, // ignore for now } -void SvgppContext::set_text_impl(const std::any &range) { - (void)range; - // ignore for now +void SvgppContext::set_text(boost::iterator_range v) { + m_svgAnalyzer->set_text({v.begin(), v.end()}); } diff --git a/tests/svgpp_context.h b/tests/svgpp_context.h index b086ebce5..d23e57804 100644 --- a/tests/svgpp_context.h +++ b/tests/svgpp_context.h @@ -2,6 +2,7 @@ #include +#include #include class ISVGAnalyzer; @@ -62,13 +63,10 @@ public: void set(svgpp::tag::attribute::y y, double v); void set(svgpp::tag::attribute::width width, double v); void set(svgpp::tag::attribute::height height, double v); - template void set_text(const Range &range) { - set_text_impl(range); - } + void set_text(boost::iterator_range v); private: void set_impl(svgpp::tag::attribute::points &points, const std::any &range); - void set_text_impl(const std::any &range); ISVGAnalyzer *m_svgAnalyzer = nullptr; }; diff --git a/tests/test_svg_analyzer.cpp b/tests/test_svg_analyzer.cpp index aba839a13..6e5f57bab 100644 --- a/tests/test_svg_analyzer.cpp +++ b/tests/test_svg_analyzer.cpp @@ -134,9 +134,12 @@ TEST_CASE( CHECK(recreated_svg.find("") != std::string::npos); CHECK(recreated_svg.find("") != std::string::npos); CHECK(recreated_svg.find("") != std::string::npos); - CHECK(recreated_svg.find("") != std::string::npos); + CHECK(recreated_svg.find("<title>g1") != std::string::npos); + CHECK(recreated_svg.find("a") != std::string::npos); + CHECK(recreated_svg.find("b") != std::string::npos); if (shape != "point") { - CHECK(recreated_svg.find("") != std::string::npos); + CHECK(recreated_svg.find("a") != std::string::npos); + CHECK(recreated_svg.find("b") != std::string::npos); } CHECK(recreated_svg.find("") != std::string::npos); CHECK(recreated_svg.find("") != std::string::npos);