/** * 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 2.5 */ 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(static::$loaded[__METHOD__][$sig])) { return; } // Include core static::core(); // Include jQuery JHtml::_('jquery.framework'); JHtml::_('script', 'system/highlighter.js', false, true); foreach ($terms as $i => $term) { $terms[$i] = JFilterOutput::stringJSSafe($term); } $document = JFactory::getDocument(); $document->addScriptDeclaration("\n\t\t\tjQuery(function (\$) {\n\t\t\t\tvar start = document.getElementById('" . $start . "');\n\t\t\t\tvar end = document.getElementById('" . $end . "');\n\t\t\t\tif (!start || !end || !Joomla.Highlighter) {\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t\thighlighter = new Joomla.Highlighter({\n\t\t\t\t\tstartElement: start,\n\t\t\t\t\tendElement: end,\n\t\t\t\t\tclassName: '" . $className . "',\n\t\t\t\t\tonlyWords: false,\n\t\t\t\t\ttag: '" . $tag . "'\n\t\t\t\t}).highlight([\"" . implode('","', $terms) . "\"]);\n\t\t\t\t\$(start).remove();\n\t\t\t\t\$(end).remove();\n\t\t\t});\n\t\t"); static::$loaded[__METHOD__][$sig] = true; return; }