Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Console] Update indentation behaviour #45249

Merged
merged 11 commits into from
Sep 11, 2019
30 changes: 25 additions & 5 deletions src/legacy/core_plugins/console/public/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,32 @@ utils.collapseLiteralStrings = function (data) {
return splitData.join('');
};

/*
The following regex describes global match on:
1. one colon followed by any number of space characters
2. one double quote (not escaped, special case for JSON in JSON).
3. greedily match any non double quote and non newline char OR any escaped double quote char (non-capturing).
4. handle a special case where an escaped slash may be the last character
5. one double quote

For instance: `: "some characters \" here"`
Will match and be expanded to: `"""some characters " here"""`

*/

const LITERAL_STRING_CANDIDATES = /((:[\s\r\n]*)([^\\])"(\\"|[^"\n])*\\?")/g;

utils.expandLiteralStrings = function (data) {
return data.replace(/("(?:\\"|[^"])*?")/g, function (match, string) {
// expand things with two slashes or more
if (string.split(/\\./).length > 2) {
string = JSON.parse(string).replace('^\s*\n', '').replace('\n\s*^', '');
return '"""' + string + '"""';
return data.replace(LITERAL_STRING_CANDIDATES, function (match, string) {
// Expand to triple quotes if there are _any_ slashes
if (string.match(/\\./)) {
const firstDoubleQuoteIdx = string.indexOf('"');
const colonAndAnySpacing = string.slice(0, firstDoubleQuoteIdx);
const rawStringifiedValue = string.slice(firstDoubleQuoteIdx, string.length);
const jsonValue = JSON.parse(rawStringifiedValue)
.replace('^\s*\n', '')
.replace('\n\s*^', '');
return `${colonAndAnySpacing}"""${jsonValue}"""`;
} else {
return string;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,41 @@
Scripts in requests
-------------------------------------
{
"f": { "script" : { "source": "\ntest\ntest\\2\n" } },
"g": { "script" : "second + \"\\\";" },
"f": "short with \\",
"h": 1,
"f": { "script": { "source": "\ntest\ntest\\\\\\\\\\\\\\\\2\n" } },
"g": { "script": "second + \"\\\";" },
"a": "short with \\",
"\\\\h": 1,
"script": "a + 2"
}
-------------------------------------
{
"f": { "script" : { "source": """
"f": { "script": { "source": """
test
test\2
test\\\\\\\\2
""" } },
"g": { "script" : """second + "\";""" },
"f": "short with \\",
"h": 1,
"g": { "script": """second + "\";""" },
"a": """short with \""",
"\\\\h": 1,
"script": "a + 2"
}
==========
Preserve triple quotes
-------------------------------------
{
"content\\\": "tri\"ple",
}
-------------------------------------
{
"content\\\": """tri"ple""",
}
==========
Correctly parse with JSON embedded inside values
-------------------------------------
{
"content\\\\": " { \"json\": \"inside\\\\\" ok! 1\n \" } "
}
-------------------------------------
{
"content\\\\": """ { "json": "inside\\" ok! 1
" } """
}