diff --git a/public/app/angular/misc.ts b/public/app/angular/misc.ts index feb04eddee0..202f69a60df 100644 --- a/public/app/angular/misc.ts +++ b/public/app/angular/misc.ts @@ -1,7 +1,6 @@ import angular from 'angular'; import Clipboard from 'clipboard'; import coreModule from '../core/core_module'; -import kbn from 'app/core/utils/kbn'; import { appEvents } from 'app/core/core'; import { AppEvents } from '@grafana/data'; @@ -14,9 +13,11 @@ function tip($compile: any) { '&]/g, (m: string) => '&#' + m.charCodeAt(0) + ';') + '\'">'; - _t = _t.replace(/{/g, '\\{').replace(/}/g, '\\}'); elem.replaceWith($compile(angular.element(_t))(scope)); }, }; diff --git a/public/app/core/utils/kbn.test.ts b/public/app/core/utils/kbn.test.ts index 103b149e282..d6187c90995 100644 --- a/public/app/core/utils/kbn.test.ts +++ b/public/app/core/utils/kbn.test.ts @@ -73,3 +73,11 @@ describe('describe_interval', () => { expect(() => kbn.describeInterval('xyz')).toThrow(); }); }); + +describe('addSlashes', () => { + it('properly escapes backslashes, single-quotes, double-quotes and the number zero', () => { + expect(kbn.addSlashes('this is a \'test\' with "quotes" backslashes (\\) and zero (0)')).toEqual( + 'this is a \\\'test\\\' with \\"quotes\\" backslashes (\\\\) and zero (\\0)' + ); + }); +}); diff --git a/public/app/core/utils/kbn.ts b/public/app/core/utils/kbn.ts index 255282c3184..6b797dbf047 100644 --- a/public/app/core/utils/kbn.ts +++ b/public/app/core/utils/kbn.ts @@ -47,13 +47,7 @@ const kbn = { return strings.join(':'); }, toPercent: (nr: number, outOf: number) => Math.floor((nr / outOf) * 10000) / 100 + '%', - addSlashes: (str: string) => { - str = str.replace(/\\/g, '\\\\'); - str = str.replace(/\'/g, "\\'"); - str = str.replace(/\"/g, '\\"'); - str = str.replace(/\0/g, '\\0'); - return str; - }, + addSlashes: (str: string) => str.replace(/[\'\"\\0]/g, '\\$&'), /** @deprecated since 7.2, use grafana/data */ describeInterval: (str: string) => { deprecationWarning('kbn.ts', 'kbn.stringToJsRegex()', '@grafana/data');