From 4dba56ba87f69916522969b8534f413893260439 Mon Sep 17 00:00:00 2001 From: rlx <0x0073@0x2620.org> Date: Wed, 1 Feb 2012 08:26:06 +0000 Subject: [PATCH] improve Ox.highlightHTML and annotations CSS --- source/Ox.UI/css/Ox.UI.css | 3 + source/Ox.UI/themes/classic/css/classic.css | 5 +- source/Ox.UI/themes/modern/css/modern.css | 2 +- source/Ox/js/String.js | 86 +++++++++++++++------ 4 files changed, 71 insertions(+), 25 deletions(-) diff --git a/source/Ox.UI/css/Ox.UI.css b/source/Ox.UI/css/Ox.UI.css index 019894e4..c352e0b9 100644 --- a/source/Ox.UI/css/Ox.UI.css +++ b/source/Ox.UI/css/Ox.UI.css @@ -604,6 +604,9 @@ OxArrayEditable -------------------------------------------------------------------------------- */ +.OxArrayEditable { + line-height: 14px; +} .OxArrayEditable.OxArrayEditableInput { padding: 4px; } diff --git a/source/Ox.UI/themes/classic/css/classic.css b/source/Ox.UI/themes/classic/css/classic.css index 089b90ca..a1945035 100644 --- a/source/Ox.UI/themes/classic/css/classic.css +++ b/source/Ox.UI/themes/classic/css/classic.css @@ -765,11 +765,14 @@ Video .OxThemeClassic .OxAnnotationFolder .OxArrayEditable .OxSeparator { color: rgb(96, 96, 96); } +.OxThemeClassic .OxAnnotationFolder .OxEditableElement .OxHighlight { + border-radius: 2px; +} .OxThemeClassic .OxAnnotationFolder .OxEditableElement.OxPlaceholder .OxValue { color: rgb(160, 160, 160); } .OxThemeClassic .OxAnnotationFolder .OxEditableElement.OxWarning .OxValue { - color: rgb(192, 64, 64); + border-bottom: 2px dotted rgb(255, 64, 64); } .OxThemeClassic .OxAnnotationFolder .OxArrayEditable .OxEditableElement.OxSelected { background: rgb(192, 192, 255); diff --git a/source/Ox.UI/themes/modern/css/modern.css b/source/Ox.UI/themes/modern/css/modern.css index efa2bdd1..2dacf2aa 100644 --- a/source/Ox.UI/themes/modern/css/modern.css +++ b/source/Ox.UI/themes/modern/css/modern.css @@ -789,7 +789,7 @@ Video color: rgb(96, 96, 96); } .OxThemeModern .OxAnnotationFolder .OxEditableElement.OxWarning .OxValue { - color: rgb(255, 64, 64); + border-bottom: 2px dotted rgb(192, 0, 0); } .OxThemeModern .OxAnnotationFolder .OxArrayEditable .OxEditableElement.OxSelected { background: rgb(64, 64, 192); diff --git a/source/Ox/js/String.js b/source/Ox/js/String.js index 0cf84165..3d7b4404 100644 --- a/source/Ox/js/String.js +++ b/source/Ox/js/String.js @@ -54,7 +54,7 @@ Ox.highlight Highlight matches in a string > Ox.highlight('foobar', 'foo', 'match') 'foobar' @*/ -// fixme: with regexp, special chars would have to be escaped +// fixme: with regexp, special chars have to be escaped Ox.highlight = function(txt, str, classname) { return str && str.length ? txt.replace( new RegExp('(' + str + ')', 'ig'), @@ -63,30 +63,70 @@ Ox.highlight = function(txt, str, classname) { }; /*@ -Ox.highlightHTML Highlight matches in a html string - > Ox.highlight('foobar', 'foo', 'match') - 'foobar' +Ox.highlightHTML Highlight matches in an HTML string + > Ox.highlightHTML('foobar', 'foobar', 'h') + 'foobar' + > Ox.highlightHTML('foobar', 'foobar', 'h') + 'foobar' + > Ox.highlightHTML('foo
bar', 'foobar', 'h') + 'foo
bar' + > Ox.highlightHTML('AT&T', 'AT&T', 'h') + 'AT&T' + > Ox.highlightHTML('AT&T', 'amp', 'h') + 'AT&T' @*/ -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; +Ox.highlightHTML = function(html, str, classname) { + var count = 0, + isEntity = false, + isTag = false, + position, + positions = [], + tags = ['a', 'b', 'code', 'i', 's', 'sub', 'sup', 'u']; + str = Ox.encodeHTML(str).toLowerCase(); + Ox.forEach(html.toLowerCase(), function(chr, i) { + // check for entity or tag start + if (!isEntity && chr == '&') { + isEntity = true; + } else if (!isTag && chr == '<') { + Ox.forEach(tags, function(tag) { + if (html.substr(i + 1).match(new RegExp('^/?' + tag + '\\W'))) { + isTag = true; + return false; + } + }); } - } - return result; -}; + // if outside entity or tag + if (!isEntity && !isTag) { + // if character matches + if (chr == str[count]) { + if (count == 0) { + position = i; + } + count++; + if (count == str.length) { + // make sure matches are last to first + positions.unshift([position, i + 1]); + } + } else { + count = 0; + } + } + // check for entity or tag end + if (isEntity && chr == ';') { + isEntity = false; + } else if (isTag && chr == '>') { + isTag = false; + } + }); + positions.forEach(function(position) { + var match = '' + + html.substr(position[0], position[1] - position[0]) + .replace(/(<.*?>)/g, '$1') + + ''; + html = html.substr(0, position[0]) + match + html.substr(position[1]); + }); + return html; +} /*@ Ox.isValidEmail Tests if a string is a valid e-mail address