Skip to content

Commit

Permalink
[Console] Update indentation behaviour (#45249)
Browse files Browse the repository at this point in the history
* SQL template with triple quote in completion

* Slight update to SQL query template and bugfix for collapse/expand of request bodies

* Add comment and slight update to triple quote expansion

* Updated tests after changes to newline behaviour

* Restore old backslash triple quote expansion behaviour

* Handle some more tricky cases, but make sure to preserve newlines.
Update tests for new cases too

* Make regex more specific
Expanded tests

* Now make it x-browser

* Slight refactor and renaming

* Update comment
  • Loading branch information
jloleysens authored Sep 11, 2019
1 parent 745d76d commit 98bf7bf
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 14 deletions.
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
" } """
}

0 comments on commit 98bf7bf

Please sign in to comment.