Separate escape characters of double quotted XML attribute value
This commit is contained in:
Filip Sykala 2022-03-02 08:15:26 +01:00
parent fdf8ebed7f
commit 4e97830c2b
4 changed files with 32 additions and 5 deletions

View file

@ -617,6 +617,7 @@ ExPolygons Emboss::text2shapes(FontFileWithCache &font_with_cache,
cursor.x() += count_spaces * space_opt->advance_width; cursor.x() += count_spaces * space_opt->advance_width;
continue; continue;
} }
if (wc == '\r') continue;
int unicode = static_cast<int>(wc); int unicode = static_cast<int>(wc);
std::optional<Glyph> glyph_opt = Private::get_glyph(unicode, font, font_prop, cache, font_info_opt); std::optional<Glyph> glyph_opt = Private::get_glyph(unicode, font, font_prop, cache, font_info_opt);

View file

@ -3307,10 +3307,10 @@ void TextConfigurationSerialization::to_xml(std::stringstream &stream, const Tex
{ {
stream << " <" << TEXT_TAG << " "; stream << " <" << TEXT_TAG << " ";
stream << TEXT_DATA_ATTR << "=\"" << xml_escape(tc.text) << "\" "; stream << TEXT_DATA_ATTR << "=\"" << xml_escape_double_quotes_attribute_value(tc.text) << "\" ";
// font item // font item
const FontItem &fi = tc.font_item; const FontItem &fi = tc.font_item;
stream << FONT_DESCRIPTOR_ATTR << "=\"" << fi.path << "\" "; stream << FONT_DESCRIPTOR_ATTR << "=\"" << xml_escape_double_quotes_attribute_value(fi.path) << "\" ";
stream << FONT_DESCRIPTOR_TYPE_ATTR << "=\"" << TextConfigurationSerialization::to_string.at(fi.type) << "\" "; stream << FONT_DESCRIPTOR_TYPE_ATTR << "=\"" << TextConfigurationSerialization::to_string.at(fi.type) << "\" ";
// font property // font property

View file

@ -250,6 +250,7 @@ inline typename CONTAINER_TYPE::value_type& next_value_modulo(typename CONTAINER
} }
extern std::string xml_escape(std::string text, bool is_marked = false); extern std::string xml_escape(std::string text, bool is_marked = false);
extern std::string xml_escape_double_quotes_attribute_value(std::string text);
#if defined __GNUC__ && __GNUC__ < 5 && !defined __clang__ #if defined __GNUC__ && __GNUC__ < 5 && !defined __clang__

View file

@ -992,7 +992,7 @@ std::string xml_escape(std::string text, bool is_marked/* = false*/)
std::string::size_type pos = 0; std::string::size_type pos = 0;
for (;;) for (;;)
{ {
pos = text.find_first_of("\"\'&<>\n\t", pos); pos = text.find_first_of("\"\'&<>", pos);
if (pos == std::string::npos) if (pos == std::string::npos)
break; break;
@ -1004,8 +1004,33 @@ std::string xml_escape(std::string text, bool is_marked/* = false*/)
case '&': replacement = "&amp;"; break; case '&': replacement = "&amp;"; break;
case '<': replacement = is_marked ? "<" :"&lt;"; break; case '<': replacement = is_marked ? "<" :"&lt;"; break;
case '>': replacement = is_marked ? ">" : "&gt;"; break; case '>': replacement = is_marked ? ">" : "&gt;"; break;
case '\n': replacement = "&#x000A;"; break; default: break;
case '\t': replacement = "&#x0009;"; break; }
text.replace(pos, 1, replacement);
pos += replacement.size();
}
return text;
}
// Definition of escape symbols https://www.w3.org/TR/REC-xml/#AVNormalize
std::string xml_escape_double_quotes_attribute_value(std::string text)
{
std::string::size_type pos = 0;
for (;;) {
pos = text.find_first_of("\"&<\r\n\t", pos);
if (pos == std::string::npos) break;
std::string replacement;
switch (text[pos]) {
case '\"': replacement = "&quot;"; break;
case '&': replacement = "&amp;"; break;
case '<': replacement = "&lt;"; break;
case '\r': replacement = "&#xD;"; break;
case '\n': replacement = "&#xA;"; break;
case '\t': replacement = "&#x9;"; break;
default: break; default: break;
} }