From 945fea1dc0ac365685c4af304cc9ded44bee41c0 Mon Sep 17 00:00:00 2001 From: Martin Raifer Date: Thu, 1 Aug 2024 15:19:33 +0200 Subject: [PATCH] allow to enter strings with quote marks in raw tag editor's text mode (#10371) and use fully quoted strings for cases which have characters that need to be encoded old: tag=foo\"bar\" -> new: tag=foo"bar" or tag="foo\"bar\"" old: tag=foo\nbar -> new: tag="foo\nbar" falls back to raw tag values if a quoted string cannot be parsed --- modules/ui/sections/raw_tag_editor.js | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/modules/ui/sections/raw_tag_editor.js b/modules/ui/sections/raw_tag_editor.js index 2db9c4d2e..672dff86a 100644 --- a/modules/ui/sections/raw_tag_editor.js +++ b/modules/ui/sections/raw_tag_editor.js @@ -321,21 +321,25 @@ export function uiSectionRawTagEditor(id, context) { } function stringify(s) { - return JSON.stringify(s).slice(1, -1); // without leading/trailing " + const stringified = JSON.stringify(s).slice(1, -1); // without leading/trailing " + if (stringified !== s) { + return `"${stringified}"`; + } else { + return s; + } } function unstringify(s) { - var leading = ''; - var trailing = ''; - if (s.length < 1 || s.charAt(0) !== '"') { - leading = '"'; + const isQuoted = s.length > 1 && s.charAt(0) === '"' && s.charAt(s.length - 1) === '"'; + if (isQuoted) { + try { + return JSON.parse(s); + } catch { + return s; + } + } else { + return s; } - if (s.length < 2 || s.charAt(s.length - 1) !== '"' || - (s.charAt(s.length - 1) === '"' && s.charAt(s.length - 2) === '\\') - ) { - trailing = '"'; - } - return JSON.parse(leading + s + trailing); } function rowsToText(rows) {