From afecffd956869144266d0f17f5c76ec0d4012ba2 Mon Sep 17 00:00:00 2001 From: actboy168 Date: Thu, 10 Dec 2020 16:10:37 +0800 Subject: [PATCH] Attribute value supports rml escape --- Include/RmlUi/Core/StringUtilities.h | 3 +++ Source/Core/BaseXMLParser.cpp | 2 +- Source/Core/StringUtilities.cpp | 39 ++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/Include/RmlUi/Core/StringUtilities.h b/Include/RmlUi/Core/StringUtilities.h index 92c761058..fe11e34ea 100644 --- a/Include/RmlUi/Core/StringUtilities.h +++ b/Include/RmlUi/Core/StringUtilities.h @@ -78,6 +78,9 @@ namespace StringUtilities /// Encode RML characters, eg. '<' to '<' RMLUICORE_API String EncodeRml(const String& string); + /// Decode RML characters, eg. '<' to '<' + RMLUICORE_API String DecodeRml(const String& string); + // Replaces all occurences of 'search' in 'subject' with 'replace'. RMLUICORE_API String Replace(String subject, const String& search, const String& replace); // Replaces all occurences of 'search' in 'subject' with 'replace'. diff --git a/Source/Core/BaseXMLParser.cpp b/Source/Core/BaseXMLParser.cpp index 10e907262..0d792df48 100644 --- a/Source/Core/BaseXMLParser.cpp +++ b/Source/Core/BaseXMLParser.cpp @@ -382,7 +382,7 @@ bool BaseXMLParser::ReadAttributes(XMLAttributes& attributes, bool& parse_raw_xm if (attributes_for_inner_xml_data.count(attribute) == 1) parse_raw_xml_content = true; - attributes[attribute] = value; + attributes[attribute] = StringUtilities::DecodeRml(value); // Check for the end of the tag. if (PeekString("/", false) || PeekString(">", false)) diff --git a/Source/Core/StringUtilities.cpp b/Source/Core/StringUtilities.cpp index 92624882a..64ab3db74 100644 --- a/Source/Core/StringUtilities.cpp +++ b/Source/Core/StringUtilities.cpp @@ -122,6 +122,45 @@ RMLUICORE_API String StringUtilities::EncodeRml(const String& string) return result; } +String StringUtilities::DecodeRml(const String& s) +{ + String result; + result.reserve(s.size()); + for (size_t i = 0; i < s.size();) + { + if (s[i] == '&') + { + if (s[i+1] == 'l' && s[i+2] == 't' && s[i+3] == ';') + { + result += "<"; + i += 4; + continue; + } + else if (s[i+1] == 'g' && s[i+2] == 't' && s[i+3] == ';') + { + result += ">"; + i += 4; + continue; + } + else if (s[i+1] == 'a' && s[i+2] == 'm' && s[i+3] == 'p' && s[i+4] == ';') + { + result += "&"; + i += 5; + continue; + } + else if (s[i+1] == 'q' && s[i+2] == 'u' && s[i+3] == 'o' && s[i+4] == 't' && s[i+5] == ';') + { + result += "\""; + i += 6; + continue; + } + } + result += s[i]; + i += 1; + } + return result; +} + String StringUtilities::Replace(String subject, const String& search, const String& replace) { size_t pos = 0;