forked from vdw/HideSeek
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.highlight.js
53 lines (50 loc) · 1.69 KB
/
jquery.highlight.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* highlight v4
*
* Highlights arbitrary terms.
*
* <http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html>
*
* @license MIT
* @author Johann Burkard <mailto:[email protected]>
* @link http://johannburkard.de
*/
(function ($, hideseek) {
$.fn.highlight = function (pat, ignoreAccents) {
function innerHighlight(node, pat) {
var skip = 0;
if (node.nodeType == 3) {
var data = ignoreAccents ? hideseek.removeAccents(node.data) : node.data;
var pos = data.toUpperCase().indexOf(pat);
if (pos >= 0) {
var spannode = document.createElement('span');
spannode.className = 'highlight';
var middlebit = node.splitText(pos);
var endbit = middlebit.splitText(pat.length);
var middleclone = middlebit.cloneNode(true);
spannode.appendChild(middleclone);
middlebit.parentNode.replaceChild(spannode, middlebit);
skip = 1;
}
}
else if (node.nodeType == 1 && node.childNodes && !/(script|style)/i.test(node.tagName)) {
for (var i = 0; i < node.childNodes.length; ++i) {
i += innerHighlight(node.childNodes[i], pat);
}
}
return skip;
}
return this.length && pat && pat.length ? this.each(function () {
innerHighlight(this, pat.toUpperCase());
}) : this;
};
$.fn.removeHighlight = function () {
return this.find("span.highlight").each(function () {
this.parentNode.firstChild.nodeName;
with (this.parentNode) {
replaceChild(this.firstChild, this);
normalize();
}
}).end();
};
})(jQuery, window["hideseek"] = window["hideseek"] || {});