-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhighlight.js
70 lines (61 loc) · 2.16 KB
/
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
define([
'dojo/_base/declare',
'dojo/_base/lang',
'dojo/dom-construct'
], function(declare , lang, domConstruct){
return declare(null, {
cssClass: 'highlight',
searchNode: null,
cache: null,
constructor: function(options){
lang.mixin( this, options);
},
replaceHtml: function(searchText, replacement, searchNode){
if (!searchText || typeof replacement === 'undefined') return;
searchNode = searchNode || this.searchNode;
if(this.cache === null){
this.cache = lang.clone( searchNode );
}
const regex = typeof searchText === 'string' ? new RegExp(searchText, 'gi') : searchText;
const childNodes = (searchNode || document.body).childNodes;
const excludes = 'html,head,style,title,link,meta,script,object,iframe';
let cnLength = childNodes.length;
while (cnLength--) {
const currentNode = childNodes[cnLength];
if (currentNode.nodeType === 1 && (excludes + ',').indexOf(currentNode.nodeName.toLowerCase() + ',') === -1) {
this.replaceHtml(searchText, replacement, currentNode);
}
if (currentNode.nodeType !== 3 || !regex.test(currentNode.data) ) {
continue;
}
const parent = currentNode.parentNode,
frag = (function(){
const html = currentNode.data.replace(regex, replacement);
const wrap = document.createElement('div');
const frag = document.createDocumentFragment();
wrap.innerHTML = html;
while (wrap.firstChild){
frag.appendChild(wrap.firstChild);
}
return frag;
})();
parent.insertBefore(frag, currentNode);
parent.removeChild(currentNode);
}
},
clear: function(){
if(this.cache){
domConstruct.place( this.cache , this.searchNode, "after");
domConstruct.destroy(this.searchNode);
this.searchNode = this.cache;
this.cache = null;
}
},
cssHighlight: function(searchText){
const me = this;
this.replaceHtml(searchText , function(text) {
return '<span class="'+me.cssClass+'">'+text+'</span>';
});
}
});
});