Skip to content

Commit

Permalink
Merge pull request #577 from nickvollmar/single-quote-parameters
Browse files Browse the repository at this point in the history
Fix parameter passing for single-quoted string literals
  • Loading branch information
sunng87 authored May 10, 2023
2 parents 91abf43 + 21341df commit 4cb081e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
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>"
);
}

0 comments on commit 4cb081e

Please sign in to comment.