From 6b25453f11e2230e442f3af48db99a5b05516222 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Tue, 21 Jul 2015 11:00:09 +0200 Subject: [PATCH] templating(influxdb): regex escape values when multi or all format is enabled, closes #2373 --- .../features/templating/templateValuesSrv.js | 21 +++++++++++++++++-- public/test/specs/templateValuesSrv-specs.js | 13 ++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/public/app/features/templating/templateValuesSrv.js b/public/app/features/templating/templateValuesSrv.js index c1d787804d7..57cce7a8df5 100644 --- a/public/app/features/templating/templateValuesSrv.js +++ b/public/app/features/templating/templateValuesSrv.js @@ -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 = '{'; diff --git a/public/test/specs/templateValuesSrv-specs.js b/public/test/specs/templateValuesSrv-specs.js index d0af589a068..e9c4adb4a65 100644 --- a/public/test/specs/templateValuesSrv-specs.js +++ b/public/test/specs/templateValuesSrv-specs.js @@ -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'); + }); + }); + }); });