SHARE
TWEET

Untitled

a guest Sep 13th, 2017 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function wrapText(container, text) {
  2.     // Construct a regular expression that matches text at the start or end of a string or surrounded by non-word characters.
  3.     // Escape any special regex characters in text.
  4.     var textRE = new RegExp('(^|\\W)' + text.replace(/[\\^$*+.?[\]{}()|]/, '\\$&') + '($|\\W)', 'm');
  5.     var nodeText;
  6.     var nodeStack = [];
  7.  
  8.     // Remove empty text nodes and combine adjacent text nodes.
  9.     container.normalize();
  10.  
  11.     // Iterate through the container's child elements, looking for text nodes.
  12.     var curNode = container.firstChild;
  13.  
  14.     while (curNode != null) {
  15.         if (curNode.nodeType == Node.TEXT_NODE) {
  16.             // Get node text in a cross-browser compatible fashion.
  17.             if (typeof curNode.textContent == 'string')
  18.                 nodeText = curNode.textContent;
  19.             else
  20.                 nodeText = curNode.innerText;
  21.  
  22.             // Use a regular expression to check if this text node contains the target text.
  23.             var match = textRE.exec(nodeText);
  24.             if (match != null) {
  25.                 // Create a document fragment to hold the new nodes.
  26.                 var fragment = document.createDocumentFragment();
  27.  
  28.                 // Create a new text node for any preceding text.
  29.                 if (match.index > 0)
  30.                     fragment.appendChild(document.createTextNode(match.input.substr(0, match.index)));
  31.  
  32.                 // Create the wrapper span and add the matched text to it.
  33.                 var spanNode = document.createElement('span');
  34.                 spanNode.className = 'highlighted';
  35.                 spanNode.appendChild(document.createTextNode(match[0]));
  36.                 fragment.appendChild(spanNode);
  37.  
  38.                 // Create a new text node for any following text.
  39.                 if (match.index + match[0].length < match.input.length)
  40.                     fragment.appendChild(document.createTextNode(match.input.substr(match.index + match[0].length)));
  41.  
  42.                 // Replace the existing text node with the fragment.
  43.                 curNode.parentNode.replaceChild(fragment, curNode);
  44.  
  45.                 curNode = spanNode;
  46.             }
  47.         } else if (curNode.nodeType == Node.ELEMENT_NODE && curNode.firstChild != null) {
  48.             nodeStack.push(curNode);
  49.             curNode = curNode.firstChild;
  50.             // Skip the normal node advancement code.
  51.             continue;
  52.         }
  53.  
  54.         // If there's no more siblings at this level, pop back up the stack until we find one.
  55.         while (curNode != null && curNode.nextSibling == null)
  56.             curNode = nodeStack.pop();
  57.  
  58.         // If curNode is null, that means we've completed our scan of the DOM tree.
  59.         // If not, we need to advance to the next sibling.
  60.         if (curNode != null)
  61.             curNode = curNode.nextSibling;
  62.     }
  63. }
RAW Paste Data
Top