diff --git a/src/js/scriptlet-filtering-core.js b/src/js/scriptlet-filtering-core.js index e1f963d4b5ff0..021e3af305d0d 100644 --- a/src/js/scriptlet-filtering-core.js +++ b/src/js/scriptlet-filtering-core.js @@ -98,24 +98,18 @@ const patchScriptlet = (content, arglist) => { ); }; +const requote = s => { + if ( /^(["'`]).+\1$|,/.test(s) === false ) { return s; } + if ( s.includes("'") === false ) { return `'${s}'`; } + if ( s.includes('"') === false ) { return `"${s}"`; } + if ( s.includes('`') === false ) { return `\`${s}\``; } + return `'${s.replace(/'/g, "\\'")}'`; +}; + const decompile = json => { - const args = JSON.parse(json).map(s => { - if ( /^(["'`]).+\1$/.test(s) ) { - const c0 = s.charAt(0); - const inner = s.slice(1,-1); - if ( c0 === '"' || c0 === '`' ) { - return inner.includes("'") - ? '`' + s.replace(/`/g, '\\`') + '`' - : `'${s}'`; - } - return inner.includes('"') - ? '`' + s.replace(/`/g, '\\`') + '`' - : `"${s}"`; - } - return s.replace(/,/g, '\\,'); - }); + const args = JSON.parse(json); if ( args.length === 0 ) { return '+js()'; } - return `+js(${args.join(', ')})`; + return `+js(${args.map(s => requote(s)).join(', ')})`; }; /******************************************************************************/