mirror of
https://github.com/django-components/django-components.git
synced 2025-08-29 18:34:03 +00:00
fix: typo in html_attrs defaults dict fetching (#513)
Co-authored-by: Juro Oravec <juraj.oravec.josefson@gmail.com>
This commit is contained in:
parent
95f6554f4c
commit
0101f6dae6
2 changed files with 10 additions and 8 deletions
|
@ -11,6 +11,9 @@ from django.utils.safestring import SafeString, mark_safe
|
||||||
|
|
||||||
from django_components.template_parser import process_aggregate_kwargs
|
from django_components.template_parser import process_aggregate_kwargs
|
||||||
|
|
||||||
|
HTML_ATTRS_DEFAULTS_KEY = "defaults"
|
||||||
|
HTML_ATTRS_ATTRS_KEY = "attrs"
|
||||||
|
|
||||||
|
|
||||||
class HtmlAttrsNode(Node):
|
class HtmlAttrsNode(Node):
|
||||||
def __init__(
|
def __init__(
|
||||||
|
@ -30,12 +33,12 @@ class HtmlAttrsNode(Node):
|
||||||
# Resolve kwargs, while also extracting attrs and defaults keys
|
# Resolve kwargs, while also extracting attrs and defaults keys
|
||||||
for key, value in self.kwargs:
|
for key, value in self.kwargs:
|
||||||
resolved_value = value.resolve(context)
|
resolved_value = value.resolve(context)
|
||||||
if key.startswith("attrs:") or key.startswith("defaults:"):
|
if key.startswith(f"{HTML_ATTRS_ATTRS_KEY}:") or key.startswith(f"{HTML_ATTRS_DEFAULTS_KEY}:"):
|
||||||
attrs_and_defaults_from_kwargs[key] = resolved_value
|
attrs_and_defaults_from_kwargs[key] = resolved_value
|
||||||
continue
|
continue
|
||||||
# NOTE: These were already extracted into separate variables, so
|
# NOTE: These were already extracted into separate variables, so
|
||||||
# ignore them here.
|
# ignore them here.
|
||||||
elif key == "attrs" or key == "defaults":
|
elif key == HTML_ATTRS_ATTRS_KEY or key == HTML_ATTRS_DEFAULTS_KEY:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
append_attrs.append((key, resolved_value))
|
append_attrs.append((key, resolved_value))
|
||||||
|
@ -46,16 +49,16 @@ class HtmlAttrsNode(Node):
|
||||||
# So by assigning the `attrs` and `defaults` keys, users are forced to use only
|
# So by assigning the `attrs` and `defaults` keys, users are forced to use only
|
||||||
# one approach or the other, but not both simultaneously.
|
# one approach or the other, but not both simultaneously.
|
||||||
if self.attributes:
|
if self.attributes:
|
||||||
attrs_and_defaults_from_kwargs["attrs"] = self.attributes.resolve(context)
|
attrs_and_defaults_from_kwargs[HTML_ATTRS_ATTRS_KEY] = self.attributes.resolve(context)
|
||||||
if self.default_attrs:
|
if self.default_attrs:
|
||||||
attrs_and_defaults_from_kwargs["defaults"] = self.default_attrs.resolve(context)
|
attrs_and_defaults_from_kwargs[HTML_ATTRS_DEFAULTS_KEY] = self.default_attrs.resolve(context)
|
||||||
|
|
||||||
# Turn `{"attrs:blabla": 1}` into `{"attrs": {"blabla": 1}}`
|
# Turn `{"attrs:blabla": 1}` into `{"attrs": {"blabla": 1}}`
|
||||||
attrs_and_defaults_from_kwargs = process_aggregate_kwargs(attrs_and_defaults_from_kwargs)
|
attrs_and_defaults_from_kwargs = process_aggregate_kwargs(attrs_and_defaults_from_kwargs)
|
||||||
|
|
||||||
# NOTE: We want to allow to use `html_attrs` even without `attrs` or `defaults` params
|
# NOTE: We want to allow to use `html_attrs` even without `attrs` or `defaults` params
|
||||||
attrs = attrs_and_defaults_from_kwargs.get("attrs", {})
|
attrs = attrs_and_defaults_from_kwargs.get(HTML_ATTRS_ATTRS_KEY, {})
|
||||||
default_attrs = attrs_and_defaults_from_kwargs.get("defualts", {})
|
default_attrs = attrs_and_defaults_from_kwargs.get(HTML_ATTRS_DEFAULTS_KEY, {})
|
||||||
|
|
||||||
final_attrs = {**default_attrs, **attrs}
|
final_attrs = {**default_attrs, **attrs}
|
||||||
final_attrs = append_attributes(*final_attrs.items(), *append_attrs)
|
final_attrs = append_attributes(*final_attrs.items(), *append_attrs)
|
||||||
|
|
|
@ -274,12 +274,11 @@ class HtmlAttrsTests(BaseTestCase):
|
||||||
self.assertHTMLEqual(
|
self.assertHTMLEqual(
|
||||||
rendered,
|
rendered,
|
||||||
"""
|
"""
|
||||||
<div class="added_class another-class" data-id=123>
|
<div class="added_class another-class override-me" data-id=123>
|
||||||
content
|
content
|
||||||
</div>
|
</div>
|
||||||
""",
|
""",
|
||||||
)
|
)
|
||||||
self.assertNotIn("override-me", rendered)
|
|
||||||
|
|
||||||
def test_tag_no_defaults(self):
|
def test_tag_no_defaults(self):
|
||||||
@component.register("test")
|
@component.register("test")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue