Skip to content

Commit

Permalink
Code review recent commit re. quoting parameters
Browse files Browse the repository at this point in the history
Related commit:
fa3a290
  • Loading branch information
gorhill committed Jan 21, 2024
1 parent 77dc333 commit 49dd68e
Showing 1 changed file with 10 additions and 16 deletions.
26 changes: 10 additions & 16 deletions src/js/scriptlet-filtering-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(', ')})`;
};

/******************************************************************************/
Expand Down

0 comments on commit 49dd68e

Please sign in to comment.