From cfece1239f65744a76e85640c7ba9a89e1ff9b53 Mon Sep 17 00:00:00 2001 From: Adam Kudrna Date: Tue, 9 Apr 2024 22:48:15 +0200 Subject: [PATCH] Fix(web-twig): Don't render the `name` attribute of `Checkbox` when empty --- .../components/Checkbox/Checkbox.twig | 3 +- .../__fixtures__/checkboxDefault.twig | 50 ++++++++++++++++++- .../checkboxDefault.twig.snap.html | 15 +++++- .../checkboxDefaultPure.twig.snap.html | 2 +- .../__snapshots__/checkboxItem.twig.snap.html | 2 +- 5 files changed, 66 insertions(+), 6 deletions(-) diff --git a/packages/web-twig/src/Resources/components/Checkbox/Checkbox.twig b/packages/web-twig/src/Resources/components/Checkbox/Checkbox.twig index 4240ed88d1..7a46474136 100644 --- a/packages/web-twig/src/Resources/components/Checkbox/Checkbox.twig +++ b/packages/web-twig/src/Resources/components/Checkbox/Checkbox.twig @@ -33,6 +33,7 @@ {# Attributes #} {%- set _checkedAttr = _isChecked ? 'checked' : null -%} {%- set _disabledAttr = _isDisabled ? 'disabled' : null -%} +{%- set _nameAttr = _name ? 'name="' ~ _name | escape('html_attr') ~ '"' : null -%} {%- set _requiredAttr = _isRequired ? 'required' : null -%} {%- set _valueAttr = _value ? 'value=' ~ _value : null -%} @@ -53,10 +54,10 @@ {{ inputProps(props, _allowedInputAttributes, _inputProps) }} type="checkbox" id="{{ _id }}" - name="{{ _name }}" class="{{ _inputClassName }}" {{ _checkedAttr }} {{ _disabledAttr }} + {{ _nameAttr | raw }}" {{ _requiredAttr }} {{ _valueAttr }} {# Intentionally without `raw` to prevent XSS. #} /> diff --git a/packages/web-twig/src/Resources/components/Checkbox/__tests__/__fixtures__/checkboxDefault.twig b/packages/web-twig/src/Resources/components/Checkbox/__tests__/__fixtures__/checkboxDefault.twig index e22285addd..efd59c286c 100644 --- a/packages/web-twig/src/Resources/components/Checkbox/__tests__/__fixtures__/checkboxDefault.twig +++ b/packages/web-twig/src/Resources/components/Checkbox/__tests__/__fixtures__/checkboxDefault.twig @@ -1,3 +1,49 @@ -{% set inputProps = { "data-validate": "true" } %} + + + + + + + - + + + + + + + +{% set inputProps = { "data-validate": "true" } %} + diff --git a/packages/web-twig/src/Resources/components/Checkbox/__tests__/__snapshots__/checkboxDefault.twig.snap.html b/packages/web-twig/src/Resources/components/Checkbox/__tests__/__snapshots__/checkboxDefault.twig.snap.html index 95b5a8a881..cdd2990938 100644 --- a/packages/web-twig/src/Resources/components/Checkbox/__tests__/__snapshots__/checkboxDefault.twig.snap.html +++ b/packages/web-twig/src/Resources/components/Checkbox/__tests__/__snapshots__/checkboxDefault.twig.snap.html @@ -5,6 +5,19 @@ - + + + + + + + + + diff --git a/packages/web-twig/src/Resources/components/Checkbox/__tests__/__snapshots__/checkboxDefaultPure.twig.snap.html b/packages/web-twig/src/Resources/components/Checkbox/__tests__/__snapshots__/checkboxDefaultPure.twig.snap.html index 3891ffa6be..07555e2aa6 100644 --- a/packages/web-twig/src/Resources/components/Checkbox/__tests__/__snapshots__/checkboxDefaultPure.twig.snap.html +++ b/packages/web-twig/src/Resources/components/Checkbox/__tests__/__snapshots__/checkboxDefaultPure.twig.snap.html @@ -5,7 +5,7 @@ -