]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/javascript/jquery/jquery.highLight.js
Release 6.5.0
[Github/sugarcrm.git] / include / javascript / jquery / jquery.highLight.js
1     jQuery.fn.highlight = function(pat) {
2         function innerHighlight(node, pat) {
3             var skip = 0;
4             if (node.nodeType == 3) {
5                 var pos = node.data.toUpperCase().indexOf(pat);
6                 if (pos >= 0) {
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);
14                     skip = 1;
15                 }
16             }
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);
20                 }
21             }
22             return skip;
23         }
24
25         return this.each(function() {
26             innerHighlight(this, pat.toUpperCase());
27         });
28     };
29
30     jQuery.fn.removeHighlight = function() {
31         return this.find("span.highlight").each(
32                 function() {
33                     this.parentNode.firstChild.nodeName;
34                     with (this.parentNode) {
35                         replaceChild(this.firstChild, this);
36                         normalize();
37                     }
38                 }).end();
39     };