Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions jquery.highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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) {
Expand All @@ -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);
});
};