diff options
Diffstat (limited to 'vendor/handlebars/src/template.rs')
-rw-r--r-- | vendor/handlebars/src/template.rs | 50 |
1 files changed, 33 insertions, 17 deletions
diff --git a/vendor/handlebars/src/template.rs b/vendor/handlebars/src/template.rs index 617f47711..4bde14859 100644 --- a/vendor/handlebars/src/template.rs +++ b/vendor/handlebars/src/template.rs @@ -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 => { @@ -508,8 +530,8 @@ impl Template { } } - pub(crate) fn compile2<'a>( - source: &'a str, + pub(crate) fn compile2( + source: &str, options: TemplateOptions, ) -> Result<Template, TemplateError> { let mut helper_stack: VecDeque<HelperTemplate> = VecDeque::new(); @@ -1010,7 +1032,7 @@ mod test { let terr = Template::compile(source).unwrap_err(); - assert!(matches!(terr.reason, TemplateErrorReason::InvalidSyntax)); + assert!(matches!(terr.reason(), TemplateErrorReason::InvalidSyntax)); assert_eq!(terr.line_no.unwrap(), 4); assert_eq!(terr.column_no.unwrap(), 5); } @@ -1126,16 +1148,10 @@ mod test { let sources = ["{{invalid", "{{{invalid", "{{invalid}", "{{!hello"]; for s in sources.iter() { let result = Template::compile(s.to_owned()); - if let Err(e) = result { - match e.reason { - TemplateErrorReason::InvalidSyntax => {} - _ => { - panic!("Unexpected error type {}", e); - } - } - } else { - panic!("Undetected error"); - } + assert!(matches!( + *result.unwrap_err().reason(), + TemplateErrorReason::InvalidSyntax + )); } } @@ -1316,6 +1332,6 @@ mod test { let s = "{{#>(X)}}{{/X}}"; let result = Template::compile(s); assert!(result.is_err()); - assert_eq!("decorator \"Subexpression(Subexpression { element: Expression(HelperTemplate { name: Path(Relative(([Named(\\\"X\\\")], \\\"X\\\"))), params: [], hash: {}, block_param: None, template: None, inverse: None, block: false }) })\" was opened, but \"X\" is closing", format!("{}", result.unwrap_err().reason)); + assert_eq!("decorator \"Subexpression(Subexpression { element: Expression(HelperTemplate { name: Path(Relative(([Named(\\\"X\\\")], \\\"X\\\"))), params: [], hash: {}, block_param: None, template: None, inverse: None, block: false }) })\" was opened, but \"X\" is closing", format!("{}", result.unwrap_err().reason())); } } |