From 0c191d23766696f48dffe2f6d24252b13f9252f5 Mon Sep 17 00:00:00 2001 From: Sagnik Ganguly Date: Wed, 14 Dec 2022 20:38:45 +0530 Subject: [PATCH] ADDED: String Function: 'toHtmlEntities, fromHtmlEntities' --- src/js/helpers/string.js | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/js/helpers/string.js b/src/js/helpers/string.js index 5cf71db9..ddf7c77a 100644 --- a/src/js/helpers/string.js +++ b/src/js/helpers/string.js @@ -17,15 +17,31 @@ String.prototype.guid = function() { }; String.prototype.ltrim = function(s) { - return this.replace(new RegExp("^" + s + "*"), ''); + return this.replace(new RegExp("^" + s + "*"), ""); }; String.prototype.rtrim = function(s) { - return this.replace(new RegExp(s + "*$"), ''); + return this.replace(new RegExp(s + "*$"), ""); }; -$.expr[':'].textEquals = $.expr.createPseudo(function(arg) { +/** + * Convert a string to HTML entities + */ +String.prototype.toHtmlEntities = function() { + return escapeHTML(this); +}; + +/** + * Create string from HTML entities + */ +String.fromHtmlEntities = function(string) { + return (string + "").replace(/&#\d+;/gm, function(s) { + return String.fromCharCode(s.match(/\d+/gm)[0]); + }); +}; + +$.expr[":"].textEquals = $.expr.createPseudo(function(arg) { arg = arg.trim(); return function(elem) { return $(elem).text().trim().match("^" + arg + "$"); - } + }; }); \ No newline at end of file