This repository has been archived by the owner on Sep 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
jquery.highlight.min.js
5 lines (5 loc) · 2.61 KB
/
jquery.highlight.min.js
1
2
3
4
5
/*
* jQuery Highlight plugin
* Copyright (c) 2009 Bartek Szopka, 2012 Natrim
* Licensed under MIT license.
*/if(typeof jQuery==="function")jQuery(function($){"use strict";$.extend({highlight:function(a,b,c,d,e){if(a.nodeType===3){var f=a.data;if(e){f=$.stripAccent(f)}var g=f.match(b);if(g){var h=document.createElement(c||'span');h.className=d||'highlight';var j=a.splitText(g.index);j.splitText(g[0].length);var k=j.cloneNode(true);h.appendChild(k);j.parentNode.replaceChild(h,j);return 1}}else if((a.nodeType===1&&a.childNodes)&&!/(script|style)/i.test(a.tagName)&&!(a.tagName===c.toUpperCase()&&a.className===d)){for(var i=0;i<a.childNodes.length;i++){i+=$.highlight(a.childNodes[i],b,c,d,e)}}else if(a.nodeType===9){$.highlight(a.body,b,c,d,e)}return 0},stripAccent:function(a){var b=[/[\xC0-\xC2]/g,/[\xE0-\xE2]/g,/[\xC8-\xCA]/g,/[\xE8-\xEB]/g,/[\xCC-\xCE]/g,/[\xEC-\xEE]/g,/[\xD2-\xD4]/g,/[\xF2-\xF4]/g,/[\xD9-\xDB]/g,/[\xF9-\xFB]/g,/[\u0106\u0108\u010A\u010C]/g,/[\u0107\u0109\u010D\010B]/g,/[\u0154\u0156\u0158]/g,/[\u0155\u0157\u0159]/g,/[\u0179\u017B\u017D]/g,/[\u017A\u017C\u017E]/g,/[\u015A\u015C\u015E\u0160]/g,/[\u015B\u015D\u015F\u0161]/g,/[\u010E\u110]/g,/[\u010F\u0111]/g,/[\u00DD\u0176\u0178]/g,/[\u00FD\u00FF\u0177]/g,/[\u0143\u0145\u0147\u014A]/g,/[\u0144\u0146\u0148\u0149\u014B]/g];var c=['A','a','E','e','I','i','O','o','U','u','C','c','R','r','T','t','Z','z','S','s','D','d','Y','y','N','n'];for(var i=0;i<b.length;++i){a=a.replace(b[i],c[i])}return a}});$.fn.unhighlight=function(f){var g={className:'highlight',element:'span'};$.extend(g,f);function newNormalize(a){var b;for(var i=0,children=a.childNodes,nodeCount=children.length;i<nodeCount;i++){var c=children[i];if(c.nodeType==1){newNormalize(c);continue}if(c.nodeType!=3){continue}var d=c.nextSibling;if(d==null||d.nodeType!=3){continue}var e=c.nodeValue+d.nodeValue;b=a.ownerDocument.createTextNode(e);a.insertBefore(b,c);a.removeChild(c);a.removeChild(d);i--;nodeCount--}}return this.find(g.element+"."+g.className).each(function(){var a=this.parentNode;a.replaceChild(this.firstChild,this);newNormalize(a)}).end()};$.fn.highlight=function(b,c){var d={className:'highlight',element:'span',caseSensitive:false,wordsOnly:false,ignoreAccents:true};$.extend(d,c);if(b.constructor===String){b=[b]}b=$.grep(b,function(a,i){return a!==''});b=$.map(b,function(a,i){if(d.ignoreAccents){a=$.stripAccent(a)}return a.replace(/[-\[\]{}()*+?.,\\\^$|#\s]/g,"\\$&")});if(b.length===0){return this}var e=d.caseSensitive?"":"i";var f="("+b.join("|")+")";if(d.wordsOnly){f="\\b"+f+"\\b"}var g=new RegExp(f,e);return this.each(function(){$.highlight(this,g,d.element,d.className,d.ignoreAccents)})}});