Add ability to escape comma in a scriptlet's list of arguments

An instance of `\,` will not be interpreted as an arguments
separator -- thus allowing the use of commas inside
argument values.
This commit is contained in:
Raymond Hill 2019-08-14 11:02:01 -04:00
parent 68ae847ba3
commit d67340f14d
No known key found for this signature in database
GPG key ID: 25E1490B761470C2

View file

@ -230,17 +230,26 @@
toInject.set(rawToken, content);
};
// Fill template placeholders. Return falsy if:
// - At least one argument contains anything else than /\w/ and `.`
// Fill-in scriptlet argument placeholders.
const patchScriptlet = function(content, args) {
let s = args;
let len = s.length;
let beg = 0, pos = 0;
let i = 1;
while ( args !== '' ) {
let pos = args.indexOf(',');
if ( pos === -1 ) { pos = args.length; }
const arg = args.slice(0, pos).trim().replace(reEscapeScriptArg, '\\$&');
content = content.replace(`{{${i}}}`, arg);
args = args.slice(pos + 1).trim();
while ( beg < len ) {
pos = s.indexOf(',', pos);
// Escaped comma? If so, skip.
if ( pos > 0 && s.charCodeAt(pos - 1) === 0x5C /* '\\' */ ) {
s = s.slice(0, pos - 1) + s.slice(pos);
len -= 1;
continue;
}
if ( pos === -1 ) { pos = len; }
content = content.replace(
`{{${i}}}`,
s.slice(beg, pos).trim().replace(reEscapeScriptArg, '\\$&')
);
beg = pos = pos + 1;
i++;
}
return content;