From d67340f14db6ce5b446ef0ff4586b5e4d31f1086 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Wed, 14 Aug 2019 11:02:01 -0400 Subject: [PATCH] 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. --- src/js/scriptlet-filtering.js | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/js/scriptlet-filtering.js b/src/js/scriptlet-filtering.js index be98e80b6..c52af9b50 100644 --- a/src/js/scriptlet-filtering.js +++ b/src/js/scriptlet-filtering.js @@ -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;