From f46bb59e800f674c23d9476e607609109bfc26d1 Mon Sep 17 00:00:00 2001 From: Dave Cridland Date: Wed, 24 Jul 2024 13:07:45 +0100 Subject: [PATCH] Bugfix: Handle data nodes when mutating element value --- rapidxml.hpp | 13 +++++++++++++ test/round-trips.cpp | 17 +++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/rapidxml.hpp b/rapidxml.hpp index ad91a47..5cc6805 100644 --- a/rapidxml.hpp +++ b/rapidxml.hpp @@ -799,6 +799,8 @@ namespace rapidxml } void value(view_type const & v) { m_value = v; + this->value_raw(""); + if (this->m_parent) this->m_parent->dirty_parent(); } // Return true if the value has been decoded. bool value_decoded() const { @@ -945,7 +947,18 @@ namespace rapidxml } void value(view_type const & v) { + if (this->m_type == node_element) { + // Set the first data node to the value, if one exists. + for (auto node = m_first_node; node; node = node->m_next_sibling) { + if (node->type() == node_data) { + node->value(v); + break; + } + } + } m_value = v; + this->value_raw(""); + dirty(); } bool value_decoded() const { diff --git a/test/round-trips.cpp b/test/round-trips.cpp index 4ee949a..2015cba 100644 --- a/test/round-trips.cpp +++ b/test/round-trips.cpp @@ -121,6 +121,23 @@ TEST(RoundTrip, SimpleLtBody) { EXPECT_EQ(input, std::string(buffer.data(), buffer.size() - 1)); } +TEST(RoundTrip, MutateBody) { + const char input[] = "<"; + const char expected[] = "<"; + const char expected2[] = "new value"; + std::vector buffer{input, input + sizeof(input)}; + rapidxml::xml_document<> doc; + doc.parse(buffer.data()); + auto output = print(doc); + EXPECT_EQ(expected, output); + // Have we mutated the underlying buffer? + EXPECT_EQ(input, std::string(buffer.data(), buffer.size() - 1)); + doc.first_node()->value("new value"); + EXPECT_EQ(doc.first_node()->value_raw(), ""); + EXPECT_EQ(doc.first_node()->value(), "new value"); + EXPECT_EQ(expected2, print(doc)); +} + TEST(RoundTrip, Everything) { const char input[] = ""; const char expected[] = "";