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 all 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
28 changes: 25 additions & 3 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,11 +288,33 @@ 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) {
// Parse the parameter as a JSON literal
let param_literal = it.next().unwrap();
let json_result = 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('"', "\\\"")
);
Json::from_str(&double_quoted)
}
_ => Json::from_str(param_span.as_str()),
};
if let Ok(json) = json_result {
Parameter::Literal(json)
} else {
Parameter::Name(s.to_owned())
return Err(TemplateError::of(TemplateErrorReason::InvalidParam(
param_span.as_str().to_owned(),
)));
}
}
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>"
);
}