From 190611a0799fc1e07878bb62c2bbeacf91e7406a Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Mon, 31 Aug 2020 10:51:30 +0200 Subject: [PATCH] Detect comments in jinja templates (#39496) --- homeassistant/helpers/template.py | 2 +- tests/helpers/test_template.py | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/homeassistant/helpers/template.py b/homeassistant/helpers/template.py index fddd32c8760..b9dc854cd2b 100644 --- a/homeassistant/helpers/template.py +++ b/homeassistant/helpers/template.py @@ -52,7 +52,7 @@ _RE_GET_ENTITIES = re.compile( re.I | re.M, ) -_RE_JINJA_DELIMITERS = re.compile(r"\{%|\{\{") +_RE_JINJA_DELIMITERS = re.compile(r"\{%|\{\{|\{#") _RESERVED_NAMES = {"contextfunction", "evalcontextfunction", "environmentfunction"} diff --git a/tests/helpers/test_template.py b/tests/helpers/test_template.py index 3b293c106f9..b0643d1addf 100644 --- a/tests/helpers/test_template.py +++ b/tests/helpers/test_template.py @@ -2279,3 +2279,12 @@ async def test_cache_garbage_collection(): assert not template._NO_HASS_ENV.template_cache.get( template_string ) # pylint: disable=protected-access + + +def test_is_template_string(): + """Test is template string.""" + assert template.is_template_string("{{ x }}") is True + assert template.is_template_string("{% if x == 2 %}1{% else %}0{%end if %}") is True + assert template.is_template_string("{# a comment #} Hey") is True + assert template.is_template_string("1") is False + assert template.is_template_string("Some Text") is False