forked from jobrapido/jr-highlight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jr-highlight.js
102 lines (89 loc) · 2.91 KB
/
jr-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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
highlight v4
Highlights arbitrary terms.
<http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html>
MIT license.
Johann Burkard
<http://johannburkard.de>
<mailto:[email protected]>
*/
!function($) {
$.fn.highlight = function(pat, ignore, skipClasses) {
function replaceDiacritics(str) {
var diacritics = [ [ /[\u00c0-\u00c6]/g, 'A' ],
[ /[\u00e0-\u00e6]/g, 'a' ],
[ /[\u00c7]/g, 'C' ],
[ /[\u00e7]/g, 'c' ],
[ /[\u00c8-\u00cb]/g, 'E' ],
[ /[\u00e8-\u00eb]/g, 'e' ],
[ /[\u00cc-\u00cf]/g, 'I' ],
[ /[\u00ec-\u00ef]/g, 'i' ],
[ /[\u00d1|\u0147]/g, 'N' ],
[ /[\u00f1|\u0148]/g, 'n' ],
[ /[\u00d2-\u00d8|\u0150]/g, 'O' ],
[ /[\u00f2-\u00f8|\u0151]/g, 'o' ],
[ /[\u0160]/g, 'S' ],
[ /[\u0161]/g, 's' ],
[ /[\u00d9-\u00dc]/g, 'U' ],
[ /[\u00f9-\u00fc]/g, 'u' ],
[ /[\u00dd]/g, 'Y' ],
[ /[\u00fd]/g, 'y' ]
];
for ( var i = 0; i < diacritics.length; i++) {
str = str.replace(diacritics[i][0], diacritics[i][1]);
}
return str;
}
function innerHighlight(node, pat, ignore) {
var skip = 0;
if (node.nodeType === 3) {
var isPatternArray = $.isArray(pat);
if (!isPatternArray) {
pat = [pat];
}
var patternCount = pat.length;
for (var ii = 0; ii < patternCount; ii++) {
var currentTerm = (ignore ? replaceDiacritics(pat[ii]) : pat[ii]).toUpperCase();
var pos = (ignore ? replaceDiacritics(node.data) : node.data).toUpperCase().indexOf(currentTerm);
if (pos >= 0) {
var spannode = document.createElement('span');
spannode.className = 'highlight';
var middlebit = node.splitText(pos);
var endbit = middlebit.splitText(currentTerm.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) && !hasSkippedClass(node)) {
for (var i = 0; i < node.childNodes.length; ++i) {
i += innerHighlight(node.childNodes[i], pat, ignore);
}
}
return skip;
}
function hasSkippedClass(node) {
return $(skipClasses).is(function (i) {
return $(node).hasClass(skipClasses[i]);
});
}
return this.length && pat && pat.length ? this.each(function() {
ignore = typeof ignore !== 'undefined' && ignore != null ? ignore : $.fn.highlight.defaults.ignore;
skipClasses = typeof skipClasses !== 'undefined' && ignore != null ? skipClasses : [];
innerHighlight(this, pat, ignore);
}) : this;
};
$.fn.highlight.defaults = {
ignore : false
};
$.fn.removeHighlight = function() {
return this.find('span.highlight').each(function() {
this.parentNode.firstChild.nodeName;
with(this.parentNode) {
replaceChild(this.firstChild, this);
normalize();
}
}).end();
};
}(window.jQuery);