Widget:Highlight: Difference between revisions

From Buddha-Nature
((by SublimeText.Mediawiker))
((by SublimeText.Mediawiker))
Line 16: Line 16:
                     return 1; //skip added node in parent
                     return 1; //skip added node in parent
                 }
                 }
             } else if ((node.nodeType === 1 <!--{$amps}--> node.childNodes) <!--{$amps}--> // only element nodes that have children
             } else if ((node.nodeType === 1 <!--{$amps|escape:'html'}--> node.childNodes) <!--{$amps|escape:'html'}--> // only element nodes that have children
                     !/(script|style)/i.test(node.tagName) <!--{$amps}--> // ignore script and style nodes
                     !/(script|style)/i.test(node.tagName) <!--{$amps|escape:'html'}--> // ignore script and style nodes
                     !(node.tagName === nodeName.toUpperCase() <!--{$amps}--> node.className === className)) { // skip if already highlighted
                     !(node.tagName === nodeName.toUpperCase() <!--{$amps|escape:'html'}--> node.className === className)) { // skip if already highlighted
                 for (var i = 0; i < node.childNodes.length; i++) {
                 for (var i = 0; i < node.childNodes.length; i++) {
                     i += jQuery.highlight(node.childNodes[i], re, nodeName, className);
                     i += jQuery.highlight(node.childNodes[i], re, nodeName, className);

Revision as of 17:22, 4 March 2020

<script src="https://code.jquery.com/jquery-1.12.4.js"></script>

<script>

   jQuery.extend({
       highlight: function (node, re, nodeName, className) {
           if (node.nodeType === 3) {
               var match = node.data.match(re);
               if (match) {
                   var highlight = document.createElement(nodeName || 'span');
                   highlight.className = className || 'highlight';
                   var wordNode = node.splitText(match.index);
                   wordNode.splitText(match[0].length);
                   var wordClone = wordNode.cloneNode(true);
                   highlight.appendChild(wordClone);
                   wordNode.parentNode.replaceChild(highlight, wordNode);
                   return 1; //skip added node in parent
               }
           } else if ((node.nodeType === 1  node.childNodes)  // only element nodes that have children
                   !/(script|style)/i.test(node.tagName)  // ignore script and style nodes
                   !(node.tagName === nodeName.toUpperCase()  node.className === className)) { // skip if already highlighted
               for (var i = 0; i < node.childNodes.length; i++) {
                   i += jQuery.highlight(node.childNodes[i], re, nodeName, className);
               }
           }
           return 0;
       }
   });
   jQuery.fn.unhighlight = function (options) {
       var settings = { className: 'highlight', element: 'span' };
       jQuery.extend(settings, options);
       return this.find(settings.element + "." + settings.className).each(function () {
           var parent = this.parentNode;
           parent.replaceChild(this.firstChild, this);
           parent.normalize();
       }).end();
   };
   jQuery.fn.highlight = function (words, options) {
       var settings = { className: 'highlight', element: 'span', caseSensitive: false, wordsOnly: false };
       jQuery.extend(settings, options);
       
       if (words.constructor === String) {
           words = [words];
       }
       words = jQuery.grep(words, function(word, i){
         return word != ;
       });
       words = jQuery.map(words, function(word, i) {
         return word.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
       });
       if (words.length == 0) { return this; };
       var flag = settings.caseSensitive ? "" : "i";
       var pattern = "(" + words.join("|") + ")";
       if (settings.wordsOnly) {
           pattern = "\\b" + pattern + "\\b";
       }
       var re = new RegExp(pattern, flag);
       
       return this.each(function () {
           jQuery.highlight(this, re, settings.element, settings.className);
       });
   };

</script>

<script> $(function() { $('#searchresults').highlight(); }); </script>