8 lines
1.8 KiB
JavaScript
8 lines
1.8 KiB
JavaScript
/*
|
|
GNU General Public License version 2 or later; see LICENSE.txt
|
|
*/
|
|
if("undefined"===typeof Joomla)var Joomla={};
|
|
Joomla.Highlighter=new Class({Implements:Options,options:{autoUnhighlight:!0,caseSensitive:!1,startElement:!1,endElement:!1,elements:[],className:"highlight",onlyWords:!0,tag:"span"},initialize:function(a){this.setOptions(a);this.getElements(this.options.startElement,this.options.endElement);this.words=[]},highlight:function(a){a.constructor===String&&(a=[a]);this.options.autoUnhighlight&&this.unhighlight(a);var b=this.options.onlyWords?"\u0008"+b+"\u0008":"("+a.join("\\b|\\b")+")",c=RegExp(b,this.options.caseSensitive?
|
|
"":"i");this.options.elements.each(function(a){this.recurse(a,c,this.options.className)},this);return this},unhighlight:function(a){a.constructor===String&&(a=[a]);a.each(function(a){a=this.options.caseSensitive?a:a.toUpperCase();this.words[a]&&(a=$$(this.words[a]),a.setProperty("class",""),a.each(function(a){var b=document.createTextNode(a.getText());a.getParent().replaceChild(b,a)}))},this);return this},recurse:function(a,b,c){if(3===a.nodeType){var d=a.data.match(b);if(d)return b=new Element(this.options.tag),
|
|
b.addClass(c),a=a.splitText(d.index),a.splitText(d[0].length),c=a.cloneNode(!0),b.appendChild(c),a.parentNode.replaceChild(b,a),b.setProperty("rel",b.get("text")),a=b.get("text"),this.options.caseSensitive||(a=b.get("text").toUpperCase()),this.words[a]||(this.words[a]=[]),this.words[a].push(b),1}else if(1===a.nodeType&&a.childNodes&&!/(script|style|textarea|iframe)/i.test(a.tagName)&&!(a.tagName===this.options.tag.toUpperCase()&&a.className===c))for(d=0;d<a.childNodes.length;d++)d+=this.recurse(a.childNodes[d],
|
|
b,c);return 0},getElements:function(a,b){var c=a.getNext();c.id!=b.id&&(this.options.elements.include(c),this.getElements(c,b))}}); |