Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added palette feature #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)/, {
Expand Down
8 changes: 6 additions & 2 deletions highlightRegex.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 === '' ) {

Expand Down Expand Up @@ -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
Expand All @@ -119,4 +123,4 @@

return $( this )
}
})( jQuery );
})( jQuery );
2 changes: 1 addition & 1 deletion highlightRegex.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.