Skip to content

Commit

Permalink
Bugfix: Handle data nodes when mutating element value
Browse files Browse the repository at this point in the history
  • Loading branch information
dwd committed Jul 24, 2024
1 parent d00e499 commit f46bb59
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
13 changes: 13 additions & 0 deletions rapidxml.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
17 changes: 17 additions & 0 deletions test/round-trips.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,23 @@ TEST(RoundTrip, SimpleLtBody) {
EXPECT_EQ(input, std::string(buffer.data(), buffer.size() - 1));
}

TEST(RoundTrip, MutateBody) {
const char input[] = "<simple arg=\"&apos;\">&lt;</simple>";
const char expected[] = "<simple arg=\"'\">&lt;</simple>";
const char expected2[] = "<simple arg=\"'\">new value</simple>";
std::vector<char> buffer{input, input + sizeof(input)};
rapidxml::xml_document<> doc;
doc.parse<rapidxml::parse_full>(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[] = "<?xml charset='utf-8' ?><!DOCTYPE ><simple arg=\"&apos;\"><!-- Comment here --></simple>";
const char expected[] = "<?xml charset=\"utf-8\"?><!DOCTYPE ><simple arg=\"'\"><!-- Comment here --></simple>";
Expand Down

0 comments on commit f46bb59

Please sign in to comment.