-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: allow a custom escapeExpression for an env #1523
base: 4.x
Are you sure you want to change the base?
Conversation
Hey @nknapp, just wondering whether you had a chance to look at this? Is it a small enough addition that you would be happy to merge and release? Thanks! |
Sorry, I haven't had a thorough look yet. But I think something similar is part of our plans avoid security vulnerabilities in the future. I would like to defer until then to avoid unnecessary api changes. |
I would like to merge this, but the API is not what I would want to have. I have to do a general API discusssion with @wycats for overriding this like that (helperMissing, blockHelperMissing, escapeExpression). I don't know when I will be able to do that and it might fall in the "we need a spec" category. I would think of an interface similar to Handlebars.registerHook('escapeExpression', (string) => ...);
// or
Handlebars.hooks.escapeExpression((string) => ...) Technically, this is not a language feature, so I'm not sure ich 'we need a spec' applies here, but in the end, that @wycats decision. |
@nknapp I'd be very happy with your proposed change. I made my PR change as small as possible to minimise the impact, but totally understand if you'd like to implement this as a proper supported and documented API. |
In #1559, I added the
I don't know. Does anyone have a good idea, or knows how other frameworks solve this? In any case, it might be helpful to pass the old method into the hook function as well, so that the wrapper can be decide to call it. Or maybe not in the first stage. |
@thekiwi would it be OK for you to have a runtime option to override "escapeExpression"? |
@nknapp It should be, are you able to provide a snippet demonstrating how I would use it? |
It would be something like const template = Handlebars.compile('{{name}}');
const output = template({ name: 'Jonny "the hound" Walker' }, {
hooks: {
escapeExpression: string => string.replace(/(["'\\])/g,'\\$1')
}
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is better than having to override the global utils.escapeExpression.
Till some other mechanism (such as hooks() etc.) is made available, this should be strongly considered for merging.
Ideally I too would like to see a interface that does not require the use of |
We use handlebars.js for both XML templating and JSON templating within the same application. As such, we'd like to be able to override the escapeExpressions function on a single Handlebars environment, rather than globally. This is a small PR that allows us to do that, by first attempting to get the environment's version of
escapeExpression
and falling back to theUtils
version if that is falsey.Usage (or how we might end up using it):
Please let me know if anything is unclear or if you'd like me to make additional changes.
4.x
-branch contains the latest version. Please target that branch in the PR.