From dd359582d46128b8d01d56f15525dfad796e5a5e Mon Sep 17 00:00:00 2001 From: Daniel Hultgren Date: Fri, 24 Jul 2020 22:32:27 +0200 Subject: [PATCH] Fixes #51574 - Added a whitespace sanitizing proc (#52350) * Fixed #51574 - Added htmlrendertext and implemented it for the voice analyzer * Made a htmlrendertext regex static Co-authored-by: Bobbahbrown * Made a htmlrendertext regex static again Co-authored-by: Bobbahbrown --- code/__HELPERS/text.dm | 21 +++++++++++++++++++++ code/modules/assembly/voice.dm | 4 ++++ 2 files changed, 25 insertions(+) diff --git a/code/__HELPERS/text.dm b/code/__HELPERS/text.dm index d8fbb6b40d3..77764634d8c 100644 --- a/code/__HELPERS/text.dm +++ b/code/__HELPERS/text.dm @@ -68,6 +68,27 @@ /proc/adminscrub(t,limit=MAX_MESSAGE_LEN) return copytext((html_encode(strip_html_simple(t))),1,limit) +/** + * Perform a whitespace cleanup on the text, similar to what HTML renderers do + * + * This is useful if you want to better predict how text is going to look like when displaying it to a user + * HTML renderers collapse multiple whitespaces into one, trims prepending and appending spaces, among other things. This proc attempts to do the same thing. + * HTML5 defines whitespace pretty much exactly like regex defines the \s group, [ \t\r\n\f]. + * Arguments: + * * t - The text to "render" + */ +/proc/htmlrendertext(t) + // Trim "whitespace" by lazily capturing word characters in the middle + var/static/regex/matchMiddle = new(@"^\s*([\W\w]*?)\s*$") + if(matchMiddle.Find(t) == 0) + return t + t = matchMiddle.group[1] + + // Replace any non-space whitespace characters with spaces, and also multiple occurences with just one space + var/static/regex/matchSpacing = new(@"\s+", "g") + t = replacetext(t, matchSpacing, " ") + + return t //Returns null if there is any bad text in the string /proc/reject_bad_text(text, max_length = 512, ascii_only = TRUE) diff --git a/code/modules/assembly/voice.dm b/code/modules/assembly/voice.dm index 70af1573f3e..6aa7110cb60 100644 --- a/code/modules/assembly/voice.dm +++ b/code/modules/assembly/voice.dm @@ -32,6 +32,10 @@ if(speaker == src) return + // raw_message can contain multiple spaces between words etc which are not seen in chat due to HTML rendering + // this means if the teller records a message with e.g. double spaces or tabs, other people will not be able to trigger the sensor since they don't know how to perform the same combination + raw_message = htmlrendertext(raw_message) + if(listening && !radio_freq) record_speech(speaker, raw_message, message_language) else