Created
June 5, 2015 18:07
-
-
Save mveinot/a0cda12ae4f78c33445e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function highlightSearchTerms(sword) { | |
$$('body').map(Element.extend).first().descendants().each(function (el) { | |
if (el.nodeType == Node.ELEMENT_NODE && el.tagName != 'TEXTAREA' && el.tagName != 'INPUT' && el.tagName != 'SCRIPT') { | |
$A(el.childNodes).each(function (onlyChild) { | |
var pos = onlyChild.textContent.indexOf(sword); | |
if (onlyChild.nodeType == Node.TEXT_NODE && pos >= 0) { | |
//console.log(onlyChild); | |
var spannode = document.createElement('span'); | |
spannode.className = 'highlight'; | |
var middlebit = onlyChild.splitText(pos); | |
var endbit = middlebit.splitText(sword.length); | |
var middleclone = middlebit.cloneNode(true); | |
spannode.appendChild(middleclone); | |
middlebit.parentNode.replaceChild(spannode, middlebit); | |
//onlyChild. = el.innerHTML.replace(new RegExp('('+sword+')', 'gi'), '<span class="highlight">$1</span>'); | |
} | |
}); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment