1
0
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:
Raymond Hill 2024-01-21 09:26:17 -05:00
parent 77dc3339ac
commit 49dd68ef3d
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2

View File

@ -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(', ')})`;
};
/******************************************************************************/