Guest User

hilitor.js

a guest
Feb 14th, 2025
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 3.66 KB | Source Code | 0 0
  1. <script>
  2. // Original JavaScript code by Chirp Internet: www.chirp.com.au
  3. // Please acknowledge use of this code by including this header.
  4. function Hilitor(id, tag)
  5. {
  6.   // private variables
  7.   var targetNode = document.getElementById(id) || document.body;
  8.   var hiliteTag = tag || "MARK";
  9.   var skipTags = new RegExp("^(?:" + hiliteTag + "|SCRIPT|FORM|SPAN)$");
  10.   //var colors = ["#ff6", "#a0ffff", "#9f9", "#f99", "#f6f"];
  11.   var colors = ["#ffa"];
  12.   var wordColor = [];
  13.   var colorIdx = 0;
  14.   var matchRegExp = "";
  15.   var openLeft = false;
  16.   var openRight = false;
  17.  
  18.   // characters to strip from start and end of the input string
  19.   var endRegExp = new RegExp('^[^\\w]+|[^\\w]+$', "g");
  20.  
  21.   // characters used to break up the input string into words
  22.   var breakRegExp = new RegExp('[^\\w\'-]+', "g");
  23.  
  24.   this.setEndRegExp = function(regex) {
  25.     endRegExp = regex;
  26.     return endRegExp;
  27.   };
  28.  
  29.   this.setBreakRegExp = function(regex) {
  30.     breakRegExp = regex;
  31.     return breakRegExp;
  32.   };
  33.  
  34.   this.setMatchType = function(type)
  35.   {
  36.     switch(type)
  37.     {
  38.       case "left":
  39.         this.openLeft = false;
  40.         this.openRight = true;
  41.         break;
  42.  
  43.       case "right":
  44.         this.openLeft = true;
  45.         this.openRight = false;
  46.         break;
  47.  
  48.       case "open":
  49.         this.openLeft = this.openRight = true;
  50.         break;
  51.  
  52.       default:
  53.         this.openLeft = this.openRight = false;
  54.  
  55.     }
  56.   };
  57.  
  58.   this.setRegex = function(input)
  59.   {
  60.     input = input.replace(endRegExp, "");
  61.     input = input.replace(breakRegExp, "|");
  62.     input = input.replace(/^\||\|$/g, "");
  63.     if(input) {
  64.       var re = "(" + input + ")";
  65.       if(!this.openLeft) {
  66.         re = "\\b" + re;
  67.       }
  68.       if(!this.openRight) {
  69.         re = re + "\\b";
  70.       }
  71.       matchRegExp = new RegExp(re, "i");
  72.       return matchRegExp;
  73.     }
  74.     return false;
  75.   };
  76.  
  77.   this.getRegex = function()
  78.   {
  79.     var retval = matchRegExp.toString();
  80.     retval = retval.replace(/(^\/(\\b)?|\(|\)|(\\b)?\/i$)/g, "");
  81.     retval = retval.replace(/\|/g, " ");
  82.     return retval;
  83.   };
  84.   // recursively apply word highlighting
  85.   this.hiliteWords = function(node)
  86.   {
  87.     if(node === undefined || !node) return;
  88.     if(!matchRegExp) return;
  89.     if(skipTags.test(node.nodeName)) return;
  90.  
  91.     if(node.hasChildNodes()) {
  92.       for(var i=0; i < node.childNodes.length; i++)
  93.         this.hiliteWords(node.childNodes[i]);
  94.     }
  95.     if(node.nodeType == 3) { // NODE_TEXT
  96.       if((nv = node.nodeValue) && (regs = matchRegExp.exec(nv))) {
  97.         if(!wordColor[regs[0].toLowerCase()]) {
  98.           wordColor[regs[0].toLowerCase()] = colors[colorIdx++ % colors.length];
  99.         }
  100.  
  101.         var match = document.createElement(hiliteTag);
  102.         match.appendChild(document.createTextNode(regs[0]));
  103.         match.style.backgroundColor = wordColor[regs[0].toLowerCase()];
  104.         match.style.color = "#000";
  105.  
  106.         var after = node.splitText(regs.index);
  107.         after.nodeValue = after.nodeValue.substring(regs[0].length);
  108.         node.parentNode.insertBefore(match, after);
  109.       }
  110.     };
  111.   };
  112.   // remove highlighting
  113.   this.remove = function()
  114.   {
  115.     var arr = document.getElementsByTagName(hiliteTag);
  116.     while(arr.length && (el = arr[0])) {
  117.       var parent = el.parentNode;
  118.       parent.replaceChild(el.firstChild, el);
  119.       parent.normalize();
  120.     }
  121.   };
  122.   // start highlighting at target node
  123.   this.apply = function(input)
  124.   {
  125.     this.remove();
  126.     if(input === undefined || !(input = input.replace(/(^\s+|\s+$)/g, ""))) {
  127.       return;
  128.     }
  129.     if(this.setRegex(input)) {
  130.       this.hiliteWords(targetNode);
  131.     }
  132.     return matchRegExp;
  133.   };
  134. }
  135. </script>
Advertisement
Add Comment
Please, Sign In to add comment