This repository has been archived by the owner on Sep 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
highlightController.js
114 lines (86 loc) · 3 KB
/
highlightController.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
103
104
105
106
107
108
109
110
111
112
113
114
angular
.module('material.components.autocomplete')
.controller('MdHighlightCtrl', MdHighlightCtrl);
function MdHighlightCtrl ($scope, $element, $attrs, $mdUtil) {
this.$scope = $scope;
this.$element = $element;
this.$attrs = $attrs;
this.$mdUtil = $mdUtil;
// Cache the Regex to avoid rebuilding each time.
this.regex = null;
}
MdHighlightCtrl.prototype.init = function(unsafeTermFn, unsafeContentFn) {
this.flags = this.$attrs.mdHighlightFlags || '';
this.unregisterFn = this.$scope.$watch(function($scope) {
return {
term: unsafeTermFn($scope),
contentText: unsafeContentFn($scope)
};
}.bind(this), this.onRender.bind(this), true);
this.$element.on('$destroy', this.unregisterFn);
};
/**
* Triggered once a new change has been recognized and the highlighted
* text needs to be updated.
*/
MdHighlightCtrl.prototype.onRender = function(state, prevState) {
var contentText = state.contentText;
/* Update the regex if it's outdated, because we don't want to rebuilt it constantly. */
if (this.regex === null || state.term !== prevState.term) {
this.regex = this.createRegex(state.term, this.flags);
}
/* If a term is available apply the regex to the content */
if (state.term) {
this.applyRegex(contentText);
} else {
this.$element.text(contentText);
}
};
/**
* Decomposes the specified text into different tokens (whether match or not).
* Breaking down the string guarantees proper XSS protection due to the native browser
* escaping of unsafe text.
*/
MdHighlightCtrl.prototype.applyRegex = function(text) {
var tokens = this.resolveTokens(text);
this.$element.empty();
tokens.forEach(function (token) {
if (token.isMatch) {
var tokenEl = angular.element('<span class="highlight">').text(token.text);
this.$element.append(tokenEl);
} else {
this.$element.append(document.createTextNode(token));
}
}.bind(this));
};
/**
* Decomposes the specified text into different tokens by running the regex against the text.
*/
MdHighlightCtrl.prototype.resolveTokens = function(string) {
var tokens = [];
var lastIndex = 0;
// Use replace here, because it supports global and single regular expressions at same time.
string.replace(this.regex, function(match, index) {
appendToken(lastIndex, index);
tokens.push({
text: match,
isMatch: true
});
lastIndex = index + match.length;
});
// Append the missing text as a token.
appendToken(lastIndex);
return tokens;
function appendToken(from, to) {
var targetText = string.slice(from, to);
targetText && tokens.push(targetText);
}
};
/** Creates a regex for the specified text with the given flags. */
MdHighlightCtrl.prototype.createRegex = function(term, flags) {
var startFlag = '', endFlag = '';
var regexTerm = this.$mdUtil.sanitize(term);
if (flags.indexOf('^') >= 0) startFlag = '^';
if (flags.indexOf('$') >= 0) endFlag = '$';
return new RegExp(startFlag + regexTerm + endFlag, flags.replace(/[$^]/g, ''));
};