Skip to content

Commit 71a6fa5

Browse files
Handle another double quote special case (#54474) (#54560)
Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com> Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
1 parent a64013f commit 71a6fa5

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/legacy/core_plugins/console/public/np_ready/lib/utils/__tests__/utils_string_expanding.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,33 @@ Correctly handle new lines in triple quotes
5252
SELECT * FROM "TABLE"
5353
"""
5454
}
55+
==========
56+
Single quotes escaped special case, start and end
57+
-------------------------------------
58+
{
59+
"query": "\"test\""
60+
}
61+
-------------------------------------
62+
{
63+
"query": "\"test\""
64+
}
65+
==========
66+
Single quotes escaped special case, start
67+
-------------------------------------
68+
{
69+
"query": "\"test"
70+
}
71+
-------------------------------------
72+
{
73+
"query": "\"test"
74+
}
75+
==========
76+
Single quotes escaped special case, end
77+
-------------------------------------
78+
{
79+
"query": "test\""
80+
}
81+
-------------------------------------
82+
{
83+
"query": "test\""
84+
}

src/legacy/core_plugins/console/public/np_ready/lib/utils/utils.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,20 @@ export function expandLiteralStrings(data: string) {
8484
// Expand to triple quotes if there are _any_ slashes
8585
if (string.match(/\\./)) {
8686
const firstDoubleQuoteIdx = string.indexOf('"');
87+
const lastDoubleQuoteIdx = string.lastIndexOf('"');
88+
89+
// Handle a special case where we may have a value like "\"test\"". We don't
90+
// want to expand this to """"test"""" - so we terminate before processing the string
91+
// further if we detect this either at the start or end of the double quote section.
92+
93+
if (string[firstDoubleQuoteIdx + 1] === '\\' && string[firstDoubleQuoteIdx + 2] === '"') {
94+
return string;
95+
}
96+
97+
if (string[lastDoubleQuoteIdx - 1] === '"' && string[lastDoubleQuoteIdx - 2] === '\\') {
98+
return string;
99+
}
100+
87101
const colonAndAnySpacing = string.slice(0, firstDoubleQuoteIdx);
88102
const rawStringifiedValue = string.slice(firstDoubleQuoteIdx, string.length);
89103
// Remove one level of JSON stringification

0 commit comments

Comments
 (0)