Skip to content
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

Invalid syntax if (even an escaped) \ is the last character in quotes #422

Closed
YannikSc opened this issue Mar 9, 2021 · 2 comments · Fixed by #423
Closed

Invalid syntax if (even an escaped) \ is the last character in quotes #422

YannikSc opened this issue Mar 9, 2021 · 2 comments · Fixed by #423
Assignees
Labels

Comments

@YannikSc
Copy link

YannikSc commented Mar 9, 2021

When defining a string in a template, which contains a \ as last character (even escaped as "\\") the renderer fails with an InvalidSyntax error.

Not sure if this is an intended behaviour, but it definitly feels like a bug and is also not the case in other parsers (like handlebars.js). Sorry if there is already another topic describing this, I have'nt found it.

here is an example which reproduces the bug:

#[macro_use]
extern crate handlebars;

use std::collections::BTreeMap;
use handlebars::Handlebars;

handlebars_helper!(replace: |input: str, from: str, to: str| {
    input.replace(from, to)
});

// Error when having a \ as last character in a string
fn main() {
    let data = BTreeMap::<String, String>::new();
    let mut bars = Handlebars::new();
    bars.register_helper("replace", Box::new(replace));

    // Replace a / with a \

    // Works with "\\ " (if the character, before the quote is not a "\" )
    println!("{}", bars.render_template(r#"{{replace "some/path" "/" "\\ " }}"#, &data).unwrap()); // works
    println!("{}", bars.render_template(r#"{{replace "some/path" "/" "\\something else" }}"#, &data).unwrap()); // works

    // Fails otherwise
    println!("{}", bars.render_template(r#"{{replace "some/path" "/" "\\" }}"#, &data).unwrap()); // fails
    println!("{}", bars.render_template(r#"{{replace "some/path" "/" "something before \\" }}"#, &data).unwrap()); // fails
}
@sunng87
Copy link
Owner

sunng87 commented Mar 10, 2021

These seems to be affected by how we deal with escape in json. It works with the original javascript version. Let me investigate.

@sunng87 sunng87 self-assigned this Mar 10, 2021
@sunng87 sunng87 added the syntax label Mar 10, 2021
sunng87 added a commit that referenced this issue Mar 26, 2021
@sunng87
Copy link
Owner

sunng87 commented Mar 27, 2021

@YannikSc the fix is available in 3.5.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants