fixes first issue

This commit is contained in:
Will Abbott 2025-06-29 12:04:22 +01:00
parent a703ee3558
commit 3ec0fbbac3
2 changed files with 64 additions and 6 deletions

View file

@ -22,8 +22,10 @@ class CottonVarsNode(Node):
if cotton_data["stack"]:
current_component = cotton_data["stack"][-1]
attrs = current_component["attrs"]
slots = current_component.get("slots", {})
else:
attrs = Attrs({})
slots = {}
vars = {}
@ -31,13 +33,15 @@ class CottonVarsNode(Node):
key_to_exclude = key
if key not in attrs.exclude_unprocessable():
if key.startswith(":"):
try:
key_to_exclude = key[1:]
vars[key_to_exclude] = DynamicAttr(value, is_cvar=True).resolve(context)
except UnprocessableDynamicAttr:
pass
key_to_exclude = key[1:]
if key_to_exclude not in slots:
try:
vars[key_to_exclude] = DynamicAttr(value, is_cvar=True).resolve(context)
except UnprocessableDynamicAttr:
pass
else:
attrs[key] = value
if key not in slots:
attrs[key] = value
attrs.exclude_from_string_output(key_to_exclude)
# Process cvars without values

View file

@ -444,3 +444,57 @@ class CvarTests(CottonTestCase):
self.assertTrue("Attrs: ''" in content)
self.assertTrue("Action: 'something completely different'" in content)
def test_dynamic_cvars_can_be_overridden_by_named_slots(self):
self.create_template(
"cotton/cvars_named_slots.html",
"""
<c-vars :action="{'do': 'it'}" />
Action: '{{ action }}'
""",
)
# View template that uses the proxy component
self.create_template(
"cvars_named_slots_view.html",
"""
<c-cvars-named-slots>
<c-slot name="action">overridden action</c-slot>
</c-cvars-named-slots>
""",
"view/",
)
# Override URLconf
with self.settings(ROOT_URLCONF=self.url_conf()):
response = self.client.get("/view/")
content = response.content.decode().strip()
self.assertTrue("Action: 'overridden action'" in content)
def test_dynamic_cvars_are_not_present_in_attrs_string(self):
self.create_template(
"cotton/cvars_dynamic_attrs.html",
"""
<c-vars :disabled />
Attrs: '{{ attrs }}'
""",
)
# View template that uses the proxy component
self.create_template(
"cvars_dynamic_attrs_view.html",
"""
<c-cvars-dynamic-attrs />
""",
"view/",
)
# Override URLconf
with self.settings(ROOT_URLCONF=self.url_conf()):
response = self.client.get("/view/")
content = response.content.decode().strip()
self.assertTrue("Attrs: ''" in content)