From 98bf7bfdd658fd16a767f599338a7b67f8af7a33 Mon Sep 17 00:00:00 2001 From: Jean-Louis Leysens Date: Wed, 11 Sep 2019 14:18:20 +0200 Subject: [PATCH] [Console] Update indentation behaviour (#45249) * 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 --- .../core_plugins/console/public/src/utils.js | 30 +++++++++++--- .../tests/src/utils_string_expanding.txt | 39 ++++++++++++++----- 2 files changed, 55 insertions(+), 14 deletions(-) diff --git a/src/legacy/core_plugins/console/public/src/utils.js b/src/legacy/core_plugins/console/public/src/utils.js index 15a32f90942a2..5b6bd1646c300 100644 --- a/src/legacy/core_plugins/console/public/src/utils.js +++ b/src/legacy/core_plugins/console/public/src/utils.js @@ -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; } diff --git a/src/legacy/core_plugins/console/public/tests/src/utils_string_expanding.txt b/src/legacy/core_plugins/console/public/tests/src/utils_string_expanding.txt index 4f3abf177490a..34bf0f3bc20e7 100644 --- a/src/legacy/core_plugins/console/public/tests/src/utils_string_expanding.txt +++ b/src/legacy/core_plugins/console/public/tests/src/utils_string_expanding.txt @@ -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 + " } """ +}