From 83fa68221232b98b6f08154cac26f03dee1a0efd Mon Sep 17 00:00:00 2001 From: Francis Secada Date: Wed, 19 Aug 2026 11:32:43 -0400 Subject: [PATCH] spike(cotton): resolve django-cotton class readability with a capture tag Django's template language has no Jinja-style {%- -%} trim markers, so the class="..." if/elif chains in the five cotton Button theme partials could not be reformatted the way the Jinja side was (#74) without literal newlines leaking into the rendered class attribute. Add {% cf_ui_class %}...{% endcf_ui_class %} (cf_ui.py), a small capture tag that renders its nodelist normally (inheriting ambient autoescape) and collapses whitespace runs to a single space, stripped at both ends. Applied via the `as ` form so the readable, one-branch-per-line block sits above the actual HTML tag rather than inside its class attribute. Every class token stays literal source text in the .html file, satisfying the constraint that rules out computing classes in Python: Tailwind's scanner reads templates/**/*.{html,jinja}, never primitives.py, so a Python-computed class is silently tree-shaken out of a real daisyUI build. Verification: - Render-equivalence: all 2520 (theme x variant x size x state x full_width x href x class) combinations produce whitespace-equivalent output to the pre-change baseline. - The real django-cotton compiler (not render_to_string, which bypasses it) accepts through the new tag cleanly across all 5 themes, including defaults. - djlint accepts and stabilizes the multi-line block once its custom tag name is registered via `custom_blocks` in pyproject.toml -- idempotent reformat, 0 lint errors, both before and after. - New unit tests for the tag itself (whitespace collapse, autoescape inheritance, the `as` form, syntax errors) plus a parametrized regression test over all 5 button partials. Mutation-checked: reverting the tag's whitespace collapse, or the parity test's new scan region, turns the relevant tests red. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01Ur2c6d3peuerUGgRdJ6Fvs --- pyproject.toml | 5 + .../cotton/_themes/bootstrap/button.html | 35 +++++- .../cotton/_themes/bulma/button.html | 33 ++++- .../cotton/_themes/daisy/button.html | 35 +++++- .../cotton/_themes/fomantic/button.html | 35 +++++- .../cotton/_themes/foundation/button.html | 33 ++++- src/cf_ui/templatetags/cf_ui.py | 57 +++++++++ tests/unit/test_cf_ui_class_tag.py | 118 ++++++++++++++++++ tests/unit/test_primitives.py | 26 +++- 9 files changed, 366 insertions(+), 11 deletions(-) create mode 100644 tests/unit/test_cf_ui_class_tag.py diff --git a/pyproject.toml b/pyproject.toml index 81e3754..0d7aaf1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -153,6 +153,11 @@ indent-style = "space" profile = "django" indent = 2 single_attribute_per_line = true +# {% cf_ui_class %}...{% endcf_ui_class %} is a custom block tag (cf_ui.py) +# that lets a cotton theme partial write its class="..." if/elif chain one +# branch per line — Django has no {%- -%} trim markers, so without this the +# tag looks like an unmatched end tag (T038) to djlint's own parser. +custom_blocks = "cf_ui_class" # Both extensions are formatted. The Jinja tree is passed `--profile=jinja` # on its own hook invocation; this file-level setting cannot vary per path. extension = "html" diff --git a/src/cf_ui/templates/cotton/_themes/bootstrap/button.html b/src/cf_ui/templates/cotton/_themes/bootstrap/button.html index 33b003a..f066148 100644 --- a/src/cf_ui/templates/cotton/_themes/bootstrap/button.html +++ b/src/cf_ui/templates/cotton/_themes/bootstrap/button.html @@ -1 +1,34 @@ -{% with cf_tag=href|yesno:"a,button" %}<{{ cf_tag }} class="btn{% if variant == 'primary' %} btn-primary{% elif variant == 'secondary' %} btn-secondary{% elif variant == 'success' %} btn-success{% elif variant == 'warning' %} btn-warning{% elif variant == 'danger' %} btn-danger{% elif variant == 'info' %} btn-info{% elif variant == 'neutral' %} btn-light{% endif %}{% if size == 'small' %} btn-sm{% elif size == 'large' %} btn-lg{% endif %}{% if state == 'disabled' %} disabled{% endif %}{% if full_width %} w-100{% endif %}{% if class %} {{ class }}{% endif %}"{% if href %}{% if state == 'disabled' %} role="link" aria-disabled="true"{% else %} href="{{ href }}"{% endif %}{% else %} type="{{ type }}"{% if state == 'disabled' %} disabled{% endif %}{% endif %}>{% if state == 'loading' %} {% endif %}{{ slot }}{% endwith %} +{% load cf_ui %} +{% cf_ui_class as cf_class %} + btn + {% if variant == 'primary' %} + btn-primary + {% elif variant == 'secondary' %} + btn-secondary + {% elif variant == 'success' %} + btn-success + {% elif variant == 'warning' %} + btn-warning + {% elif variant == 'danger' %} + btn-danger + {% elif variant == 'info' %} + btn-info + {% elif variant == 'neutral' %} + btn-light + {% endif %} + {% if size == 'small' %} + btn-sm + {% elif size == 'large' %} + btn-lg + {% endif %} + {% if state == 'disabled' %} + disabled + {% endif %} + {% if full_width %} + w-100 + {% endif %} + {% if class %} + {{ class }} + {% endif %} +{% endcf_ui_class %} +{% with cf_tag=href|yesno:"a,button" %}<{{ cf_tag }} class="{{ cf_class }}"{% if href %}{% if state == 'disabled' %} role="link" aria-disabled="true"{% else %} href="{{ href }}"{% endif %}{% else %} type="{{ type }}"{% if state == 'disabled' %} disabled{% endif %}{% endif %}>{% if state == 'loading' %} {% endif %}{{ slot }}{% endwith %} diff --git a/src/cf_ui/templates/cotton/_themes/bulma/button.html b/src/cf_ui/templates/cotton/_themes/bulma/button.html index 27026bf..ae47981 100644 --- a/src/cf_ui/templates/cotton/_themes/bulma/button.html +++ b/src/cf_ui/templates/cotton/_themes/bulma/button.html @@ -1 +1,32 @@ -{% with cf_tag=href|yesno:"a,button" %}<{{ cf_tag }} class="button{% if variant == 'primary' %} is-primary{% elif variant == 'secondary' %} is-link{% elif variant == 'success' %} is-success{% elif variant == 'warning' %} is-warning{% elif variant == 'danger' %} is-danger{% elif variant == 'info' %} is-info{% endif %}{% if size == 'small' %} is-small{% elif size == 'large' %} is-large{% endif %}{% if state == 'loading' %} is-loading{% endif %}{% if full_width %} is-fullwidth{% endif %}{% if class %} {{ class }}{% endif %}"{% if href %}{% if state == 'disabled' %} role="link" aria-disabled="true"{% else %} href="{{ href }}"{% endif %}{% else %} type="{{ type }}"{% if state == 'disabled' %} disabled{% endif %}{% endif %}>{{ slot }}{% endwith %} +{% load cf_ui %} +{% cf_ui_class as cf_class %} + button + {% if variant == 'primary' %} + is-primary + {% elif variant == 'secondary' %} + is-link + {% elif variant == 'success' %} + is-success + {% elif variant == 'warning' %} + is-warning + {% elif variant == 'danger' %} + is-danger + {% elif variant == 'info' %} + is-info + {% endif %} + {% if size == 'small' %} + is-small + {% elif size == 'large' %} + is-large + {% endif %} + {% if state == 'loading' %} + is-loading + {% endif %} + {% if full_width %} + is-fullwidth + {% endif %} + {% if class %} + {{ class }} + {% endif %} +{% endcf_ui_class %} +{% with cf_tag=href|yesno:"a,button" %}<{{ cf_tag }} class="{{ cf_class }}"{% if href %}{% if state == 'disabled' %} role="link" aria-disabled="true"{% else %} href="{{ href }}"{% endif %}{% else %} type="{{ type }}"{% if state == 'disabled' %} disabled{% endif %}{% endif %}>{{ slot }}{% endwith %} diff --git a/src/cf_ui/templates/cotton/_themes/daisy/button.html b/src/cf_ui/templates/cotton/_themes/daisy/button.html index f804323..0758230 100644 --- a/src/cf_ui/templates/cotton/_themes/daisy/button.html +++ b/src/cf_ui/templates/cotton/_themes/daisy/button.html @@ -1 +1,34 @@ -{% with cf_tag=href|yesno:"a,button" %}<{{ cf_tag }} class="btn{% if variant == 'primary' %} btn-primary{% elif variant == 'secondary' %} btn-secondary{% elif variant == 'success' %} btn-success{% elif variant == 'warning' %} btn-warning{% elif variant == 'danger' %} btn-error{% elif variant == 'info' %} btn-info{% elif variant == 'neutral' %} btn-neutral{% endif %}{% if size == 'small' %} btn-sm{% elif size == 'large' %} btn-lg{% endif %}{% if state == 'disabled' %} btn-disabled{% endif %}{% if full_width %} btn-block{% endif %}{% if class %} {{ class }}{% endif %}"{% if href %}{% if state == 'disabled' %} role="link" aria-disabled="true"{% else %} href="{{ href }}"{% endif %}{% else %} type="{{ type }}"{% if state == 'disabled' %} disabled{% endif %}{% endif %}>{% if state == 'loading' %} {% endif %}{{ slot }}{% endwith %} +{% load cf_ui %} +{% cf_ui_class as cf_class %} + btn + {% if variant == 'primary' %} + btn-primary + {% elif variant == 'secondary' %} + btn-secondary + {% elif variant == 'success' %} + btn-success + {% elif variant == 'warning' %} + btn-warning + {% elif variant == 'danger' %} + btn-error + {% elif variant == 'info' %} + btn-info + {% elif variant == 'neutral' %} + btn-neutral + {% endif %} + {% if size == 'small' %} + btn-sm + {% elif size == 'large' %} + btn-lg + {% endif %} + {% if state == 'disabled' %} + btn-disabled + {% endif %} + {% if full_width %} + btn-block + {% endif %} + {% if class %} + {{ class }} + {% endif %} +{% endcf_ui_class %} +{% with cf_tag=href|yesno:"a,button" %}<{{ cf_tag }} class="{{ cf_class }}"{% if href %}{% if state == 'disabled' %} role="link" aria-disabled="true"{% else %} href="{{ href }}"{% endif %}{% else %} type="{{ type }}"{% if state == 'disabled' %} disabled{% endif %}{% endif %}>{% if state == 'loading' %} {% endif %}{{ slot }}{% endwith %} diff --git a/src/cf_ui/templates/cotton/_themes/fomantic/button.html b/src/cf_ui/templates/cotton/_themes/fomantic/button.html index 6191bec..db47898 100644 --- a/src/cf_ui/templates/cotton/_themes/fomantic/button.html +++ b/src/cf_ui/templates/cotton/_themes/fomantic/button.html @@ -1 +1,34 @@ -{% with cf_tag=href|yesno:"a,button" %}<{{ cf_tag }} class="ui button{% if variant == 'primary' %} primary{% elif variant == 'secondary' %} secondary{% elif variant == 'success' %} positive{% elif variant == 'warning' %} yellow{% elif variant == 'danger' %} negative{% elif variant == 'info' %} teal{% endif %}{% if size == 'small' %} small{% elif size == 'large' %} large{% endif %}{% if state == 'loading' %} loading{% elif state == 'disabled' %} disabled{% endif %}{% if full_width %} fluid{% endif %}{% if class %} {{ class }}{% endif %}"{% if href %}{% if state == 'disabled' %} role="link" aria-disabled="true"{% else %} href="{{ href }}"{% endif %}{% else %} type="{{ type }}"{% if state == 'disabled' %} disabled{% endif %}{% endif %}>{{ slot }}{% endwith %} +{% load cf_ui %} +{% cf_ui_class as cf_class %} + ui button + {% if variant == 'primary' %} + primary + {% elif variant == 'secondary' %} + secondary + {% elif variant == 'success' %} + positive + {% elif variant == 'warning' %} + yellow + {% elif variant == 'danger' %} + negative + {% elif variant == 'info' %} + teal + {% endif %} + {% if size == 'small' %} + small + {% elif size == 'large' %} + large + {% endif %} + {% if state == 'loading' %} + loading + {% elif state == 'disabled' %} + disabled + {% endif %} + {% if full_width %} + fluid + {% endif %} + {% if class %} + {{ class }} + {% endif %} +{% endcf_ui_class %} +{% with cf_tag=href|yesno:"a,button" %}<{{ cf_tag }} class="{{ cf_class }}"{% if href %}{% if state == 'disabled' %} role="link" aria-disabled="true"{% else %} href="{{ href }}"{% endif %}{% else %} type="{{ type }}"{% if state == 'disabled' %} disabled{% endif %}{% endif %}>{{ slot }}{% endwith %} diff --git a/src/cf_ui/templates/cotton/_themes/foundation/button.html b/src/cf_ui/templates/cotton/_themes/foundation/button.html index 656dba9..c3d0a16 100644 --- a/src/cf_ui/templates/cotton/_themes/foundation/button.html +++ b/src/cf_ui/templates/cotton/_themes/foundation/button.html @@ -1 +1,32 @@ -{% with cf_tag=href|yesno:"a,button" %}<{{ cf_tag }} class="button{% if variant == 'primary' %} primary{% elif variant == 'secondary' %} secondary{% elif variant == 'success' %} success{% elif variant == 'warning' %} warning{% elif variant == 'danger' %} alert{% elif variant == 'info' %} secondary{% endif %}{% if size == 'small' %} small{% elif size == 'large' %} large{% endif %}{% if state == 'disabled' %} disabled{% endif %}{% if full_width %} expanded{% endif %}{% if class %} {{ class }}{% endif %}"{% if href %}{% if state == 'disabled' %} role="link" aria-disabled="true"{% else %} href="{{ href }}"{% endif %}{% else %} type="{{ type }}"{% if state == 'disabled' %} disabled{% endif %}{% endif %}>{{ slot }}{% endwith %} +{% load cf_ui %} +{% cf_ui_class as cf_class %} + button + {% if variant == 'primary' %} + primary + {% elif variant == 'secondary' %} + secondary + {% elif variant == 'success' %} + success + {% elif variant == 'warning' %} + warning + {% elif variant == 'danger' %} + alert + {% elif variant == 'info' %} + secondary + {% endif %} + {% if size == 'small' %} + small + {% elif size == 'large' %} + large + {% endif %} + {% if state == 'disabled' %} + disabled + {% endif %} + {% if full_width %} + expanded + {% endif %} + {% if class %} + {{ class }} + {% endif %} +{% endcf_ui_class %} +{% with cf_tag=href|yesno:"a,button" %}<{{ cf_tag }} class="{{ cf_class }}"{% if href %}{% if state == 'disabled' %} role="link" aria-disabled="true"{% else %} href="{{ href }}"{% endif %}{% else %} type="{{ type }}"{% if state == 'disabled' %} disabled{% endif %}{% endif %}>{{ slot }}{% endwith %} diff --git a/src/cf_ui/templatetags/cf_ui.py b/src/cf_ui/templatetags/cf_ui.py index 34e2f27..1daf64b 100644 --- a/src/cf_ui/templatetags/cf_ui.py +++ b/src/cf_ui/templatetags/cf_ui.py @@ -1,3 +1,5 @@ +import re + from django import template from django.conf import settings from django.templatetags.static import static @@ -10,6 +12,8 @@ register = template.Library() +_WHITESPACE_RE = re.compile(r"\s+") + _CDN_CSS = { "bulma": "https://cdn.jsdelivr.net/npm/bulma@{v}/css/bulma.min.css", "bootstrap": "https://cdn.jsdelivr.net/npm/bootstrap@{v}/dist/css/bootstrap.min.css", @@ -136,6 +140,59 @@ def cf_ui_validate(component: str, **axes) -> str: return validate_primitive(component, **axes) +class ClassCaptureNode(template.Node): + def __init__(self, nodelist, target_var=None): + self.nodelist = nodelist + self.target_var = target_var + + def render(self, context) -> str: + collapsed = _WHITESPACE_RE.sub(" ", self.nodelist.render(context)).strip() + if self.target_var: + context[self.target_var] = mark_safe(collapsed) + return "" + return mark_safe(collapsed) + + +@register.tag("cf_ui_class") +def cf_ui_class(parser, token): + """Collapse a multi-line ``{% if %}`` class chain to a single-space string. + + Django has no Jinja-style ``{%- -%}`` trim markers, so a chained + ``{% if %}...{% elif %}...{% endif %}`` class attribute has to stay on one + physical line — a literal newline anywhere in the block becomes literal + whitespace in the rendered ``class`` attribute. This tag lets the same + chain be written readably, one branch per line, by rendering its nodelist + normally (inheriting ambient autoescape — a hostile ``{{ class }}`` is + escaped exactly as it already was) and then collapsing every run of + whitespace between tokens to one space and stripping the ends:: + +