From 9568acd5925aa113b3d8a62e4f743bf433618b34 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Tue, 31 Jan 2012 12:00:34 +0000 Subject: [PATCH] highlight html strings without breaking tags --- source/Ox.UI/js/Form/Ox.Editable.js | 2 +- source/Ox/js/String.js | 28 +++++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/source/Ox.UI/js/Form/Ox.Editable.js b/source/Ox.UI/js/Form/Ox.Editable.js index 01f82203..e7252b7a 100644 --- a/source/Ox.UI/js/Form/Ox.Editable.js +++ b/source/Ox.UI/js/Form/Ox.Editable.js @@ -177,7 +177,7 @@ Ox.Editable = function(options, self) { value = self.options.format(self.options.value) } if (self.options.highlight) { - value = Ox.highlight(value, self.options.highlight, 'OxHighlight'); + value = Ox.highlightHTML(value, self.options.highlight, 'OxHighlight'); } return value; } diff --git a/source/Ox/js/String.js b/source/Ox/js/String.js index 600223b3..0cf84165 100644 --- a/source/Ox/js/String.js +++ b/source/Ox/js/String.js @@ -62,6 +62,32 @@ Ox.highlight = function(txt, str, classname) { ) : txt; }; +/*@ +Ox.highlightHTML Highlight matches in a html string + > Ox.highlight('foobar', 'foo', 'match') + 'foobar' +@*/ +Ox.highlightHTML = function(txt, str, classname) { + var i = -1, + result = '', + value = txt.toLowerCase(); + str = str.toLowerCase(); + while (txt.length) { + if ((i = value.indexOf(str, i + 1)) < 0) { + result += txt; + txt = ''; + } else if (value.lastIndexOf('>', i) >= value.lastIndexOf('<', i)) { + result += txt.substring(0, i) + + '' + + txt.substr(i, str.length) + ''; + txt = txt.substr(i + str.length); + value = txt.toLowerCase(); + i = -1; + } + } + return result; +}; + /*@ Ox.isValidEmail Tests if a string is a valid e-mail address (str) -> True if the string is a valid e-mail address @@ -488,4 +514,4 @@ Ox.wordwrap = function(str, len, sep, bal, spa) { }); } return Ox.trim(lines.join(sep)); -}; \ No newline at end of file +};