From 42582776872a36fc1c4f34e1f9f9481bff7b5130 Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Wed, 8 Oct 2014 15:56:11 -0400 Subject: util.stanza: Escape newlines and tabs (\r\n\t) when serializing stanzas. \r\n transforms into \n otherwise, and \r\n\t in attributes transforms into spaces. --- util/stanza.lua | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/util/stanza.lua b/util/stanza.lua index 7c214210..2fcf2c79 100644 --- a/util/stanza.lua +++ b/util/stanza.lua @@ -202,8 +202,19 @@ end local xml_escape do - local escape_table = { ["'"] = "'", ["\""] = """, ["<"] = "<", [">"] = ">", ["&"] = "&" }; - function xml_escape(str) return (s_gsub(str, "['&<>\"]", escape_table)); end + local escape_table = { + ["'"] = "'"; + ['"'] = """; + ["<"] = "<"; + [">"] = ">"; + ["&"] = "&"; + -- escape this whitespace because [\r\n\t] change into spaces in attributes + -- and \r\n changes into \n in text, and we want to preserve original bytes + ["\t"] = " "; + ["\n"] = " "; + ["\r"] = " "; + }; + function xml_escape(str) return (s_gsub(str, "['&<>\"\t\n\r]", escape_table)); end _M.xml_escape = xml_escape; end -- cgit v1.2.3