From a8392df6b8f1a421fcec2f2b215e7a2f3e3a9a2f Mon Sep 17 00:00:00 2001 From: Ning Sun Date: Sun, 14 Mar 2021 14:25:13 +0800 Subject: [PATCH 1/3] (test) add test to reproduce #422 --- tests/escape.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/tests/escape.rs b/tests/escape.rs index 360a74fc5..563ab76b9 100644 --- a/tests/escape.rs +++ b/tests/escape.rs @@ -3,7 +3,7 @@ extern crate handlebars; #[macro_use] extern crate serde_json; -use handlebars::Handlebars; +use handlebars::{handlebars_helper, Handlebars}; #[test] fn test_escape_216() { @@ -19,3 +19,25 @@ fn test_escape_216() { r"\\\\ foo bar foobar foo#bar foo//bar foo\foo foo\\\foo\\foo \\foo {{FOO}} {{FOO}}" ); } + +#[test] +fn test_string_no_escape_422() { + let mut hbs = Handlebars::new(); + + handlebars_helper!(replace: |input: str, from: str, to: str| { + input.replace(from, to) + }); + hbs.register_helper("replace", Box::new(replace)); + + 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() + ); +} From de39b9773d170e35807bd81cdb78542b30cf5274 Mon Sep 17 00:00:00 2001 From: Ning Sun Date: Fri, 26 Mar 2021 23:05:30 +0800 Subject: [PATCH 2/3] (fix) json string literal rule --- src/grammar.pest | 8 +++++++- src/grammar.rs | 3 +-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/grammar.pest b/src/grammar.pest index 9f87fae94..6ed45e53f 100644 --- a/src/grammar.pest +++ b/src/grammar.pest @@ -15,7 +15,13 @@ literal = { string_literal | null_literal = @{ "null" ~ !symbol_char } boolean_literal = @{ ("true"|"false") ~ !symbol_char } number_literal = @{ "-"? ~ ASCII_DIGIT+ ~ "."? ~ ASCII_DIGIT* ~ ("E" ~ "-"? ~ ASCII_DIGIT+)? } -string_literal = @{ ("\"" ~ (!"\"" ~ ("\\\"" | ANY))* ~ "\"") | ("'" ~ (!"'" ~ ("\\'" | ANY))* ~ "'") } +json_char = { + !("\"" | "\\") ~ ANY + | "\\" ~ ("\"" | "\\" | "/" | "b" | "f" | "n" | "r" | "t") + | "\\" ~ ("u" ~ ASCII_HEX_DIGIT{4}) +} +string_inner = @{ json_char* } +string_literal = ${ "\"" ~ string_inner ~ "\"" } array_literal = { "[" ~ literal? ~ ("," ~ literal)* ~ "]" } object_literal = { "{" ~ (string_literal ~ ":" ~ literal)? ~ ("," ~ string_literal ~ ":" ~ literal)* ~ "}" } diff --git a/src/grammar.rs b/src/grammar.rs index 6f7b23803..6be52a6cc 100644 --- a/src/grammar.rs +++ b/src/grammar.rs @@ -125,8 +125,6 @@ mod test { let s = vec![ "\"json string\"", "\"quot: \\\"\"", - "'json string'", - "'quot: \\''", "[]", "[\"hello\"]", "[1,2,3,4,true]", @@ -174,6 +172,7 @@ mod test { "{{exp 1}}", "{{exp \"literal\"}}", "{{exp \"literal with space\"}}", + r#"{{exp "literal with escape \\\\"}}"#, "{{exp ref}}", "{{exp (sub)}}", "{{exp (sub 123)}}", From dda5576e1fe01252cab519ceec95fa98fc43cbfe Mon Sep 17 00:00:00 2001 From: Ning Sun Date: Fri, 26 Mar 2021 23:24:16 +0800 Subject: [PATCH 3/3] (fix) allow upper_case_acronyms --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 1bf311666..4ba1a3dea 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -358,7 +358,7 @@ //! //! -#![allow(dead_code)] +#![allow(dead_code, clippy::upper_case_acronyms)] #![warn(rust_2018_idioms)] #![recursion_limit = "200"]