mirror of
https://github.com/django-components/django-components.git
synced 2025-08-04 06:18:17 +00:00

* feat: skeleton of dependency manager backend (#688) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> * refactor: selectolax update and tests cleanup (#702) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> * refactor: move release notes to own file (#704) * chore: merge changes from master (#705) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Yassin Rakha <yaso2go@gmail.com> Co-authored-by: Emil Stenström <emil@emilstenstrom.se> fix for nested slots (#698) (#699) * refactor: remove joint {% component_dependencies %} tag (#706) Co-authored-by: Emil Stenström <emil@emilstenstrom.se> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> * refactor: split up utils file and move utils to util dir (#707) * docs: Move docs inside src/ to allow imports in python scripts (#708) * refactor: Docs prep 1 (#715) * refactor: Document template tags (#716) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> * refactor: pass slot fills in template via slots param (#719) * chore: Merge master to dev (#729) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Yassin Rakha <yaso2go@gmail.com> Co-authored-by: Emil Stenström <emil@emilstenstrom.se> Co-authored-by: Tom Larsen <larsent@gmail.com> fix for nested slots (#698) (#699) * fix: Do not raise error if multiple slots with same name are flagged as default (#727) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> * refactor: tag formatter - allow fwd slash in end tag (#730) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> * refactor: Use lowercase names for registry settings (#731) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> * docs: add docstrings (#732) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> * feat: define settings as a data class for type hints, intellisense, and docs (#733) * refactor: fix reload-on-change logic, expose autodiscover's dirs-getting logic, rename settings (#734) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> * docs: document settings (#743) * docs: document settings * refactor: fix linter errors * feat: passthrough slots and more (#758) * feat: passthrough slots and more * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * refactor: remove ComponentSlotContext.slots * refactor: update comment * docs: update changelog * refactor: update docstrings * refactor: document and test-cover more changes * refactor: revert fill without name * docs: update README --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> * fix: apostrophes in tags (#765) * refactor: fix merge error - duplicate code --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Emil Stenström <emil@emilstenstrom.se>
137 lines
4.6 KiB
Python
137 lines
4.6 KiB
Python
from django.template import Context, Template
|
|
from django.template.base import Lexer, Parser
|
|
|
|
from django_components import Component, registry, types
|
|
from django_components.expression import (
|
|
is_aggregate_key,
|
|
process_aggregate_kwargs,
|
|
safe_resolve_dict,
|
|
safe_resolve_list,
|
|
)
|
|
from django_components.templatetags.component_tags import TagSpec, _parse_tag
|
|
|
|
from .django_test_setup import setup_test_config
|
|
from .testutils import BaseTestCase, parametrize_context_behavior
|
|
|
|
setup_test_config({"autodiscover": False})
|
|
|
|
|
|
class ParserTest(BaseTestCase):
|
|
def test_parses_args_kwargs(self):
|
|
template_str = "{% component 42 myvar key='val' key2=val2 %}"
|
|
tokens = Lexer(template_str).tokenize()
|
|
parser = Parser(tokens=tokens)
|
|
spec = TagSpec(
|
|
tag="component",
|
|
pos_or_keyword_args=["num", "var"],
|
|
keywordonly_args=True,
|
|
)
|
|
tag = _parse_tag(parser, parser.tokens[0], tag_spec=spec)
|
|
|
|
ctx = {"myvar": {"a": "b"}, "val2": 1}
|
|
args = safe_resolve_list(ctx, tag.args)
|
|
named_args = safe_resolve_dict(ctx, tag.named_args)
|
|
kwargs = tag.kwargs.resolve(ctx)
|
|
|
|
self.assertListEqual(args, [])
|
|
self.assertDictEqual(named_args, {})
|
|
self.assertDictEqual(kwargs, {"num": 42, "var": {"a": "b"}, "key": "val", "key2": 1})
|
|
|
|
def test_parses_special_kwargs(self):
|
|
template_str = "{% component date=date @lol=2 na-me=bzz @event:na-me.mod=bzz #my-id=True %}"
|
|
tokens = Lexer(template_str).tokenize()
|
|
parser = Parser(tokens=tokens)
|
|
spec = TagSpec(tag="component", keywordonly_args=True)
|
|
tag = _parse_tag(parser, parser.tokens[0], tag_spec=spec)
|
|
|
|
ctx = Context({"date": 2024, "bzz": "fzz"})
|
|
args = safe_resolve_list(ctx, tag.args)
|
|
kwargs = tag.kwargs.resolve(ctx)
|
|
|
|
self.assertListEqual(args, [])
|
|
self.assertDictEqual(
|
|
kwargs,
|
|
{
|
|
"@event": {"na-me.mod": "fzz"},
|
|
"@lol": 2,
|
|
"date": 2024,
|
|
"na-me": "fzz",
|
|
"#my-id": True,
|
|
},
|
|
)
|
|
|
|
|
|
class ParserComponentTest(BaseTestCase):
|
|
class SimpleComponent(Component):
|
|
template: types.django_html = """
|
|
{{ date }}
|
|
{{ id }}
|
|
{{ on_click }}
|
|
"""
|
|
|
|
def get_context_data(self, **kwargs):
|
|
return {
|
|
"date": kwargs["my-date"],
|
|
"id": kwargs["#some_id"],
|
|
"on_click": kwargs["@click.native"],
|
|
}
|
|
|
|
@parametrize_context_behavior(["django", "isolated"])
|
|
def test_special_chars_accessible_via_kwargs(self):
|
|
registry.register("test", self.SimpleComponent)
|
|
|
|
template_str: types.django_html = """
|
|
{% load component_tags %}
|
|
{% component "test" my-date="2015-06-19" @click.native=do_something #some_id=True %}
|
|
{% endcomponent %}
|
|
"""
|
|
template = Template(template_str)
|
|
rendered = template.render(Context({"do_something": "abc"}))
|
|
self.assertHTMLEqual(
|
|
rendered,
|
|
"""
|
|
2015-06-19
|
|
True
|
|
abc
|
|
""",
|
|
)
|
|
|
|
|
|
class AggregateKwargsTest(BaseTestCase):
|
|
def test_aggregate_kwargs(self):
|
|
processed = process_aggregate_kwargs(
|
|
{
|
|
"attrs:@click.stop": "dispatch('click_event')",
|
|
"attrs:x-data": "{hello: 'world'}",
|
|
"attrs:class": "class_var",
|
|
"my_dict:one": 2,
|
|
"three": "four",
|
|
":placeholder": "No text",
|
|
}
|
|
)
|
|
|
|
self.assertDictEqual(
|
|
processed,
|
|
{
|
|
"attrs": {
|
|
"@click.stop": "dispatch('click_event')",
|
|
"x-data": "{hello: 'world'}",
|
|
"class": "class_var",
|
|
},
|
|
"my_dict": {"one": 2},
|
|
"three": "four",
|
|
":placeholder": "No text",
|
|
},
|
|
)
|
|
|
|
def is_aggregate_key(self):
|
|
self.assertEqual(is_aggregate_key(""), False)
|
|
self.assertEqual(is_aggregate_key(" "), False)
|
|
self.assertEqual(is_aggregate_key(" : "), False)
|
|
self.assertEqual(is_aggregate_key("attrs"), False)
|
|
self.assertEqual(is_aggregate_key(":attrs"), False)
|
|
self.assertEqual(is_aggregate_key(" :attrs "), False)
|
|
self.assertEqual(is_aggregate_key("attrs:"), False)
|
|
self.assertEqual(is_aggregate_key(":attrs:"), False)
|
|
self.assertEqual(is_aggregate_key("at:trs"), True)
|
|
self.assertEqual(is_aggregate_key(":at:trs"), False)
|