From 1d14cd969cb81ed966a077f31bebb28216e35b3d Mon Sep 17 00:00:00 2001 From: Adi Luhung Suryadi Date: Wed, 6 Feb 2013 15:47:25 +0800 Subject: [PATCH 1/2] Added blacklist option. it's an array of jquery selectors(#id or .class) that will be ommited from highlighting, work recursively. (e.g.) $('body').highlight(highlight_arr, { wordsOnly: true, className: 'highlight', blacklist: blacklistArray }); --- jquery.highlight.js | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/jquery.highlight.js b/jquery.highlight.js index 9dcf3c7..9ec544b 100644 --- a/jquery.highlight.js +++ b/jquery.highlight.js @@ -44,7 +44,7 @@ */ jQuery.extend({ - highlight: function (node, re, nodeName, className) { + highlight: function (node, re, nodeName, className, blacklist) { if (node.nodeType === 3) { var match = node.data.match(re); if (match) { @@ -60,8 +60,26 @@ jQuery.extend({ } else if ((node.nodeType === 1 && node.childNodes) && // only element nodes that have children !/(script|style)/i.test(node.tagName) && // ignore script and style nodes !(node.tagName === nodeName.toUpperCase() && node.className === className)) { // skip if already highlighted - for (var i = 0; i < node.childNodes.length; i++) { - i += jQuery.highlight(node.childNodes[i], re, nodeName, className); + var blacklisted = false, + blacklistItem, pattern, classRegExp, target, match, + i, iLength; + + if (blacklist.length && (node.className || node.id)) { + for(i = 0, iLength = blacklist.length; i < iLength; i++) { + blacklistItem = blacklist[i]; + pattern = "\\b" + blacklist[i].substr(1) + "\\b"; + classRegExp = new RegExp(pattern); + + // detect if blacklist item is id or else class + target = (blacklistItem.charAt(0) === '#') ? node.id : node.className; + match = target.match(classRegExp); + blacklisted = match ? true : false; + }; + } + if (! blacklisted) { + for (var i = 0; i < node.childNodes.length; i++) { + i += jQuery.highlight(node.childNodes[i], re, nodeName, className, blacklist); + } } } return 0; @@ -80,7 +98,7 @@ jQuery.fn.unhighlight = function (options) { }; jQuery.fn.highlight = function (words, options) { - var settings = { className: 'highlight', element: 'span', caseSensitive: false, wordsOnly: false }; + var settings = { className: 'highlight', element: 'span', caseSensitive: false, wordsOnly: false, blacklist: [] }; jQuery.extend(settings, options); if (words.constructor === String) { @@ -102,7 +120,7 @@ jQuery.fn.highlight = function (words, options) { var re = new RegExp(pattern, flag); return this.each(function () { - jQuery.highlight(this, re, settings.element, settings.className); + jQuery.highlight(this, re, settings.element, settings.className, settings.blacklist); }); }; From 256268cd7f0aa92e359535b53195c70b81d8fe99 Mon Sep 17 00:00:00 2001 From: Adi Luhung Suryadi Date: Wed, 6 Feb 2013 16:08:30 +0800 Subject: [PATCH 2/2] fix wrong logic on conditional checking of blacklisted node --- jquery.highlight.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jquery.highlight.js b/jquery.highlight.js index 9ec544b..0dbba51 100644 --- a/jquery.highlight.js +++ b/jquery.highlight.js @@ -73,7 +73,7 @@ jQuery.extend({ // detect if blacklist item is id or else class target = (blacklistItem.charAt(0) === '#') ? node.id : node.className; match = target.match(classRegExp); - blacklisted = match ? true : false; + blacklisted = match ? true : blacklisted; }; } if (! blacklisted) {