Skip to content

Commit

Permalink
fix: incorrect name for default slot in is_filled dict (#482)
Browse files Browse the repository at this point in the history
  • Loading branch information
JuroOravec authored May 7, 2024
1 parent dee1524 commit dd6540c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 17 deletions.
12 changes: 11 additions & 1 deletion src/django_components/slots.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,17 @@ def _resolve_default_slot(

# Here we've identified which slot the default/implicit fill belongs to
if default_fill:
named_fills[slot.name] = default_fill._replace(name=slot.name)
# NOTE: We recreate new instance, passing all fields, instead of using
# `NamedTuple._replace`, because `_replace` is not typed.
named_fills[slot.name] = SlotFill(
is_filled=default_fill.is_filled,
nodelist=default_fill.nodelist,
context_data=default_fill.context_data,
alias=default_fill.alias,
# Updated fields
name=slot.name,
escaped_name=_escape_slot_name(slot.name),
)

# Check: Only component templates that include a 'default' slot
# can be invoked with implicit filling.
Expand Down
51 changes: 35 additions & 16 deletions tests/test_templatetags.py
Original file line number Diff line number Diff line change
Expand Up @@ -1157,7 +1157,7 @@ class ComplexChildComponent(component.Component):
class ComplexParentComponent(component.Component):
template: types.django_html = """
{% load component_tags %}
ITEMS: {{ items }}
ITEMS: {{ items|safe }}
{% for item in items %}
<li>
{% component "complex_child" %}
Expand Down Expand Up @@ -1296,7 +1296,7 @@ def test_component_nesting_deep_slot_inside_component_fill__isolated(self):
items = [{"value": 1}, {"value": 2}, {"value": 3}]
rendered = template.render(Context({"items": items}))
expected = """
ITEMS: [{&#x27;value&#x27;: 1}, {&#x27;value&#x27;: 2}, {&#x27;value&#x27;: 3}]
ITEMS: [{'value': 1}, {'value': 2}, {'value': 3}]
<li>
<div> 1 </div>
</li>
Expand Down Expand Up @@ -1522,12 +1522,12 @@ class IsFilledVarsComponent(component.Component):
template: types.django_html = """
{% load component_tags %}
<div class="frontmatter-component">
{% slot "title" %}{% endslot %}
{% slot "title" default %}{% endslot %}
{% slot "my_title" %}{% endslot %}
{% slot "my title 1" %}{% endslot %}
{% slot "my-title-2" %}{% endslot %}
{% slot "escape this: #$%^*()" %}{% endslot %}
{{ component_vars.is_filled }}
{{ component_vars.is_filled|safe }}
</div>
"""

Expand All @@ -1546,14 +1546,33 @@ def test_is_filled_vars(self):
{% endcomponent %}
"""
rendered = Template(template).render(Context())
# NOTE: `&#x27;` are escaped quotes
expected = """
<div class="frontmatter-component">
{&#x27;title&#x27;: True,
&#x27;my_title&#x27;: False,
&#x27;my_title_1&#x27;: False,
&#x27;my_title_2&#x27;: True,
&#x27;escape_this_________&#x27;: True}
{'title': True,
'my_title': False,
'my_title_1': False,
'my_title_2': True,
'escape_this_________': True}
</div>
"""
self.assertHTMLEqual(rendered, expected)

def test_is_filled_vars_default(self):
template: types.django_html = """
{% load component_tags %}
{% component "is_filled_vars" %}
bla bla
{% endcomponent %}
"""
rendered = Template(template).render(Context())
expected = """
<div class="frontmatter-component">
bla bla
{'title': True,
'my_title': False,
'my_title_1': False,
'my_title_2': False,
'escape_this_________': False}
</div>
"""
self.assertHTMLEqual(rendered, expected)
Expand Down Expand Up @@ -2337,10 +2356,10 @@ def test_inner_slot_iteration_nested_with_slot_default_and_outer_scope_variable_
{% load component_tags %}
{% component "slot_in_a_loop" objects=objects %}
{% fill "slot_inner" %}
{{ outer_scope_variable_1 }}
{{ outer_scope_variable_1|safe }}
{% component "slot_in_a_loop" objects=objects %}
{% fill "slot_inner" as "super_slot_inner" %}
{{ outer_scope_variable_2 }}
{{ outer_scope_variable_2|safe }}
{{ super_slot_inner.default }}
{% endfill %}
{% endcomponent %}
Expand All @@ -2363,13 +2382,13 @@ def test_inner_slot_iteration_nested_with_slot_default_and_outer_scope_variable_
"""
OUTER_SCOPE_VARIABLE1
OUTER_SCOPE_VARIABLE2
{&#x27;inner&#x27;: [&#x27;ITER1_OBJ1&#x27;, &#x27;ITER1_OBJ2&#x27;]} default
{'inner': ['ITER1_OBJ1', 'ITER1_OBJ2']} default
OUTER_SCOPE_VARIABLE2
{&#x27;inner&#x27;: [&#x27;ITER2_OBJ1&#x27;, &#x27;ITER2_OBJ2&#x27;]} default
{'inner': ['ITER2_OBJ1', 'ITER2_OBJ2']} default
OUTER_SCOPE_VARIABLE1
OUTER_SCOPE_VARIABLE2
{&#x27;inner&#x27;: [&#x27;ITER1_OBJ1&#x27;, &#x27;ITER1_OBJ2&#x27;]} default
{'inner': ['ITER1_OBJ1', 'ITER1_OBJ2']} default
OUTER_SCOPE_VARIABLE2
{&#x27;inner&#x27;: [&#x27;ITER2_OBJ1&#x27;, &#x27;ITER2_OBJ2&#x27;]} default
{'inner': ['ITER2_OBJ1', 'ITER2_OBJ2']} default
""",
)

0 comments on commit dd6540c

Please sign in to comment.