Skip to content

Commit

Permalink
Merge pull request #113 from wrabit/issue-111-others
Browse files Browse the repository at this point in the history
fixed unintended literal evaluation of non dynamic properties
  • Loading branch information
wrabit authored Sep 2, 2024
2 parents d5ddf2f + 47df6a0 commit a60ac93
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
1 change: 0 additions & 1 deletion django_cotton/cotton_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from django.template import TemplateDoesNotExist, Origin
from django.utils._os import safe_join
from django.template import Template
from django.conf import settings
from django.apps import apps

from bs4 import BeautifulSoup, MarkupResemblesLocatorWarning
Expand Down
15 changes: 9 additions & 6 deletions django_cotton/templatetags/_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,20 @@ def render(self, context):
if "ctn_template_expression_attrs" in local_named_slots_ctx:
for expression_attr in local_named_slots_ctx["ctn_template_expression_attrs"]:
# Process them like a non-extracted attribute
evaluated = self._process_dynamic_attribute(
local_named_slots_ctx[expression_attr], local_ctx
)
expression_attr = expression_attr.lstrip(":")
attrs[expression_attr] = evaluated
if expression_attr[0] == ":":
evaluated = self._process_dynamic_attribute(
local_named_slots_ctx[expression_attr], local_ctx
)
expression_attr = expression_attr[1:]
attrs[expression_attr] = evaluated
else:
attrs[expression_attr] = local_named_slots_ctx[expression_attr]

attrs_string = " ".join(f"{key}={ensure_quoted(value)}" for key, value in attrs.items())
local_ctx["attrs"] = mark_safe(attrs_string)
local_ctx["attrs_dict"] = attrs

# Store attr names in a callable format, i.e. 'x-init' will be accessible by {{ x_init }}
# Ensure attributes are accessible, eg. 'x-init' -> {{ x_init }}
attrs = {key.replace("-", "_"): value for key, value in attrs.items()}
local_ctx.update(attrs)

Expand Down
28 changes: 28 additions & 0 deletions django_cotton/tests/test_cotton.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,34 @@ def test_boolean_attributes(self):
response = self.client.get("/view/")
self.assertContains(response, "It's True")

def test_attributes_without_colons_are_not_evaluated(self):
self.create_template(
"cotton/empty_variables.html",
"""
{% if something == "1,234" %}
All good
{% endif %}
{% if something == "(1, 234)" %}
"1,234" was evaluated as a tuple
{% endif %}
""",
)

self.create_template(
"empty_variables_view.html",
"""
<c-empty-variables something="{{ something }}" />
""",
"view/",
context={"something": "1,234"},
)

# Override URLconf
with self.settings(ROOT_URLCONF=self.get_url_conf()):
response = self.client.get("/view/")
self.assertContains(response, "All good")


class CottonTestCase(TestCase):
def test_parent_component_is_rendered(self):
Expand Down

0 comments on commit a60ac93

Please sign in to comment.