Skip to content

Commit d67340f

Browse files
committed
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.
1 parent 68ae847 commit d67340f

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

src/js/scriptlet-filtering.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -230,17 +230,26 @@
230230
toInject.set(rawToken, content);
231231
};
232232

233-
// Fill template placeholders. Return falsy if:
234-
// - At least one argument contains anything else than /\w/ and `.`
235-
233+
// Fill-in scriptlet argument placeholders.
236234
const patchScriptlet = function(content, args) {
235+
let s = args;
236+
let len = s.length;
237+
let beg = 0, pos = 0;
237238
let i = 1;
238-
while ( args !== '' ) {
239-
let pos = args.indexOf(',');
240-
if ( pos === -1 ) { pos = args.length; }
241-
const arg = args.slice(0, pos).trim().replace(reEscapeScriptArg, '\\$&');
242-
content = content.replace(`{{${i}}}`, arg);
243-
args = args.slice(pos + 1).trim();
239+
while ( beg < len ) {
240+
pos = s.indexOf(',', pos);
241+
// Escaped comma? If so, skip.
242+
if ( pos > 0 && s.charCodeAt(pos - 1) === 0x5C /* '\\' */ ) {
243+
s = s.slice(0, pos - 1) + s.slice(pos);
244+
len -= 1;
245+
continue;
246+
}
247+
if ( pos === -1 ) { pos = len; }
248+
content = content.replace(
249+
`{{${i}}}`,
250+
s.slice(beg, pos).trim().replace(reEscapeScriptArg, '\\$&')
251+
);
252+
beg = pos = pos + 1;
244253
i++;
245254
}
246255
return content;

0 commit comments

Comments
 (0)