mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Chore: Correctly escape strings in JSONFormatter (#44096)
* Chore: Properly escape double-quotes in strings in JSONFormatter * escape slashes
This commit is contained in:
parent
8ee3f59cd4
commit
bddf3d7558
@ -0,0 +1,45 @@
|
||||
import { formatString } from './helpers';
|
||||
|
||||
describe('JSONFormatter helpers', () => {
|
||||
describe('escapeString', () => {
|
||||
it("does not escape strings that don't contain quotes", () => {
|
||||
const input = 'hello world';
|
||||
const expected = 'hello world';
|
||||
|
||||
const result = formatString(input);
|
||||
expect(result).toEqual(expected);
|
||||
});
|
||||
|
||||
it('does not escape strings that contain single quotes', () => {
|
||||
const input = `'hello world'`;
|
||||
const expected = `'hello world'`;
|
||||
|
||||
const result = formatString(input);
|
||||
expect(result).toEqual(expected);
|
||||
});
|
||||
|
||||
it('does escapes strings that contain one double quote', () => {
|
||||
const input = `"hello world`;
|
||||
const expected = `\\"hello world`;
|
||||
|
||||
const result = formatString(input);
|
||||
expect(result).toEqual(expected);
|
||||
});
|
||||
|
||||
it('does escapes strings that contain two double quotes', () => {
|
||||
const input = `"hello world"`;
|
||||
const expected = `\\"hello world\\"`;
|
||||
|
||||
const result = formatString(input);
|
||||
expect(result).toEqual(expected);
|
||||
});
|
||||
|
||||
it('does escapes a string that looks like JSON', () => {
|
||||
const input = `{"hello": "world"}`;
|
||||
const expected = `{\\"hello\\": \\"world\\"}`;
|
||||
|
||||
const result = formatString(input);
|
||||
expect(result).toEqual(expected);
|
||||
});
|
||||
});
|
||||
});
|
@ -4,8 +4,8 @@
|
||||
/*
|
||||
* Escapes `"` characters from string
|
||||
*/
|
||||
function escapeString(str: string): string {
|
||||
return str.replace('"', '"');
|
||||
export function formatString(str: string): string {
|
||||
return str.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
|
||||
}
|
||||
|
||||
/*
|
||||
@ -62,7 +62,7 @@ export function getValuePreview(object: object, value: string): string {
|
||||
}
|
||||
|
||||
if (type === 'string') {
|
||||
value = '"' + escapeString(value) + '"';
|
||||
value = '"' + formatString(value) + '"';
|
||||
}
|
||||
if (type === 'function') {
|
||||
// Remove content of the function
|
||||
|
Loading…
Reference in New Issue
Block a user