Skip to content

Commit

Permalink
Add ability to escape comma in a scriptlet's list of arguments
Browse files Browse the repository at this point in the history
An instance of `\,` will not be interpreted as an arguments
separator -- thus allowing the use of commas inside
argument values.
  • Loading branch information
gorhill committed Aug 14, 2019
1 parent 68ae847 commit d67340f
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/js/scriptlet-filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

1 comment on commit d67340f

@uBlock-user
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for adding this 🥇

Please sign in to comment.