Skip to content

Commit a146f5b

Browse files
committed
Fix lstrip space control behavour
1 parent 540086d commit a146f5b

File tree

3 files changed

+39
-18
lines changed

3 files changed

+39
-18
lines changed

src/template_parser.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -428,20 +428,26 @@ class TemplateParser : public LexerHelper
428428
size_t StripBlockLeft(TextBlockInfo& currentBlockInfo, size_t ctrlCharPos, size_t endOffset)
429429
{
430430
bool doStrip = m_settings.lstripBlocks;
431+
bool doTotalStrip = false;
431432
if (ctrlCharPos < m_template->size())
432433
{
433434
auto ctrlChar = (*m_template)[ctrlCharPos];
434-
doStrip = ctrlChar == '+' ? false : (ctrlChar == '-' ? true : doStrip);
435+
if (ctrlChar == '+')
436+
doStrip = false;
437+
else
438+
doTotalStrip = ctrlChar == '-';
439+
440+
doStrip |= doTotalStrip;
435441
}
436442
if (!doStrip || currentBlockInfo.type != TextBlockType::RawText)
437443
return endOffset;
438444

439445
auto locale = std::locale();
440446
auto& tpl = *m_template;
441-
for (; endOffset > 0; -- endOffset)
447+
for (; endOffset != currentBlockInfo.range.startOffset && endOffset > 0; -- endOffset)
442448
{
443449
auto ch = tpl[endOffset - 1];
444-
if (!std::isspace(ch, locale) || ch == '\n')
450+
if (!std::isspace(ch, locale) || (!doTotalStrip && ch == '\n'))
445451
break;
446452
}
447453
return endOffset;

test/basic_tests.cpp

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,7 @@ from Parser!)";
127127

128128
std::string result = tpl.RenderAsString(ValuesMap{});
129129
std::cout << result << std::endl;
130-
std::string expectedResult = R"(Hello World
131-
--
130+
std::string expectedResult = R"(Hello World --
132131
from Parser!)";
133132
EXPECT_STREQ(expectedResult.c_str(), result.c_str());
134133
}
@@ -161,15 +160,14 @@ from Parser!)";
161160

162161
std::string result = tpl.RenderAsString(ValuesMap{});
163162
std::cout << result << std::endl;
164-
std::string expectedResult = R"(Hello World
165-
--<
163+
std::string expectedResult = R"(Hello World --<
166164
from Parser!)";
167165
EXPECT_STREQ(expectedResult.c_str(), result.c_str());
168166
}
169167

170168
TEST(BasicTests, StripLSpaces_4)
171169
{
172-
std::string source = R"(Hello World
170+
std::string source = "Hello World\t\t \t" R"(
173171
{%- set delim = ' --' %} {{+delim}}<
174172
from Parser!)";
175173

@@ -178,25 +176,22 @@ from Parser!)";
178176

179177
std::string result = tpl.RenderAsString(ValuesMap{});
180178
std::cout << result << std::endl;
181-
std::string expectedResult = R"(Hello World
182-
--<
179+
std::string expectedResult = R"(Hello World --<
183180
from Parser!)";
184181
EXPECT_STREQ(expectedResult.c_str(), result.c_str());
185182
}
186183

187184
TEST(BasicTests, StripLSpaces_5)
188185
{
189-
std::string source = R"(Hello World
190-
{%- set delim = ' --' %} {{-delim}}<
186+
std::string source = R"(Hello World{%- set delim = ' --' %} {{-delim}}<
191187
from Parser!)";
192188

193189
Template tpl;
194190
ASSERT_TRUE(tpl.Load(source));
195191

196192
std::string result = tpl.RenderAsString(ValuesMap{});
197193
std::cout << result << std::endl;
198-
std::string expectedResult = R"(Hello World
199-
--<
194+
std::string expectedResult = R"(Hello World --<
200195
from Parser!)";
201196
EXPECT_STREQ(expectedResult.c_str(), result.c_str());
202197
}
@@ -229,8 +224,7 @@ from Parser!)";
229224

230225
std::string result = tpl.RenderAsString(ValuesMap{});
231226
std::cout << result << std::endl;
232-
std::string expectedResult = R"(Hello World
233-
--
227+
std::string expectedResult = R"(Hello World --
234228
from Parser!)";
235229
EXPECT_STREQ(expectedResult.c_str(), result.c_str());
236230
}
@@ -246,8 +240,7 @@ from Parser!)";
246240

247241
std::string result = tpl.RenderAsString(ValuesMap{});
248242
std::cout << result << std::endl;
249-
std::string expectedResult = R"(Hello World
250-
--
243+
std::string expectedResult = R"(Hello World --
251244
from Parser!)";
252245
EXPECT_STREQ(expectedResult.c_str(), result.c_str());
253246
}

test/extends_test.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,3 +226,25 @@ Some Stuff
226226
-><-->SCOPEDMACROTEXT<-)";
227227
EXPECT_STREQ(expectedResult.c_str(), result.c_str());
228228
}
229+
230+
TEST_F(ExtendsTest, MacroUsageWithTrimming)
231+
{
232+
m_templateFs->AddFile("base.j2tpl", R"({% macro testMacro(str) -%}
233+
#{{ str | upper }}#
234+
{%- endmacro %}
235+
{%- block body scoped%}{% endblock body%})");
236+
m_templateFs->AddFile("derived.j2tpl",
237+
R"({% extends "base.j2tpl" %}{% block body %}->{{ testMacro('RegularMacroText') }}<-{% endblock %})");
238+
239+
auto baseTpl = m_env.LoadTemplate("base.j2tpl").value();
240+
auto tpl = m_env.LoadTemplate("derived.j2tpl").value();
241+
242+
std::string baseResult = baseTpl.RenderAsString(jinja2::ValuesMap{});
243+
std::cout << baseResult << std::endl;
244+
std::string expectedResult = "";
245+
EXPECT_STREQ(expectedResult.c_str(), baseResult.c_str());
246+
std::string result = tpl.RenderAsString(jinja2::ValuesMap{});
247+
std::cout << result << std::endl;
248+
expectedResult = R"(->#REGULARMACROTEXT#<-)";
249+
EXPECT_STREQ(expectedResult.c_str(), result.c_str());
250+
}

0 commit comments

Comments
 (0)