Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit 45278ec

Browse files
ivovizThomasBurleson
authored andcommitted
fix(highlight-flags): make ^/$ work properly
Fixes #8096 Closes #8120
1 parent b49ebcf commit 45278ec

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

src/components/autocomplete/autocomplete.spec.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,6 +1204,40 @@ describe('<md-autocomplete>', function() {
12041204

12051205
element.remove();
12061206
}));
1207+
1208+
it('should properly apply highlight flags', inject(function() {
1209+
var template = '<div md-highlight-text="query" md-highlight-flags="{{flags}}">{{message}}</div>';
1210+
var scope = createScope(null, {message: 'Some text', query: 'some', flags: '^i'});
1211+
var element = compile(template, scope);
1212+
1213+
expect(element.html()).toBe('<span class="highlight">Some</span> text');
1214+
1215+
scope.query = 'text';
1216+
scope.$apply();
1217+
1218+
expect(element.html()).toBe('Some text');
1219+
1220+
scope.message = 'Some text, some flags';
1221+
scope.query = 'some';
1222+
scope.flags = 'ig';
1223+
element = compile(template, scope);
1224+
1225+
expect(element.html()).toBe('<span class="highlight">Some</span> text, <span class="highlight">some</span> flags');
1226+
1227+
scope.query = 'some';
1228+
scope.flags = '^i';
1229+
element = compile(template, scope);
1230+
1231+
expect(element.html()).toBe('<span class="highlight">Some</span> text, some flags');
1232+
1233+
scope.query = 's';
1234+
scope.flags = '$i';
1235+
element = compile(template, scope);
1236+
1237+
expect(element.html()).toBe('Some text, some flag<span class="highlight">s</span>');
1238+
1239+
element.remove();
1240+
}));
12071241
});
12081242

12091243
});

src/components/autocomplete/js/highlightController.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,9 @@ function MdHighlightCtrl ($scope, $element, $attrs) {
3232
}
3333

3434
function getRegExp (text, flags) {
35-
var str = '';
36-
if (flags.indexOf('^') >= 1) str += '^';
37-
str += text;
38-
if (flags.indexOf('$') >= 1) str += '$';
39-
return new RegExp(sanitize(str), flags.replace(/[\$\^]/g, ''));
35+
var startFlag = '', endFlag = '';
36+
if (flags.indexOf('^') >= 0) startFlag = '^';
37+
if (flags.indexOf('$') >= 0) endFlag = '$';
38+
return new RegExp(startFlag + sanitize(text) + endFlag, flags.replace(/[\$\^]/g, ''));
4039
}
4140
}

0 commit comments

Comments
 (0)