Skip to content
Browse files

Move the highlighter to JHtmlBehavior

  • Loading branch information...
1 parent 694fc32 commit ecc25f8de785e62247895b475c11a8083d41a7d8 @realityking realityking committed Dec 5, 2011
View
52 libraries/joomla/html/html/behavior.php
@@ -746,6 +746,58 @@ public static function keepalive()
}
/**
+ * Highlight some words via Javascript.
+ *
+ * @param array $terms Array of words that should be highlighted.
+ * @param string $start ID of the element that marks the begin of the section in which words
+ * should be highlighted. Note this element will be removed from the DOM.
+ * @param string $end ID of the element that end this section.
+ * Note this element will be removed from the DOM.
+ * @param string $className Class name of the element highlights are wrapped in.
+ * @param string $tag Tag that will be used to wrap the highlighted words.
+ *
+ * @return void
+ *
+ * @since 11.4
+ */
+ public static function highlighter(array $terms, $start = 'highlighter-start', $end = 'highlighter-end', $className = 'highlight', $tag = 'span')
+ {
+ $sig = md5(serialize(array($terms, $start, $end)));
+ if (isset(self::$loaded[__METHOD__][$sig]))
+ {
+ return;
+ }
+
+ JHtml::_('script', 'system/highlighter.js', true, true);
+
+ $terms = str_replace('"', '\"', $terms);
+
+ $document = JFactory::getDocument();
+ $document->addScriptDeclaration("
+ window.addEvent('domready', function () {
+ var start = document.id('" . $start . "');
+ var end = document.id('" . $end . "');
+ if (!start || !end || !window.highlight) {
+ return true;
+ }
+ highlighter = new Joomla.Highlighter({
+ startElement: start,
+ endElement: end,
+ className: '" . $className . "'
+ onlyWords: false,
+ tag: '" . $tag . "'
+ }).highlight([\"" . implode('","', $terms) . "\"]);
+ start.dispose();
+ end.dispose();
+ });
+ ");
+
+ self::$loaded[__METHOD__][$sig] = true;
+
+ return;
+ }
+
+ /**
* Break us out of any containing iframes
*
* @param string $location Location to display in
View
26 libraries/joomla/html/html/string.php
@@ -151,30 +151,4 @@ public static function abridge($text, $length = 50, $intro = 30)
return $text;
}
-
- /**
- * Method to setup the JavaScript highlight behavior.
- *
- * @param array $terms An array of terms to highlight.
- *
- * @return void
- *
- * @since 11.4
- */
- public static function highlighter($terms)
- {
- // Get the document object.
- $doc = JFactory::getDocument();
-
- // We only want to highlight text on regular html pages.
- if ($doc->getType() == 'html' && JFactory::getApplication()->input->get('tmpl', null, 'cmd') !== 'component')
- {
- // Add the highlighter media.
- $uncompressed = JFactory::getConfig()->get('debug') ? '-uncompressed' : '';
- JHtml::_('script', 'system/highlighter' . $uncompressed . '.js', true, true);
-
- // Add the terms to highlight.
- $doc->addScriptDeclaration("window.highlight = [\"" . implode('","', $terms) . "\"];");
- }
- }
}
View
29 media/system/js/highlighter-uncompressed.js
@@ -4,7 +4,13 @@
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
-var Highlighter = new Class({
+// Only define the Joomla namespace if not defined.
+if (typeof(Joomla) === 'undefined') {
+ var Joomla = {};
+}
+
+Joomla.Highlighter = new Class({
+ Implements: Options,
options: {
autoUnhighlight: true,
caseSensitive: false,
@@ -15,11 +21,13 @@ var Highlighter = new Class({
onlyWords: true,
tag: 'span'
},
+
initialize: function (options) {
this.setOptions(options);
this.getElements(this.options.startElement, this.options.endElement);
this.words = [];
},
+
highlight: function (words) {
if (words.constructor === String) {
words = [words];
@@ -34,6 +42,7 @@ var Highlighter = new Class({
}, this);
return this;
},
+
unhighlight: function (words) {
if (words.constructor === String) {
words = [words];
@@ -51,6 +60,7 @@ var Highlighter = new Class({
}, this);
return this;
},
+
recurse: function (node, regex, klass) {
if (node.nodeType === 3) {
var match = node.data.match(regex);
@@ -80,6 +90,7 @@ var Highlighter = new Class({
}
return 0;
},
+
getElements: function (start, end) {
var next = start.getNext();
if (next.id != end.id) {
@@ -88,19 +99,3 @@ var Highlighter = new Class({
}
}
});
-Highlighter.implement(new Options);
-window.addEvent('domready', function () {
- var start = document.id('highlight-start');
- var end = document.id('highlight-end');
- if (!start || !end || !window.highlight) {
- return true;
- }
- highlighter = new Highlighter({
- startElement: start,
- endElement: end,
- autoUnhighlight: true,
- onlyWords: false
- }).highlight(window.highlight);
- start.dispose();
- end.dispose();
-});
View
13 media/system/js/highlighter.js
@@ -1,5 +1,8 @@
-var Highlighter=new Class({options:{autoUnhighlight:true,caseSensitive:false,startElement:false,endElement:false,elements:new Array(),className:"highlight",onlyWords:true,tag:"span"},initialize:function(options){this.setOptions(options);this.getElements(this.options.startElement,this.options.endElement);this.words=[];},highlight:function(words){if(words.constructor===String){words=[words];}if(this.options.autoUnhighlight){this.unhighlight(words);}var pattern=this.options.onlyWords?"\b"+pattern+"\b":"("+words.join("\\b|\\b")+")";
-var regex=new RegExp(pattern,this.options.caseSensitive?"":"i");this.options.elements.each(function(el){this.recurse(el,regex,this.options.className);},this);return this;},unhighlight:function(words){if(words.constructor===String){words=[words];}words.each(function(word){word=(this.options.caseSensitive?word:word.toUpperCase());if(this.words[word]){var elements=$$(this.words[word]);elements.setProperty("class","");elements.each(function(el){var tn=document.createTextNode(el.getText());el.getParent().replaceChild(tn,el);
-});}},this);return this;},recurse:function(node,regex,klass){if(node.nodeType===3){var match=node.data.match(regex);if(match){var highlight=new Element(this.options.tag);highlight.addClass(klass);var wordNode=node.splitText(match.index);wordNode.splitText(match[0].length);var wordClone=wordNode.cloneNode(true);highlight.appendChild(wordClone);wordNode.parentNode.replaceChild(highlight,wordNode);highlight.setProperty("rel",highlight.get("text"));var comparer=highlight.get("text");if(!this.options.caseSensitive){comparer=highlight.get("text").toUpperCase();
-}if(!this.words[comparer]){this.words[comparer]=[];}this.words[comparer].push(highlight);return 1;}}else{if((node.nodeType===1&&node.childNodes)&&!/(script|style|textarea|iframe)/i.test(node.tagName)&&!(node.tagName===this.options.tag.toUpperCase()&&node.className===klass)){for(var i=0;i<node.childNodes.length;i++){i+=this.recurse(node.childNodes[i],regex,klass);}}}return 0;},getElements:function(start,end){var next=start.getNext();if(next.id!=end.id){this.options.elements.include(next);this.getElements(next,end);
-}}});Highlighter.implement(new Options);window.addEvent("domready",function(){var start=document.id("highlight-start");var end=document.id("highlight-end");if(!start||!end||!window.highlight){return true;}highlighter=new Highlighter({startElement:start,endElement:end,autoUnhighlight:true,onlyWords:false}).highlight(window.highlight);start.dispose();end.dispose();});
+/*
+ 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))}});

0 comments on commit ecc25f8

Please sign in to comment.
Something went wrong with that request. Please try again.