/*1* jQuery Highlight plugin2*3* Based on highlight v3 by Johann Burkard4* http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html5*6* Code a little bit refactored and cleaned (in my humble opinion).7* Most important changes:8* - has an option to highlight only entire words (wordsOnly - false by default),9* - has an option to be case sensitive (caseSensitive - false by default)10* - highlight element tag and class names can be specified in options11*12* Usage:13* // wrap every occurrance of text 'lorem' in content14* // with <span class='highlight'> (default options)15* $('#content').highlight('lorem');16*17* // search for and highlight more terms at once18* // so you can save some time on traversing DOM19* $('#content').highlight(['lorem', 'ipsum']);20* $('#content').highlight('lorem ipsum');21*22* // search only for entire word 'lorem'23* $('#content').highlight('lorem', { wordsOnly: true });24*25* // don't ignore case during search of term 'lorem'26* $('#content').highlight('lorem', { caseSensitive: true });27*28* // wrap every occurrance of term 'ipsum' in content29* // with <em class='important'>30* $('#content').highlight('ipsum', { element: 'em', className: 'important' });31*32* // remove default highlight33* $('#content').unhighlight();34*35* // remove custom highlight36* $('#content').unhighlight({ element: 'em', className: 'important' });37*38*39* Copyright (c) 2009 Bartek Szopka40*41* Licensed under MIT license.42*43*/4445jQuery.extend({46highlight: function (node, re, nodeName, className) {47if (node.nodeType === 3) {48var match = node.data.match(re);49if (match) {50var highlight = document.createElement(nodeName || 'span');51highlight.className = className || 'highlight';52var wordNode = node.splitText(match.index);53wordNode.splitText(match[0].length);54var wordClone = wordNode.cloneNode(true);55highlight.appendChild(wordClone);56wordNode.parentNode.replaceChild(highlight, wordNode);57return 1; //skip added node in parent58}59} else if ((node.nodeType === 1 && node.childNodes) && // only element nodes that have children60!/(script|style)/i.test(node.tagName) && // ignore script and style nodes61!(node.tagName === nodeName.toUpperCase() && node.className === className)) { // skip if already highlighted62for (var i = 0; i < node.childNodes.length; i++) {63i += jQuery.highlight(node.childNodes[i], re, nodeName, className);64}65}66return 0;67}68});6970jQuery.fn.unhighlight = function (options) {71var settings = { className: 'highlight', element: 'span' };72jQuery.extend(settings, options);7374return this.find(settings.element + "." + settings.className).each(function () {75var parent = this.parentNode;76parent.replaceChild(this.firstChild, this);77parent.normalize();78}).end();79};8081jQuery.fn.highlight = function (words, options) {82var settings = { className: 'highlight', element: 'span', caseSensitive: false, wordsOnly: false };83jQuery.extend(settings, options);8485if (words.constructor === String) {86words = [words];87}88words = jQuery.grep(words, function(word, i){89return word != '';90});91words = jQuery.map(words, function(word, i) {92return word.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");93});94if (words.length == 0) { return this; };9596var flag = settings.caseSensitive ? "" : "i";97var pattern = "(" + words.join("|") + ")";98if (settings.wordsOnly) {99pattern = "\\b" + pattern + "\\b";100}101var re = new RegExp(pattern, flag);102103return this.each(function () {104jQuery.highlight(this, re, settings.element, settings.className);105});106};107108109110