From 29d0ff886b467443cafa2cced320c38af09ec191 Mon Sep 17 00:00:00 2001 From: Matt Sephton Date: Sat, 16 Feb 2013 17:17:28 +0000 Subject: [PATCH] added palette feature --- README.md | 10 ++++++++++ highlightRegex.js | 8 ++++++-- highlightRegex.min.js | 2 +- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 0cf1b2b..c3ce1b9 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,16 @@ If you'd like to wrap with a different sort tag and/or class, use className: 'andHale' }); +If you'd like to specify a palette of classes so that each match group is styled differently, use: + + $('#jquery.selector').highlightRegex( /some ([rR]egex)/, { + tagType: 'strong', + className: 'andHale', + palette: ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'] + }); + +Note: colours are recycled if there are more match groups than colours. + Additional attributes can be set on the created tag $('#jquery.selector').highlightRegex( /some ([rR]egex)/, { diff --git a/highlightRegex.js b/highlightRegex.js index c5caa35..930f1f9 100644 --- a/highlightRegex.js +++ b/highlightRegex.js @@ -59,6 +59,7 @@ options.className = options.className || 'highlight' options.tagType = options.tagType || 'span' options.attrs = options.attrs || {} + options.palette = options.palette || [] if ( typeof regex === 'undefined' || regex.source === '' ) { @@ -89,12 +90,15 @@ while ( searchnode.data && ( pos = searchnode.data.search( regex )) >= 0 ) { - match = searchnode.data.slice( pos ).match( regex )[ 0 ] + exec = regex.exec( searchnode.data.slice( pos ) ) + index = exec.lastIndexOf(exec[0]) - 1 + match = exec[0] if ( match.length > 0 ) { spannode = document.createElement( options.tagType ) spannode.className = options.className + if (options.palette.length) spannode.className = spannode.className +' '+ options.palette[index % options.palette.length] $(spannode).attr(options.attrs) parent = searchnode.parentNode @@ -119,4 +123,4 @@ return $( this ) } -})( jQuery ); +})( jQuery ); \ No newline at end of file diff --git a/highlightRegex.min.js b/highlightRegex.min.js index 94b2f0c..7d3f375 100644 --- a/highlightRegex.min.js +++ b/highlightRegex.min.js @@ -6,4 +6,4 @@ * * (c) 2009-13 Jacob Rothstein * MIT license - */(function(a){var b=function(c){if(!!c&&!!c.childNodes){var d=a.makeArray(c.childNodes),e=null;a.each(d,function(a,d){d.nodeType===3?d.nodeValue===""?c.removeChild(d):e!==null?(e.nodeValue+=d.nodeValue,c.removeChild(d)):e=d:(e=null,d.childNodes&&b(d))})}};a.fn.highlightRegex=function(c,d){typeof c=="object"&&c.constructor.name!=="RegExp"&&(d=c,c=undefined),typeof d=="undefined"&&(d={}),d.className=d.className||"highlight",d.tagType=d.tagType||"span",d.attrs=d.attrs||{},typeof c=="undefined"||c.source===""?a(this).find(d.tagType+"."+d.className).each(function(){a(this).replaceWith(a(this).text()),b(a(this).parent().get(0))}):a(this).each(function(){var e=a(this).get(0);b(e),a.each(a.makeArray(e.childNodes),function(e,f){var g,h,i,j,k,l;b(f);if(f.nodeType==3)while(f.data&&(j=f.data.search(c))>=0){k=f.data.slice(j).match(c)[0];if(k.length>0)g=document.createElement(d.tagType),g.className=d.className,a(g).attr(d.attrs),l=f.parentNode,h=f.splitText(j),f=h.splitText(k.length),i=h.cloneNode(!0),g.appendChild(i),l.replaceChild(g,h);else break}else a(f).highlightRegex(c,d)})});return a(this)}})(jQuery) \ No newline at end of file + */(function(e){var t=function(n){if(!n||!n.childNodes)return;var r=e.makeArray(n.childNodes),i=null;e.each(r,function(e,r){if(r.nodeType===3)if(r.nodeValue==="")n.removeChild(r);else if(i!==null){i.nodeValue+=r.nodeValue;n.removeChild(r)}else i=r;else{i=null;r.childNodes&&t(r)}})};e.fn.highlightRegex=function(n,r){if(typeof n=="object"&&n.constructor.name!=="RegExp"){r=n;n=undefined}typeof r=="undefined"&&(r={});r.className=r.className||"highlight";r.tagType=r.tagType||"span";r.attrs=r.attrs||{};r.palette=r.palette||[];typeof n=="undefined"||n.source===""?e(this).find(r.tagType+"."+r.className).each(function(){e(this).replaceWith(e(this).text());t(e(this).parent().get(0))}):e(this).each(function(){var i=e(this).get(0);t(i);e.each(e.makeArray(i.childNodes),function(i,s){var o,u,a,f,l,c;t(s);if(s.nodeType==3)while(s.data&&(f=s.data.search(n))>=0){exec=n.exec(s.data.slice(f));index=exec.lastIndexOf(exec[0])-1;l=exec[0];if(!(l.length>0))break;o=document.createElement(r.tagType);o.className=r.className;r.palette.length&&(o.className=o.className+" "+r.palette[index%r.palette.length]);e(o).attr(r.attrs);c=s.parentNode;u=s.splitText(f);s=u.splitText(l.length);a=u.cloneNode(!0);o.appendChild(a);c.replaceChild(o,u)}else e(s).highlightRegex(n,r)})});return e(this)}})(jQuery); \ No newline at end of file