From 9220f83b3dcd4376d1ec8a5ab5bdf0a0e031a65f Mon Sep 17 00:00:00 2001 From: Benjamin Schweizer Date: Mon, 6 Aug 2018 21:54:12 +0200 Subject: [PATCH] replaced escape() call, renamed formatter to be more expressive --- docs/sources/reference/templating.md | 2 +- .../features/templating/specs/template_srv.jest.ts | 6 +++--- public/app/features/templating/template_srv.ts | 13 ++++++++++--- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/docs/sources/reference/templating.md b/docs/sources/reference/templating.md index d59117fefea..1482eb34350 100644 --- a/docs/sources/reference/templating.md +++ b/docs/sources/reference/templating.md @@ -52,7 +52,7 @@ Filter Option | Example | Raw | Interpolated | Description `csv`| ${servers:csv} | `'test1', 'test2'` | `test1,test2` | Formats multi-value variable as a comma-separated string `distributed`| ${servers:distributed} | `'test1', 'test2'` | `test1,servers=test2` | Formats multi-value variable in custom format for OpenTSDB. `lucene`| ${servers:lucene} | `'test', 'test2'` | `("test" OR "test2")` | Formats multi-value variable as a lucene expression. -`urlescape` | ${servers:urlescape} | `'foo()bar baz', 'test2'` | `{foo%28%29bar%20baz%2Ctest2}` | Formats multi-value variable into a glob, url escaped +`percentencode` | ${servers:percentencode} | `'foo()bar BAZ', 'test2'` | `{foo%28%29bar%20BAZ%2Ctest2}` | Formats multi-value variable into a glob, percent-escaped Test the formatting options on the [Grafana Play site](http://play.grafana.org/d/cJtIfcWiz/template-variable-formatting-options?orgId=1). diff --git a/public/app/features/templating/specs/template_srv.jest.ts b/public/app/features/templating/specs/template_srv.jest.ts index 85a159fc098..b4501e81f59 100644 --- a/public/app/features/templating/specs/template_srv.jest.ts +++ b/public/app/features/templating/specs/template_srv.jest.ts @@ -275,9 +275,9 @@ describe('templateSrv', function() { expect(result).toBe('test,test2'); }); - it('multi value and urlescape format should render url-escaped string', function() { - var result = _templateSrv.formatValue(['foo()bar baz', 'test2'], 'urlescape'); - expect(result).toBe('%7Bfoo%28%29bar%20baz%2Ctest2%7D'); + it('multi value and percentencode format should render percent-encoded string', function() { + var result = _templateSrv.formatValue(['foo()bar BAZ', 'test2'], 'percentencode'); + expect(result).toBe('%7Bfoo%28%29bar%20BAZ%2Ctest2%7D'); }); it('slash should be properly escaped in regex format', function() { diff --git a/public/app/features/templating/template_srv.ts b/public/app/features/templating/template_srv.ts index 7ce539b6506..3d462f1bcde 100644 --- a/public/app/features/templating/template_srv.ts +++ b/public/app/features/templating/template_srv.ts @@ -83,6 +83,13 @@ export class TemplateSrv { return '(' + quotedValues.join(' OR ') + ')'; } + // like encodeURIComponent() but for all characters except alpha-numerics + encodeURIQueryValue(str) { + return str.replace(/[^a-z0-9]/gi, function(c) { + return '%' + c.charCodeAt(0).toString(16); + }); + } + formatValue(value, format, variable) { // for some scopedVars there is no variable variable = variable || {}; @@ -124,12 +131,12 @@ export class TemplateSrv { } return value; } - case 'urlescape': { + case 'percentencode': { // like glob, but url escaped if (_.isArray(value)) { - return escape('{' + value.join(',') + '}'); + return this.encodeURIQueryValue('{' + value.join(',') + '}'); } - return escape(value); + return this.encodeURIQueryValue(value); } default: { if (_.isArray(value)) {