diff options
author | Benjamin Paassen <bpaassen@techfak.uni-bielefeld.de> | 2015-01-13 11:26:20 +0100 |
---|---|---|
committer | Benjamin Paassen <bpaassen@techfak.uni-bielefeld.de> | 2015-01-13 11:26:20 +0100 |
commit | 3311fa10fdd841a1b2d6146b848166f3da6e5487 (patch) | |
tree | 6da1f5822d2c6b52eb74fb78bc5f7af3b68c4bb3 | |
parent | 83b034d47728a7519128b2777ac95fa0b558f882 (diff) |
escaped attribute values in XML serialization.
-rw-r--r-- | src/core/XML.cpp | 57 |
1 files changed, 29 insertions, 28 deletions
diff --git a/src/core/XML.cpp b/src/core/XML.cpp index 9c23e3b..528e1cd 100644 --- a/src/core/XML.cpp +++ b/src/core/XML.cpp @@ -36,6 +36,33 @@ void Node::serialize(std::ostream &out, const std::string &doctype, bool pretty) doSerialize(out, 0, pretty); } +static std::string escapePredefinedEntities(const std::string &input) +{ + std::stringstream ss; + for (const char &c : input) { + switch (c) { + case '<': + ss << "<"; + break; + case '>': + ss << ">"; + break; + case '&': + ss << "&"; + break; + case '\'': + ss << "'"; + break; + case '\"': + ss << """; + break; + default: + ss << c; + } + } + return std::move(ss.str()); +} + void Element::doSerialize(std::ostream &out, unsigned int tabdepth, bool pretty) { if (pretty) { @@ -45,7 +72,8 @@ void Element::doSerialize(std::ostream &out, unsigned int tabdepth, bool pretty) } out << '<' << name; for (auto &a : attributes) { - out << ' ' << a.first << "=\"" << a.second << '\"'; + out << ' ' << a.first << "=\"" << escapePredefinedEntities(a.second) + << '\"'; } // if we have no children, we close the tag immediately. if (children.size() == 0) { @@ -73,33 +101,6 @@ void Element::doSerialize(std::ostream &out, unsigned int tabdepth, bool pretty) } } -static std::string escapePredefinedEntities(const std::string &input) -{ - std::stringstream ss; - for (const char &c : input) { - switch (c) { - case '<': - ss << "<"; - break; - case '>': - ss << ">"; - break; - case '&': - ss << "&"; - break; - case '\'': - ss << "'"; - break; - case '\"': - ss << """; - break; - default: - ss << c; - } - } - return std::move(ss.str()); -} - void Text::doSerialize(std::ostream &out, unsigned int tabdepth, bool pretty) { if (pretty) { |