forked from natrim/jquery.highlight.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.highlight.min.js
5 lines (5 loc) · 2.19 KB
/
jquery.highlight.min.js
1
2
3
4
5
/*
* jQuery Highlight plugin
* Copyright (c) 2009 Bartek Szopka, 2012 Natrim
* Licensed under MIT license.
*/typeof jQuery=="function"&&jQuery(function(e){"use strict";e.extend({highlight:function(t,n,r,i,s){if(t.nodeType===3){var o=t.data;s&&(o=e.stripAccent(o));var u=o.match(n);if(u){var a=document.createElement(r||"span");a.className=i||"highlight";var f=t.splitText(u.index);f.splitText(u[0].length);var l=f.cloneNode(!0);return a.appendChild(l),f.parentNode.replaceChild(a,f),1}}else if(t.nodeType===1&&t.childNodes&&!/(script|style)/i.test(t.tagName)&&(t.tagName!==r.toUpperCase()||t.className!==i))for(var c=0;c<t.childNodes.length;c++)c+=e.highlight(t.childNodes[c],n,r,i,s);else t.nodeType===9&&e.highlight(t.body,n,r,i,s);return 0},stripAccent:function(e){var t=[/[\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],n=["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 r=0;r<t.length;++r)e=e.replace(t[r],n[r]);return e}}),e.fn.unhighlight=function(t){var n={className:"highlight",element:"span"};return e.extend(n,t),this.find(n.element+"."+n.className).each(function(){var e=this.parentNode;e.replaceChild(this.firstChild,this),e.normalize()}).end()},e.fn.highlight=function(t,n){var r={className:"highlight",element:"span",caseSensitive:!1,wordsOnly:!1,ignoreAccents:!0};e.extend(r,n),t.constructor===String&&(t=[t]),t=e.grep(t,function(e,t){return e!==""}),t=e.map(t,function(t,n){return r.ignoreAccents&&(t=e.stripAccent(t)),t.replace(/[-\[\]{}()*+?.,\\\^$|#\s]/g,"\\$&")});if(t.length===0)return this;var i=r.caseSensitive?"":"i",s="("+t.join("|")+")";r.wordsOnly&&(s="\\b"+s+"\\b");var o=new RegExp(s,i);return this.each(function(){e.highlight(this,o,r.element,r.className,r.ignoreAccents)})}});