From aa012e6f09413ce6611f26e75c51cd838574aac7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Wed, 3 Jan 2024 21:23:10 +0100 Subject: [PATCH 1/4] allow disabled- and form-Attribute on fieldsets MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Müller --- src/View/Helper/FormCollection.php | 4 ++- test/View/Helper/FormCollectionTest.php | 42 +++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/View/Helper/FormCollection.php b/src/View/Helper/FormCollection.php index 23b055d98..0718ee514 100644 --- a/src/View/Helper/FormCollection.php +++ b/src/View/Helper/FormCollection.php @@ -25,7 +25,9 @@ class FormCollection extends AbstractHelper * @var array */ protected $validTagAttributes = [ - 'name' => true, + 'name' => true, + 'disabled' => true, + 'form' => true, ]; /** diff --git a/test/View/Helper/FormCollectionTest.php b/test/View/Helper/FormCollectionTest.php index e93e353b1..9b6f44d10 100644 --- a/test/View/Helper/FormCollectionTest.php +++ b/test/View/Helper/FormCollectionTest.php @@ -462,4 +462,46 @@ public static function provideDoctypesAndPermitFlagForNameAttribute(): array [Doctype::CUSTOM, false], ]; } + + /** + * @dataProvider provideDoctypesAndPermitFlagForDisabledAttribute + */ + public function testRenderCollectionWithDisabledAttribute( + string $doctype, + bool $allowsShortAttribute + ): void { + $this->helper->setDoctype($doctype); + + $form = $this->getForm(); + $collection = $form->get('colors'); + $collection->setAttribute('disabled', true); + + $markup = $this->helper->render($collection); + + if ($allowsShortAttribute) { + self::assertStringContainsString('
', $markup); + } elseif ($doctype === Doctype::XHTML5) { + self::assertStringContainsString('
', $markup); + } else { + self::assertStringContainsString('
', $markup); + } + } + + public static function provideDoctypesAndPermitFlagForDisabledAttribute(): array + { + return [ + [Doctype::XHTML11, false], + [Doctype::XHTML1_STRICT, false], + [Doctype::XHTML1_TRANSITIONAL, false], + [Doctype::XHTML1_FRAMESET, false], + [Doctype::XHTML1_RDFA, false], + [Doctype::XHTML1_RDFA11, false], + [Doctype::XHTML_BASIC1, false], + [Doctype::XHTML5, false], + [Doctype::HTML4_STRICT, false], + [Doctype::HTML4_LOOSE, false], + [Doctype::HTML4_FRAMESET, false], + [Doctype::HTML5, true], + ]; + } } From e4d74cfe58caa974d91a6086fd2bf8c70b9c338f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Thu, 4 Jan 2024 13:39:58 +0100 Subject: [PATCH 2/4] update handling for HTML5 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Müller --- src/View/Helper/FormCollection.php | 14 ++++++-------- test/View/Helper/FormCollectionTest.php | 5 +---- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/src/View/Helper/FormCollection.php b/src/View/Helper/FormCollection.php index 0718ee514..65452b574 100644 --- a/src/View/Helper/FormCollection.php +++ b/src/View/Helper/FormCollection.php @@ -8,7 +8,6 @@ use Laminas\Form\ElementInterface; use Laminas\Form\FieldsetInterface; use Laminas\Form\LabelAwareInterface; -use Laminas\View\Helper\Doctype; use Laminas\View\Helper\HelperInterface; use RuntimeException; @@ -79,11 +78,6 @@ class FormCollection extends AbstractHelper */ protected $fieldsetHelper; - private array $doctypesAllowedToHaveNameAttribute = [ - Doctype::HTML5 => true, - Doctype::XHTML5 => true, - ]; - /** * Invoke helper as function * @@ -138,8 +132,12 @@ public function render(ElementInterface $element): string // Every collection is wrapped by a fieldset if needed if ($this->shouldWrap) { $attributes = $element->getAttributes(); - if (! isset($this->doctypesAllowedToHaveNameAttribute[$this->getDoctype()])) { - unset($attributes['name']); + if (! $this->getDoctypeHelper()->isHtml5()) { + unset( + $attributes['name'], + $attributes['disabled'], + $attributes['form'] + ); } $attributesString = $attributes !== [] ? ' ' . $this->createAttributesString($attributes) : ''; diff --git a/test/View/Helper/FormCollectionTest.php b/test/View/Helper/FormCollectionTest.php index 9b6f44d10..6b699672b 100644 --- a/test/View/Helper/FormCollectionTest.php +++ b/test/View/Helper/FormCollectionTest.php @@ -411,7 +411,6 @@ public function testCanDisableLabelHtmlEscape(): void public function testForElementHelperNotInstanceOfHelperInterface(): void { $method = new ReflectionMethod(FormCollectionHelper::class, 'getElementHelper'); - $method->setAccessible(true); $this->expectException(RuntimeException::class); $this->expectExceptionMessage( @@ -458,8 +457,6 @@ public static function provideDoctypesAndPermitFlagForNameAttribute(): array [Doctype::HTML4_LOOSE, false], [Doctype::HTML4_FRAMESET, false], [Doctype::HTML5, true], - [Doctype::CUSTOM_XHTML, false], - [Doctype::CUSTOM, false], ]; } @@ -483,7 +480,7 @@ public function testRenderCollectionWithDisabledAttribute( } elseif ($doctype === Doctype::XHTML5) { self::assertStringContainsString('
', $markup); } else { - self::assertStringContainsString('
', $markup); + self::assertStringContainsString('
', $markup); } } From 79adda0484683181288c77bb9679458a309d9f1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Thu, 4 Jan 2024 16:25:38 +0100 Subject: [PATCH 3/4] update test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Müller --- test/View/Helper/FormCollectionTest.php | 83 ++++++++++++++++++++----- 1 file changed, 67 insertions(+), 16 deletions(-) diff --git a/test/View/Helper/FormCollectionTest.php b/test/View/Helper/FormCollectionTest.php index 6b699672b..b000481fb 100644 --- a/test/View/Helper/FormCollectionTest.php +++ b/test/View/Helper/FormCollectionTest.php @@ -465,6 +465,7 @@ public static function provideDoctypesAndPermitFlagForNameAttribute(): array */ public function testRenderCollectionWithDisabledAttribute( string $doctype, + bool $allowsNameAttribute, bool $allowsShortAttribute ): void { $this->helper->setDoctype($doctype); @@ -475,10 +476,12 @@ public function testRenderCollectionWithDisabledAttribute( $markup = $this->helper->render($collection); - if ($allowsShortAttribute) { - self::assertStringContainsString('
', $markup); - } elseif ($doctype === Doctype::XHTML5) { - self::assertStringContainsString('
', $markup); + if ($allowsNameAttribute) { + if ($allowsShortAttribute) { + self::assertStringContainsString('
', $markup); + } else { + self::assertStringContainsString('
', $markup); + } } else { self::assertStringContainsString('
', $markup); } @@ -487,18 +490,66 @@ public function testRenderCollectionWithDisabledAttribute( public static function provideDoctypesAndPermitFlagForDisabledAttribute(): array { return [ - [Doctype::XHTML11, false], - [Doctype::XHTML1_STRICT, false], - [Doctype::XHTML1_TRANSITIONAL, false], - [Doctype::XHTML1_FRAMESET, false], - [Doctype::XHTML1_RDFA, false], - [Doctype::XHTML1_RDFA11, false], - [Doctype::XHTML_BASIC1, false], - [Doctype::XHTML5, false], - [Doctype::HTML4_STRICT, false], - [Doctype::HTML4_LOOSE, false], - [Doctype::HTML4_FRAMESET, false], - [Doctype::HTML5, true], + [ + 'doctype' => Doctype::XHTML11, + 'allowsNameAttribute' => false, + 'allowsShortAttribute' => false, + ], + [ + 'doctype' => Doctype::XHTML1_STRICT, + 'allowsNameAttribute' => false, + 'allowsShortAttribute' => false, + ], + [ + 'doctype' => Doctype::XHTML1_TRANSITIONAL, + 'allowsNameAttribute' => false, + 'allowsShortAttribute' => false, + ], + [ + 'doctype' => Doctype::XHTML1_FRAMESET, + 'allowsNameAttribute' => false, + 'allowsShortAttribute' => false, + ], + [ + 'doctype' => Doctype::XHTML1_RDFA, + 'allowsNameAttribute' => false, + 'allowsShortAttribute' => false, + ], + [ + 'doctype' => Doctype::XHTML1_RDFA11, + 'allowsNameAttribute' => false, + 'allowsShortAttribute' => false, + ], + [ + 'doctype' => Doctype::XHTML_BASIC1, + 'allowsNameAttribute' => false, + 'allowsShortAttribute' => false, + ], + [ + 'doctype' => Doctype::XHTML5, + 'allowsNameAttribute' => true, + 'allowsShortAttribute' => false, + ], + [ + 'doctype' => Doctype::HTML4_STRICT, + 'allowsNameAttribute' => false, + 'allowsShortAttribute' => false, + ], + [ + 'doctype' => Doctype::HTML4_LOOSE, + 'allowsNameAttribute' => false, + 'allowsShortAttribute' => false, + ], + [ + 'doctype' => Doctype::HTML4_FRAMESET, + 'allowsNameAttribute' => false, + 'allowsShortAttribute' => false, + ], + [ + 'doctype' => Doctype::HTML5, + 'allowsNameAttribute' => true, + 'allowsShortAttribute' => true, + ], ]; } } From 227047d02bff586d6ec83b39a39a14770b6276bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Thu, 4 Jan 2024 16:28:58 +0100 Subject: [PATCH 4/4] add test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Müller --- test/View/Helper/FormCollectionTest.php | 39 +++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/test/View/Helper/FormCollectionTest.php b/test/View/Helper/FormCollectionTest.php index b000481fb..b4033cec1 100644 --- a/test/View/Helper/FormCollectionTest.php +++ b/test/View/Helper/FormCollectionTest.php @@ -552,4 +552,43 @@ public static function provideDoctypesAndPermitFlagForDisabledAttribute(): array ], ]; } + + /** + * @dataProvider provideDoctypesAndPermitFlagForFormAttribute + */ + public function testRenderCollectionWithFormAttributeAndDoctypeHtml5( + string $doctype, + bool $allowsFormAttribute + ): void { + $this->helper->setDoctype($doctype); + + $form = $this->getForm(); + $collection = $form->get('colors'); + $collection->setAttribute('form', 'foo'); + + $markup = $this->helper->render($collection); + if ($allowsFormAttribute) { + self::assertStringContainsString('
', $markup); + } else { + self::assertStringContainsString('
', $markup); + } + } + + public static function provideDoctypesAndPermitFlagForFormAttribute(): array + { + return [ + [Doctype::XHTML11, false], + [Doctype::XHTML1_STRICT, false], + [Doctype::XHTML1_TRANSITIONAL, false], + [Doctype::XHTML1_FRAMESET, false], + [Doctype::XHTML1_RDFA, false], + [Doctype::XHTML1_RDFA11, false], + [Doctype::XHTML_BASIC1, false], + [Doctype::XHTML5, true], + [Doctype::HTML4_STRICT, false], + [Doctype::HTML4_LOOSE, false], + [Doctype::HTML4_FRAMESET, false], + [Doctype::HTML5, true], + ]; + } }