1 jQuery.fn.highlight = function(pat) {
2 function innerHighlight(node, pat) {
4 if (node.nodeType == 3) {
5 var pos = node.data.toUpperCase().indexOf(pat);
7 var spannode = document.createElement('span');
8 spannode.className = 'highlight';
9 var middlebit = node.splitText(pos);
10 var endbit = middlebit.splitText(pat.length);
11 var middleclone = middlebit.cloneNode(true);
12 spannode.appendChild(middleclone);
13 middlebit.parentNode.replaceChild(spannode, middlebit);
17 else if (node.nodeType == 1 && node.childNodes && !/(script|style)/i.test(node.tagName)) {
18 for (var i = 0; i < node.childNodes.length; ++i) {
19 i += innerHighlight(node.childNodes[i], pat);
25 return this.each(function() {
26 innerHighlight(this, pat.toUpperCase());
30 jQuery.fn.removeHighlight = function() {
31 return this.find("span.highlight").each(
33 this.parentNode.firstChild.nodeName;
34 with (this.parentNode) {
35 replaceChild(this.firstChild, this);