mirror of
https://github.com/gorhill/uBlock.git
synced 2024-10-06 09:37:12 +02:00
Code review recent commit re. quoting parameters
Related commit:
fa3a290ad4
This commit is contained in:
parent
77dc3339ac
commit
49dd68ef3d
@ -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(', ')})`;
|
||||
};
|
||||
|
||||
/******************************************************************************/
|
||||
|
Loading…
Reference in New Issue
Block a user