User:Xeworlebi/highlight.js

// Reusable generic function function surroundInElement(el, regex, surrounderCreateFunc) { // script and style elements are left alone if (!/^(script|style)$/.test(el.tagName)) { var child = el.lastChild; while (child) { if (child.nodeType == 1) { surroundInElement(child, regex, surrounderCreateFunc); } else if (child.nodeType == 3) { surroundMatchingText(child, regex, surrounderCreateFunc); }           child = child.previousSibling; }   } }

// Reusable generic function function surroundMatchingText(textNode, regex, surrounderCreateFunc) { var parent = textNode.parentNode; var result, surroundingNode, matchedTextNode, matchLength, matchedText; while ( textNode && (result = regex.exec(textNode.data)) ) { matchedTextNode = textNode.splitText(result.index); matchedText = result[0]; matchLength = matchedText.length; textNode = (matchedTextNode.length > matchLength) ? matchedTextNode.splitText(matchLength) : null; surroundingNode = surrounderCreateFunc(matchedTextNode.cloneNode(true)); parent.insertBefore(surroundingNode, matchedTextNode); parent.removeChild(matchedTextNode); } }

// highlighting markup function redSpan(matchedTextNode) { var el = document.createElement("span"); el.style.backgroundColor = "red"; el.style.color = "white"; el.appendChild(matchedTextNode); return el; }

// detect/highligh function wrapPunct(container) { surroundInElement(container, /[‘’“”‛‟′″˝]+/, redSpan); }

function wrapNdash(container) { surroundInElement(container, /18-49/, redSpan); }

wrapPunct(document.body); wrapNdash(document.body);