From 08cf758da7e99d7a59b80d46e6d745daabd5b7d8 Mon Sep 17 00:00:00 2001 From: John Chesley Date: Mon, 17 Aug 2015 10:28:17 -0400 Subject: [PATCH 1/3] add failing test case for dollar sign bug --- tests/test_simple_plustache.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/test_simple_plustache.cpp b/tests/test_simple_plustache.cpp index 585e59d..39f21bf 100644 --- a/tests/test_simple_plustache.cpp +++ b/tests/test_simple_plustache.cpp @@ -15,6 +15,8 @@ class SimpleTest : public ::testing::Test std::string template_string; std::string result_notfound; std::string notfound; + std::string result_dollars; + std::string dollars; std::map ctx; std::string file; @@ -30,6 +32,7 @@ class SimpleTest : public ::testing::Test { template_string = "text {{title}} text"; notfound = "text {{fitle}} text"; + dollars = "text {{dollars}}"; file = "multiple.mustache"; std::ofstream myfile; @@ -38,10 +41,13 @@ class SimpleTest : public ::testing::Test myfile.close(); ctx["title"] = "replaced"; + ctx["dollars"] = "$0"; + Plustache::template_t t; result_string = t.render(template_string, ctx); result_file = t.render(file, ctx); result_notfound = t.render(notfound, ctx); + result_dollars = t.render(dollars, ctx); } virtual void TearDown() @@ -68,3 +74,9 @@ TEST_F(SimpleTest, TestSimpleNotFoundMustacheFromString) const std::string expected = "text text"; EXPECT_EQ(expected, result_notfound); } + +TEST_F(SimpleTest, TestDollarSignsInValue) +{ + const std::string expected = "text $0"; + EXPECT_EQ(expected, result_dollars); +} From f8a104be2fe2863ea1e29dc85e550ae3680e1f33 Mon Sep 17 00:00:00 2001 From: John Chesley Date: Mon, 17 Aug 2015 10:38:45 -0400 Subject: [PATCH 2/3] add failing test case for disappearing parens (#17) --- tests/test_simple_plustache.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/test_simple_plustache.cpp b/tests/test_simple_plustache.cpp index 39f21bf..e2ae606 100644 --- a/tests/test_simple_plustache.cpp +++ b/tests/test_simple_plustache.cpp @@ -17,6 +17,8 @@ class SimpleTest : public ::testing::Test std::string notfound; std::string result_dollars; std::string dollars; + std::string result_parens; + std::string parens; std::map ctx; std::string file; @@ -33,6 +35,7 @@ class SimpleTest : public ::testing::Test template_string = "text {{title}} text"; notfound = "text {{fitle}} text"; dollars = "text {{dollars}}"; + parens = "text {{ open_paren }}inside{{ close_paren }}"; file = "multiple.mustache"; std::ofstream myfile; @@ -42,12 +45,15 @@ class SimpleTest : public ::testing::Test ctx["title"] = "replaced"; ctx["dollars"] = "$0"; + ctx["open_paren"] = "("; + ctx["close_paren"] = ")"; Plustache::template_t t; result_string = t.render(template_string, ctx); result_file = t.render(file, ctx); result_notfound = t.render(notfound, ctx); result_dollars = t.render(dollars, ctx); + result_parens = t.render(parens, ctx); } virtual void TearDown() @@ -80,3 +86,9 @@ TEST_F(SimpleTest, TestDollarSignsInValue) const std::string expected = "text $0"; EXPECT_EQ(expected, result_dollars); } + +TEST_F(SimpleTest, TestParensInValue) +{ + const std::string expected = "text (inside)"; + EXPECT_EQ(expected, result_parens); +} From bb39b48698b7c7b1785b8db8b4d0b31ff08b3d29 Mon Sep 17 00:00:00 2001 From: John Chesley Date: Tue, 18 Aug 2015 10:43:46 -0400 Subject: [PATCH 3/3] use boost format_literal flag to suppress regex_replace handling special characters --- src/template.cpp | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/src/template.cpp b/src/template.cpp index b2f93d0..b740372 100644 --- a/src/template.cpp +++ b/src/template.cpp @@ -104,17 +104,7 @@ std::string template_t::render_tags(const std::string& tmplate, { try { - // get value - std::string s = ctx.get(key)[0][key]; - // escape backslash in std::string - const std::string f = "\\"; - size_t found = s.find(f); - while(found != std::string::npos) - { - s.replace(found,f.length(),"\\\\"); - found = s.find(f, found+2); - } - repl.assign(s); + repl.assign(ctx.get(key)[0][key]); } catch(int i) { repl.assign(""); } } @@ -140,8 +130,8 @@ std::string template_t::render_tags(const std::string& tmplate, } // replace - ret += regex_replace(text, tag, repl, - boost::match_default | boost::format_all); + ret += boost::regex_replace(text, tag, repl, + boost::match_default | boost::format_all | boost::regex_constants::format_literal); // change delimiter after was removed if (modifier == "=") { @@ -250,7 +240,7 @@ std::string template_t::render_sections(const std::string& tmplate, } else repl.assign(""); ret += boost::regex_replace(text, section, repl, - boost::match_default | boost::format_all); + boost::match_default | boost::format_all | boost::regex_constants::format_literal); rest.assign(matches[0].second, end); start = matches[0].second; } @@ -335,7 +325,7 @@ std::string template_t::html_escape(const std::string& s) std::string repl; repl = escape_lut[key]; ret += boost::regex_replace(text, escape_chars, repl, - boost::match_default | boost::format_all); + boost::match_default | boost::format_all | boost::regex_constants::format_literal); rest.assign(matches[0].second, end); start = matches[0].second; }