From 3d83150eb0f03b43271d3e8574158b8fc1d90629 Mon Sep 17 00:00:00 2001 From: Francis Secada Date: Wed, 19 Aug 2026 13:02:16 -0400 Subject: [PATCH 1/3] feat(select,textarea,form-field): roll out attrs passthrough (#76) Extends the attrs passthrough mechanism landed on Button (#72) to Select, Textarea, and FormField across all five themes, always targeting the control element rather than the field wrapper. - primitives.py: RESERVED_ATTRS entries for select/textarea/form-field - render_attrs() now unwraps a JinjaX HTMLAttrs object via .as_dict before validating, since JinjaX's real Catalog unconditionally overwrites a component's `attrs` prop with its own extra-kwargs collector on every render (fixes a live crash in test_form_field_endpoint_renders_input under the real Catalog) - All 15 jinja templates + 15 cotton theme partials + 3 cotton wrappers wire attrs onto the control (select/textarea/input), never the field wrapper div - Jinja templates without prior StrictUndefined guards on other props (bulma) get one scoped to the new attrs prop only Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01Ur2c6d3peuerUGgRdJ6Fvs --- src/cf_ui/primitives.py | 14 ++ .../cotton/_themes/bootstrap/form-field.html | 2 + .../cotton/_themes/bootstrap/select.html | 2 + .../cotton/_themes/bootstrap/textarea.html | 2 + .../cotton/_themes/bulma/form-field.html | 2 + .../cotton/_themes/bulma/select.html | 2 + .../cotton/_themes/bulma/textarea.html | 2 + .../cotton/_themes/daisy/form-field.html | 2 + .../cotton/_themes/daisy/select.html | 2 + .../cotton/_themes/daisy/textarea.html | 2 + .../cotton/_themes/fomantic/form-field.html | 2 + .../cotton/_themes/fomantic/select.html | 2 + .../cotton/_themes/fomantic/textarea.html | 2 + .../cotton/_themes/foundation/form-field.html | 2 + .../cotton/_themes/foundation/select.html | 2 + .../cotton/_themes/foundation/textarea.html | 2 + src/cf_ui/templates/cotton/cf/form-field.html | 1 + src/cf_ui/templates/cotton/cf/select.html | 11 +- src/cf_ui/templates/cotton/cf/textarea.html | 11 +- .../templates/jinja/bootstrap/FormField.jinja | 4 +- .../templates/jinja/bootstrap/Select.jinja | 4 +- .../templates/jinja/bootstrap/Textarea.jinja | 4 +- .../templates/jinja/bulma/FormField.jinja | 4 +- src/cf_ui/templates/jinja/bulma/Select.jinja | 4 +- .../templates/jinja/bulma/Textarea.jinja | 4 +- .../templates/jinja/daisy/FormField.jinja | 4 +- src/cf_ui/templates/jinja/daisy/Select.jinja | 4 +- .../templates/jinja/daisy/Textarea.jinja | 4 +- .../templates/jinja/fomantic/FormField.jinja | 4 +- .../templates/jinja/fomantic/Select.jinja | 4 +- .../templates/jinja/fomantic/Textarea.jinja | 4 +- .../jinja/foundation/FormField.jinja | 4 +- .../templates/jinja/foundation/Select.jinja | 4 +- .../templates/jinja/foundation/Textarea.jinja | 4 +- ...t_textarea_form_field_attrs_passthrough.py | 118 +++++++++++++ tests/unit/jinja/conftest.py | 5 +- tests/unit/jinja/test_bootstrap.py | 3 + tests/unit/jinja/test_daisy.py | 3 + tests/unit/jinja/test_fomantic.py | 3 + tests/unit/jinja/test_foundation.py | 3 + ...t_textarea_form_field_attrs_passthrough.py | 163 ++++++++++++++++++ 41 files changed, 407 insertions(+), 18 deletions(-) create mode 100644 tests/unit/cotton/test_select_textarea_form_field_attrs_passthrough.py create mode 100644 tests/unit/jinja/test_select_textarea_form_field_attrs_passthrough.py diff --git a/src/cf_ui/primitives.py b/src/cf_ui/primitives.py index 7e43b22..0ce3ef3 100644 --- a/src/cf_ui/primitives.py +++ b/src/cf_ui/primitives.py @@ -799,6 +799,9 @@ def validate(component: str, **axes: Any) -> str: #: A primitive not listed here has not adopted ``attrs`` yet. RESERVED_ATTRS: dict[str, frozenset[str]] = { "button": frozenset({"class", "type", "href", "role", "aria-disabled", "disabled"}), + "select": frozenset({"id", "name", "class"}), + "textarea": frozenset({"id", "name", "class", "rows"}), + "form-field": frozenset({"id", "name", "class", "type", "value", "required"}), } @@ -850,6 +853,17 @@ def render_attrs(component: str, attrs: dict[str, str] | None) -> _SafeAttrs: character set, or one that collides with a name the component already renders. """ + if hasattr(attrs, "as_dict"): + # A real JinjaX ``Catalog`` render reaches here with a JinjaX + # ``HTMLAttrs`` object, not the plain dict the rest of this function + # expects: JinjaX reserves the prop name ``attrs`` for its own + # extra-kwargs collector and overwrites whatever a component's own + # `{#def}` declared for it (jinjax's ``catalog.py``, unconditionally, + # for every component). ``.as_dict`` is JinjaX's own escape hatch for + # this, and cf-ui's validation/escaping below applies identically + # once it holds a plain dict. + attrs = attrs.as_dict + if not attrs: return _SafeAttrs("") diff --git a/src/cf_ui/templates/cotton/_themes/bootstrap/form-field.html b/src/cf_ui/templates/cotton/_themes/bootstrap/form-field.html index 9ec312f..b46f14a 100644 --- a/src/cf_ui/templates/cotton/_themes/bootstrap/form-field.html +++ b/src/cf_ui/templates/cotton/_themes/bootstrap/form-field.html @@ -1,3 +1,4 @@ +{% load cf_ui %}
{% if error %}
{{ error }}
{% endif %}
diff --git a/src/cf_ui/templates/cotton/_themes/bootstrap/select.html b/src/cf_ui/templates/cotton/_themes/bootstrap/select.html index c9c4a26..bf703d1 100644 --- a/src/cf_ui/templates/cotton/_themes/bootstrap/select.html +++ b/src/cf_ui/templates/cotton/_themes/bootstrap/select.html @@ -1,9 +1,11 @@ +{% load cf_ui %}
{% if error %}
{{ error }}
{% endif %}
diff --git a/src/cf_ui/templates/cotton/_themes/bulma/form-field.html b/src/cf_ui/templates/cotton/_themes/bulma/form-field.html index 8bea0ad..fc84b26 100644 --- a/src/cf_ui/templates/cotton/_themes/bulma/form-field.html +++ b/src/cf_ui/templates/cotton/_themes/bulma/form-field.html @@ -1,3 +1,4 @@ +{% load cf_ui %}
@@ -8,6 +9,7 @@ name="{{ name }}" value="{{ value }}" {% if required == "true" %}required{% endif %} + {% cf_ui_render_attrs "form-field" attrs %} >
{% if error %}

{{ error }}

{% endif %} diff --git a/src/cf_ui/templates/cotton/_themes/bulma/select.html b/src/cf_ui/templates/cotton/_themes/bulma/select.html index b1d683f..42b835f 100644 --- a/src/cf_ui/templates/cotton/_themes/bulma/select.html +++ b/src/cf_ui/templates/cotton/_themes/bulma/select.html @@ -1,3 +1,4 @@ +{% load cf_ui %}
@@ -6,6 +7,7 @@ id="{{ name }}" name="{{ name }}" {% if input_class %} class="{{ input_class }}"{% endif %} + {% cf_ui_render_attrs "select" attrs %} > {% for opt in options %}
{% comment %} Not `
`: `.ui.form .error.message` is diff --git a/src/cf_ui/templates/cotton/_themes/fomantic/select.html b/src/cf_ui/templates/cotton/_themes/fomantic/select.html index 0bb3dc8..774a835 100644 --- a/src/cf_ui/templates/cotton/_themes/fomantic/select.html +++ b/src/cf_ui/templates/cotton/_themes/fomantic/select.html @@ -1,3 +1,4 @@ +{% load cf_ui %} {% comment %} A real ` {% if error %} {{ error }} diff --git a/src/cf_ui/templates/cotton/_themes/foundation/select.html b/src/cf_ui/templates/cotton/_themes/foundation/select.html index b3e393a..2213f21 100644 --- a/src/cf_ui/templates/cotton/_themes/foundation/select.html +++ b/src/cf_ui/templates/cotton/_themes/foundation/select.html @@ -1,3 +1,4 @@ +{% load cf_ui %}
{% if error %} {{ error }} diff --git a/src/cf_ui/templates/cotton/cf/form-field.html b/src/cf_ui/templates/cotton/cf/form-field.html index 8a0b66d..578964e 100644 --- a/src/cf_ui/templates/cotton/cf/form-field.html +++ b/src/cf_ui/templates/cotton/cf/form-field.html @@ -7,6 +7,7 @@ required="false" input_class="" class="" + attrs="{}" /> {% load cf_ui %} {% cf_ui_theme_path "form-field" as cf_ui_partial %} diff --git a/src/cf_ui/templates/cotton/cf/select.html b/src/cf_ui/templates/cotton/cf/select.html index 11492d4..66f9c20 100644 --- a/src/cf_ui/templates/cotton/cf/select.html +++ b/src/cf_ui/templates/cotton/cf/select.html @@ -1,4 +1,13 @@ - + {% load cf_ui %} {% cf_ui_theme_path "select" as cf_ui_partial %} {% include cf_ui_partial %} diff --git a/src/cf_ui/templates/cotton/cf/textarea.html b/src/cf_ui/templates/cotton/cf/textarea.html index 1099a36..0ab4639 100644 --- a/src/cf_ui/templates/cotton/cf/textarea.html +++ b/src/cf_ui/templates/cotton/cf/textarea.html @@ -1,4 +1,13 @@ - + {% load cf_ui %} {% cf_ui_theme_path "textarea" as cf_ui_partial %} {% include cf_ui_partial %} diff --git a/src/cf_ui/templates/jinja/bootstrap/FormField.jinja b/src/cf_ui/templates/jinja/bootstrap/FormField.jinja index 12ee3e1..8edfeba 100644 --- a/src/cf_ui/templates/jinja/bootstrap/FormField.jinja +++ b/src/cf_ui/templates/jinja/bootstrap/FormField.jinja @@ -1,4 +1,4 @@ -{#def name, label, value="", error="", type="text", required=false, extra_class="", input_class="" #} +{#def name, label, value="", error="", type="text", required=false, extra_class="", input_class="", attrs={} #} {% autoescape true %} {% set value = value if value is defined else "" %} {% set error = error if error is defined else "" %} @@ -6,6 +6,7 @@ {% set required = required if required is defined else false %} {% set extra_class = extra_class if extra_class is defined else "" %} {% set input_class = input_class if input_class is defined else "" %} + {% set attrs = attrs if attrs is defined else {} %}
{% if error %}
{{ error }}
{% endif %}
diff --git a/src/cf_ui/templates/jinja/bootstrap/Select.jinja b/src/cf_ui/templates/jinja/bootstrap/Select.jinja index 05bf4e9..332f039 100644 --- a/src/cf_ui/templates/jinja/bootstrap/Select.jinja +++ b/src/cf_ui/templates/jinja/bootstrap/Select.jinja @@ -1,16 +1,18 @@ -{#def name, label, value="", error="", options=[], extra_class="", input_class="" #} +{#def name, label, value="", error="", options=[], extra_class="", input_class="", attrs={} #} {% autoescape true %} {% set value = value if value is defined else "" %} {% set error = error if error is defined else "" %} {% set options = options if options is defined else [] %} {% set extra_class = extra_class if extra_class is defined else "" %} {% set input_class = input_class if input_class is defined else "" %} + {% set attrs = attrs if attrs is defined else {} %}
{% if error %}
{{ error }}
{% endif %}
diff --git a/src/cf_ui/templates/jinja/bulma/FormField.jinja b/src/cf_ui/templates/jinja/bulma/FormField.jinja index 82f166f..61e38d4 100644 --- a/src/cf_ui/templates/jinja/bulma/FormField.jinja +++ b/src/cf_ui/templates/jinja/bulma/FormField.jinja @@ -1,5 +1,6 @@ -{#def name, label, value="", error="", type="text", required=false, extra_class="", input_class="" #} +{#def name, label, value="", error="", type="text", required=false, extra_class="", input_class="", attrs={} #} {% autoescape true %} + {% set attrs = attrs if attrs is defined else {} %}
@@ -10,6 +11,7 @@ name="{{ name }}" value="{{ value }}" {% if required %} required{% endif %} + {{- cf_ui_render_attrs("form-field", attrs) }} >
{% if error %}

{{ error }}

{% endif %} diff --git a/src/cf_ui/templates/jinja/bulma/Select.jinja b/src/cf_ui/templates/jinja/bulma/Select.jinja index ed79d8f..1a8beb7 100644 --- a/src/cf_ui/templates/jinja/bulma/Select.jinja +++ b/src/cf_ui/templates/jinja/bulma/Select.jinja @@ -1,5 +1,6 @@ -{#def name, label, value="", error="", options=[], extra_class="", input_class="" #} +{#def name, label, value="", error="", options=[], extra_class="", input_class="", attrs={} #} {% autoescape true %} + {% set attrs = attrs if attrs is defined else {} %}
@@ -8,6 +9,7 @@ id="{{ name }}" name="{{ name }}" {% if input_class %} class="{{ input_class }}"{% endif %} + {{- cf_ui_render_attrs("select", attrs) }} > {% for opt in options %}
{# Not `
`: `.ui.form .error.message` is diff --git a/src/cf_ui/templates/jinja/fomantic/Select.jinja b/src/cf_ui/templates/jinja/fomantic/Select.jinja index 0c1cac1..6bf23a3 100644 --- a/src/cf_ui/templates/jinja/fomantic/Select.jinja +++ b/src/cf_ui/templates/jinja/fomantic/Select.jinja @@ -1,10 +1,11 @@ -{#def name, label, value="", error="", options=[], extra_class="", input_class="" #} +{#def name, label, value="", error="", options=[], extra_class="", input_class="", attrs={} #} {% autoescape true %} {% set value = value if value is defined else "" %} {% set error = error if error is defined else "" %} {% set options = options if options is defined else [] %} {% set extra_class = extra_class if extra_class is defined else "" %} {% set input_class = input_class if input_class is defined else "" %} + {% set attrs = attrs if attrs is defined else {} %} {# A real ` {% for opt in options %}