From b01f542c91d65ebc71bb8d843272b154c3d8e4b1 Mon Sep 17 00:00:00 2001 From: Alfred Egger Date: Wed, 16 Dec 2020 21:13:40 +0100 Subject: [PATCH] Add escapeHTML function to Javascipt code --- js/devel/helpers.js | 51 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/js/devel/helpers.js b/js/devel/helpers.js index 46f5e77..a586f3f 100644 --- a/js/devel/helpers.js +++ b/js/devel/helpers.js @@ -26,4 +26,53 @@ function toBool(str) { return false; } return null; -} \ No newline at end of file +} + + +function escapeHTML(string) { + var str = '' + string + var matchHtmlRegExp = /["'&<>]/ + var match = matchHtmlRegExp.exec(str) + + if (!match) { + return str + } + + var escape + var html = '' + var index = 0 + var lastIndex = 0 + + for (index = match.index; index < str.length; index++) { + switch (str.charCodeAt(index)) { + case 34: // " + escape = '"' + break + case 38: // & + escape = '&' + break + case 39: // ' + escape = ''' + break + case 60: // < + escape = '<' + break + case 62: // > + escape = '>' + break + default: + continue + } + + if (lastIndex !== index) { + html += str.substring(lastIndex, index) + } + + lastIndex = index + 1 + html += escape + } + + return lastIndex !== index + ? html + str.substring(lastIndex, index) + : html +}