Rework slot management to avoid nodelist copying (fixes #64)

Co-authored-by: rbeard0330 <@dul2k3BKW6m>
This commit is contained in:
Emil Stenström 2021-05-30 18:46:11 +02:00 committed by GitHub
parent 5e8ae9d27b
commit a5bd0cf2e3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 40 deletions

View file

@ -0,0 +1,2 @@
{% load component_tags %}
{% include 'slotted_template.html' with context=None only %}

View file

@ -2,9 +2,9 @@ from textwrap import dedent
from django.template import Context, Template, TemplateSyntaxError
from .django_test_setup import * # NOQA
from django_components import component
from .django_test_setup import * # NOQA
from .testutils import Django30CompatibleSimpleTestCase as SimpleTestCase
@ -33,6 +33,11 @@ class SlottedComponent(component.Component):
return "slotted_template.html"
class BrokenComponent(component.Component):
def template(self, context):
return "template_with_illegal_slot.html"
class SlottedComponentWithMissingVariable(component.Component):
def template(self, context):
return "slotted_template_with_missing_variable.html"
@ -589,6 +594,7 @@ class TemplateSyntaxErrorTests(SimpleTestCase):
def setUpClass(cls):
super().setUpClass()
component.registry.register('test', SlottedComponent)
component.registry.register('broken_component', BrokenComponent)
def test_variable_outside_slot_tag_is_error(self):
with self.assertRaises(TemplateSyntaxError):
@ -634,3 +640,25 @@ class TemplateSyntaxErrorTests(SimpleTestCase):
{% slot "header" %}{% endslot %}
"""
)
def test_slot_with_no_parent_is_error(self):
with self.assertRaises(TemplateSyntaxError):
Template(
"""
{% load component_tags %}
{% slot "header" %}contents{% endslot %}
"""
).render(Context({}))
def test_isolated_slot_is_error(self):
with self.assertRaises(TemplateSyntaxError):
Template(
"""
{% load component_tags %}
{% component_block "broken_component" %}
{% slot "header" %}Custom header{% endslot %}
{% slot "main" %}Custom main{% endslot %}
{% slot "footer" %}Custom footer{% endslot %}
{% endcomponent_block %}
"""
).render(Context({}))