diff --git a/jquery.highlight.js b/jquery.highlight.js index 9dcf3c7..0dbba51 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 : blacklisted; + }; + } + 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); }); };