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

Fix parameter passing for single-quoted string literals #577

Merged
merged 4 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,11 +288,26 @@ impl Template {
Parameter::Path(Path::new(param_span.as_str(), path_segs))
}
Rule::literal => {
let s = param_span.as_str();
if let Ok(json) = Json::from_str(s) {
Parameter::Literal(json)
} else {
Parameter::Name(s.to_owned())
// Parse the parameter as a JSON literal
let param_literal = it.next().unwrap();
match param_literal.as_rule() {
Rule::string_literal
if it.peek().unwrap().as_rule() == Rule::string_inner_single_quote =>
{
// ...unless the parameter is a single-quoted string.
// In that case, transform it to a double-quoted string
// and then parse it as a JSON literal.
let string_inner_single_quote = it.next().unwrap();
let double_quoted = format!(
"\"{}\"",
string_inner_single_quote
.as_str()
.replace("\\'", "'")
.replace('"', "\\\"")
);
Parameter::Literal(Json::from_str(&double_quoted).unwrap())
Copy link
Owner

Choose a reason for hiding this comment

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

is it 100% safe to do unwrap here? Otherwise we better raise an TemplateError here

}
_ => Parameter::Literal(Json::from_str(param_span.as_str()).unwrap()),
}
}
Rule::subexpression => {
Expand Down
25 changes: 25 additions & 0 deletions tests/escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,44 @@ fn test_string_no_escape_422() {
handlebars_helper!(replace: |input: str, from: str, to: str| {
input.replace(from, to)
});
handlebars_helper!(echo: |input: str| {
input
});
hbs.register_helper("replace", Box::new(replace));
hbs.register_helper("echo", Box::new(echo));

assert_eq!(
r#"some\ path"#,
hbs.render_template(r#"{{replace "some/path" "/" "\\ " }}"#, &())
.unwrap()
);
assert_eq!(
r#"some\ path"#,
hbs.render_template(r#"{{replace 'some/path' '/' '\\ ' }}"#, &())
.unwrap()
);

assert_eq!(
r#"some\path"#,
hbs.render_template(r#"{{replace "some/path" "/" "\\" }}"#, &())
.unwrap()
);
assert_eq!(
r#"some\path"#,
hbs.render_template(r#"{{replace 'some/path' '/' '\\' }}"#, &())
.unwrap()
);

assert_eq!(
r#"double-quoted \ 'with' "nesting""#,
hbs.render_template(r#"{{echo "double-quoted \\ 'with' \"nesting\""}}"#, &())
.unwrap()
);
assert_eq!(
r#"single-quoted \ 'with' "nesting""#,
hbs.render_template(r#"{{echo 'single-quoted \\ \'with\' "nesting"'}}"#, &())
.unwrap()
);
}

#[test]
Expand Down
7 changes: 6 additions & 1 deletion tests/helper_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,10 @@ fn test_macro_helper() {
)
.unwrap(),
"false"
)
);

assert_eq!(
hbs.render_template("{{tag 'html'}}", &()).unwrap(),
"<html>"
);
}