Bug 29578: Upgrade the jQuery highlighter plugin
[koha.git] / koha-tmpl / opac-tmpl / lib / jquery / plugins / jquery.highlight-5.js
1 /*
2
3 highlight v5
4
5 Highlights arbitrary terms.
6
7 <http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html>
8
9 MIT license.
10
11 Johann Burkard
12 <http://johannburkard.de>
13 <mailto:jb@eaio.com>
14
15 */
16
17 jQuery.fn.highlight = function(pat) {
18  function innerHighlight(node, pat) {
19   var skip = 0;
20   if (node.nodeType == 3) {
21    var pos = node.data.toUpperCase().indexOf(pat);
22    pos -= (node.data.substr(0, pos).toUpperCase().length - node.data.substr(0, pos).length);
23    if (pos >= 0) {
24     var spannode = document.createElement('span');
25     spannode.className = 'term';
26     var middlebit = node.splitText(pos);
27     var endbit = middlebit.splitText(pat.length);
28     var middleclone = middlebit.cloneNode(true);
29     spannode.appendChild(middleclone);
30     middlebit.parentNode.replaceChild(spannode, middlebit);
31     skip = 1;
32    }
33   }
34   else if (node.nodeType == 1 && node.childNodes && !/(script|style)/i.test(node.tagName)) {
35    for (var i = 0; i < node.childNodes.length; ++i) {
36     i += innerHighlight(node.childNodes[i], pat);
37    }
38   }
39   return skip;
40  }
41  return this.length && pat && pat.length ? this.each(function() {
42   innerHighlight(this, pat.toUpperCase());
43  }) : this;
44 };
45
46 jQuery.fn.removeHighlight = function() {
47  return this.find("span.term").each(function() {
48   $(this).contents().unwrap();
49  });
50 };