10000 Add escape filter (#144) · jinja2cpp/Jinja2Cpp@ac607d2 · GitHub
[go: up one dir, main page]

Skip to content

Commit ac607d2

Browse files
morenolflexferrum
authored andcommitted
Add escape filter (#144)
1 parent 3dc7857 commit ac607d2

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/string_converter_filter.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,31 @@ InternalValue StringConverter::Filter(const InternalValue& baseVal, RenderContex
318318
isFirstChar = false;
319319
});
320320
break;
321+
case EscapeHtmlMode:
322+
result = ApplyStringConverter<GenericStringEncoder>(baseVal, [](auto ch, auto&& fn) mutable {
323+
switch(ch)
324+
{
325+
case '<':
326+
fn('&', 'l', 't', ';');
327+
break;
328+
case '>':
329+
fn('&', 'g', 't', ';');
330+
break;
331+
case '&':
332+
fn('&', 'a', 'm', 'p', ';');
333+
break;
334+
case '\'':
335+
fn('&', '#', '3', '9', ';');
336+
break;
337+
case '\"':
338+
fn('&', '#', '3', '4', ';');
339+
break;
340+
default:
341+
fn(ch);
342+
break;
343+
}
344+
});
345+
break;
321346
default:
322347
break;
323348
}

test/filters_test.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,3 +476,9 @@ INSTANTIATE_TEST_CASE_P(Capitalize, FilterGenericTest, ::testing::Values(
476476
InputOutputPair{"' Hello World' | capitalize | pprint", "' hello world'"},
477477
InputOutputPair{"'Hello123OOO, World!' | capitalize | pprint", "'Hello123ooo, world!'"}
478478
));
479+
480+
INSTANTIATE_TEST_CASE_P(Escape, FilterGenericTest, ::testing::Values(
481+
InputOutputPair{"'' | escape | pprint", "''"},
482+
InputOutputPair{"'abcd&><efgh' | escape | pprint", "'abcd&amp;&gt;&lt;efgh'"},
483+
InputOutputPair{"'\\\"\\'' | escape | pprint", "'&#34;&#39;'"}
484+
));

0 commit comments

Comments
 (0)
0