mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
templating(influxdb): regex escape values when multi or all format is enabled, closes #2373
This commit is contained in:
parent
06a32ce7be
commit
6b25453f11
@ -223,10 +223,25 @@ function (angular, _, kbn) {
|
||||
}
|
||||
|
||||
return _.map(_.keys(options).sort(), function(key) {
|
||||
return { text: key, value: key };
|
||||
var option = { text: key, value: key };
|
||||
|
||||
// check if values need to be regex escaped
|
||||
if (self.shouldRegexEscape(variable)) {
|
||||
option.value = self.regexEscape(option.value);
|
||||
}
|
||||
|
||||
return option;
|
||||
});
|
||||
};
|
||||
|
||||
this.shouldRegexEscape = function(variable) {
|
||||
return (variable.includeAll || variable.multi) && variable.allFormat.indexOf('regex') !== -1;
|
||||
};
|
||||
|
||||
this.regexEscape = function(value) {
|
||||
return value.replace(/[-[\]{}()*+!<=:?.\/\\^$|#\s,]/g, '\\$&');
|
||||
};
|
||||
|
||||
this.addAllOption = function(variable) {
|
||||
var allValue = '';
|
||||
switch(variable.allFormat) {
|
||||
@ -237,7 +252,9 @@ function (angular, _, kbn) {
|
||||
allValue = '.*';
|
||||
break;
|
||||
case 'regex values':
|
||||
allValue = '(' + _.pluck(variable.options, 'text').join('|') + ')';
|
||||
allValue = '(' + _.map(variable.options, function(option) {
|
||||
return self.regexEscape(option.text);
|
||||
}).join('|') + ')';
|
||||
break;
|
||||
default:
|
||||
allValue = '{';
|
||||
|
@ -322,6 +322,19 @@ define([
|
||||
});
|
||||
});
|
||||
|
||||
describeUpdateVariable('with include all regex values and values require escaping', function(scenario) {
|
||||
scenario.setup(function() {
|
||||
scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'regex values' };
|
||||
scenario.queryResult = [{text: '/root'}, {text: '/var'}, { text: '/lib'}];
|
||||
});
|
||||
|
||||
it('should regex escape options', function() {
|
||||
expect(scenario.variable.options[0].value).to.be('(\\/lib|\\/root|\\/var)');
|
||||
expect(scenario.variable.options[1].value).to.be('\\/lib');
|
||||
expect(scenario.variable.options[1].text).to.be('/lib');
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user